| 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 "ctkSpinBox.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 ctkSpinBoxPrivate
 
- {
 
-   Q_DECLARE_PUBLIC(ctkSpinBox);
 
- protected:
 
-   ctkSpinBox* const q_ptr;
 
- public:
 
-   ctkSpinBoxPrivate(ctkSpinBox& object);
 
-   QDoubleSpinBox* SpinBox;
 
-   ctkSpinBox::SetMode Mode;
 
-   int DefaultDecimals;
 
-   ctkSpinBox::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);
 
- };
 
- //-----------------------------------------------------------------------------
 
- ctkSpinBoxPrivate::ctkSpinBoxPrivate(ctkSpinBox& object) : q_ptr(&object)
 
- {
 
-   qRegisterMetaType<ctkSpinBox::SetMode>("ctkSpinBox::SetMode");
 
-   qRegisterMetaType<ctkSpinBox::DecimalsOptions>("ctkSpinBox::DecimalsOption");
 
-   this->SpinBox = 0;
 
-   this->Mode = ctkSpinBox::SetIfDifferent;
 
-   this->DefaultDecimals = 2;
 
-   this->DOption = ctkSpinBox::UseShortcuts;
 
- }
 
- //-----------------------------------------------------------------------------
 
- void ctkSpinBoxPrivate::init()
 
