-
Notifications
You must be signed in to change notification settings - Fork 3
/
Report.cpp
325 lines (269 loc) · 8.47 KB
/
Report.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
#include <QTextStream>
#include <QMessageBox>
#include <QTextEdit>
#include <QBoxLayout>
#include <QToolBar>
#include <QSizeGrip>
#include <QFileDialog>
#include <QFileInfo>
#include <QFile>
#include "qtimemage.h"
#include "util.h"
#include "configItem.h"
#include "Project.h"
#include "Report.h"
static configRect S_geometry("ReportWidget_geom", 20, 20, 400, 800);
static configString S_rptSaveDir("ReportWidget_saveDir");
/***********************************************************************************/
/*********************** ProjectReport *********************************************/
/***********************************************************************************/
int
ProjectReport::
run(int64_t project_id, const QDateTime &begin, const QDateTime &end)
/*************************************************************************
* Populate data members.
*/
{
this->prj_id= project_id;
Project *prj= G.projectTable[project_id];
Q_ASSERT(prj);
this->parent_id= -1;
prj->project_id(&this->parent_id); // May be null
this->busySecs= prj->reportTime(begin, end);
return 0;
}
int
ProjectReport::
decihours() const
/*************************************************************************
* Round busySecs to decihours.
*/
{
return lround((double)this->busySecs/360.);
}
/***********************************************************************************/
/************************ ClientReport *********************************************/
/***********************************************************************************/
int
ClientReport::
run(int64_t client_id, const QDateTime &begin, const QDateTime &end)
/*************************************************************************
* Populate data members.
*/
{
/* Stash a copy for reference later */
this->begin_dt= begin;
this->end_dt= end;
QVector<int64_t> prj_id_vec;
this->rpt_hash.clear();
this->child_hash.clear();
this->client_id= client_id;
int rtn = G.projectTable.fetchAll(prj_id_vec, QString("WHERE client_id=%1").arg(client_id));
if (rtn < 0) qFatal("Project::fetchAll() failed!");
for (int i = 0; i < prj_id_vec.size(); ++i) {
int64_t prj_id= prj_id_vec[i];
ProjectReport *pr= new ProjectReport;
pr->run(prj_id, begin, end);
this->rpt_hash[prj_id]= pr;
if(pr->parent_id == -1) continue;
this->child_hash.insert(pr->parent_id, prj_id);
}
return 0;
}
int
ClientReport::
cumulativeDecihours() const
/*************************************************************************
* Total of all projects for this client.
*/
{
int rtn= 0;
ProjectReport *rpt;
foreach(rpt, this->rpt_hash) {
rtn += rpt->decihours();
}
return rtn;
}
int
ClientReport::
projectDecihours(int64_t prj_id) const
/*************************************************************************
* Retrieve the decihours for a single project.
*/
{
const ProjectReport *rpt= this->rpt_hash[prj_id];
Q_ASSERT(rpt);
return rpt->decihours();
}
int
ClientReport::
cumulativeProjectDecihours(int64_t prj_id) const
/*************************************************************************
* Retrieve the cumulative decihours for a project and all of it's subprojects
*/
{
int rtn= 0;
const ProjectReport *rpt= this->rpt_hash[prj_id];
Q_ASSERT(rpt);
rtn += rpt->decihours();
QHash<int64_t, int64_t>::const_iterator i = this->child_hash.find(prj_id);
while (i != this->child_hash.end() && i.key() == prj_id) {
rtn += cumulativeProjectDecihours(i.value());
++i;
}
return rtn;
}
void
ClientReport::
activeProjectIds(QMap<QString,ProjectReport*> &prjMap) const
/*************************************************************************
* Retrieve a vector of all active projects in this report.
*/
{
Project *prj;
ProjectReport *pr;
int64_t prj_id;
QHash<int64_t, ProjectReport*>::const_iterator i = this->rpt_hash.begin();
for(; i != this->rpt_hash.end(); ++i) {
pr= i.value();
prj_id= pr->prj_id;
prj= G.projectTable[prj_id];
Q_ASSERT(prj);
//J_DBG_FN << "prj_id= " << prj_id << ", busySecs= " << pr->busySecs;
if(!cumulativeProjectDecihours(prj_id)) continue;
prjMap[prj->longTitle()]= pr;
}
}
/***********************************************************************************/
/*********************** ReportWidget ****************************************/
/***********************************************************************************/
ReportWidget::
ReportWidget(ClientReport &rpt, QWidget *parent)
/*************************************************************************
* Constructor
*/
: QWidget(parent)
{
/* Listen for Global setVisible signals */
connect(&G, SIGNAL(sig_setVisibleTopLevelWindows(bool)), SLOT(setVisible(bool)));
/* Geometry management */
resize(S_geometry.width(), S_geometry.height());
connect(&G, SIGNAL(sig_aboutToSaveSettings()), SLOT(storeSettings()));
QVBoxLayout *vbl= new QVBoxLayout(this);
{ /* tool bar */
QToolBar *tb= new QToolBar(this);
vbl->addWidget(tb);
tb->addAction("Save", this, SLOT(saveToDisk()));
}
{ /* 2nd row */
_te= new QTextEdit(this);
vbl->addWidget(_te);
_te->setAcceptRichText(true);
_te->setReadOnly(true);
}
{ /* Sizegrip */
QHBoxLayout *hbl= new QHBoxLayout;
vbl->addLayout(hbl);
hbl->addStretch(10);
QSizeGrip *sg= new QSizeGrip(this);
hbl->addWidget(sg);
// vbl->addWidget(sg);
}
showReport(rpt);
}
ReportWidget::
~ReportWidget()
/*************************************************************************
* Destructor
*/
{}
void
ReportWidget::
saveToDisk()
/*************************************************************************
* Save html to disk.
*/
{
QString title("Filename for Save");
for(;;) {
QString rtn= QFileDialog::getSaveFileName(this, title, S_rptSaveDir);
if(rtn.isNull()) return;
QFileInfo fi(rtn);
S_rptSaveDir= fi.path();
QFile f(rtn);
if(!f.open(QFile::WriteOnly|QFile::Truncate)) {
QString question = QString("Error: Cannot open:\n\"%1\"?").arg(fi.filePath());
QMessageBox::warning(this,
title,
question,
QMessageBox::Ok);
continue;
} else {
QTextStream strm(&f);
strm << "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n";
strm << "<html>\n<body>\n";
strm << _html.join(' ');
strm << "</body>\n</html>\n";
break;
}
}
}
void
ReportWidget::
storeSettings()
/*************************************************************************
* Stash geometry
*/
{
S_geometry= geometry();
}
int
ReportWidget::
showReport(ClientReport &rpt)
/*************************************************************************
* Generate the text for rpt.
*/
{
Client *cl= G.clientTable[rpt.client_id];
setWindowTitle(
QString("Timecard for %1, %2 - %3")
.arg(cl->name())
.arg(rpt.begin_dt.date().toString())
.arg(rpt.end_dt.date().toString()
)
);
_html.clear();
_html << QString("<center>\n<h2>Timecard for %1<br>\n%2 - %3</h2></center>\n")
.arg(cl->name())
.arg(rpt.begin_dt.date().toString())
.arg(rpt.end_dt.date().toString());
_html << "";
_html << QString("<h3>Cumulative hours, all projects: %1</h3>\n").arg(decihour2hrStr(rpt.cumulativeDecihours()));
_html << "<table border=1 cellpadding=5>\n";
_html << "<tr> <th></th> <th colspan=2>Time</th> </tr>\n";
_html << "<tr> <th>Project</th> <th>Individual</th> <th>Aggregate</th> </tr>\n";
QMap<QString,ProjectReport*> prjMap;
rpt.activeProjectIds(prjMap);
QMap<QString, ProjectReport*>::const_iterator i = prjMap.begin();
for(; i != prjMap.end(); ++i) {
ProjectReport *pr= i.value();
_html << "<tr>"
<< "<td>" << i.key().toHtmlEscaped() << "</td>"
<< "<td align=right>" << decihour2hrStr(pr->decihours()) << "</td>"
<< "<td align=right>" << decihour2hrStr(rpt.cumulativeProjectDecihours(pr->prj_id)) << "</td>"
<< "</tr>\n";
}
_html << "</table>\n";
_te->setText(_html.join(' '));
return 0;
}
void
ReportWidget::
closeEvent(QCloseEvent *event)
/*************************************************************************
* Reimplemented so we can save the geometry.
*/
{
storeSettings();
deleteLater();
}