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 <qpainter.h>
00026 
00027 #include <kpopupmenu.h>
00028 #include <kdatepicker.h>
00029 #include <kdatetbl.h>
00030 #include <kexiutils/utils.h>
00031 
00032 KexiDBTimeEdit::KexiDBTimeEdit(const QTime &time, QWidget *parent, const char *name)
00033  : QTimeEdit(time, parent, name), KexiFormDataItemInterface()
00034 {
00035     m_invalidState = false;
00036     setAutoAdvance(true);
00037     m_cleared = false;
00038 
00039 #ifdef QDateTimeEditor_HACK
00040     m_dte_time = KexiUtils::findFirstChild<QDateTimeEditor>(this, "QDateTimeEditor");
00041 #else
00042     m_dte_time = 0;
00043 #endif
00044 
00045     connect(this, SIGNAL(valueChanged(const QTime&)), this, SLOT(slotValueChanged(const QTime&)));
00046 }
00047 
00048 KexiDBTimeEdit::~KexiDBTimeEdit()
00049 {
00050 }
00051 
00052 void KexiDBTimeEdit::setInvalidState( const QString&)
00053 {
00054     setEnabled(false);
00055     setReadOnly(true);
00056     m_invalidState = true;
00058     if (focusPolicy() & TabFocus)
00059         setFocusPolicy(QWidget::ClickFocus);
00060 }
00061 
00062 void
00063 KexiDBTimeEdit::setEnabled(bool enabled)
00064 {
00065      // prevent the user from reenabling the widget when it is in invalid state
00066     if(enabled && m_invalidState)
00067         return;
00068     QTimeEdit::setEnabled(enabled);
00069 }
00070 
00071 void KexiDBTimeEdit::setValueInternal(const QVariant &add, bool removeOld)
00072 {
00073     m_cleared = !m_origValue.isValid();
00074 
00075     int setNumberOnFocus = -1;
00076     QTime t;
00077     QString addString(add.toString());
00078     if (removeOld) {
00079         if (!addString.isEmpty() && addString[0].latin1()>='0' && addString[0].latin1() <='9') {
00080             setNumberOnFocus = addString[0].latin1()-'0';
00081             t = QTime(setNumberOnFocus, 0, 0);
00082         }
00083     }
00084     else
00085         t = m_origValue.toTime();
00086 
00087     setTime(t);
00088 }
00089 
00090 QVariant
00091 KexiDBTimeEdit::value()
00092 {
00093     //QDateTime - a hack needed because QVariant(QTime) has broken isNull()
00094     return QVariant(QDateTime( m_cleared ? QDate() : QDate(0,1,2)/*nevermind*/, time()));
00095 }
00096 
00097 bool KexiDBTimeEdit::valueIsNull()
00098 {
00099     return !time().isValid() || time().isNull();
00100 }
00101 
00102 bool KexiDBTimeEdit::valueIsEmpty()
00103 {
00104     return m_cleared;
00105 }
00106 
00107 bool KexiDBTimeEdit::isReadOnly() const
00108 {
00111     return m_readOnly; 
00112 }
00113 
00114 void KexiDBTimeEdit::setReadOnly(bool set)
00115 {
00116     m_readOnly = set;
00117 }
00118 
00119 QWidget*
00120 KexiDBTimeEdit::widget()
00121 {
00122     return this;
00123 }
00124 
00125 bool KexiDBTimeEdit::cursorAtStart()
00126 {
00127 #ifdef QDateTimeEditor_HACK
00128     return m_dte_time && hasFocus() && m_dte_time->focusSection()==0;
00129 #else
00130     return false;
00131 #endif
00132 }
00133 
00134 bool KexiDBTimeEdit::cursorAtEnd()
00135 {
00136 #ifdef QDateTimeEditor_HACK
00137     return m_dte_time && hasFocus()
00138         && m_dte_time->focusSection()==int(m_dte_time->sectionCount()-1);
00139 #else
00140     return false;
00141 #endif
00142 }
00143 
00144 void KexiDBTimeEdit::clear()
00145 {
00146     setTime(QTime());
00147     m_cleared = true;
00148 }
00149 
00150 void
00151 KexiDBTimeEdit::slotValueChanged(const QTime&)
00152 {
00153     m_cleared = false;
00154 }
00155 
00156 #include "kexidbtimeedit.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys