Files
lsim/src/expdoublespinbox.cpp

121 lines
4.2 KiB
C++
Raw Permalink Normal View History

/***************************************************************************
* Copyright (C) 2008 by Peter Dahlberg *
* pdahlberg@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program 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 General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "expdoublespinbox.h"
#include <QRegExp>
#include <QLocale>
#include <QRegExpValidator>
#include <QLineEdit>
#include <QtDebug>
#include <cmath>
ExpDoubleSpinBox::ExpDoubleSpinBox()
: QDoubleSpinBox() {
setPositiveExp(4);
setNegativeExp(3);
setDisplayDecimals(decimals());
}
ExpDoubleSpinBox::~ExpDoubleSpinBox() {
}
double ExpDoubleSpinBox::valueFromText ( const QString& text ) const {
QString myText(text);
myText.chop(suffix().size());
myText.remove(prefix().size());
return myText.toDouble();
}
QString ExpDoubleSpinBox::textFromValue ( double val ) const {
//qDebug() << val;
if((val < pow(10,getPositiveExp()) && val > 1) || (val > -pow(10,getPositiveExp()) && val < -1) || val == 0) {
//return QDoubleSpinBox::textFromValue ( val );
return QString("%L1").arg(val,0,'f',getDisplayDecimals());
}
else if ( (val >= pow(10, -getNegativeExp()) && val <= 1 ) || (val <= -pow(10, -getNegativeExp()) && val >= -1 ) ){
//return QDoubleSpinBox::textFromValue ( val );
return QString("%L1").arg(val,0,'f',getDisplayDecimals());
}
else {
return QString("%L1").arg(val,0,'e',getDisplayDecimals());
}
//return QDoubleSpinBox::textFromValue ( val );
}
QValidator::State ExpDoubleSpinBox::validate ( QString & input, int & pos ) const {
QString myInput(input);
myInput.chop(suffix().size());
myInput.remove(prefix().size());
QLocale loc;
QChar decpoint = loc.decimalPoint(); //Locale abhaengiger dezimaltrenner
QString regExpString =QString("[\\-,\\+]?\\d*\\%1?\\d+([e,E][\\-,\\+]\\d+)?").arg(decpoint);
QRegExp regExp(regExpString);
QValidator *validator = new QRegExpValidator(regExp, 0);
//qDebug() << validator->validate(input,pos);
return validator->validate(myInput,pos);
}
QDoubleSpinBox::StepEnabled ExpDoubleSpinBox::stepEnabled () const {
QAbstractSpinBox::StepEnabled enabled = QDoubleSpinBox::stepEnabled();
if(
(value() > 0 && value() < pow(10, -getNegativeExp())) ||
(value() < 0 && value() > -pow(10, -getNegativeExp()))
) { //kein step bei negativen Zehnerpotenzen
enabled = QAbstractSpinBox::StepNone;
}
return enabled;
}
void ExpDoubleSpinBox::setNegativeExp (int exponent) {
if (negativeExp != exponent && exponent >= 0) negativeExp = exponent;
}
void ExpDoubleSpinBox::setPositiveExp (int exponent) {
if (positiveExp != exponent && exponent >= 0) positiveExp = exponent;
}
const int ExpDoubleSpinBox::getNegativeExp () const{
return negativeExp;
}
const int ExpDoubleSpinBox::getPositiveExp () const{
return positiveExp;
}
void ExpDoubleSpinBox::stepBy ( int steps ) {
QDoubleSpinBox::stepBy(steps);
}
int ExpDoubleSpinBox::getDisplayDecimals() const {
return displayDecimals;
}
void ExpDoubleSpinBox::setDisplayDecimals ( int theValue ) {
if (displayDecimals == theValue) return;
//Nicht mehr anzeigen, als die gneauigkeit zulaesst
if (theValue > decimals()) theValue = decimals();
displayDecimals = theValue;
}