forked from Mudlet/Mudlet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTAction.cpp
420 lines (373 loc) · 13.6 KB
/
TAction.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
/***************************************************************************
* Copyright (C) 2008-2013 by Heiko Koehn - KoehnHeiko@googlemail.com *
* Copyright (C) 2014 by Ahmed Charles - acharles@outlook.com *
* Copyright (C) 2017 by Stephen Lyons - slysven@virginmedia.com *
* *
* This program 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. *
* *
* This program 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 this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "TAction.h"
#include "EAction.h"
#include "Host.h"
#include "TConsole.h"
#include "TDebug.h"
#include "TEasyButtonBar.h"
#include "TFlipButton.h"
#include "TToolBar.h"
#include "mudlet.h"
using namespace std;
TAction::TAction(TAction* parent, Host* pHost)
: Tree<TAction>(parent)
, mpToolBar(nullptr)
, mpEasyButtonBar(nullptr)
, mButtonState(false)
, mPosX(0)
, mPosY(0)
, mNeedsToBeCompiled(true)
, mButtonColumns(1)
, mIsLabel(false)
, mUseCustomLayout(false)
, mButtonColor(QColor(Qt::white))
, mpHost(pHost)
, exportItem(true)
, mModuleMasterFolder(false)
, mModuleMember(false)
, mOrientation()
, mLocation()
, mIsPushDownButton()
, mButtonRotation()
, mButtonFlat()
, mSizeX()
, mSizeY()
, mDataChanged(true)
, mToolbarLastDockArea(Qt::LeftDockWidgetArea)
, mToolbarLastFloatingState(true)
{
}
TAction::TAction(const QString& name, Host* pHost)
: Tree<TAction>(nullptr)
, mpToolBar(nullptr)
, mpEasyButtonBar(nullptr)
, mButtonState(false)
, mPosX(0)
, mPosY(0)
, mName(name)
, mNeedsToBeCompiled(true)
, mButtonColumns(1)
, mIsLabel(false)
, mUseCustomLayout(false)
, mButtonColor(QColor(Qt::white))
, mpHost(pHost)
, exportItem(true)
, mModuleMasterFolder(false)
, mModuleMember(false)
, mOrientation()
, mLocation()
, mIsPushDownButton()
, mButtonRotation()
, mButtonFlat()
, mSizeX()
, mSizeY()
, mDataChanged(true)
, mToolbarLastDockArea(Qt::LeftDockWidgetArea)
, mToolbarLastFloatingState(true)
{
}
TAction::~TAction()
{
if (mpHost) {
mpHost->getActionUnit()->unregisterAction(this);
}
if (mpToolBar) {
mpToolBar->hide();
}
if (mpEasyButtonBar) {
mpEasyButtonBar->hide();
}
}
bool TAction::registerAction()
{
if (!mpHost) {
qDebug() << "ERROR: TAction::registerTrigger() pHost=0";
return false;
}
return mpHost->getActionUnit()->registerAction(this);
}
void TAction::compileAll()
{
mNeedsToBeCompiled = true;
if (!compileScript()) {
if (mudlet::debugMode) {
TDebug(QColor(Qt::white), QColor(Qt::red)) << "ERROR: Lua compile error. compiling script of action:" << mName << "\n" >> 0;
}
mOK_code = false;
}
for (auto action : *mpMyChildrenList) {
action->compileAll();
}
}
void TAction::compile()
{
if (mNeedsToBeCompiled) {
if (!compileScript()) {
if (mudlet::debugMode) {
TDebug(QColor(Qt::white), QColor(Qt::red)) << "ERROR: Lua compile error. compiling script of action:" << mName << "\n" >> 0;
}
mOK_code = false;
}
}
for (auto action : *mpMyChildrenList) {
action->compile();
}
}
bool TAction::setScript(const QString& script)
{
if (script != mScript) {
setDataChanged();
}
mScript = script;
mNeedsToBeCompiled = true;
mOK_code = compileScript();
return mOK_code;
}
bool TAction::compileScript()
{
mFuncName = QString("Action") + QString::number(mID);
QString code = QString("function ") + mFuncName + QString("()\n") + mScript + QString("\nend\n");
QString error;
if (mpHost->mLuaInterpreter.compile(code, error, QString("Button: ") + getName())) {
mNeedsToBeCompiled = false;
mOK_code = true;
return true;
} else {
mOK_code = false;
setError(error);
return false;
}
}
void TAction::execute()
{
if (mIsPushDownButton) {
if (mButtonState) {
if (!mCommandButtonDown.isEmpty()) {
mpHost->send(mCommandButtonDown);
}
} else {
if (!mCommandButtonUp.isEmpty()) {
mpHost->send(mCommandButtonUp);
}
}
} else {
if (!mCommandButtonDown.isEmpty()) {
mpHost->send(mCommandButtonDown);
}
}
// Moved this to be before the testing/compilation of the script so that
// the "command"s still work even if the script doesn't!
mpHost->mpConsole->mButtonState = (mButtonState ? 2 : 1);
if (mNeedsToBeCompiled) {
if (!compileScript()) {
mpHost->mpConsole->activateWindow();
mpHost->mpConsole->setFocus();
return;
}
}
mpHost->mLuaInterpreter.call(mFuncName, mName);
// move focus back to the active console / command line:
mpHost->mpConsole->activateWindow();
mpHost->mpConsole->setFocus();
}
void TAction::expandToolbar(TToolBar* pT)
{
for (auto action : *mpMyChildrenList) {
if (!action->isActive()) {
// This test and conditional loop abort was missing from this method
// but is needed so that disabled buttons do not appear on
// floating toolbars - possible future scope here to have "disabled"
// buttons show in a "greyed-out" state... - Slysven
continue;
}
QIcon icon(action->mIcon);
QString name = action->getName();
auto button = new TFlipButton(action, mpHost);
button->setIcon(icon);
button->setText(name);
button->setCheckable(action->mIsPushDownButton);
if (action->mIsPushDownButton) {
button->setChecked(action->mButtonState);
} else {
// The following was added to ensure a non-Pushdown button is never
// left in a checked state - Slysven
button->setChecked(false);
}
button->setFlat(mButtonFlat);
// This applies the CSS for THIS TAction to a CHILD's representation on the Toolbar
button->setStyleSheet(css);
/*
* CHECK: The other expandToolbar(...) has the following in this position:
* //FIXME: Heiko April 2012: only run checkbox button scripts, but run them even if unchecked
* if( action->mIsPushDownButton && mpHost->mIsProfileLoadingSequence )
* {
* qDebug()<<"expandToolBar() name="<<action->mName<<" executing script";
* action->execute();
* }
* Why does it have this and we do not? - Slysven
*/
if (action->isFolder()) {
auto newMenu = new QMenu(pT);
// This applies the CSS for THIS TAction to a CHILD's own menu - is this right
newMenu->setStyleSheet(css);
// CHECK: Use the Child's CSS instead for a menu on it? - Slysven:
// newMenu->setStyleSheet( action->css );
action->insertActions(pT, newMenu);
// This has been move until AFTER the child's menu has been
// populated, it was being done straight after newMenu was created,
// but I think we ought to insert the items into the menu before
// applying the menu to the button - Slysven
button->setMenu(newMenu);
}
// Moved to be AFTER the action->mIsFolder test as I think we ought to
// add the button to the toolbar AFTER any menu (children) items have
// been put on the button - Slysven
pT->addButton(button);
}
}
// This seems to be the TToolBar version of TAction::fillMenu(TEasyButtonBar *, QMenu *)
// Unlike the other this one seems to introduce an "intermediate" single menu
// item to which the sub-menu is added.
void TAction::insertActions(TToolBar* pT, QMenu* menu)
{
mpToolBar = pT;
QIcon icon(mIcon);
auto action = new EAction(icon, mName);
action->setCheckable(mIsPushDownButton);
action->mID = mID;
action->mpHost = mpHost;
action->setStatusTip(mName);
menu->addAction(action);
if (isFolder()) {
// The use of mudlet::self() here meant that the QMenu was not destroyed
// until the mudlet instance is at the end of the application!
// Changed to use pT, the toolbar
auto newMenu = new QMenu(pT);
newMenu->setStyleSheet(css);
action->setMenu(newMenu);
for (auto childAction : *mpMyChildrenList) {
childAction->insertActions(pT, newMenu);
}
}
}
void TAction::expandToolbar(TEasyButtonBar* pT)
{
for (auto action : *mpMyChildrenList) {
if (!action->isActive()) {
continue;
}
QIcon icon(action->mIcon);
QString name = action->getName();
auto button = new TFlipButton(action, mpHost);
button->setIcon(icon);
button->setText(name);
button->setCheckable(action->mIsPushDownButton);
if (action->mIsPushDownButton) {
button->setChecked(action->mButtonState);
} else {
// The following was added to ensure a non-Pushdown button is never
// left in a checked state - Slysven
button->setChecked(false);
}
button->setFlat(mButtonFlat);
// This applies the CSS for THIS TAction to a CHILD's representation on the Toolbar
button->setStyleSheet(css);
//FIXME: Heiko April 2012: only run checkbox button scripts, but run them even if unchecked
if (action->mIsPushDownButton && mpHost->mIsProfileLoadingSequence) {
qDebug() << "expandToolBar() name=" << action->mName << " executing script";
action->execute();
}
if (action->isFolder()) {
// This applied the CSS for THIS TAction to a CHILD's own menu - is this right
auto newMenu = new QMenu(button);
// CHECK: consider using the Child's CSS instead for a menu on it
// - Slysven:
// newMenu->setStyleSheet( action->css );
newMenu->setStyleSheet(css);
// This has been moved until AFTER the child's menu has been
// populated, it was being done straight after newMenu was created,
// but I think we ought to insert the items into the menu before
// applying the menu to the button - Slysven
action->fillMenu(pT, newMenu);
button->setMenu(newMenu);
}
// Moved to be AFTER the action->mIsFolder test as I think we ought to
// add the button to the toolbar AFTER any menu (children) items have
// been put on the button - Slysven
pT->addButton(button);
}
}
// This seems to be the second half of TEasyButtonBar version of:
// TAction::insertActions( TToolBar *, QMenu * )
// the need for the split is not yet clear to me! - Slysven
void TAction::fillMenu(TEasyButtonBar* pT, QMenu* menu)
{
for (auto action : *mpMyChildrenList) {
if (!action->isActive()) {
continue;
}
mpEasyButtonBar = pT;
QIcon icon(mIcon);
auto newAction = new EAction(icon, action->mName);
newAction->mID = action->mID;
newAction->mpHost = mpHost;
newAction->setStatusTip(action->mName);
newAction->setCheckable(action->mIsPushDownButton);
if (action->mIsPushDownButton) {
newAction->setChecked(action->mButtonState);
} else {
newAction->setChecked(false);
}
//FIXME: Heiko April 2012 -> expandToolBar()
if (action->mIsPushDownButton && mpHost->mIsProfileLoadingSequence) {
action->execute();
}
if (action->isFolder()) {
// Adding a QWidget derived pointer to new QMenu() means the menu
// will be destroyed when the pointed to item is, we just need to
// find the item that it is attached to - ah ha, try the toolbar...
auto newMenu = new QMenu(pT);
newAction->setMenu(newMenu);
// CHECK: consider using the Child's CSS instead for a menu on it
// - Slysven:
// newMenu->setStyleSheet( action->css );
newMenu->setStyleSheet(css);
action->fillMenu(pT, newMenu);
}
// Menu is PARENT'S menu newAction, this line moved to be AFTER child builds it's own menu if it is a folder
menu->addAction(newAction);
}
}
// This only has code corresponding to the first part of:
// TAction::insertActions( TToolBar * pT, QMenu * menu )
void TAction::insertActions(TEasyButtonBar* pT, QMenu* menu)
{
mpEasyButtonBar = pT;
QIcon icon(mIcon);
auto action = new EAction(icon, mName);
action->setCheckable(mIsPushDownButton);
action->mID = mID;
action->mpHost = mpHost;
action->setStatusTip(mName);
Q_ASSERT_X(menu, "TAction::insertActions( TEasyButtonBar *, QMenu * )", "method called with a NULL QMenu pointer!");
menu->addAction(action);
}