mirror of
https://bitbucket.org/smil3y/kdelibs.git
synced 2025-02-23 18:32:49 +00:00
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:
parent
27ae4b6288
commit
f559d2b200
7 changed files with 124 additions and 308 deletions
|
@ -109,7 +109,7 @@ KInputDialogHelper::KInputDialogHelper( const QString &caption, const QString &l
|
|||
}
|
||||
|
||||
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 )
|
||||
: KDialog(parent),
|
||||
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->setSingleStep(step);
|
||||
m_intSpinBox->setValue(value);
|
||||
m_intSpinBox->setBase(base);
|
||||
layout->addWidget(m_intSpinBox);
|
||||
|
||||
layout->setMargin(0);
|
||||
|
@ -382,10 +381,10 @@ QString getMultiLineText( const QString &caption,
|
|||
}
|
||||
|
||||
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 )
|
||||
{
|
||||
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);
|
||||
|
||||
|
@ -399,13 +398,6 @@ int getInteger( const QString &caption, const QString &label,
|
|||
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 value, double minValue, double maxValue, double step, int decimals,
|
||||
bool *ok, QWidget *parent )
|
||||
|
|
|
@ -100,7 +100,7 @@ namespace KInputDialog
|
|||
* value for the spin box. minValue and maxValue are the minimum and
|
||||
* maximum allowable values the user may choose. step is the amount by
|
||||
* 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 label Text of the label for the spin box
|
||||
|
@ -108,21 +108,11 @@ namespace KInputDialog
|
|||
* @param minValue Minimum value user can input
|
||||
* @param maxValue Maximum value user can input
|
||||
* @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 parent Parent of the dialog widget
|
||||
*
|
||||
* @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,
|
||||
int value=0, int minValue=INT_MIN, int maxValue=INT_MAX,
|
||||
int step=1, bool *ok=0, QWidget *parent=0 );
|
||||
|
|
|
@ -46,7 +46,7 @@ class KInputDialogHelper : public KDialog
|
|||
KInputDialogHelper( const QString &caption, const QString &label,
|
||||
const QString &value, QWidget *parent );
|
||||
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,
|
||||
double minValue, double maxValue, double step, int decimals,
|
||||
QWidget *parent );
|
||||
|
|
|
@ -1,182 +1,29 @@
|
|||
/**********************************************************************
|
||||
**
|
||||
**
|
||||
** KIntValidator:
|
||||
** Copyright (C) 1999 Glen Parker <glenebob@nwlink.com>
|
||||
** KDoubleValidator:
|
||||
** Copyright (c) 2002 Marc Mutz <mutz@kde.org>
|
||||
**
|
||||
** This library is free software; you can redistribute it and/or
|
||||
** modify it under the terms of the GNU Library General Public
|
||||
** 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
|
||||
** 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; if not, write to the Free
|
||||
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
**
|
||||
*****************************************************************************/
|
||||
/*
|
||||
This file is part of the KDE libraries
|
||||
Copyright (C) 2024 Ivailo Monev <xakepa10@gmail.com>
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Library General Public
|
||||
License version 2, as published by the Free Software Foundation.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
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
|
||||
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||
Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include "knumvalidator.h"
|
||||
|
||||
#include <QWidget>
|
||||
#include <QString>
|
||||
|
||||
#include <klocale.h>
|
||||
#include <kglobal.h>
|
||||
#include <kdebug.h>
|
||||
|
||||
///////////////////////////////////////////////////////////////
|
||||
// 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)
|
||||
static void kAcceptLocalizedNumbers(QValidator *validator, const bool accept)
|
||||
{
|
||||
if (accept) {
|
||||
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
|
||||
{
|
||||
public:
|
||||
|
|
|
@ -1,139 +1,104 @@
|
|||
/**********************************************************************
|
||||
**
|
||||
** Copyright (C) 1999 Glen Parker <glenebob@nwlink.com>
|
||||
** Copyright (C) 2002 Marc Mutz <mutz@kde.org>
|
||||
**
|
||||
** This library is free software; you can redistribute it and/or
|
||||
** modify it under the terms of the GNU Library General Public
|
||||
** 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
|
||||
** 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; if not, write to the Free
|
||||
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
**
|
||||
*****************************************************************************/
|
||||
/*
|
||||
This file is part of the KDE libraries
|
||||
Copyright (C) 2024 Ivailo Monev <xakepa10@gmail.com>
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Library General Public
|
||||
License version 2, as published by the Free Software Foundation.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
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
|
||||
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||
Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#ifndef KNUMVALIDATOR_H
|
||||
#define KNUMVALIDATOR_H
|
||||
|
||||
#include <kdeui_export.h>
|
||||
|
||||
#include <QtGui/QValidator>
|
||||
|
||||
#include <QWidget>
|
||||
#include <QString>
|
||||
#include <QValidator>
|
||||
|
||||
/**
|
||||
* QValidator for integers.
|
||||
|
||||
This can be used by QLineEdit or subclass to provide validated
|
||||
text entry. Can be provided with a base value (default is 10), to allow
|
||||
the proper entry of hexadecimal, octal, or any other base numeric data.
|
||||
|
||||
@author Glen Parker <glenebob@nwlink.com>
|
||||
@version 0.0.1
|
||||
*/
|
||||
class KDEUI_EXPORT KIntValidator : public QValidator
|
||||
* @short A locale-aware QIntValidator
|
||||
*
|
||||
* QIntValidator extends QIntValidator to be locale-aware. That means that - subject to not being
|
||||
* disabled - the system locale thousand separator, positive and negative sign are used for
|
||||
* validation.
|
||||
*
|
||||
* @author Ivailo Monev <xakepa10@gmail.com>
|
||||
* @see KIntValidator
|
||||
**/
|
||||
class KDEUI_EXPORT KIntValidator : public QIntValidator
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(bool acceptLocalizedNumbers READ acceptLocalizedNumbers WRITE setAcceptLocalizedNumbers)
|
||||
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();
|
||||
|
||||
/**
|
||||
* Validates the text, and return the result. Does not modify the parameters.
|
||||
*/
|
||||
virtual QValidator::State validate(QString &, int &) const;
|
||||
/** @return whether localized numbers are accepted, enabled by default */
|
||||
bool acceptLocalizedNumbers() const;
|
||||
|
||||
/**
|
||||
* Fixes the text if possible, providing a valid string. The parameter may be modified.
|
||||
*/
|
||||
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 whether to accept localized numbers, enabled by default */
|
||||
void setAcceptLocalizedNumbers(bool accept);
|
||||
|
||||
/**
|
||||
* 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:
|
||||
class KIntValidatorPrivate;
|
||||
KIntValidatorPrivate * const d;
|
||||
KIntValidatorPrivate* const d;
|
||||
};
|
||||
|
||||
/**
|
||||
@short A locale-aware QDoubleValidator
|
||||
|
||||
KDoubleValidator extends QDoubleValidator to be locale-aware. That
|
||||
means that - subject to not being disabled - the system locale
|
||||
decimal point, thousand separator, positive and negative sign are
|
||||
used for validation.
|
||||
|
||||
@author Marc Mutz <mutz@kde.org>
|
||||
@see KIntValidator
|
||||
**/
|
||||
* @short A locale-aware QDoubleValidator
|
||||
*
|
||||
* KDoubleValidator extends QDoubleValidator to be locale-aware. That means that - subject to not
|
||||
* being disabled - the system locale decimal point, thousand separator, positive and negative sign
|
||||
* are used for validation.
|
||||
*
|
||||
* @author Ivailo Monev <xakepa10@gmail.com>
|
||||
* @see KIntValidator
|
||||
**/
|
||||
class KDEUI_EXPORT KDoubleValidator : public QDoubleValidator
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(bool acceptLocalizedNumbers READ acceptLocalizedNumbers WRITE setAcceptLocalizedNumbers)
|
||||
public:
|
||||
/**
|
||||
Constuct a locale-aware KDoubleValidator with default range
|
||||
(whatever QDoubleValidator uses for that) and parent @p
|
||||
parent
|
||||
* Constuct a locale-aware KDoubleValidator with default range
|
||||
*/
|
||||
explicit KDoubleValidator(QObject *parent);
|
||||
|
||||
/**
|
||||
Constuct a locale-aware KDoubleValidator for range [@p bottom,@p
|
||||
top] and a precision of @p decimals decimals after the decimal
|
||||
point.
|
||||
* Constuct a locale-aware KDoubleValidator for the speicified range and decimals
|
||||
*/
|
||||
KDoubleValidator(double bottom, double top, int decimals, QObject *parent);
|
||||
|
||||
virtual ~KDoubleValidator();
|
||||
|
||||
/** @return whether localized numbers are accepted (default: true) */
|
||||
/** @return whether localized numbers are accepted, enabled by default */
|
||||
bool acceptLocalizedNumbers() const;
|
||||
|
||||
/** Sets whether to accept localized numbers (default: true) */
|
||||
/** Sets whether to accept localized numbers, enabled by default */
|
||||
void setAcceptLocalizedNumbers(bool accept);
|
||||
|
||||
private:
|
||||
class KDoubleValidatorPrivate;
|
||||
KDoubleValidatorPrivate * const d;
|
||||
KDoubleValidatorPrivate* const d;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -85,7 +85,6 @@ KIntNumInput::KIntNumInput(QWidget* parent)
|
|||
setFocusProxy(d->spinbox);
|
||||
setRange(INT_MIN, INT_MAX);
|
||||
setSingleStep(1);
|
||||
setBase(10);
|
||||
setValue(0);
|
||||
}
|
||||
|
||||
|
@ -172,16 +171,6 @@ void KIntNumInput::setAlignment(const Qt::Alignment 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
|
||||
{
|
||||
return d->slider->isVisible();
|
||||
|
|
|
@ -50,7 +50,6 @@ class KDEUI_EXPORT KIntNumInput : public QWidget
|
|||
Q_PROPERTY(QString prefix READ prefix WRITE setPrefix)
|
||||
Q_PROPERTY(QString specialValueText READ specialValueText WRITE setSpecialValueText)
|
||||
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)
|
||||
public:
|
||||
/**
|
||||
|
@ -78,15 +77,6 @@ public:
|
|||
Qt::Alignment alignment() const;
|
||||
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.
|
||||
* @default disabled
|
||||
|
|
Loading…
Add table
Reference in a new issue