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 #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 KexiDateTimeTableEdit::KexiDateTimeTableEdit(KexiTableViewColumn &column, QWidget *parent)
00051  : KexiInputTableEdit(column, parent)
00052 {
00053     setName("KexiDateTimeTableEdit");
00054 
00056 
00057     m_lineedit->setInputMask( 
00058         dateTimeInputMask( m_dateFormatter, m_timeFormatter ) );
00059 }
00060 
00061 KexiDateTimeTableEdit::~KexiDateTimeTableEdit()
00062 {
00063 }
00064 
00065 void KexiDateTimeTableEdit::setValueInInternalEditor(const QVariant &value)
00066 {
00067     if (value.isValid() && value.toDateTime().isValid()) 
00068         m_lineedit->setText( 
00069             m_dateFormatter.dateToString( value.toDateTime().date() ) + " " +
00070             m_timeFormatter.timeToString( value.toDateTime().time() ) );
00071     else
00072         m_lineedit->setText( QString::null );
00073 }
00074 
00075 void KexiDateTimeTableEdit::setValueInternal(const QVariant& add_, bool removeOld)
00076 {
00077     if (removeOld) {
00078         //new time entering... just fill the line edit
00080         QString add(add_.toString());
00081         m_lineedit->setText(add);
00082         m_lineedit->setCursorPosition(add.length());
00083         return;
00084     }
00085     setValueInInternalEditor( m_origValue );
00086     m_lineedit->setCursorPosition(0); //ok?
00087 }
00088 
00089 void KexiDateTimeTableEdit::setupContents( QPainter *p, bool focused, const QVariant& val, 
00090     QString &txt, int &align, int &x, int &y_offset, int &w, int &h )
00091 {
00092     Q_UNUSED(p);
00093     Q_UNUSED(focused);
00094     Q_UNUSED(x);
00095     Q_UNUSED(w);
00096     Q_UNUSED(h);
00097 #ifdef Q_WS_WIN
00098     y_offset = -1;
00099 #else
00100     y_offset = 0;
00101 #endif
00102     if (val.toDateTime().isValid())
00103         txt = m_dateFormatter.dateToString(val.toDateTime().date()) + " " 
00104             + m_timeFormatter.timeToString(val.toDateTime().time());
00105     align |= AlignLeft;
00106 }
00107 
00108 bool KexiDateTimeTableEdit::valueIsNull()
00109 {
00110     if (textIsEmpty())
00111         return true;
00112     return !stringToDateTime(m_dateFormatter, m_timeFormatter, m_lineedit->text()).isValid();
00113 }
00114 
00115 bool KexiDateTimeTableEdit::valueIsEmpty()
00116 {
00117     return valueIsNull();//js OK? TODO (nonsense?)
00118 }
00119 
00120 QVariant KexiDateTimeTableEdit::value()
00121 {
00122     if (textIsEmpty())
00123         return QVariant();
00124     return stringToDateTime(m_dateFormatter, m_timeFormatter, m_lineedit->text());
00125 }
00126 
00127 bool KexiDateTimeTableEdit::valueIsValid()
00128 {
00129     return dateTimeIsValid( m_dateFormatter, m_timeFormatter, m_lineedit->text() );
00130 }
00131 
00132 bool KexiDateTimeTableEdit::textIsEmpty() const
00133 {
00134     return dateTimeIsEmpty( m_dateFormatter, m_timeFormatter, m_lineedit->text() );
00135 }
00136 
00137 void KexiDateTimeTableEdit::handleCopyAction(const QVariant& value, const QVariant& visibleValue)
00138 {
00139     Q_UNUSED(visibleValue);
00140     if (!value.isNull() && value.toDateTime().isValid())
00141         qApp->clipboard()->setText( m_dateFormatter.dateToString(value.toDateTime().date()) + " " 
00142             + m_timeFormatter.timeToString(value.toDateTime().time()) );
00143     else
00144         qApp->clipboard()->setText( QString::null );
00145 }
00146 
00147 void KexiDateTimeTableEdit::handleAction(const QString& actionName)
00148 {
00149     const bool alreadyVisible = m_lineedit->isVisible();
00150 
00151     if (actionName=="edit_paste") {
00152         const QVariant newValue( stringToDateTime(m_dateFormatter, m_timeFormatter, qApp->clipboard()->text()) );
00153         if (!alreadyVisible) { //paste as the entire text if the cell was not in edit mode
00154             emit editRequested();
00155             m_lineedit->clear();
00156         }
00157         setValueInInternalEditor( newValue );
00158     }
00159     else
00160         KexiInputTableEdit::handleAction(actionName);
00161 }
00162 
00163 KEXI_CELLEDITOR_FACTORY_ITEM_IMPL(KexiDateTimeEditorFactoryItem, KexiDateTimeTableEdit)
00164 
00165 #include "kexidatetimetableedit.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys