Files
create/src/Gui/SpinBox.cpp
2011-10-10 13:44:52 +00:00

227 lines
5.7 KiB
C++

/***************************************************************************
* Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* 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., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <climits>
#endif
#include "SpinBox.h"
using namespace Gui;
UnsignedValidator::UnsignedValidator( QObject * parent )
: QValidator( parent )
{
b = 0;
t = UINT_MAX;
}
UnsignedValidator::UnsignedValidator( uint minimum, uint maximum, QObject * parent )
: QValidator( parent )
{
b = minimum;
t = maximum;
}
UnsignedValidator::~UnsignedValidator()
{
}
QValidator::State UnsignedValidator::validate( QString & input, int & ) const
{
QString stripped = input.trimmed();
if ( stripped.isEmpty() )
return Intermediate;
bool ok;
uint entered = input.toUInt( &ok );
if ( !ok )
return Invalid;
else if ( entered < b )
return Intermediate;
else if ( entered > t )
return Invalid;
// else if ( entered < b || entered > t )
// return Invalid;
else
return Acceptable;
}
void UnsignedValidator::setRange( uint minimum, uint maximum )
{
b = minimum;
t = maximum;
}
void UnsignedValidator::setBottom( uint bottom )
{
setRange( bottom, top() );
}
void UnsignedValidator::setTop( uint top )
{
setRange( bottom(), top );
}
namespace Gui {
class UIntSpinBoxPrivate
{
public:
UnsignedValidator * mValidator;
UIntSpinBoxPrivate() : mValidator(0)
{
}
uint mapToUInt( int v ) const
{
uint ui;
if ( v == INT_MIN ) {
ui = 0;
} else if ( v == INT_MAX ) {
ui = UINT_MAX;
} else if ( v < 0 ) {
v -= INT_MIN; ui = (uint)v;
} else {
ui = (uint)v; ui -= INT_MIN;
} return ui;
}
int mapToInt( uint v ) const
{
int in;
if ( v == UINT_MAX ) {
in = INT_MAX;
} else if ( v == 0 ) {
in = INT_MIN;
} else if ( v > INT_MAX ) {
v += INT_MIN; in = (int)v;
} else {
in = v; in += INT_MIN;
} return in;
}
};
} // namespace Gui
UIntSpinBox::UIntSpinBox (QWidget* parent)
: QSpinBox (parent)
{
d = new UIntSpinBoxPrivate;
d->mValidator = new UnsignedValidator(this->minimum(), this->maximum(), this);
connect(this, SIGNAL(valueChanged(int)),
this, SLOT(valueChange(int)));
setRange(0, 99);
setValue(0);
updateValidator();
}
UIntSpinBox::~UIntSpinBox()
{
delete d->mValidator;
delete d; d = 0;
}
void UIntSpinBox::setRange(uint minVal, uint maxVal)
{
int iminVal = d->mapToInt(minVal);
int imaxVal = d->mapToInt(maxVal);
QSpinBox::setRange(iminVal, imaxVal);
updateValidator();
}
QValidator::State UIntSpinBox::validate (QString & input, int & pos) const
{
return d->mValidator->validate(input, pos);
}
uint UIntSpinBox::value() const
{
return d->mapToUInt(QSpinBox::value());
}
void UIntSpinBox::setValue(uint value)
{
QSpinBox::setValue(d->mapToInt(value));
}
void UIntSpinBox::valueChange(int value)
{
valueChanged(d->mapToUInt(value));
}
uint UIntSpinBox::minimum() const
{
return d->mapToUInt(QSpinBox::minimum());
}
void UIntSpinBox::setMinimum(uint minVal)
{
uint maxVal = maximum();
if (maxVal < minVal)
maxVal = minVal;
setRange(minVal, maxVal);
}
uint UIntSpinBox::maximum() const
{
return d->mapToUInt(QSpinBox::maximum());
}
void UIntSpinBox::setMaximum(uint maxVal)
{
uint minVal = minimum();
if (minVal > maxVal)
minVal = maxVal;
setRange(minVal, maxVal);
}
QString UIntSpinBox::textFromValue (int v) const
{
uint val = d->mapToUInt(v);
QString s;
s.setNum(val);
return s;
}
int UIntSpinBox::valueFromText (const QString & text) const
{
bool ok;
QString s = text;
uint newVal = s.toUInt(&ok);
if (!ok && !(prefix().isEmpty() && suffix().isEmpty())) {
s = cleanText();
newVal = s.toUInt(&ok);
}
return d->mapToInt(newVal);
}
void UIntSpinBox::updateValidator()
{
d->mValidator->setRange(this->minimum(), this->maximum());
}
#include "moc_SpinBox.cpp"