kexi

kexitimetableedit.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004,2006 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This program is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this program; see the file COPYING.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  */
00019 
00020 #include "kexitimetableedit.h"
00021 
00022 #include <qapplication.h>
00023 #include <qpainter.h>
00024 #include <qvariant.h>
00025 #include <qrect.h>
00026 #include <qpalette.h>
00027 #include <qcolor.h>
00028 #include <qfontmetrics.h>
00029 #include <qdatetime.h>
00030 #include <qcursor.h>
00031 #include <qpoint.h>
00032 #include <qlayout.h>
00033 #include <qtoolbutton.h>
00034 #include <qdatetimeedit.h>
00035 #include <qclipboard.h>
00036 
00037 #include <kdebug.h>
00038 #include <klocale.h>
00039 #include <kglobal.h>
00040 #include <kdatepicker.h>
00041 #include <kdatetbl.h>
00042 #include <klineedit.h>
00043 #include <kpopupmenu.h>
00044 #include <kdatewidget.h>
00045 
00046 #include <kexiutils/utils.h>
00047 
00048 KexiTimeTableEdit::KexiTimeTableEdit(KexiTableViewColumn &column, QWidget *parent)
00049  : KexiInputTableEdit(column, parent)
00050 {
00051     setName("KexiTimeTableEdit");
00052 
00054 
00055     m_lineedit->setInputMask( m_formatter.inputMask() );
00056 }
00057 
00058 KexiTimeTableEdit::~KexiTimeTableEdit()
00059 {
00060 }
00061 
00062 void KexiTimeTableEdit::setValueInInternalEditor(const QVariant &value)
00063 {
00064     if (value.isValid() && value.toTime().isValid()) 
00065         m_lineedit->setText( m_formatter.timeToString( value.toTime() ) );
00066     else
00067         m_lineedit->setText( QString::null );
00068 }
00069 
00070 void KexiTimeTableEdit::setValueInternal(const QVariant& add_, bool removeOld)
00071 {
00072     if (removeOld) {
00073         //new time entering... just fill the line edit
00075         QString add(add_.toString());
00076         m_lineedit->setText(add);
00077         m_lineedit->setCursorPosition(add.length());
00078         return;
00079     }
00080     setValueInInternalEditor( m_origValue );
00081     m_lineedit->setCursorPosition(0); //ok?
00082 }
00083 
00084 void KexiTimeTableEdit::setupContents( QPainter *p, bool focused, const QVariant& val,  
00085     QString &txt, int &align, int &x, int &y_offset, int &w, int &h )
00086 {
00087     Q_UNUSED(p);
00088     Q_UNUSED(focused);
00089     Q_UNUSED(x);
00090     Q_UNUSED(w);
00091     Q_UNUSED(h);
00092 #ifdef Q_WS_WIN
00093     y_offset = -1;
00094 #else
00095     y_offset = 0;
00096 #endif
00097     if (!val.isNull() && val.canCast(QVariant::Time))
00098         txt = m_formatter.timeToString(val.toTime());
00099     align |= AlignLeft;
00100 }
00101 
00102 bool KexiTimeTableEdit::valueIsNull()
00103 {
00104     if (m_formatter.isEmpty( m_lineedit->text() )) //empty time is null
00105         return true;
00106     return !timeValue().isValid();
00107 }
00108 
00109 bool KexiTimeTableEdit::valueIsEmpty()
00110 {
00111     return valueIsNull();// OK? TODO (nonsense?)
00112 }
00113 
00114 QTime KexiTimeTableEdit::timeValue()
00115 {
00116     return m_formatter.stringToTime( m_lineedit->text() );
00117 }
00118 
00119 QVariant KexiTimeTableEdit::value()
00120 {
00121     return m_formatter.stringToVariant( m_lineedit->text() );
00122 }
00123 
00124 bool KexiTimeTableEdit::valueIsValid()
00125 {
00126     if (m_formatter.isEmpty( m_lineedit->text() )) //empty time is valid
00127         return true;
00128     return m_formatter.stringToTime( m_lineedit->text() ).isValid();
00129 }
00130 
00131 void KexiTimeTableEdit::handleCopyAction(const QVariant& value, const QVariant& visibleValue)
00132 {
00133     Q_UNUSED(visibleValue);
00134     if (!value.isNull() && value.toTime().isValid())
00135         qApp->clipboard()->setText( m_formatter.timeToString(value.toTime()) );
00136     else
00137         qApp->clipboard()->setText( QString::null );
00138 }
00139 
00140 void KexiTimeTableEdit::handleAction(const QString& actionName)
00141 {
00142     const bool alreadyVisible = m_lineedit->isVisible();
00143 
00144     if (actionName=="edit_paste") {
00145         const QVariant newValue( m_formatter.stringToTime( qApp->clipboard()->text() ) );
00146         if (!alreadyVisible) { //paste as the entire text if the cell was not in edit mode
00147             emit editRequested();
00148             m_lineedit->clear();
00149         }
00150         setValueInInternalEditor( newValue );
00151     }
00152     else
00153         KexiInputTableEdit::handleAction(actionName);
00154 }
00155 
00156 KEXI_CELLEDITOR_FACTORY_ITEM_IMPL(KexiTimeEditorFactoryItem, KexiTimeTableEdit)
00157 
00158 #include "kexitimetableedit.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys