kexi

kexidbdateedit.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 "kexidbdateedit.h"
00022 #include <qlayout.h>
00023 #include <qtoolbutton.h>
00024 #include <kpopupmenu.h>
00025 #include <kdatepicker.h>
00026 #include <kdatetbl.h>
00027 
00028 #include <kexiutils/utils.h>
00029 #include <kexidb/queryschema.h>
00030 
00031 KexiDBDateEdit::KexiDBDateEdit(const QDate &date, QWidget *parent, const char *name)
00032  : QWidget(parent, name), KexiFormDataItemInterface()
00033 {
00034     m_invalidState = false;
00035     m_cleared = false;
00036     m_readOnly = false;
00037 
00038     m_edit = new QDateEdit(date, this);
00039     m_edit->setAutoAdvance(true);
00040     m_edit->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::MinimumExpanding);
00041     connect( m_edit, SIGNAL(valueChanged(const QDate&)), this, SLOT(slotValueChanged(const QDate&)) );
00042     connect( m_edit, SIGNAL(valueChanged(const QDate&)), this, SIGNAL(dateChanged(const QDate&)) );
00043 
00044     QToolButton* btn = new QToolButton(this);
00045     btn->setText("...");
00046     btn->setFixedWidth( QFontMetrics(btn->font()).width(" ... ") );
00047     btn->setPopupDelay(1); //1 ms
00048 
00049 #ifdef QDateTimeEditor_HACK
00050     m_dte_date = KexiUtils::findFirstChild<QDateTimeEditor>(m_edit, "QDateTimeEditor");
00051 #else
00052     m_dte_date = 0;
00053 #endif
00054 
00055     m_datePickerPopupMenu = new KPopupMenu(0, "date_popup");
00056     connect(m_datePickerPopupMenu, SIGNAL(aboutToShow()), this, SLOT(slotShowDatePicker()));
00057     m_datePicker = new KDatePicker(m_datePickerPopupMenu, QDate::currentDate(), 0);
00058 
00059     KDateTable *dt = KexiUtils::findFirstChild<KDateTable>(m_datePicker, "KDateTable");
00060     if (dt)
00061         connect(dt, SIGNAL(tableClicked()), this, SLOT(acceptDate()));
00062     m_datePicker->setCloseButton(true);
00063     m_datePicker->installEventFilter(this);
00064     m_datePickerPopupMenu->insertItem(m_datePicker);
00065     btn->setPopup(m_datePickerPopupMenu);
00066 
00067     QHBoxLayout* layout = new QHBoxLayout(this);
00068     layout->addWidget(m_edit, 1);
00069     layout->addWidget(btn, 0);
00070 
00071     setFocusProxy(m_edit);
00072 }
00073 
00074 KexiDBDateEdit::~KexiDBDateEdit()
00075 {
00076 }
00077 
00078 void KexiDBDateEdit::setInvalidState( const QString& )
00079 {
00080     setEnabled(false);
00081     setReadOnly(true);
00082     m_invalidState = true;
00084     if (focusPolicy() & TabFocus)
00085         setFocusPolicy(QWidget::ClickFocus);
00086 }
00087 
00088 void
00089 KexiDBDateEdit::setEnabled(bool enabled)
00090 {
00091      // prevent the user from reenabling the widget when it is in invalid state
00092     if(enabled && m_invalidState)
00093         return;
00094     QWidget::setEnabled(enabled);
00095 }
00096 
00097 void KexiDBDateEdit::setValueInternal(const QVariant &add, bool removeOld)
00098 {
00099     int setNumberOnFocus = -1;
00100     QDate d;
00101     QString addString(add.toString());
00102     if (removeOld) {
00103         if (!addString.isEmpty() && addString[0].latin1()>='0' && addString[0].latin1() <='9') {
00104             setNumberOnFocus = addString[0].latin1()-'0';
00105             d = QDate(setNumberOnFocus*1000, 1, 1);
00106         }
00107     }
00108     else
00109         d = m_origValue.toDate();
00110 
00111     m_edit->setDate(d);
00112 }
00113 
00114 QVariant
00115 KexiDBDateEdit::value()
00116 {
00117     return QVariant(m_edit->date());
00118 }
00119 
00120 bool KexiDBDateEdit::valueIsNull()
00121 {
00122     return !m_edit->date().isValid() || m_edit->date().isNull();
00123 }
00124 
00125 bool KexiDBDateEdit::valueIsEmpty()
00126 {
00127     return m_cleared;
00128 }
00129 
00130 bool KexiDBDateEdit::isReadOnly() const
00131 {
00134     return m_readOnly; 
00135 }
00136 
00137 void KexiDBDateEdit::setReadOnly(bool set)
00138 {
00139     m_readOnly = set;
00140 }
00141 
00142 QWidget*
00143 KexiDBDateEdit::widget()
00144 {
00145     return this;
00146 }
00147 
00148 bool KexiDBDateEdit::cursorAtStart()
00149 {
00150 #ifdef QDateTimeEditor_HACK
00151     return m_dte_date && m_edit->hasFocus() && m_dte_date->focusSection()==0;
00152 #else
00153     return false;
00154 #endif
00155 }
00156 
00157 bool KexiDBDateEdit::cursorAtEnd()
00158 {
00159 #ifdef QDateTimeEditor_HACK
00160     return m_dte_date && m_edit->hasFocus()
00161         && m_dte_date->focusSection()==int(m_dte_date->sectionCount()-1);
00162 #else
00163     return false;
00164 #endif
00165 }
00166 
00167 void KexiDBDateEdit::clear()
00168 {
00169     m_edit->setDate(QDate());
00170     m_cleared = true;
00171 }
00172 
00173 void
00174 KexiDBDateEdit::slotValueChanged(const QDate&)
00175 {
00176     m_cleared = false;
00177 }
00178 
00179 void
00180 KexiDBDateEdit::slotShowDatePicker()
00181 {
00182     QDate date = m_edit->date();
00183 
00184     m_datePicker->setDate(date);
00185     m_datePicker->setFocus();
00186     m_datePicker->show();
00187     m_datePicker->setFocus();
00188 }
00189 
00190 void
00191 KexiDBDateEdit::acceptDate()
00192 {
00193     m_edit->setDate(m_datePicker->date());
00194     m_datePickerPopupMenu->hide();
00195 }
00196 
00197 bool
00198 KexiDBDateEdit::eventFilter(QObject *o, QEvent *e)
00199 {
00200     if (o != m_datePicker)
00201         return false;
00202 
00203     switch (e->type()) {
00204         case QEvent::Hide:
00205             m_datePickerPopupMenu->hide();
00206             break;
00207         case QEvent::KeyPress:
00208         case QEvent::KeyRelease: {
00209             QKeyEvent *ke = (QKeyEvent *)e;
00210             if (ke->key()==Key_Enter || ke->key()==Key_Return) {
00211                 //accepting picker
00212                 acceptDate();
00213                 return true;
00214             }
00215             else if (ke->key()==Key_Escape) {
00216                 //cancelling picker
00217                 m_datePickerPopupMenu->hide();
00218                 return true;
00219             }
00220             else
00221                  m_datePickerPopupMenu->setFocus();
00222             break;
00223         }
00224         default:
00225             break;
00226     }
00227     return false;
00228 }
00229 
00230 #include "kexidbdateedit.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys