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  MythUIGroup *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  MythUIGroup *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 = qApp->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  MythUIButton *button = new MythUIButton(parent, objectName());
278  button->CopyFrom(this);
279 }
280 
285 {
286  MythUIButton *button = dynamic_cast<MythUIButton *>(base);
287 
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 }
QString GetDefaultText(void) const
Definition: mythuitext.h:44
void SetState(const QString &state)
bool TranslateKeyPress(const QString &context, QKeyEvent *e, QStringList &actions, bool allowJumps=true)
Get a list of actions for a keypress in the given context.
QString m_state
Definition: mythuibutton.h:68
void LosingFocus()
QString GetDefaultText(void) const
MythUIText * m_Text
Definition: mythuibutton.h:66
void Enabling()
void Reset(void) override
Reset the widget to it's original state, should not reset changes made by the theme.
virtual void SetText(const QString &text)
Definition: mythuitext.cpp:136
bool ParseElement(const QString &filename, QDomElement &element, bool showWarnings) override
Parse the xml definition of this widget setting the state of the object accordingly.
Gesture gesture(void) const
Get the gesture type.
Definition: mythgesture.h:107
The base class on which all widgets and screens are based.
Definition: mythuitype.h:63
class QTimer * m_clickTimer
Definition: mythuibutton.h:72
void TakingFocus()
MythUIButton(MythUIType *parent, const QString &name)
void CopyFrom(MythUIType *base) override
Copy this widgets state from another.
A C++ ripoff of the stroke library for MythTV.
void SetLocked(bool locked)
Create a group of widgets.
Definition: mythuigroup.h:11
void Clicked()
A custom event that represents a mouse gesture.
Definition: mythgesture.h:39
const char * name
Definition: ParseText.cpp:328
QString GetText(void) const
A single button widget.
Definition: mythuibutton.h:21
static QString parseText(QDomElement &element)
MythMainWindow * GetMythMainWindow(void)
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
virtual void Reset(void)
Reset the widget to it's original state, should not reset changes made by the theme.
Definition: mythuitype.cpp:69
QString m_ValueText
Definition: mythuibutton.h:63
bool m_Enabled
Definition: mythuitype.h:239
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
virtual void CopyFrom(MythUIType *base)
Copy this widgets state from another.
bool IsEnabled(void) const
Definition: mythuitype.h:94
void SetInitialStates(void)
bool DisplayState(const QString &name)
QString m_Message
Definition: mythuibutton.h:62
MythUIStateType * m_BackgroundState
Definition: mythuibutton.h:65
void SetFontState(const QString &)
Definition: mythuitext.cpp:225
void Finalize(void) override
Perform any post-xml parsing initialisation tasks.
void Disabling()
void CreateCopy(MythUIType *parent) override
Copy the state of this widget to the one given, it must be of the same type.
void SetText(const QString &msg)
MythUIType * GetChild(const QString &name) const
Get a named child of this UIType.
Definition: mythuitype.cpp:132
void Push(bool lock=false)
void SetCanTakeFocus(bool set=true)
Set whether this widget can take focus.
Definition: mythuitype.cpp:344
MythUIType * GetCurrentState()
virtual bool ParseElement(const QString &filename, QDomElement &element, bool showWarnings)
Parse the xml definition of this widget setting the state of the object accordingly.
bool m_HasFocus
Definition: mythuitype.h:237
bool gestureEvent(MythGestureEvent *event) override
Mouse click/movement handler, receives mouse gesture events from the QCoreApplication event loop.