2008-10-08 22:11:27 +00:00
|
|
|
/***************************************************************************
|
|
|
|
|
* 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);
|
2008-11-02 20:34:58 +00:00
|
|
|
setNegativeExp(3);
|
|
|
|
|
setDisplayDecimals(decimals());
|
2008-10-08 22:11:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ExpDoubleSpinBox::~ExpDoubleSpinBox() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
double ExpDoubleSpinBox::valueFromText ( const QString& text ) const {
|
|
|
|
|
return text.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 );
|
2008-11-02 20:34:58 +00:00
|
|
|
return QString("%L1").arg(val,0,'f',getDisplayDecimals());
|
2008-10-08 22:11:27 +00:00
|
|
|
}
|
|
|
|
|
else if ( (val >= pow(10, -getNegativeExp()) && val <= 1 ) || (val <= -pow(10, -getNegativeExp()) && val >= -1 ) ){
|
|
|
|
|
//return QDoubleSpinBox::textFromValue ( val );
|
2008-11-02 20:34:58 +00:00
|
|
|
return QString("%L1").arg(val,0,'f',getDisplayDecimals());
|
2008-10-08 22:11:27 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2008-11-02 20:34:58 +00:00
|
|
|
return QString("%L1").arg(val,0,'e',getDisplayDecimals());
|
2008-10-08 22:11:27 +00:00
|
|
|
}
|
|
|
|
|
//return QDoubleSpinBox::textFromValue ( val );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QValidator::State ExpDoubleSpinBox::validate ( QString & input, int & pos ) const {
|
|
|
|
|
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(input,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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-11-02 20:34:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|