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  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 
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  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 
175 void 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 
206 void 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 
229 void 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 
246 QString MythUIButton::GetText() const
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 
300  MythUIType::CopyFrom(base);
301 
303 }
304 
309 {
312 }
MythUIButton::Clicked
void Clicked()
MythUIText::SetFontState
void SetFontState(const QString &state)
Definition: mythuitext.cpp:202
MythUIButton::SetLocked
void SetLocked(bool locked)
Definition: mythuibutton.cpp:206
MythGestureEvent::Click
@ Click
Definition: mythgesture.h:77
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:138
MythUIStateType::GetCurrentState
MythUIType * GetCurrentState()
Definition: mythuistatetype.h:43
MythUIType::SetCanTakeFocus
void SetCanTakeFocus(bool set=true)
Set whether this widget can take focus.
Definition: mythuitype.cpp:362
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:246
MythUIButton::UnPush
void UnPush()
Definition: mythuibutton.cpp:186
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:278
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:157
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:1111
MythUIType::IsEnabled
bool IsEnabled(void) const
Definition: mythuitype.h:117
MythUIType::Disabling
void Disabling()
MythUIButton::SetText
void SetText(const QString &msg)
Definition: mythuibutton.cpp:229
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:175
MythUIType::CopyFrom
virtual void CopyFrom(MythUIType *base)
Copy this widgets state from another.
Definition: mythuitype.cpp:1171
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:73
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:115
MythUIButton::Finalize
void Finalize(void) override
Perform any post-xml parsing initialisation tasks.
Definition: mythuibutton.cpp:308
MythUIButton::CopyFrom
void CopyFrom(MythUIType *base) override
Copy this widgets state from another.
Definition: mythuibutton.cpp:287
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:1237
build_compdb.filename
filename
Definition: build_compdb.py:21
mythmainwindow.h
MythUIButton::GetDefaultText
QString GetDefaultText(void) const
Definition: mythuibutton.cpp:251
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:259
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