kexi
kexitextformatter.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
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;
00082 }
00083 else if (d->field->isFPNumericType()) {
00086 if (value.toDouble() == 0.0)
00087 return add.isEmpty() ? "0" : add;
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
00095 }
00096 else if (sl.count()==2) {
00097
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];
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
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;
00136 }
00137
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
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))
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
|