MythTV  master
mythscreenstack.cpp
Go to the documentation of this file.
1 #include "mythscreenstack.h"
2 #include "mythmainwindow.h"
3 #include "mythscreentype.h"
4 #include "mythpainter.h"
5 #include "mythevent.h"
6 
7 #include <cassert>
8 
9 #include <QCoreApplication>
10 #include <QTimer>
11 #include <QString>
12 
13 const int kFadeVal = 20;
14 
15 MythScreenStack::MythScreenStack(MythMainWindow *parent, const QString &name,
16  bool mainstack)
17  : QObject(parent)
18 {
19  setObjectName(name);
20 
21  if (parent)
22  parent->AddScreenStack(this, mainstack);
23 
24  EnableEffects();
25 }
26 
28 {
29  CheckDeletes(true);
30 
31  while (!m_children.isEmpty())
32  {
33  MythScreenType *child = m_children.back();
34  MythScreenStack::PopScreen(child, false, true); // Don't fade, do delete
35  }
36 }
37 
39 {
42 }
43 
45 {
46  return m_children.count();
47 }
48 
49 void MythScreenStack::AddScreen(MythScreenType *screen, bool allowFade)
50 {
51  if (!screen)
52  return;
53 
54  m_doInit = false;
55 
57  if (old && screen->IsFullscreen())
58  old->aboutToHide();
59 
60  m_children.push_back(screen);
61 
62  if (allowFade && m_doTransitions)
63  {
64  m_newTop = screen;
66  }
67  else
68  {
69  if (parent())
70  reinterpret_cast<MythMainWindow *>(parent())->update();
72  if (!screen->IsInitialized())
73  m_doInit = true;
74  }
75 
76  screen->aboutToShow();
77 
78  m_topScreen = screen;
79 
81 }
82 
83 void MythScreenStack::PopScreen(MythScreenType *screen, bool allowFade,
84  bool deleteScreen)
85 {
86  if (!screen)
87  {
88  screen = m_topScreen;
89  }
90  if (!screen || screen->IsDeleting())
91  return;
92 
93  bool poppedFullscreen = screen->IsFullscreen();
94 
95  screen->aboutToHide();
96 
97  if (m_children.isEmpty())
98  return;
99 
100  MythMainWindow *mainwindow = GetMythMainWindow();
101 
102  screen->setParent(nullptr);
103  if ((screen == m_topScreen) && allowFade && m_doTransitions
104  && !mainwindow->IsExitingToMain())
105  {
106  screen->SetFullscreen(false);
107  if (deleteScreen)
108  {
109  screen->SetDeleting(true);
110  m_toDelete.push_back(screen);
111  }
112  screen->AdjustAlpha(1, -kFadeVal);
113  }
114  else
115  {
116  for (int i = 0; i < m_children.size(); ++i)
117  {
118  if (m_children.at(i) == screen)
119  m_children.remove(i);
120  }
121  if (deleteScreen)
122  screen->deleteLater();
123 
124  screen = nullptr;
125 
126  mainwindow->update();
127  if (mainwindow->IsExitingToMain())
128  {
129  QCoreApplication::postEvent(
130  mainwindow, new QEvent(MythEvent::kExitToMainMenuEventType));
131  }
132  }
133 
134  m_topScreen = nullptr;
135 
137 
138  // If we're fading it, we still want to draw it.
139  if (screen && !m_drawOrder.contains(screen))
140  m_drawOrder.push_back(screen);
141 
142  if (!m_children.isEmpty())
143  {
144  for (auto *draw : qAsConst(m_drawOrder))
145  {
146  if (draw != screen && !draw->IsDeleting())
147  {
148  m_topScreen = draw;
149  draw->SetAlpha(255);
150  if (poppedFullscreen)
151  draw->aboutToShow();
152  }
153  }
154  }
155 
156  if (m_topScreen)
157  {
159 
160  if (!allowFade || !m_doTransitions)
162  }
163  else
164  {
165  // Screen still needs to be redrawn if we have popped the last screen
166  // off the popup stack, or similar
167  if (mainwindow->GetMainStack())
168  {
169  MythScreenType *mainscreen = mainwindow->GetMainStack()->GetTopScreen();
170  if (mainscreen)
171  mainscreen->SetRedraw();
172  }
173 
174  if (!allowFade || !m_doTransitions)
175  emit topScreenChanged(nullptr);
176  }
177 }
178 
180 {
181  if (m_topScreen)
182  return m_topScreen;
183  if (!m_drawOrder.isEmpty())
184  return m_drawOrder.back();
185  return nullptr;
186 }
187 
188 void MythScreenStack::GetDrawOrder(QVector<MythScreenType *> &screens)
189 {
190  if (m_inNewTransition)
192  CheckDeletes();
193 
194  screens = m_drawOrder;
195 }
196 
197 void MythScreenStack::GetScreenList(QVector<MythScreenType *> &screens)
198 {
199  if (m_inNewTransition)
201  CheckDeletes();
202 
203  screens = m_children;
204 }
205 
207 {
208  // make sure Init() is called outside the paintEvent
211  {
212  m_initTimerStarted = true;
213  QTimer::singleShot(100, this, &MythScreenStack::doInit);
214  }
215 }
216 
218 {
219  if (m_doInit && m_topScreen)
220  {
221  m_doInit = false;
222 
223  if (!m_topScreen->IsLoaded())
225 
226  if (!m_topScreen->IsInitialized())
227  m_topScreen->doInit();
228  }
229  m_initTimerStarted = false;
230 }
231 
233 {
234  m_drawOrder.clear();
235 
236  if (m_children.isEmpty())
237  return;
238 
239  for (auto *screen : qAsConst(m_children))
240  {
241  if (screen->IsFullscreen())
242  m_drawOrder.clear();
243 
244  m_drawOrder.push_back(screen);
245  }
246 
247  if (m_drawOrder.isEmpty())
248  {
249  MythScreenType *screen = GetTopScreen();
250  if (screen)
251  m_drawOrder.push_back(screen);
252  }
253 }
254 
256 {
257  m_inNewTransition = true;
258  m_newTop->SetAlpha(0);
260 
261  if (m_newTop->IsFullscreen())
262  {
263  for (auto *draw : qAsConst(m_drawOrder))
264  {
265  if (!draw->IsDeleting())
266  draw->AdjustAlpha(1, -kFadeVal);
267  }
268 
269  m_drawOrder.push_back(m_newTop);
270  }
271  else
273 }
274 
276 {
277  if (!m_newTop)
278  {
279  m_inNewTransition = false;
280  return;
281  }
282 
283  if (m_newTop->GetAlpha() >= 255)
284  {
285  m_inNewTransition = false;
286  if (!m_newTop->IsInitialized())
287  m_doInit = true;
288  m_newTop = nullptr;
289 
291  }
292 }
293 
295 {
296  if (m_toDelete.isEmpty())
297  return;
298 
299  bool changed = false;
300 
301  QVector<MythScreenType *>::Iterator it = m_toDelete.begin();
302  while (it != m_toDelete.end() && !m_toDelete.isEmpty())
303  {
304  bool deleteit = false;
305 
306  if (force || (*it)->GetAlpha() <= 0)
307  {
308  deleteit = true;
309  }
310 
311  if (!deleteit)
312  {
313  bool found = false;
314 
315  for (const auto *test : qAsConst(m_drawOrder))
316  {
317  if (*it == test)
318  {
319  found = true;
320  break;
321  }
322  }
323 
324  if (!found)
325  deleteit = true;
326  }
327 
328  if (deleteit)
329  {
330  for (auto *test = m_children.begin();
331  test != m_children.end();
332  ++test)
333  {
334  if (*test == *it)
335  {
336  m_children.erase(test);
337  break;
338  }
339  }
340 
341  if (*it == m_newTop)
342  m_newTop = nullptr;
343  delete (*it);
344  m_toDelete.erase(it);
345  it = m_toDelete.begin();
346  changed = true;
347  continue;
348  }
349 
350  ++it;
351  }
352 
353  if (changed)
354  {
357  }
358 }
359 
360 QString MythScreenStack::GetLocation(bool fullPath) const
361 {
362  if (fullPath)
363  {
364  QString path;
365  for (auto *child : qAsConst(m_children))
366  {
367  if (!child->IsDeleting())
368  {
369  if (path.isEmpty())
370  path = child->objectName();
371  else
372  path += '/' + child->objectName();
373  }
374  }
375  return path;
376  }
377 
378  if (m_topScreen)
379  return m_topScreen->objectName();
380 
381  return QString();
382 }
383 
385 {
386  return GetMythPainter();
387 }
force
bool force
Definition: mythtv/programs/mythcommflag/main.cpp:74
MythScreenType::IsLoading
bool IsLoading(void) const
Definition: mythscreentype.h:81
MythScreenStack::m_doTransitions
bool m_doTransitions
Definition: mythscreenstack.h:60
MythMainWindow::GetMainStack
MythScreenStack * GetMainStack()
Definition: mythmainwindow.cpp:321
mythevent.h
MythPainter::SupportsAlpha
virtual bool SupportsAlpha(void)=0
MythPainter::SupportsAnimation
virtual bool SupportsAnimation(void)=0
MythScreenStack::ScheduleInitIfNeeded
void ScheduleInitIfNeeded(void)
Definition: mythscreenstack.cpp:206
mythscreenstack.h
MythScreenStack::m_inNewTransition
bool m_inNewTransition
Definition: mythscreenstack.h:63
kFadeVal
const int kFadeVal
Definition: mythscreenstack.cpp:13
MythScreenType
Screen in which all other widgets are contained and rendered.
Definition: mythscreentype.h:45
MythScreenStack::EnableEffects
void EnableEffects(void)
Definition: mythscreenstack.cpp:38
MythScreenStack::m_children
QVector< MythScreenType * > m_children
Definition: mythscreenstack.h:55
MythScreenStack::m_drawOrder
QVector< MythScreenType * > m_drawOrder
Definition: mythscreenstack.h:56
MythScreenStack::GetDrawOrder
void GetDrawOrder(QVector< MythScreenType * > &screens)
Definition: mythscreenstack.cpp:188
MythEvent::kExitToMainMenuEventType
static Type kExitToMainMenuEventType
Definition: mythevent.h:76
MythScreenStack::GetScreenList
void GetScreenList(QVector< MythScreenType * > &screens)
Definition: mythscreenstack.cpp:197
MythScreenStack::CheckDeletes
void CheckDeletes(bool force=false)
Definition: mythscreenstack.cpp:294
MythScreenType::IsLoaded
bool IsLoaded(void) const
Definition: mythscreentype.h:82
MythScreenStack::DoNewFadeTransition
void DoNewFadeTransition()
Definition: mythscreenstack.cpp:255
MythUIType::SetAlpha
void SetAlpha(int newalpha)
Definition: mythuitype.cpp:931
MythScreenStack::doInit
void doInit(void)
Definition: mythscreenstack.cpp:217
MythScreenStack::topScreenChanged
void topScreenChanged(MythScreenType *screen)
MythUIType::AdjustAlpha
void AdjustAlpha(int mode, int alphachange, int minalpha=0, int maxalpha=255)
Definition: mythuitype.cpp:913
mythpainter.h
MythScreenStack::GetPainter
static MythPainter * GetPainter(void)
Definition: mythscreenstack.cpp:384
MythScreenStack::TotalScreens
int TotalScreens() const
Definition: mythscreenstack.cpp:44
MythScreenType::LoadInForeground
void LoadInForeground(void)
Definition: mythscreentype.cpp:317
MythMainWindow::AddScreenStack
void AddScreenStack(MythScreenStack *stack, bool main=false)
Definition: mythmainwindow.cpp:300
MythScreenStack::~MythScreenStack
~MythScreenStack() override
Definition: mythscreenstack.cpp:27
MythScreenType::doInit
void doInit(void)
Definition: mythscreentype.cpp:381
MythScreenStack::m_initTimerStarted
bool m_initTimerStarted
Definition: mythscreenstack.h:62
MythScreenStack::m_toDelete
QVector< MythScreenType * > m_toDelete
Definition: mythscreenstack.h:66
MythPainter
Definition: mythpainter.h:33
MythScreenType::IsDeleting
bool IsDeleting(void) const
Definition: mythscreentype.cpp:272
MythScreenStack::PopScreen
virtual void PopScreen(MythScreenType *screen=nullptr, bool allowFade=true, bool deleteScreen=true)
Definition: mythscreenstack.cpp:83
MythScreenStack::m_topScreen
MythScreenType * m_topScreen
Definition: mythscreenstack.h:58
GetMythMainWindow
MythMainWindow * GetMythMainWindow(void)
Definition: mythmainwindow.cpp:105
MythScreenStack::MythScreenStack
MythScreenStack(MythMainWindow *parent, const QString &name, bool main=false)
Definition: mythscreenstack.cpp:15
MythUIType::GetAlpha
int GetAlpha(void) const
Definition: mythuitype.cpp:940
MythScreenStack::GetLocation
QString GetLocation(bool fullPath) const
Definition: mythscreenstack.cpp:360
MythScreenStack::m_doInit
bool m_doInit
Definition: mythscreenstack.h:61
MythScreenStack::CheckNewFadeTransition
void CheckNewFadeTransition()
Definition: mythscreenstack.cpp:275
MythScreenType::aboutToHide
virtual void aboutToHide(void)
Definition: mythscreentype.cpp:238
MythScreenType::SetDeleting
void SetDeleting(bool deleting)
Definition: mythscreentype.cpp:277
MythScreenStack::RecalculateDrawOrder
virtual void RecalculateDrawOrder(void)
Definition: mythscreenstack.cpp:232
GetMythPainter
MythPainter * GetMythPainter(void)
Definition: mythmainwindow.cpp:120
MythScreenType::aboutToShow
virtual void aboutToShow(void)
Definition: mythscreentype.cpp:254
mythmainwindow.h
MythScreenStack::AddScreen
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
Definition: mythscreenstack.cpp:49
MythUIType::SetRedraw
void SetRedraw(void)
Definition: mythuitype.cpp:294
MythScreenType::IsInitialized
bool IsInitialized(void) const
Has Init() been called on this screen?
Definition: mythscreentype.cpp:376
MythScreenType::IsFullscreen
bool IsFullscreen(void) const
Definition: mythscreentype.cpp:102
MythScreenStack::m_newTop
MythScreenType * m_newTop
Definition: mythscreenstack.h:64
MythMainWindow
Definition: mythmainwindow.h:35
mythscreentype.h
MythMainWindow::IsExitingToMain
bool IsExitingToMain(void) const
Definition: mythmainwindow.cpp:1067
MythScreenType::SetFullscreen
void SetFullscreen(bool full)
Definition: mythscreentype.cpp:107
MythScreenStack::GetTopScreen
virtual MythScreenType * GetTopScreen(void) const
Definition: mythscreenstack.cpp:179