• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdepim
  • Sitemap
  • Contact Us
 

ktimetracker

edithistorydialog.cpp

Go to the documentation of this file.
00001 /*
00002  *     Copyright (C) 2007 by Mathias Soeken <msoeken@tzi.de>
00003  *
00004  *   This program is free software; you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation; either version 2 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License along
00015  *   with this program; if not, write to the
00016  *      Free Software Foundation, Inc.
00017  *      51 Franklin Street, Fifth Floor
00018  *      Boston, MA  02110-1301  USA.
00019  *
00020  */
00021 
00022 #include "edithistorydialog.h"
00023 
00024 #include <QItemDelegate>
00025 #include <QModelIndex>
00026 #include <QStyleOptionViewItem>
00027 #include <QTableWidget>
00028 #include <QHeaderView>
00029 
00030 #include <KDateTimeWidget>
00031 #include <KDebug>
00032 #include <KLocale>
00033 #include <KMessageBox>
00034 
00035 #include "taskview.h"
00036 
00037 class HistoryWidgetDelegate : public QItemDelegate 
00038 {
00039 public:
00040   HistoryWidgetDelegate( QObject *parent = 0 ) : QItemDelegate( parent ) {}
00041 
00042   QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &) const 
00043   {
00044     KDateTimeWidget *editor = new KDateTimeWidget( parent );
00045     editor->setAutoFillBackground( true );
00046     editor->setPalette( option.palette );
00047     // FIXME highlight() background
00048     editor->setBackgroundRole( QPalette::Background );
00049 
00050     return editor;
00051   }
00052 
00053   void setEditorData( QWidget *editor, const QModelIndex &index ) const 
00054   {
00055     QDateTime dateTime = QDateTime::fromString( index.model()->data( index, Qt::DisplayRole ).toString(), "yyyy-MM-dd HH:mm:ss" );
00056 
00057     KDateTimeWidget *dateTimeWidget = static_cast<KDateTimeWidget*>( editor );
00058     dateTimeWidget->setDateTime( dateTime );
00059   }
00060 
00061   void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const 
00062   {
00063     KDateTimeWidget *dateTimeWidget = static_cast<KDateTimeWidget*>( editor );
00064     QDateTime dateTime = dateTimeWidget->dateTime();
00065 
00066     model->setData( index, dateTime.toString( "yyyy-MM-dd HH:mm:ss" ), Qt::EditRole );
00067   }
00068 
00069   void updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const 
00070   {
00071     editor->setGeometry( option.rect );
00072   }
00073 };
00074 
00075 EditHistoryDialog::EditHistoryDialog( TaskView *parent ) 
00076   : KDialog( parent, Qt::WindowContextHelpButtonHint ),
00077     mParent( parent )
00078 {
00079   kDebug(5970) << "Entering function";
00080   setButtons( KDialog::Close );
00081   setWindowTitle( i18n( "Edit History" ) );
00082 
00083   mHistoryWidget = new QTableWidget( this );
00084 
00085   /* Item Delegate for displaying KDateTimeWidget instead of QLineEdit */
00086   HistoryWidgetDelegate *historyWidgetDelegate = new HistoryWidgetDelegate( this );
00087   mHistoryWidget->setItemDelegateForColumn( 1, historyWidgetDelegate );
00088   mHistoryWidget->setItemDelegateForColumn( 2, historyWidgetDelegate );
00089 
00090   /* Columns */
00091   mHistoryWidget->setColumnCount( 5 );
00092   mHistoryWidget->setEditTriggers( QAbstractItemView::AllEditTriggers );
00093   mHistoryWidget->setHorizontalHeaderLabels( 
00094     QStringList() << i18n( "Task" ) << i18n( "StartTime" ) << i18n( "EndTime" )
00095                   << i18n( "Comment" ) );
00096   mHistoryWidget->horizontalHeader()->setStretchLastSection( true );
00097   mHistoryWidget->setColumnHidden( 4, true );  // hide the "UID" column
00098 
00099   listAllEvents();
00100   mHistoryWidget->setSortingEnabled( true ); 
00101   mHistoryWidget->resizeColumnsToContents();
00102   setMainWidget( mHistoryWidget );
00103 }
00104 
00105 QTableWidget* EditHistoryDialog::tableWidget()
00106 {
00107   return mHistoryWidget;
00108 }
00109 
00110 void EditHistoryDialog::listAllEvents()
00111 {
00112   connect( mHistoryWidget, SIGNAL( cellChanged( int, int ) ), 
00113            this, SLOT( historyWidgetCellChanged( int, int ) ) );
00114 
00115   KCal::Event::List eventList = mParent->storage()->rawevents();
00116   for ( KCal::Event::List::iterator i = eventList.begin(); 
00117         i != eventList.end(); ++i ) 
00118   {
00119     int row = mHistoryWidget->rowCount();
00120     mHistoryWidget->insertRow( row );
00121     QTableWidgetItem* item = new QTableWidgetItem( (*i)->relatedTo()->summary() );
00122     item->setFlags( Qt::ItemIsEnabled );
00123     item->setWhatsThis( i18n( "You can change this task's comment, start time and end time." ) );
00124     mHistoryWidget->setItem( row, 0, item );
00125     // dtStart is stored like DTSTART;TZID=Europe/Berlin:20080327T231056
00126     // dtEnd is stored like DTEND:20080327T231509Z
00127     // we need to handle both differently
00128     QDateTime start = QDateTime::fromTime_t( (*i)->dtStart().toTime_t() );
00129     QDateTime end = QDateTime::fromString( (*i)->dtEnd().toString(), Qt::ISODate );
00130     kDebug() << "start =" << start << "; end =" << end;
00131     mHistoryWidget->setItem( row, 1, new QTableWidgetItem( start.toString( "yyyy-MM-dd HH:mm:ss" ) ) );
00132     mHistoryWidget->setItem( row, 2, new QTableWidgetItem( end.toString( "yyyy-MM-dd HH:mm:ss" ) ) );
00133     mHistoryWidget->setItem( row, 4, new QTableWidgetItem( (*i)->uid() ) );
00134     kDebug() <<"(*i)->comments.count() ="  << (*i)->comments().count();
00135     if ( (*i)->comments().count() > 0 ) {
00136       mHistoryWidget->setItem( row, 3, new QTableWidgetItem( (*i)->comments().last() ) );
00137     }
00138   }
00139   mHistoryWidget->resizeColumnsToContents();
00140   mHistoryWidget->setColumnWidth( 1, 300 );
00141   mHistoryWidget->setColumnWidth( 2, 300 );
00142   setMinimumSize( mHistoryWidget->columnWidth( 0 ) 
00143                         + mHistoryWidget->columnWidth( 1 ) 
00144                         + mHistoryWidget->columnWidth( 2 ) 
00145                         + mHistoryWidget->columnWidth( 3 ), height() );
00146 }
00147 
00148 void EditHistoryDialog::historyWidgetCellChanged( int row, int col )
00149 {
00150   kDebug( 5970 ) << "Entering function";
00151   kDebug( 5970 ) << "row =" << row << " col =" << col;
00152   if ( mHistoryWidget->item( row, 4 ) ) 
00153   { // the user did the change, not the program
00154     if ( col == 1 ) 
00155     { // StartDate changed
00156       kDebug( 5970 ) << "user changed StartDate to" << mHistoryWidget->item( row, col )->text();
00157       QString uid = mHistoryWidget->item( row, 4 )->text();
00158       KCal::Event::List eventList = mParent->storage()->rawevents();
00159       for( KCal::Event::List::iterator i = eventList.begin(); i != eventList.end(); ++i ) 
00160       {
00161         kDebug(5970) << "row=" << row <<" col=" << col;
00162         if ( (*i)->uid() == uid ) 
00163         {
00164           if ( KDateTime::fromString( mHistoryWidget->item( row, col )->text() ).isValid() ) 
00165           {
00166             QDateTime datetime = QDateTime::fromString( mHistoryWidget->item( row, col )->text(), "yyyy-MM-dd HH:mm:ss" );
00167             KDateTime kdatetime = KDateTime::fromString( datetime.toString( Qt::ISODate ) );
00168             (*i)->setDtStart( kdatetime );
00169             mParent->reFreshTimes();
00170             kDebug(5970) <<"Program SetDtStart to" << mHistoryWidget->item( row, col )->text();
00171           }
00172           else 
00173             KMessageBox::information( 0, i18n( "This is not a valid Date/Time." ) );
00174         }
00175       }
00176     }
00177     if ( col == 2 ) 
00178     { // EndDate changed
00179       kDebug( 5970 ) <<"user changed EndDate to" << mHistoryWidget->item(row,col)->text();
00180       QString uid = mHistoryWidget->item( row, 4 )->text();
00181       KCal::Event::List eventList = mParent->storage()->rawevents();
00182       for( KCal::Event::List::iterator i = eventList.begin(); i != eventList.end(); ++i) 
00183       {
00184         kDebug() <<"row=" << row <<" col=" << col;
00185         if ( (*i)->uid() == uid ) 
00186         {
00187           if ( KDateTime::fromString( mHistoryWidget->item( row, col )->text() ).isValid() ) 
00188           {
00189             QDateTime datetime = QDateTime::fromString( mHistoryWidget->item( row, col )->text(), "yyyy-MM-dd HH:mm:ss" );
00190             KDateTime kdatetime = KDateTime::fromString( datetime.toString( Qt::ISODate ) );
00191             (*i)->setDtEnd( kdatetime );
00192             mParent->reFreshTimes();
00193             kDebug(5970) <<"Program SetDtEnd to" << mHistoryWidget->item( row, col )->text();
00194           }
00195           else 
00196             KMessageBox::information( 0, i18n( "This is not a valid Date/Time." ) );
00197         }
00198       }
00199     }
00200     if ( col == 3 ) { // Comment changed
00201       kDebug( 5970 ) <<"user changed Comment to" << mHistoryWidget->item(row,col)->text();
00202       QString uid = mHistoryWidget->item( row, 4 )->text();
00203       kDebug() <<"uid =" << uid;
00204       KCal::Event::List eventList = mParent->storage()->rawevents();
00205       for ( KCal::Event::List::iterator i = eventList.begin(); i != eventList.end(); ++i ) {
00206         kDebug() <<"row=" << row <<" col=" << col;
00207         if ( (*i)->uid() == uid ) {
00208           (*i)->addComment( mHistoryWidget->item( row, col )->text() ); 
00209           kDebug() <<"added" << mHistoryWidget->item( row, col )->text();
00210         }
00211       }
00212     }
00213   }
00214 }
00215 
00216 #include "edithistorydialog.moc"

ktimetracker

Skip menu "ktimetracker"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members

kdepim

Skip menu "kdepim"
  • akonadi
  •   clients
  •   kabc
  •   kcal
  •   kcm
  • akregator
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt
  • kdgantt1
  • kjots
  • kleopatra
  • kmail
  • kmobiletools
  • knode
  • knotes
  • kontact
  • kontactinterfaces
  • korganizer
  •   korgac
  • kpilot
  • ktimetracker
  •   doc
  • libkdepim
  • libkholidays
  • libkleo
  • libkpgp
  • maildir
Generated for kdepim by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal