kontact

todosummarywidget.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 <qcursor.h>
00025 #include <qlabel.h>
00026 #include <qlayout.h>
00027 #include <qtooltip.h>
00028 
00029 #include <kdialog.h>
00030 #include <kglobal.h>
00031 #include <kiconloader.h>
00032 #include <klocale.h>
00033 #include <kparts/part.h>
00034 #include <kpopupmenu.h>
00035 #include <kstandarddirs.h>
00036 #include <kurllabel.h>
00037 #include <libkcal/resourcecalendar.h>
00038 #include <libkcal/resourcelocal.h>
00039 #include <libkcal/todo.h>
00040 #include <libkcal/incidenceformatter.h>
00041 #include <libkdepim/kpimprefs.h>
00042 
00043 #include "korganizeriface_stub.h"
00044 
00045 #include "core.h"
00046 #include "plugin.h"
00047 #include "todoplugin.h"
00048 
00049 #include "korganizer/stdcalendar.h"
00050 #include "korganizer/koglobals.h"
00051 #include "korganizer/incidencechanger.h"
00052 
00053 #include "todosummarywidget.h"
00054 
00055 TodoSummaryWidget::TodoSummaryWidget( TodoPlugin *plugin,
00056                                       QWidget *parent, const char *name )
00057   : Kontact::Summary( parent, name ), mPlugin( plugin )
00058 {
00059   QVBoxLayout *mainLayout = new QVBoxLayout( this, 3, 3 );
00060 
00061   QPixmap icon = KGlobal::iconLoader()->loadIcon( "kontact_todo",
00062                    KIcon::Desktop, KIcon::SizeMedium );
00063   QWidget *header = createHeader( this, icon, i18n( "To-dos" ) );
00064   mainLayout->addWidget( header );
00065 
00066   mLayout = new QGridLayout( mainLayout, 7, 4, 3 );
00067   mLayout->setRowStretch( 6, 1 );
00068 
00069   mCalendar = KOrg::StdCalendar::self();
00070   mCalendar->load();
00071 
00072   connect( mCalendar, SIGNAL( calendarChanged() ), SLOT( updateView() ) );
00073   connect( mPlugin->core(), SIGNAL( dayChanged( const QDate& ) ),
00074            SLOT( updateView() ) );
00075 
00076   updateView();
00077 }
00078 
00079 TodoSummaryWidget::~TodoSummaryWidget()
00080 {
00081 }
00082 
00083 void TodoSummaryWidget::updateView()
00084 {
00085   mLabels.setAutoDelete( true );
00086   mLabels.clear();
00087   mLabels.setAutoDelete( false );
00088 
00089   KConfig config( "kcmkorgsummaryrc" );
00090   config.setGroup( "Todo" );
00091   bool showAllTodos = config.readBoolEntry( "ShowAllTodos", false );
00092 
00093   KIconLoader loader( "kdepim" );
00094 
00095   QLabel *label = 0;
00096   int counter = 0;
00097 
00098   QDate currentDate = QDate::currentDate();
00099   KCal::Todo::List todos = mCalendar->todos();
00100   if ( todos.count() > 0 ) {
00101     QPixmap pm = loader.loadIcon( "todo", KIcon::Small );
00102     KCal::Todo::List::ConstIterator it;
00103     for ( it = todos.begin(); it != todos.end(); ++it ) {
00104       KCal::Todo *todo = *it;
00105 
00106       bool accepted = false;
00107       QString stateText;
00108 
00109       // show all incomplete todos
00110       if ( showAllTodos && !todo->isCompleted())
00111         accepted = true;
00112 
00113       // show uncomplete todos from the last days
00114       if ( todo->hasDueDate() && !todo->isCompleted() &&
00115            todo->dtDue().date() < currentDate ) {
00116         accepted = true;
00117         stateText = i18n( "overdue" );
00118       }
00119 
00120       // show todos which started somewhere in the past and has to be finished in future
00121       if ( todo->hasStartDate() && todo->hasDueDate() &&
00122            todo->dtStart().date() < currentDate &&
00123            currentDate < todo->dtDue().date() ) {
00124         accepted = true;
00125         stateText = i18n( "in progress" );
00126       }
00127 
00128       // all todos which start today
00129       if ( todo->hasStartDate() && todo->dtStart().date() == currentDate ) {
00130         accepted = true;
00131         stateText = i18n( "starts today" );
00132       }
00133 
00134       // all todos which end today
00135       if ( todo->hasDueDate() && todo->dtDue().date() == currentDate ) {
00136         accepted = true;
00137         stateText = i18n( "ends today" );
00138       }
00139 
00140       if ( !accepted )
00141         continue;
00142 
00143       label = new QLabel( this );
00144       label->setPixmap( pm );
00145       label->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum );
00146       mLayout->addWidget( label, counter, 0 );
00147       mLabels.append( label );
00148 
00149       label = new QLabel( QString::number( todo->percentComplete() ) + "%", this );
00150       label->setAlignment( AlignHCenter | AlignVCenter );
00151       label->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum );
00152       mLayout->addWidget( label, counter, 1 );
00153       mLabels.append( label );
00154 
00155       QString sSummary = todo->summary();
00156       if ( todo->relatedTo() ) { // show parent only, not entire ancestry
00157         sSummary = todo->relatedTo()->summary() + ":" + todo->summary();
00158       }
00159       KURLLabel *urlLabel = new KURLLabel( this );
00160       urlLabel->setText( sSummary );
00161       urlLabel->setURL( todo->uid() );
00162       urlLabel->installEventFilter( this );
00163       urlLabel->setTextFormat( Qt::RichText );
00164       mLayout->addWidget( urlLabel, counter, 2 );
00165       mLabels.append( urlLabel );
00166 
00167       connect( urlLabel, SIGNAL( leftClickedURL( const QString& ) ),
00168                this, SLOT( viewTodo( const QString& ) ) );
00169       connect( urlLabel, SIGNAL( rightClickedURL( const QString& ) ),
00170                this, SLOT( popupMenu( const QString& ) ) );
00171 
00172       QString tipText( KCal::IncidenceFormatter::toolTipString( todo, true ) );
00173       if ( !tipText.isEmpty() ) {
00174         QToolTip::add( urlLabel, tipText );
00175       }
00176 
00177       label = new QLabel( stateText, this );
00178       label->setAlignment( AlignLeft | AlignVCenter );
00179       label->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum );
00180       mLayout->addWidget( label, counter, 3 );
00181       mLabels.append( label );
00182 
00183       counter++;
00184     }
00185   }
00186 
00187   if ( counter == 0 ) {
00188     QLabel *noTodos = new QLabel( i18n( "No to-dos pending" ), this );
00189     noTodos->setAlignment( AlignHCenter | AlignVCenter );
00190     mLayout->addWidget( noTodos, 0, 1 );
00191     mLabels.append( noTodos );
00192   }
00193 
00194   for ( label = mLabels.first(); label; label = mLabels.next() )
00195     label->show();
00196 }
00197 
00198 void TodoSummaryWidget::viewTodo( const QString &uid )
00199 {
00200   mPlugin->core()->selectPlugin( "kontact_todoplugin" );//ensure loaded
00201   KOrganizerIface_stub iface( "korganizer", "KOrganizerIface" );
00202   iface.editIncidence( uid );
00203 }
00204 
00205 void TodoSummaryWidget::removeTodo( const QString &uid )
00206 {
00207   mPlugin->core()->selectPlugin( "kontact_todoplugin" );//ensure loaded
00208   KOrganizerIface_stub iface( "korganizer", "KOrganizerIface" );
00209   iface.deleteIncidence( uid, false );
00210 }
00211 
00212 void TodoSummaryWidget::completeTodo( const QString &uid )
00213 {
00214   KCal::Todo *todo = mCalendar->todo( uid );
00215   IncidenceChanger *changer = new IncidenceChanger( mCalendar, this );
00216   if ( !todo->isReadOnly() && changer->beginChange( todo ) ) {
00217     KCal::Todo *oldTodo = todo->clone();
00218     todo->setCompleted( QDateTime::currentDateTime() );
00219     changer->changeIncidence( oldTodo, todo, KOGlobals::COMPLETION_MODIFIED );
00220     changer->endChange( todo );
00221     delete oldTodo;
00222     updateView();
00223   }
00224 }
00225 
00226 void TodoSummaryWidget::popupMenu( const QString &uid )
00227 {
00228   KPopupMenu popup( this );
00229   popup.insertItem( i18n( "&Edit To-do..." ), 0 );
00230   popup.insertItem( KGlobal::iconLoader()->loadIcon( "editdelete", KIcon::Small),
00231                     i18n( "&Delete To-do" ), 1 );
00232   KCal::Todo *todo = mCalendar->todo( uid );
00233   if ( !todo->isCompleted() ) {
00234     popup.insertItem( KGlobal::iconLoader()->loadIcon( "checkedbox", KIcon::Small),
00235                       i18n( "&Mark To-do Completed" ), 2 );
00236   }
00237 
00238   switch ( popup.exec( QCursor::pos() ) ) {
00239     case 0:
00240       viewTodo( uid );
00241       break;
00242     case 1:
00243       removeTodo( uid );
00244       break;
00245     case 2:
00246       completeTodo( uid );
00247       break;
00248   }
00249 }
00250 
00251 bool TodoSummaryWidget::eventFilter( QObject *obj, QEvent* e )
00252 {
00253   if ( obj->inherits( "KURLLabel" ) ) {
00254     KURLLabel* label = static_cast<KURLLabel*>( obj );
00255     if ( e->type() == QEvent::Enter )
00256       emit message( i18n( "Edit To-do: \"%1\"" ).arg( label->text() ) );
00257     if ( e->type() == QEvent::Leave )
00258       emit message( QString::null );
00259   }
00260 
00261   return Kontact::Summary::eventFilter( obj, e );
00262 }
00263 
00264 QStringList TodoSummaryWidget::configModules() const
00265 {
00266   return QStringList( "kcmtodosummary.desktop" );
00267 }
00268 
00269 #include "todosummarywidget.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys