MythTV  master
mythuispinbox.cpp
Go to the documentation of this file.
1 
2 #include "mythmainwindow.h"
3 #include "mythuispinbox.h"
4 #include "mythlogging.h"
5 #include "mythuibutton.h"
6 #include "mythuitextedit.h"
7 #include "mythuitext.h"
8 
9 // QT headers
10 #include <QCoreApplication>
11 #include <QDomDocument>
12 #include <utility>
13 
25 void MythUISpinBox::SetRange(int low, int high, int step, uint pageMultiple)
26 {
27  if ((high == low) || step == 0)
28  return;
29 
30  m_low = low;
31  m_high = high;
32  m_step = step;
33 
34  m_moveAmount = pageMultiple;
35 
36  bool reverse = false;
37  int value = low;
38 
39  if (low > high)
40  reverse = true;
41 
42  Reset();
43 
44  while ((reverse && (value >= high)) ||
45  (!reverse && (value <= high)))
46  {
47  QString text;
48 
49  if (m_hasTemplate)
50  {
51  QString temp;
52 
53  if (value < 0 && !m_negativeTemplate.isEmpty())
54  temp = m_negativeTemplate;
55  else if (value == 0 && !m_zeroTemplate.isEmpty())
56  temp = m_zeroTemplate;
57  else if (!m_positiveTemplate.isEmpty())
58  temp = m_positiveTemplate;
59 
60  if (!temp.isEmpty())
61  {
62  if (temp.contains("%n"))
63  {
64  text = QCoreApplication::translate("ThemeUI", temp.toUtf8(), nullptr,
65  qAbs(value));
66  }
67  else
68  {
69  text = QCoreApplication::translate("ThemeUI", temp.toUtf8());
70  }
71  }
72  }
73 
74  if (text.isEmpty())
75  text = QString::number(value);
76 
77  new MythUIButtonListItem(this, text, QVariant::fromValue(value));
78 
79  if (reverse)
80  value = value - step;
81  else
82  value = value + step;
83  }
84 
86 }
87 
88 
95 void MythUISpinBox::AddSelection(int value, const QString &label)
96 {
97  if (!label.isEmpty())
98  {
99  MythUIButtonListItem *item = GetItemByData(value);
100  if (item)
101  {
102  item->SetText(label);
103  return;
104  }
105  }
106 
107  int insertPos=-1;
108 
109  for (int pos = 0; pos < m_itemList.size(); pos++)
110  {
111  MythUIButtonListItem *item = m_itemList.at(pos);
112  if (item->GetData().toInt() > value)
113  {
114  insertPos = pos;
115  break;
116  }
117  }
118 
119  new MythUIButtonListItem(this, label.isEmpty() ? QString(value) : label,
120  QVariant::fromValue(value), insertPos);
121 }
122 
127  const QString &filename, QDomElement &element, bool showWarnings)
128 {
129  if (element.tagName() == "template")
130  {
131  QString format = parseText(element);
132 
133  if (element.attribute("type") == "negative")
134  m_negativeTemplate = format;
135  else if (element.attribute("type") == "zero")
136  m_zeroTemplate = format;
137  else
138  m_positiveTemplate = format;
139 
140  m_hasTemplate = true;
141  }
142  else
143  {
144  return MythUIButtonList::ParseElement(filename, element, showWarnings);
145  }
146 
147  return true;
148 }
149 
154 {
155  bool handled = false;
156 
157  if ((unit == MovePage) && m_moveAmount)
159  else
160  handled = MythUIButtonList::MoveDown(unit, amount);
161 
162  return handled;
163 }
164 
169 {
170  bool handled = false;
171 
172  if ((unit == MovePage) && m_moveAmount)
174  else
175  handled = MythUIButtonList::MoveUp(unit, amount);
176 
177  return handled;
178 }
179 
184 {
185  auto *spinbox = new MythUISpinBox(parent, objectName());
186  spinbox->CopyFrom(this);
187 }
188 
193 {
194  auto *spinbox = dynamic_cast<MythUISpinBox *>(base);
195 
196  if (!spinbox)
197  return;
198 
199  m_hasTemplate = spinbox->m_hasTemplate;
200  m_negativeTemplate = spinbox->m_negativeTemplate;
201  m_zeroTemplate = spinbox->m_zeroTemplate;
202  m_positiveTemplate = spinbox->m_positiveTemplate;
203 
205 }
206 
207 // Open the entry dialog on certain key presses. A select or search action will
208 // open the dialog. A number or minus sign will open the entry dialog with the
209 // given key as the first digit.
210 // If the spinbox uses a template, the entries are not just numbers
211 // but can be sentences. The whole sentence is put in the entry field,
212 // allowing the user to change the number part of it.
213 
214 bool MythUISpinBox::keyPressEvent(QKeyEvent *event)
215 {
216  QStringList actions;
217  bool handled = false;
218  handled = GetMythMainWindow()->TranslateKeyPress("Global", event, actions);
219  if (handled)
220  return true;
221 
223  if (item == nullptr)
224  return MythUIButtonList::keyPressEvent(event);
225 
226  QString initialEntry = item->GetText();
227  bool doEntry = false;
228 
229  // Only invoke the entry dialog if the entry is a number
230  bool isNumber = false;
231  (void)initialEntry.toLongLong(&isNumber,10);
232  if (!isNumber)
233  return MythUIButtonList::keyPressEvent(event);
234 
235  for (int i = 0; i < actions.size(); ++i)
236  {
237  if (actions[i] >= ACTION_0 && actions[i] <= ACTION_9)
238  {
239  if (!m_hasTemplate)
240  initialEntry = actions[i];
241  doEntry=true;
242  break;
243  }
244  if (actions[i] == ACTION_SELECT || actions[i] == "SEARCH")
245  {
246  doEntry=true;
247  break;
248  }
249  }
250  if (actions.empty() && event->text() == "-")
251  {
252  if (!m_hasTemplate)
253  initialEntry = "-";
254  doEntry=true;
255  }
256 
257  if (doEntry)
258  {
259  ShowEntryDialog(initialEntry);
260  handled = true;
261  }
262 
263  if (handled)
264  return true;
265  return MythUIButtonList::keyPressEvent(event);
266 }
267 
268 void MythUISpinBox::ShowEntryDialog(QString initialEntry)
269 {
270  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
271 
272  auto *dlg = new SpinBoxEntryDialog(popupStack, "SpinBoxEntryDialog",
273  this, std::move(initialEntry), m_low, m_high, m_step);
274 
275  if (dlg->Create())
276  popupStack->AddScreen(dlg);
277  else
278  delete dlg;
279 }
280 
281 // Convenience Dialog to allow entry of a Spinbox value
282 
284  MythUIButtonList *parentList, QString searchText,
285  int low, int high, int step)
286  : MythScreenType(parent, name, false),
287  m_parentList(parentList),
288  m_searchText(std::move(searchText)),
289  m_entryEdit(nullptr),
290  m_cancelButton(nullptr),
291  m_okButton(nullptr),
292  m_rulesText(nullptr),
293  m_okClicked(false),
294  m_low(low),
295  m_high(high),
296  m_step(step)
297 
298 {
299  m_selection = parentList->GetCurrentPos();
300 }
301 
302 
304 {
305  if (!CopyWindowFromBase("SpinBoxEntryDialog", this))
306  return false;
307 
308  bool err = false;
309  UIUtilE::Assign(this, m_entryEdit, "entry", &err);
310  UIUtilW::Assign(this, m_cancelButton,"cancel", &err);
311  UIUtilW::Assign(this, m_rulesText,"rules", &err);
312  UIUtilE::Assign(this, m_okButton, "ok", &err);
313 
314  if (err)
315  {
316  LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'SpinBoxEntryDialog'");
317  return false;
318  }
319 
321  entryChanged();
322  if (m_rulesText)
323  {
324  InfoMap infoMap;
325  infoMap["low"] = QString::number(m_low);
326  infoMap["high"] = QString::number(m_high);
327  infoMap["step"] = QString::number(m_step);
328  m_rulesText->SetTextFromMap(infoMap);
329  }
330 
331  connect(m_entryEdit, SIGNAL(valueChanged()), SLOT(entryChanged()));
332  if (m_cancelButton)
333  connect(m_cancelButton, SIGNAL(Clicked()), SLOT(Close()));
334  connect(m_okButton, SIGNAL(Clicked()), SLOT(okClicked()));
335 
336  BuildFocusList();
337 
338  return true;
339 }
340 
342 {
343  int currPos = 0;
344  int count = m_parentList->GetCount();
345  QString searchText = m_entryEdit->GetText();
346  bool found = false;
347  for (currPos = 0; currPos < count; currPos++)
348  {
349  if (searchText.compare(m_parentList->GetItemAt(currPos)->GetText(),
350  Qt::CaseInsensitive) == 0)
351  {
352  found = true;
353  m_selection = currPos;
354  break;
355  }
356  }
357  m_okButton->SetEnabled(found);
358 }
359 
361 {
363  Close();
364 }
MythUIButtonList::GetItemAt
MythUIButtonListItem * GetItemAt(int pos) const
Definition: mythuibuttonlist.cpp:1676
SpinBoxEntryDialog::m_entryEdit
MythUITextEdit * m_entryEdit
Definition: mythuispinbox.h:78
MythUISpinBox::m_positiveTemplate
QString m_positiveTemplate
Definition: mythuispinbox.h:50
SpinBoxEntryDialog::SpinBoxEntryDialog
SpinBoxEntryDialog(MythScreenStack *parent, const char *name, MythUIButtonList *parentList, QString searchText, int low, int high, int step)
Definition: mythuispinbox.cpp:283
MythUISpinBox::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: mythuispinbox.cpp:126
MythUIButtonList::GetItemCurrent
MythUIButtonListItem * GetItemCurrent() const
Definition: mythuibuttonlist.cpp:1590
MythUIButtonList::m_itemList
QList< MythUIButtonListItem * > m_itemList
Definition: mythuibuttonlist.h:339
mythuitext.h
MythUISpinBox::CreateCopy
void CreateCopy(MythUIType *parent) override
Copy the state of this widget to the one given, it must be of the same type.
Definition: mythuispinbox.cpp:183
SpinBoxEntryDialog::okClicked
void okClicked(void)
Definition: mythuispinbox.cpp:360
MythUISpinBox::m_low
int m_low
Definition: mythuispinbox.h:53
false
VERBOSE_PREAMBLE false
Definition: verbosedefs.h:85
SpinBoxEntryDialog::m_high
int m_high
Definition: mythuispinbox.h:85
SpinBoxEntryDialog::m_parentList
MythUIButtonList * m_parentList
Definition: mythuispinbox.h:76
MythScreenType::Close
virtual void Close()
Definition: mythscreentype.cpp:402
ACTION_0
#define ACTION_0
Definition: mythuiactions.h:4
MythUIButtonList::MoveDown
virtual bool MoveDown(MovementUnit unit=MoveItem, uint amount=0)
Definition: mythuibuttonlist.cpp:2163
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
SpinBoxEntryDialog::m_searchText
QString m_searchText
Definition: mythuispinbox.h:77
MythScreenStack
Definition: mythscreenstack.h:16
SpinBoxEntryDialog::m_okButton
MythUIButton * m_okButton
Definition: mythuispinbox.h:80
MythUISpinBox::m_zeroTemplate
QString m_zeroTemplate
Definition: mythuispinbox.h:49
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
SpinBoxEntryDialog::m_selection
int m_selection
Definition: mythuispinbox.h:82
SpinBoxEntryDialog
Definition: mythuispinbox.h:61
MythScreenType
Screen in which all other widgets are contained and rendered.
Definition: mythscreentype.h:45
MythUISpinBox::keyPressEvent
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
Definition: mythuispinbox.cpp:214
MythUITextEdit::GetText
QString GetText(void) const
Definition: mythuitextedit.h:47
MythUIButtonList::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: mythuibuttonlist.cpp:2839
MythUISpinBox::MoveDown
bool MoveDown(MovementUnit unit=MoveItem, uint amount=0) override
Definition: mythuispinbox.cpp:153
MythUISpinBox::m_high
int m_high
Definition: mythuispinbox.h:54
MythUISpinBox::SetRange
void SetRange(int low, int high, int step, uint pageMultiple=5)
Set the lower and upper bounds of the spinbox, the interval and page amount.
Definition: mythuispinbox.cpp:25
MythMainWindow::GetStack
MythScreenStack * GetStack(const QString &stackname)
Definition: mythmainwindow.cpp:309
SpinBoxEntryDialog::m_step
int m_step
Definition: mythuispinbox.h:86
MythUIButtonList::GetCount
int GetCount() const
Definition: mythuibuttonlist.cpp:1655
MythUIButtonList::GetItemByData
MythUIButtonListItem * GetItemByData(const QVariant &data)
Definition: mythuibuttonlist.cpp:1684
XMLParseBase::parseText
static QString parseText(QDomElement &element)
Definition: xmlparsebase.cpp:324
InfoMap
QHash< QString, QString > InfoMap
Definition: mythtypes.h:15
MythUIButtonList::MovePage
@ MovePage
Definition: mythuibuttonlist.h:157
MythUIButtonListItem::SetText
void SetText(const QString &text, const QString &name="", const QString &state="")
Definition: mythuibuttonlist.cpp:3272
MythUIButtonList::MythUIButtonListItem
friend class MythUIButtonListItem
Definition: mythuibuttonlist.h:347
MythUISpinBox::m_step
int m_step
Definition: mythuispinbox.h:55
MythUISpinBox::m_negativeTemplate
QString m_negativeTemplate
Definition: mythuispinbox.h:48
MythUIButtonListItem
Definition: mythuibuttonlist.h:28
MythUITextEdit::SetText
void SetText(const QString &text, bool moveCursor=true)
Definition: mythuitextedit.cpp:216
mythlogging.h
MythUISpinBox::MythUISpinBox
MythUISpinBox(MythUIType *parent, const QString &name)
Definition: mythuispinbox.h:20
MythUIButtonList::GetCurrentPos
int GetCurrentPos() const
Definition: mythuibuttonlist.h:190
SpinBoxEntryDialog::Create
bool Create(void) override
Definition: mythuispinbox.cpp:303
MythUIButtonList::keyPressEvent
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
Definition: mythuibuttonlist.cpp:2490
MythUIButtonList::MoveByAmount
@ MoveByAmount
Definition: mythuibuttonlist.h:158
SpinBoxEntryDialog::m_low
int m_low
Definition: mythuispinbox.h:84
MythScreenType::BuildFocusList
void BuildFocusList(void)
Definition: mythscreentype.cpp:222
MythUISpinBox::MoveUp
bool MoveUp(MovementUnit unit=MoveItem, uint amount=0) override
Definition: mythuispinbox.cpp:168
XMLParseBase::CopyWindowFromBase
static bool CopyWindowFromBase(const QString &windowname, MythScreenType *win)
Definition: xmlparsebase.cpp:917
SpinBoxEntryDialog::m_cancelButton
MythUIButton * m_cancelButton
Definition: mythuispinbox.h:79
filename
QString filename
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:634
ACTION_SELECT
#define ACTION_SELECT
Definition: mythuiactions.h:15
MythUIType::SetEnabled
void SetEnabled(bool enable)
Definition: mythuitype.cpp:1107
MythUIButtonList::MoveUp
virtual bool MoveUp(MovementUnit unit=MoveItem, uint amount=0)
Definition: mythuibuttonlist.cpp:1945
MythUIButtonListItem::GetData
QVariant GetData()
Definition: mythuibuttonlist.cpp:3588
uint
unsigned int uint
Definition: compat.h:140
ACTION_9
#define ACTION_9
Definition: mythuiactions.h:13
MythUIButtonList::MovementUnit
MovementUnit
Definition: mythuibuttonlist.h:157
UIUtilDisp::Assign
static bool Assign(ContainerType *container, UIType *&item, const QString &name, bool *err=nullptr)
Definition: mythuiutils.h:27
MythUIType
The base class on which all widgets and screens are based.
Definition: mythuitype.h:64
MythUIButtonListItem::GetText
QString GetText(const QString &name="") const
Definition: mythuibuttonlist.cpp:3313
mythuispinbox.h
SpinBoxEntryDialog::entryChanged
void entryChanged(void)
Definition: mythuispinbox.cpp:341
mythuitextedit.h
MythUISpinBox::CopyFrom
void CopyFrom(MythUIType *base) override
Copy this widgets state from another.
Definition: mythuispinbox.cpp:192
MythUIButtonList::CalculateArrowStates
void CalculateArrowStates(void)
Definition: mythuibuttonlist.cpp:1393
MythUIText::SetTextFromMap
void SetTextFromMap(const InfoMap &map)
Definition: mythuitext.cpp:157
MythUIButtonList::Reset
void Reset() override
Reset the widget to it's original state, should not reset changes made by the theme.
Definition: mythuibuttonlist.cpp:114
GetMythMainWindow
MythMainWindow * GetMythMainWindow(void)
Definition: mythmainwindow.cpp:107
MythUIButtonList::SetItemCurrent
void SetItemCurrent(MythUIButtonListItem *item)
Definition: mythuibuttonlist.cpp:1557
MythUISpinBox
A widget for offering a range of numerical values where only the the bounding values and interval are...
Definition: mythuispinbox.h:17
mythuibutton.h
MythUISpinBox::m_hasTemplate
bool m_hasTemplate
Definition: mythuispinbox.h:47
MythUISpinBox::ShowEntryDialog
void ShowEntryDialog(QString initialEntry)
Definition: mythuispinbox.cpp:268
MythUIButtonList
List widget, displays list items in a variety of themeable arrangements and can trigger signals when ...
Definition: mythuibuttonlist.h:144
mythmainwindow.h
MythScreenStack::AddScreen
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
Definition: mythscreenstack.cpp:49
MythUISpinBox::AddSelection
void AddSelection(int value, const QString &label="")
Add a special label for a value of the spinbox, it does not need to be in the range.
Definition: mythuispinbox.cpp:95
SpinBoxEntryDialog::m_rulesText
MythUIText * m_rulesText
Definition: mythuispinbox.h:81
MythUISpinBox::m_moveAmount
uint m_moveAmount
Definition: mythuispinbox.h:52
MythUIButtonList::CopyFrom
void CopyFrom(MythUIType *base) override
Copy this widgets state from another.
Definition: mythuibuttonlist.cpp:2977