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

ktimetracker

  • sources
  • kde-4.12
  • kdepim
  • ktimetracker
historydialog.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007 by Thorsten Staerk and Mathias Soeken <msoeken@tzi.de>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the
16  * Free Software Foundation, Inc.
17  * 51 Franklin Street, Fifth Floor
18  * Boston, MA 02110-1301 USA.
19  *
20  */
21 
22 #include "historydialog.h"
23 #include "ui_historydialog.h"
24 #include "taskview.h"
25 #include "kttcalendar.h"
26 
27 #include <QItemDelegate>
28 #include <KDateTimeWidget>
29 #include <KMessageBox>
30 #include <KDebug>
31 
32 class HistoryWidgetDelegate : public QItemDelegate
33 {
34 
35 public:
36  HistoryWidgetDelegate( QObject *parent = 0 ) : QItemDelegate( parent ) {}
37 
38  QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &) const
39  {
40  KDateTimeWidget *editor = new KDateTimeWidget( parent );
41  editor->setAutoFillBackground( true );
42  editor->setPalette( option.palette );
43  editor->setBackgroundRole( QPalette::Background );
44  return editor;
45  }
46 
47  void setEditorData( QWidget *editor, const QModelIndex &index ) const
48  {
49  QDateTime dateTime = QDateTime::fromString( index.model()->data( index, Qt::DisplayRole ).toString(), "yyyy-MM-dd HH:mm:ss" );
50  KDateTimeWidget *dateTimeWidget = static_cast<KDateTimeWidget*>( editor );
51  dateTimeWidget->setDateTime( dateTime );
52  }
53 
54  void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
55  {
56  KDateTimeWidget *dateTimeWidget = static_cast<KDateTimeWidget*>( editor );
57  QDateTime dateTime = dateTimeWidget->dateTime();
58  model->setData( index, dateTime.toString( "yyyy-MM-dd HH:mm:ss" ), Qt::EditRole );
59  }
60 
61  void updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const
62  {
63  editor->setGeometry( option.rect );
64  }
65 };
66 
67 historydialog::historydialog(TaskView *parent) :
68  QDialog(parent),
69  m_ui(new Ui::historydialog)
70 {
71  mparent=parent;
72  m_ui->setupUi(this);
73  /* Item Delegate for displaying KDateTimeWidget instead of QLineEdit */
74  HistoryWidgetDelegate *historyWidgetDelegate = new HistoryWidgetDelegate( m_ui->historytablewidget );
75  m_ui->historytablewidget->setItemDelegateForColumn( 1, historyWidgetDelegate );
76  m_ui->historytablewidget->setItemDelegateForColumn( 2, historyWidgetDelegate );
77 
78  m_ui->historytablewidget->setEditTriggers( QAbstractItemView::AllEditTriggers );
79  m_ui->historytablewidget->setColumnCount(5);
80  m_ui->historytablewidget->setHorizontalHeaderLabels(
81  QStringList() << i18n( "Task" ) << i18n( "StartTime" ) << i18n( "EndTime" )
82  << i18n( "Comment" ) << QString( "event UID" ) );
83  m_ui->historytablewidget->horizontalHeader()->setStretchLastSection( true );
84  m_ui->historytablewidget->setColumnHidden( 4, true ); // hide the "UID" column
85  listallevents();
86  m_ui->historytablewidget->setSortingEnabled( true );
87  m_ui->historytablewidget->sortItems( 1, Qt::DescendingOrder );
88  m_ui->historytablewidget->resizeColumnsToContents();
89 }
90 
91 historydialog::~historydialog()
92 {
93  delete m_ui;
94 }
95 
96 QString historydialog::listallevents()
97 {
98  QString err=QString();
99  // if sorting is enabled and we write to row x, we cannot be sure row x will be in row x some lines later
100  bool old_sortingenabled=m_ui->historytablewidget->isSortingEnabled();
101  m_ui->historytablewidget->setSortingEnabled( false );
102  connect( m_ui->historytablewidget, SIGNAL(cellChanged(int,int)),
103  this, SLOT(historyWidgetCellChanged(int,int)) );
104 
105  KCalCore::Event::List eventList = mparent->storage()->rawevents();
106  KTimeTracker::KTTCalendar::Ptr calendar = mparent->storage()->calendar();
107  for ( KCalCore::Event::List::iterator i = eventList.begin();
108  i != eventList.end(); ++i )
109  {
110  int row = m_ui->historytablewidget->rowCount();
111  m_ui->historytablewidget->insertRow( row );
112  QTableWidgetItem* item=0;
113  if ( !(*i)->relatedTo().isEmpty() ) // maybe the file is corrupt and (*i)->relatedTo is NULL
114  {
115  KCalCore::Incidence::Ptr parent = calendar ? calendar->incidence( (*i)->relatedTo() ) : KCalCore::Incidence::Ptr();
116  item = new QTableWidgetItem( parent ? parent->summary() : (*i)->summary() );
117  item->setFlags( Qt::ItemIsEnabled );
118  item->setWhatsThis( i18n( "You can change this task's comment, start time and end time." ) );
119  m_ui->historytablewidget->setItem( row, 0, item );
120  // dtStart is stored like DTSTART;TZID=Europe/Berlin:20080327T231056
121  // dtEnd is stored like DTEND:20080327T231509Z
122  // we need to handle both differently
123  QDateTime start = QDateTime::fromTime_t( (*i)->dtStart().toTime_t() );
124  QDateTime end = QDateTime::fromString( (*i)->dtEnd().toString(), Qt::ISODate );
125  kDebug() << "start =" << start << "; end =" << end;
126  m_ui->historytablewidget->setItem( row, 1, new QTableWidgetItem( start.toString( "yyyy-MM-dd HH:mm:ss" ) ) );
127  m_ui->historytablewidget->setItem( row, 2, new QTableWidgetItem( end.toString( "yyyy-MM-dd HH:mm:ss" ) ) );
128  m_ui->historytablewidget->setItem( row, 4, new QTableWidgetItem( (*i)->uid() ) );
129  kDebug() <<"(*i)->comments.count() =" << (*i)->comments().count();
130  if ( (*i)->comments().count() > 0 )
131  {
132  m_ui->historytablewidget->setItem( row, 3, new QTableWidgetItem( (*i)->comments().last() ) );
133  }
134  }
135  else
136  {
137  kDebug(5970) << "There is no 'relatedTo' entry for " << (*i)->summary();
138  err="NoRelatedToForEvent";
139  }
140  }
141  m_ui->historytablewidget->resizeColumnsToContents();
142  m_ui->historytablewidget->setColumnWidth( 1, 300 );
143  m_ui->historytablewidget->setColumnWidth( 2, 300 );
144  setMinimumSize( m_ui->historytablewidget->columnWidth( 0 )
145  + m_ui->historytablewidget->columnWidth( 1 )
146  + m_ui->historytablewidget->columnWidth( 2 )
147  + m_ui->historytablewidget->columnWidth( 3 ), height() );
148  m_ui->historytablewidget->setSortingEnabled(old_sortingenabled);
149  return err;
150 }
151 
152 void historydialog::changeEvent(QEvent *e)
153 {
154  QDialog::changeEvent(e);
155  switch (e->type())
156  {
157  case QEvent::LanguageChange:
158  m_ui->retranslateUi(this);
159  break;
160  default:
161  break;
162  }
163 }
164 
165 void historydialog::historyWidgetCellChanged( int row, int col )
166 {
167  kDebug( 5970 ) << "Entering function";
168  kDebug( 5970 ) << "row =" << row << " col =" << col;
169  if ( m_ui->historytablewidget->item( row, 4 ) )
170  { // the user did the change, not the program
171  if ( col == 1 )
172  { // StartDate changed
173  kDebug( 5970 ) << "user changed StartDate to" << m_ui->historytablewidget->item( row, col )->text();
174  QString uid = m_ui->historytablewidget->item( row, 4 )->text();
175  KCalCore::Event::List eventList = mparent->storage()->rawevents();
176  for( KCalCore::Event::List::iterator i = eventList.begin(); i != eventList.end(); ++i )
177  {
178  kDebug(5970) << "row=" << row <<" col=" << col;
179  if ( (*i)->uid() == uid )
180  {
181  if ( KDateTime::fromString( m_ui->historytablewidget->item( row, col )->text() ).isValid() )
182  {
183  QDateTime datetime = QDateTime::fromString( m_ui->historytablewidget->item( row, col )->text(), "yyyy-MM-dd HH:mm:ss" );
184  KDateTime kdatetime = KDateTime::fromString( datetime.toString( Qt::ISODate ) );
185  (*i)->setDtStart( kdatetime );
186  mparent->reFreshTimes();
187  kDebug(5970) <<"Program SetDtStart to" << m_ui->historytablewidget->item( row, col )->text();
188  }
189  else
190  KMessageBox::information( 0, i18n( "This is not a valid Date/Time." ) );
191  }
192  }
193  }
194  if ( col == 2 )
195  { // EndDate changed
196  kDebug( 5970 ) <<"user changed EndDate to" << m_ui->historytablewidget->item(row,col)->text();
197  QString uid = m_ui->historytablewidget->item( row, 4 )->text();
198  KCalCore::Event::List eventList = mparent->storage()->rawevents();
199  for( KCalCore::Event::List::iterator i = eventList.begin(); i != eventList.end(); ++i)
200  {
201  kDebug() <<"row=" << row <<" col=" << col;
202  if ( (*i)->uid() == uid )
203  {
204  if ( KDateTime::fromString( m_ui->historytablewidget->item( row, col )->text() ).isValid() )
205  {
206  QDateTime datetime = QDateTime::fromString( m_ui->historytablewidget->item( row, col )->text(), "yyyy-MM-dd HH:mm:ss" );
207  KDateTime kdatetime = KDateTime::fromString( datetime.toString( Qt::ISODate ) );
208  (*i)->setDtEnd( kdatetime );
209  mparent->reFreshTimes();
210  kDebug(5970) <<"Program SetDtEnd to" << m_ui->historytablewidget->item( row, col )->text();
211  }
212  else
213  KMessageBox::information( 0, i18n( "This is not a valid Date/Time." ) );
214  }
215  }
216  }
217  if ( col == 3 )
218  { // Comment changed
219  kDebug( 5970 ) <<"user changed Comment to" << m_ui->historytablewidget->item(row,col)->text();
220  QString uid = m_ui->historytablewidget->item( row, 4 )->text();
221  kDebug() <<"uid =" << uid;
222  KCalCore::Event::List eventList = mparent->storage()->rawevents();
223  for ( KCalCore::Event::List::iterator i = eventList.begin(); i != eventList.end(); ++i )
224  {
225  kDebug() <<"row=" << row <<" col=" << col;
226  if ( (*i)->uid() == uid )
227  {
228  (*i)->addComment( m_ui->historytablewidget->item( row, col )->text() );
229  kDebug() <<"added" << m_ui->historytablewidget->item( row, col )->text();
230  }
231  }
232  }
233  }
234 }
235 
236 QString historydialog::refresh()
237 {
238  QString err;
239  while (m_ui->historytablewidget->rowCount()>0)
240  m_ui->historytablewidget->removeRow(0);
241  listallevents();
242  return err;
243 }
244 
245 #include "historydialog.moc"
246 
247 void historydialog::on_deletepushbutton_clicked()
248 {
249  if (m_ui->historytablewidget->item( m_ui->historytablewidget->currentRow(), 4))
250  { // if an item is current
251  QString uid = m_ui->historytablewidget->item( m_ui->historytablewidget->currentRow(), 4 )->text();
252  kDebug() <<"uid =" << uid;
253  KCalCore::Event::List eventList = mparent->storage()->rawevents();
254  for ( KCalCore::Event::List::iterator i = eventList.begin(); i != eventList.end(); ++i )
255  {
256  if ( (*i)->uid() == uid )
257  {
258  kDebug(5970) << "removing uid " << (*i)->uid();
259  mparent->storage()->removeEvent((*i)->uid());
260  mparent->reFreshTimes();
261  this->refresh();
262  }
263  }
264  }
265  else KMessageBox::information(this, i18n("Please select a task to delete."));
266 }
267 
268 void historydialog::on_okpushbutton_clicked()
269 {
270  m_ui->historytablewidget->setCurrentCell(0,0); // you need to change the cell to store the value
271  close();
272 }
historydialog::historydialog
historydialog(TaskView *parent)
Definition: historydialog.cpp:67
historydialog.h
KTimeTracker::KTTCalendar::Ptr
QSharedPointer< KTimeTracker::KTTCalendar > Ptr
Definition: kttcalendar.h:32
QDialog
QWidget
TaskView::reFreshTimes
QString reFreshTimes()
Refresh the times of the tasks, e.g.
Definition: taskview.cpp:548
QObject
historydialog::listallevents
QString listallevents()
Definition: historydialog.cpp:96
taskview.h
historydialog::changeEvent
void changeEvent(QEvent *e)
Definition: historydialog.cpp:152
TaskView::storage
timetrackerstorage * storage()
Returns a pointer to storage object.
Definition: taskview.cpp:387
timetrackerstorage::rawevents
KCalCore::Event::List rawevents()
list of all events
Definition: timetrackerstorage.cpp:320
historydialog::~historydialog
~historydialog()
Definition: historydialog.cpp:91
historydialog::refresh
QString refresh()
Definition: historydialog.cpp:236
timetrackerstorage::calendar
KTimeTracker::KTTCalendar::Ptr calendar() const
Definition: timetrackerstorage.cpp:1073
timetrackerstorage::removeEvent
QString removeEvent(QString uid)
Definition: timetrackerstorage.cpp:494
historydialog
Definition: historydialog.h:33
kttcalendar.h
TaskView
Container and interface for the tasks.
Definition: taskview.h:50
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:55:10 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

ktimetracker

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

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal