kexi

kexidatetimetableedit.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2002   Lucijan Busch <lucijan@gmx.at>
00003    Copyright (C) 2003   Daniel Molkentin <molkentin@kde.org>
00004    Copyright (C) 2003-2004,2006 Jaroslaw Staniek <js@iidea.pl>
00005 
00006    This program is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License as published by the Free Software Foundation; either
00009    version 2 of the License, or (at your option) any later version.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this program; see the file COPYING.  If not, write to
00018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020  */
00021 
00022 #include "kexidatetimetableedit.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 KexiDateTimeTableEdit::KexiDateTimeTableEdit(KexiTableViewColumn &column, QScrollView *parent)
00050  : KexiInputTableEdit(column, parent)
00051 {
00052     setName("KexiDateTimeTableEdit");
00053 
00055 
00056     QString mask(m_dateFormatter.inputMask());
00057     mask.truncate(m_dateFormatter.inputMask().length()-2);
00058     m_lineedit->setInputMask( mask + " " + m_timeFormatter.inputMask() );
00059 }
00060 
00061 KexiDateTimeTableEdit::~KexiDateTimeTableEdit()
00062 {
00063 }
00064 
00065 void KexiDateTimeTableEdit::setValueInternal(const QVariant& add_, bool removeOld)
00066 {
00067     if (removeOld) {
00068         //new time entering... just fill the line edit
00070         QString add(add_.toString());
00071         m_lineedit->setText(add);
00072         m_lineedit->setCursorPosition(add.length());
00073         return;
00074     }
00075     if (m_origValue.isValid()) {
00076         m_lineedit->setText(
00077             m_dateFormatter.dateToString( m_origValue.toDateTime().date() ) + " " +
00078             m_timeFormatter.timeToString( m_origValue.toDateTime().time() )
00079         );
00080     }
00081     else {
00082         m_lineedit->setText( QString::null );
00083     }
00084     m_lineedit->setCursorPosition(0); //ok?
00085 }
00086 
00087 void KexiDateTimeTableEdit::setupContents( QPainter *p, bool focused, QVariant val, 
00088     QString &txt, int &align, int &x, int &y_offset, int &w, int &h )
00089 {
00090     Q_UNUSED(p);
00091     Q_UNUSED(focused);
00092     Q_UNUSED(x);
00093     Q_UNUSED(w);
00094     Q_UNUSED(h);
00095 #ifdef Q_WS_WIN
00096     y_offset = -1;
00097 #else
00098     y_offset = 0;
00099 #endif
00100     if (val.toDateTime().isValid())
00101         txt = m_dateFormatter.dateToString(val.toDateTime().date()) + " " 
00102             + m_timeFormatter.timeToString(val.toDateTime().time());
00103     align |= AlignLeft;
00104 }
00105 
00106 bool KexiDateTimeTableEdit::valueIsNull()
00107 {
00108     if (textIsEmpty())
00109         return true;
00110     return !dateTimeValue().isValid();
00111 }
00112 
00113 bool KexiDateTimeTableEdit::valueIsEmpty()
00114 {
00115     return valueIsNull();//js OK? TODO (nonsense?)
00116 }
00117 
00118 QDateTime KexiDateTimeTableEdit::dateTimeValue()
00119 {
00120     QString s = m_lineedit->text().stripWhiteSpace();
00121     const int timepos = s.find(" ");
00122     const bool emptyTime = timepos >= 0 && s.mid(timepos+1).replace(':',"").stripWhiteSpace().isEmpty();
00123     if (emptyTime)
00124         s = s.left(timepos);
00125     if (timepos>0 && !emptyTime) {
00126         return QDateTime(
00127             m_dateFormatter.stringToDate( s.left(timepos) ),
00128             m_timeFormatter.stringToTime( s.mid(timepos+1) )
00129         );
00130     }
00131     else {
00132         return QDateTime(
00133             m_dateFormatter.stringToDate( s ),
00134             QTime(0,0,0)
00135         );
00136     }
00137 }
00138 
00139 QVariant KexiDateTimeTableEdit::value()
00140 {
00141     if (textIsEmpty())
00142         return QVariant();
00143     return dateTimeValue();
00144 }
00145 
00146 bool KexiDateTimeTableEdit::valueIsValid()
00147 {
00148     QString s(m_lineedit->text());
00149     int timepos = s.find(" ");
00150     const bool emptyTime = timepos >= 0 && s.mid(timepos+1).replace(':',"").stripWhiteSpace().isEmpty();
00151     if (timepos >= 0 && s.left(timepos).replace(m_dateFormatter.separator(), "").stripWhiteSpace().isEmpty()
00152         && emptyTime)
00153         //empty date/time is valid
00154         return true;
00155     return timepos>=0 && m_dateFormatter.stringToDate( s.left(timepos) ).isValid()
00156         && (emptyTime /*date without time is also valid*/ || m_timeFormatter.stringToTime( s.mid(timepos+1) ).isValid());
00157 }
00158 
00159 bool KexiDateTimeTableEdit::textIsEmpty() const
00160 {
00161     QString s(m_lineedit->text());
00162     int timepos = s.find(" ");
00163     const bool emptyTime = timepos >= 0 && s.mid(timepos+1).replace(':',"").stripWhiteSpace().isEmpty();
00164     return (timepos >= 0 && s.left(timepos).replace(m_dateFormatter.separator(), "").stripWhiteSpace().isEmpty()
00165         && emptyTime);
00166 }
00167 
00168 KEXI_CELLEDITOR_FACTORY_ITEM_IMPL(KexiDateTimeEditorFactoryItem, KexiDateTimeTableEdit)
00169 
00170 #include "kexidatetimetableedit.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys