kexitextformatter.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2007 Jaroslaw Staniek <js@iidea.pl>
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include <tdelocale.h>
21
22#include "kexitextformatter.h"
23#include <widget/utils/kexidatetimeformatter.h>
24#include <kexidb/utils.h>
25
27class KexiTextFormatter::Private
28{
29 public:
30 Private() : field(0), dateFormatter(0), timeFormatter(0)
31 {
32 }
33
34 ~Private()
35 {
36 delete dateFormatter;
37 delete timeFormatter;
38 }
39
40 KexiDB::Field* field;
41 KexiDateFormatter *dateFormatter;
42 KexiTimeFormatter *timeFormatter;
43};
44
45KexiTextFormatter::KexiTextFormatter()
46 : d( new Private )
47{
48}
49
50KexiTextFormatter::~KexiTextFormatter()
51{
52 delete d;
53}
54
56{
57 d->field = field;
58 if (!d->field)
59 return;
60 if (d->field->type() == KexiDB::Field::Date || d->field->type() == KexiDB::Field::DateTime)
61 d->dateFormatter = new KexiDateFormatter();
62 else {
63 delete d->dateFormatter;
64 d->dateFormatter = 0;
65 }
66 if (d->field->type() == KexiDB::Field::Time || d->field->type() == KexiDB::Field::DateTime)
67 d->timeFormatter = new KexiTimeFormatter();
68 else {
69 delete d->timeFormatter;
70 d->timeFormatter = 0;
71 }
72}
73
74TQString KexiTextFormatter::valueToText(const TQVariant& value, const TQString& add) const
75{
76 //cases, in order of expected frequency
77 if (!d->field || d->field->isTextType())
78 return value.toString() + add;
79 else if (d->field->isIntegerType()) {
80 if (value.toInt() == 0)
81 return add; //eat 0
82 }
83 else if (d->field->isFPNumericType()) {
86 if (value.toDouble() == 0.0)
87 return add.isEmpty() ? "0" : add; //eat 0
88#if 0 //moved to KexiDB::formatNumberForVisibleDecimalPlaces()
89 TQString text( TQString::number(value.toDouble(), 'f',
90 TQMAX(d->field->visibleDecimalPlaces(), 10)) );
93 TQStringList sl = TQStringList::split(".", text);
94 //nothing
95 }
96 else if (sl.count()==2) {
97// kdDebug() << "sl.count()=="<<sl.count()<< " " <<sl[0] << " | " << sl[1] << endl;
98 const TQString sl1 = sl[1];
99 int pos = sl1.length()-1;
100 if (pos>=1) {
101 for (;pos>=0 && sl1[pos]=='0';pos--)
102 ;
103 pos++;
104 }
105 if (pos>0)
106 text = sl[0] + m_decsym + sl1.left(pos);
107 else
108 text = sl[0]; //no decimal point
109 }
110#endif
112 value.toDouble(), d->field->visibleDecimalPlaces() ) + add;
113 }
114 else if (d->field->type() == KexiDB::Field::Boolean) {
116 const bool boolValue = value.isNull() ? TQVariant(add).toBool() : value.toBool();
117 return boolValue ? "1" : "0";
118 }
119 else if (d->field->type() == KexiDB::Field::Date) {
120 return d->dateFormatter->dateToString( value.toString().isEmpty() ? TQDate() : value.toDate() );
121 }
122 else if (d->field->type() == KexiDB::Field::Time) {
123 return d->timeFormatter->timeToString(
124 //hack to avoid converting null variant to valid TQTime(0,0,0)
125 value.toString().isEmpty() ? value.toTime() : TQTime(99,0,0) );
126 }
127 else if (d->field->type() == KexiDB::Field::DateTime) {
128 if (value.toString().isEmpty() )
129 return add;
130 return d->dateFormatter->dateToString( value.toDateTime().date() ) + " " +
131 d->timeFormatter->timeToString( value.toDateTime().time() );
132 }
133 else if (d->field->type() == KexiDB::Field::BigInteger) {
134 if (value.toLongLong() == 0)
135 return add; //eat 0
136 }
137 //default: text
138 return value.toString() + add;
139}
140
141TQVariant KexiTextFormatter::textToValue(const TQString& text) const
142{
143 if (!d->field)
144 return TQVariant();
145 const KexiDB::Field::Type t = d->field->type();
146 switch (t) {
149 return text;
152 return text.toShort();
155 return text.toInt();
157 return text.toLongLong();
160 return text == "1" ? TQVariant(true) : TQVariant(false);
161 case KexiDB::Field::Date:
162 return d->dateFormatter->stringToVariant( text );
163 case KexiDB::Field::Time:
164 return d->timeFormatter->stringToVariant( text );
165 case KexiDB::Field::DateTime:
166 return stringToDateTime(*d->dateFormatter, *d->timeFormatter, text);
169 // replace custom decimal symbol with '.' as required by to{Float|Double}()
170 TQString fixedText( text );
171 fixedText.replace(TDEGlobal::locale()->decimalSymbol(), ".");
172 if (t == KexiDB::Field::Double)
173 return fixedText.toDouble();
174 return fixedText.toFloat();
175 }
176 default:
177 return text;
178 }
180}
181
182bool KexiTextFormatter::valueIsEmpty(const TQString& text) const
183{
184 if (text.isEmpty())
185 return true;
186
187 if (d->field) {
188 const KexiDB::Field::Type t = d->field->type();
189 if (t == KexiDB::Field::Date)
190 return d->dateFormatter->isEmpty( text );
191 else if (t == KexiDB::Field::Time)
192 return d->timeFormatter->isEmpty( text );
193 else if (t == KexiDB::Field::Time)
194 return dateTimeIsEmpty( *d->dateFormatter, *d->timeFormatter, text );
195 }
196
198 return text.isEmpty();
199}
200
201bool KexiTextFormatter::valueIsValid(const TQString& text) const
202{
203 if (!d->field)
204 return true;
206 if (valueIsEmpty(text)/*ok?*/)
207 return true;
208
209 const KexiDB::Field::Type t = d->field->type();
210 if (t == KexiDB::Field::Date)
211 return d->dateFormatter->stringToVariant( text ).isValid();
212 else if (t == KexiDB::Field::Time)
213 return d->timeFormatter->stringToVariant( text ).isValid();
214 else if (t == KexiDB::Field::DateTime)
215 return dateTimeIsValid( *d->dateFormatter, *d->timeFormatter, text );
216
218 return true;
219}
220
222{
223 const KexiDB::Field::Type t = d->field->type();
224 if (t==KexiDB::Field::Date) {
226 return d->dateFormatter->inputMask();
227 }
228 else if (t==KexiDB::Field::Time) {
230 d->timeFormatter->inputMask();
231 }
232 else if (t==KexiDB::Field::DateTime) {
233 dateTimeInputMask( *d->dateFormatter, *d->timeFormatter );
234 }
235 return TQString();
236}
237
Meta-data for a field.
Definition field.h:73
@ ShortInteger
Definition field.h:86
Date formatter used by KexiDateTableEdit and KexiDateTimeTableEdit.
void setField(KexiDB::Field *field)
Assigns field to the formatter. This affects its behaviour.
TQVariant textToValue(const TQString &text) const
bool valueIsEmpty(const TQString &text) const
bool valueIsValid(const TQString &text) const
TQString inputMask() const
TQString valueToText(const TQVariant &value, const TQString &add) const
Time formatter used by KexiTimeTableEdit and KexiDateTimeTableEdit Following time formats are allowed...
KEXI_DB_EXPORT TQString formatNumberForVisibleDecimalPlaces(double value, int decimalPlaces)