| 1 |
/****************************************************************************** |
| 2 |
* |
| 3 |
* Copyright (C) 1997-2019 by Dimitri van Heesch. |
| 4 |
* |
| 5 |
* Permission to use, copy, modify, and distribute this software and its |
| 6 |
* documentation under the terms of the GNU General Public License is hereby |
| 7 |
* granted. No representations are made about the suitability of this software |
| 8 |
* for any purpose. It is provided "as is" without express or implied warranty. |
| 9 |
* See the GNU General Public License for more details. |
| 10 |
* |
| 11 |
*/ |
| 12 |
|
| 13 |
#include "inputint.h" |
| 14 |
#include "helplabel.h" |
| 15 |
#include "config_msg.h" |
| 16 |
|
| 17 |
#include <QSpinBox> |
| 18 |
#include <QGridLayout> |
| 19 |
#include <QWheelEvent> |
| 20 |
#include <QTextStream> |
| 21 |
|
| 22 |
class NoWheelSpinBox : public QSpinBox |
| 23 |
{ |
| 24 |
protected: |
| 25 |
void wheelEvent(QWheelEvent *e) |
| 26 |
{ |
| 27 |
e->ignore(); |
| 28 |
} |
| 29 |
}; |
| 30 |
|
| 31 |
InputInt::InputInt( QGridLayout *layout,int &row, |
| 32 |
const QString & id, |
| 33 |
int defVal, int minVal,int maxVal, |
| 34 |
const QString & docs ) |
| 35 |
: m_default(defVal), m_minVal(minVal), m_maxVal(maxVal), m_docs(docs), m_id(id) |
| 36 |
{ |
| 37 |
m_lab = new HelpLabel(id); |
| 38 |
m_sp = new NoWheelSpinBox; |
| 39 |
m_sp->setMinimum(minVal); |
| 40 |
m_sp->setMaximum(maxVal); |
| 41 |
m_sp->setSingleStep(1); |
| 42 |
m_val=defVal-1; // force update |
| 43 |
setValue(defVal); |
| 44 |
|
| 45 |
layout->addWidget( m_lab, row, 0 ); |
| 46 |
layout->addWidget( m_sp, row, 1 ); |
| 47 |
|
| 48 |
connect(m_sp, SIGNAL(valueChanged(int)), |
| 49 |
this, SLOT(setValue(int)) ); |
| 50 |
connect( m_lab, SIGNAL(enter()), SLOT(help()) ); |
| 51 |
connect( m_lab, SIGNAL(reset()), SLOT(reset()) ); |
| 52 |
row++; |
| 53 |
} |
| 54 |
|
| 55 |
void InputInt::help() |
| 56 |
{ |
| 57 |
showHelp(this); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
void InputInt::setValue(int val) |
| 62 |
{ |
| 63 |
int newVal = val; |
| 64 |
newVal = qMax(m_minVal,newVal); |
| 65 |
newVal = qMin(m_maxVal,newVal); |
| 66 |
if (val != newVal) |
| 67 |
{ |
| 68 |
config_warn("argument '%d' for option %s is not a valid number in the range [%d..%d]!" |
| 69 |
" Using the default: %d!\n",val,qPrintable(m_id),m_minVal,m_maxVal,m_default); |
| 70 |
newVal = m_default; |
| 71 |
} |
| 72 |
if (newVal!=m_val) |
| 73 |
{ |
| 74 |
m_val = newVal; |
| 75 |
m_sp->setValue(newVal); |
| 76 |
m_value = m_val; |
| 77 |
updateDefault(); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
void InputInt::updateDefault() |
| 82 |
{ |
| 83 |
{ |
| 84 |
if (m_val==m_default || !m_lab->isEnabled()) |
| 85 |
{ |
| 86 |
m_lab->setText(QString::fromLatin1("<qt>")+m_id+QString::fromLatin1("</qt>")); |
| 87 |
} |
| 88 |
else |
| 89 |
{ |
| 90 |
m_lab->setText(QString::fromLatin1("<qt><font color='red'>")+m_id+QString::fromLatin1("</font></qt>")); |
| 91 |
} |
| 92 |
emit changed(); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
void InputInt::setEnabled(bool state) |
| 97 |
{ |
| 98 |
m_lab->setEnabled(state); |
| 99 |
m_sp->setEnabled(state); |
| 100 |
updateDefault(); |
| 101 |
} |
| 102 |
|
| 103 |
QVariant &InputInt::value() |
| 104 |
{ |
| 105 |
return m_value; |
| 106 |
} |
| 107 |
|
| 108 |
void InputInt::update() |
| 109 |
{ |
| 110 |
bool ok; |
| 111 |
int newVal = m_value.toInt(&ok); |
| 112 |
if (!ok) |
| 113 |
{ |
| 114 |
config_warn("argument '%s' for option %s is not a valid number in the range [%d..%d]!" |
| 115 |
" Using the default: %d!\n",qPrintable(m_value.toString()),qPrintable(m_id),m_minVal,m_maxVal,m_default); |
| 116 |
newVal = m_default; |
| 117 |
} |
| 118 |
setValue(newVal); |
| 119 |
} |
| 120 |
|
| 121 |
void InputInt::reset() |
| 122 |
{ |
| 123 |
setValue(m_default); |
| 124 |
} |
| 125 |
|
| 126 |
void InputInt::writeValue(QTextStream &t,TextCodecAdapter *,bool) |
| 127 |
{ |
| 128 |
t << m_val; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
bool InputInt::isDefault() |
| 133 |
{ |
| 134 |
return m_val == m_default; |
| 135 |
} |