-
Notifications
You must be signed in to change notification settings - Fork 14
/
pqEventDispatcher.cxx
382 lines (333 loc) · 11.2 KB
/
pqEventDispatcher.cxx
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
// SPDX-FileCopyrightText: Copyright (c) Kitware Inc.
// SPDX-FileCopyrightText: Copyright (c) Sandia Corporation
// SPDX-License-Identifier: BSD-3-Clause
#include "pqEventDispatcher.h"
#include "pqEventPlayer.h"
#include "pqEventSource.h"
#include <QAbstractEventDispatcher>
#include <QApplication>
#include <QDialog>
#include <QEventLoop>
#include <QList>
#include <QMainWindow>
#include <QPointer>
#include <QThread>
#include <QTimer>
#include <QtDebug>
#include <iostream>
using namespace std;
//-----------------------------------------------------------------------------
namespace
{
static QList<QPointer<QTimer>> RegisteredTimers;
void processTimers()
{
Q_FOREACH (QTimer* timer, RegisteredTimers)
{
if (timer && timer->isActive())
{
QTimerEvent event(timer->timerId());
qApp->notify(timer, &event);
}
}
}
static int EventPlaybackDelay = QT_TESTING_EVENT_PLAYBACK_DELAY;
};
bool pqEventDispatcher::DeferMenuTimeouts = false;
bool pqEventDispatcher::DeferEventsIfBlocked = false;
bool pqEventDispatcher::PlayingBlockingEvent = false;
//-----------------------------------------------------------------------------
pqEventDispatcher::pqEventDispatcher(QObject* parentObject)
: Superclass(parentObject)
{
this->ActiveSource = NULL;
this->ActivePlayer = NULL;
this->PlayBackStatus = true;
this->PlayBackFinished = false;
this->PlayBackPaused = false;
this->PlayBackOneStep = false;
this->PlayBackStoped = false;
#ifdef __APPLE__
this->BlockTimer.setInterval(1000);
#else
this->BlockTimer.setInterval(100);
#endif
this->BlockTimer.setSingleShot(true);
QObject::connect(&this->BlockTimer, SIGNAL(timeout()), this, SLOT(playEventOnBlocking()));
}
//-----------------------------------------------------------------------------
pqEventDispatcher::~pqEventDispatcher() {}
//-----------------------------------------------------------------------------
void pqEventDispatcher::setEventPlaybackDelay(int milliseconds)
{
EventPlaybackDelay = (milliseconds <= 0) ? 0 : milliseconds;
}
//-----------------------------------------------------------------------------
int pqEventDispatcher::eventPlaybackDelay()
{
return EventPlaybackDelay;
}
//-----------------------------------------------------------------------------
void pqEventDispatcher::registerTimer(QTimer* timer)
{
if (timer)
{
RegisteredTimers.push_back(timer);
}
}
//-----------------------------------------------------------------------------
void pqEventDispatcher::deferEventsIfBlocked(bool enable)
{
pqEventDispatcher::DeferEventsIfBlocked = enable;
}
//-----------------------------------------------------------------------------
void pqEventDispatcher::aboutToBlock()
{
// if (!pqEventDispatcher::DeferMenuTimeouts)
{
if (!this->BlockTimer.isActive())
{
// cout << "aboutToBlock" << endl;
// Request a delayed playback for an event.
this->BlockTimer.start();
}
}
}
//-----------------------------------------------------------------------------
void pqEventDispatcher::awake()
{
// if (!pqEventDispatcher::DeferMenuTimeouts)
// {
// // cout << "awake" << endl;
// // this->BlockTimer.stop();
// }
}
//-----------------------------------------------------------------------------
void pqEventDispatcher::setTimeStep(int value)
{
EventPlaybackDelay = value;
}
//-----------------------------------------------------------------------------
void pqEventDispatcher::run(bool value)
{
this->PlayBackPaused = !value;
if (value)
{
Q_EMIT this->restarted();
}
else
{
Q_EMIT this->paused();
}
}
//-----------------------------------------------------------------------------
void pqEventDispatcher::stop()
{
this->PlayBackPaused = false;
this->PlayBackFinished = true;
}
//-----------------------------------------------------------------------------
bool pqEventDispatcher::isPaused() const
{
return this->PlayBackPaused;
}
//-----------------------------------------------------------------------------
bool pqEventDispatcher::status() const
{
return this->PlayBackStatus;
}
//-----------------------------------------------------------------------------
void pqEventDispatcher::oneStep()
{
this->PlayBackOneStep = true;
}
//-----------------------------------------------------------------------------
bool pqEventDispatcher::playEvents(pqEventSource& source, pqEventPlayer& player)
{
if (this->ActiveSource || this->ActivePlayer)
{
qCritical() << "Event dispatcher is already playing";
return false;
}
this->ActiveSource = &source;
this->ActivePlayer = &player;
QApplication::setEffectEnabled(Qt::UI_General, false);
QApplication::setEffectEnabled(Qt::UI_AnimateMenu, false); // Show animated menus.
QApplication::setEffectEnabled(Qt::UI_FadeMenu, false); // Show faded menus.
QApplication::setEffectEnabled(Qt::UI_AnimateCombo, false); // Show animated comboboxes.
QApplication::setEffectEnabled(Qt::UI_AnimateTooltip, false); // Show tooltip animations.
QApplication::setEffectEnabled(Qt::UI_FadeTooltip, false); // Show tooltip fading effects.
QObject::connect(
QAbstractEventDispatcher::instance(), SIGNAL(aboutToBlock()), this, SLOT(aboutToBlock()));
QObject::connect(QAbstractEventDispatcher::instance(), SIGNAL(awake()), this, SLOT(awake()));
// This is how the playback logic works:
// * In here, we continuously keep on playing one event after another until
// we are done processing all the events.
// * If a modal dialog pops up, then this->aboutToBlock() gets called. To me
// accurate, aboutToBlock() is called everytime the sub-event loop is entered
// and more modal dialogs that loop is entered after processing of each event
// (not merely when the dialog pops up).
// * In this->aboutToBlock() we start a timer which on timeout processes just 1 event.
// * After executing that event, if the dialog still is up, them aboutToBlock() will
// be called again and the cycle continues. If not, the control returns to this main
// playback loop, and it continues.
this->PlayBackStatus = true; // success.
this->PlayBackFinished = false;
while (!this->PlayBackFinished)
{
if (!this->PlayBackPaused)
{
// cerr << "=== playEvent(1) ===" << endl;
this->playEvent();
}
else
{
if (this->PlayBackOneStep)
{
this->PlayBackOneStep = false;
this->playEvent();
}
else
{
this->processEventsAndWait(100);
}
}
}
this->ActiveSource = NULL;
this->ActivePlayer = NULL;
QObject::disconnect(
QAbstractEventDispatcher::instance(), SIGNAL(aboutToBlock()), this, SLOT(aboutToBlock()));
QObject::disconnect(QAbstractEventDispatcher::instance(), SIGNAL(awake()), this, SLOT(awake()));
return this->PlayBackStatus;
}
//-----------------------------------------------------------------------------
void pqEventDispatcher::playEventOnBlocking()
{
// if(this->PlayingBlockingEvent)
// {
// qDebug() << "Event blocking already playing ....";
// return;
// }
if (pqEventDispatcher::DeferMenuTimeouts || pqEventDispatcher::DeferEventsIfBlocked)
{
// cerr << "=== playEventOnBlocking ===" << endl;
this->BlockTimer.start();
return;
}
pqEventDispatcher::PlayingBlockingEvent = true;
// cout << "---blocked event: " << endl;
// if needed for debugging, I can print blocking annotation here.
// cerr << "=== playEvent(2) ===" << endl;
this->playEvent(1);
// if (!this->BlockTimer.isActive())
// {
// this->BlockTimer.start();
// }
pqEventDispatcher::PlayingBlockingEvent = false;
}
//-----------------------------------------------------------------------------
void pqEventDispatcher::playEvent(int indent)
{
this->BlockTimer.stop();
if (this->PlayBackFinished)
{
return;
}
if (!this->ActiveSource)
{
this->PlayBackFinished = true;
this->PlayBackStatus = false; // failure.
qCritical("Internal error: playEvent called without a valid event source.");
return;
}
QString object;
QString command;
QString arguments;
int eventType;
int result = this->ActiveSource->getNextEvent(object, command, arguments, eventType);
if (result == pqEventSource::DONE)
{
this->PlayBackFinished = true;
return;
}
else if (result == pqEventSource::FAILURE)
{
this->PlayBackFinished = true;
this->PlayBackStatus = false; // failure.
return;
}
static unsigned long counter = 0;
unsigned long local_counter = counter++;
QString pretty_name = object.mid(object.lastIndexOf('/'));
bool print_debug = getenv("PV_DEBUG_TEST") != NULL;
if (print_debug)
{
QString eventString = "Event";
if (eventType == pqEventTypes::CHECK_EVENT)
{
eventString = "Check Event";
}
cout << QTime::currentTime().toString("hh:mm:ss").toStdString().c_str() << " : "
<< QString().fill(' ', 4 * indent).toStdString().c_str() << local_counter << ": Test ("
<< indent << "): " << eventString.toUtf8().data() << ": "
<< pretty_name.toStdString().c_str() << ": " << command.toStdString().c_str() << " : "
<< arguments.toStdString().c_str() << endl;
}
bool error = false;
this->ActivePlayer->playEvent(object, command, arguments, eventType, error);
this->BlockTimer.stop();
// process any posted events. We call processEvents() so that any slots
// connected using QueuedConnection also get handled.
this->processEventsAndWait(EventPlaybackDelay);
// We explicitly timeout timers that have been registered with us.
processTimers();
this->BlockTimer.stop();
if (print_debug)
{
cout << QTime::currentTime().toString("hh:mm:ss").toStdString().c_str() << " : "
<< QString().fill(' ', 4 * indent).toStdString().c_str() << local_counter << ": Done"
<< endl;
}
if (error)
{
this->PlayBackStatus = false;
this->PlayBackFinished = true;
return;
}
}
//-----------------------------------------------------------------------------
void pqEventDispatcher::processEventsAndWait(int ms)
{
bool prev = pqEventDispatcher::DeferMenuTimeouts;
pqEventDispatcher::DeferMenuTimeouts = true;
if (ms > 0)
{
QApplication::sendPostedEvents();
QEventLoop loop;
QTimer::singleShot(ms, &loop, SLOT(quit()));
loop.exec();
}
// When this gets called during playback from a blocking event loop (e.g. a modal dialog)
// calling `QApplication::processEvents()` has a sideeffect on Qt 5 + OsX where it does
// not quit the eventloop for the modal dialog until a mouse event (for example) is
// received by the application. Avoiding calling QApplication::processEvents when already
// processing a event loop other the apps main event loop avoids that problem.
if (!pqEventDispatcher::PlayingBlockingEvent)
{
QApplication::processEvents();
}
QApplication::sendPostedEvents();
if (!pqEventDispatcher::PlayingBlockingEvent)
{
QApplication::processEvents();
}
pqEventDispatcher::DeferMenuTimeouts = prev;
}
//-----------------------------------------------------------------------------
void pqEventDispatcher::processEvents(QEventLoop::ProcessEventsFlags flags)
{
bool prev = pqEventDispatcher::DeferMenuTimeouts;
pqEventDispatcher::DeferMenuTimeouts = true;
QApplication::processEvents(flags);
pqEventDispatcher::DeferMenuTimeouts = prev;
}