00001
00002 #include "mythuispinbox.h"
00003
00004
00005 #include <QDomDocument>
00006 #include <QCoreApplication>
00007
00008 MythUISpinBox::MythUISpinBox(MythUIType *parent, const QString &name)
00009 : MythUIButtonList(parent, name), m_hasTemplate(false),
00010 m_moveAmount(0)
00011 {
00012 }
00013
00014 MythUISpinBox::~MythUISpinBox()
00015 {
00016 }
00017
00029 void MythUISpinBox::SetRange(int low, int high, int step, uint pageMultiple)
00030 {
00031 if ((high == low) || step == 0)
00032 return;
00033
00034 m_moveAmount = pageMultiple;
00035
00036 bool reverse = false;
00037 int value = low;
00038
00039 if (low > high)
00040 reverse = true;
00041
00042 Reset();
00043
00044 while ((reverse && (value >= high)) ||
00045 (!reverse && (value <= high)))
00046 {
00047 QString text;
00048
00049 if (m_hasTemplate)
00050 {
00051 QString temp;
00052
00053 if (value < 0 && !m_negativeTemplate.isEmpty())
00054 temp = m_negativeTemplate;
00055 else if (value == 0 && !m_zeroTemplate.isEmpty())
00056 temp = m_zeroTemplate;
00057 else if (!m_positiveTemplate.isEmpty())
00058 temp = m_positiveTemplate;
00059
00060 if (!temp.isEmpty())
00061 {
00062 if (temp.contains("%n"))
00063 text = qApp->translate("ThemeUI", temp.toUtf8(), NULL,
00064 QCoreApplication::UnicodeUTF8,
00065 qAbs(value));
00066 else
00067 text = qApp->translate("ThemeUI", temp.toUtf8(), NULL,
00068 QCoreApplication::UnicodeUTF8);
00069 }
00070 }
00071
00072 if (text.isEmpty())
00073 text = QString::number(value);
00074
00075 new MythUIButtonListItem(this, text, qVariantFromValue(value));
00076
00077 if (reverse)
00078 value = value - step;
00079 else
00080 value = value + step;
00081 }
00082
00083 SetPositionArrowStates();
00084 }
00085
00089 bool MythUISpinBox::ParseElement(
00090 const QString &filename, QDomElement &element, bool showWarnings)
00091 {
00092 if (element.tagName() == "template")
00093 {
00094 QString format = parseText(element);
00095
00096 if (element.attribute("type") == "negative")
00097 m_negativeTemplate = format;
00098 else if (element.attribute("type") == "zero")
00099 m_zeroTemplate = format;
00100 else
00101 m_positiveTemplate = format;
00102
00103 m_hasTemplate = true;
00104 }
00105 else
00106 {
00107 return MythUIButtonList::ParseElement(filename, element, showWarnings);
00108 }
00109
00110 return true;
00111 }
00112
00116 bool MythUISpinBox::MoveDown(MovementUnit unit, uint amount)
00117 {
00118 bool handled = false;
00119
00120 if ((unit == MovePage) && m_moveAmount)
00121 handled = MythUIButtonList::MoveDown(MoveByAmount, m_moveAmount);
00122 else
00123 handled = MythUIButtonList::MoveDown(unit, amount);
00124
00125 return handled;
00126 }
00127
00131 bool MythUISpinBox::MoveUp(MovementUnit unit, uint amount)
00132 {
00133 bool handled = false;
00134
00135 if ((unit == MovePage) && m_moveAmount)
00136 handled = MythUIButtonList::MoveUp(MoveByAmount, m_moveAmount);
00137 else
00138 handled = MythUIButtonList::MoveUp(unit, amount);
00139
00140 return handled;
00141 }
00142
00146 void MythUISpinBox::CreateCopy(MythUIType *parent)
00147 {
00148 MythUISpinBox *spinbox = new MythUISpinBox(parent, objectName());
00149 spinbox->CopyFrom(this);
00150 }
00151
00155 void MythUISpinBox::CopyFrom(MythUIType *base)
00156 {
00157 MythUISpinBox *spinbox = dynamic_cast<MythUISpinBox *>(base);
00158
00159 if (!spinbox)
00160 return;
00161
00162 m_hasTemplate = spinbox->m_hasTemplate;
00163 m_negativeTemplate = spinbox->m_negativeTemplate;
00164 m_zeroTemplate = spinbox->m_zeroTemplate;
00165 m_positiveTemplate = spinbox->m_positiveTemplate;
00166
00167 MythUIButtonList::CopyFrom(base);
00168 }