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
16MythUIStateType::MythUIStateType(MythUIType *parent, const QString &name)
17 : MythUIComposite(parent, name)
18{
19 emit DependChanged(false);
20}
21
22bool 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
37bool 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
84bool 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)
110 }
111 }
113
114 return (m_currentState != nullptr);
115}
116
118{
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)
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)
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
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
312void 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 if (auto * list = dynamic_cast<MythUIButtonList *>(m_parent); list)
344 m_parentArea = list->GetButtonArea();
345 }
346 else
347 {
349 }
350 }
351 else
352 {
354 }
355
356 m_area.Reset();
358
359 if (recurse)
360 {
361 for (auto * child : std::as_const(m_childrenList))
362 child->RecalculateArea(recurse);
363 }
364}
365
367{
368 if (m_currentState == nullptr || !m_currentState->IsVisible())
369 {
370 emit DependChanged(true);
371 return;
372 }
373 QList<MythUIType *> *children = m_currentState->GetAllChildren();
374 for (auto *child : std::as_const(*children))
375 {
376 if (child->IsVisible())
377 {
378 emit DependChanged(false);
379 return;
380 }
381 }
382 emit DependChanged(true);
383}
384
386{
387 if (m_objectsByName.isEmpty() && m_objectsByState.isEmpty())
388 return;
389
390 QMap<QString, MythUIType *>::Iterator i;
391
392 for (i = m_objectsByName.begin(); i != m_objectsByName.end(); ++i)
393 {
394 MythUIType *type = i.value();
395
396 auto *textType = dynamic_cast<MythUIText *> (type);
397 if (textType)
398 textType->SetTextFromMap(infoMap);
399
400 auto *group = dynamic_cast<MythUIComposite *> (type);
401 if (group)
402 group->SetTextFromMap(infoMap);
403 }
404
405 QMap<int, MythUIType *>::Iterator j;
406
407 for (j = m_objectsByState.begin(); j != m_objectsByState.end(); ++j)
408 {
409 MythUIType *type = j.value();
410
411 auto *textType = dynamic_cast<MythUIText *> (type);
412 if (textType)
413 textType->SetTextFromMap(infoMap);
414
415 auto *group = dynamic_cast<MythUIComposite *> (type);
416 if (group)
417 group->SetTextFromMap(infoMap);
418 }
419}
Wrapper around QRect allowing us to handle percentage and other relative values for areas in mythui.
Definition: mythrect.h:18
void Reset(void)
Definition: mythrect.cpp:59
void CalculateArea(QRect parentArea)
Definition: mythrect.cpp:64
List widget, displays list items in a variety of themeable arrangements and can trigger signals when ...
virtual void SetTextFromMap(const InfoMap &infoMap)
Create a group of widgets.
Definition: mythuigroup.h:12
Image widget, displays a single image or multiple images in sequence.
Definition: mythuiimage.h:98
This widget is used for grouping other widgets for display when a particular named state is called.
void SetTextFromMap(const InfoMap &infoMap) override
void Finalize(void) override
Perform any post-xml parsing initialisation tasks.
bool ParseElement(const QString &filename, QDomElement &element, bool showWarnings) override
Parse the xml definition of this widget setting the state of the object accordingly.
void LoadNow(void) override
Cause images in this and child widgets to be loaded.
MythUIStateType(MythUIType *parent, const QString &name)
void RecalculateArea(bool recurse=true) override
void EnsureStateLoaded(const QString &name)
MythUIType * GetState(const QString &name)
MythUIType * m_currentState
void Reset(void) override
Reset the widget to it's original state, should not reset changes made by the theme.
MythRect m_parentArea
bool AddImage(const QString &name, MythImage *image)
QMap< QString, MythUIType * > m_objectsByName
bool DisplayState(const QString &name)
QMap< int, MythUIType * > m_objectsByState
void CopyFrom(MythUIType *base) override
Copy this widgets state from another.
virtual void AdjustDependence(void)
void CreateCopy(MythUIType *parent) override
Copy the state of this widget to the one given, it must be of the same type.
bool AddObject(const QString &name, MythUIType *object)
All purpose text widget, displays a text string.
Definition: mythuitext.h:29
void SetTextFromMap(const InfoMap &map)
Definition: mythuitext.cpp:138
The base class on which all widgets and screens are based.
Definition: mythuitype.h:86
QList< MythUIType * > * GetAllChildren(void)
Return a list of all child widgets.
Definition: mythuitype.cpp:202
bool IsVisible(bool recurse=false) const
Definition: mythuitype.cpp:903
virtual void SetVisible(bool visible)
virtual void LoadNow(void)
Cause images in this and child widgets to be loaded.
void ExpandArea(QRect rect)
Definition: mythuitype.cpp:869
virtual void CopyFrom(MythUIType *base)
Copy this widgets state from another.
void SetRedraw(void)
Definition: mythuitype.cpp:313
MythUIType * m_parent
Definition: mythuitype.h:297
virtual MythRect GetFullArea(void) const
Definition: mythuitype.cpp:893
MythUIType * GetChild(const QString &name) const
Get a named child of this UIType.
Definition: mythuitype.cpp:138
void DeleteChild(const QString &name)
Delete a named child of this UIType.
Definition: mythuitype.cpp:153
void DependChanged(bool isDefault)
virtual void Reset(void)
Reset the widget to it's original state, should not reset changes made by the theme.
Definition: mythuitype.cpp:73
virtual bool ParseElement(const QString &filename, QDomElement &element, bool showWarnings)
Parse the xml definition of this widget setting the state of the object accordingly.
QList< MythUIType * > m_childrenList
Definition: mythuitype.h:255
MythRect m_area
Definition: mythuitype.h:277
bool m_deferload
Definition: mythuitype.h:306
static MythUIType * ParseUIType(const QString &filename, QDomElement &element, const QString &type, MythUIType *parent, MythScreenType *screen, bool showWarnings, QMap< QString, QString > &parentDependsMap)
static bool parseBool(const QString &text)
MythMainWindow * GetMythMainWindow(void)
QHash< QString, QString > InfoMap
Definition: mythtypes.h:15