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
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
* mainwindow.cc
*
* Wed Sep 17 09:41:09 CEST 2008
* Copyright 2008 Bent Bisballe Nyeng
* deva@aasimon.org
****************************************************************************/
/*
* This file is part of Pracro.
*
* Pracro is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Pracro is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Pracro; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#include "mainwindow.h"
#include <QApplication>
#include <QDomDocument>
#include <QDomNodeList>
#include <QDomNode>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QScrollArea>
#include <QSettings>
#include <QStatusBar>
#include <QToolBar>
#include <QAction>
#include <QEvent>
#include <QCloseEvent>
#include "aboutbox.h"
#include "messagebox.h"
#include "macrodrawer.h"
#include "macrowindow.h"
#include "macro.h"
#include "header.h"
#include "template.h"
#include "debug.h"
class Dbg : public QLabel {
public:
Dbg() {
setText("dbg");
}
void mousePressEvent(QMouseEvent *)
{
dbg_toggle();
}
};
MainWindow *gmainwindow = NULL;
MainWindow::MainWindow(QString patientid, QString title, QString course,
QString templ, QString host, quint16 port, QString user)
: QMainWindow(0), // Qt::WindowContextHelpButtonHint
netcom(host, port)
{
gmainwindow = this;
closing = false;
scrollarea = new QScrollArea();
setCentralWidget(scrollarea);
central = new QWidget();
scrollarea->setWidget(central);
scrollarea->setWidgetResizable(true);
scrollarea->setFocusPolicy(Qt::NoFocus);
central->setFocusPolicy(Qt::NoFocus);
central->setLayout(new QVBoxLayout());
connect(&netcom, SIGNAL(gotReply(QDomDocument&)),
this, SLOT(handle(QDomDocument&)));
connect(&netcom, SIGNAL(networkError(QString, QString)),
this, SLOT(showError(QString, QString)));
isStored = false;
header = NULL;
this->patientid = patientid;
this->user = user;
this->course = course;
this->templ = templ;
setWindowTitle("Pracro - " + title);
setWindowIcon(QIcon(":/icons/icon.png"));
QStatusBar *status = statusBar();
aboutButton = new QPushButton("Pracro v."VERSION, status);
aboutButton->setIcon(QIcon(":icons/icon.png"));
aboutButton->setMaximumHeight(18);
aboutButton->setFocusPolicy(Qt::NoFocus);
connect(aboutButton, SIGNAL(clicked()), this, SLOT(showAbout()));
status->addPermanentWidget(aboutButton);
if(dbg_enabled()) {
status->addPermanentWidget(new Dbg());
}
QToolBar *toolbar = addToolBar("controls");
toolbar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
QAction *close_commit = toolbar->addAction(tr("Close and commit"));
close_commit->setIcon(QPixmap(":icons/icon_close_commit.png"));
QAction *close_no_commit = toolbar->addAction(tr("Close no commit"));
close_no_commit->setIcon(QPixmap(":icons/icon_close_no_commit.png"));
/*
QAction *close_discard = toolbar->addAction("Close discard");
close_discard->setIcon(QPixmap(":icons/icon_discard.png"));
connect(close_discard, SIGNAL(triggered()), this, SLOT(closeDiscard()));
*/
toolbar->addSeparator();
connect(close_commit, SIGNAL(triggered()), this, SLOT(closeCommit()));
connect(close_no_commit, SIGNAL(triggered()), this, SLOT(closeNoCommit()));
setStatusBar(status);
init();
}
MainWindow::~MainWindow()
{
}
void MainWindow::showAbout()
{
AboutBox aboutBox(this, Qt::Dialog);
aboutBox.exec();
}
bool MainWindow::hasOpen(void *me)
{
Entities::iterator i = entities.begin();
while(i != entities.end()) {
if(me != i.value() && i.value()->isOpen()) return true;
i++;
}
return false;
}
void MainWindow::closeCommit()
{
if(hasOpen(NULL)) {
MessageBox::warning(this, tr("Close first."),
tr("Close open macros first."));
return;
}
netcom.commit();
// isStored = true;
closing = true;
}
void MainWindow::closeNoCommit()
{
if(hasOpen(NULL)) {
MessageBox::warning(this, tr("Close first."),
tr("Close open macros first."));
return;
}
netcom.nocommit();
// isStored = true;
closing = true;
}
void MainWindow::closeDiscard()
{
if(hasOpen(NULL)) {
MessageBox::warning(this, tr("Close first."),
tr("Close open macros first."));
return;
}
MessageBox::StandardButton res =
MessageBox::critical(this,
tr("Discard"),
tr("This session will <strong>NOT</strong> be "
"stored in the journal.<br/>"
"Are you sure you want to continue?"),
MessageBox::Yes | MessageBox::No);
if(res == MessageBox::Yes) {
netcom.discard();
// isStored = true;
closing = true;
}
}
void MainWindow::closeEvent(QCloseEvent *event)
{
if(hasOpen(NULL)) {
MessageBox::warning(this, "Close first.", "Close open macros first.");
event->ignore();
return;
}
if(isStored || MessageBox::critical(this,
tr("Discard"),
tr("This session will "
"<strong>NOT</strong> be stored in "
"the journal.<br/>"
"Are you sure you want to continue?"),
MessageBox::Yes | MessageBox::No)
== MessageBox::Yes) {
if(!isStored) {
netcom.discard();
// isStored = true;
closing = true;
event->ignore();
return;
}
QSettings settings("Aasimon.org", "Pracro");
settings.beginGroup("MainWindow");
settings.setValue("size", size());
settings.setValue("pos", pos());
settings.endGroup();
event->accept();
dbg_free();
} else {
event->ignore();
}
emit isClosing();
}
void MainWindow::init()
{
QSettings settings("Aasimon.org", "Pracro");
settings.beginGroup("MainWindow");
resize(settings.value("size", QSize(700, 800)).toSize());
move(settings.value("pos", QPoint(0, 0)).toPoint());
settings.endGroup();
netcom.patientid = patientid;
netcom.templ = templ;
netcom.course = course;
netcom.user = user;
netcom.initConnection();
}
void MainWindow::updateTemplateHeaders(QDomNode &node)
{
QDomElement elem = node.toElement();
if(elem.tagName() != "template") return;
QString title = elem.attribute("title");
QString name = elem.attribute("name");
if(!header) {
header = new QLabel();
QFont headerfont = header->font();
headerfont.setBold(true);
headerfont.setPointSize(headerfont.pointSize() + 4);
header->setFont(headerfont);
header->setAlignment(Qt::AlignHCenter);
central->layout()->addWidget(header);
}
header->setText(title);
statusBar()->showMessage(title + " (" + name +
") - SessionID: " + netcom.sessionid);
}
void MainWindow::showError(QString title, QString text)
{
MessageBox::critical(this, title, text);
}
void MainWindow::updateMacros(QDomNodeList &nodes)
{
bool creation = entities.size() == 0;
for(int j = 0; j < nodes.count(); j++) {
QDomNode node = nodes.at(j);
QDomElement elem = node.toElement();
if(elem.tagName() == "macro" || elem.tagName() == "header") {
QString name = elem.attribute("name");
if(entities.find(j) == entities.end()) {
Entity *e = NULL;
if(elem.tagName() == "macro") {
e = new Macro(entities, netcom, course, templ, scrollarea);
}
if(elem.tagName() == "header") {
e = new Header();
}
entities[j] = e;
entities[j]->update(node);
central->layout()->addWidget(e->widget());
} else {
entities[j]->update(node);
}
continue;
}
showError("XML Error", "Expected macro/header tag. Got '" +
elem.tagName() + "' tag.");
continue;
}
if(creation) {
for(int i = 0; i < entities.size(); i++) {
Macro *macro = (Macro*)entities[i];
if(macro->isstatic == false) {
macro->widget()->setFocus();
break;
}
}
QVBoxLayout *l = (QVBoxLayout*)central->layout();
l->addStretch(1);
}
}
void MainWindow::updateTemplates(QDomNodeList &nodes)
{
for(int j = 0; j < nodes.count(); j++) {
QDomNode node = nodes.at(j);
QDomElement elem = node.toElement();
if(elem.tagName() == "template") {
QString name = elem.attribute("name");
if(entities.find(j) == entities.end()) {
Template *e = new Template(netcom);
connect(e, SIGNAL(setTemplate(QString)),
this, SLOT(setTemplate(QString)));
entities[j] = e;
entities[j]->update(node);
central->layout()->addWidget(e->widget());
if(j == 0) e->widget()->setFocus();
} else {
entities[j]->update(node);
}
continue;
}
showError("XML Error", "Expected template tag. Got '" +
elem.tagName() + "' tag.");
continue;
}
}
void MainWindow::setTemplate(QString name)
{
templ = name;
Entities::iterator i = entities.begin();
while(i != entities.end()) {
delete i.value()->widget();
// delete i.value();
i++;
}
entities.clear();
}
void MainWindow::handle(QDomDocument &doc)
{
DEBUG(mainwindow, "Handle %s\n",
doc.toString().toStdString().c_str());
QDomNodeList nodes = doc.documentElement().childNodes();
for(int j = 0; j < nodes.count(); j++) {
QDomNode node = nodes.at(j);
QDomElement element = node.toElement();
if(element.tagName() == "error") {
showError("Pracro Server Error", element.text());
closing = false;
continue;
}
if(element.tagName() != "template" && element.tagName() != "course") {
showError("XML error", "Outer tag not a template or course.");
continue;
}
if(element.tagName() == "template" && element.attribute("name") != templ) {
showError("XML error",
"Got template name that didn't match current session.");
continue;
}
// Set headers. titles, etc...
updateTemplateHeaders(node);
if(element.tagName() == "course") {
// Update macros, header and resumes.
QDomNodeList templatenodes = node.childNodes();
updateTemplates(templatenodes);
}
if(element.tagName() == "template") {
// Update macros, header and resumes.
QDomNodeList macronodes = node.childNodes();
updateMacros(macronodes);
}
}
if(closing) {
isStored = true;
close();
}
}
|