SpinBox.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net>     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 
00026 #ifndef _PreComp_
00027 # include <climits>
00028 #endif
00029 
00030 #include "SpinBox.h"
00031 
00032 using namespace Gui;
00033 
00034 
00035 UnsignedValidator::UnsignedValidator( QObject * parent )
00036   : QValidator( parent )
00037 {
00038     b =  0;
00039     t =  UINT_MAX;
00040 }
00041 
00042 UnsignedValidator::UnsignedValidator( uint minimum, uint maximum, QObject * parent )
00043   : QValidator( parent )
00044 {
00045     b = minimum;
00046     t = maximum;
00047 }
00048 
00049 UnsignedValidator::~UnsignedValidator()
00050 {
00051 
00052 }
00053 
00054 QValidator::State UnsignedValidator::validate( QString & input, int & ) const
00055 {
00056     QString stripped = input.trimmed();
00057     if ( stripped.isEmpty() )
00058         return Intermediate;
00059     bool ok;
00060     uint entered = input.toUInt( &ok );
00061     if ( !ok )
00062         return Invalid;
00063     else if ( entered < b )
00064         return Intermediate;
00065     else if ( entered > t )
00066         return Invalid;
00067     //  else if ( entered < b || entered > t )
00068     //    return Invalid;
00069     else
00070         return Acceptable;
00071 }
00072 
00073 void UnsignedValidator::setRange( uint minimum, uint maximum )
00074 {
00075     b = minimum;
00076     t = maximum;
00077 }
00078 
00079 void UnsignedValidator::setBottom( uint bottom )
00080 {
00081     setRange( bottom, top() );
00082 }
00083 
00084 void UnsignedValidator::setTop( uint top )
00085 {
00086     setRange( bottom(), top );
00087 }
00088 
00089 namespace Gui {
00090 class UIntSpinBoxPrivate
00091 {
00092 public:
00093     UnsignedValidator * mValidator;
00094 
00095     UIntSpinBoxPrivate() : mValidator(0)
00096     {
00097     }
00098     uint mapToUInt( int v ) const
00099     {
00100         uint ui;
00101         if ( v == INT_MIN ) {
00102             ui = 0;
00103         } else if ( v == INT_MAX ) {
00104             ui = UINT_MAX;
00105         } else if ( v < 0 ) {
00106             v -= INT_MIN; ui = (uint)v;
00107         } else {
00108             ui = (uint)v; ui -= INT_MIN;
00109         } return ui;
00110     }
00111     int mapToInt( uint v ) const
00112     {
00113         int in;
00114         if ( v == UINT_MAX ) {
00115             in = INT_MAX;
00116         } else if ( v == 0 ) {
00117             in = INT_MIN;
00118         } else if ( v > INT_MAX ) {
00119             v += INT_MIN; in = (int)v;
00120         } else {
00121             in = v; in += INT_MIN;
00122         } return in;
00123     }
00124 };
00125 
00126 } // namespace Gui
00127 
00128 UIntSpinBox::UIntSpinBox (QWidget* parent)
00129   : QSpinBox (parent)
00130 {
00131     d = new UIntSpinBoxPrivate;
00132     d->mValidator =  new UnsignedValidator(this->minimum(), this->maximum(), this);
00133     connect(this, SIGNAL(valueChanged(int)),
00134             this, SLOT(valueChange(int)));
00135     setRange(0, 99);
00136     setValue(0);
00137     updateValidator();
00138 }
00139 
00140 UIntSpinBox::~UIntSpinBox()
00141 {
00142     delete d->mValidator;
00143     delete d; d = 0;
00144 }
00145 
00146 void UIntSpinBox::setRange(uint minVal, uint maxVal)
00147 {
00148     int iminVal = d->mapToInt(minVal);
00149     int imaxVal = d->mapToInt(maxVal);
00150     QSpinBox::setRange(iminVal, imaxVal);
00151     updateValidator();
00152 }
00153 
00154 QValidator::State UIntSpinBox::validate (QString & input, int & pos) const
00155 {
00156     return d->mValidator->validate(input, pos);
00157 }
00158 
00159 uint UIntSpinBox::value() const
00160 {
00161     return d->mapToUInt(QSpinBox::value());
00162 }
00163 
00164 void UIntSpinBox::setValue(uint value)
00165 {
00166     QSpinBox::setValue(d->mapToInt(value));
00167 }
00168 
00169 void UIntSpinBox::valueChange(int value)
00170 {
00171     valueChanged(d->mapToUInt(value));
00172 }
00173 
00174 uint UIntSpinBox::minimum() const
00175 {
00176     return d->mapToUInt(QSpinBox::minimum());
00177 }
00178 
00179 void UIntSpinBox::setMinimum(uint minVal)
00180 {
00181     uint maxVal = maximum();
00182     if (maxVal < minVal)
00183         maxVal = minVal;
00184     setRange(minVal, maxVal);
00185 }
00186 
00187 uint UIntSpinBox::maximum() const
00188 {
00189     return d->mapToUInt(QSpinBox::maximum());
00190 }
00191 
00192 void UIntSpinBox::setMaximum(uint maxVal)
00193 {
00194     uint minVal = minimum();
00195     if (minVal > maxVal)
00196         minVal = maxVal;
00197     setRange(minVal, maxVal);
00198 }
00199 
00200 QString UIntSpinBox::textFromValue (int v) const
00201 {
00202     uint val = d->mapToUInt(v);
00203     QString s;
00204     s.setNum(val);
00205     return s;
00206 }
00207 
00208 int UIntSpinBox::valueFromText (const QString & text) const
00209 {
00210     bool ok;
00211     QString s = text;
00212     uint newVal = s.toUInt(&ok);
00213     if (!ok && !(prefix().isEmpty() && suffix().isEmpty())) {
00214         s = cleanText();
00215         newVal = s.toUInt(&ok);
00216     }
00217 
00218     return d->mapToInt(newVal);
00219 }
00220 
00221 void UIntSpinBox::updateValidator() 
00222 {
00223     d->mValidator->setRange(this->minimum(), this->maximum());
00224 }
00225 
00226 #include "moc_SpinBox.cpp"

Generated on Wed Nov 23 19:00:40 2011 for FreeCAD by  doxygen 1.6.1