kontact

summarywidget.cpp

00001 /*
00002     This file is part of Kontact.
00003     Copyright (c) 2003 Tobias Koenig <tokoe@kde.org>
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018 
00019     As a special exception, permission is given to link this program
00020     with any edition of Qt, and distribute the resulting executable,
00021     without including the source code for Qt in the source distribution.
00022 */
00023 
00024 #include <qlabel.h>
00025 #include <qlayout.h>
00026 
00027 #include <kdialog.h>
00028 #include <kglobal.h>
00029 #include <kiconloader.h>
00030 #include <klocale.h>
00031 #include <kparts/part.h>
00032 #include <kstandarddirs.h>
00033 #include <kurllabel.h>
00034 #include <qtooltip.h>
00035 #include <libkcal/event.h>
00036 #include <libkcal/resourcecalendar.h>
00037 #include <libkcal/resourcelocal.h>
00038 #include <libkdepim/kpimprefs.h>
00039 
00040 #include "korganizeriface_stub.h"
00041 
00042 #include "core.h"
00043 #include "plugin.h"
00044 #include "korganizerplugin.h"
00045 
00046 #include "korganizer/stdcalendar.h"
00047 
00048 #include "summarywidget.h"
00049 
00050 SummaryWidget::SummaryWidget( KOrganizerPlugin *plugin, QWidget *parent,
00051                               const char *name )
00052   : Kontact::Summary( parent, name ), mPlugin( plugin ), mCalendar( 0 )
00053 {
00054   QVBoxLayout *mainLayout = new QVBoxLayout( this, 3, 3 );
00055 
00056   QPixmap icon = KGlobal::iconLoader()->loadIcon( "kontact_date",
00057                    KIcon::Desktop, KIcon::SizeMedium );
00058   QWidget *header = createHeader( this, icon, i18n( "Appointments" ) );
00059   mainLayout->addWidget( header );
00060 
00061   mLayout = new QGridLayout( mainLayout, 7, 5, 3 );
00062   mLayout->setRowStretch( 6, 1 );
00063 
00064   mCalendar = KOrg::StdCalendar::self();
00065   mCalendar->load();
00066 
00067   connect( mCalendar, SIGNAL( calendarChanged() ), SLOT( updateView() ) );
00068   connect( mPlugin->core(), SIGNAL( dayChanged( const QDate& ) ),
00069            SLOT( updateView() ) );
00070 
00071   updateView();
00072 }
00073 
00074 SummaryWidget::~SummaryWidget()
00075 {
00076 }
00077 
00078 void SummaryWidget::updateView()
00079 {
00080   mLabels.setAutoDelete( true );
00081   mLabels.clear();
00082   mLabels.setAutoDelete( false );
00083 
00084   KIconLoader loader( "kdepim" );
00085 
00086   KConfig config( "kcmkorgsummaryrc" );
00087 
00088   config.setGroup( "Calendar" );
00089   int days = config.readNumEntry( "DaysToShow", 1 );
00090 
00091   QLabel *label = 0;
00092   int counter = 0;
00093   QPixmap pm = loader.loadIcon( "appointment", KIcon::Small );
00094 
00095   QDate dt;
00096   QDate currentDate = QDate::currentDate();
00097   for ( dt=currentDate;
00098         dt<=currentDate.addDays( days - 1 );
00099         dt=dt.addDays(1) ) {
00100     KCal::Event::List events = mCalendar->events( dt );
00101 
00102     KCal::Event *ev;
00103     KCal::Event::List::ConstIterator it;
00104     QDateTime qdt;
00105 
00106     // Find recurring events, replacing the QDate with the currentDate
00107     for ( it=events.begin(); it!=events.end(); ++it ) {
00108       ev = *it;
00109       if ( ev->recursOn( dt ) ) {
00110         qdt = ev->dtStart();
00111         qdt.setDate( dt );
00112         ev->setDtStart( qdt );
00113       }
00114     }
00115 
00116     // sort the events for this date by summary
00117     events = KCal::Calendar::sortEvents( &events,
00118                                          KCal::EventSortSummary,
00119                                          KCal::SortDirectionAscending );
00120     // sort the events for this date by start date
00121     events = KCal::Calendar::sortEvents( &events,
00122                                          KCal::EventSortStartDate,
00123                                          KCal::SortDirectionAscending );
00124 
00125     for ( it=events.begin(); it!=events.end(); ++it ) {
00126       ev = *it;
00127 
00128       // Count number of days remaining in multiday event
00129       int span=1; int dayof=1;
00130       if ( ev->isMultiDay() ) {
00131         QDate d = ev->dtStart().date();
00132         if ( d < currentDate ) {
00133           d = currentDate;
00134         }
00135         while ( d < ev->dtEnd().date() ) {
00136           if ( d < dt ) {
00137             dayof++;
00138           }
00139           span++;
00140           d=d.addDays( 1 );
00141         }
00142       }
00143 
00144       // If this date is part of a floating, multiday event, then we
00145       // only make a print for the first day of the event.
00146       if ( ev->isMultiDay() && ev->doesFloat() && dayof != 1 ) continue;
00147 
00148       // Fill Appointment Pixmap Field
00149       label = new QLabel( this );
00150       label->setPixmap( pm );
00151       label->setMaximumWidth( label->minimumSizeHint().width() );
00152       label->setAlignment( AlignVCenter );
00153       mLayout->addWidget( label, counter, 0 );
00154       mLabels.append( label );
00155 
00156       // Fill Event Date Field
00157       bool makeBold = false;
00158       QString datestr;
00159 
00160       // Modify event date for printing
00161       QDate sD = QDate::QDate( dt.year(), dt.month(), dt.day() );
00162       if ( ( sD.month() == currentDate.month() ) &&
00163            ( sD.day()   == currentDate.day() ) ) {
00164         datestr = i18n( "Today" );
00165         makeBold = true;
00166       } else if ( ( sD.month() == currentDate.addDays( 1 ).month() ) &&
00167                   ( sD.day()   == currentDate.addDays( 1 ).day() ) ) {
00168         datestr = i18n( "Tomorrow" );
00169       } else {
00170         datestr = KGlobal::locale()->formatDate( sD );
00171       }
00172 
00173       // Print the date span for multiday, floating events, for the
00174       // first day of the event only.
00175       if ( ev->isMultiDay() && ev->doesFloat() && dayof == 1 && span > 1 ) {
00176         datestr = KGlobal::locale()->formatDate( ev->dtStart().date() );
00177         datestr += " -\n " +
00178                    KGlobal::locale()->formatDate( sD.addDays( span-1 ) );
00179       }
00180 
00181       label = new QLabel( datestr, this );
00182       label->setAlignment( AlignLeft | AlignVCenter );
00183       if ( makeBold ) {
00184         QFont font = label->font();
00185         font.setBold( true );
00186         label->setFont( font );
00187       }
00188       mLayout->addWidget( label, counter, 1 );
00189       mLabels.append( label );
00190 
00191       // Fill Event Summary Field
00192       QString newtext = ev->summary();
00193       if ( ev->isMultiDay() &&  !ev->doesFloat() ) {
00194         newtext.append( QString(" (%1/%2)").arg( dayof ).arg( span ) );
00195       }
00196 
00197       KURLLabel *urlLabel = new KURLLabel( ev->uid(), newtext, this );
00198       urlLabel->installEventFilter( this );
00199       urlLabel->setAlignment( urlLabel->alignment() | Qt::WordBreak );
00200       mLayout->addWidget( urlLabel, counter, 2 );
00201       mLabels.append( urlLabel );
00202 
00203       if ( !ev->description().isEmpty() ) {
00204         QToolTip::add( urlLabel, ev->description() );
00205       }
00206 
00207       // Fill Event Time Range Field (only for non-floating Events)
00208       if ( !ev->doesFloat() ) {
00209         QTime sST = ev->dtStart().time();
00210         QTime sET = ev->dtEnd().time();
00211         if ( ev->isMultiDay() ) {
00212           if ( ev->dtStart().date() < dt ) {
00213             sST = QTime::QTime( 0, 0 );
00214           }
00215           if ( ev->dtEnd().date() > dt ) {
00216             sET = QTime::QTime( 23, 59 );
00217           }
00218         }
00219         datestr = i18n( "Time from - to", "%1 - %2" )
00220                   .arg( KGlobal::locale()->formatTime( sST ) )
00221                   .arg( KGlobal::locale()->formatTime( sET ) );
00222         label = new QLabel( datestr, this );
00223         label->setAlignment( AlignLeft | AlignVCenter );
00224         mLayout->addWidget( label, counter, 3 );
00225         mLabels.append( label );
00226       }
00227 
00228       connect( urlLabel, SIGNAL( leftClickedURL( const QString& ) ),
00229                this, SLOT( selectEvent( const QString& ) ) );
00230 
00231       counter++;
00232     }
00233   }
00234 
00235   if ( !counter ) {
00236     QLabel *noEvents = new QLabel(
00237       i18n( "No appointments pending within the next day",
00238             "No appointments pending within the next %n days",
00239             days ), this, "nothing to see" );
00240     noEvents->setAlignment( AlignHCenter | AlignVCenter );
00241     mLayout->addWidget( noEvents, 0, 2 );
00242     mLabels.append( noEvents );
00243   }
00244 
00245   for ( label = mLabels.first(); label; label = mLabels.next() )
00246     label->show();
00247 }
00248 
00249 void SummaryWidget::selectEvent( const QString &uid )
00250 {
00251   mPlugin->core()->selectPlugin( "kontact_korganizerplugin" ); //ensure loaded
00252   KOrganizerIface_stub iface( "korganizer", "KOrganizerIface" );
00253   iface.editIncidence( uid );
00254 }
00255 
00256 bool SummaryWidget::eventFilter( QObject *obj, QEvent* e )
00257 {
00258   if ( obj->inherits( "KURLLabel" ) ) {
00259     KURLLabel* label = static_cast<KURLLabel*>( obj );
00260     if ( e->type() == QEvent::Enter )
00261       emit message( i18n( "Edit Appointment: \"%1\"" ).arg( label->text() ) );
00262     if ( e->type() == QEvent::Leave )
00263       emit message( QString::null );
00264   }
00265 
00266   return Kontact::Summary::eventFilter( obj, e );
00267 }
00268 
00269 QStringList SummaryWidget::configModules() const
00270 {
00271   return QStringList( "kcmkorgsummary.desktop" );
00272 }
00273 
00274 #include "summarywidget.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys