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
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
* macrowindow.cc
*
* Fri Aug 31 12:27:45 CEST 2007
* Copyright 2007 Lars Bisballe Jensen and Bent Bisballe Nyeng
* elsenator@gmail.com and 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 "macrowindow.h"
#include <QVBoxLayout>
#include <QDomDocument>
#include <QDomElement>
#include <QByteArray>
#include <QApplication>
#include "messagebox.h"
#include "widgets/widget.h"
#include "widgets/window.h"
#include "lua.h"
#include "mainwindow.h"
#include "macrodrawer.h"
#include "debug.h"
extern MainWindow *gmainwindow;
extern QString cpr;
extern QString user;
MacroWindow::MacroWindow(NetCom &n, QString course, QString templ,
bool is_static, bool compact,
QScrollArea *scrollarea,
MacroDrawer *d)
: Collapser(NULL, is_static?NULL:scrollarea), netcom(n)
{
this->scrollarea = scrollarea;
drawer = d;
this->is_static = is_static;
DEBUG(macrowindow, "Constructor %p\n", this);
mainwidget = NULL;
lua = new LUA(&mainwidget, templ, "", user, cpr);
waschanged = false;
this->course = course;
this->templ = templ;
resumewidget = new ResumeWidget(compact);
active = true;
connect(this, SIGNAL(doneAnimating(QWidget*)),
this, SLOT(animated(QWidget*)));
}
MacroWindow::~MacroWindow()
{
clear();
delete resumewidget;
}
void MacroWindow::update(QDomNode &node)
{
QDomElement elem = node.toElement();
if(elem.tagName() != "macro") return;
if(macro == "") macro = elem.attribute("name", "");
if(version == "") version = elem.attribute("version", "");
lua->macro = macro;
if(macro != elem.attribute("name", "")) return;
if(version != elem.attribute("version", "")) return;
QDomNodeList children = node.childNodes();
// No content reveals resumewidget with 'dummy' text.
if(children.count() == 0) {
animateToWidget(resumewidget, false);
return;
}
QDomNode childnode = children.at(0);
QDomElement childelem = childnode.toElement();
if(childelem.tagName() == "resume") {
updateResume(childnode);
return;
}
initMacro(node);
}
void MacroWindow::updateResume(QDomNode &node)
{
QDomElement elem = node.toElement();
if(elem.tagName() != "resume") return;
QString resume = elem.text();
ResumeWidget::state_t state = ResumeWidget::OLD;
if(elem.hasAttribute("state")) {
if(elem.attribute("state") == "old") state = ResumeWidget::OLD;
if(elem.attribute("state") == "new") state = ResumeWidget::NEW;
if(elem.attribute("state") == "dirty") state = ResumeWidget::DIRTY;
}
resumewidget->setText(resume, state);
animateToWidget(resumewidget, false);
}
void MacroWindow::initMacro(QDomNode &node)
{
QDomElement elem = node.toElement();
if(elem.tagName() == "scripts") {
// Nothing to do here
}
if(elem.tagName() == "script") {
lua->runScript(elem.text(), NULL, "preload");
}
if(elem.tagName() == "widgets") {
Window *window = new Window(elem, this);
mainwidget = window;
connect(window, SIGNAL(wasChanged()), this, SLOT(macroChanged()));
macrotitle = elem.attribute("caption");
window->setValues();
window->runEventOnInit(true);
if(waschanged == true) macroChanged();
WARN(macrowindow, "New window.\n");
animateToWidget(mainwidget->qwidget(), true);
return; // No further recursion here.
}
QDomNodeList children = node.childNodes();
for (int i=0; i<children.count();i++) {
QDomNode child = children.at(i);
initMacro(child);
}
}
bool MacroWindow::doCommit()
{
if(mainwidget->committable(true) == false) return false;
if(mainwidget->valid()) {
QVector< Widget* > wlist;
if(mainwidget) wlist = mainwidget->widgetList();
netcom.send(wlist, course, templ, macro, version);
return true;
} else {
MessageBox::critical(NULL, tr("Error"),
tr("The macro ") + macrotitle +
tr(" was not filled out correctly, please try again.\n"),
MessageBox::Ok);
return false;
}
}
void MacroWindow::commit()
{
doCommit();
drawer->setFocus();
}
void MacroWindow::cancel()
{
collapseWrapper();
drawer->setFocus();
}
void MacroWindow::expandWrapper()
{
if(currentWidget() != resumewidget) return;
netcom.send(course, templ, macro);
}
void MacroWindow::collapseWrapper()
{
if(currentWidget() == resumewidget) return;
if(waschanged == false) {
netcom.send(course, templ);
return;
}
MessageBox::StandardButton res =
MessageBox::warning(NULL,
tr("Save the macro changes?"),
tr("you have choosen to close the macro ") +
macrotitle + ".\n" +
tr("do you want to save before closing?"),
MessageBox::Save | MessageBox::Close |
MessageBox::Cancel);
switch(res) {
case MessageBox::Save:
doCommit();
break;
case MessageBox::Close:
netcom.send(course, templ);
break;
case MessageBox::Cancel:
default:
break;
}
}
void MacroWindow::toggleMacro()
{
if(!active) return;
if(currentWidget() == resumewidget) {
if(gmainwindow && !gmainwindow->hasOpen(this)) {
expandWrapper();
} else {
MessageBox::warning(this, tr("Close first"),
tr("Close other one first."));
}
} else {
collapseWrapper();
}
}
void MacroWindow::macroChanged()
{
DEBUG(macrowindow, "This macro was changed!\n");
emit macroHasChanged();
waschanged = true;
}
void MacroWindow::setActive(bool active)
{
if(this->active == active) return;
this->active = active;
setEnabled(active);
emit activationChanged(active);
}
void MacroWindow::clear()
{
lua->clear();
if(mainwidget) {
drawer->setFocus();
mainwidget->deleteLater();
mainwidget = NULL;
}
}
QWidgetList getRecursiveWidgetList(QWidget *w)
{
QWidgetList list;
list.append(w);
QObjectList objs = w->children();
for(int i = 0; i < objs.size(); i++) {
if(objs[i]->isWidgetType()) {
QWidget *cw = (QWidget*)objs[i];
list.append(getRecursiveWidgetList(cw));
}
}
return list;
}
void setTabOrderRecursive(QWidget *w)
{
QWidgetList list = getRecursiveWidgetList(w);
QWidget *last = NULL;
for(int i = 0; i < list.size(); i++) {
QWidget *w = list[i];
if((w->focusPolicy() & Qt::TabFocus) != 0) {
if(last) QWidget::setTabOrder(last, w);
last = w;
}
}
}
void MacroWindow::animated(QWidget *w)
{
if(w == resumewidget) {
clear();
} else {
if(mainwidget == NULL) return;
waschanged = false;
mainwidget->setValues();
if(is_static) return;
setTabOrderRecursive(scrollarea);
// Set keyboard focus on the first focusable widget in the macro.
mainwidget->setKeyboardFocusRecursive();
}
}
|