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
11 #include "mythlogging.h"
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 using namespace std::chrono_literals;
21 
22 MythUIButton::MythUIButton(MythUIType *parent, const QString &name)
23  : MythUIType(parent, name)
24 {
25  m_clickTimer = new QTimer();
26  m_clickTimer->setSingleShot(true);
27 
29 
30  connect(this, &MythUIType::TakingFocus, this, &MythUIButton::Select);
31  connect(this, &MythUIType::LosingFocus, this, &MythUIButton::Deselect);
32  connect(this, &MythUIType::Enabling, this, &MythUIButton::Enable);
33  connect(this, &MythUIType::Disabling, this, &MythUIButton::Disable);
34 
35  SetCanTakeFocus(true);
36 }
37 
39 {
40  if (m_clickTimer)
41  m_clickTimer->deleteLater();
42 }
43 
45 {
46  m_backgroundState = dynamic_cast<MythUIStateType *>(GetChild("buttonstate"));
47 
48  if (!m_backgroundState)
49  LOG(VB_GENERAL, LOG_ERR, QString("Button %1 is missing required "
50  "elements").arg(objectName()));
51 
52  SetState("active");
53 
54  if (m_text && m_message.isEmpty())
56 }
57 
62 {
64 }
65 
67 {
68  if (!IsEnabled() || m_pushed)
69  return;
70 
71  SetState("selected");
72 }
73 
75 {
76  if (m_pushed)
77  return;
78 
79  if (IsEnabled())
80  SetState("active");
81  else
82  SetState("disabled");
83 }
84 
86 {
87  SetState("active");
88 }
89 
91 {
92  SetState("disabled");
93 }
94 
95 void MythUIButton::SetState(const QString& state)
96 {
97  if (m_state == state)
98  return;
99 
100  if (m_pushed && state != "pushed")
101  UnPush();
102 
103  m_state = state;
104 
105  if (!m_backgroundState)
106  return;
107 
109 
110  auto *activeState = dynamic_cast<MythUIGroup *>
112 
113  if (activeState)
114  m_text = dynamic_cast<MythUIText *>(activeState->GetChild("text"));
115 
116  if (m_text)
117  {
120  }
121 }
122 
126 bool MythUIButton::keyPressEvent(QKeyEvent *event)
127 {
128  QStringList actions;
129  bool handled = false;
130  handled = GetMythMainWindow()->TranslateKeyPress("Global", event, actions);
131 
132  for (int i = 0; i < actions.size() && !handled; i++)
133  {
134  QString action = actions[i];
135  handled = true;
136 
137  if (action == "SELECT")
138  {
139  if (IsEnabled())
140  {
141  if (m_pushed)
142  UnPush();
143  else
144  Push();
145  }
146  }
147  else
148  handled = false;
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:220
MythUIButton::SetLocked
void SetLocked(bool locked)
Definition: mythuibutton.cpp:206
hardwareprofile.smolt.timeout
float timeout
Definition: smolt.py:103
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:131
MythUIStateType::GetCurrentState
MythUIType * GetCurrentState()
Definition: mythuistatetype.h:41
arg
arg(title).arg(filename).arg(doDelete))
MythUIType::SetCanTakeFocus
void SetCanTakeFocus(bool set=true)
Set whether this widget can take focus.
Definition: mythuitype.cpp:343
MythUIGroup
Create a group of widgets.
Definition: mythuigroup.h:11
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
mythuistatetype.h
MythUIButton::Deselect
void Deselect()
Definition: mythuibutton.cpp:74
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:126
XMLParseBase::parseText
static QString parseText(QDomElement &element)
Definition: xmlparsebase.cpp:324
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:85
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:259
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:1114
MythUIType::IsEnabled
bool IsEnabled(void) const
Definition: mythuitype.h:115
MythUIType::Disabling
void Disabling()
MythUIButton::SetText
void SetText(const QString &msg)
Definition: mythuibutton.cpp:229
MythUIText::GetDefaultText
QString GetDefaultText(void) const
Definition: mythuitext.h:46
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
filename
QString filename
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:637
MythGestureEvent::Click
@ Click
Definition: mythgesture.h:77
MythUIType::CopyFrom
virtual void CopyFrom(MythUIType *base)
Copy this widgets state from another.
Definition: mythuitype.cpp:1152
MythUIButton::~MythUIButton
~MythUIButton() override
Definition: mythuibutton.cpp:38
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:70
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:84
MythUIButton::Select
void Select()
Definition: mythuibutton.cpp:66
MythUIButton::m_state
QString m_state
Definition: mythuibutton.h:68
MythUIText
All purpose text widget, displays a text string.
Definition: mythuitext.h:30
MythUIText::SetText
virtual void SetText(const QString &text)
Definition: mythuitext.cpp:133
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:261
MythUIButton::m_clickTimer
class QTimer * m_clickTimer
Definition: mythuibutton.h:72
MythUIButton::MythUIButton
MythUIButton(MythUIType *parent, const QString &name)
Definition: mythuibutton.cpp:22
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:90
MythUIButton::SetState
void SetState(const QString &state)
Definition: mythuibutton.cpp:95
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:1218
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:61
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:44
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