MythTV master
mythuibutton.cpp
Go to the documentation of this file.
1#include <chrono>
2
3#include "mythuibutton.h"
4
5// QT
6#include <QTimer>
7#include <QDomDocument>
8#include <QCoreApplication>
9
10// Myth headers
12
13// MythUI headers
14#include "mythgesture.h"
15#include "mythmainwindow.h"
16#include "mythuigroup.h"
17#include "mythuistatetype.h"
18#include "mythuitext.h"
19
20MythUIButton::MythUIButton(MythUIType *parent, const QString &name)
21 : MythUIType(parent, name),
22 m_clickTimer(new QTimer())
23{
24 m_clickTimer->setSingleShot(true);
25
27
28 connect(this, &MythUIType::TakingFocus, this, &MythUIButton::Select);
29 connect(this, &MythUIType::LosingFocus, this, &MythUIButton::Deselect);
30 connect(this, &MythUIType::Enabling, this, &MythUIButton::Enable);
31 connect(this, &MythUIType::Disabling, this, &MythUIButton::Disable);
32
33 SetCanTakeFocus(true);
34}
35
37{
38 if (m_clickTimer)
39 m_clickTimer->deleteLater();
40}
41
43{
44 m_backgroundState = dynamic_cast<MythUIStateType *>(GetChild("buttonstate"));
45
47 LOG(VB_GENERAL, LOG_ERR, QString("Button %1 is missing required "
48 "elements").arg(objectName()));
49
50 SetState("active");
51
52 if (m_text && m_message.isEmpty())
54}
55
60{
62}
63
65{
66 if (!IsEnabled() || m_pushed)
67 return;
68
69 SetState("selected");
70}
71
73{
74 if (m_pushed)
75 return;
76
77 if (IsEnabled())
78 SetState("active");
79 else
80 SetState("disabled");
81}
82
84{
85 SetState("active");
86}
87
89{
90 SetState("disabled");
91}
92
93void MythUIButton::SetState(const QString& state)
94{
95 if (m_state == state)
96 return;
97
98 if (m_pushed && state != "pushed")
99 UnPush();
100
101 m_state = state;
102
104 return;
105
107
108 auto *activeState = dynamic_cast<MythUIGroup *>
110
111 if (activeState)
112 m_text = dynamic_cast<MythUIText *>(activeState->GetChild("text"));
113
114 if (m_text)
115 {
118 }
119}
120
124bool MythUIButton::keyPressEvent(QKeyEvent *event)
125{
126 QStringList actions;
127 bool handled = false;
128 handled = GetMythMainWindow()->TranslateKeyPress("Global", event, actions);
129
130 for (int i = 0; i < actions.size() && !handled; i++)
131 {
132 const QString& action = actions[i];
133 handled = true;
134
135 if (action == "SELECT")
136 {
137 if (IsEnabled())
138 {
139 if (m_pushed)
140 UnPush();
141 else
142 Push();
143 }
144 }
145 else
146 {
147 handled = false;
148 }
149 }
150
151 return handled;
152}
153
158{
159 if (event->GetGesture() == MythGestureEvent::Click)
160 {
161 if (IsEnabled())
162 {
163 if (m_pushed)
164 UnPush();
165 else
166 Push();
167
168 return true;
169 }
170 }
171
172 return false;
173}
174
175void MythUIButton::Push(bool lock)
176{
177 m_pushed = true;
178 SetState("pushed");
179
180 if (!lock && !m_lockable)
181 m_clickTimer->start(500ms);
182
183 emit Clicked();
184}
185
187{
188 if (!m_pushed)
189 return;
190
191 m_clickTimer->stop();
192
193 m_pushed = false;
194
195 if (m_hasFocus)
196 SetState("selected");
197 else if (m_enabled)
198 SetState("active");
199 else
200 SetState("disabled");
201
202 if (m_lockable)
203 emit Clicked();
204}
205
206void MythUIButton::SetLocked(bool locked)
207{
208 if (!m_lockable)
209 return;
210
211 if (locked)
212 {
213 m_pushed = true;
214 SetState("pushed");
215 }
216 else
217 {
218 m_pushed = false;
219
220 if (m_hasFocus)
221 SetState("selected");
222 else if (m_enabled)
223 SetState("active");
224 else
225 SetState("disabled");
226 }
227}
228
229void MythUIButton::SetText(const QString &msg)
230{
231 if (m_message == msg)
232 return;
233
234 m_message = msg;
235
236 auto *activeState = dynamic_cast<MythUIGroup *>
238
239 if (activeState)
240 m_text = dynamic_cast<MythUIText *>(activeState->GetChild("text"));
241
242 if (m_text)
243 m_text->SetText(msg);
244}
245
247{
248 return m_message;
249}
250
252{
253 return m_text->GetDefaultText();
254}
255
260 const QString &filename, QDomElement &element, bool showWarnings)
261{
262 if (element.tagName() == "value")
263 {
264 m_valueText = QCoreApplication::translate("ThemeUI",
265 parseText(element).toUtf8());
266 }
267 else
268 {
269 return MythUIType::ParseElement(filename, element, showWarnings);
270 }
271
272 return true;
273}
274
279{
280 auto *button = new MythUIButton(parent, objectName());
281 button->CopyFrom(this);
282}
283
288{
289 auto *button = dynamic_cast<MythUIButton *>(base);
290 if (!button)
291 {
292 LOG(VB_GENERAL, LOG_ERR, "Dynamic cast of base failed");
293 return;
294 }
295
296 m_message = button->m_message;
297 m_valueText = button->m_valueText;
298 m_lockable = button->m_lockable;
299
301
303}
304
309{
312}
A custom event that represents a mouse gesture.
Definition: mythgesture.h:40
Gesture GetGesture() const
Definition: mythgesture.h:85
bool TranslateKeyPress(const QString &Context, QKeyEvent *Event, QStringList &Actions, bool AllowJumps=true)
Get a list of actions for a keypress in the given context.
A single button widget.
Definition: mythuibutton.h:22
void SetInitialStates(void)
MythUIStateType * m_backgroundState
Definition: mythuibutton.h:65
void SetText(const QString &msg)
void CopyFrom(MythUIType *base) override
Copy this widgets state from another.
void CreateCopy(MythUIType *parent) override
Copy the state of this widget to the one given, it must be of the same type.
bool ParseElement(const QString &filename, QDomElement &element, bool showWarnings) override
Parse the xml definition of this widget setting the state of the object accordingly.
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
void SetState(const QString &state)
MythUIButton(MythUIType *parent, const QString &name)
class QTimer * m_clickTimer
Definition: mythuibutton.h:72
QString m_message
Definition: mythuibutton.h:62
void SetLocked(bool locked)
void Push(bool lock=false)
~MythUIButton() override
void Reset(void) override
Reset the widget to it's original state, should not reset changes made by the theme.
void Finalize(void) override
Perform any post-xml parsing initialisation tasks.
QString GetDefaultText(void) const
MythUIText * m_text
Definition: mythuibutton.h:66
void Clicked()
bool gestureEvent(MythGestureEvent *event) override
Mouse click/movement handler, receives mouse gesture events from the QCoreApplication event loop.
QString m_state
Definition: mythuibutton.h:68
QString m_valueText
Definition: mythuibutton.h:63
QString GetText(void) const
Create a group of widgets.
Definition: mythuigroup.h:12
This widget is used for grouping other widgets for display when a particular named state is called.
MythUIType * GetCurrentState()
bool DisplayState(const QString &name)
All purpose text widget, displays a text string.
Definition: mythuitext.h:29
void SetFontState(const QString &state)
Definition: mythuitext.cpp:202
QString GetDefaultText(void) const
Definition: mythuitext.h:44
virtual void SetText(const QString &text)
Definition: mythuitext.cpp:115
The base class on which all widgets and screens are based.
Definition: mythuitype.h:86
bool IsEnabled(void) const
Definition: mythuitype.h:119
void SetCanTakeFocus(bool set=true)
Set whether this widget can take focus.
Definition: mythuitype.cpp:362
void TakingFocus(void)
void Disabling(void)
virtual void CopyFrom(MythUIType *base)
Copy this widgets state from another.
void Enabling(void)
bool m_enabled
Definition: mythuitype.h:266
bool m_hasFocus
Definition: mythuitype.h:264
MythUIType * GetChild(const QString &name) const
Get a named child of this UIType.
Definition: mythuitype.cpp:138
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.
void LosingFocus(void)
static QString parseText(QDomElement &element)
A C++ ripoff of the stroke library for MythTV.
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
MythMainWindow * GetMythMainWindow(void)