- {
 
-   Q_Q(ctkSpinBox);
 
-   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 ctkSpinBoxPrivate::compare(double x1, double x2) const
 
- {
 
-   Q_Q(const ctkSpinBox);
 
-   return q->round(x1) == q->round(x2);
 
- }
 
- //-----------------------------------------------------------------------------
 
- void ctkSpinBoxPrivate::setDecimals(int dec)
 
- {
 
-   Q_Q(ctkSpinBox);
 
-   this->SpinBox->setDecimals(dec);
 
-   emit q->decimalsChanged(dec);
 
- }
 
- //-----------------------------------------------------------------------------
 
- ctkSpinBox::ctkSpinBox(QWidget* newParent)
 
-   : QWidget(newParent)
 
-   , d_ptr(new ctkSpinBoxPrivate(*this))
 
- {
 
-   Q_D(ctkSpinBox);
 
-   d->init();
 
- }
 
- //-----------------------------------------------------------------------------
 
- ctkSpinBox::ctkSpinBox(ctkSpinBox::SetMode mode, QWidget* newParent)
 
-   : QWidget(newParent)
 
-   , d_ptr(new ctkSpinBoxPrivate(*this))
 
- {
 
-   Q_D(ctkSpinBox);
 
-   d->init();
 
-   this->setSetMode(mode);
 
- }
 
- //-----------------------------------------------------------------------------
 
- double ctkSpinBox::value() const
 
- {
 
-   Q_D(const ctkSpinBox);
 
-   return d->SpinBox->value();
 
- }
 
- //-----------------------------------------------------------------------------
 
- double ctkSpinBox::displayedValue() const
 
- {
 
-   return this->round(this->value());
 
- }
 
- //-----------------------------------------------------------------------------
 
- QString ctkSpinBox::text() const
 
- {
 
-   Q_D(const ctkSpinBox);
 
-   return d->SpinBox->text();
 
- }
 
- //-----------------------------------------------------------------------------
 
- QString ctkSpinBox::cleanText() const
 
- {
 
-   Q_D(const ctkSpinBox);
 
-   return d->SpinBox->cleanText();
 
- }
 
- //-----------------------------------------------------------------------------
 
- Qt::Alignment ctkSpinBox::alignment() const
 
- {
 
-   Q_D(const ctkSpinBox);
 
-   return d->SpinBox->alignment();
 
- }
 
- //-----------------------------------------------------------------------------
 
- void ctkSpinBox::setAlignment(Qt::Alignment flag)
 
- {
 
-   Q_D(const ctkSpinBox);
 
-   if (d->Mode == ctkSpinBox::SetIfDifferent && flag == d->SpinBox->alignment())
 
-     {
 
-     return;
 
-     }
 
-   d->SpinBox->setAlignment(flag);
 
- }
 
- //-----------------------------------------------------------------------------
 
- void ctkSpinBox::setFrame(bool frame)
 
- {
 
-   Q_D(const ctkSpinBox);
 
-   if (d->Mode == ctkSpinBox::SetIfDifferent && frame == d->SpinBox->hasFrame())
 
-     {
 
-     return;
 
-     }
 
-   d->SpinBox->setFrame(frame);
 
- }
 
- //-----------------------------------------------------------------------------
 
- bool ctkSpinBox::hasFrame() const
 
- {
 
-   Q_D(const ctkSpinBox);
 
-   return d->SpinBox->hasFrame();
 
- }
 
- //-----------------------------------------------------------------------------
 
- QString ctkSpinBox::prefix() const
 
- {
 
-   Q_D(const ctkSpinBox);
 
-   return d->SpinBox->prefix();
 
- }
 
- //-----------------------------------------------------------------------------
 
- void ctkSpinBox::setPrefix(const QString &prefix)
 
- {
 
-   Q_D(const ctkSpinBox);
 
-   if (d->Mode == ctkSpinBox::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 ctkSpinBox::suffix() const
 
- {
 
-   Q_D(const ctkSpinBox);
 
-   return d->SpinBox->suffix();
 
- }
 
- //-----------------------------------------------------------------------------
 
- void ctkSpinBox::setSuffix(const QString &suffix)
 
- {
 
-   Q_D(const ctkSpinBox);
 
-   if (d->Mode == ctkSpinBox::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 ctkSpinBox::singleStep() const
 
- {
 
-   Q_D(const ctkSpinBox);
 
-   return d->SpinBox->singleStep();
 
- }
 
- //-----------------------------------------------------------------------------
 
- void ctkSpinBox::setSingleStep(double step)
 
- {
 
-   Q_D(ctkSpinBox);
 
-   if (d->Mode == ctkSpinBox::SetIfDifferent
 
-     && d->compare(step, this->singleStep()))
 
-     {
 
-     return;
 
-     }
 
-   d->SpinBox->setSingleStep(step);
 
- }
 
- //-----------------------------------------------------------------------------
 
- double ctkSpinBox::minimum() const
 
- {
 
-   Q_D(const ctkSpinBox);
 
-   return d->SpinBox->minimum();
 
- }
 
- //-----------------------------------------------------------------------------
 
- void ctkSpinBox::setMinimum(double min)
 
- {
 
-   Q_D(ctkSpinBox);
 
-   if (d->Mode == ctkSpinBox::SetIfDifferent
 
-     && d->compare(min, this->minimum()))
 
-     {
 
-     return;
 
-     }
 
-   d->SpinBox->setMinimum(min);
 
- }
 
- //-----------------------------------------------------------------------------
 
- double ctkSpinBox::maximum() const
 
- {
 
-   Q_D(const ctkSpinBox);
 
-   return d->SpinBox->maximum();
 
- }
 
- //-----------------------------------------------------------------------------
 
- void ctkSpinBox::setMaximum(double max)
 
- {
 
-   Q_D(ctkSpinBox);
 
-   if (d->Mode == ctkSpinBox::SetIfDifferent
 
-     && d->compare(max, this->maximum()))
 
-     {
 
-     return;
 
-     }
 
-   d->SpinBox->setMaximum(max);
 
- }
 
- //-----------------------------------------------------------------------------
 
- void ctkSpinBox::setRange(double min, double max)
 
- {
 
-   Q_D(ctkSpinBox);
 
-   if (d->Mode == ctkSpinBox::SetIfDifferent
 
-     && d->compare(max, this->maximum()) && d->compare(min, this->minimum()))
 
-     {
 
-     return;
 
-     }
 
-   d->SpinBox->setRange(min, max);
 
- }
 
- //-----------------------------------------------------------------------------
 
- int ctkSpinBox::decimals() const
 
- {
 
-   Q_D(const ctkSpinBox);
 
-   return d->SpinBox->decimals();
 
- }
 
- //-----------------------------------------------------------------------------
 
- void ctkSpinBox::setDecimals(int dec)
 
- {
 
-   Q_D(ctkSpinBox);
 
-   dec = qMax(0, dec);
 
-   if (d->Mode == ctkSpinBox::SetIfDifferent && dec == this->decimals())
 
-     {
 
-     return;
 
-     }
 
-   d->DefaultDecimals = dec;
 
-   d->setDecimals(d->DefaultDecimals);
 
- }
 
- //-----------------------------------------------------------------------------
 
- double ctkSpinBox::round(double value) const
 
- {
 
-   Q_D(const ctkSpinBox);
 
-   return QString::number(value, 'f', d->SpinBox->decimals()).toDouble();
 
- }
 
- //-----------------------------------------------------------------------------
 
- QDoubleSpinBox* ctkSpinBox::spinBox() const
 
- {
 
-   Q_D(const ctkSpinBox);
 
-   return d->SpinBox;
 
- }
 
- //-----------------------------------------------------------------------------
 
- void ctkSpinBox::setValue(double value)
 
- {
 
-   Q_D(ctkSpinBox);
 
-   if (d->Mode == ctkSpinBox::SetIfDifferent)
 
-     {
 
-     this->setValueIfDifferent(value);
 
-     }
 
-   else
 
-     {
 
-     this->setValueAlways(value);
 
-     }
 
- }
 
- //-----------------------------------------------------------------------------
 
- void ctkSpinBox::setValueIfDifferent(double value)
 
- {
 
-   Q_D(ctkSpinBox);
 
-   if (! d->compare(this->value(), value))
 
-     {
 
-     d->SpinBox->setValue(value);
 
-     }
 
- }
 
- //-----------------------------------------------------------------------------
 
- void ctkSpinBox::setValueAlways(double value)
 
- {
 
-   Q_D(const ctkSpinBox);
 
-   d->SpinBox->setValue(value);
 
- }
 
- //-----------------------------------------------------------------------------
 
- void ctkSpinBox::stepUp()
 
- {
 
-   Q_D(const ctkSpinBox);
 
-   d->SpinBox->stepUp();
 
- }
 
- //-----------------------------------------------------------------------------
 
- void ctkSpinBox::stepDown()
 
- {
 
-   Q_D(const ctkSpinBox);
 
-   d->SpinBox->stepDown();
 
- }
 
- //-----------------------------------------------------------------------------
 
- ctkSpinBox::SetMode ctkSpinBox::setMode() const
 
- {
 
-   Q_D(const ctkSpinBox);
 
-   return d->Mode;
 
- }
 
- //-----------------------------------------------------------------------------
 
- void ctkSpinBox::setSetMode(ctkSpinBox::SetMode newMode)
 
- {
 
-   Q_D(ctkSpinBox);
 
-   d->Mode = newMode;
 
- }
 
- //-----------------------------------------------------------------------------
 
- ctkSpinBox::DecimalsOptions ctkSpinBox::decimalsOption()
 
- {
 
-   Q_D(const ctkSpinBox);
 
-   return d->DOption;
 
- }
 
- //-----------------------------------------------------------------------------
 
- void ctkSpinBox::setDecimalsOption(ctkSpinBox::DecimalsOptions option)
 
- {
 
-   Q_D(ctkSpinBox);
 
-   if (d->Mode == ctkSpinBox::SetIfDifferent && option == d->DOption)
 
-     {
 
-     return;
 
-     }
 
-   d->DOption = option;
 
- }
 
- //-----------------------------------------------------------------------------
 
- bool ctkSpinBox::eventFilter(QObject* obj, QEvent* event)
 
- {
 
-   Q_D(ctkSpinBox);
 
-   if (d->DOption & ctkSpinBox::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);
 
-     }
 
- }
 
 
  |