-
Notifications
You must be signed in to change notification settings - Fork 345
/
mythmainwindow.cpp
3052 lines (2599 loc) · 88.6 KB
/
mythmainwindow.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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "mythmainwindow.h"
#include "mythmainwindow_internal.h"
// C headers
#include <cmath>
// C++ headers
#include <algorithm>
#include <vector>
using namespace std;
// QT headers
#ifdef USE_OPENGL_PAINTER
#include <QGLFormat>
#endif
#include <QWaitCondition>
#include <QApplication>
#include <QTimer>
#include <QDesktopWidget>
#include <QHash>
#include <QFile>
#include <QDir>
#include <QEvent>
#include <QKeyEvent>
#include <QKeySequence>
#include <QSize>
// Platform headers
#include "unistd.h"
#ifdef QWS
#include <qwindowsystem_qws.h>
#endif
#ifdef Q_WS_MACX_OLDQT
#include <HIToolbox/Menus.h> // For GetMBarHeight()
#endif
// libmythbase headers
#include "mythdb.h"
#include "mythlogging.h"
#include "mythevent.h"
#include "mythdirs.h"
#include "compat.h"
#include "mythsignalingtimer.h"
#include "mythcorecontext.h"
#include "mythmedia.h"
#include "mythmiscutil.h"
#include "mythdate.h"
// libmythui headers
#include "myththemebase.h"
#include "screensaver.h"
#include "lirc.h"
#include "lircevent.h"
#include "mythudplistener.h"
#include "mythrender_base.h"
#include "mythuistatetracker.h"
#include "mythuiactions.h"
#include "mythrect.h"
#include "mythuidefines.h"
#ifdef USING_APPLEREMOTE
#include "AppleRemoteListener.h"
#endif
#ifdef USE_JOYSTICK_MENU
#include "jsmenu.h"
#include "jsmenuevent.h"
#endif
#ifdef USING_LIBCEC
#include "cecadapter.h"
#endif
#include "mythscreentype.h"
#include "mythpainter.h"
#ifdef USE_OPENGL_PAINTER
#include "mythpainter_ogl.h"
#endif
#include "mythpainter_qt.h"
#include "mythgesture.h"
#include "mythuihelper.h"
#include "mythdialogbox.h"
#ifdef _WIN32
#include "mythpainter_d3d9.h"
#endif
#define GESTURE_TIMEOUT 1000
#define STANDBY_TIMEOUT 90 // Minutes
#define LOC QString("MythMainWindow: ")
class KeyContext
{
public:
void AddMapping(int key, QString action)
{
actionMap[key].append(action);
}
bool GetMapping(int key, QStringList &actions)
{
if (actionMap.count(key) > 0)
{
actions += actionMap[key];
return true;
}
return false;
}
QMap<int, QStringList> actionMap;
};
struct JumpData
{
void (*callback)(void);
QString destination;
QString description;
bool exittomain;
QString localAction;
};
struct MPData {
QString description;
MediaPlayCallback playFn;
};
class MythMainWindowPrivate
{
public:
MythMainWindowPrivate() :
wmult(1.0f), hmult(1.0f),
screenwidth(0), screenheight(0),
xbase(0), ybase(0),
does_fill_screen(false),
ignore_lirc_keys(false),
ignore_joystick_keys(false),
lircThread(NULL),
#ifdef USE_JOYSTICK_MENU
joystickThread(NULL),
#endif
#ifdef USING_APPLEREMOTE
appleRemoteListener(NULL),
appleRemote(NULL),
#endif
#ifdef USING_LIBCEC
cecAdapter(NULL),
#endif
exitingtomain(false),
popwindows(false),
m_useDB(true),
exitmenucallback(NULL),
exitmenumediadevicecallback(NULL),
mediadeviceforcallback(NULL),
escapekey(0),
sysEventHandler(NULL),
drawInterval(1000 / MythMainWindow::drawRefresh),
drawTimer(NULL),
mainStack(NULL),
painter(NULL),
render(NULL),
AllowInput(true),
gesture(MythGesture()),
gestureTimer(NULL),
hideMouseTimer(NULL),
paintwin(NULL),
oldpaintwin(NULL),
oldpainter(NULL),
oldrender(NULL),
m_drawDisabledDepth(0),
m_drawEnabled(true),
m_themeBase(NULL),
m_udpListener(NULL),
m_pendingUpdate(false),
idleTimer(NULL),
idleTime(0),
standby(false),
enteringStandby(false),
disableIdle(false),
NC(NULL),
firstinit(true),
m_bSavedPOS(false)
{
}
int TranslateKeyNum(QKeyEvent *e);
float wmult, hmult;
int screenwidth, screenheight;
QRect screenRect;
QRect uiScreenRect;
int xbase, ybase;
bool does_fill_screen;
bool ignore_lirc_keys;
bool ignore_joystick_keys;
LIRC *lircThread;
#ifdef USE_JOYSTICK_MENU
JoystickMenuThread *joystickThread;
#endif
#ifdef USING_APPLEREMOTE
AppleRemoteListener *appleRemoteListener;
AppleRemote *appleRemote;
#endif
#ifdef USING_LIBCEC
CECAdapter* cecAdapter;
#endif
bool exitingtomain;
bool popwindows;
bool m_useDB; ///< To allow or prevent database access
QHash<QString, KeyContext *> keyContexts;
QMap<int, JumpData*> jumpMap;
QMap<QString, JumpData> destinationMap;
QMap<QString, MPData> mediaPluginMap;
QHash<QString, QHash<QString, QString> > actionText;
void (*exitmenucallback)(void);
void (*exitmenumediadevicecallback)(MythMediaDevice* mediadevice);
MythMediaDevice * mediadeviceforcallback;
int escapekey;
QObject *sysEventHandler;
int drawInterval;
MythSignalingTimer *drawTimer;
QVector<MythScreenStack *> stackList;
MythScreenStack *mainStack;
MythPainter *painter;
MythRender *render;
bool AllowInput;
QRegion repaintRegion;
MythGesture gesture;
QTimer *gestureTimer;
QTimer *hideMouseTimer;
/* compatibility only, FIXME remove */
std::vector<QWidget *> widgetList;
QMap<QWidget *, bool> enabledWidgets;
QWidget *paintwin;
QWidget *oldpaintwin;
MythPainter *oldpainter;
MythRender *oldrender;
QMutex m_drawDisableLock;
QMutex m_setDrawEnabledLock;
QWaitCondition m_setDrawEnabledWait;
uint m_drawDisabledDepth;
bool m_drawEnabled;
MythThemeBase *m_themeBase;
MythUDPListener *m_udpListener;
bool m_pendingUpdate;
QTimer *idleTimer;
int idleTime;
bool standby;
bool enteringStandby;
bool disableIdle;
MythNotificationCenter *NC;
// window aspect
bool firstinit;
bool m_bSavedPOS;
};
// Make keynum in QKeyEvent be equivalent to what's in QKeySequence
int MythMainWindowPrivate::TranslateKeyNum(QKeyEvent* e)
{
int keynum = e->key();
if ((keynum != Qt::Key_Shift ) && (keynum !=Qt::Key_Control ) &&
(keynum != Qt::Key_Meta ) && (keynum !=Qt::Key_Alt ) &&
(keynum != Qt::Key_Super_L) && (keynum !=Qt::Key_Super_R ) &&
(keynum != Qt::Key_Hyper_L) && (keynum !=Qt::Key_Hyper_R ) &&
(keynum != Qt::Key_AltGr ) && (keynum !=Qt::Key_CapsLock ) &&
(keynum != Qt::Key_NumLock) && (keynum !=Qt::Key_ScrollLock ))
{
Qt::KeyboardModifiers modifiers;
// if modifiers have been pressed, rebuild keynum
if ((modifiers = e->modifiers()) != Qt::NoModifier)
{
int modnum = (((modifiers & Qt::ShiftModifier) &&
(keynum > 0x7f) &&
(keynum != Qt::Key_Backtab)) ? Qt::SHIFT : 0) |
((modifiers & Qt::ControlModifier) ? Qt::CTRL : 0) |
((modifiers & Qt::MetaModifier) ? Qt::META : 0) |
((modifiers & Qt::AltModifier) ? Qt::ALT : 0);
modnum &= ~Qt::UNICODE_ACCEL;
return (keynum |= modnum);
}
}
return keynum;
}
static MythMainWindow *mainWin = NULL;
static QMutex mainLock;
/**
* \brief Return the existing main window, or create one
* \param useDB
* If this is a temporary window, which is used to bootstrap
* the database, passing false prevents any database access.
*
* \sa MythContextPrivate::TempMainWindow()
*/
MythMainWindow *MythMainWindow::getMainWindow(const bool useDB)
{
if (mainWin)
return mainWin;
QMutexLocker lock(&mainLock);
if (!mainWin)
{
mainWin = new MythMainWindow(useDB);
gCoreContext->SetGUIObject(mainWin);
}
return mainWin;
}
void MythMainWindow::destroyMainWindow(void)
{
if (gCoreContext)
gCoreContext->SetGUIObject(NULL);
delete mainWin;
mainWin = NULL;
}
MythMainWindow *GetMythMainWindow(void)
{
return MythMainWindow::getMainWindow();
}
bool HasMythMainWindow(void)
{
return (mainWin);
}
void DestroyMythMainWindow(void)
{
MythMainWindow::destroyMainWindow();
}
MythPainter *GetMythPainter(void)
{
return MythMainWindow::getMainWindow()->GetCurrentPainter();
}
MythNotificationCenter *GetNotificationCenter(void)
{
if (!mainWin ||
!mainWin->GetCurrentNotificationCenter())
return NULL;
return mainWin->GetCurrentNotificationCenter();
}
#ifdef USE_OPENGL_PAINTER
MythPainterWindowGL::MythPainterWindowGL(MythMainWindow *win,
MythMainWindowPrivate *priv,
MythRenderOpenGL *rend)
: MythPainterWindowWidget(win), parent(win), d(priv), render(rend)
{
rend->setWidget(this);
#ifdef USE_OPENGL_QT5
setAttribute(Qt::WA_NoSystemBackground);
#else
setAutoBufferSwap(false);
#endif
}
#ifdef USE_OPENGL_QT5
QPaintEngine *MythPainterWindowGL::paintEngine() const
{
return testAttribute(Qt::WA_PaintOnScreen) ? 0 : parent->paintEngine();
}
MythPainterWindowGL::~MythPainterWindowGL()
{
if (render)
{
render->DecrRef();
render = NULL;
}
}
#endif
void MythPainterWindowGL::paintEvent(QPaintEvent *pe)
{
d->repaintRegion = d->repaintRegion.united(pe->region());
parent->drawScreen();
}
#endif
#ifdef _WIN32
MythPainterWindowD3D9::MythPainterWindowD3D9(MythMainWindow *win,
MythMainWindowPrivate *priv)
: QGLWidget(win),
parent(win), d(priv)
{
setAutoBufferSwap(false);
}
void MythPainterWindowD3D9::paintEvent(QPaintEvent *pe)
{
d->repaintRegion = d->repaintRegion.united(pe->region());
parent->drawScreen();
}
#endif
MythPainterWindowQt::MythPainterWindowQt(MythMainWindow *win,
MythMainWindowPrivate *priv)
: QWidget(win),
parent(win), d(priv)
{
#ifdef USE_OPENGL_QT5
setAttribute(Qt::WA_NoSystemBackground);
#endif
}
void MythPainterWindowQt::paintEvent(QPaintEvent *pe)
{
d->repaintRegion = d->repaintRegion.united(pe->region());
parent->drawScreen();
}
MythMainWindow::MythMainWindow(const bool useDB)
: QWidget(NULL)
{
d = new MythMainWindowPrivate;
setObjectName("mainwindow");
d->AllowInput = false;
// This prevents database errors from RegisterKey() when there is no DB:
d->m_useDB = useDB;
d->painter = NULL;
d->paintwin = NULL;
d->oldpainter = NULL;
d->oldpaintwin = NULL;
d->oldrender = NULL;
//Init();
d->ignore_lirc_keys = false;
d->ignore_joystick_keys = false;
d->exitingtomain = false;
d->popwindows = true;
d->exitmenucallback = NULL;
d->exitmenumediadevicecallback = NULL;
d->mediadeviceforcallback = NULL;
d->escapekey = Qt::Key_Escape;
d->mainStack = NULL;
d->sysEventHandler = NULL;
installEventFilter(this);
d->lircThread = NULL;
StartLIRC();
#ifdef USE_JOYSTICK_MENU
d->ignore_joystick_keys = false;
QString joy_config_file = GetConfDir() + "/joystickmenurc";
d->joystickThread = NULL;
d->joystickThread = new JoystickMenuThread(this);
if (d->joystickThread->Init(joy_config_file))
d->joystickThread->start();
#endif
#ifdef USING_APPLEREMOTE
d->appleRemoteListener = new AppleRemoteListener(this);
d->appleRemote = AppleRemote::Get();
d->appleRemote->setListener(d->appleRemoteListener);
d->appleRemote->startListening();
if (d->appleRemote->isListeningToRemote())
{
d->appleRemote->start();
}
else
{
// start listening failed, no remote receiver present
delete d->appleRemote;
delete d->appleRemoteListener;
d->appleRemote = NULL;
d->appleRemoteListener = NULL;
}
#endif
#ifdef USING_LIBCEC
d->cecAdapter = new CECAdapter();
if (!d->cecAdapter->IsValid())
{
delete d->cecAdapter;
d->cecAdapter = NULL;
}
#endif
d->m_udpListener = new MythUDPListener();
InitKeys();
d->gestureTimer = new QTimer(this);
connect(d->gestureTimer, SIGNAL(timeout()), this, SLOT(mouseTimeout()));
d->hideMouseTimer = new QTimer(this);
d->hideMouseTimer->setSingleShot(true);
d->hideMouseTimer->setInterval(3000); // 3 seconds
connect(d->hideMouseTimer, SIGNAL(timeout()), SLOT(HideMouseTimeout()));
d->drawTimer = new MythSignalingTimer(this, SLOT(animate()));
d->drawTimer->start(d->drawInterval);
d->AllowInput = true;
d->repaintRegion = QRegion(QRect(0,0,0,0));
d->m_drawEnabled = true;
connect(this, SIGNAL(signalRemoteScreenShot(QString,int,int)),
this, SLOT(doRemoteScreenShot(QString,int,int)),
Qt::BlockingQueuedConnection);
// We need to listen for playback start/end events
gCoreContext->addListener(this);
d->idleTime = gCoreContext->GetNumSetting("FrontendIdleTimeout",
STANDBY_TIMEOUT);
if (d->idleTime < 0)
d->idleTime = 0;
d->idleTimer = new QTimer(this);
d->idleTimer->setSingleShot(false);
d->idleTimer->setInterval(1000 * 60 * d->idleTime);
connect(d->idleTimer, SIGNAL(timeout()), SLOT(IdleTimeout()));
if (d->idleTime > 0)
d->idleTimer->start();
}
MythMainWindow::~MythMainWindow()
{
gCoreContext->removeListener(this);
d->drawTimer->stop();
while (!d->stackList.isEmpty())
{
MythScreenStack *stack = d->stackList.back();
d->stackList.pop_back();
if (stack == d->mainStack)
d->mainStack = NULL;
delete stack;
}
delete d->m_themeBase;
while (!d->keyContexts.isEmpty())
{
KeyContext *context = *d->keyContexts.begin();
d->keyContexts.erase(d->keyContexts.begin());
delete context;
}
#ifdef USE_LIRC
if (d->lircThread)
{
d->lircThread->deleteLater();
d->lircThread = NULL;
}
#endif
#ifdef USE_JOYSTICK_MENU
if (d->joystickThread)
{
if (d->joystickThread->isRunning())
{
d->joystickThread->Stop();
d->joystickThread->wait();
}
delete d->joystickThread;
d->joystickThread = NULL;
}
#endif
#ifdef USING_APPLEREMOTE
delete d->appleRemote;
delete d->appleRemoteListener;
#endif
#ifdef USING_LIBCEC
if (d->cecAdapter)
delete d->cecAdapter;
#endif
delete d->NC;
delete d;
}
MythPainter *MythMainWindow::GetCurrentPainter(void)
{
return d->painter;
}
MythNotificationCenter *MythMainWindow::GetCurrentNotificationCenter(void)
{
return d->NC;
}
QWidget *MythMainWindow::GetPaintWindow(void)
{
return d->paintwin;
}
void MythMainWindow::ShowPainterWindow(void)
{
if (d->paintwin)
d->paintwin->show();
if (d->render)
d->render->Release();
}
void MythMainWindow::HidePainterWindow(void)
{
if (d->paintwin)
{
d->paintwin->clearMask();
if (!(d->render && d->render->IsShared()))
d->paintwin->hide();
}
}
void MythMainWindow::ResizePainterWindow(const QSize &size)
{
if (!d->paintwin)
return;
d->paintwin->setFixedSize(size);
d->paintwin->resize(size);
}
MythRender *MythMainWindow::GetRenderDevice()
{
return d->render;
}
void MythMainWindow::AddScreenStack(MythScreenStack *stack, bool main)
{
d->stackList.push_back(stack);
if (main)
d->mainStack = stack;
}
void MythMainWindow::PopScreenStack()
{
MythScreenStack *stack = d->stackList.back();
d->stackList.pop_back();
if (stack == d->mainStack)
d->mainStack = NULL;
delete stack;
}
int MythMainWindow::GetStackCount(void)
{
return d->stackList.size();
}
MythScreenStack *MythMainWindow::GetMainStack(void)
{
return d->mainStack;
}
MythScreenStack *MythMainWindow::GetStack(const QString &stackname)
{
QVector<MythScreenStack *>::Iterator it;
for (it = d->stackList.begin(); it != d->stackList.end(); ++it)
{
if ((*it)->objectName() == stackname)
return *it;
}
return NULL;
}
MythScreenStack* MythMainWindow::GetStackAt(int pos)
{
if (pos >= 0 && pos < d->stackList.size())
return d->stackList.at(pos);
return NULL;
}
void MythMainWindow::animate(void)
{
/* FIXME: remove */
if (currentWidget() || !d->m_drawEnabled)
return;
if (!d->paintwin)
return;
d->drawTimer->blockSignals(true);
bool redraw = false;
if (!d->repaintRegion.isEmpty())
redraw = true;
QVector<MythScreenStack *>::Iterator it;
for (it = d->stackList.begin(); it != d->stackList.end(); ++it)
{
QVector<MythScreenType *> drawList;
(*it)->GetDrawOrder(drawList);
QVector<MythScreenType *>::Iterator screenit;
for (screenit = drawList.begin(); screenit != drawList.end();
++screenit)
{
(*screenit)->Pulse();
if ((*screenit)->NeedsRedraw())
{
QRegion topDirty = (*screenit)->GetDirtyArea();
(*screenit)->ResetNeedsRedraw();
d->repaintRegion = d->repaintRegion.united(topDirty);
redraw = true;
}
}
}
if (redraw && !(d->render && d->render->IsShared()))
d->paintwin->update(d->repaintRegion);
for (it = d->stackList.begin(); it != d->stackList.end(); ++it)
(*it)->ScheduleInitIfNeeded();
d->drawTimer->blockSignals(false);
}
void MythMainWindow::drawScreen(void)
{
/* FIXME: remove */
if (currentWidget() || !d->m_drawEnabled)
return;
if (!d->painter->SupportsClipping())
d->repaintRegion = d->repaintRegion.united(d->uiScreenRect);
else
{
// Ensure that the region is not larger than the screen which
// can happen with bad themes
d->repaintRegion = d->repaintRegion.intersected(d->uiScreenRect);
// Check for any widgets that have been updated since we built
// the dirty region list in ::animate()
QVector<MythScreenStack *>::Iterator it;
for (it = d->stackList.begin(); it != d->stackList.end(); ++it)
{
QVector<MythScreenType *> redrawList;
(*it)->GetDrawOrder(redrawList);
QVector<MythScreenType *>::Iterator screenit;
for (screenit = redrawList.begin(); screenit != redrawList.end();
++screenit)
{
if ((*screenit)->NeedsRedraw())
{
QRegion topDirty = (*screenit)->GetDirtyArea();
QVector<QRect> wrects = topDirty.rects();
for (int i = 0; i < wrects.size(); i++)
{
bool foundThisRect = false;
QVector<QRect> drects = d->repaintRegion.rects();
for (int j = 0; j < drects.size(); j++)
{
if (drects[j].contains(wrects[i]))
{
foundThisRect = true;
break;
}
}
if (!foundThisRect)
return;
}
}
}
}
}
if (!(d->render && d->render->IsShared()))
draw();
d->repaintRegion = QRegion(QRect(0, 0, 0, 0));
}
void MythMainWindow::draw(MythPainter *painter /* = 0 */)
{
if (!painter)
painter = d->painter;
if (!painter)
return;
painter->Begin(d->paintwin);
if (!painter->SupportsClipping())
d->repaintRegion = QRegion(d->uiScreenRect);
QVector<QRect> rects = d->repaintRegion.rects();
for (int i = 0; i < rects.size(); i++)
{
if (rects[i].width() == 0 || rects[i].height() == 0)
continue;
if (rects[i] != d->uiScreenRect)
painter->SetClipRect(rects[i]);
QVector<MythScreenStack *>::Iterator it;
for (it = d->stackList.begin(); it != d->stackList.end(); ++it)
{
QVector<MythScreenType *> redrawList;
(*it)->GetDrawOrder(redrawList);
QVector<MythScreenType *>::Iterator screenit;
for (screenit = redrawList.begin(); screenit != redrawList.end();
++screenit)
{
(*screenit)->Draw(painter, 0, 0, 255, rects[i]);
}
}
}
painter->End();
d->repaintRegion = QRegion();
}
// virtual
QPaintEngine *MythMainWindow::paintEngine() const
{
#ifdef USE_OPENGL_QT5
return testAttribute(Qt::WA_PaintOnScreen) ? 0 : QWidget::paintEngine();
#else
return QWidget::paintEngine();
#endif
}
void MythMainWindow::closeEvent(QCloseEvent *e)
{
if (e->spontaneous())
{
QKeyEvent *key = new QKeyEvent(QEvent::KeyPress, d->escapekey,
Qt::NoModifier);
QCoreApplication::postEvent(this, key);
e->ignore();
}
else
QWidget::closeEvent(e);
}
void MythMainWindow::GrabWindow(QImage &image)
{
WId winid;
QWidget *active = QApplication::activeWindow();
if (active)
winid = active->winId();
else
winid = QApplication::desktop()->winId();
QPixmap p = QPixmap::grabWindow(winid);
image = p.toImage();
}
/* This is required to allow a screenshot to be requested by another thread
* other than the UI thread, and to wait for the screenshot before returning.
* It is used by mythweb for the remote access screenshots
*/
void MythMainWindow::doRemoteScreenShot(QString filename, int x, int y)
{
// This will be running in the UI thread, as is required by QPixmap
QStringList args;
args << QString::number(x);
args << QString::number(y);
args << filename;
MythEvent me(MythEvent::MythEventMessage, ACTION_SCREENSHOT, args);
qApp->sendEvent(this, &me);
}
void MythMainWindow::RemoteScreenShot(QString filename, int x, int y)
{
// This will be running in a non-UI thread and is used to trigger a
// function in the UI thread, and waits for completion of that handler
emit signalRemoteScreenShot(filename, x, y);
}
bool MythMainWindow::SaveScreenShot(const QImage &image, QString filename)
{
if (filename.isEmpty())
{
QString fpath = GetMythDB()->GetSetting("ScreenShotPath", "/tmp");
filename = QString("%1/myth-screenshot-%2.png").arg(fpath)
.arg(MythDate::toString(
MythDate::current(), MythDate::kScreenShotFilename));
}
QString extension = filename.section('.', -1, -1);
if (extension == "jpg")
extension = "JPEG";
else
extension = "PNG";
LOG(VB_GENERAL, LOG_INFO, QString("Saving screenshot to %1 (%2x%3)")
.arg(filename).arg(image.width()).arg(image.height()));
if (image.save(filename, extension.toLatin1(), 100))
{
LOG(VB_GENERAL, LOG_INFO, "MythMainWindow::screenShot succeeded");
return true;
}
LOG(VB_GENERAL, LOG_INFO, "MythMainWindow::screenShot Failed!");
return false;
}
bool MythMainWindow::ScreenShot(int w, int h, QString filename)
{
QImage img;
GrabWindow(img);
if (w <= 0)
w = img.width();
if (h <= 0)
h = img.height();
img = img.scaled(w, h, Qt::KeepAspectRatio, Qt::SmoothTransformation);
return SaveScreenShot(img, filename);
}
bool MythMainWindow::event(QEvent *e)
{
if (!updatesEnabled() && (e->type() == QEvent::UpdateRequest))
d->m_pendingUpdate = true;
if (e->type() == QEvent::Show && !e->spontaneous())
{
QCoreApplication::postEvent(
this, new QEvent(MythEvent::kMythPostShowEventType));
}
if (e->type() == MythEvent::kMythPostShowEventType)
{
raise();
activateWindow();
return true;
}