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( "korganizer" );
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   for ( dt=QDate::currentDate();
00097         dt<=QDate::currentDate().addDays( days - 1 );
00098         dt=dt.addDays(1) ) {
00099     KCal::Event::List events = mCalendar->events( dt );
00100 
00101     // sort the events for this date by summary
00102     events = KCal::Calendar::sortEvents( &events,
00103                                          KCal::EventSortSummary,
00104                                          KCal::SortDirectionAscending );
00105     // sort the events for this date by start date
00106     events = KCal::Calendar::sortEvents( &events,
00107                                          KCal::EventSortStartDate,
00108                                          KCal::SortDirectionAscending );
00109 
00110     KCal::Event *ev;
00111     KCal::Event::List::ConstIterator it;
00112     for ( it=events.begin(); it!=events.end(); ++it ) {
00113       ev = *it;
00114 
00115       // Count number of days remaining in multiday event
00116       int span=1; int dayof=1;
00117       if ( ev->isMultiDay() ) {
00118         QDate d = ev->dtStart().date();
00119         if ( d < QDate::currentDate() ) {
00120           d = QDate::currentDate();
00121         }
00122         while ( d < ev->dtEnd().date() ) {
00123           if ( d < dt ) {
00124             dayof++;
00125           }
00126           span++;
00127           d=d.addDays( 1 );
00128         }
00129       }
00130 
00131       // If this date is part of a floating, multiday event, then we
00132       // only make a print for the first day of the event.
00133       if ( ev->isMultiDay() && ev->doesFloat() && dayof != 1 ) continue;
00134 
00135       // Fill Appointment Pixmap Field
00136       label = new QLabel( this );
00137       label->setPixmap( pm );
00138       label->setMaximumWidth( label->minimumSizeHint().width() );
00139       label->setAlignment( AlignVCenter );
00140       mLayout->addWidget( label, counter, 0 );
00141       mLabels.append( label );
00142 
00143       // Fill Event Date Field
00144       bool makeBold = false;
00145       QString datestr;
00146 
00147       // Modify event date for printing
00148       QDate sD = QDate::QDate( dt.year(), dt.month(), dt.day() );
00149       if ( ( sD.month() == QDate::currentDate().month() ) &&
00150            ( sD.day()   == QDate::currentDate().day() ) ) {
00151         datestr = i18n( "Today" );
00152         makeBold = true;
00153       } else if ( ( sD.month() == QDate::currentDate().addDays( 1 ).month() ) &&
00154                   ( sD.day()   == QDate::currentDate().addDays( 1 ).day() ) ) {
00155         datestr = i18n( "Tomorrow" );
00156       } else {
00157         datestr = KGlobal::locale()->formatDate( sD );
00158       }
00159 
00160       // Print the date span for multiday, floating events, for the
00161       // first day of the event only.
00162       if ( ev->isMultiDay() && ev->doesFloat() && dayof == 1 && span > 1 ) {
00163         QString endstr = KGlobal::locale()->formatDate( sD.addDays( span-1 ) );
00164         datestr += " -\n " + endstr;
00165       }
00166 
00167       label = new QLabel( datestr, this );
00168       label->setAlignment( AlignLeft | AlignVCenter );
00169       if ( makeBold ) {
00170         QFont font = label->font();
00171         font.setBold( true );
00172         label->setFont( font );
00173       }
00174       mLayout->addWidget( label, counter, 1 );
00175       mLabels.append( label );
00176 
00177       // Fill Event Summary Field
00178       QString newtext = ev->summary();
00179       if ( ev->isMultiDay() &&  !ev->doesFloat() ) {
00180         newtext.append( QString(" (%1/%2)").arg( dayof ).arg( span ) );
00181       }
00182 
00183       KURLLabel *urlLabel = new KURLLabel( ev->uid(), newtext, this );
00184       urlLabel->installEventFilter( this );
00185       urlLabel->setAlignment( urlLabel->alignment() | Qt::WordBreak );
00186       mLayout->addWidget( urlLabel, counter, 2 );
00187       mLabels.append( urlLabel );
00188 
00189       if ( !ev->description().isEmpty() ) {
00190         QToolTip::add( urlLabel, ev->description() );
00191       }
00192 
00193       // Fill Event Time Range Field (only for non-floating Events)
00194       if ( !ev->doesFloat() ) {
00195         QTime sST = ev->dtStart().time();
00196         QTime sET = ev->dtEnd().time();
00197         if ( ev->isMultiDay() ) {
00198           if ( ev->dtStart().date() < dt ) {
00199             sST = QTime::QTime( 0, 0 );
00200           }
00201           if ( ev->dtEnd().date() > dt ) {
00202             sET = QTime::QTime( 23, 59 );
00203           }
00204         }
00205         datestr = i18n( "Time from - to", "%1 - %2" )
00206                   .arg( KGlobal::locale()->formatTime( sST ) )
00207                   .arg( KGlobal::locale()->formatTime( sET ) );
00208         label = new QLabel( datestr, this );
00209         label->setAlignment( AlignLeft | AlignVCenter );
00210         mLayout->addWidget( label, counter, 3 );
00211         mLabels.append( label );
00212       }
00213 
00214       connect( urlLabel, SIGNAL( leftClickedURL( const QString& ) ),
00215                this, SLOT( selectEvent( const QString& ) ) );
00216 
00217       counter++;
00218     }
00219   }
00220 
00221   if ( !counter ) {
00222     QLabel *noEvents = new QLabel(
00223       i18n( "No appointments pending within the next day",
00224             "No appointments pending within the next %n days",
00225             days ), this, "nothing to see" );
00226     noEvents->setAlignment( AlignHCenter | AlignVCenter );
00227     mLayout->addWidget( noEvents, 0, 2 );
00228     mLabels.append( noEvents );
00229   }
00230 
00231   for ( label = mLabels.first(); label; label = mLabels.next() )
00232     label->show();
00233 }
00234 
00235 void SummaryWidget::selectEvent( const QString &uid )
00236 {
00237   mPlugin->core()->selectPlugin( "kontact_korganizerplugin" ); //ensure loaded
00238   KOrganizerIface_stub iface( "korganizer", "KOrganizerIface" );
00239   iface.editIncidence( uid );
00240 }
00241 
00242 bool SummaryWidget::eventFilter( QObject *obj, QEvent* e )
00243 {
00244   if ( obj->inherits( "KURLLabel" ) ) {
00245     KURLLabel* label = static_cast<KURLLabel*>( obj );
00246     if ( e->type() == QEvent::Enter )
00247       emit message( i18n( "Edit Appointment: \"%1\"" ).arg( label->text() ) );
00248     if ( e->type() == QEvent::Leave )
00249       emit message( QString::null );
00250   }
00251 
00252   return Kontact::Summary::eventFilter( obj, e );
00253 }
00254 
00255 QStringList SummaryWidget::configModules() const
00256 {
00257   return QStringList( "kcmkorgsummary.desktop" );
00258 }
00259 
00260 #include "summarywidget.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys