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 
20 MythUIButton::MythUIButton(MythUIType *parent, const QString &name)
21  : MythUIType(parent, name)
22 {
23  m_clickTimer = new QTimer();
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 
46  if (!m_backgroundState)
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 
93 void 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 
103  if (!m_backgroundState)
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 
124 bool 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  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  handled = false;
147  }
148 
149  return handled;
150 }
151 
156 {
157  if (event->GetGesture() == MythGestureEvent::Click)
158  {
159  if (IsEnabled())
160  {
161  if (m_pushed)
162  UnPush();
163  else
164  Push();
165 
166  return true;
167  }
168  }
169 
170  return false;
171 }
172 
173 void MythUIButton::Push(bool lock)
174 {
175  m_pushed = true;
176  SetState("pushed");
177 
178  if (!lock && !m_lockable)
179  m_clickTimer->start(500ms);
180 
181  emit Clicked();
182 }
183 
185 {
186  if (!m_pushed)
187  return;
188 
189  m_clickTimer->stop();
190 
191  m_pushed = false;
192 
193  if (m_hasFocus)
194  SetState("selected");
195  else if (m_enabled)
196  SetState("active");
197  else
198  SetState("disabled");
199 
200  if (m_lockable)
201  emit Clicked();
202 }
203 
204 void MythUIButton::SetLocked(bool locked)
205 {
206  if (!m_lockable)
207  return;
208 
209  if (locked)
210  {
211  m_pushed = true;
212  SetState("pushed");
213  }
214  else
215  {
216  m_pushed = false;
217 
218  if (m_hasFocus)
219  SetState("selected");
220  else if (m_enabled)
221  SetState("active");
222  else
223  SetState("disabled");
224  }
225 }
226 
227 void MythUIButton::SetText(const QString &msg)
228 {
229  if (m_message == msg)
230  return;
231 
232  m_message = msg;
233 
234  auto *activeState = dynamic_cast<MythUIGroup *>
236 
237  if (activeState)
238  m_text = dynamic_cast<MythUIText *>(activeState->GetChild("text"));
239 
240  if (m_text)
241  m_text->SetText(msg);
242 }
243 
244 QString MythUIButton::GetText() const
245 {
246  return m_message;
247 }
248 
250 {
251  return m_text->GetDefaultText();
252 }
253 
258  const QString &filename, QDomElement &element, bool showWarnings)
259 {
260  if (element.tagName() == "value")
261  {
262  m_valueText = QCoreApplication::translate("ThemeUI",
263  parseText(element).toUtf8());
264  }
265  else
266  {
267  return MythUIType::ParseElement(filename, element, showWarnings);
268  }
269 
270  return true;
271 }
272 
277 {
278  auto *button = new MythUIButton(parent, objectName());
279  button->CopyFrom(this);
280 }
281 
286 {
287  auto *button = dynamic_cast<MythUIButton *>(base);
288  if (!button)
289  {
290  LOG(VB_GENERAL, LOG_ERR, "Dynamic cast of base failed");
291  return;
292  }
293 
294  m_message = button->m_message;
295  m_valueText = button->m_valueText;
296  m_lockable = button->m_lockable;
297 
298  MythUIType::CopyFrom(base);
299 
301 }
302 
307 {
310 }
MythUIButton::Clicked
void Clicked()
MythUIText::SetFontState
void SetFontState(const QString &state)
Definition: mythuitext.cpp:219
MythUIButton::SetLocked
void SetLocked(bool locked)
Definition: mythuibutton.cpp:204
hardwareprofile.smolt.timeout
float timeout
Definition: smolt.py:102
mythuitext.h
MythUIButton::m_lockable
bool m_lockable
Definition: mythuibutton.h:71
MythGestureEvent::GetGesture
Gesture GetGesture() const
Definition: mythgesture.h:85
MythUIType::Enabling
void Enabling()
MythUIButton::m_text
MythUIText * m_text
Definition: mythuibutton.h:66
MythUIType::GetChild
MythUIType * GetChild(const QString &name) const
Get a named child of this UIType.
Definition: mythuitype.cpp:135
MythUIStateType::GetCurrentState
MythUIType * GetCurrentState()
Definition: mythuistatetype.h:41
MythUIType::SetCanTakeFocus
void SetCanTakeFocus(bool set=true)
Set whether this widget can take focus.
Definition: mythuitype.cpp:359
MythUIGroup
Create a group of widgets.
Definition: mythuigroup.h:11
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
mythuistatetype.h
MythUIButton::Deselect
void Deselect()
Definition: mythuibutton.cpp:72
MythUIButton::GetText
QString GetText(void) const
Definition: mythuibutton.cpp:244
MythUIButton::UnPush
void UnPush()
Definition: mythuibutton.cpp:184
MythUIButton::keyPressEvent
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
Definition: mythuibutton.cpp:124
XMLParseBase::parseText
static QString parseText(QDomElement &element)
Definition: xmlparsebase.cpp:315
MythUIButton::CreateCopy
void CreateCopy(MythUIType *parent) override
Copy the state of this widget to the one given, it must be of the same type.
Definition: mythuibutton.cpp:276
MythUIType::TakingFocus
void TakingFocus()
MythUIButton::Enable
void Enable()
Definition: mythuibutton.cpp:83
MythUIButton::gestureEvent
bool gestureEvent(MythGestureEvent *event) override
Mouse click/movement handler, receives mouse gesture events from the QCoreApplication event loop.
Definition: mythuibutton.cpp:155
MythUIType::m_hasFocus
bool m_hasFocus
Definition: mythuitype.h:262
mythlogging.h
MythUIButton::m_message
QString m_message
Definition: mythuibutton.h:62
MythMainWindow::TranslateKeyPress
bool TranslateKeyPress(const QString &Context, QKeyEvent *Event, QStringList &Actions, bool AllowJumps=true)
Get a list of actions for a keypress in the given context.
Definition: mythmainwindow.cpp:1112
MythUIType::IsEnabled
bool IsEnabled(void) const
Definition: mythuitype.h:117
MythUIType::Disabling
void Disabling()
MythUIButton::SetText
void SetText(const QString &msg)
Definition: mythuibutton.cpp:227
MythUIText::GetDefaultText
QString GetDefaultText(void) const
Definition: mythuitext.h:44
MythUIButton
A single button widget.
Definition: mythuibutton.h:21
MythUIButton::m_backgroundState
MythUIStateType * m_backgroundState
Definition: mythuibutton.h:65
MythUIButton::Push
void Push(bool lock=false)
Definition: mythuibutton.cpp:173
MythGestureEvent::Click
@ Click
Definition: mythgesture.h:77
MythUIType::CopyFrom
virtual void CopyFrom(MythUIType *base)
Copy this widgets state from another.
Definition: mythuitype.cpp:1176
MythUIButton::~MythUIButton
~MythUIButton() override
Definition: mythuibutton.cpp:36
mythgesture.h
A C++ ripoff of the stroke library for MythTV.
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:72
MythUIButton::m_valueText
QString m_valueText
Definition: mythuibutton.h:63
mythuigroup.h
MythUIType
The base class on which all widgets and screens are based.
Definition: mythuitype.h:85
MythUIButton::Select
void Select()
Definition: mythuibutton.cpp:64
MythUIButton::m_state
QString m_state
Definition: mythuibutton.h:68
MythUIText
All purpose text widget, displays a text string.
Definition: mythuitext.h:28
MythUIText::SetText
virtual void SetText(const QString &text)
Definition: mythuitext.cpp:132
MythUIButton::Finalize
void Finalize(void) override
Perform any post-xml parsing initialisation tasks.
Definition: mythuibutton.cpp:306
MythUIButton::CopyFrom
void CopyFrom(MythUIType *base) override
Copy this widgets state from another.
Definition: mythuibutton.cpp:285
GetMythMainWindow
MythMainWindow * GetMythMainWindow(void)
Definition: mythmainwindow.cpp:104
MythUIType::m_enabled
bool m_enabled
Definition: mythuitype.h:264
MythUIButton::m_clickTimer
class QTimer * m_clickTimer
Definition: mythuibutton.h:72
MythUIButton::MythUIButton
MythUIButton(MythUIType *parent, const QString &name)
Definition: mythuibutton.cpp:20
build_compdb.action
action
Definition: build_compdb.py:9
mythuibutton.h
MythGestureEvent
A custom event that represents a mouse gesture.
Definition: mythgesture.h:39
MythUIButton::Disable
void Disable()
Definition: mythuibutton.cpp:88
MythUIButton::SetState
void SetState(const QString &state)
Definition: mythuibutton.cpp:93
MythUIType::LosingFocus
void LosingFocus()
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:1242
build_compdb.filename
filename
Definition: build_compdb.py:21
mythmainwindow.h
MythUIButton::GetDefaultText
QString GetDefaultText(void) const
Definition: mythuibutton.cpp:249
MythUIButton::m_pushed
bool m_pushed
Definition: mythuibutton.h:70
MythUIButton::Reset
void Reset(void) override
Reset the widget to it's original state, should not reset changes made by the theme.
Definition: mythuibutton.cpp:59
MythUIButton::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: mythuibutton.cpp:257
MythUIButton::SetInitialStates
void SetInitialStates(void)
Definition: mythuibutton.cpp:42
MythUIStateType
This widget is used for grouping other widgets for display when a particular named state is called....
Definition: mythuistatetype.h:22
MythUIStateType::DisplayState
bool DisplayState(const QString &name)
Definition: mythuistatetype.cpp:84