MythTV  master
mythuistatetype.cpp
Go to the documentation of this file.
1 // Own header
2 #include "mythuistatetype.h"
3 #include "mythuibuttonlist.h"
4 
5 // Qt headers
6 #include <QDomDocument>
7 
8 // MythUI headers
9 #include "mythimage.h"
10 #include "mythuiimage.h"
11 #include "mythuigroup.h"
12 #include "mythuitext.h"
13 #include "mythpainter.h"
14 #include "mythmainwindow.h"
15 
16 MythUIStateType::MythUIStateType(MythUIType *parent, const QString &name)
17  : MythUIComposite(parent, name)
18 {
19  emit DependChanged(false);
20 }
21 
22 bool MythUIStateType::AddImage(const QString &name, MythImage *image)
23 {
24  QString key = name.toLower();
25 
26  if (m_objectsByName.contains(key) || !image)
27  return false;
28 
29  // Uses name, not key which is lower case otherwise we break
30  // inheritance
31  auto *imType = new MythUIImage(this, name);
32  imType->SetImage(image);
33 
34  return AddObject(key, imType);
35 }
36 
37 bool MythUIStateType::AddObject(const QString &name, MythUIType *object)
38 {
39  QString key = name.toLower();
40 
41  if (m_objectsByName.contains(key) || !object)
42  return false;
43 
44  object->SetVisible(false);
45  m_objectsByName[key] = object;
46 
47  MythRect objectArea = object->GetArea();
48  objectArea.CalculateArea(m_parentArea);
49 
50  ExpandArea(objectArea);
51 
52  return true;
53 }
54 
56 {
57  if (m_objectsByState.contains((int)type) || !image)
58  return false;
59 
60  QString name = QString("stateimage%1").arg(type);
61 
62  auto *imType = new MythUIImage(this, name);
63  imType->SetImage(image);
64 
65  return AddObject(type, imType);
66 }
67 
69 {
70  if (m_objectsByState.contains((int)type) || !object)
71  return false;
72 
73  object->SetVisible(false);
74  m_objectsByState[(int)type] = object;
75 
76  MythRect objectArea = object->GetArea();
77  objectArea.CalculateArea(m_parentArea);
78 
79  ExpandArea(objectArea);
80 
81  return true;
82 }
83 
84 bool MythUIStateType::DisplayState(const QString &name)
85 {
86  if (name.isEmpty())
87  return false;
88 
90 
91  QMap<QString, MythUIType *>::Iterator i = m_objectsByName.find(name.toLower());
92 
93  if (i != m_objectsByName.end())
94  m_currentState = i.value();
95  else
96  m_currentState = nullptr;
97 
98  if (m_currentState != old)
99  {
101  {
104 
105  if (old)
106  old->SetVisible(false);
107 
108  if (m_currentState)
109  m_currentState->SetVisible(true);
110  }
111  }
113 
114  return (m_currentState != nullptr);
115 }
116 
118 {
119  MythUIType *old = m_currentState;
120 
121  QMap<int, MythUIType *>::Iterator i = m_objectsByState.find((int)type);
122 
123  if (i != m_objectsByState.end())
124  m_currentState = i.value();
125  else
126  m_currentState = nullptr;
127 
128  if (m_currentState != old)
129  {
131  {
134 
135  if (old)
136  old->SetVisible(false);
137 
138  if (m_currentState)
139  m_currentState->SetVisible(true);
140  }
141  }
143 
144  return (m_currentState != nullptr);
145 }
146 
148 {
149  QString lcname = name.toLower();
150 
151  if (m_objectsByName.contains(lcname))
152  return m_objectsByName[lcname];
153 
154  return nullptr;
155 }
156 
158 {
159  if (m_objectsByState.contains(state))
160  return m_objectsByState[state];
161 
162  return nullptr;
163 }
164 
169 {
170  if (m_objectsByName.isEmpty() && m_objectsByState.isEmpty())
171  return;
172 
173  QMap<QString, MythUIType *>::Iterator i;
174 
175  for (i = m_objectsByName.begin(); i != m_objectsByName.end(); ++i)
176  {
177  DeleteChild(i.value());
178  }
179 
180  QMap<int, MythUIType *>::Iterator j;
181 
182  for (j = m_objectsByState.begin(); j != m_objectsByState.end(); ++j)
183  {
184  DeleteChild(j.value());
185  }
186 
187  m_objectsByName.clear();
188  m_objectsByState.clear();
189 
190  m_currentState = nullptr;
191  SetRedraw();
192 }
193 
198 {
199  if (!DisplayState("default") && !DisplayState("active"))
200  {
201  if (!DisplayState(None))
202  {
203  if (m_currentState)
204  m_currentState->SetVisible(false);
205 
206  m_currentState = nullptr;
207  }
208  }
209 
211 }
212 
214  const QString &filename, QDomElement &element, bool showWarnings)
215 {
216  QMap<QString, QString> dependsMap;
217  if (element.tagName() == "showempty")
218  m_showEmpty = parseBool(element);
219  else if (element.tagName() == "state")
220  {
221  QString name = element.attribute("name", "");
222  QString type = element.attribute("type", "");
223 
224  QString statename;
225 
226  if (!type.isEmpty())
227  statename = type;
228  else
229  statename = name;
230 
231  element.setAttribute("name", statename);
232 
233  MythUIGroup *uitype = dynamic_cast<MythUIGroup *>
234  (ParseUIType(filename, element, "group", this, nullptr, showWarnings, dependsMap));
235 
236  if (!type.isEmpty())
237  {
238  StateType stype = None;
239 
240  if (type == "off")
241  stype = Off;
242  else if (type == "half")
243  stype = Half;
244  else if (type == "full")
245  stype = Full;
246 
247  if (uitype && !m_objectsByState.contains((int)stype))
248  AddObject(stype, uitype);
249  }
250  else if (!name.isEmpty())
251  {
252  if (uitype && !m_objectsByName.contains(name))
253  AddObject(name, uitype);
254  }
255  }
256  else
257  {
258  return MythUIType::ParseElement(filename, element, showWarnings);
259  }
260 
261  return true;
262 }
263 
265 {
266  auto *st = dynamic_cast<MythUIStateType *>(base);
267 
268  if (!st)
269  return;
270 
271  m_showEmpty = st->m_showEmpty;
272 
273  MythUIType::CopyFrom(base);
274 
275  QMap<QString, MythUIType *>::iterator i;
276 
277  for (i = st->m_objectsByName.begin(); i != st->m_objectsByName.end(); ++i)
278  {
279  MythUIType *other = i.value();
280  const QString& key = i.key();
281 
282  MythUIType *newtype = GetChild(other->objectName());
283  AddObject(key, newtype);
284  newtype->SetVisible(false);
285  }
286 
287  QMap<int, MythUIType *>::iterator j;
288 
289  for (j = st->m_objectsByState.begin(); j != st->m_objectsByState.end(); ++j)
290  {
291  MythUIType *other = j.value();
292  int key = j.key();
293 
294  MythUIType *newtype = GetChild(other->objectName());
295  AddObject((StateType)key, newtype);
296  newtype->SetVisible(false);
297  }
298 }
299 
301 {
302  auto *st = new MythUIStateType(parent, objectName());
303  st->CopyFrom(this);
304 }
305 
307 {
308  if (!DisplayState("default"))
310 }
311 
312 void MythUIStateType::EnsureStateLoaded(const QString &name)
313 {
314  if (name.isEmpty())
315  return;
316 
317  QMap<QString, MythUIType *>::Iterator i = m_objectsByName.find(name);
318 
319  if (i != m_objectsByName.end())
320  i.value()->LoadNow();
321 }
322 
324 {
325  QMap<int, MythUIType *>::Iterator i = m_objectsByState.find((int)type);
326 
327  if (i != m_objectsByState.end())
328  i.value()->LoadNow();
329 }
330 
332 {
333  if (!m_deferload)
335 }
336 
338 {
339  if (m_parent)
340  {
341  if (objectName().startsWith("buttonlist button"))
342  {
343  auto *list = dynamic_cast<MythUIButtonList *>(m_parent);
344  m_parentArea = list->GetButtonArea();
345  }
346  else
348  }
349  else
351 
352  m_area.Reset();
354 
355  if (recurse)
356  {
357  for (auto * child : qAsConst(m_childrenList))
358  child->RecalculateArea(recurse);
359  }
360 }
361 
363 {
364  if (m_currentState == nullptr || !m_currentState->IsVisible())
365  {
366  emit DependChanged(true);
367  return;
368  }
369  QList<MythUIType *> *children = m_currentState->GetAllChildren();
370  for (auto *child : qAsConst(*children))
371  {
372  if (child->IsVisible())
373  {
374  emit DependChanged(false);
375  return;
376  }
377  }
378  emit DependChanged(true);
379 }
380 
382 {
383  if (m_objectsByName.isEmpty() && m_objectsByState.isEmpty())
384  return;
385 
386  QMap<QString, MythUIType *>::Iterator i;
387 
388  for (i = m_objectsByName.begin(); i != m_objectsByName.end(); ++i)
389  {
390  MythUIType *type = i.value();
391 
392  auto *textType = dynamic_cast<MythUIText *> (type);
393  if (textType)
394  textType->SetTextFromMap(infoMap);
395 
396  auto *group = dynamic_cast<MythUIComposite *> (type);
397  if (group)
398  group->SetTextFromMap(infoMap);
399  }
400 
401  QMap<int, MythUIType *>::Iterator j;
402 
403  for (j = m_objectsByState.begin(); j != m_objectsByState.end(); ++j)
404  {
405  MythUIType *type = j.value();
406 
407  auto *textType = dynamic_cast<MythUIText *> (type);
408  if (textType)
409  textType->SetTextFromMap(infoMap);
410 
411  auto *group = dynamic_cast<MythUIComposite *> (type);
412  if (group)
413  group->SetTextFromMap(infoMap);
414  }
415 }
MythUIType::m_area
MythRect m_area
Definition: mythuitype.h:252
MythUIStateType::m_objectsByName
QMap< QString, MythUIType * > m_objectsByName
Definition: mythuistatetype.h:64
mythuitext.h
MythUIStateType::EnsureStateLoaded
void EnsureStateLoaded(const QString &name)
Definition: mythuistatetype.cpp:312
MythRect::CalculateArea
void CalculateArea(const QRect &parentArea)
Definition: mythrect.cpp:64
MythUIImage
Image widget, displays a single image or multiple images in sequence.
Definition: mythuiimage.h:98
MythUIType::DeleteChild
void DeleteChild(const QString &name)
Delete a named child of this UIType.
Definition: mythuitype.cpp:146
MythUIScreenBounds::GetUIScreenRect
QRect GetUIScreenRect()
Definition: mythuiscreenbounds.cpp:192
MythUIStateType::Clear
void Clear(void)
Definition: mythuistatetype.cpp:168
MythUIType::GetFullArea
virtual MythRect GetFullArea(void) const
Definition: mythuitype.cpp:878
MythUIStateType::GetState
MythUIType * GetState(const QString &name)
Definition: mythuistatetype.cpp:147
MythUIStateType::SetTextFromMap
void SetTextFromMap(const InfoMap &infoMap) override
Definition: mythuistatetype.cpp:381
MythUIType::GetChild
MythUIType * GetChild(const QString &name) const
Get a named child of this UIType.
Definition: mythuitype.cpp:131
MythRect::Reset
void Reset(void)
Definition: mythrect.cpp:59
MythUIStateType::RecalculateArea
void RecalculateArea(bool recurse=true) override
Definition: mythuistatetype.cpp:337
MythUIGroup
Create a group of widgets.
Definition: mythuigroup.h:12
mythuistatetype.h
MythUIType::GetAllChildren
QList< MythUIType * > * GetAllChildren(void)
Return a list of all child widgets.
Definition: mythuitype.cpp:195
MythUIStateType::Reset
void Reset(void) override
Reset the widget to it's original state, should not reset changes made by the theme.
Definition: mythuistatetype.cpp:197
MythUIStateType::CopyFrom
void CopyFrom(MythUIType *base) override
Copy this widgets state from another.
Definition: mythuistatetype.cpp:264
MythRect
Wrapper around QRect allowing us to handle percentage and other relative values for areas in mythui.
Definition: mythrect.h:18
mythuibuttonlist.h
mythuiimage.h
MythUIStateType::Finalize
void Finalize(void) override
Perform any post-xml parsing initialisation tasks.
Definition: mythuistatetype.cpp:306
InfoMap
QHash< QString, QString > InfoMap
Definition: mythtypes.h:15
MythUIStateType::CreateCopy
void CreateCopy(MythUIType *parent) override
Copy the state of this widget to the one given, it must be of the same type.
Definition: mythuistatetype.cpp:300
MythUIStateType::m_showEmpty
bool m_showEmpty
Definition: mythuistatetype.h:70
MythUIType::DependChanged
void DependChanged(bool isDefault)
MythUIStateType::LoadNow
void LoadNow(void) override
Cause images in this and child widgets to be loaded.
Definition: mythuistatetype.cpp:331
MythUIStateType::Half
@ Half
Definition: mythuistatetype.h:25
MythUIComposite::SetTextFromMap
virtual void SetTextFromMap(const InfoMap &infoMap)
Definition: mythuicomposite.cpp:9
MythUIType::m_parent
MythUIType * m_parent
Definition: mythuitype.h:272
MythUIType::m_childrenList
QList< MythUIType * > m_childrenList
Definition: mythuitype.h:231
filename
QString filename
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:637
MythUIStateType::AdjustDependence
virtual void AdjustDependence(void)
Definition: mythuistatetype.cpp:362
MythUIType::CopyFrom
virtual void CopyFrom(MythUIType *base)
Copy this widgets state from another.
Definition: mythuitype.cpp:1152
mythpainter.h
MythUIStateType::Full
@ Full
Definition: mythuistatetype.h:25
MythUIType::m_deferload
bool m_deferload
Definition: mythuitype.h:281
MythUIStateType::AddImage
bool AddImage(const QString &name, MythImage *image)
Definition: mythuistatetype.cpp:22
MythUIType::Reset
virtual void Reset(void)
Reset the widget to it's original state, should not reset changes made by the theme.
Definition: mythuitype.cpp:70
mythuigroup.h
MythUIType
The base class on which all widgets and screens are based.
Definition: mythuitype.h:66
MythUIStateType::None
@ None
Definition: mythuistatetype.h:25
MythUIStateType::Off
@ Off
Definition: mythuistatetype.h:25
mythimage.h
MythUIStateType::m_objectsByState
QMap< int, MythUIType * > m_objectsByState
Definition: mythuistatetype.h:65
MythUIText
All purpose text widget, displays a text string.
Definition: mythuitext.h:29
XMLParseBase::ParseUIType
static MythUIType * ParseUIType(const QString &filename, QDomElement &element, const QString &type, MythUIType *parent, MythScreenType *screen, bool showWarnings, QMap< QString, QString > &parentDependsMap)
Definition: xmlparsebase.cpp:435
MythUIStateType::StateType
StateType
Definition: mythuistatetype.h:25
MythUIType::LoadNow
virtual void LoadNow(void)
Cause images in this and child widgets to be loaded.
Definition: mythuitype.cpp:1381
MythImage
Definition: mythimage.h:37
MythUIStateType::m_currentState
MythUIType * m_currentState
Definition: mythuistatetype.h:67
MythUIType::ExpandArea
void ExpandArea(const QRect &rect)
Definition: mythuitype.cpp:854
MythUIText::SetTextFromMap
void SetTextFromMap(const InfoMap &map)
Definition: mythuitext.cpp:157
MythUIType::SetVisible
virtual void SetVisible(bool visible)
Definition: mythuitype.cpp:1086
MythUIStateType::MythUIStateType
MythUIStateType(MythUIType *parent, const QString &name)
Definition: mythuistatetype.cpp:16
GetMythMainWindow
MythMainWindow * GetMythMainWindow(void)
Definition: mythmainwindow.cpp:105
MythUIStateType::ParseElement
bool ParseElement(const QString &filename, QDomElement &element, bool showWarnings) override
Parse the xml definition of this widget setting the state of the object accordingly.
Definition: mythuistatetype.cpp:213
MythUIType::ParseElement
virtual bool ParseElement(const QString &filename, QDomElement &element, bool showWarnings)
Parse the xml definition of this widget setting the state of the object accordingly.
Definition: mythuitype.cpp:1218
MythUIButtonList
List widget, displays list items in a variety of themeable arrangements and can trigger signals when ...
Definition: mythuibuttonlist.h:144
mythmainwindow.h
MythUIType::SetRedraw
void SetRedraw(void)
Definition: mythuitype.cpp:294
XMLParseBase::parseBool
static bool parseBool(const QString &text)
Definition: xmlparsebase.cpp:65
MythUIType::IsVisible
bool IsVisible(bool recurse=false) const
Definition: mythuitype.cpp:888
MythUIStateType::m_parentArea
MythRect m_parentArea
Definition: mythuistatetype.h:68
MythUIStateType
This widget is used for grouping other widgets for display when a particular named state is called.
Definition: mythuistatetype.h:23
MythUIStateType::AddObject
bool AddObject(const QString &name, MythUIType *object)
Definition: mythuistatetype.cpp:37
MythUIStateType::DisplayState
bool DisplayState(const QString &name)
Definition: mythuistatetype.cpp:84
MythUIComposite
Definition: mythuicomposite.h:8