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 <tqcursor.h>
00025 #include <tqlabel.h>
00026 #include <tqlayout.h>
00027 #include <tqtooltip.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                                       TQWidget *parent, const char *name )
00057   : Kontact::Summary( parent, name ), mPlugin( plugin )
00058 {
00059   TQVBoxLayout *mainLayout = new TQVBoxLayout( this, 3, 3 );
00060 
00061   TQPixmap icon = KGlobal::iconLoader()->loadIcon( "kontact_todo",
00062                    KIcon::Desktop, KIcon::SizeMedium );
00063   TQWidget *header = createHeader( this, icon, i18n( "To-do" ) );
00064   mainLayout->addWidget( header );
00065 
00066   mLayout = new TQGridLayout( mainLayout, 7, 4, 3 );
00067   mLayout->setRowStretch( 6, 1 );
00068 
00069   mCalendar = KOrg::StdCalendar::self();
00070 
00071   connect( mCalendar, TQT_SIGNAL( calendarChanged() ), TQT_SLOT( updateView() ) );
00072   connect( mPlugin->core(), TQT_SIGNAL( dayChanged( const TQDate& ) ),
00073            TQT_SLOT( updateView() ) );
00074 
00075   updateView();
00076 }
00077 
00078 TodoSummaryWidget::~TodoSummaryWidget()
00079 {
00080 }
00081 
00082 void TodoSummaryWidget::updateView()
00083 {
00084   mLabels.setAutoDelete( true );
00085   mLabels.clear();
00086   mLabels.setAutoDelete( false );
00087 
00088   KConfig config( "kcmkorgsummaryrc" );
00089   config.setGroup( "Todo" );
00090   bool showAllTodos = config.readBoolEntry( "ShowAllTodos", false );
00091 
00092   KIconLoader loader( "kdepim" );
00093 
00094   TQLabel *label = 0;
00095   int counter = 0;
00096 
00097   TQDate currentDate = TQDate::currentDate();
00098   KCal::Todo::List todos = mCalendar->todos();
00099   if ( todos.count() > 0 ) {
00100     TQPixmap pm = loader.loadIcon( "todo", KIcon::Small );
00101     KCal::Todo::List::ConstIterator it;
00102     for ( it = todos.begin(); it != todos.end(); ++it ) {
00103       KCal::Todo *todo = *it;
00104 
00105       bool accepted = false;
00106       TQString stateText;
00107 
00108       // show all incomplete todos
00109       if ( showAllTodos && !todo->isCompleted())
00110         accepted = true;
00111 
00112       // show uncomplete todos from the last days
00113       if ( todo->hasDueDate() && !todo->isCompleted() &&
00114            todo->dtDue().date() < currentDate ) {
00115         accepted = true;
00116         stateText = i18n( "overdue" );
00117       }
00118 
00119       // show todos which started somewhere in the past and has to be finished in future
00120       if ( todo->hasStartDate() && todo->hasDueDate() &&
00121            todo->dtStart().date() < currentDate &&
00122            currentDate < todo->dtDue().date() ) {
00123         accepted = true;
00124         stateText = i18n( "in progress" );
00125       }
00126 
00127       // all todos which start today
00128       if ( todo->hasStartDate() && todo->dtStart().date() == currentDate ) {
00129         accepted = true;
00130         stateText = i18n( "starts today" );
00131       }
00132 
00133       // all todos which end today
00134       if ( todo->hasDueDate() && todo->dtDue().date() == currentDate ) {
00135         accepted = true;
00136         stateText = i18n( "ends today" );
00137       }
00138 
00139       if ( !accepted )
00140         continue;
00141 
00142       label = new TQLabel( this );
00143       label->setPixmap( pm );
00144       label->setSizePolicy( TQSizePolicy::Maximum, TQSizePolicy::Maximum );
00145       mLayout->addWidget( label, counter, 0 );
00146       mLabels.append( label );
00147 
00148       label = new TQLabel( TQString::number( todo->percentComplete() ) + "%", this );
00149       label->setAlignment( AlignHCenter | AlignVCenter );
00150       label->setSizePolicy( TQSizePolicy::Maximum, TQSizePolicy::Maximum );
00151       mLayout->addWidget( label, counter, 1 );
00152       mLabels.append( label );
00153 
00154       TQString sSummary = todo->summary();
00155       if ( todo->relatedTo() ) { // show parent only, not entire ancestry
00156         sSummary = todo->relatedTo()->summary() + ":" + todo->summary();
00157       }
00158       KURLLabel *urlLabel = new KURLLabel( this );
00159       urlLabel->setText( sSummary );
00160       urlLabel->setURL( todo->uid() );
00161       urlLabel->installEventFilter( this );
00162       urlLabel->setTextFormat( Qt::RichText );
00163       mLayout->addWidget( urlLabel, counter, 2 );
00164       mLabels.append( urlLabel );
00165 
00166       connect( urlLabel, TQT_SIGNAL( leftClickedURL( const TQString& ) ),
00167                this, TQT_SLOT( viewTodo( const TQString& ) ) );
00168       connect( urlLabel, TQT_SIGNAL( rightClickedURL( const TQString& ) ),
00169                this, TQT_SLOT( popupMenu( const TQString& ) ) );
00170 
00171       TQString tipText( KCal::IncidenceFormatter::toolTipStr( mCalendar, todo, currentDate, true ) );
00172       if ( !tipText.isEmpty() ) {
00173         TQToolTip::add( urlLabel, tipText );
00174       }
00175 
00176       label = new TQLabel( stateText, this );
00177       label->setAlignment( AlignLeft | AlignVCenter );
00178       label->setSizePolicy( TQSizePolicy::Maximum, TQSizePolicy::Maximum );
00179       mLayout->addWidget( label, counter, 3 );
00180       mLabels.append( label );
00181 
00182       counter++;
00183     }
00184   }
00185 
00186   if ( counter == 0 ) {
00187     TQLabel *noTodos = new TQLabel( i18n( "No to-dos pending" ), this );
00188     noTodos->setAlignment( AlignHCenter | AlignVCenter );
00189     mLayout->addWidget( noTodos, 0, 1 );
00190     mLabels.append( noTodos );
00191   }
00192 
00193   for ( label = mLabels.first(); label; label = mLabels.next() )
00194     label->show();
00195 }
00196 
00197 void TodoSummaryWidget::viewTodo( const TQString &uid )
00198 {
00199   mPlugin->core()->selectPlugin( "kontact_todoplugin" );//ensure loaded
00200   KOrganizerIface_stub iface( "korganizer", "KOrganizerIface" );
00201   iface.editIncidence( uid );
00202 }
00203 
00204 void TodoSummaryWidget::removeTodo( const TQString &uid )
00205 {
00206   mPlugin->core()->selectPlugin( "kontact_todoplugin" );//ensure loaded
00207   KOrganizerIface_stub iface( "korganizer", "KOrganizerIface" );
00208   iface.deleteIncidence( uid, false );
00209 }
00210 
00211 void TodoSummaryWidget::completeTodo( const TQString &uid )
00212 {
00213   KCal::Todo *todo = mCalendar->todo( uid );
00214   IncidenceChanger *changer = new IncidenceChanger( mCalendar, this );
00215   if ( !todo->isReadOnly() && changer->beginChange( todo, 0, TQString() ) ) {
00216     KCal::Todo *oldTodo = todo->clone();
00217     todo->setCompleted( TQDateTime::currentDateTime() );
00218     changer->changeIncidence( oldTodo, todo, KOGlobals::COMPLETION_MODIFIED, this );
00219     changer->endChange( todo, 0, TQString() );
00220     delete oldTodo;
00221     updateView();
00222   }
00223 }
00224 
00225 void TodoSummaryWidget::popupMenu( const TQString &uid )
00226 {
00227   KPopupMenu popup( this );
00228   TQToolTip::remove( 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( TQCursor::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( TQObject *obj, TQEvent* e )
00252 {
00253   if ( obj->inherits( "KURLLabel" ) ) {
00254     KURLLabel* label = static_cast<KURLLabel*>( obj );
00255     if ( e->type() == TQEvent::Enter )
00256       emit message( i18n( "Edit To-do: \"%1\"" ).arg( label->text() ) );
00257     if ( e->type() == TQEvent::Leave )
00258       emit message( TQString::null );
00259   }
00260 
00261   return Kontact::Summary::eventFilter( obj, e );
00262 }
00263 
00264 TQStringList TodoSummaryWidget::configModules() const
00265 {
00266   return TQStringList( "kcmtodosummary.desktop" );
00267 }
00268 
00269 #include "todosummarywidget.moc"