kexi

kexitextformatter.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2007 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This program is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this program; see the file COPYING.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  */
00019 
00020 #include <klocale.h>
00021 
00022 #include "kexitextformatter.h"
00023 #include <widget/utils/kexidatetimeformatter.h>
00024 #include <kexidb/utils.h>
00025 
00027 class KexiTextFormatter::Private
00028 {
00029     public:
00030         Private() : field(0), dateFormatter(0), timeFormatter(0)
00031         {
00032         }
00033         
00034         ~Private()
00035         {
00036             delete dateFormatter;
00037             delete timeFormatter;
00038         }
00039 
00040         KexiDB::Field* field;
00041         KexiDateFormatter *dateFormatter;
00042         KexiTimeFormatter *timeFormatter;
00043 };
00044 
00045 KexiTextFormatter::KexiTextFormatter()
00046  : d( new Private )
00047 {
00048 }
00049 
00050 KexiTextFormatter::~KexiTextFormatter()
00051 {
00052     delete d;
00053 }
00054 
00055 void KexiTextFormatter::setField( KexiDB::Field* field )
00056 {
00057     d->field = field;
00058     if (!d->field)
00059         return;
00060     if (d->field->type() == KexiDB::Field::Date || d->field->type() == KexiDB::Field::DateTime)
00061         d->dateFormatter = new KexiDateFormatter();
00062     else {
00063         delete d->dateFormatter;
00064         d->dateFormatter = 0;
00065     }
00066     if (d->field->type() == KexiDB::Field::Time || d->field->type() == KexiDB::Field::DateTime)
00067         d->timeFormatter = new KexiTimeFormatter();
00068     else {
00069         delete d->timeFormatter;
00070         d->timeFormatter = 0;
00071     }
00072 }
00073 
00074 QString KexiTextFormatter::valueToText(const QVariant& value, const QString& add) const
00075 {
00076     //cases, in order of expected frequency
00077     if (!d->field || d->field->isTextType())
00078         return value.toString() + add;
00079     else if (d->field->isIntegerType()) {
00080         if (value.toInt() == 0)
00081             return add; //eat 0
00082     }
00083     else if (d->field->isFPNumericType()) {
00086         if (value.toDouble() == 0.0)
00087             return add.isEmpty() ? "0" : add; //eat 0
00088 #if 0 //moved to KexiDB::formatNumberForVisibleDecimalPlaces()
00089         QString text( QString::number(value.toDouble(), 'f', 
00090             QMAX(d->field->visibleDecimalPlaces(), 10)) ); 
00091 
00092 
00093         QStringList sl = QStringList::split(".", text);
00094             //nothing
00095         }
00096         else if (sl.count()==2) {
00097 //              kdDebug() << "sl.count()=="<<sl.count()<< " " <<sl[0] << " | " << sl[1] << endl;
00098             const QString sl1 = sl[1];
00099             int pos = sl1.length()-1;
00100             if (pos>=1) {
00101                 for (;pos>=0 && sl1[pos]=='0';pos--)
00102                     ;
00103                 pos++;
00104             }
00105             if (pos>0)
00106                 text = sl[0] + m_decsym + sl1.left(pos);
00107             else
00108                 text = sl[0]; //no decimal point
00109         }
00110 #endif
00111         return KexiDB::formatNumberForVisibleDecimalPlaces( 
00112             value.toDouble(), d->field->visibleDecimalPlaces() ) + add;
00113     }
00114     else if (d->field->type() == KexiDB::Field::Boolean) {
00116         const bool boolValue = value.isNull() ? QVariant(add).toBool() : value.toBool();
00117         return boolValue ? "1" : "0";
00118     }
00119     else if (d->field->type() == KexiDB::Field::Date) {
00120         return d->dateFormatter->dateToString( value.toString().isEmpty() ? QDate() : value.toDate() );
00121     }
00122     else if (d->field->type() == KexiDB::Field::Time) {
00123         return d->timeFormatter->timeToString( 
00124             //hack to avoid converting null variant to valid QTime(0,0,0)
00125             value.toString().isEmpty() ? value.toTime() : QTime(99,0,0) );
00126     }
00127     else if (d->field->type() == KexiDB::Field::DateTime) {
00128         if (value.toString().isEmpty() )
00129             return add;
00130         return d->dateFormatter->dateToString( value.toDateTime().date() ) + " " +
00131             d->timeFormatter->timeToString( value.toDateTime().time() );
00132     }
00133     else if (d->field->type() == KexiDB::Field::BigInteger) {
00134         if (value.toLongLong() == 0)
00135             return add; //eat 0
00136     }
00137     //default: text
00138     return value.toString() + add;
00139 }
00140 
00141 QVariant KexiTextFormatter::textToValue(const QString& text) const
00142 {
00143     if (!d->field)
00144         return QVariant();
00145     const KexiDB::Field::Type t = d->field->type();
00146     switch (t) {
00147     case KexiDB::Field::Text:
00148     case KexiDB::Field::LongText:
00149         return text;
00150     case KexiDB::Field::Byte:
00151     case KexiDB::Field::ShortInteger:
00152         return text.toShort();
00154     case KexiDB::Field::Integer:
00155         return text.toInt();
00156     case KexiDB::Field::BigInteger:
00157         return text.toLongLong();
00158     case KexiDB::Field::Boolean:
00160         return text == "1" ? QVariant(true,1) : QVariant(false,0);
00161     case KexiDB::Field::Date:
00162         return d->dateFormatter->stringToVariant( text );
00163     case KexiDB::Field::Time:
00164         return d->timeFormatter->stringToVariant( text );
00165     case KexiDB::Field::DateTime:
00166         return stringToDateTime(*d->dateFormatter, *d->timeFormatter, text);
00167     case KexiDB::Field::Float:
00168     case KexiDB::Field::Double: {
00169         // replace custom decimal symbol with '.' as required by to{Float|Double}()
00170         QString fixedText( text );
00171         fixedText.replace(KGlobal::locale()->decimalSymbol(), ".");
00172         if (t == KexiDB::Field::Double)
00173             return fixedText.toDouble();
00174         return fixedText.toFloat();
00175     }
00176     default:
00177         return text;
00178     }
00180 }
00181 
00182 bool KexiTextFormatter::valueIsEmpty(const QString& text) const
00183 {
00184     if (text.isEmpty())
00185         return true;
00186 
00187     if (d->field) {
00188         const KexiDB::Field::Type t = d->field->type();
00189         if (t == KexiDB::Field::Date)
00190             return d->dateFormatter->isEmpty( text );
00191         else if (t == KexiDB::Field::Time)
00192             return d->timeFormatter->isEmpty( text );
00193         else if (t == KexiDB::Field::Time)
00194             return dateTimeIsEmpty( *d->dateFormatter, *d->timeFormatter, text );
00195     }
00196 
00198     return text.isEmpty();
00199 }
00200 
00201 bool KexiTextFormatter::valueIsValid(const QString& text) const
00202 {
00203     if (!d->field)
00204         return true;
00206     if (valueIsEmpty(text)/*ok?*/)
00207         return true;
00208 
00209     const KexiDB::Field::Type t = d->field->type();
00210     if (t == KexiDB::Field::Date)
00211         return d->dateFormatter->stringToVariant( text ).isValid();
00212     else if (t == KexiDB::Field::Time)
00213         return d->timeFormatter->stringToVariant( text ).isValid();
00214     else if (t == KexiDB::Field::DateTime)
00215         return dateTimeIsValid( *d->dateFormatter, *d->timeFormatter, text );
00216 
00218     return true;
00219 }
00220 
00221 QString KexiTextFormatter::inputMask() const
00222 {
00223     const KexiDB::Field::Type t = d->field->type();
00224     if (t==KexiDB::Field::Date) {
00226         return d->dateFormatter->inputMask();
00227     }
00228     else if (t==KexiDB::Field::Time) {
00230         d->timeFormatter->inputMask();
00231     }
00232     else if (t==KexiDB::Field::DateTime) {
00233         dateTimeInputMask( *d->dateFormatter, *d->timeFormatter );
00234     }
00235     return QString::null;
00236 }
00237 
KDE Home | KDE Accessibility Home | Description of Access Keys