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