MythTV  master
mythuibutton.cpp
Go to the documentation of this file.
1 
2 #include "mythuibutton.h"
3 
4 // QT
5 #include <QTimer>
6 #include <QDomDocument>
7 #include <QCoreApplication>
8 
9 // Myth headers
10 #include "mythlogging.h"
11 
12 // MythUI headers
13 #include "mythmainwindow.h"
14 #include "mythuigroup.h"
15 #include "mythuistatetype.h"
16 #include "mythuitext.h"
17 #include "mythgesture.h"
18 
19 MythUIButton::MythUIButton(MythUIType *parent, const QString &name)
20  : MythUIType(parent, name)
21 {
22  m_clickTimer = new QTimer();
23  m_clickTimer->setSingleShot(true);
24 
25  connect(m_clickTimer, SIGNAL(timeout()), SLOT(UnPush()));
26 
27  connect(this, SIGNAL(TakingFocus()), SLOT(Select()));
28  connect(this, SIGNAL(LosingFocus()), SLOT(Deselect()));
29  connect(this, SIGNAL(Enabling()), SLOT(Enable()));
30  connect(this, SIGNAL(Disabling()), SLOT(Disable()));
31 
32  SetCanTakeFocus(true);
33 }
34 
36 {
37  if (m_clickTimer)
38  m_clickTimer->deleteLater();
39 }
40 
42 {
43  m_BackgroundState = dynamic_cast<MythUIStateType *>(GetChild("buttonstate"));
44 
45  if (!m_BackgroundState)
46  LOG(VB_GENERAL, LOG_ERR, QString("Button %1 is missing required "
47  "elements").arg(objectName()));
48 
49  SetState("active");
50 
51  if (m_Text && m_Message.isEmpty())
53 }
54 
59 {
61 }
62 
64 {
65  if (!IsEnabled() || m_Pushed)
66  return;
67 
68  SetState("selected");
69 }
70 
72 {
73  if (m_Pushed)
74  return;
75 
76  if (IsEnabled())
77  SetState("active");
78  else
79  SetState("disabled");
80 }
81 
83 {
84  SetState("active");
85 }
86 
88 {
89  SetState("disabled");
90 }
91 
92 void MythUIButton::SetState(const QString& state)
93 {
94  if (m_state == state)
95  return;
96 
97  if (m_Pushed && state != "pushed")
98  UnPush();
99 
100  m_state = state;
101 
102  if (!m_BackgroundState)
103  return;
104 
106 
107  auto *activeState = dynamic_cast<MythUIGroup *>
109 
110  if (activeState)
111  m_Text = dynamic_cast<MythUIText *>(activeState->GetChild("text"));
112 
113  if (m_Text)
114  {
117  }
118 }
119 
123 bool MythUIButton::keyPressEvent(QKeyEvent *event)
124 {
125  QStringList actions;
126  bool handled = false;
127  handled = GetMythMainWindow()->TranslateKeyPress("Global", event, actions);
128 
129  for (int i = 0; i < actions.size() && !handled; i++)
130  {
131  QString action = actions[i];
132  handled = true;
133 
134  if (action == "SELECT")
135  {
136  if (IsEnabled())
137  {
138  if (m_Pushed)
139  UnPush();
140  else
141  Push();
142  }
143  }
144  else
145  handled = false;
146  }
147 
148  return handled;
149 }
150 
155 {
156  if (event->gesture() == MythGestureEvent::Click)
157  {
158  if (IsEnabled())
159  {
160  if (m_Pushed)
161  UnPush();
162  else
163  Push();
164 
165  return true;
166  }
167  }
168 
169  return false;
170 }
171 
172 void MythUIButton::Push(bool lock)
173 {
174  m_Pushed = true;
175  SetState("pushed");
176 
177  if (!lock && !m_Lockable)
178  m_clickTimer->start(500);
179 
180  emit Clicked();
181 }
182 
184 {
185  if (!m_Pushed)
186  return;
187 
188  m_clickTimer->stop();
189 
190  m_Pushed = false;
191 
192  if (m_HasFocus)
193  SetState("selected");
194  else if (m_Enabled)
195  SetState("active");
196  else
197  SetState("disabled");
198 
199  if (m_Lockable)
200  emit Clicked();
201 }
202 
203 void MythUIButton::SetLocked(bool locked)
204 {
205  if (!m_Lockable)
206  return;
207 
208  if (locked)
209  {
210  m_Pushed = true;
211  SetState("pushed");
212  }
213  else
214  {
215  m_Pushed = false;
216 
217  if (m_HasFocus)
218  SetState("selected");
219  else if (m_Enabled)
220  SetState("active");
221  else
222  SetState("disabled");
223  }
224 }
225 
226 void MythUIButton::SetText(const QString &msg)
227 {
228  if (m_Message == msg)
229  return;
230 
231  m_Message = msg;
232 
233  auto *activeState = dynamic_cast<MythUIGroup *>
235 
236  if (activeState)
237  m_Text = dynamic_cast<MythUIText *>(activeState->GetChild("text"));
238 
239  if (m_Text)
240  m_Text->SetText(msg);
241 }
242 
243 QString MythUIButton::GetText() const
244 {
245  return m_Message;
246 }
247 
249 {
250  return m_Text->GetDefaultText();
251 }
252 
257  const QString &filename, QDomElement &element, bool showWarnings)
258 {
259  if (element.tagName() == "value")
260  {
261  m_ValueText = QCoreApplication::translate("ThemeUI",
262  parseText(element).toUtf8());
263  }
264  else
265  {
266  return MythUIType::ParseElement(filename, element, showWarnings);
267  }
268 
269  return true;
270 }
271 
276 {
277  auto *button = new MythUIButton(parent, objectName());
278  button->CopyFrom(this);
279 }
280 
285 {
286  auto *button = dynamic_cast<MythUIButton *>(base);
287  if (!button)
288  {
289  LOG(VB_GENERAL, LOG_ERR, "Dynamic cast of base failed");
290  return;
291  }
292 
293  m_Message = button->m_Message;
294  m_ValueText = button->m_ValueText;
295  m_Lockable = button->m_Lockable;
296 
297  MythUIType::CopyFrom(base);
298 
300 }
301 
306 {
309 }
MythUIButton::Clicked
void Clicked()
MythUIText::SetFontState
void SetFontState(const QString &state)
Definition: mythuitext.cpp:224
MythUIButton::SetLocked
void SetLocked(bool locked)
Definition: mythuibutton.cpp:203
hardwareprofile.smolt.timeout
float timeout
Definition: smolt.py:103
mythuitext.h
MythUIType::Enabling
void Enabling()
MythMainWindow::TranslateKeyPress
bool TranslateKeyPress(const QString &context, QKeyEvent *e, QStringList &actions, bool allowJumps=true)
Get a list of actions for a keypress in the given context.
Definition: mythmainwindow.cpp:1106
MythUIType::GetChild
MythUIType * GetChild(const QString &name) const
Get a named child of this UIType.
Definition: mythuitype.cpp:130
MythUIButton::m_Text
MythUIText * m_Text
Definition: mythuibutton.h:66
MythUIButton::m_Message
QString m_Message
Definition: mythuibutton.h:62
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:342
MythUIButton::m_Lockable
bool m_Lockable
Definition: mythuibutton.h:71
MythUIGroup
Create a group of widgets.
Definition: mythuigroup.h:11
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
MythUIButton::m_ValueText
QString m_ValueText
Definition: mythuibutton.h:63
mythuistatetype.h
MythUIButton::Deselect
void Deselect()
Definition: mythuibutton.cpp:71
MythUIButton::GetText
QString GetText(void) const
Definition: mythuibutton.cpp:243
MythUIButton::UnPush
void UnPush()
Definition: mythuibutton.cpp:183
MythUIButton::keyPressEvent
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
Definition: mythuibutton.cpp:123
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:275
MythUIType::TakingFocus
void TakingFocus()
MythUIType::m_HasFocus
bool m_HasFocus
Definition: mythuitype.h:237
MythUIButton::Enable
void Enable()
Definition: mythuibutton.cpp:82
MythUIButton::gestureEvent
bool gestureEvent(MythGestureEvent *event) override
Mouse click/movement handler, receives mouse gesture events from the QCoreApplication event loop.
Definition: mythuibutton.cpp:154
mythlogging.h
MythUIType::IsEnabled
bool IsEnabled(void) const
Definition: mythuitype.h:94
MythUIType::Disabling
void Disabling()
MythGestureEvent::gesture
Gesture gesture(void) const
Get the gesture type.
Definition: mythgesture.h:109
MythUIButton::SetText
void SetText(const QString &msg)
Definition: mythuibutton.cpp:226
MythUIText::GetDefaultText
QString GetDefaultText(void) const
Definition: mythuitext.h:44
MythUIButton
A single button widget.
Definition: mythuibutton.h:21
MythUIButton::Push
void Push(bool lock=false)
Definition: mythuibutton.cpp:172
filename
QString filename
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:634
MythGestureEvent::Click
@ Click
Definition: mythgesture.h:75
MythUIType::CopyFrom
virtual void CopyFrom(MythUIType *base)
Copy this widgets state from another.
Definition: mythuitype.cpp:1147
MythUIButton::~MythUIButton
~MythUIButton() override
Definition: mythuibutton.cpp:35
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:69
mythuigroup.h
MythUIType
The base class on which all widgets and screens are based.
Definition: mythuitype.h:63
MythUIButton::Select
void Select()
Definition: mythuibutton.cpp:63
MythUIButton::m_Pushed
bool m_Pushed
Definition: mythuibutton.h:70
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:135
MythUIButton::Finalize
void Finalize(void) override
Perform any post-xml parsing initialisation tasks.
Definition: mythuibutton.cpp:305
MythUIButton::CopyFrom
void CopyFrom(MythUIType *base) override
Copy this widgets state from another.
Definition: mythuibutton.cpp:284
MythUIButton::m_BackgroundState
MythUIStateType * m_BackgroundState
Definition: mythuibutton.h:65
GetMythMainWindow
MythMainWindow * GetMythMainWindow(void)
Definition: mythmainwindow.cpp:107
MythUIButton::m_clickTimer
class QTimer * m_clickTimer
Definition: mythuibutton.h:72
MythUIButton::MythUIButton
MythUIButton(MythUIType *parent, const QString &name)
Definition: mythuibutton.cpp:19
build_compdb.action
action
Definition: build_compdb.py:9
mythuibutton.h
MythUIType::m_Enabled
bool m_Enabled
Definition: mythuitype.h:239
MythGestureEvent
A custom event that represents a mouse gesture.
Definition: mythgesture.h:39
MythUIButton::Disable
void Disable()
Definition: mythuibutton.cpp:87
MythUIButton::SetState
void SetState(const QString &state)
Definition: mythuibutton.cpp:92
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:1213
mythmainwindow.h
MythUIButton::GetDefaultText
QString GetDefaultText(void) const
Definition: mythuibutton.cpp:248
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:58
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:256
MythUIButton::SetInitialStates
void SetInitialStates(void)
Definition: mythuibutton.cpp:41
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