kexi

kexidatetableedit.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 "kexidatetableedit.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 #include <qclipboard.h>
00038 
00039 #include <kdebug.h>
00040 #include <klocale.h>
00041 #include <kglobal.h>
00042 #include <kdatepicker.h>
00043 #include <kdatetbl.h>
00044 #include <klineedit.h>
00045 #include <kpopupmenu.h>
00046 #include <kdatewidget.h>
00047 
00048 #include <kexiutils/utils.h>
00049 
00050 
00051 KexiDateTableEdit::KexiDateTableEdit(KexiTableViewColumn &column, QWidget *parent)
00052  : KexiInputTableEdit(column, parent)
00053 {
00054     setName("KexiDateTableEdit");
00055 
00057 
00058     m_lineedit->setInputMask( m_formatter.inputMask() );
00059 }
00060 
00061 KexiDateTableEdit::~KexiDateTableEdit()
00062 {
00063 }
00064 
00065 void KexiDateTableEdit::setValueInInternalEditor(const QVariant &value)
00066 {
00067     if (value.isValid() && value.toDate().isValid()) 
00068         m_lineedit->setText( m_formatter.dateToString( value.toDate() ) );
00069     else
00070         m_lineedit->setText( QString::null );
00071 }
00072 
00073 void KexiDateTableEdit::setValueInternal(const QVariant& add_, bool removeOld)
00074 {
00075     if (removeOld) {
00076         //new date entering... just fill the line edit
00078         QString add(add_.toString());
00079         m_lineedit->setText(add);
00080         m_lineedit->setCursorPosition(add.length());
00081         return;
00082     }
00083     setValueInInternalEditor( m_origValue );
00084     m_lineedit->setCursorPosition(0); //ok?
00085 }
00086 
00087 void KexiDateTableEdit::setupContents( QPainter *p, bool focused, const 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.toDate().isValid())
00101         txt = m_formatter.dateToString(val.toDate());
00102 //      txt = val.toDate().toString(Qt::LocalDate);
00103     align |= AlignLeft;
00104 }
00105 
00106 bool KexiDateTableEdit::valueIsNull()
00107 {
00108 //  if (m_lineedit->text().replace(m_formatter.separator(),"").stripWhiteSpace().isEmpty())
00109     if (m_formatter.isEmpty(m_lineedit->text())) //empty date is null
00110         return true;
00111     return dateValue().isNull();
00112 }
00113 
00114 bool KexiDateTableEdit::valueIsEmpty()
00115 {
00116     return valueIsNull();//js OK? TODO (nonsense?)
00117 }
00118 
00119 QDate KexiDateTableEdit::dateValue() const
00120 {
00121     return m_formatter.stringToDate( m_lineedit->text() );
00122 }
00123 
00124 QVariant KexiDateTableEdit::value()
00125 {
00126     return m_formatter.stringToVariant( m_lineedit->text() );
00127 }
00128 
00129 bool KexiDateTableEdit::valueIsValid()
00130 {
00131     if (m_formatter.isEmpty(m_lineedit->text())) //empty date is valid
00132         return true;
00133     return m_formatter.stringToDate( m_lineedit->text() ).isValid();
00134 }
00135 
00136 void KexiDateTableEdit::handleCopyAction(const QVariant& value, const QVariant& visibleValue)
00137 {
00138     Q_UNUSED(visibleValue);
00139     if (!value.isNull() && value.toDate().isValid())
00140         qApp->clipboard()->setText( m_formatter.dateToString(value.toDate()) );
00141     else
00142         qApp->clipboard()->setText( QString::null );
00143 }
00144 
00145 void KexiDateTableEdit::handleAction(const QString& actionName)
00146 {
00147     const bool alreadyVisible = m_lineedit->isVisible();
00148 
00149     if (actionName=="edit_paste") {
00150         const QVariant newValue( m_formatter.stringToDate(qApp->clipboard()->text()) );
00151         if (!alreadyVisible) { //paste as the entire text if the cell was not in edit mode
00152             emit editRequested();
00153             m_lineedit->clear();
00154         }
00155         setValueInInternalEditor( newValue );
00156     }
00157     else
00158         KexiInputTableEdit::handleAction(actionName);
00159 }
00160 
00161 /*
00162 void
00163 KexiDateTableEdit::slotDateChanged(QDate date)
00164 {
00165     m_edit->setDate(date);
00166     repaint();
00167 }
00168 
00169 void
00170 KexiDateTableEdit::slotShowDatePicker()
00171 {
00172     QDate date = m_edit->date();
00173 
00174     m_datePicker->setDate(date);
00175     m_datePicker->setFocus();
00176     m_datePicker->show();
00177     m_datePicker->setFocus();
00178 }
00179 
00181 void KexiDateTableEdit::moveToFirstSection()
00182 {
00183     if (!m_dte_date_obj)
00184         return;
00185 #ifdef QDateTimeEditor_HACK
00186     if (m_dte_date)
00187         m_dte_date->setFocusSection(0);
00188 #else
00189 #ifdef Q_WS_WIN //tmp
00190     QKeyEvent ke_left(QEvent::KeyPress, Qt::Key_Left, 0, 0);
00191     for (int i=0; i<8; i++)
00192         QApplication::sendEvent( m_dte_date_obj, &ke_left );
00193 #endif
00194 #endif
00195 }
00196 
00197 bool KexiDateTableEdit::eventFilter( QObject *o, QEvent *e )
00198 {
00199     if (o==m_datePicker) {
00200         kdDebug() << e->type() << endl;
00201         switch (e->type()) {
00202         case QEvent::Hide:
00203             m_datePickerPopupMenu->hide();
00204             break;
00205         case QEvent::KeyPress:
00206         case QEvent::KeyRelease: {
00207             kdDebug() << "ok!" << endl;
00208             QKeyEvent *ke = (QKeyEvent *)e;
00209             if (ke->key()==Key_Enter || ke->key()==Key_Return) {
00210                 //accepting picker
00211                 acceptDate();
00212                 return true;
00213             }
00214             else if (ke->key()==Key_Escape) {
00215                 //canceling picker
00216                 m_datePickerPopupMenu->hide();
00217                 kdDebug() << "reject" << endl;
00218                 return true;
00219             }
00220             else m_datePickerPopupMenu->setFocus();
00221             break;
00222             }
00223         default:
00224             break;
00225         }
00226     }
00227 #ifdef Q_WS_WIN //tmp
00228     else if (e->type()==QEvent::FocusIn && o->parent() && o->parent()->parent()==m_edit
00229         && m_setNumberOnFocus >= 0 && m_dte_date_obj)
00230     {
00231         // there was a number character passed as 'add' parameter in init():
00232         moveToFirstSection();
00233         QKeyEvent ke(QEvent::KeyPress, int(Qt::Key_0)+m_setNumberOnFocus, 
00234             '0'+m_setNumberOnFocus, 0, QString::number(m_setNumberOnFocus));
00235         QApplication::sendEvent( m_dte_date_obj, &ke );
00236         m_setNumberOnFocus = -1;
00237     }
00238 #endif
00239 #ifdef QDateTimeEditor_HACK
00240     else if (e->type()==QEvent::KeyPress && m_dte_date) {
00241         QKeyEvent *ke = static_cast<QKeyEvent*>(e);
00242         if ((ke->key()==Qt::Key_Right && !m_sentEvent && cursorAtEnd())
00243             || (ke->key()==Qt::Key_Left && !m_sentEvent && cursorAtStart()))
00244         {
00245             //the editor should send this key event:
00246             m_sentEvent = true; //avoid recursion
00247             QApplication::sendEvent( this, ke );
00248             m_sentEvent = false;
00249             ke->ignore();
00250             return true;
00251         }
00252     }
00253 #endif
00254     return false;
00255 }
00256 
00257 void KexiDateTableEdit::acceptDate()
00258 {
00259     m_edit->setDate(m_datePicker->date());
00260     m_datePickerPopupMenu->hide();
00261     kdDebug() << "accept" << endl;
00262 }
00263 
00264 bool KexiDateTableEdit::cursorAtStart()
00265 {
00266 #ifdef QDateTimeEditor_HACK
00267     return m_dte_date && m_edit->hasFocus() && m_dte_date->focusSection()==0;
00268 #else
00269     return false;
00270 #endif
00271 }
00272 
00273 bool KexiDateTableEdit::cursorAtEnd()
00274 {
00275 #ifdef QDateTimeEditor_HACK
00276     return m_dte_date && m_edit->hasFocus() 
00277         && m_dte_date->focusSection()==int(m_dte_date->sectionCount()-1);
00278 #else
00279     return false;
00280 #endif
00281 }
00282 
00283 void KexiDateTableEdit::clear()
00284 {
00285     m_edit->setDate(QDate());
00286 }*/
00287 
00288 KEXI_CELLEDITOR_FACTORY_ITEM_IMPL(KexiDateEditorFactoryItem, KexiDateTableEdit)
00289 
00290 #include "kexidatetableedit.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys