kdeui: simplify KIntValidator

KDoubleValidator rewritten also by me, see the following commit:
84ea11af90

thus copyright goes to me. also now KIntValidator uses the global
KLocale for validatation

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2024-05-21 09:39:56 +03:00
parent 27ae4b6288
commit f559d2b200
7 changed files with 124 additions and 308 deletions

View file

@ -109,7 +109,7 @@ KInputDialogHelper::KInputDialogHelper( const QString &caption, const QString &l
} }
KInputDialogHelper::KInputDialogHelper( const QString &caption, const QString &label, KInputDialogHelper::KInputDialogHelper( const QString &caption, const QString &label,
int value, int minValue, int maxValue, int step, int base, int value, int minValue, int maxValue, int step,
QWidget *parent ) QWidget *parent )
: KDialog(parent), : KDialog(parent),
m_label(0), m_lineEdit(0), m_intSpinBox(0), m_label(0), m_lineEdit(0), m_intSpinBox(0),
@ -131,7 +131,6 @@ KInputDialogHelper::KInputDialogHelper( const QString &caption, const QString &l
m_intSpinBox->setRange(minValue, maxValue); m_intSpinBox->setRange(minValue, maxValue);
m_intSpinBox->setSingleStep(step); m_intSpinBox->setSingleStep(step);
m_intSpinBox->setValue(value); m_intSpinBox->setValue(value);
m_intSpinBox->setBase(base);
layout->addWidget(m_intSpinBox); layout->addWidget(m_intSpinBox);
layout->setMargin(0); layout->setMargin(0);
@ -382,10 +381,10 @@ QString getMultiLineText( const QString &caption,
} }
int getInteger( const QString &caption, const QString &label, int getInteger( const QString &caption, const QString &label,
int value, int minValue, int maxValue, int step, int base, bool *ok, int value, int minValue, int maxValue, int step, bool *ok,
QWidget *parent ) QWidget *parent )
{ {
KInputDialogHelper dlg(caption, label, value, minValue, maxValue, step, base, parent); KInputDialogHelper dlg(caption, label, value, minValue, maxValue, step, parent);
bool _ok = (dlg.exec() == KDialog::Accepted); bool _ok = (dlg.exec() == KDialog::Accepted);
@ -399,13 +398,6 @@ int getInteger( const QString &caption, const QString &label,
return result; return result;
} }
int getInteger( const QString &caption, const QString &label,
int value, int minValue, int maxValue, int step, bool *ok,
QWidget *parent )
{
return getInteger(caption, label, value, minValue, maxValue, step, 10, ok, parent);
}
double getDouble( const QString &caption, const QString &label, double getDouble( const QString &caption, const QString &label,
double value, double minValue, double maxValue, double step, int decimals, double value, double minValue, double maxValue, double step, int decimals,
bool *ok, QWidget *parent ) bool *ok, QWidget *parent )

View file

@ -100,7 +100,7 @@ namespace KInputDialog
* value for the spin box. minValue and maxValue are the minimum and * value for the spin box. minValue and maxValue are the minimum and
* maximum allowable values the user may choose. step is the amount by * maximum allowable values the user may choose. step is the amount by
* which the value will change as the user presses the increment and * which the value will change as the user presses the increment and
* decrement buttons of the spin box. Base is the base of the number. * decrement buttons of the spin box..
* *
* @param caption Caption of the dialog * @param caption Caption of the dialog
* @param label Text of the label for the spin box * @param label Text of the label for the spin box
@ -108,21 +108,11 @@ namespace KInputDialog
* @param minValue Minimum value user can input * @param minValue Minimum value user can input
* @param maxValue Maximum value user can input * @param maxValue Maximum value user can input
* @param step Amount by which value is incremented or decremented * @param step Amount by which value is incremented or decremented
* @param base Base of the number
* @param ok This bool would be set to true if user pressed Ok * @param ok This bool would be set to true if user pressed Ok
* @param parent Parent of the dialog widget * @param parent Parent of the dialog widget
* *
* @return Number user entered if Ok was pressed, else 0 * @return Number user entered if Ok was pressed, else 0
*/ */
KDEUI_EXPORT int getInteger( const QString &caption, const QString &label,
int value=0, int minValue=INT_MIN, int maxValue=INT_MAX,
int step=1, int base=10, bool *ok=0, QWidget *parent=0 );
/**
* This is an overloaded convenience function. It behaves exactly same as
* above except it assumes base to be 10, i.e. accepts decimal numbers.
*/
KDEUI_EXPORT int getInteger( const QString &caption, const QString &label, KDEUI_EXPORT int getInteger( const QString &caption, const QString &label,
int value=0, int minValue=INT_MIN, int maxValue=INT_MAX, int value=0, int minValue=INT_MIN, int maxValue=INT_MAX,
int step=1, bool *ok=0, QWidget *parent=0 ); int step=1, bool *ok=0, QWidget *parent=0 );

View file

@ -46,7 +46,7 @@ class KInputDialogHelper : public KDialog
KInputDialogHelper( const QString &caption, const QString &label, KInputDialogHelper( const QString &caption, const QString &label,
const QString &value, QWidget *parent ); const QString &value, QWidget *parent );
KInputDialogHelper( const QString &caption, const QString &label, int value, KInputDialogHelper( const QString &caption, const QString &label, int value,
int minValue, int maxValue, int step, int base, QWidget *parent ); int minValue, int maxValue, int step, QWidget *parent );
KInputDialogHelper( const QString &caption, const QString &label, double value, KInputDialogHelper( const QString &caption, const QString &label, double value,
double minValue, double maxValue, double step, int decimals, double minValue, double maxValue, double step, int decimals,
QWidget *parent ); QWidget *parent );

View file

@ -1,182 +1,29 @@
/********************************************************************** /*
** This file is part of the KDE libraries
** Copyright (C) 2024 Ivailo Monev <xakepa10@gmail.com>
** KIntValidator:
** Copyright (C) 1999 Glen Parker <glenebob@nwlink.com> This library is free software; you can redistribute it and/or
** KDoubleValidator: modify it under the terms of the GNU Library General Public
** Copyright (c) 2002 Marc Mutz <mutz@kde.org> License version 2, as published by the Free Software Foundation.
**
** This library is free software; you can redistribute it and/or This library is distributed in the hope that it will be useful,
** modify it under the terms of the GNU Library General Public but WITHOUT ANY WARRANTY; without even the implied warranty of
** License as published by the Free Software Foundation; either MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** version 2 of the License, or (at your option) any later version. Library General Public License for more details.
**
** This library is distributed in the hope that it will be useful, You should have received a copy of the GNU Library General Public License
** but WITHOUT ANY WARRANTY; without even the implied warranty of along with this library; see the file COPYING.LIB. If not, write to
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
** Library General Public License for more details. Boston, MA 02110-1301, USA.
** */
** You should have received a copy of the GNU Library General Public
** License along with this library; if not, write to the Free
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**
*****************************************************************************/
#include "knumvalidator.h" #include "knumvalidator.h"
#include <QWidget>
#include <QString>
#include <klocale.h> #include <klocale.h>
#include <kglobal.h> #include <kglobal.h>
#include <kdebug.h> #include <kdebug.h>
/////////////////////////////////////////////////////////////// static void kAcceptLocalizedNumbers(QValidator *validator, const bool accept)
// Implementation of KIntValidator
//
class KIntValidator::KIntValidatorPrivate
{
public:
KIntValidatorPrivate()
: _base(0), _min(0), _max(0)
{
}
int _base;
int _min;
int _max;
};
KIntValidator::KIntValidator(QWidget *parent, int base)
: QValidator(parent),
d(new KIntValidatorPrivate())
{
setBase(base);
}
KIntValidator::KIntValidator(int bottom, int top, QWidget *parent, int base)
: QValidator(parent),
d(new KIntValidatorPrivate())
{
setBase(base);
setRange(bottom, top);
}
KIntValidator::~KIntValidator()
{
delete d;
}
QValidator::State KIntValidator::validate(QString &str, int &) const
{
bool ok = false;
int val = 0;
QString newStr;
newStr = str.trimmed();
if (d->_base > 10) {
newStr = newStr.toUpper();
}
if (newStr == QLatin1String("-")) {
// a special case
if ((d->_min || d->_max) && d->_min >= 0) {
ok = false;
} else {
return QValidator::Acceptable;
}
} else if (!newStr.isEmpty()) {
val = newStr.toInt(&ok, d->_base);
} else {
val = 0;
ok = true;
}
if (!ok) {
return QValidator::Invalid;
}
if ((!d->_min && ! d->_max) || (val >= d->_min && val <= d->_max)) {
return QValidator::Acceptable;
}
if (d->_max && d->_min >= 0 && val < 0) {
return QValidator::Invalid;
}
return QValidator::Intermediate;
}
void KIntValidator::fixup(QString &str) const
{
int dummy = 0;
int val = 0;
QValidator::State state;
state = validate(str, dummy);
if (state == QValidator::Invalid || state == QValidator::Acceptable) {
return;
}
if (!d->_min && !d->_max) {
return;
}
val = str.toInt(0, d->_base);
if (val < d->_min) {
val = d->_min;
}
if (val > d->_max) {
val = d->_max;
}
str.setNum(val, d->_base);
}
void KIntValidator::setRange(int bottom, int top)
{
d->_min = bottom;
d->_max = top;
if (d->_max < d->_min) {
d->_max = d->_min;
}
}
void KIntValidator::setBase(int base)
{
d->_base = base;
if (d->_base < 2) {
d->_base = 2;
}
if (d->_base > 36) {
d->_base = 36;
}
}
int KIntValidator::bottom() const
{
return d->_min;
}
int KIntValidator::top() const
{
return d->_max;
}
int KIntValidator::base() const
{
return d->_base;
}
///////////////////////////////////////////////////////////////
// Implementation of KDoubleValidator
//
static void kAcceptLocalizedNumbers(KDoubleValidator *validator, const bool accept)
{ {
if (accept) { if (accept) {
validator->setLocale(KGlobal::locale()->toLocale()); validator->setLocale(KGlobal::locale()->toLocale());
@ -185,6 +32,49 @@ static void kAcceptLocalizedNumbers(KDoubleValidator *validator, const bool acce
} }
} }
class KIntValidator::KIntValidatorPrivate
{
public:
KIntValidatorPrivate()
: acceptLocalizedNumbers(true)
{
}
bool acceptLocalizedNumbers;
};
KIntValidator::KIntValidator(QObject *parent)
: QIntValidator(parent),
d(new KIntValidatorPrivate())
{
kAcceptLocalizedNumbers(this, true);
}
KIntValidator::KIntValidator(int bottom, int top, QObject *parent)
: QIntValidator(bottom, top, parent),
d(new KIntValidatorPrivate())
{
kAcceptLocalizedNumbers(this, true);
}
KIntValidator::~KIntValidator()
{
delete d;
}
bool KIntValidator::acceptLocalizedNumbers() const
{
return d->acceptLocalizedNumbers;
}
void KIntValidator::setAcceptLocalizedNumbers(bool accept)
{
d->acceptLocalizedNumbers = accept;
kAcceptLocalizedNumbers(this, accept);
}
class KDoubleValidator::KDoubleValidatorPrivate class KDoubleValidator::KDoubleValidatorPrivate
{ {
public: public:

View file

@ -1,139 +1,104 @@
/********************************************************************** /*
** This file is part of the KDE libraries
** Copyright (C) 1999 Glen Parker <glenebob@nwlink.com> Copyright (C) 2024 Ivailo Monev <xakepa10@gmail.com>
** Copyright (C) 2002 Marc Mutz <mutz@kde.org>
** This library is free software; you can redistribute it and/or
** This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public
** modify it under the terms of the GNU Library General Public License version 2, as published by the Free Software Foundation.
** License as published by the Free Software Foundation; either
** version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** This library is distributed in the hope that it will be useful, MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** but WITHOUT ANY WARRANTY; without even the implied warranty of Library General Public License for more details.
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Library General Public License for more details. You should have received a copy of the GNU Library General Public License
** along with this library; see the file COPYING.LIB. If not, write to
** You should have received a copy of the GNU Library General Public the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
** License along with this library; if not, write to the Free Boston, MA 02110-1301, USA.
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
**
*****************************************************************************/
#ifndef KNUMVALIDATOR_H #ifndef KNUMVALIDATOR_H
#define KNUMVALIDATOR_H #define KNUMVALIDATOR_H
#include <kdeui_export.h> #include <kdeui_export.h>
#include <QtGui/QValidator> #include <QValidator>
#include <QWidget>
#include <QString>
/** /**
* QValidator for integers. * @short A locale-aware QIntValidator
*
This can be used by QLineEdit or subclass to provide validated * QIntValidator extends QIntValidator to be locale-aware. That means that - subject to not being
text entry. Can be provided with a base value (default is 10), to allow * disabled - the system locale thousand separator, positive and negative sign are used for
the proper entry of hexadecimal, octal, or any other base numeric data. * validation.
*
@author Glen Parker <glenebob@nwlink.com> * @author Ivailo Monev <xakepa10@gmail.com>
@version 0.0.1 * @see KIntValidator
*/ **/
class KDEUI_EXPORT KIntValidator : public QValidator class KDEUI_EXPORT KIntValidator : public QIntValidator
{ {
Q_OBJECT
Q_PROPERTY(bool acceptLocalizedNumbers READ acceptLocalizedNumbers WRITE setAcceptLocalizedNumbers)
public: public:
/** /**
* Constructor. Also sets the base value. * Constuct a locale-aware KIntValidator with default range
*/ */
explicit KIntValidator(QWidget * parent, int base = 10); explicit KIntValidator(QObject *parent);
/** /**
* Constructor. Also sets the minimum, maximum, and numeric base values. * Constuct a locale-aware KIntValidator for the speicified range and decimals
*/ */
KIntValidator(int bottom, int top, QWidget *parent, int base = 10); KIntValidator(int bottom, int top, QObject *parent);
virtual ~KIntValidator(); virtual ~KIntValidator();
/** /** @return whether localized numbers are accepted, enabled by default */
* Validates the text, and return the result. Does not modify the parameters. bool acceptLocalizedNumbers() const;
*/
virtual QValidator::State validate(QString &, int &) const;
/** /** Sets whether to accept localized numbers, enabled by default */
* Fixes the text if possible, providing a valid string. The parameter may be modified. void setAcceptLocalizedNumbers(bool accept);
*/
virtual void fixup(QString &) const;
/**
* Sets the minimum and maximum values allowed.
* If @p top is greater than @p bottom, it is set to the value of @p bottom.
*/
virtual void setRange(int bottom, int top);
/**
* Sets the numeric base value. @p base must be between 2 and 36.
*/
virtual void setBase(int base);
/**
* Returns the current minimum value allowed.
*/
virtual int bottom() const;
/**
* Returns the current maximum value allowed.
*/
virtual int top() const;
/**
* Returns the current numeric base.
*/
virtual int base() const;
private: private:
class KIntValidatorPrivate; class KIntValidatorPrivate;
KIntValidatorPrivate * const d; KIntValidatorPrivate* const d;
}; };
/** /**
@short A locale-aware QDoubleValidator * @short A locale-aware QDoubleValidator
*
KDoubleValidator extends QDoubleValidator to be locale-aware. That * KDoubleValidator extends QDoubleValidator to be locale-aware. That means that - subject to not
means that - subject to not being disabled - the system locale * being disabled - the system locale decimal point, thousand separator, positive and negative sign
decimal point, thousand separator, positive and negative sign are * are used for validation.
used for validation. *
* @author Ivailo Monev <xakepa10@gmail.com>
@author Marc Mutz <mutz@kde.org> * @see KIntValidator
@see KIntValidator **/
**/
class KDEUI_EXPORT KDoubleValidator : public QDoubleValidator class KDEUI_EXPORT KDoubleValidator : public QDoubleValidator
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(bool acceptLocalizedNumbers READ acceptLocalizedNumbers WRITE setAcceptLocalizedNumbers) Q_PROPERTY(bool acceptLocalizedNumbers READ acceptLocalizedNumbers WRITE setAcceptLocalizedNumbers)
public: public:
/** /**
Constuct a locale-aware KDoubleValidator with default range * Constuct a locale-aware KDoubleValidator with default range
(whatever QDoubleValidator uses for that) and parent @p
parent
*/ */
explicit KDoubleValidator(QObject *parent); explicit KDoubleValidator(QObject *parent);
/** /**
Constuct a locale-aware KDoubleValidator for range [@p bottom,@p * Constuct a locale-aware KDoubleValidator for the speicified range and decimals
top] and a precision of @p decimals decimals after the decimal */
point.
*/
KDoubleValidator(double bottom, double top, int decimals, QObject *parent); KDoubleValidator(double bottom, double top, int decimals, QObject *parent);
virtual ~KDoubleValidator(); virtual ~KDoubleValidator();
/** @return whether localized numbers are accepted (default: true) */ /** @return whether localized numbers are accepted, enabled by default */
bool acceptLocalizedNumbers() const; bool acceptLocalizedNumbers() const;
/** Sets whether to accept localized numbers (default: true) */ /** Sets whether to accept localized numbers, enabled by default */
void setAcceptLocalizedNumbers(bool accept); void setAcceptLocalizedNumbers(bool accept);
private: private:
class KDoubleValidatorPrivate; class KDoubleValidatorPrivate;
KDoubleValidatorPrivate * const d; KDoubleValidatorPrivate* const d;
}; };
#endif #endif

View file

@ -85,7 +85,6 @@ KIntNumInput::KIntNumInput(QWidget* parent)
setFocusProxy(d->spinbox); setFocusProxy(d->spinbox);
setRange(INT_MIN, INT_MAX); setRange(INT_MIN, INT_MAX);
setSingleStep(1); setSingleStep(1);
setBase(10);
setValue(0); setValue(0);
} }
@ -172,16 +171,6 @@ void KIntNumInput::setAlignment(const Qt::Alignment alignment)
d->spinbox->setAlignment(alignment); d->spinbox->setAlignment(alignment);
} }
int KIntNumInput::base() const
{
return d->validator->base();
}
void KIntNumInput::setBase(int base)
{
d->validator->setBase(base);
}
bool KIntNumInput::sliderEnabled() const bool KIntNumInput::sliderEnabled() const
{ {
return d->slider->isVisible(); return d->slider->isVisible();

View file

@ -50,7 +50,6 @@ class KDEUI_EXPORT KIntNumInput : public QWidget
Q_PROPERTY(QString prefix READ prefix WRITE setPrefix) Q_PROPERTY(QString prefix READ prefix WRITE setPrefix)
Q_PROPERTY(QString specialValueText READ specialValueText WRITE setSpecialValueText) Q_PROPERTY(QString specialValueText READ specialValueText WRITE setSpecialValueText)
Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment) Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)
Q_PROPERTY(int base READ base WRITE setBase)
Q_PROPERTY(bool sliderEnabled READ sliderEnabled WRITE setSliderEnabled) Q_PROPERTY(bool sliderEnabled READ sliderEnabled WRITE setSliderEnabled)
public: public:
/** /**
@ -78,15 +77,6 @@ public:
Qt::Alignment alignment() const; Qt::Alignment alignment() const;
void setAlignment(const Qt::Alignment alignment); void setAlignment(const Qt::Alignment alignment);
/**
* @return the base in which numbers in the spin box are represented.
*/
int base() const;
/**
* Sets the base in which the numbers in the spin box are represented.
*/
void setBase(int base);
/** /**
* @return if slider is enabled. * @return if slider is enabled.
* @default disabled * @default disabled