kexitimetableedit.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004,2006 Jaroslaw Staniek <js@iidea.pl>
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "kexitimetableedit.h"
21
22#include <tqapplication.h>
23#include <tqpainter.h>
24#include <tqvariant.h>
25#include <tqrect.h>
26#include <tqpalette.h>
27#include <tqcolor.h>
28#include <tqfontmetrics.h>
29#include <tqdatetime.h>
30#include <tqcursor.h>
31#include <tqpoint.h>
32#include <tqlayout.h>
33#include <tqtoolbutton.h>
34#include <tqdatetimeedit.h>
35#include <tqclipboard.h>
36
37#include <kdebug.h>
38#include <tdelocale.h>
39#include <tdeglobal.h>
40#include <kdatepicker.h>
41#include <kdatetbl.h>
42#include <klineedit.h>
43#include <tdepopupmenu.h>
44#include <kdatewidget.h>
45
46#include <kexiutils/utils.h>
47
49 : KexiInputTableEdit(column, parent)
50{
51 setName("KexiTimeTableEdit");
52
54
55 m_lineedit->setInputMask( m_formatter.inputMask() );
56}
57
58KexiTimeTableEdit::~KexiTimeTableEdit()
59{
60}
61
63{
64 if (value.isValid() && value.toTime().isValid())
65 m_lineedit->setText( m_formatter.timeToString( value.toTime() ) );
66 else
67 m_lineedit->setText( TQString() );
68}
69
70void KexiTimeTableEdit::setValueInternal(const TQVariant& add_, bool removeOld)
71{
72 if (removeOld) {
73 //new time entering... just fill the line edit
75 TQString add(add_.toString());
76 m_lineedit->setText(add);
77 m_lineedit->setCursorPosition(add.length());
78 return;
79 }
80 setValueInInternalEditor( m_origValue );
81 m_lineedit->setCursorPosition(0); //ok?
82}
83
84void KexiTimeTableEdit::setupContents( TQPainter *p, bool focused, const TQVariant& val,
85 TQString &txt, int &align, int &x, int &y_offset, int &w, int &h )
86{
87 Q_UNUSED(p);
88 Q_UNUSED(focused);
89 Q_UNUSED(x);
90 Q_UNUSED(w);
91 Q_UNUSED(h);
92#ifdef TQ_WS_WIN
93 y_offset = -1;
94#else
95 y_offset = 0;
96#endif
97 if (!val.isNull() && val.canCast(TQVariant::Time))
98 txt = m_formatter.timeToString(val.toTime());
99 align |= AlignLeft;
100}
101
103{
104 if (m_formatter.isEmpty( m_lineedit->text() )) //empty time is null
105 return true;
106 return !timeValue().isValid();
107}
108
110{
111 return valueIsNull();// OK? TODO (nonsense?)
112}
113
114TQTime KexiTimeTableEdit::timeValue()
115{
116 return m_formatter.stringToTime( m_lineedit->text() );
117}
118
120{
121 return m_formatter.stringToVariant( m_lineedit->text() );
122}
123
125{
126 if (m_formatter.isEmpty( m_lineedit->text() )) //empty time is valid
127 return true;
128 return m_formatter.stringToTime( m_lineedit->text() ).isValid();
129}
130
131void KexiTimeTableEdit::handleCopyAction(const TQVariant& value, const TQVariant& visibleValue)
132{
133 Q_UNUSED(visibleValue);
134 if (!value.isNull() && value.toTime().isValid())
135 tqApp->clipboard()->setText( m_formatter.timeToString(value.toTime()) );
136 else
137 tqApp->clipboard()->setText( TQString() );
138}
139
140void KexiTimeTableEdit::handleAction(const TQString& actionName)
141{
142 const bool alreadyVisible = m_lineedit->isVisible();
143
144 if (actionName=="edit_paste") {
145 const TQVariant newValue( m_formatter.stringToTime( tqApp->clipboard()->text() ) );
146 if (!alreadyVisible) { //paste as the entire text if the cell was not in edit mode
147 emit editRequested();
148 m_lineedit->clear();
149 }
150 setValueInInternalEditor( newValue );
151 }
152 else
154}
155
156KEXI_CELLEDITOR_FACTORY_ITEM_IMPL(KexiTimeEditorFactoryItem, KexiTimeTableEdit)
157
158#include "kexitimetableedit.moc"
virtual TQVariant visibleValue()
General purpose cell editor using line edit widget.
KLineEdit * m_lineedit
orig. Line Edit's text after conversion - for easy comparing
virtual void handleAction(const TQString &actionName)
TQTime stringToTime(const TQString &str) const
converts string str to time using predefined settings
TQString timeToString(const TQTime &time) const
converts time to string using predefined settings
bool isEmpty(const TQString &str) const
TQVariant stringToVariant(const TQString &str)
TQString inputMask() const
Editor class for Time type. It is a replacement TQTimeEdit due to usability problems: people are accu...
KexiTimeTableEdit(KexiTableViewColumn &column, TQWidget *parent=0)
virtual void handleAction(const TQString &actionName)
KexiTimeFormatter m_formatter
Used to format and convert time values.
virtual void setupContents(TQPainter *p, bool focused, const TQVariant &val, TQString &txt, int &align, int &x, int &y_offset, int &w, int &h)
virtual bool valueIsValid()
virtual bool valueIsEmpty()
virtual TQVariant value()
virtual void setValueInternal(const TQVariant &add, bool removeOld)
initializes this editor with add value
virtual void handleCopyAction(const TQVariant &value, const TQVariant &visibleValue)
virtual bool valueIsNull()
void setValueInInternalEditor(const TQVariant &value)
helper