/
mythscreentype.cpp
640 lines (527 loc) · 15.1 KB
/
mythscreentype.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
#include "mythscreentype.h"
#include <QCoreApplication>
#include <QDomDocument>
#include <QRunnable>
#include "mythcorecontext.h"
#include "mythobservable.h"
#include "mthreadpool.h"
#include "mythscreenstack.h"
#include "mythmainwindow.h"
#include "mythuihelper.h"
#include "mythprogressdialog.h"
#include "mythuigroup.h"
#include "mythlogging.h"
class SemaphoreLocker
{
public:
SemaphoreLocker(QSemaphore *lock) : m_lock(lock)
{
if (m_lock)
m_lock->acquire();
}
~SemaphoreLocker()
{
if (m_lock)
m_lock->release();
}
private:
QSemaphore *m_lock;
};
QEvent::Type ScreenLoadCompletionEvent::kEventType =
(QEvent::Type) QEvent::registerEventType();
class ScreenLoadTask : public QRunnable
{
public:
ScreenLoadTask(MythScreenType &parent) : m_parent(parent) {}
private:
void run(void)
{
m_parent.Load();
m_parent.m_IsLoaded = true;
m_parent.m_IsLoading = false;
m_parent.m_LoadLock.release();
}
MythScreenType &m_parent;
};
MythScreenType::MythScreenType(
MythScreenStack *parent, const QString &name, bool fullscreen) :
MythUIType(parent, name), m_LoadLock(1)
{
m_FullScreen = fullscreen;
m_CurrentFocusWidget = NULL;
m_ScreenStack = parent;
m_BusyPopup = NULL;
m_IsDeleting = false;
m_IsLoading = false;
m_IsLoaded = false;
m_IsInitialized = false;
// Can be overridden, of course, but default to full sized.
m_Area = GetMythMainWindow()->GetUIScreenRect();
if (QCoreApplication::applicationName() == MYTH_APPNAME_MYTHFRONTEND)
gCoreContext->SendSystemEvent(
QString("SCREEN_TYPE CREATED %1").arg(name));
}
MythScreenType::MythScreenType(
MythUIType *parent, const QString &name, bool fullscreen) :
MythUIType(parent, name), m_LoadLock(1)
{
m_FullScreen = fullscreen;
m_CurrentFocusWidget = NULL;
m_ScreenStack = NULL;
m_BusyPopup = NULL;
m_IsDeleting = false;
m_IsLoading = false;
m_IsLoaded = false;
m_IsInitialized = false;
m_Area = GetMythMainWindow()->GetUIScreenRect();
if (QCoreApplication::applicationName() == MYTH_APPNAME_MYTHFRONTEND)
gCoreContext->SendSystemEvent(
QString("SCREEN_TYPE CREATED %1").arg(name));
}
MythScreenType::~MythScreenType()
{
if (QCoreApplication::applicationName() == MYTH_APPNAME_MYTHFRONTEND)
gCoreContext->SendSystemEvent(
QString("SCREEN_TYPE DESTROYED %1").arg(objectName()));
// locking ensures background screen load can finish running
SemaphoreLocker locker(&m_LoadLock);
m_CurrentFocusWidget = NULL;
emit Exiting();
}
bool MythScreenType::IsFullscreen(void) const
{
return m_FullScreen;
}
void MythScreenType::SetFullscreen(bool full)
{
m_FullScreen = full;
}
MythUIType *MythScreenType::GetFocusWidget(void) const
{
return m_CurrentFocusWidget;
}
bool MythScreenType::SetFocusWidget(MythUIType *widget)
{
if (!widget || !widget->IsVisible())
{
QMap<int, MythUIType *>::iterator it = m_FocusWidgetList.begin();
MythUIType *current;
while (it != m_FocusWidgetList.end())
{
current = *it;
if (current->CanTakeFocus() && current->IsVisible())
{
widget = current;
break;
}
++it;
}
}
if (!widget)
return false;
if (m_CurrentFocusWidget == widget)
return true;
MythUIText *helpText = dynamic_cast<MythUIText *>(GetChild("helptext"));
if (helpText)
helpText->Reset();
if (m_CurrentFocusWidget)
m_CurrentFocusWidget->LoseFocus();
m_CurrentFocusWidget = widget;
m_CurrentFocusWidget->TakeFocus();
if (helpText && !widget->GetHelpText().isEmpty())
helpText->SetText(widget->GetHelpText());
return true;
}
bool MythScreenType::NextPrevWidgetFocus(bool up)
{
if (!m_CurrentFocusWidget || m_FocusWidgetList.isEmpty())
return SetFocusWidget(NULL);
bool reachedCurrent = false;
bool looped = false;
QMap<int, MythUIType *>::iterator it = m_FocusWidgetList.begin();
MythUIType *current;
// There is probably a more efficient way to do this, but the list
// is never going to be that big so it will do for now
if (up)
{
while (it != m_FocusWidgetList.end())
{
current = *it;
if ((looped || reachedCurrent) &&
current->IsVisible() && current->IsEnabled())
return SetFocusWidget(current);
if (current == m_CurrentFocusWidget)
reachedCurrent = true;
++it;
if (it == m_FocusWidgetList.end())
{
if (looped)
return false;
else
{
looped = true;
it = m_FocusWidgetList.begin();
}
}
}
}
else
{
it = m_FocusWidgetList.end() - 1;
while (it != m_FocusWidgetList.begin() - 1)
{
current = *it;
if ((looped || reachedCurrent) &&
current->IsVisible() && current->IsEnabled())
return SetFocusWidget(current);
if (current == m_CurrentFocusWidget)
reachedCurrent = true;
--it;
if (it == m_FocusWidgetList.begin() - 1)
{
if (looped)
return false;
else
{
looped = true;
it = m_FocusWidgetList.end() - 1;
}
}
}
}
return false;
}
void MythScreenType::BuildFocusList(void)
{
m_FocusWidgetList.clear();
m_CurrentFocusWidget = NULL;
AddFocusableChildrenToList(m_FocusWidgetList);
if (m_FocusWidgetList.size() > 0)
SetFocusWidget();
}
MythScreenStack *MythScreenType::GetScreenStack(void) const
{
return m_ScreenStack;
}
void MythScreenType::aboutToHide(void)
{
if (!m_FullScreen)
{
if (!GetMythMainWindow()->GetPaintWindow()->mask().isEmpty())
{
// remove this screen's area from the mask so any embedded video is
// shown which was covered by this screen
if (!m_SavedMask.isEmpty())
GetMythMainWindow()->GetPaintWindow()->setMask(m_SavedMask);
}
}
ActivateAnimations(MythUIAnimation::AboutToHide);
}
void MythScreenType::aboutToShow(void)
{
if (!m_FullScreen)
{
if (!GetMythMainWindow()->GetPaintWindow()->mask().isEmpty())
{
// add this screens area to the mask so any embedded video isn't
// shown in front of this screen
QRegion region = GetMythMainWindow()->GetPaintWindow()->mask();
m_SavedMask = region;
region = region.unite(QRegion(m_Area));
GetMythMainWindow()->GetPaintWindow()->setMask(region);
}
}
ActivateAnimations(MythUIAnimation::AboutToShow);
}
bool MythScreenType::IsDeleting(void) const
{
return m_IsDeleting;
}
void MythScreenType::SetDeleting(bool deleting)
{
m_IsDeleting = deleting;
}
bool MythScreenType::Create(void)
{
return true;
}
/**
* \brief Load data which will ultimately be displayed on-screen or used to
* determine what appears on-screen (See Warning)
*
* \warning This method should only load data, it should NEVER perform UI
* routines or segfaults WILL result. This includes assinging data to
* any widgets, calling methods on a widget or anything else which
* triggers redraws. The safest and recommended approach is to avoid
* any interaction with a libmythui class or class member.
*/
void MythScreenType::Load(void)
{
// Virtual
}
void MythScreenType::LoadInBackground(QString message)
{
m_LoadLock.acquire();
m_IsLoading = true;
m_IsLoaded = false;
m_ScreenStack->AllowReInit();
OpenBusyPopup(message);
ScreenLoadTask *loadTask = new ScreenLoadTask(*this);
MThreadPool::globalInstance()->start(loadTask, "ScreenLoad");
}
void MythScreenType::LoadInForeground(void)
{
SemaphoreLocker locker(&m_LoadLock);
m_IsLoading = true;
m_IsLoaded = false;
m_ScreenStack->AllowReInit();
Load();
m_IsLoaded = true;
m_IsLoading = false;
}
void MythScreenType::ReloadInBackground(void)
{
m_IsInitialized = false;
LoadInBackground();
}
void MythScreenType::OpenBusyPopup(QString message)
{
if (m_BusyPopup)
return;
QString msg(tr("Loading..."));
if (!message.isEmpty())
msg = message;
MythScreenStack *popupStack =
GetMythMainWindow()->GetStack("popup stack");
m_BusyPopup =
new MythUIBusyDialog(msg, popupStack, "mythscreentypebusydialog");
if (m_BusyPopup->Create())
popupStack->AddScreen(m_BusyPopup, false);
}
void MythScreenType::CloseBusyPopup(void)
{
if (m_BusyPopup)
m_BusyPopup->Close();
m_BusyPopup = NULL;
}
void MythScreenType::SetBusyPopupMessage(const QString &message)
{
if (m_BusyPopup)
m_BusyPopup->SetMessage(message);
}
void MythScreenType::ResetBusyPopup(void)
{
if (m_BusyPopup)
m_BusyPopup->Reset();
}
/**
* \brief Has Init() been called on this screen?
*/
bool MythScreenType::IsInitialized(void) const
{
return m_IsInitialized;
}
void MythScreenType::doInit(void)
{
SemaphoreLocker locker(&m_LoadLock); // don't run while loading..
CloseBusyPopup();
Init();
m_IsInitialized = true;
}
/**
* \brief Used after calling Load() to assign data to widgets and other UI
* initilisation which is prohibited in Load()
*
* \warning Do NOT confuse this with Load(), they serve very different purposes
* and most often both should be used when creating a new screen.
*/
void MythScreenType::Init(void)
{
// Virtual
}
void MythScreenType::Close(void)
{
CloseBusyPopup();
if (GetScreenStack())
GetScreenStack()->PopScreen(this);
}
void MythScreenType::ShowMenu(void)
{
// Virtual
}
static void DoSetTextFromMap(MythUIType *UItype, QHash<QString, QString> &infoMap)
{
QList<MythUIType *> *children = UItype->GetAllChildren();
MythUIText *textType;
QMutableListIterator<MythUIType *> i(*children);
while (i.hasNext())
{
MythUIType *type = i.next();
textType = dynamic_cast<MythUIText *> (type);
if (textType)
textType->SetTextFromMap(infoMap);
MythUIGroup *group = dynamic_cast<MythUIGroup *> (type);
if (group)
DoSetTextFromMap(type, infoMap);
}
}
void MythScreenType::SetTextFromMap(QHash<QString, QString> &infoMap)
{
DoSetTextFromMap((MythUIType*) this, infoMap);
}
static void DoResetMap(MythUIType *UItype, QHash<QString, QString> &infoMap)
{
if (infoMap.isEmpty())
return;
QList<MythUIType *> *children = UItype->GetAllChildren();
MythUIText *textType;
QMutableListIterator<MythUIType *> i(*children);
while (i.hasNext())
{
MythUIType *type = i.next();
if (!type->IsVisible())
continue;
textType = dynamic_cast<MythUIText *> (type);
if (textType && infoMap.contains(textType->objectName()))
textType->Reset();
MythUIGroup *group = dynamic_cast<MythUIGroup *> (type);
if (group)
DoResetMap(type, infoMap);
}
}
void MythScreenType::ResetMap(QHash<QString, QString> &infoMap)
{
DoResetMap(this, infoMap);
}
bool MythScreenType::keyPressEvent(QKeyEvent *event)
{
if (m_CurrentFocusWidget && m_CurrentFocusWidget->keyPressEvent(event))
return true;
bool handled = false;
QStringList actions;
handled = GetMythMainWindow()->TranslateKeyPress("Global", event, actions);
for (int i = 0; i < actions.size() && !handled; i++)
{
QString action = actions[i];
handled = true;
if (action == "LEFT" || action == "UP" || action == "PREVIOUS")
{
if (!NextPrevWidgetFocus(false))
handled = false;
}
else if (action == "RIGHT" || action == "DOWN" || action == "NEXT")
{
if (!NextPrevWidgetFocus(true))
handled = false;
}
else if (action == "ESCAPE")
Close();
else if (action == "MENU")
ShowMenu();
else if (action.startsWith("SYSEVENT"))
gCoreContext->SendSystemEvent(QString("KEY_%1").arg(action.mid(8)));
else if (action == ACTION_SCREENSHOT)
GetMythMainWindow()->ScreenShot();
else if (action == ACTION_TVPOWERON)
GetMythMainWindow()->HandleTVPower(true);
else if (action == ACTION_TVPOWEROFF)
GetMythMainWindow()->HandleTVPower(false);
else
handled = false;
}
return handled;
}
bool MythScreenType::gestureEvent(MythGestureEvent *event)
{
bool handled = false;
if (event->gesture() == MythGestureEvent::Click)
{
switch (event->GetButton())
{
case MythGestureEvent::RightButton :
ShowMenu();
handled = true;
break;
default :
break;
}
}
if (!handled)
{
MythUIType *clicked = GetChildAt(event->GetPosition());
if (clicked && clicked->IsEnabled())
{
SetFocusWidget(clicked);
if (clicked->gestureEvent(event))
handled = true;
}
}
return handled;
}
/**
* \copydoc MythUIType::ParseElement()
*/
bool MythScreenType::ParseElement(
const QString &filename, QDomElement &element, bool showWarnings)
{
if (element.tagName() == "area")
{
MythRect rect = parseRect(element, false);
MythRect rectN = parseRect(element);
QRect screenArea = GetMythMainWindow()->GetUIScreenRect();
if (rect.x() == -1)
rectN.moveLeft((screenArea.width() - rectN.width()) / 2);
if (rect.y() == -1)
rectN.moveTop((screenArea.height() - rectN.height()) / 2);
SetArea(rectN);
if (m_Area.width() < screenArea.width() ||
m_Area.height() < screenArea.height())
{
m_FullScreen = false;
}
else
{
m_FullScreen = true;
}
}
else
{
return MythUIType::ParseElement(filename, element, showWarnings);
}
return true;
}
/**
* \copydoc MythUIType::CopyFrom()
*/
void MythScreenType::CopyFrom(MythUIType *base)
{
MythScreenType *st = dynamic_cast<MythScreenType *>(base);
if (!st)
{
LOG(VB_GENERAL, LOG_ERR, "ERROR, bad parsing");
return;
}
m_FullScreen = st->m_FullScreen;
m_IsDeleting = false;
MythUIType::CopyFrom(base);
ConnectDependants(true);
BuildFocusList();
};
/**
* \copydoc MythUIType::CreateCopy()
*
* Do not use.
*
*/
void MythScreenType::CreateCopy(MythUIType *)
{
LOG(VB_GENERAL, LOG_ERR, "CreateCopy called on screentype - bad.");
}
MythPainter* MythScreenType::GetPainter(void)
{
if (m_Painter)
return m_Painter;
if (m_ScreenStack)
return m_ScreenStack->GetPainter();
return GetMythPainter();
}