kdeui: drop KIntValidator and KDoubleValidator

by reworking KIntNumInput and KDoubleNumInput to set the locale of the
internal spin boxes (QSpinBox or QDoubleSpinBox) both of which use
either internal validator (QSpinBoxValidator for QAbstractSpinBox) or
validate via the locale

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2024-05-21 12:31:14 +03:00
parent de27e20efc
commit 46f5e8cbc2
8 changed files with 58 additions and 274 deletions

View file

@ -88,7 +88,6 @@ install(
KFileWidget KFileWidget
KDiskFreeSpaceInfo KDiskFreeSpaceInfo
KDoubleNumInput KDoubleNumInput
KDoubleValidator
KEMail KEMail
KEMailDialog KEMailDialog
KEMailSettings KEMailSettings
@ -136,7 +135,6 @@ install(
KInputDialog KInputDialog
KComponentData KComponentData
KIntNumInput KIntNumInput
KIntValidator
KJob KJob
KJobTrackerInterface KJobTrackerInterface
KJobUiDelegate KJobUiDelegate

View file

@ -1 +0,0 @@
#include "../knumvalidator.h"

View file

@ -1 +0,0 @@
#include "../knumvalidator.h"

View file

@ -155,7 +155,6 @@ set(kdeui_LIB_SRCS
util/kguiitem.cpp util/kguiitem.cpp
util/kkeyserver.cpp util/kkeyserver.cpp
util/kselectionowner.cpp util/kselectionowner.cpp
util/knumvalidator.cpp
util/kpassivepopup.cpp util/kpassivepopup.cpp
util/kstandardguiitem.cpp util/kstandardguiitem.cpp
util/kxerrorhandler.cpp util/kxerrorhandler.cpp
@ -403,7 +402,6 @@ install(
util/kkeyboardlayout.h util/kkeyboardlayout.h
util/kdebugger.h util/kdebugger.h
util/kselectionowner.h util/kselectionowner.h
util/knumvalidator.h
util/kpassivepopup.h util/kpassivepopup.h
util/kstandardguiitem.h util/kstandardguiitem.h
util/kxerrorhandler.h util/kxerrorhandler.h

View file

@ -1,117 +0,0 @@
/*
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 "klocale.h"
#include "kglobal.h"
#include "kdebug.h"
static void kAcceptLocalizedNumbers(QValidator *validator, const bool accept)
{
if (accept) {
validator->setLocale(KGlobal::locale()->toLocale());
} else {
validator->setLocale(QLocale::c());
}
}
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:
KDoubleValidatorPrivate()
: acceptLocalizedNumbers(true)
{
}
bool acceptLocalizedNumbers;
};
KDoubleValidator::KDoubleValidator(QObject *parent)
: QDoubleValidator(parent),
d(new KDoubleValidatorPrivate())
{
kAcceptLocalizedNumbers(this, true);
}
KDoubleValidator::KDoubleValidator(double bottom, double top, int decimals, QObject *parent)
: QDoubleValidator(bottom, top, decimals, parent),
d(new KDoubleValidatorPrivate())
{
kAcceptLocalizedNumbers(this, true);
}
KDoubleValidator::~KDoubleValidator()
{
delete d;
}
bool KDoubleValidator::acceptLocalizedNumbers() const
{
return d->acceptLocalizedNumbers;
}
void KDoubleValidator::setAcceptLocalizedNumbers(bool accept)
{
d->acceptLocalizedNumbers = accept;
kAcceptLocalizedNumbers(this, accept);
}
#include "moc_knumvalidator.cpp"

View file

@ -1,103 +0,0 @@
/*
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 <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 KDoubleValidator
**/
class KDEUI_EXPORT KIntValidator : public QIntValidator
{
Q_OBJECT
Q_PROPERTY(bool acceptLocalizedNumbers READ acceptLocalizedNumbers WRITE setAcceptLocalizedNumbers)
public:
/**
* Constuct a locale-aware KIntValidator with default range
*/
explicit KIntValidator(QObject *parent);
/**
* Constuct a locale-aware KIntValidator for the speicified range and decimals
*/
KIntValidator(int bottom, int top, QObject *parent);
virtual ~KIntValidator();
/** @return whether localized numbers are accepted, enabled by default */
bool acceptLocalizedNumbers() const;
/** Sets whether to accept localized numbers, enabled by default */
void setAcceptLocalizedNumbers(bool accept);
private:
class KIntValidatorPrivate;
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 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
*/
explicit KDoubleValidator(QObject *parent);
/**
* 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, enabled by default */
bool acceptLocalizedNumbers() const;
/** Sets whether to accept localized numbers, enabled by default */
void setAcceptLocalizedNumbers(bool accept);
private:
class KDoubleValidatorPrivate;
KDoubleValidatorPrivate* const d;
};
#endif

View file

@ -18,20 +18,33 @@
*/ */
#include "knuminput.h" #include "knuminput.h"
#include "knumvalidator.h" #include "kglobal.h"
#include "klocale.h"
#include "kdebug.h" #include "kdebug.h"
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QSpinBox> #include <QSpinBox>
#include <QDoubleSpinBox> #include <QDoubleSpinBox>
#include <QSlider> #include <QSlider>
#include <QEvent>
static void setupSpinBox(QSpinBox *spinbox, const int value)
{
spinbox->setLocale(KGlobal::locale()->toLocale());
spinbox->setValue(value);
}
static void setupDoubleSpinBox(QDoubleSpinBox *spinbox, const double value)
{
spinbox->setLocale(KGlobal::locale()->toLocale());
spinbox->setValue(value);
}
class KIntNumInputPrivate class KIntNumInputPrivate
{ {
public: public:
KIntNumInputPrivate() KIntNumInputPrivate()
: validator(nullptr), : slider(nullptr),
slider(nullptr),
spinbox(nullptr) spinbox(nullptr)
{ {
} }
@ -46,7 +59,6 @@ public:
slider->setValue(value); slider->setValue(value);
} }
KIntValidator* validator;
QSlider* slider; QSlider* slider;
QSpinBox* spinbox; QSpinBox* spinbox;
KLocalizedString suffix; KLocalizedString suffix;
@ -56,7 +68,6 @@ KIntNumInput::KIntNumInput(QWidget* parent)
: QWidget(parent), : QWidget(parent),
d(new KIntNumInputPrivate()) d(new KIntNumInputPrivate())
{ {
d->validator = new KIntValidator(this);
QHBoxLayout* hboxlayout = new QHBoxLayout(this); QHBoxLayout* hboxlayout = new QHBoxLayout(this);
hboxlayout->setMargin(0); hboxlayout->setMargin(0);
d->slider = new QSlider(Qt::Horizontal, this); d->slider = new QSlider(Qt::Horizontal, this);
@ -83,6 +94,9 @@ KIntNumInput::KIntNumInput(QWidget* parent)
setLayout(hboxlayout); setLayout(hboxlayout);
setFocusProxy(d->spinbox); setFocusProxy(d->spinbox);
setupSpinBox(d->spinbox, d->spinbox->value());
setRange(INT_MIN, INT_MAX); setRange(INT_MIN, INT_MAX);
setSingleStep(1); setSingleStep(1);
setValue(0); setValue(0);
@ -95,7 +109,6 @@ KIntNumInput::~KIntNumInput()
void KIntNumInput::setRange(int min, int max) void KIntNumInput::setRange(int min, int max)
{ {
d->validator->setRange(min, max);
d->slider->setRange(min, max); d->slider->setRange(min, max);
d->spinbox->setRange(min, max); d->spinbox->setRange(min, max);
} }
@ -112,7 +125,6 @@ int KIntNumInput::minimum() const
void KIntNumInput::setMinimum(int min) void KIntNumInput::setMinimum(int min)
{ {
d->validator->setRange(min, maximum());
d->slider->setMinimum(min); d->slider->setMinimum(min);
d->spinbox->setMinimum(min); d->spinbox->setMinimum(min);
} }
@ -124,7 +136,6 @@ int KIntNumInput::maximum() const
void KIntNumInput::setMaximum(int max) void KIntNumInput::setMaximum(int max)
{ {
d->validator->setRange(minimum(), max);
d->slider->setMaximum(max); d->slider->setMaximum(max);
d->spinbox->setMaximum(max); d->spinbox->setMaximum(max);
} }
@ -187,16 +198,6 @@ void KIntNumInput::setSteps(int single, int page)
d->slider->setPageStep(page); d->slider->setPageStep(page);
} }
QValidator::State KIntNumInput::validate(QString &input, int &pos) const
{
return d->validator->validate(input, pos);
}
void KIntNumInput::fixup(QString &input) const
{
d->validator->fixup(input);
}
void KIntNumInput::setValue(int value) void KIntNumInput::setValue(int value)
{ {
d->spinbox->setValue(value); d->spinbox->setValue(value);
@ -219,13 +220,25 @@ void KIntNumInput::setPrefix(const QString &prefix)
d->spinbox->setPrefix(prefix); d->spinbox->setPrefix(prefix);
} }
void KIntNumInput::changeEvent(QEvent *event)
{
switch (event->type()) {
case QEvent::LocaleChange:
case QEvent::LanguageChange: {
setupSpinBox(d->spinbox, d->spinbox->value());
break;
}
default: {
break;
}
}
}
class KDoubleNumInputPrivate class KDoubleNumInputPrivate
{ {
public: public:
KDoubleNumInputPrivate() KDoubleNumInputPrivate()
: validator(nullptr), : slider(nullptr),
slider(nullptr),
spinbox(nullptr) spinbox(nullptr)
{ {
} }
@ -245,7 +258,6 @@ public:
spinbox->setValue(value); spinbox->setValue(value);
} }
KDoubleValidator* validator;
QSlider* slider; QSlider* slider;
QDoubleSpinBox* spinbox; QDoubleSpinBox* spinbox;
KLocalizedString suffix; KLocalizedString suffix;
@ -255,7 +267,6 @@ KDoubleNumInput::KDoubleNumInput(QWidget *parent)
: QWidget(parent), : QWidget(parent),
d(new KDoubleNumInputPrivate()) d(new KDoubleNumInputPrivate())
{ {
d->validator = new KDoubleValidator(this);
QHBoxLayout* hboxlayout = new QHBoxLayout(this); QHBoxLayout* hboxlayout = new QHBoxLayout(this);
hboxlayout->setMargin(0); hboxlayout->setMargin(0);
d->slider = new QSlider(Qt::Horizontal, this); d->slider = new QSlider(Qt::Horizontal, this);
@ -282,6 +293,9 @@ KDoubleNumInput::KDoubleNumInput(QWidget *parent)
setLayout(hboxlayout); setLayout(hboxlayout);
setFocusProxy(d->spinbox); setFocusProxy(d->spinbox);
setupDoubleSpinBox(d->spinbox, d->spinbox->value());
setRange(0.0, 9999.0); setRange(0.0, 9999.0);
setSingleStep(0.01); setSingleStep(0.01);
setDecimals(2); setDecimals(2);
@ -295,7 +309,6 @@ KDoubleNumInput::~KDoubleNumInput()
void KDoubleNumInput::setRange(double min, double max) void KDoubleNumInput::setRange(double min, double max)
{ {
d->validator->setRange(min, max, decimals());
d->slider->setRange(qRound(min), qRound(max)); d->slider->setRange(qRound(min), qRound(max));
d->spinbox->setRange(min, max); d->spinbox->setRange(min, max);
} }
@ -312,7 +325,6 @@ double KDoubleNumInput::minimum() const
void KDoubleNumInput::setMinimum(double min) void KDoubleNumInput::setMinimum(double min)
{ {
d->validator->setRange(min, maximum(), decimals());
d->slider->setMinimum(min); d->slider->setMinimum(min);
d->spinbox->setMinimum(min); d->spinbox->setMinimum(min);
} }
@ -324,7 +336,6 @@ double KDoubleNumInput::maximum() const
void KDoubleNumInput::setMaximum(double max) void KDoubleNumInput::setMaximum(double max)
{ {
d->validator->setRange(minimum(), max, decimals());
d->slider->setMaximum(max); d->slider->setMaximum(max);
d->spinbox->setMaximum(max); d->spinbox->setMaximum(max);
} }
@ -398,16 +409,6 @@ void KDoubleNumInput::setSteps(int single, int page)
d->slider->setPageStep(page); d->slider->setPageStep(page);
} }
QValidator::State KDoubleNumInput::validate(QString &input, int &pos) const
{
return d->validator->validate(input, pos);
}
void KDoubleNumInput::fixup(QString &input) const
{
d->validator->fixup(input);
}
void KDoubleNumInput::setValue(double value) void KDoubleNumInput::setValue(double value)
{ {
d->spinbox->setValue(value); d->spinbox->setValue(value);
@ -430,4 +431,18 @@ void KDoubleNumInput::setPrefix(const QString &prefix)
d->spinbox->setPrefix(prefix); d->spinbox->setPrefix(prefix);
} }
void KDoubleNumInput::changeEvent(QEvent *event)
{
switch (event->type()) {
case QEvent::LocaleChange:
case QEvent::LanguageChange: {
setupDoubleSpinBox(d->spinbox, d->spinbox->value());
break;
}
default: {
break;
}
}
}
#include "moc_knuminput.cpp" #include "moc_knuminput.cpp"

View file

@ -24,7 +24,6 @@
#include <klocalizedstring.h> #include <klocalizedstring.h>
#include <QWidget> #include <QWidget>
#include <QValidator>
class KIntNumInputPrivate; class KIntNumInputPrivate;
class KDoubleNumInputPrivate; class KDoubleNumInputPrivate;
@ -96,12 +95,6 @@ public:
*/ */
void setSteps(int single, int page); void setSteps(int single, int page);
/**
* Validation overrides
*/
virtual QValidator::State validate(QString &input, int &pos) const;
virtual void fixup(QString &input) const;
public Q_SLOTS: public Q_SLOTS:
/** /**
* Spin box and slider proxies * Spin box and slider proxies
@ -115,6 +108,10 @@ Q_SIGNALS:
void valueChanged(int); void valueChanged(int);
void editingFinished(); void editingFinished();
protected:
// QWidget reimplementation
virtual void changeEvent(QEvent *event);
private: private:
friend KIntNumInputPrivate; friend KIntNumInputPrivate;
KIntNumInputPrivate* const d; KIntNumInputPrivate* const d;
@ -201,12 +198,6 @@ public:
*/ */
void setSteps(int single, int page); void setSteps(int single, int page);
/**
* Validation overrides
*/
virtual QValidator::State validate(QString &input, int &pos) const;
virtual void fixup(QString &input) const;
public Q_SLOTS: public Q_SLOTS:
/** /**
* Spin box and slider proxies * Spin box and slider proxies
@ -220,6 +211,10 @@ Q_SIGNALS:
void valueChanged(double); void valueChanged(double);
void editingFinished(); void editingFinished();
protected:
// QWidget reimplementation
virtual void changeEvent(QEvent *event);
private: private:
friend KDoubleNumInputPrivate; friend KDoubleNumInputPrivate;
KDoubleNumInputPrivate* const d; KDoubleNumInputPrivate* const d;