www.pudn.com > calculator.rar > doubledata.cpp


/****************************************************************************
**
** Copyright (C) 2000-2006 TROLLTECH ASA. All rights reserved.
**
** This file is part of the Phone Edition of the Qtopia Toolkit.
**
** Licensees holding a valid license agreement from Trolltech or any of its
** authorized distributors may use this file in accordance with
** the License Agreement provided with the Licensed Software.
**
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
**   information about Trolltech's Commercial License Agreements.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**
**
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/
#include 

#include "doubledata.h"
#include "engine.h"

// Data type
DoubleData::DoubleData(): Data() {};

void DoubleData::set(double d) {
    dbl = d;
    edited = false;
    formattedOutput = formattedOutput.sprintf("%13.13f", dbl);
    
    int point = formattedOutput.indexOf('.');
    if (point > 11)
    {   
        QRegExp reg = QRegExp("[1..9]+");
        if (point > 12)
            systemEngine->setError(eSurpassLimits);
        else if ( point == 12 && formattedOutput.mid(13).contains(reg) ) {
            systemEngine->setError(eSurpassLimits);
        }
    }

    formattedOutput.truncate(12);
    //remove trailing zeros if decimal point is present
    if (formattedOutput.indexOf('.') > -1){
        int max = formattedOutput.length();
        int i = max - 1;
        while (formattedOutput.at(i) == '0')
            i--;
        formattedOutput.remove(++i, max-i);
        
        if (formattedOutput.at( i-1 ) == '.')
            formattedOutput.remove( i-1 , 1 );
    }
    
    if (!strcmp(formattedOutput.toLatin1(),"nan")) { // No tr
	systemEngine->setError(eNotANumber);
    } else if (!strcmp(formattedOutput.toLatin1(),"inf")) { // No tr
	systemEngine->setError(eInf);
    } else if (!strcmp(formattedOutput.toLatin1(),"-inf")) { // No tr
	systemEngine->setError(eNegInf);
    }
}

double DoubleData::get() { return dbl; }

bool DoubleData::push(char c, bool commit) {
    if (edited && formattedOutput.length() >= 12)
	return false;
    // Allow zero to be input as a value, but only once
    if (formattedOutput == "0" && c == '0')
	return !edited;

    //when +/- is pressed while no number has been entered
    //return to !edited mode
    if (formattedOutput == "0" && edited) 
        edited = !edited;

    QString tmpString = formattedOutput;
    if (!edited) {
	if (c == '.') 
	    tmpString = QString("0");
	else
	    tmpString.truncate(0);
	// Dont change the value of edited on the test run
	if (commit)
	    edited = true;
    }
    tmpString.append(QChar(c));
    bool ok;
    double tmp = tmpString.toDouble(&ok);
    if (ok) {
	if (commit) {
	    formattedOutput = tmpString;
	    dbl = tmp;
	}
    } else
	qDebug("Wrong character pushed");
    return ok;
}
bool DoubleData::del() {
    if (!edited)
	return true;
    if (formattedOutput.length() == 1) {
	formattedOutput.truncate(0);
	formattedOutput.append("0");
	edited = false;
	dbl = 0;
        return true;
    } else {
	QString tmpString = formattedOutput;
	tmpString.truncate(formattedOutput.length()-1);
	bool ok;
	double tmp = tmpString.toDouble(&ok);
	if (ok) {
	    formattedOutput = tmpString;
	    dbl = tmp;
	} else
            return true;
    }
    return false;
}
void DoubleData::clear() {
    dbl = 0;
    formattedOutput.truncate(0);
    formattedOutput.append("0");
    edited = false;
}