-
Notifications
You must be signed in to change notification settings - Fork 3
/
EventTreeWidget.cpp
476 lines (381 loc) · 12.5 KB
/
EventTreeWidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#include <assert.h>
#include <QMenu>
#include <QMessageBox>
#include <QVariant>
#include "Event.h"
#include "EventTreeWidget.h"
#include "MainWindow.h"
#include "ProjectChooser.h"
#include "qtimemage.h"
#include "Report.h"
#include "util.h"
/*****************************************************************************/
/**************** EventTreeWidgetItem **************************************/
/*****************************************************************************/
const char* EventTreeWidgetItem::S_datetimeSec_fmt= "yyyy-MM-dd hh:mm:ss";
const char* EventTreeWidgetItem::S_datetimeMin_fmt= "yyyy-MM-dd hh:mm";
EventTreeWidgetItem::
EventTreeWidgetItem(int64_t event_id)
/*****************************************************************************
* Constructor
*/
{
Event *ev= G.eventTable[event_id];
Q_ASSERT(ev);
setData(0, Qt::UserRole, QVariant::fromValue(event_id));
setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable);
refresh();
}
EventTreeWidgetItem::
~EventTreeWidgetItem()
/*****************************************************************************
* Destructor
*/
{
}
void
EventTreeWidgetItem::
setText(int column, const QString &text)
/*****************************************************************************
* Set the text in a column of this item.
*/
{
setData(column, Qt::DisplayRole, text);
}
int64_t
EventTreeWidgetItem::
event_id() const
/*****************************************************************************
* Return the event_id for this item.
*/
{
QVariant qv= data(0, Qt::UserRole);
return qv.isValid() ? qv.toLongLong() : -1;
}
Event*
EventTreeWidgetItem::
getEvent() const
/*****************************************************************************
* Return Event this item represents.
*/
{
int64_t event_id= this->event_id();
assert(-1 != event_id);
return G.eventTable[event_id];
}
EventTreeWidgetItem*
EventTreeWidgetItem::
child(int ndx)
/*****************************************************************************
* Return child at index cast to our type.
*/
{
return static_cast<EventTreeWidgetItem*>(QTreeWidgetItem::child(ndx));
}
void
EventTreeWidgetItem::
refresh()
/*****************************************************************************
* Refresh the text contents of this item.
*/
{
Event *ev= getEvent();
Q_ASSERT(ev);
/* Set our columns of visible information */
setText(TYPE_COL, ev->type_str());
QDateTime qdt= QDateTime::fromSecsSinceEpoch(ev->when_ts());
setText(TIMESTAMP_COL, qdt.toString(S_datetimeSec_fmt));
}
/*****************************************************************************/
/**************** EventTreeWidget ******************************************/
/*****************************************************************************/
EventTreeWidget::
EventTreeWidget(QWidget *parent)
/*****************************************************************************
* Constructor
*/
: QTreeWidget(parent)
, _ignoreEditedEvent(false)
, _menu(NULL)
{
connect(&G.eventTable, SIGNAL(sig_insert(int64_t)), SLOT(event_insert(int64_t)));
connect(&G.eventTable, SIGNAL(sig_update(int64_t)), SLOT(event_update(int64_t)));
connect(&G.eventTable, SIGNAL(sig_remove(int64_t)), SLOT(event_remove(int64_t)));
setUniformRowHeights(true);
setSelectionMode(QAbstractItemView::ContiguousSelection);
QStringList headers;
headers << "Event Type" << "Time Stamp";
setColumnCount(EventTreeWidgetItem::N_COLS);
setHeaderLabels(headers);
setAlternatingRowColors(true);
/* Field the result of an edit */
connect(this, SIGNAL(itemChanged(QTreeWidgetItem*,int)), SLOT(editedEvent(QTreeWidgetItem*,int)));
/* Context menu */
setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(showEventCtxtMenu(const QPoint&)));
setSortingEnabled(false);
}
EventTreeWidget::
~EventTreeWidget()
/*****************************************************************************
* Destructor
*/
{}
EventTreeWidgetItem*
EventTreeWidget::
findItem(int64_t event_id)
/*****************************************************************************
* Find an item by it's event_id.
*/
{
EventTreeWidgetItem *parent= static_cast<EventTreeWidgetItem*>(invisibleRootItem());
EventTreeWidgetItem *item;
/* Search these children */
for(int i= 0; i < parent->childCount(); ++i) {
item= parent->child(i);
if(event_id == item->event_id()) return item;
}
return NULL;
}
void
EventTreeWidget::
editedEvent(QTreeWidgetItem *item, int col)
/******************************************************************************************
* An event got edited.
*/
{
if(_ignoreEditedEvent) return;
assert(item);
EventTreeWidgetItem *etwItem= static_cast<EventTreeWidgetItem*>(item);
Event *ev= etwItem->getEvent();
Q_ASSERT(ev);
switch(col) {
case 0:
{
QString txt= etwItem->text(col);
if(txt.contains("START", Qt::CaseInsensitive)) {
ev->set_type_enum(Event::START);
} else if(txt.contains("STOP", Qt::CaseInsensitive)) {
ev->set_type_enum(Event::STOP);
} else if(txt.contains("PAUSE", Qt::CaseInsensitive)) {
ev->set_type_enum(Event::PAUSE);
} else {
QMessageBox::warning(this,
"Invalid Input",
"Sorry, must be one of START, STOP, PAUSE",
QMessageBox::Ok);
etwItem->refresh();
/* Bail out early */
return;
}
} break;
case 1:
{
QString txt= etwItem->text(col);
QDateTime ts= QDateTime::fromString(txt, EventTreeWidgetItem::S_datetimeSec_fmt);
if(!ts.isValid())
ts= QDateTime::fromString(txt, EventTreeWidgetItem::S_datetimeMin_fmt);
if(!ts.isValid()) {
QMessageBox::warning(this,
"Invalid Input",
QString("Sorry, must be in the form %1, or %2")
.arg(EventTreeWidgetItem::S_datetimeMin_fmt)
.arg(EventTreeWidgetItem::S_datetimeSec_fmt),
QMessageBox::Ok);
etwItem->refresh();
/* Bail out early */
return;
}
ev->set_when_ts(ts);
} break;
}
/* If we get to here, input must have been valid, and ev contains new information. */
ev->update();
}
EventTreeWidgetItem*
EventTreeWidget::
itemAt(const QPoint &p) const
/******************************************************************************************
* Overload from QTreeWidget
*/
{
QTreeWidgetItem *item= QTreeWidget::itemAt(p);
return item ? static_cast<EventTreeWidgetItem*>(item) : NULL;
}
void
EventTreeWidget::
showEventCtxtMenu(const QPoint &pnt)
/******************************************************************************************
* Show the view tree view context menu.
*/
{
_slctn_lst= selectedItems();
if(!_slctn_lst.count()) return;
if(_menu) delete _menu;
_menu= new QMenu(this);
switch(_slctn_lst.count()) {
case 1:
switch(columnAt(pnt.x())) {
case EventTreeWidgetItem::TYPE_COL:
_menu->addAction("Edit Type", this, SLOT(editMenuEventType()));
break;
case EventTreeWidgetItem::TIMESTAMP_COL:
_menu->addAction("Edit Timestamp", this, SLOT(editMenuEventTimestamp()));
break;
}
default:
_menu->addAction("Move Event to another project", this, SLOT(moveEvents()));
break;
}
_menu->addAction("Delete Event(s)", this, SLOT(deleteMenuEvent()));
_menu->popup(mapToGlobal(pnt));
}
void
EventTreeWidget::
setProject(int64_t project_id)
/*****************************************************************************
* Populate the tree widget with projects.
*/
{
EventTreeWidgetItem *item;
int64_t event_id;
_project_id= project_id;
/* Make sure no items exist in the QTreeWidget */
clear();
if(-1 == project_id) return;
QVector<int64_t> event_id_vec;
int rtn = G.eventTable.fetchAll(event_id_vec, QString("WHERE project_id=%1 ORDER BY when_ts DESC").arg(project_id));
if (rtn < 0) qFatal("Event::fetchAll() failed!");
for (int i = 0; i < event_id_vec.size(); ++i) {
event_id= event_id_vec[i];
item= new EventTreeWidgetItem(event_id);
addTopLevelItem(item);
}
}
EventTreeWidgetItem*
EventTreeWidget::
slctnIndex2item(int ndx)
/******************************************************************************************
* Convert a selection index into a usefully cat item.
*/
{
QTreeWidgetItem *item= _slctn_lst[ndx];
assert(item);
return static_cast<EventTreeWidgetItem*>(item);
}
void
EventTreeWidget::
editMenuEventType()
/******************************************************************************************
* Menu pick was made to edit event type.
*/
{
editItem(slctnIndex2item(0), EventTreeWidgetItem::TYPE_COL);
}
void
EventTreeWidget::
editMenuEventTimestamp()
/******************************************************************************************
* Menu pick was made to edit event timestamp.
*/
{
editItem(slctnIndex2item(0), EventTreeWidgetItem::TIMESTAMP_COL);
}
void
EventTreeWidget::
deleteMenuEvent()
/******************************************************************************************
* Menu pick was made to delete event.
*/
{
int rtn = QMessageBox::warning(this,
"Delete Event",
"Are you sure you want to delete event(s)?",
QMessageBox::Ok | QMessageBox::Cancel,
QMessageBox::Cancel);
if (rtn != QMessageBox::Ok) return;
for(int i= 0; i < _slctn_lst.count(); ++i) {
EventTreeWidgetItem *item= slctnIndex2item(i);
Event *ev= G.eventTable[item->event_id()];
Q_ASSERT(ev);
if (ev->remove()) Q_ASSERT(0);
// G.eventTable will emit a signal, and event_remove() will get called.
}
}
void
EventTreeWidget::
moveEvents()
/************************************************************************************
* An event record was inserted.
*/
{
int rtn;
/* Run the picker modally */
ProjectChooser pc("Choose the Project to which you wish to move the selected events", G.pMw.data());
rtn = pc.exec();
if (rtn == QDialog::Accepted) {
int64_t dstPrj_id= pc.selectedProject_id();
for(int i= 0; i < _slctn_lst.count(); ++i) {
EventTreeWidgetItem *item= slctnIndex2item(i);
Event *ev= G.eventTable[item->event_id()];
Q_ASSERT(ev);
/* First make a copy of the old event, with the new
* project_id.
*/
Event newEvent;
newEvent.set_project_id(dstPrj_id);
newEvent.set_type_enum(ev->type_enum());
newEvent.set_when_ts(ev->when_ts());
/* Remove the old record */
if (ev->remove()) Q_ASSERT(0);
/* Insert the new record */
if(newEvent.insert()) Q_ASSERT(0);
// G.eventTable will emit a signal, and event_remove() will get called.
}
}
G.userEventsChangeNotice();
}
void
EventTreeWidget::
event_insert(int64_t event_id)
/************************************************************************************
* An event record was inserted.
*/
{
Event *ev= G.eventTable[event_id];
if(ev->project_id() != _project_id) return;
EventTreeWidgetItem *item= new EventTreeWidgetItem(event_id);
_ignoreEditedEvent= true;
insertTopLevelItem(0, item);
_ignoreEditedEvent= false;
}
void
EventTreeWidget::
event_update(int64_t event_id)
/************************************************************************************
* An event record was updated.
*/
{
Event *ev= G.eventTable[event_id];
if(ev->project_id() != _project_id) return;
EventTreeWidgetItem *item= findItem(event_id);
Q_ASSERT(item);
_ignoreEditedEvent= true;
item->refresh();
_ignoreEditedEvent= false;
}
void
EventTreeWidget::
event_remove(int64_t event_id)
/************************************************************************************
* An event record was removed.
*/
{
Event *ev= G.eventTable[event_id];
if(ev->project_id() != _project_id) return;
EventTreeWidgetItem *item= findItem(event_id);
Q_ASSERT(item);
int ndx= indexOfTopLevelItem(item);
Q_ASSERT(ndx != -1);
delete takeTopLevelItem(ndx);
}