2 * Copyright (C) 2007 by Thorsten Staerk and Mathias Soeken <msoeken@tzi.de>
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.
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.
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.
22 #include "historydialog.h"
23 #include "ui_historydialog.h"
25 #include <QItemDelegate>
26 #include <KDateTimeWidget>
27 #include <KMessageBox>
29 class HistoryWidgetDelegate
: public QItemDelegate
33 HistoryWidgetDelegate( QObject
*parent
= 0 ) : QItemDelegate( parent
) {}
35 QWidget
*createEditor( QWidget
*parent
, const QStyleOptionViewItem
&option
, const QModelIndex
&) const
37 KDateTimeWidget
*editor
= new KDateTimeWidget( parent
);
38 editor
->setAutoFillBackground( true );
39 editor
->setPalette( option
.palette
);
40 editor
->setBackgroundRole( QPalette::Background
);
44 void setEditorData( QWidget
*editor
, const QModelIndex
&index
) const
46 QDateTime dateTime
= QDateTime::fromString( index
.model()->data( index
, Qt::DisplayRole
).toString(), "yyyy-MM-dd HH:mm:ss" );
47 KDateTimeWidget
*dateTimeWidget
= static_cast<KDateTimeWidget
*>( editor
);
48 dateTimeWidget
->setDateTime( dateTime
);
51 void setModelData( QWidget
*editor
, QAbstractItemModel
*model
, const QModelIndex
&index
) const
53 KDateTimeWidget
*dateTimeWidget
= static_cast<KDateTimeWidget
*>( editor
);
54 QDateTime dateTime
= dateTimeWidget
->dateTime();
55 model
->setData( index
, dateTime
.toString( "yyyy-MM-dd HH:mm:ss" ), Qt::EditRole
);
58 void updateEditorGeometry( QWidget
*editor
, const QStyleOptionViewItem
&option
, const QModelIndex
&) const
60 editor
->setGeometry( option
.rect
);
64 historydialog::historydialog(TaskView
*parent
) :
66 m_ui(new Ui::historydialog
)
70 /* Item Delegate for displaying KDateTimeWidget instead of QLineEdit */
71 HistoryWidgetDelegate
*historyWidgetDelegate
= new HistoryWidgetDelegate( m_ui
->historytablewidget
);
72 m_ui
->historytablewidget
->setItemDelegateForColumn( 1, historyWidgetDelegate
);
73 m_ui
->historytablewidget
->setItemDelegateForColumn( 2, historyWidgetDelegate
);
75 m_ui
->historytablewidget
->setEditTriggers( QAbstractItemView::AllEditTriggers
);
76 m_ui
->historytablewidget
->setColumnCount(5);
77 m_ui
->historytablewidget
->setHorizontalHeaderLabels(
78 QStringList() << i18n( "Task" ) << i18n( "StartTime" ) << i18n( "EndTime" )
79 << i18n( "Comment" ) << QString( "event UID" ) );
80 m_ui
->historytablewidget
->horizontalHeader()->setStretchLastSection( true );
81 m_ui
->historytablewidget
->setColumnHidden( 4, true ); // hide the "UID" column
83 m_ui
->historytablewidget
->setSortingEnabled( true );
84 m_ui
->historytablewidget
->sortItems( 1, Qt::DescendingOrder
);
85 m_ui
->historytablewidget
->resizeColumnsToContents();
88 historydialog::~historydialog()
93 QString
historydialog::listallevents()
95 QString err
=QString();
96 // if sorting is enabled and we write to row x, we cannot be sure row x will be in row x some lines later
97 bool old_sortingenabled
=m_ui
->historytablewidget
->isSortingEnabled();
98 m_ui
->historytablewidget
->setSortingEnabled( false );
99 connect( m_ui
->historytablewidget
, SIGNAL( cellChanged( int, int ) ),
100 this, SLOT( historyWidgetCellChanged( int, int ) ) );
102 KCal::Event::List eventList
= mparent
->storage()->rawevents();
103 for ( KCal::Event::List::iterator i
= eventList
.begin();
104 i
!= eventList
.end(); ++i
)
106 int row
= m_ui
->historytablewidget
->rowCount();
107 m_ui
->historytablewidget
->insertRow( row
);
108 QTableWidgetItem
* item
=0;
109 if ( (*i
)->relatedTo() ) // maybe the file is corrupt and (*i)->relatedTo is NULL
111 item
= new QTableWidgetItem( (*i
)->relatedTo()->summary() );
112 item
->setFlags( Qt::ItemIsEnabled
);
113 item
->setWhatsThis( i18n( "You can change this task's comment, start time and end time." ) );
114 m_ui
->historytablewidget
->setItem( row
, 0, item
);
115 // dtStart is stored like DTSTART;TZID=Europe/Berlin:20080327T231056
116 // dtEnd is stored like DTEND:20080327T231509Z
117 // we need to handle both differently
118 QDateTime start
= QDateTime::fromTime_t( (*i
)->dtStart().toTime_t() );
119 QDateTime end
= QDateTime::fromString( (*i
)->dtEnd().toString(), Qt::ISODate
);
120 kDebug() << "start =" << start
<< "; end =" << end
;
121 m_ui
->historytablewidget
->setItem( row
, 1, new QTableWidgetItem( start
.toString( "yyyy-MM-dd HH:mm:ss" ) ) );
122 m_ui
->historytablewidget
->setItem( row
, 2, new QTableWidgetItem( end
.toString( "yyyy-MM-dd HH:mm:ss" ) ) );
123 m_ui
->historytablewidget
->setItem( row
, 4, new QTableWidgetItem( (*i
)->uid() ) );
124 kDebug() <<"(*i)->comments.count() =" << (*i
)->comments().count();
125 if ( (*i
)->comments().count() > 0 )
127 m_ui
->historytablewidget
->setItem( row
, 3, new QTableWidgetItem( (*i
)->comments().last() ) );
132 kDebug(5970) << "There is no 'relatedTo' entry for " << (*i
)->summary();
133 err
="NoRelatedToForEvent";
136 m_ui
->historytablewidget
->resizeColumnsToContents();
137 m_ui
->historytablewidget
->setColumnWidth( 1, 300 );
138 m_ui
->historytablewidget
->setColumnWidth( 2, 300 );
139 setMinimumSize( m_ui
->historytablewidget
->columnWidth( 0 )
140 + m_ui
->historytablewidget
->columnWidth( 1 )
141 + m_ui
->historytablewidget
->columnWidth( 2 )
142 + m_ui
->historytablewidget
->columnWidth( 3 ), height() );
143 m_ui
->historytablewidget
->setSortingEnabled(old_sortingenabled
);
147 void historydialog::changeEvent(QEvent
*e
)
149 QDialog::changeEvent(e
);
152 case QEvent::LanguageChange
:
153 m_ui
->retranslateUi(this);
160 void historydialog::historyWidgetCellChanged( int row
, int col
)
162 kDebug( 5970 ) << "Entering function";
163 kDebug( 5970 ) << "row =" << row
<< " col =" << col
;
164 if ( m_ui
->historytablewidget
->item( row
, 4 ) )
165 { // the user did the change, not the program
167 { // StartDate changed
168 kDebug( 5970 ) << "user changed StartDate to" << m_ui
->historytablewidget
->item( row
, col
)->text();
169 QString uid
= m_ui
->historytablewidget
->item( row
, 4 )->text();
170 KCal::Event::List eventList
= mparent
->storage()->rawevents();
171 for( KCal::Event::List::iterator i
= eventList
.begin(); i
!= eventList
.end(); ++i
)
173 kDebug(5970) << "row=" << row
<<" col=" << col
;
174 if ( (*i
)->uid() == uid
)
176 if ( KDateTime::fromString( m_ui
->historytablewidget
->item( row
, col
)->text() ).isValid() )
178 QDateTime datetime
= QDateTime::fromString( m_ui
->historytablewidget
->item( row
, col
)->text(), "yyyy-MM-dd HH:mm:ss" );
179 KDateTime kdatetime
= KDateTime::fromString( datetime
.toString( Qt::ISODate
) );
180 (*i
)->setDtStart( kdatetime
);
181 mparent
->reFreshTimes();
182 kDebug(5970) <<"Program SetDtStart to" << m_ui
->historytablewidget
->item( row
, col
)->text();
185 KMessageBox::information( 0, i18n( "This is not a valid Date/Time." ) );
191 kDebug( 5970 ) <<"user changed EndDate to" << m_ui
->historytablewidget
->item(row
,col
)->text();
192 QString uid
= m_ui
->historytablewidget
->item( row
, 4 )->text();
193 KCal::Event::List eventList
= mparent
->storage()->rawevents();
194 for( KCal::Event::List::iterator i
= eventList
.begin(); i
!= eventList
.end(); ++i
)
196 kDebug() <<"row=" << row
<<" col=" << col
;
197 if ( (*i
)->uid() == uid
)
199 if ( KDateTime::fromString( m_ui
->historytablewidget
->item( row
, col
)->text() ).isValid() )
201 QDateTime datetime
= QDateTime::fromString( m_ui
->historytablewidget
->item( row
, col
)->text(), "yyyy-MM-dd HH:mm:ss" );
202 KDateTime kdatetime
= KDateTime::fromString( datetime
.toString( Qt::ISODate
) );
203 (*i
)->setDtEnd( kdatetime
);
204 mparent
->reFreshTimes();
205 kDebug(5970) <<"Program SetDtEnd to" << m_ui
->historytablewidget
->item( row
, col
)->text();
208 KMessageBox::information( 0, i18n( "This is not a valid Date/Time." ) );
214 kDebug( 5970 ) <<"user changed Comment to" << m_ui
->historytablewidget
->item(row
,col
)->text();
215 QString uid
= m_ui
->historytablewidget
->item( row
, 4 )->text();
216 kDebug() <<"uid =" << uid
;
217 KCal::Event::List eventList
= mparent
->storage()->rawevents();
218 for ( KCal::Event::List::iterator i
= eventList
.begin(); i
!= eventList
.end(); ++i
)
220 kDebug() <<"row=" << row
<<" col=" << col
;
221 if ( (*i
)->uid() == uid
)
223 (*i
)->addComment( m_ui
->historytablewidget
->item( row
, col
)->text() );
224 kDebug() <<"added" << m_ui
->historytablewidget
->item( row
, col
)->text();
231 QString
historydialog::refresh()
234 while (m_ui
->historytablewidget
->rowCount()>0)
235 m_ui
->historytablewidget
->removeRow(0);
240 #include "historydialog.moc"
242 void historydialog::on_deletepushbutton_clicked()
244 if (m_ui
->historytablewidget
->item( m_ui
->historytablewidget
->currentRow(), 4))
245 { // if an item is current
246 QString uid
= m_ui
->historytablewidget
->item( m_ui
->historytablewidget
->currentRow(), 4 )->text();
247 kDebug() <<"uid =" << uid
;
248 KCal::Event::List eventList
= mparent
->storage()->rawevents();
249 for ( KCal::Event::List::iterator i
= eventList
.begin(); i
!= eventList
.end(); ++i
)
251 if ( (*i
)->uid() == uid
)
253 kDebug(5970) << "removing uid " << (*i
)->uid();
254 mparent
->storage()->removeEvent((*i
)->uid());
255 mparent
->reFreshTimes();
260 else KMessageBox::information(this, "Please select a task to delete.");