00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "kexidatetableedit.h"
00023
00024 #include <qapplication.h>
00025 #include <qpainter.h>
00026 #include <qvariant.h>
00027 #include <qrect.h>
00028 #include <qpalette.h>
00029 #include <qcolor.h>
00030 #include <qfontmetrics.h>
00031 #include <qdatetime.h>
00032 #include <qcursor.h>
00033 #include <qpoint.h>
00034 #include <qlayout.h>
00035 #include <qtoolbutton.h>
00036 #include <qdatetimeedit.h>
00037
00038 #include <kdebug.h>
00039 #include <klocale.h>
00040 #include <kglobal.h>
00041 #include <kdatepicker.h>
00042 #include <kdatetbl.h>
00043 #include <klineedit.h>
00044 #include <kpopupmenu.h>
00045 #include <kdatewidget.h>
00046
00047 #include <kexiutils/utils.h>
00048
00049 KexiDateFormatter::KexiDateFormatter()
00050 {
00051
00053 QString df( KGlobal::locale()->dateFormatShort() );
00054 if (df.length()>2)
00055 m_separator = df.mid(2,1);
00056 else
00057 m_separator = "-";
00058 const int separatorLen = m_separator.length();
00059 QString yearMask("9999");
00060 QString yearDateFormat("yyyy"),
00061 monthDateFormat("MM"),
00062 dayDateFormat("dd");
00063 bool ok = df.length()>=8;
00064 int yearpos, monthpos, daypos;
00065 if (ok) {
00068 yearpos = df.find("%y", 0, false);
00069 m_longYear = !(yearpos>=0 && df.mid(yearpos+1, 1)=="y");
00070 if (!m_longYear) {
00071 yearMask = "99";
00072 yearDateFormat = "yy";
00073 }
00074 monthpos = df.find("%m", 0, true);
00075 m_monthWithLeadingZero = true;
00076 if (monthpos<0) {
00077 monthpos = df.find("%n", 0, false);
00078 m_monthWithLeadingZero = false;
00079 monthDateFormat = "M";
00080 }
00081 daypos = df.find("%d", 0, true);
00082 m_dayWithLeadingZero = true;
00083 if (daypos<0) {
00084 daypos = df.find("%e", 0, false);
00085 m_dayWithLeadingZero = false;
00086 dayDateFormat = "d";
00087 }
00088 ok = (yearpos>=0 && monthpos>=0 && daypos>=0);
00089 }
00090 m_order = QDateEdit::YMD;
00091 if (ok) {
00092 if (yearpos<monthpos && monthpos<daypos) {
00093
00094 }
00095 else if (yearpos<daypos && daypos<monthpos) {
00096 m_order = QDateEdit::YDM;
00099 m_inputMask = QString("%1%299%399").arg(yearMask).arg(m_separator).arg(m_separator);
00100 m_qtFormat = yearDateFormat+m_separator+dayDateFormat+m_separator+monthDateFormat;
00101 m_yearpos = 0;
00102 m_daypos = yearMask.length()+separatorLen;
00103 m_monthpos = m_daypos+2+separatorLen;
00104 }
00105 else if (daypos<monthpos && monthpos<yearpos) {
00106 m_order = QDateEdit::DMY;
00107 m_inputMask = QString("99%199%2%3").arg(m_separator).arg(m_separator).arg(yearMask);
00108 m_qtFormat = dayDateFormat+m_separator+monthDateFormat+m_separator+yearDateFormat;
00109 m_daypos = 0;
00110 m_monthpos = 2+separatorLen;
00111 m_yearpos = m_monthpos+2+separatorLen;
00112 }
00113 else if (monthpos<daypos && daypos<yearpos) {
00114 m_order = QDateEdit::MDY;
00115 m_inputMask = QString("99%199%2%3").arg(m_separator).arg(m_separator).arg(yearMask);
00116 m_qtFormat = monthDateFormat+m_separator+dayDateFormat+m_separator+yearDateFormat;
00117 m_monthpos = 0;
00118 m_daypos = 2+separatorLen;
00119 m_yearpos = m_daypos+2+separatorLen;
00120 }
00121 else
00122 ok = false;
00123 }
00124 if (!ok || m_order == QDateEdit::YMD) {
00125 m_inputMask = QString("%1%299%399").arg(yearMask).arg(m_separator).arg(m_separator);
00126 m_qtFormat = yearDateFormat+m_separator+monthDateFormat+m_separator+dayDateFormat;
00127 m_yearpos = 0;
00128 m_monthpos = yearMask.length()+separatorLen;
00129 m_daypos = m_monthpos+2+separatorLen;
00130 }
00131 m_inputMask += ";_";
00132 }
00133
00134 KexiDateFormatter::~KexiDateFormatter()
00135 {
00136 }
00137
00138 QDate KexiDateFormatter::stringToDate( const QString& str ) const
00139 {
00140 bool ok = true;
00141 int year = str.mid(m_yearpos, m_longYear ? 4 : 2).toInt(&ok);
00142 if (!ok)
00143 return QDate();
00144 if (year < 30) {
00145 year = 2000 + year;
00146 }
00147 else if (year < 100) {
00148 year = 1900 + year;
00149 }
00150
00151 int month = str.mid(m_monthpos, 2).toInt(&ok);
00152 if (!ok)
00153 return QDate();
00154
00155 int day = str.mid(m_daypos, 2).toInt(&ok);
00156 if (!ok)
00157 return QDate();
00158
00159 QDate date(year, month, day);
00160 if (!date.isValid())
00161 return QDate();
00162 return date;
00163 }
00164
00165 QString KexiDateFormatter::dateToString( const QDate& date ) const
00166 {
00167 return date.toString(m_qtFormat);
00168 }
00169
00170
00171
00172 KexiDateTableEdit::KexiDateTableEdit(KexiTableViewColumn &column, QScrollView *parent)
00173 : KexiInputTableEdit(column, parent)
00174 {
00175 setName("KexiDateTableEdit");
00176
00178
00179 m_lineedit->setInputMask( m_formatter.inputMask() );
00180 }
00181
00182 KexiDateTableEdit::~KexiDateTableEdit()
00183 {
00184 }
00185
00186 void KexiDateTableEdit::setValueInternal(const QVariant& add_, bool removeOld)
00187 {
00188 if (removeOld) {
00189
00191 QString add(add_.toString());
00192 m_lineedit->setText(add);
00193 m_lineedit->setCursorPosition(add.length());
00194 return;
00195 }
00196 m_lineedit->setText( m_formatter.dateToString( m_origValue.toDate() ) );
00197 m_lineedit->setCursorPosition(0);
00198 }
00199
00200 void KexiDateTableEdit::setupContents( QPainter *p, bool focused, QVariant val,
00201 QString &txt, int &align, int &x, int &y_offset, int &w, int &h )
00202 {
00203 Q_UNUSED(p);
00204 Q_UNUSED(focused);
00205 Q_UNUSED(x);
00206 Q_UNUSED(w);
00207 Q_UNUSED(h);
00208 #ifdef Q_WS_WIN
00209 y_offset = -1;
00210 #else
00211 y_offset = 0;
00212 #endif
00213 if (val.toDate().isValid())
00214 txt = m_formatter.dateToString(val.toDate());
00215
00216 align |= AlignLeft;
00217 }
00218
00219 bool KexiDateTableEdit::valueIsNull()
00220 {
00221 if (m_lineedit->text().replace(m_formatter.separator(),"").stripWhiteSpace().isEmpty())
00222 return true;
00223 return dateValue().isNull();
00224 }
00225
00226 bool KexiDateTableEdit::valueIsEmpty()
00227 {
00228 return valueIsNull();
00229 }
00230
00231 QDate KexiDateTableEdit::dateValue() const
00232 {
00233 return m_formatter.stringToDate( m_lineedit->text() );
00234 }
00235
00236 QVariant KexiDateTableEdit::value()
00237 {
00238 if (m_lineedit->text().replace(m_formatter.separator(),"").stripWhiteSpace().isEmpty())
00239 return QVariant();
00240 return dateValue();
00241
00242
00243
00244 }
00245
00246 bool KexiDateTableEdit::valueIsValid()
00247 {
00248 if (m_lineedit->text().replace(m_formatter.separator(),"").stripWhiteSpace().isEmpty())
00249 return true;
00250 return m_formatter.stringToDate( m_lineedit->text() ).isValid();
00251 }
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380 KEXI_CELLEDITOR_FACTORY_ITEM_IMPL(KexiDateEditorFactoryItem, KexiDateTableEdit)
00381
00382 #include "kexidatetableedit.moc"