2024-05-20 14:07:12 +03:00
|
|
|
/*
|
|
|
|
This file is part of the KDE libraries
|
|
|
|
Copyright (C) 2024 Ivailo Monev <xakepa10@gmail.com>
|
2014-11-13 01:04:59 +02:00
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
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.
|
2014-11-13 01:04:59 +02:00
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
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.
|
2014-11-13 01:04:59 +02:00
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
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.
|
|
|
|
*/
|
2014-11-13 01:04:59 +02:00
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
#include "knuminput.h"
|
|
|
|
#include "knumvalidator.h"
|
|
|
|
#include "kdebug.h"
|
2014-11-13 01:04:59 +02:00
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
#include <QHBoxLayout>
|
|
|
|
#include <QSpinBox>
|
|
|
|
#include <QDoubleSpinBox>
|
|
|
|
#include <QSlider>
|
2014-11-13 01:04:59 +02:00
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
class KIntNumInputPrivate
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
|
|
|
public:
|
2024-05-20 14:07:12 +03:00
|
|
|
KIntNumInputPrivate()
|
|
|
|
: validator(nullptr),
|
|
|
|
slider(nullptr),
|
|
|
|
spinbox(nullptr)
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
void _k_updateSuffix(int value)
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
if (suffix.isEmpty()) {
|
|
|
|
spinbox->setSuffix(QString());
|
|
|
|
} else {
|
|
|
|
spinbox->setSuffix(suffix.subs(value).toString());
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
KIntValidator* validator;
|
|
|
|
QSlider* slider;
|
|
|
|
QSpinBox* spinbox;
|
|
|
|
KLocalizedString suffix;
|
2014-11-13 01:04:59 +02:00
|
|
|
};
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
KIntNumInput::KIntNumInput(QWidget* parent)
|
|
|
|
: QWidget(parent),
|
|
|
|
d(new KIntNumInputPrivate())
|
|
|
|
{
|
|
|
|
d->validator = new KIntValidator(this);
|
|
|
|
QHBoxLayout* hboxlayout = new QHBoxLayout(this);
|
|
|
|
d->slider = new QSlider(this);
|
|
|
|
d->slider->setVisible(false);
|
|
|
|
hboxlayout->addWidget(d->slider);
|
|
|
|
d->spinbox = new QSpinBox(this);
|
|
|
|
connect(
|
|
|
|
d->spinbox, SIGNAL(valueChanged(int)),
|
|
|
|
this, SIGNAL(valueChanged(int))
|
|
|
|
);
|
|
|
|
connect(
|
|
|
|
d->spinbox, SIGNAL(valueChanged(int)),
|
|
|
|
this, SLOT(_k_updateSuffix(int))
|
|
|
|
);
|
|
|
|
connect(
|
|
|
|
d->spinbox, SIGNAL(editingFinished()),
|
|
|
|
this, SIGNAL(editingFinished())
|
|
|
|
);
|
|
|
|
hboxlayout->addWidget(d->spinbox);
|
|
|
|
setLayout(hboxlayout);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
KIntNumInput::~KIntNumInput()
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
2024-05-20 14:07:12 +03:00
|
|
|
void KIntNumInput::setRange(int min, int max)
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
d->spinbox->setRange(min, max);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
int KIntNumInput::value() const
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
return d->spinbox->value();
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
int KIntNumInput::minimum() const
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
return d->spinbox->minimum();
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void KIntNumInput::setMinimum(int min)
|
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
d->spinbox->setMinimum(min);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
int KIntNumInput::maximum() const
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
return d->spinbox->maximum();
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void KIntNumInput::setMaximum(int max)
|
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
d->spinbox->setMaximum(max);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int KIntNumInput::singleStep() const
|
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
return d->spinbox->singleStep();
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void KIntNumInput::setSingleStep(int singleStep)
|
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
d->spinbox->setSingleStep(singleStep);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString KIntNumInput::suffix() const
|
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
return d->spinbox->suffix();
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString KIntNumInput::prefix() const
|
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
return d->spinbox->prefix();
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString KIntNumInput::specialValueText() const
|
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
return d->spinbox->specialValueText();
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
void KIntNumInput::setSpecialValueText(const QString &text)
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
d->spinbox->setSpecialValueText(text);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
int KIntNumInput::base() const
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
return d->validator->base();
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
void KIntNumInput::setBase(int base)
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
d->validator->setBase(base);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
bool KIntNumInput::sliderEnabled() const
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
return d->slider->isVisible();
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
void KIntNumInput::setSliderEnabled(bool enabled)
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
d->slider->setVisible(enabled);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
QValidator::State KIntNumInput::validate(QString &input, int &pos) const
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
return d->validator->validate(input, pos);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
void KIntNumInput::fixup(QString &input) const
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
d->validator->fixup(input);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
void KIntNumInput::setValue(int value)
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
d->spinbox->setValue(value);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
void KIntNumInput::setSuffix(const KLocalizedString &suffix)
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
d->suffix = suffix;
|
|
|
|
d->_k_updateSuffix(d->spinbox->value());
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
void KIntNumInput::setSuffix(const QString &suffix)
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
d->suffix = KLocalizedString();
|
|
|
|
d->spinbox->setSuffix(suffix);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
void KIntNumInput::setPrefix(const QString &prefix)
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
d->spinbox->setPrefix(prefix);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
class KDoubleNumInputPrivate
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
public:
|
|
|
|
KDoubleNumInputPrivate()
|
|
|
|
: validator(nullptr),
|
|
|
|
slider(nullptr),
|
|
|
|
spinbox(nullptr)
|
|
|
|
{
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
void _k_updateSuffix(double value)
|
|
|
|
{
|
|
|
|
if (suffix.isEmpty()) {
|
|
|
|
spinbox->setSuffix(QString());
|
|
|
|
} else {
|
|
|
|
spinbox->setSuffix(suffix.subs(value).toString());
|
|
|
|
}
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
KDoubleValidator* validator;
|
|
|
|
QSlider* slider;
|
|
|
|
QDoubleSpinBox* spinbox;
|
|
|
|
KLocalizedString suffix;
|
|
|
|
};
|
2014-11-13 01:04:59 +02:00
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
KDoubleNumInput::KDoubleNumInput(QWidget *parent)
|
|
|
|
: QWidget(parent),
|
|
|
|
d(new KDoubleNumInputPrivate())
|
|
|
|
{
|
|
|
|
d->validator = new KDoubleValidator(this);
|
|
|
|
QHBoxLayout* hboxlayout = new QHBoxLayout(this);
|
|
|
|
d->slider = new QSlider(this);
|
|
|
|
d->slider->setVisible(false);
|
|
|
|
hboxlayout->addWidget(d->slider);
|
|
|
|
d->spinbox = new QDoubleSpinBox(this);
|
|
|
|
connect(
|
|
|
|
d->spinbox, SIGNAL(valueChanged(double)),
|
|
|
|
this, SIGNAL(valueChanged(double))
|
|
|
|
);
|
|
|
|
connect(
|
|
|
|
d->spinbox, SIGNAL(valueChanged(double)),
|
|
|
|
this, SLOT(_k_updateSuffix(double))
|
|
|
|
);
|
|
|
|
connect(
|
|
|
|
d->spinbox, SIGNAL(editingFinished()),
|
|
|
|
this, SIGNAL(editingFinished())
|
|
|
|
);
|
|
|
|
hboxlayout->addWidget(d->spinbox);
|
|
|
|
setLayout(hboxlayout);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
KDoubleNumInput::~KDoubleNumInput()
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
delete d;
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
void KDoubleNumInput::setRange(double min, double max)
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
d->spinbox->setRange(min, max);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
double KDoubleNumInput::value() const
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
return d->spinbox->value();
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
double KDoubleNumInput::minimum() const
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
return d->spinbox->minimum();
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void KDoubleNumInput::setMinimum(double min)
|
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
d->spinbox->setMinimum(min);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
double KDoubleNumInput::maximum() const
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
return d->spinbox->maximum();
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void KDoubleNumInput::setMaximum(double max)
|
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
d->spinbox->setMaximum(max);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
double KDoubleNumInput::singleStep() const
|
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
return d->spinbox->singleStep();
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void KDoubleNumInput::setSingleStep(double singleStep)
|
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
d->spinbox->setSingleStep(singleStep);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
QString KDoubleNumInput::suffix() const
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
return d->spinbox->suffix();
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
QString KDoubleNumInput::prefix() const
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
return d->spinbox->prefix();
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
QString KDoubleNumInput::specialValueText() const
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
return d->spinbox->specialValueText();
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
void KDoubleNumInput::setSpecialValueText(const QString &text)
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
d->spinbox->setSpecialValueText(text);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
int KDoubleNumInput::decimals() const
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
return d->spinbox->decimals();
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
void KDoubleNumInput::setDecimals(int decimals)
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
d->spinbox->setDecimals(decimals);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
bool KDoubleNumInput::sliderEnabled() const
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
return d->slider->isVisible();
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
void KDoubleNumInput::setSliderEnabled(bool enabled)
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
d->slider->setVisible(enabled);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
QValidator::State KDoubleNumInput::validate(QString &input, int &pos) const
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
return d->validator->validate(input, pos);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
void KDoubleNumInput::fixup(QString &input) const
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
d->validator->fixup(input);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
void KDoubleNumInput::setValue(double value)
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
d->spinbox->setValue(value);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
void KDoubleNumInput::setSuffix(const KLocalizedString &suffix)
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
d->suffix = suffix;
|
|
|
|
d->_k_updateSuffix(d->spinbox->value());
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
void KDoubleNumInput::setSuffix(const QString &suffix)
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2024-05-20 14:07:12 +03:00
|
|
|
d->suffix = KLocalizedString();
|
|
|
|
d->spinbox->setSuffix(suffix);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-20 14:07:12 +03:00
|
|
|
void KDoubleNumInput::setPrefix(const QString &prefix)
|
|
|
|
{
|
|
|
|
d->spinbox->setPrefix(prefix);
|
|
|
|
}
|
2014-11-13 01:04:59 +02:00
|
|
|
|
2015-02-27 07:40:26 +00:00
|
|
|
#include "moc_knuminput.cpp"
|