123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465 |
- /*=========================================================================
- Library: CTK
- Copyright (c) Kitware Inc.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0.txt
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- =========================================================================*/
- // CTK includes
- #include "ctkDoubleSpinBox.h"
- #include "ctkUtils.h"
- #include "ctkPimpl.h"
- #include <QDebug>
- // Qt includes
- #include <QDoubleSpinBox>
- #include <QEvent>
- #include <QHBoxLayout>
- #include <QKeyEvent>
- #include <QShortcut>
- #include <QSizePolicy>
- #include <QVariant>
- //-----------------------------------------------------------------------------
- class ctkDoubleSpinBoxPrivate
- {
- Q_DECLARE_PUBLIC(ctkDoubleSpinBox);
- protected:
- ctkDoubleSpinBox* const q_ptr;
- public:
- ctkDoubleSpinBoxPrivate(ctkDoubleSpinBox& object);
- QDoubleSpinBox* SpinBox;
- ctkDoubleSpinBox::SetMode Mode;
- int DefaultDecimals;
- ctkDoubleSpinBox::DecimalsOptions DOption;
- void init();
- // Compare two double previously rounded according to the number of decimals
- bool compare(double x1, double x2) const;
- // Set the number of decimals of the spinbox and emit the signal
- // No check if they are the same.
- void setDecimals(int dec);
- };
- //-----------------------------------------------------------------------------
- ctkDoubleSpinBoxPrivate::ctkDoubleSpinBoxPrivate(ctkDoubleSpinBox& object) : q_ptr(&object)
- {
- qRegisterMetaType<ctkDoubleSpinBox::SetMode>("ctkDoubleSpinBox::SetMode");
- qRegisterMetaType<ctkDoubleSpinBox::DecimalsOptions>("ctkDoubleSpinBox::DecimalsOption");
- this->SpinBox = 0;
- this->Mode = ctkDoubleSpinBox::SetIfDifferent;
- this->DefaultDecimals = 2;
- this->DOption = ctkDoubleSpinBox::UseShortcuts;
- }
- //-----------------------------------------------------------------------------
- void ctkDoubleSpinBoxPrivate::init()
- {
- Q_Q(ctkDoubleSpinBox);
- this->SpinBox = new QDoubleSpinBox(q);
- QObject::connect(this->SpinBox, SIGNAL(valueChanged(double)),
- q, SIGNAL(valueChanged(double)));
- QObject::connect(this->SpinBox, SIGNAL(valueChanged(const QString&)),
- q, SIGNAL(valueChanged(const QString &)));
- QObject::connect(this->SpinBox, SIGNAL(editingFinished()),
- q, SIGNAL(editingFinished()));
- QHBoxLayout* l = new QHBoxLayout(q);
- l->addWidget(this->SpinBox);
- l->setContentsMargins(0,0,0,0);
- q->setLayout(l);
- q->setSizePolicy(QSizePolicy(QSizePolicy::Minimum,
- QSizePolicy::Fixed, QSizePolicy::ButtonBox));
- this->SpinBox->installEventFilter(q);
- }
- //-----------------------------------------------------------------------------
- bool ctkDoubleSpinBoxPrivate::compare(double x1, double x2) const
- {
- Q_Q(const ctkDoubleSpinBox);
- return q->round(x1) == q->round(x2);
- }
- //-----------------------------------------------------------------------------
- void ctkDoubleSpinBoxPrivate::setDecimals(int dec)
- {
- Q_Q(ctkDoubleSpinBox);
- this->SpinBox->setDecimals(dec);
- emit q->decimalsChanged(dec);
- }
- //-----------------------------------------------------------------------------
- ctkDoubleSpinBox::ctkDoubleSpinBox(QWidget* newParent)
- : QWidget(newParent)
- , d_ptr(new ctkDoubleSpinBoxPrivate(*this))
- {
- Q_D(ctkDoubleSpinBox);
- d->init();
- }
- //-----------------------------------------------------------------------------
- ctkDoubleSpinBox::ctkDoubleSpinBox(ctkDoubleSpinBox::SetMode mode, QWidget* newParent)
- : QWidget(newParent)
- , d_ptr(new ctkDoubleSpinBoxPrivate(*this))
- {
- Q_D(ctkDoubleSpinBox);
- d->init();
- this->setSetMode(mode);
- }
- //-----------------------------------------------------------------------------
- double ctkDoubleSpinBox::value() const
- {
- Q_D(const ctkDoubleSpinBox);
- return d->SpinBox->value();
- }
- //-----------------------------------------------------------------------------
- double ctkDoubleSpinBox::displayedValue() const
- {
- return this->round(this->value());
- }
- //-----------------------------------------------------------------------------
- QString ctkDoubleSpinBox::text() const
- {
- Q_D(const ctkDoubleSpinBox);
- return d->SpinBox->text();
- }
- //-----------------------------------------------------------------------------
- QString ctkDoubleSpinBox::cleanText() const
- {
- Q_D(const ctkDoubleSpinBox);
- return d->SpinBox->cleanText();
- }
- //-----------------------------------------------------------------------------
- Qt::Alignment ctkDoubleSpinBox::alignment() const
- {
- Q_D(const ctkDoubleSpinBox);
- return d->SpinBox->alignment();
- }
- //-----------------------------------------------------------------------------
- void ctkDoubleSpinBox::setAlignment(Qt::Alignment flag)
- {
- Q_D(const ctkDoubleSpinBox);
- if (d->Mode == ctkDoubleSpinBox::SetIfDifferent && flag == d->SpinBox->alignment())
- {
- return;
- }
- d->SpinBox->setAlignment(flag);
- }
- //-----------------------------------------------------------------------------
- void ctkDoubleSpinBox::setFrame(bool frame)
- {
- Q_D(const ctkDoubleSpinBox);
- if (d->Mode == ctkDoubleSpinBox::SetIfDifferent && frame == d->SpinBox->hasFrame())
- {
- return;
- }
- d->SpinBox->setFrame(frame);
- }
- //-----------------------------------------------------------------------------
- bool ctkDoubleSpinBox::hasFrame() const
- {
- Q_D(const ctkDoubleSpinBox);
- return d->SpinBox->hasFrame();
- }
- //-----------------------------------------------------------------------------
- QString ctkDoubleSpinBox::prefix() const
- {
- Q_D(const ctkDoubleSpinBox);
- return d->SpinBox->prefix();
- }
- //-----------------------------------------------------------------------------
- void ctkDoubleSpinBox::setPrefix(const QString &prefix)
- {
- Q_D(const ctkDoubleSpinBox);
- if (d->Mode == ctkDoubleSpinBox::SetIfDifferent && prefix == d->SpinBox->prefix())
- {
- return;
- }
- #if QT_VERSION < 0x040800
- /// Setting the prefix doesn't recompute the sizehint, do it manually here:
- /// See: http://bugreports.qt.nokia.com/browse/QTBUG-9530
- d->SpinBox->setRange(d->SpinBox->minimum(), d->SpinBox->maximum());
- #endif
- d->SpinBox->setPrefix(prefix);
- }
- //-----------------------------------------------------------------------------
- QString ctkDoubleSpinBox::suffix() const
- {
- Q_D(const ctkDoubleSpinBox);
- return d->SpinBox->suffix();
- }
- //-----------------------------------------------------------------------------
- void ctkDoubleSpinBox::setSuffix(const QString &suffix)
- {
- Q_D(const ctkDoubleSpinBox);
- if (d->Mode == ctkDoubleSpinBox::SetIfDifferent && suffix == d->SpinBox->suffix())
- {
- return;
- }
- #if QT_VERSION < 0x040800
- /// Setting the suffix doesn't recompute the sizehint, do it manually here:
- /// See: http://bugreports.qt.nokia.com/browse/QTBUG-9530
- d->SpinBox->setRange(d->SpinBox->minimum(), d->SpinBox->maximum());
- #endif
- d->SpinBox->setSuffix(suffix);
- }
- //-----------------------------------------------------------------------------
- double ctkDoubleSpinBox::singleStep() const
- {
- Q_D(const ctkDoubleSpinBox);
- return d->SpinBox->singleStep();
- }
- //-----------------------------------------------------------------------------
- void ctkDoubleSpinBox::setSingleStep(double step)
- {
- Q_D(ctkDoubleSpinBox);
- if (d->Mode == ctkDoubleSpinBox::SetIfDifferent
- && d->compare(step, this->singleStep()))
- {
- return;
- }
- d->SpinBox->setSingleStep(step);
- }
- //-----------------------------------------------------------------------------
- double ctkDoubleSpinBox::minimum() const
- {
- Q_D(const ctkDoubleSpinBox);
- return d->SpinBox->minimum();
- }
- //-----------------------------------------------------------------------------
- void ctkDoubleSpinBox::setMinimum(double min)
- {
- Q_D(ctkDoubleSpinBox);
- if (d->Mode == ctkDoubleSpinBox::SetIfDifferent
- && d->compare(min, this->minimum()))
- {
- return;
- }
- d->SpinBox->setMinimum(min);
- }
- //-----------------------------------------------------------------------------
- double ctkDoubleSpinBox::maximum() const
- {
- Q_D(const ctkDoubleSpinBox);
- return d->SpinBox->maximum();
- }
- //-----------------------------------------------------------------------------
- void ctkDoubleSpinBox::setMaximum(double max)
- {
- Q_D(ctkDoubleSpinBox);
- if (d->Mode == ctkDoubleSpinBox::SetIfDifferent
- && d->compare(max, this->maximum()))
- {
- return;
- }
- d->SpinBox->setMaximum(max);
- }
- //-----------------------------------------------------------------------------
- void ctkDoubleSpinBox::setRange(double min, double max)
- {
- Q_D(ctkDoubleSpinBox);
- if (d->Mode == ctkDoubleSpinBox::SetIfDifferent
- && d->compare(max, this->maximum()) && d->compare(min, this->minimum()))
- {
- return;
- }
- d->SpinBox->setRange(min, max);
- }
- //-----------------------------------------------------------------------------
- int ctkDoubleSpinBox::decimals() const
- {
- Q_D(const ctkDoubleSpinBox);
- return d->SpinBox->decimals();
- }
- //-----------------------------------------------------------------------------
- void ctkDoubleSpinBox::setDecimals(int dec)
- {
- Q_D(ctkDoubleSpinBox);
- dec = qMax(0, dec);
- if (d->Mode == ctkDoubleSpinBox::SetIfDifferent && dec == this->decimals())
- {
- return;
- }
- d->DefaultDecimals = dec;
- d->setDecimals(d->DefaultDecimals);
- }
- //-----------------------------------------------------------------------------
- double ctkDoubleSpinBox::round(double value) const
- {
- Q_D(const ctkDoubleSpinBox);
- return QString::number(value, 'f', d->SpinBox->decimals()).toDouble();
- }
- //-----------------------------------------------------------------------------
- QDoubleSpinBox* ctkDoubleSpinBox::spinBox() const
- {
- Q_D(const ctkDoubleSpinBox);
- return d->SpinBox;
- }
- //-----------------------------------------------------------------------------
- void ctkDoubleSpinBox::setValue(double value)
- {
- Q_D(ctkDoubleSpinBox);
- if (d->Mode == ctkDoubleSpinBox::SetIfDifferent)
- {
- this->setValueIfDifferent(value);
- }
- else
- {
- this->setValueAlways(value);
- }
- }
- //-----------------------------------------------------------------------------
- void ctkDoubleSpinBox::setValueIfDifferent(double value)
- {
- Q_D(ctkDoubleSpinBox);
- if (! d->compare(this->value(), value))
- {
- d->SpinBox->setValue(value);
- }
- }
- //-----------------------------------------------------------------------------
- void ctkDoubleSpinBox::setValueAlways(double value)
- {
- Q_D(const ctkDoubleSpinBox);
- d->SpinBox->setValue(value);
- }
- //-----------------------------------------------------------------------------
- void ctkDoubleSpinBox::stepUp()
- {
- Q_D(const ctkDoubleSpinBox);
- d->SpinBox->stepUp();
- }
- //-----------------------------------------------------------------------------
- void ctkDoubleSpinBox::stepDown()
- {
- Q_D(const ctkDoubleSpinBox);
- d->SpinBox->stepDown();
- }
- //-----------------------------------------------------------------------------
- ctkDoubleSpinBox::SetMode ctkDoubleSpinBox::setMode() const
- {
- Q_D(const ctkDoubleSpinBox);
- return d->Mode;
- }
- //-----------------------------------------------------------------------------
- void ctkDoubleSpinBox::setSetMode(ctkDoubleSpinBox::SetMode newMode)
- {
- Q_D(ctkDoubleSpinBox);
- d->Mode = newMode;
- }
- //-----------------------------------------------------------------------------
- ctkDoubleSpinBox::DecimalsOptions ctkDoubleSpinBox::decimalsOption()
- {
- Q_D(const ctkDoubleSpinBox);
- return d->DOption;
- }
- //-----------------------------------------------------------------------------
- void ctkDoubleSpinBox::setDecimalsOption(ctkDoubleSpinBox::DecimalsOptions option)
- {
- Q_D(ctkDoubleSpinBox);
- if (d->Mode == ctkDoubleSpinBox::SetIfDifferent && option == d->DOption)
- {
- return;
- }
- d->DOption = option;
- }
- //-----------------------------------------------------------------------------
- bool ctkDoubleSpinBox::eventFilter(QObject* obj, QEvent* event)
- {
- Q_D(ctkDoubleSpinBox);
- if (d->DOption & ctkDoubleSpinBox::UseShortcuts &&
- obj == d->SpinBox && event->type() == QEvent::KeyPress)
- {
- QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
- Q_ASSERT(keyEvent);
- if (keyEvent->modifiers() & Qt::ControlModifier)
- {
- if (keyEvent->key() == Qt::Key_Plus
- || keyEvent->key() == Qt::Key_Equal)
- {
- d->setDecimals(this->decimals() + 1);
- return true;
- }
- else if (keyEvent->key() == Qt::Key_Minus)
- {
- d->setDecimals(this->decimals() - 1);
- return true;
- }
- else if (keyEvent->key() == Qt::Key_0)
- {
- d->setDecimals(d->DefaultDecimals);
- return true;
- }
- }
- return QWidget::eventFilter(obj, event);
- }
- else
- {
- // pass the event on to the parent class
- return QWidget::eventFilter(obj, event);
- }
- }
|