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 <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/resourcecalendar.h>
00036 #include <libkcal/resourcelocal.h>
00037 #include <libkcal/todo.h>
00038 #include <libkdepim/kpimprefs.h>
00039 
00040 #include "korganizeriface_stub.h"
00041 
00042 #include "core.h"
00043 #include "plugin.h"
00044 #include "todoplugin.h"
00045 
00046 #include "korganizer/stdcalendar.h"
00047 
00048 #include "todosummarywidget.h"
00049 
00050 TodoSummaryWidget::TodoSummaryWidget( TodoPlugin *plugin,
00051                                       QWidget *parent, const char *name )
00052   : Kontact::Summary( parent, name ), mPlugin( plugin )
00053 {
00054   QVBoxLayout *mainLayout = new QVBoxLayout( this, 3, 3 );
00055 
00056   QPixmap icon = KGlobal::iconLoader()->loadIcon( "kontact_todo",
00057                    KIcon::Desktop, KIcon::SizeMedium );
00058   QWidget *header = createHeader( this, icon, i18n( "To-dos" ) );
00059   mainLayout->addWidget( header );
00060 
00061   mLayout = new QGridLayout( mainLayout, 7, 4, 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 TodoSummaryWidget::~TodoSummaryWidget()
00075 {
00076 }
00077 
00078 void TodoSummaryWidget::updateView()
00079 {
00080   mLabels.setAutoDelete( true );
00081   mLabels.clear();
00082   mLabels.setAutoDelete( false );
00083 
00084   KConfig config( "kcmkorgsummaryrc" );
00085   config.setGroup( "Todo" );
00086   bool showAllTodos = config.readBoolEntry( "ShowAllTodos", false );
00087 
00088   KIconLoader loader( "korganizer" );
00089 
00090   QLabel *label = 0;
00091   int counter = 0;
00092 
00093   KCal::Todo::List todos = mCalendar->todos();
00094   if ( todos.count() > 0 ) {
00095     QPixmap pm = loader.loadIcon( "todo", KIcon::Small );
00096     KCal::Todo::List::ConstIterator it;
00097     for ( it = todos.begin(); it != todos.end(); ++it ) {
00098       KCal::Todo *todo = *it;
00099 
00100       bool accepted = false;
00101       QString stateText;
00102 
00103       // show all incomplete todos
00104       if ( showAllTodos && !todo->isCompleted())
00105         accepted = accepted || true;
00106 
00107       // show uncomplete todos from the last days
00108       if ( todo->hasDueDate() && !todo->isCompleted() &&
00109            todo->dtDue().date() < QDate::currentDate() ) {
00110         accepted = accepted || true;
00111         stateText = i18n( "overdue" );
00112       }
00113 
00114       // show todos which started somewhere in the past and has to be finished in future
00115       if ( todo->hasStartDate() && todo->hasDueDate() && todo->dtStart().date()
00116            < QDate::currentDate() && QDate::currentDate() < todo->dtDue().date() ) {
00117         accepted = accepted || true;
00118         stateText = i18n( "in progress" );
00119       }
00120 
00121       // all todos which start today
00122       if ( todo->hasStartDate() && todo->dtStart().date() == QDate::currentDate() ) {
00123         accepted = accepted || true;
00124         stateText = i18n( "starts today" );
00125       }
00126 
00127       // all todos which end today
00128       if ( todo->hasDueDate() && todo->dtDue().date() == QDate::currentDate() ) {
00129         accepted = accepted || true;
00130         stateText = i18n( "ends today" );
00131       }
00132 
00133       if ( !accepted )
00134         continue;
00135 
00136       label = new QLabel( this );
00137       label->setPixmap( pm );
00138       label->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum );
00139       mLayout->addWidget( label, counter, 0 );
00140       mLabels.append( label );
00141 
00142       label = new QLabel( QString::number( todo->percentComplete() ) + "%", this );
00143       label->setAlignment( AlignHCenter | AlignVCenter );
00144       label->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum );
00145       mLayout->addWidget( label, counter, 1 );
00146       mLabels.append( label );
00147 
00148       QString sSummary = todo->summary();
00149       if ( todo->relatedTo() ) { // show parent only, not entire ancestry
00150         sSummary = todo->relatedTo()->summary() + ":" + todo->summary();
00151       }
00152       KURLLabel *urlLabel = new KURLLabel( todo->uid(), sSummary, this );
00153       urlLabel->installEventFilter( this );
00154       urlLabel->setTextFormat( Qt::RichText );
00155       mLayout->addWidget( urlLabel, counter, 2 );
00156       mLabels.append( urlLabel );
00157 
00158       if ( !todo->description().isEmpty() ) {
00159         QToolTip::add( urlLabel, todo->description() );
00160       }
00161 
00162       label = new QLabel( stateText, this );
00163       label->setAlignment( AlignLeft | AlignVCenter );
00164       label->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum );
00165       mLayout->addWidget( label, counter, 3 );
00166       mLabels.append( label );
00167 
00168       connect( urlLabel, SIGNAL( leftClickedURL( const QString& ) ),
00169                this, SLOT( selectEvent( const QString& ) ) );
00170 
00171       counter++;
00172     }
00173   }
00174 
00175   if ( counter == 0 ) {
00176     QLabel *noTodos = new QLabel( i18n( "No to-dos pending" ), this );
00177     noTodos->setAlignment( AlignHCenter | AlignVCenter );
00178     mLayout->addWidget( noTodos, 0, 1 );
00179     mLabels.append( noTodos );
00180   }
00181 
00182   for ( label = mLabels.first(); label; label = mLabels.next() )
00183     label->show();
00184 }
00185 
00186 void TodoSummaryWidget::selectEvent( const QString &uid )
00187 {
00188   mPlugin->core()->selectPlugin( "kontact_todoplugin" );//ensure loaded
00189   KOrganizerIface_stub iface( "korganizer", "KOrganizerIface" );
00190   iface.editIncidence( uid );
00191 }
00192 
00193 bool TodoSummaryWidget::eventFilter( QObject *obj, QEvent* e )
00194 {
00195   if ( obj->inherits( "KURLLabel" ) ) {
00196     KURLLabel* label = static_cast<KURLLabel*>( obj );
00197     if ( e->type() == QEvent::Enter )
00198       emit message( i18n( "Edit To-do: \"%1\"" ).arg( label->text() ) );
00199     if ( e->type() == QEvent::Leave )
00200       emit message( QString::null );
00201   }
00202 
00203   return Kontact::Summary::eventFilter( obj, e );
00204 }
00205 
00206 QStringList TodoSummaryWidget::configModules() const
00207 {
00208   return QStringList( "kcmtodosummary.desktop" );
00209 }
00210 
00211 #include "todosummarywidget.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys