initial commit of code tree
git-svn-id: http://svn.lsim.tuxzone.org/trunk@2 4bec179b-ab65-46ed-a5f8-55b8b5c735d0
This commit is contained in:
99
src/expdoublespinbox.cpp
Normal file
99
src/expdoublespinbox.cpp
Normal file
@@ -0,0 +1,99 @@
|
||||
/***************************************************************************
|
||||
* 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);
|
||||
}
|
||||
|
||||
|
||||
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 );
|
||||
return QString("%L1").arg(val,0,'f',decimals());
|
||||
}
|
||||
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',decimals());
|
||||
}
|
||||
else {
|
||||
return QString("%L1").arg(val,0,'e',decimals());
|
||||
}
|
||||
//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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user