00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "kexitimetableedit.h"
00021
00022 #include <qapplication.h>
00023 #include <qpainter.h>
00024 #include <qvariant.h>
00025 #include <qrect.h>
00026 #include <qpalette.h>
00027 #include <qcolor.h>
00028 #include <qfontmetrics.h>
00029 #include <qdatetime.h>
00030 #include <qcursor.h>
00031 #include <qpoint.h>
00032 #include <qlayout.h>
00033 #include <qtoolbutton.h>
00034 #include <qdatetimeedit.h>
00035
00036 #include <kdebug.h>
00037 #include <klocale.h>
00038 #include <kglobal.h>
00039 #include <kdatepicker.h>
00040 #include <kdatetbl.h>
00041 #include <klineedit.h>
00042 #include <kpopupmenu.h>
00043 #include <kdatewidget.h>
00044
00045 #include <kexiutils/utils.h>
00046
00047 KexiTimeFormatter::KexiTimeFormatter()
00048 : m_hmsRegExp("(\\d*):(\\d*):(\\d*).*( am| pm){,1}", false)
00049 , m_hmRegExp("(\\d*):(\\d*).*( am| pm){,1}", false)
00050 {
00051 QString tf( KGlobal::locale()->timeFormat() );
00052
00053 QString hourVariable, minVariable, secVariable;
00054
00055
00056 m_24h = true;
00057 m_hoursWithLeadingZero = true;
00058 m_hourpos = tf.find("%H", 0, true);
00059 if (m_hourpos>=0) {
00060 m_24h = true;
00061 m_hoursWithLeadingZero = true;
00062 }
00063 else {
00064 m_hourpos = tf.find("%k", 0, true);
00065 if (m_hourpos>=0) {
00066 m_24h = true;
00067 m_hoursWithLeadingZero = false;
00068 }
00069 else {
00070 m_hourpos = tf.find("%I", 0, true);
00071 if (m_hourpos>=0) {
00072 m_24h = false;
00073 m_hoursWithLeadingZero = true;
00074 }
00075 else {
00076 m_hourpos = tf.find("%l", 0, true);
00077 if (m_hourpos>=0) {
00078 m_24h = false;
00079 m_hoursWithLeadingZero = false;
00080 }
00081 }
00082 }
00083 }
00084 m_minpos = tf.find("%M", 0, true);
00085 m_secpos = tf.find("%S", 0, true);
00086 m_ampmpos = tf.find("%p", 0, true);
00087
00088 if (m_hourpos<0 || m_minpos<0) {
00089
00090 tf = "%H:%M:%S";
00091 m_24h = true;
00092 m_hoursWithLeadingZero = false;
00093 m_hourpos = 0;
00094 m_minpos = 3;
00095 m_secpos = m_minpos + 3;
00096 m_ampmpos = -1;
00097 }
00098 hourVariable = tf.mid(m_hourpos, 2);
00099
00100 m_inputMask = tf;
00101
00102
00103
00104 m_inputMask.replace( hourVariable, "99" );
00105 m_inputMask.replace( "%M", "99" );
00106 m_inputMask.replace( "%S", "00" );
00107 m_inputMask.replace( "%p", "AA" );
00108 m_inputMask += ";_";
00109
00110 m_outputFormat = tf;
00111 }
00112
00113 KexiTimeFormatter::~KexiTimeFormatter()
00114 {
00115 }
00116
00117 QString KexiTimeFormatter::timeToString( const QTime& time ) const
00118 {
00119 if (!time.isValid())
00120 return QString::null;
00121
00122 QString s(m_outputFormat);
00123 if (m_24h) {
00124 if (m_hoursWithLeadingZero)
00125 s.replace( "%H", QString::fromLatin1(time.hour()<10 ? "0" : "") + QString::number(time.hour()) );
00126 else
00127 s.replace( "%k", QString::number(time.hour()) );
00128 }
00129 else {
00130 int time12 = (time.hour()>12) ? (time.hour()-12) : time.hour();
00131 if (m_hoursWithLeadingZero)
00132 s.replace( "%I", QString::fromLatin1(time12<10 ? "0" : "") + QString::number(time12) );
00133 else
00134 s.replace( "%l", QString::number(time12) );
00135 }
00136 s.replace( "%M", QString::fromLatin1(time.minute()<10 ? "0" : "") + QString::number(time.minute()) );
00137 if (m_secpos>=0)
00138 s.replace( "%S", QString::fromLatin1(time.second()<10 ? "0" : "") + QString::number(time.second()) );
00139 if (m_ampmpos>=0)
00140 s.replace( "%p", KGlobal::locale()->translate( time.hour()>=12 ? "pm" : "am") );
00141 return s;
00142 }
00143
00144 QTime KexiTimeFormatter::stringToTime( const QString& str )
00145 {
00146 int hour, min, sec;
00147 bool pm = false;
00148
00149 bool tryWithoutSeconds = true;
00150 if (m_secpos>=0) {
00151 if (-1 != m_hmsRegExp.search(str)) {
00152 hour = m_hmsRegExp.cap(1).toInt();
00153 min = m_hmsRegExp.cap(2).toInt();
00154 sec = m_hmsRegExp.cap(3).toInt();
00155 if (m_ampmpos >= 0 && m_hmsRegExp.numCaptures()>3)
00156 pm = m_hmsRegExp.cap(4).stripWhiteSpace().lower()=="pm";
00157 tryWithoutSeconds = false;
00158 }
00159 }
00160 if (tryWithoutSeconds) {
00161 if (-1 == m_hmRegExp.search(str))
00162 return QTime(99,0,0);
00163 hour = m_hmRegExp.cap(1).toInt();
00164 min = m_hmRegExp.cap(2).toInt();
00165 sec = 0;
00166 if (m_ampmpos >= 0 && m_hmRegExp.numCaptures()>2)
00167 pm = m_hmsRegExp.cap(4).lower()=="pm";
00168 }
00169
00170 if (pm && hour < 12)
00171 hour += 12;
00172 return QTime(hour, min, sec);
00173 }
00174
00175
00176
00177 KexiTimeTableEdit::KexiTimeTableEdit(KexiTableViewColumn &column, QScrollView *parent)
00178 : KexiInputTableEdit(column, parent)
00179 {
00180 setName("KexiTimeTableEdit");
00181
00183
00184 m_lineedit->setInputMask( m_formatter.inputMask() );
00185 }
00186
00187 KexiTimeTableEdit::~KexiTimeTableEdit()
00188 {
00189 }
00190
00191 void KexiTimeTableEdit::setValueInternal(const QVariant& add_, bool removeOld)
00192 {
00193 if (removeOld) {
00194
00196 QString add(add_.toString());
00197 m_lineedit->setText(add);
00198 m_lineedit->setCursorPosition(add.length());
00199 return;
00200 }
00201 m_lineedit->setText(
00202 m_formatter.timeToString(
00203
00204 m_origValue.isValid() ? m_origValue.toTime() : QTime(99,0,0)
00205 )
00206 );
00207 m_lineedit->setCursorPosition(0);
00208 }
00209
00210 void KexiTimeTableEdit::setupContents( QPainter *p, bool focused, QVariant val,
00211 QString &txt, int &align, int &x, int &y_offset, int &w, int &h )
00212 {
00213 Q_UNUSED(p);
00214 Q_UNUSED(focused);
00215 Q_UNUSED(x);
00216 Q_UNUSED(w);
00217 Q_UNUSED(h);
00218 #ifdef Q_WS_WIN
00219 y_offset = -1;
00220 #else
00221 y_offset = 0;
00222 #endif
00223 if (!val.isNull() && val.canCast(QVariant::Time))
00224 txt = m_formatter.timeToString(val.toTime());
00225 align |= AlignLeft;
00226 }
00227
00228 bool KexiTimeTableEdit::valueIsNull()
00229 {
00230 if (m_lineedit->text().replace(':',"").stripWhiteSpace().isEmpty())
00231 return true;
00232 return !timeValue().isValid();
00233 }
00234
00235 bool KexiTimeTableEdit::valueIsEmpty()
00236 {
00237 return valueIsNull();
00238 }
00239
00240 QTime KexiTimeTableEdit::timeValue()
00241 {
00242 return m_formatter.stringToTime( m_lineedit->text() );
00243 }
00244
00245 QVariant KexiTimeTableEdit::value()
00246 {
00247 if (m_lineedit->text().replace(':',"").stripWhiteSpace().isEmpty())
00248 return QVariant();
00249 return timeValue();
00250
00251
00252
00253 }
00254
00255 bool KexiTimeTableEdit::valueIsValid()
00256 {
00257 if (m_lineedit->text().replace(':',"").stripWhiteSpace().isEmpty())
00258 return true;
00259 return m_formatter.stringToTime( m_lineedit->text() ).isValid();
00260 }
00261
00262 KEXI_CELLEDITOR_FACTORY_ITEM_IMPL(KexiTimeEditorFactoryItem, KexiTimeTableEdit)
00263
00264 #include "kexitimetableedit.moc"