-
Notifications
You must be signed in to change notification settings - Fork 3
/
ClientPage.cpp
731 lines (592 loc) · 19.4 KB
/
ClientPage.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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
#include <QSplitter>
#include <QTimer>
#include <QMenu>
#include <QGroupBox>
#include <QBoxLayout>
#include <QLabel>
#include <QToolBox>
#include <QTreeWidgetItem>
#include <QToolButton>
#include <QMessageBox>
#include <QShowEvent>
#include <QDate>
#include "qtimemage.h"
#include "ProjectTreeWidget.h"
#include "Report.h"
#include "Project.h"
#include "ProjectEditor.h"
#include "ProjectPage.h"
#include "Client.h"
#include "configItem.h"
#include "util.h"
#include "WeekChooser.h"
#include "MonthChooser.h"
#include "ClientPage.h"
static configIntVec S_spltSettings("ClientPage_splitter_settings");
static configIntVec S_projectHdrSettings("ClientPage_ProjectsTree_header_settings");
ClientPage::
ClientPage(QWidget *parent)
/******************************************************************************************
* Constructor
*/
: QWidget(parent),
_do_storeSettings(false)
{
_client.id= -1;
/* Geometry management */
connect(&G, SIGNAL(sig_aboutToSaveSettings()), SLOT(storeSettings()));
/* Record notifications */
connect(&G.projectTable, SIGNAL(sig_insert(int64_t)), SLOT(project_insert(int64_t)));
connect(&G.projectTable, SIGNAL(sig_update(int64_t)), SLOT(project_update(int64_t)));
connect(&G.projectTable, SIGNAL(sig_remove(int64_t)), SLOT(project_remove(int64_t)));
connect(&G.eventTable, SIGNAL(sig_insert(int64_t)), SLOT(refreshTimes()));
connect(&G.eventTable, SIGNAL(sig_update(int64_t)), SLOT(refreshTimes()));
connect(&G.eventTable, SIGNAL(sig_remove(int64_t)), SLOT(refreshTimes()));
{ /******* Create a project menu *********/
_project.menu = new QMenu(this);
/* Create */
_project.create_act= _project.menu->addAction(QIcon::fromTheme("document-new"), "New Project");
connect(_project.create_act, SIGNAL(triggered()), SLOT(createProject()));
/* Create Sub */
_project.createSubproject_act= _project.menu->addAction(QIcon::fromTheme("contact-new"), "New Subproject");
_project.createSubproject_act->setEnabled(false);
connect(_project.createSubproject_act, SIGNAL(triggered()), SLOT(createSubproject()));
/* Edit */
_project.edit_act = _project.menu->addAction(QIcon::fromTheme("document-properties"), "Edit Project");
_project.edit_act->setEnabled(false);
connect(_project.edit_act, SIGNAL(triggered()), SLOT(editProject()));
/* Delete */
_project.delete_act = _project.menu->addAction(QIcon::fromTheme("edit-delete"), "Delete Project");
_project.delete_act->setEnabled(false);
connect(_project.delete_act, SIGNAL(triggered()), SLOT(deleteProject()));
}
{ /******* Create a client report menu *********/
_client.report.menu = new QMenu(this);
/* Weekly */
_client.report.weekly_act= _client.report.menu->addAction("Weekly Report");
connect(_client.report.weekly_act, SIGNAL(triggered()), SLOT(weeklyReport()));
/* Monthly */
_client.report.monthly_act= _client.report.menu->addAction("Monthly Report");
connect(_client.report.monthly_act, SIGNAL(triggered()), SLOT(monthlyReport()));
/* Custom */
_client.report.custom_act= _client.report.menu->addAction("Custom Report");
connect(_client.report.custom_act, SIGNAL(triggered()), SLOT(customReport()));
}
/******** Static GUI components **********/
QVBoxLayout *vbl= new QVBoxLayout(this);
{ /* Client area */
QLabel *lbl;
/* Client's name will be shown as the title of the QGroupBox */
_client.gb= new QGroupBox(this);
_client.gb->setAlignment(Qt::AlignHCenter);
vbl->addWidget(_client.gb);
QVBoxLayout *vbl= new QVBoxLayout;
_client.gb->setLayout(vbl);
{ /* 1st row */
QHBoxLayout *hbl= new QHBoxLayout;
vbl->addLayout(hbl);
/* Project menu button */
QToolButton *tool_btn= new QToolButton(_client.gb);
tool_btn->setMenu(_project.menu);
tool_btn->setPopupMode(QToolButton::DelayedPopup);
tool_btn->setDefaultAction(_project.create_act);
hbl->addWidget(tool_btn);
hbl->addStretch(10);
/* Rate for client */
lbl= new QLabel("<b>Base Rate:</b> $", _client.gb);
hbl->addWidget(lbl);
_client.rate_lbl= new QLabel(_client.gb);
hbl->addWidget(_client.rate_lbl);
hbl->addStretch(10);
/* Default charge quantum for client */
lbl= new QLabel("<b>Min. Billing (min):</b>", _client.gb);
hbl->addWidget(lbl);
_client.charge_quantum_lbl= new QLabel(_client.gb);
hbl->addWidget(_client.charge_quantum_lbl);
hbl->addStretch(10);
tool_btn= new QToolButton(_client.gb);
tool_btn->setMenu(_client.report.menu);
// tool_btn->setPopupMode(QToolButton::DelayedPopup);
tool_btn->setDefaultAction(_client.report.weekly_act);
hbl->addWidget(tool_btn);
}
{ /* 2nd row */
QHBoxLayout *hbl= new QHBoxLayout;
vbl->addLayout(hbl);
lbl= new QLabel("<b>Total Hours for Today:</b>", _client.gb);
hbl->addWidget(lbl);
_client.timeToday_lbl= new QLabel(_client.gb);
hbl->addWidget(_client.timeToday_lbl);
hbl->addStretch(10);
lbl= new QLabel("<b>This Week:</b>", _client.gb);
hbl->addWidget(lbl);
_client.timeWeek_lbl= new QLabel(_client.gb);
hbl->addWidget(_client.timeWeek_lbl);
hbl->addStretch(10);
lbl= new QLabel("<b>This Month:</b>", _client.gb);
hbl->addWidget(lbl);
_client.timeMonth_lbl= new QLabel(_client.gb);
hbl->addWidget(_client.timeMonth_lbl);
hbl->addStretch(10);
lbl= new QLabel("<b>This Year:</b>", _client.gb);
hbl->addWidget(lbl);
_client.timeYear_lbl= new QLabel(_client.gb);
hbl->addWidget(_client.timeYear_lbl);
hbl->addStretch(10);
}
}
_splt= new QSplitter(this);
_splt->setOrientation(Qt::Vertical);
vbl->addWidget(_splt);
{ /* Tree view of projects */
QWidget *w= new QWidget;
_splt->addWidget(w);
QHBoxLayout *hbl= new QHBoxLayout(w);
_project.trw= new ProjectTreeWidget(w);
hbl->addWidget(_project.trw);
_project.trw->setMenu(_project.menu);
/* Relay the signal out */
connect(_project.trw, SIGNAL(sig_doubleClickedProject(int64_t)), SIGNAL(sig_doubleClickedProject(int64_t)));
connect(_project.trw, SIGNAL(itemSelectionChanged()), SLOT(projectSelectionChanged()));
}
{ /* Project page */
_project.page= new ProjectPage(this);
_splt->addWidget(_project.page);
}
/* Arrange for updates in the future */
connect(&G.clientTable, SIGNAL(sig_update(int64_t)), SLOT(client_update(int64_t)));
/* Find out when project gets selected */
connect(_project.trw, SIGNAL(itemSelectionChanged()), SLOT(setCurrentItem()));
_refreshTimer= new QTimer(this);
_refreshTimer->setInterval(180*1000);
connect(_refreshTimer, SIGNAL(timeout()), this, SLOT(refreshTimes()));
}
ClientPage::
~ClientPage()
/******************************************************************************************
* Destructor
*/
{
}
void
ClientPage::
projectSelectionChanged()
/******************************************************************************************
* The project selection has changed.
*/
{
//J_DBG_FN;
emit sig_projectSelected(_client.id, _project.trw->selectedProject_id());
}
void
ClientPage::
refreshTimes()
/******************************************************************************************
* Refresh the project times
*/
{
QDateTime dtNow,
dtBeginDay,
dtBeginWeek,
dtBeginMonth,
dtBeginYear;
dtNow= QDateTime::currentDateTime();
/* Initialize to the current time */
QDate currDate= dtNow.date(),
beginMonthDate;
dtBeginDay.setDate(currDate);
dtBeginWeek.setDate(currDate.addDays(-currDate.dayOfWeek()+1));
beginMonthDate= currDate.addDays(-currDate.day()+1);
dtBeginMonth.setDate(beginMonthDate);
dtBeginYear.setDate(beginMonthDate.addMonths(-beginMonthDate.month()+1));
#if 0
J_DBG_FN << "beginDay= " << dtBeginDay.toString();
J_DBG_FN << "beginWeek= " << dtBeginWeek.toString();
J_DBG_FN << "beginMonthDate= " << dtBeginMonth.toString();
J_DBG_FN << "beginYear= " << dtBeginYear.toString();
#endif
/* Put these here to avoid repetition in reporting functions */
ClientReport dayRpt(_client.id, dtBeginDay, dtNow),
wkRpt(_client.id, dtBeginWeek, dtNow),
moRpt(_client.id, dtBeginMonth, dtNow),
yrRpt(_client.id, dtBeginYear, dtNow);
_project.trw->refreshTimes(dayRpt, wkRpt, moRpt, yrRpt);
int decihours;
decihours= dayRpt.cumulativeDecihours();
_client.timeToday_lbl->setText(decihour2hrStr(decihours));
decihours= wkRpt.cumulativeDecihours();
_client.timeWeek_lbl->setText(decihour2hrStr(decihours));
decihours= moRpt.cumulativeDecihours();
_client.timeMonth_lbl->setText(decihour2hrStr(decihours));
decihours= yrRpt.cumulativeDecihours();
_client.timeYear_lbl->setText(decihour2hrStr(decihours));
}
void
ClientPage::
showEvent(QShowEvent *event)
/*******************************************************************************************
* Reimplementation of QWidget function.
*/
{
if(event->type() == QEvent::Show) {
{ /* Splitter settings */
QList<int> lst;
for(int i= 0; i < S_spltSettings.count() && i < _splt->count(); ++i) {
lst.push_back(S_spltSettings[i]);
}
_splt->setSizes(lst);
}
/* Project QTreeWidget settings */
for(int i= 0; i < _project.trw->columnCount() && i < S_projectHdrSettings.count(); ++i) {
_project.trw->setColumnWidth(i, S_projectHdrSettings[i]);
}
refreshTimes();
_refreshTimer->start();
_do_storeSettings= true;
}
/* Call the parent's showEvent() */
QWidget::showEvent(event);
}
void
ClientPage::
hideEvent(QHideEvent * event)
/*******************************************************************************************
* Reimplementation of QWidget function.
*/
{
if(event->type() == QEvent::Hide) {
storeSettings();
_refreshTimer->stop();
_do_storeSettings= false;
}
/* Call parent's function */
QWidget::hideEvent(event);
}
void
ClientPage::
storeSettings()
/*****************************************************************************
* Store settings if we should.
*/
{
if(!_do_storeSettings) return;
{ /* Splitter */
QList<int> lst= _splt->sizes();
S_spltSettings.resize(lst.count());
for(int i= 0; i < lst.count(); ++i) {
S_spltSettings[i]= lst[i];
}
}
/* project QTreeWidget */
S_projectHdrSettings.resize(_project.trw->columnCount());
for(int i= 0; i < _project.trw->columnCount(); ++i) {
S_projectHdrSettings[i]= _project.trw->columnWidth(i);
}
}
void
ClientPage::
setClient(int64_t client_id, int64_t currentProject_id)
/******************************************************************************************
* Set display to the supplied client, which may be -1
*/
{
/* client_update() needs this to work correctly */
_client.id= client_id;
client_update(client_id);
if(_client.id != -1) {
populate_projects();
refreshTimes();
if(currentProject_id != -1) {
showProject(currentProject_id);
}
}
_project.page->setProject(currentProject_id);
}
int64_t
ClientPage::
currentProject_id()
/******************************************************************************************
* Return the current project_id, or -1
*/
{
return _project.trw->currentProject_id();
}
void
ClientPage::
showProject(int64_t prj_id)
/*****************************************************************************
* Show the project represented by prj_id
*/
{
if(-1 == prj_id) return;
ProjectTreeWidgetItem *item= _project.trw->findItem(prj_id);
if(!item) return;
_project.trw->setCurrentItem(item);
}
void
ClientPage::
setBillingProject(int64_t prj_id)
/*****************************************************************************
* This will now be the billing project.
*/
{
ProjectTreeWidgetItem *item= _project.trw->findItem(prj_id);
Q_ASSERT(item);
item->setIcon(0, QIcon::fromTheme("emblem-colors-red"));
}
void
ClientPage::
client_update(int64_t client_id)
/*****************************************************************************
* G.clientTable signalled that a record was updated.
*/
{
if(client_id != _client.id) return;
if(_client.id != -1) {
Client *cl= G.clientTable[_client.id];
Q_ASSERT(cl);
_client.gb->setTitle(cl->name());
_client.rate_lbl->setText(cent2dollarStr(cl->default_rate()));
int32_t dflt_charge_quantum= cl->default_charge_quantum();
_client.charge_quantum_lbl->setText(QString("%1").arg(dflt_charge_quantum));
} else {
_client.gb->setTitle("> Please Create a Client <");
_client.rate_lbl->setText("N/A");
_client.charge_quantum_lbl->setText("N/A");
}
}
void
ClientPage::
setCurrentItem()
/*****************************************************************************
* Make adjustments for the QTreeWidget's current item, if any.
*/
{
int64_t prj_id= currentProject_id();
if(prj_id != -1) {
_project.page->setProject(prj_id);
_project.createSubproject_act->setEnabled(true);
_project.edit_act->setEnabled(true);
_project.delete_act->setEnabled(true);
} else {
_project.page->setProject(-1);
_project.createSubproject_act->setEnabled(false);
_project.edit_act->setEnabled(false);
_project.delete_act->setEnabled(false);
}
}
void
ClientPage::
populate_projects()
/*****************************************************************************
* Populate the toolbox widget with projects.
*/
{
_project.trw->setClient(_client.id);
if(_project.trw->currentItem()) {
_project.createSubproject_act->setEnabled(true);
_project.edit_act->setEnabled(true);
_project.delete_act->setEnabled(true);
}
}
void
ClientPage::
createProject()
/***********************************************************
* Create a new prj record, and begin editing
*/
{
int rtn;
Project prj;
/* TODO: Pull default information from a config file */
Client *cl= G.clientTable[_client.id];
Q_ASSERT(cl);
prj.set_rate(cl->default_rate());
prj.set_charge_quantum(cl->default_charge_quantum());
prj.set_client_id(cl->id());
prj.set_title("New Project Record");
/* Run the prj editor modally */
ProjectEditor pe(this, &prj);
rtn = pe.exec();
if (rtn == QDialog::Accepted) {
pe.assign(&prj);
if (prj.insert()) Q_ASSERT(0);
}
}
void
ClientPage::
createSubproject()
/***********************************************************
* Create a new Project record as a child of the current project record, and begin editing
*/
{
int rtn;
Q_ASSERT(_client.id != -1);
int64_t prj_id= currentProject_id();
Q_ASSERT(prj_id != -1);
Project *parent= G.projectTable[prj_id];
Q_ASSERT(parent);
/* Now get a project object to work with */
Project prj;
prj.set_project_id(parent->id());
prj.set_rate(parent->rate());
prj.set_charge_quantum(parent->charge_quantum());
prj.set_client_id(_client.id);
prj.set_title("New Subproject Record");
/* Run the prj editor modally */
ProjectEditor pe(this, &prj);
rtn = pe.exec();
if (rtn == QDialog::Accepted) {
pe.assign(&prj);
if (prj.insert()) Q_ASSERT(0);
}
}
void
ClientPage::
editProject()
/***********************************************************
* edit the current prj record.
*/
{
int rtn;
Project *prj= _project.trw->currentProject();
Q_ASSERT(prj);
/* Run the prj editor modally */
ProjectEditor pe(this, prj);
rtn = pe.exec();
if (rtn == QDialog::Accepted) {
pe.assign(prj);
if (prj->update()) Q_ASSERT(0);
/* signal emitted from table should take care of GUI changes */
}
}
void
ClientPage::
deleteProject()
/***********************************************************
* Delete the current prj record.
*/
{
Project *prj= _project.trw->currentProject();
Q_ASSERT(prj);
int rtn;
/* Ask user if they are really sure */
QString title= prj->title();
QString question = QString("Are you sure you want to delete project:\n\"%1\"?").arg(title);
rtn = QMessageBox::warning(this,
"Delete Project Record",
question,
QMessageBox::Ok | QMessageBox::Cancel,
QMessageBox::Cancel);
if (rtn != QMessageBox::Ok) return;
if (prj->remove()) Q_ASSERT(0);
// G.projectTable will emit a signal, and project_remove() will get called.
}
void
ClientPage::
project_insert(int64_t prj_id)
/***********************************************************
* A project record was inserted.
*/
{
Project *prj= G.projectTable[prj_id];
Q_ASSERT(prj);
/* If this isn't our project, ignore it */
if(prj->client_id() != _client.id) return;
/* Add a new entry in the ProjectTreeWidget */
ProjectTreeWidgetItem *item= _project.trw->addProject(prj_id);
/* Index to that project */
_project.trw->setCurrentItem(item);
}
void
ClientPage::
project_update(int64_t prj_id)
/***********************************************************
* Delete the current prj record.
*/
{
Project *prj= G.projectTable[prj_id];
Q_ASSERT(prj);
/* If this isn't our project, ignore it */
if(prj->client_id() != _client.id) return;
ProjectTreeWidgetItem *item= _project.trw->findItem(prj_id);
if(!item) return;
item->setText(0, prj->title());
}
void
ClientPage::
project_remove(int64_t prj_id)
/***********************************************************
* A project record was deleted.
*/
{
Project *prj= G.projectTable[prj_id];
Q_ASSERT(prj);
/* If this isn't our project, ignore it */
if(prj->client_id() != _client.id) return;
/* See if we can find it in our tree */
ProjectTreeWidgetItem *item= _project.trw->findItem(prj_id);
Q_ASSERT(item);
if(_project.trw->currentItem() == item) {
_project.page->setProject(-1);
}
_project.trw->removeItem(item);
}
void
ClientPage::
weeklyReport()
/*****************************************************************************
* User wants to generate a weekly report.
*/
{
int rtn;
/* Run the picker modally */
WeekChooser wp(this);
rtn = wp.exec();
if (rtn == QDialog::Accepted) {
ClientReport rpt(_client.id, wp.begin(), wp.end());
ReportWidget *rw= new ReportWidget(rpt);
rw->show();
}
}
void
ClientPage::
monthlyReport()
/*****************************************************************************
* User wants to generate a monthly report.
*/
{
// J_DBG_FN;
int rtn;
/* Run the picker modally */
MonthChooser mp(this);
rtn = mp.exec();
if (rtn == QDialog::Accepted) {
// J_DBG_FN << "Accepted!";
ClientReport rpt(_client.id, mp.begin(), mp.end());
ReportWidget *rw= new ReportWidget(rpt);
rw->show();
}
}
void
ClientPage::
customReport()
/*****************************************************************************
* User wants to generate a custom report.
*/
{
J_DBG_FN;
// FIXME: this needs to be implemented
#if 0
int rtn;
/* Run the picker modally */
MonthChooser mp(this);
rtn = mp.exec();
if (rtn == QDialog::Accepted) {
// J_DBG_FN << "Accepted!";
ClientReport rpt(_client.id, mp.begin(), mp.end());
ReportWidget *rw= new ReportWidget(rpt);
rw->show();
}
#endif
}