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( "kdepim" );
00089 
00090   QLabel *label = 0;
00091   int counter = 0;
00092 
00093   QDate currentDate = QDate::currentDate();
00094   KCal::Todo::List todos = mCalendar->todos();
00095   if ( todos.count() > 0 ) {
00096     QPixmap pm = loader.loadIcon( "todo", KIcon::Small );
00097     KCal::Todo::List::ConstIterator it;
00098     for ( it = todos.begin(); it != todos.end(); ++it ) {
00099       KCal::Todo *todo = *it;
00100 
00101       bool accepted = false;
00102       QString stateText;
00103 
00104       // show all incomplete todos
00105       if ( showAllTodos && !todo->isCompleted())
00106         accepted = true;
00107 
00108       // show uncomplete todos from the last days
00109       if ( todo->hasDueDate() && !todo->isCompleted() &&
00110            todo->dtDue().date() < currentDate ) {
00111         accepted = true;
00112         stateText = i18n( "overdue" );
00113       }
00114 
00115       // show todos which started somewhere in the past and has to be finished in future
00116       if ( todo->hasStartDate() && todo->hasDueDate() &&
00117            todo->dtStart().date() < currentDate &&
00118            currentDate < todo->dtDue().date() ) {
00119         accepted = true;
00120         stateText = i18n( "in progress" );
00121       }
00122 
00123       // all todos which start today
00124       if ( todo->hasStartDate() && todo->dtStart().date() == currentDate ) {
00125         accepted = true;
00126         stateText = i18n( "starts today" );
00127       }
00128 
00129       // all todos which end today
00130       if ( todo->hasDueDate() && todo->dtDue().date() == currentDate ) {
00131         accepted = true;
00132         stateText = i18n( "ends today" );
00133       }
00134 
00135       if ( !accepted )
00136         continue;
00137 
00138       label = new QLabel( this );
00139       label->setPixmap( pm );
00140       label->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum );
00141       mLayout->addWidget( label, counter, 0 );
00142       mLabels.append( label );
00143 
00144       label = new QLabel( QString::number( todo->percentComplete() ) + "%", this );
00145       label->setAlignment( AlignHCenter | AlignVCenter );
00146       label->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum );
00147       mLayout->addWidget( label, counter, 1 );
00148       mLabels.append( label );
00149 
00150       QString sSummary = todo->summary();
00151       if ( todo->relatedTo() ) { // show parent only, not entire ancestry
00152         sSummary = todo->relatedTo()->summary() + ":" + todo->summary();
00153       }
00154       KURLLabel *urlLabel = new KURLLabel( todo->uid(), sSummary, this );
00155       urlLabel->installEventFilter( this );
00156       urlLabel->setTextFormat( Qt::RichText );
00157       mLayout->addWidget( urlLabel, counter, 2 );
00158       mLabels.append( urlLabel );
00159 
00160       if ( !todo->description().isEmpty() ) {
00161         QToolTip::add( urlLabel, todo->description() );
00162       }
00163 
00164       label = new QLabel( stateText, this );
00165       label->setAlignment( AlignLeft | AlignVCenter );
00166       label->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum );
00167       mLayout->addWidget( label, counter, 3 );
00168       mLabels.append( label );
00169 
00170       connect( urlLabel, SIGNAL( leftClickedURL( const QString& ) ),
00171                this, SLOT( selectEvent( const QString& ) ) );
00172 
00173       counter++;
00174     }
00175   }
00176 
00177   if ( counter == 0 ) {
00178     QLabel *noTodos = new QLabel( i18n( "No to-dos pending" ), this );
00179     noTodos->setAlignment( AlignHCenter | AlignVCenter );
00180     mLayout->addWidget( noTodos, 0, 1 );
00181     mLabels.append( noTodos );
00182   }
00183 
00184   for ( label = mLabels.first(); label; label = mLabels.next() )
00185     label->show();
00186 }
00187 
00188 void TodoSummaryWidget::selectEvent( const QString &uid )
00189 {
00190   mPlugin->core()->selectPlugin( "kontact_todoplugin" );//ensure loaded
00191   KOrganizerIface_stub iface( "korganizer", "KOrganizerIface" );
00192   iface.editIncidence( uid );
00193 }
00194 
00195 bool TodoSummaryWidget::eventFilter( QObject *obj, QEvent* e )
00196 {
00197   if ( obj->inherits( "KURLLabel" ) ) {
00198     KURLLabel* label = static_cast<KURLLabel*>( obj );
00199     if ( e->type() == QEvent::Enter )
00200       emit message( i18n( "Edit To-do: \"%1\"" ).arg( label->text() ) );
00201     if ( e->type() == QEvent::Leave )
00202       emit message( QString::null );
00203   }
00204 
00205   return Kontact::Summary::eventFilter( obj, e );
00206 }
00207 
00208 QStringList TodoSummaryWidget::configModules() const
00209 {
00210   return QStringList( "kcmtodosummary.desktop" );
00211 }
00212 
00213 #include "todosummarywidget.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys