kexi

kexidbtimeedit.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2005 Cedric Pasteur <cedric.pasteur@free.fr>
00003    Copyright (C) 2004-2005 Jaroslaw Staniek <js@iidea.pl>
00004 
00005    This program is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this program; see the file COPYING.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "kexidbtimeedit.h"
00022 
00023 #include <qtoolbutton.h>
00024 #include <qlayout.h>
00025 #include <kpopupmenu.h>
00026 #include <kdatepicker.h>
00027 #include <kdatetbl.h>
00028 #include <kexiutils/utils.h>
00029 
00030 KexiDBTimeEdit::KexiDBTimeEdit(const QTime &time, QWidget *parent, const char *name)
00031  : QTimeEdit(time, parent, name), KexiFormDataItemInterface()
00032 {
00033     m_invalidState = false;
00034     setAutoAdvance(true);
00035     m_cleared = false;
00036 
00037 #ifdef QDateTimeEditor_HACK
00038     m_dte_time = KexiUtils::findFirstChild<QDateTimeEditor>(this, "QDateTimeEditor");
00039 #else
00040     m_dte_time = 0;
00041 #endif
00042 
00043     connect(this, SIGNAL(valueChanged(const QTime&)), this, SLOT(slotValueChanged(const QTime&)));
00044 }
00045 
00046 KexiDBTimeEdit::~KexiDBTimeEdit()
00047 {
00048 }
00049 
00050 void KexiDBTimeEdit::setInvalidState( const QString&)
00051 {
00052     setEnabled(false);
00053     setReadOnly(true);
00054     m_invalidState = true;
00056     if (focusPolicy() & TabFocus)
00057         setFocusPolicy(QWidget::ClickFocus);
00058 }
00059 
00060 void
00061 KexiDBTimeEdit::setEnabled(bool enabled)
00062 {
00063      // prevent the user from reenabling the widget when it is in invalid state
00064     if(enabled && m_invalidState)
00065         return;
00066     QTimeEdit::setEnabled(enabled);
00067 }
00068 
00069 void KexiDBTimeEdit::setValueInternal(const QVariant &add, bool removeOld)
00070 {
00071     m_cleared = !m_origValue.isValid();
00072 
00073     int setNumberOnFocus = -1;
00074     QTime t;
00075     QString addString(add.toString());
00076     if (removeOld) {
00077         if (!addString.isEmpty() && addString[0].latin1()>='0' && addString[0].latin1() <='9') {
00078             setNumberOnFocus = addString[0].latin1()-'0';
00079             t = QTime(setNumberOnFocus, 0, 0);
00080         }
00081     }
00082     else
00083         t = m_origValue.toTime();
00084 
00085     setTime(t);
00086 }
00087 
00088 QVariant
00089 KexiDBTimeEdit::value()
00090 {
00091     //QDateTime - a hack needed because QVariant(QTime) has broken isNull()
00092     return QVariant(QDateTime( m_cleared ? QDate() : QDate(0,1,2)/*nevermind*/, time()));
00093 }
00094 
00095 bool KexiDBTimeEdit::valueIsNull()
00096 {
00097     return !time().isValid() || time().isNull();
00098 }
00099 
00100 bool KexiDBTimeEdit::valueIsEmpty()
00101 {
00102     return m_cleared;
00103 }
00104 
00105 bool KexiDBTimeEdit::isReadOnly() const
00106 {
00109     return m_readOnly; 
00110 }
00111 
00112 void KexiDBTimeEdit::setReadOnly(bool set)
00113 {
00114     m_readOnly = set;
00115 }
00116 
00117 QWidget*
00118 KexiDBTimeEdit::widget()
00119 {
00120     return this;
00121 }
00122 
00123 bool KexiDBTimeEdit::cursorAtStart()
00124 {
00125 #ifdef QDateTimeEditor_HACK
00126     return m_dte_time && hasFocus() && m_dte_time->focusSection()==0;
00127 #else
00128     return false;
00129 #endif
00130 }
00131 
00132 bool KexiDBTimeEdit::cursorAtEnd()
00133 {
00134 #ifdef QDateTimeEditor_HACK
00135     return m_dte_time && hasFocus()
00136         && m_dte_time->focusSection()==int(m_dte_time->sectionCount()-1);
00137 #else
00138     return false;
00139 #endif
00140 }
00141 
00142 void KexiDBTimeEdit::clear()
00143 {
00144     setTime(QTime());
00145     m_cleared = true;
00146 }
00147 
00148 void
00149 KexiDBTimeEdit::slotValueChanged(const QTime&)
00150 {
00151     m_cleared = false;
00152 }
00153 
00154 #include "kexidbtimeedit.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys