kexidatetableedit.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2002 Lucijan Busch <lucijan@gmx.at>
3 Copyright (C) 2003 Daniel Molkentin <molkentin@kde.org>
4 Copyright (C) 2003-2004,2006 Jaroslaw Staniek <js@iidea.pl>
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this program; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22#include "kexidatetableedit.h"
23
24#include <tqapplication.h>
25#include <tqpainter.h>
26#include <tqvariant.h>
27#include <tqrect.h>
28#include <tqpalette.h>
29#include <tqcolor.h>
30#include <tqfontmetrics.h>
31#include <tqdatetime.h>
32#include <tqcursor.h>
33#include <tqpoint.h>
34#include <tqlayout.h>
35#include <tqtoolbutton.h>
36#include <tqdatetimeedit.h>
37#include <tqclipboard.h>
38
39#include <kdebug.h>
40#include <tdelocale.h>
41#include <tdeglobal.h>
42#include <kdatepicker.h>
43#include <kdatetbl.h>
44#include <klineedit.h>
45#include <tdepopupmenu.h>
46#include <kdatewidget.h>
47
48#include <kexiutils/utils.h>
49
50
52 : KexiInputTableEdit(column, parent)
53{
54 setName("KexiDateTableEdit");
55
57
58 m_lineedit->setInputMask( m_formatter.inputMask() );
59}
60
61KexiDateTableEdit::~KexiDateTableEdit()
62{
63}
64
66{
67 if (value.isValid() && value.toDate().isValid())
68 m_lineedit->setText( m_formatter.dateToString( value.toDate() ) );
69 else
70 m_lineedit->setText( TQString() );
71}
72
73void KexiDateTableEdit::setValueInternal(const TQVariant& add_, bool removeOld)
74{
75 if (removeOld) {
76 //new date entering... just fill the line edit
78 TQString add(add_.toString());
79 m_lineedit->setText(add);
80 m_lineedit->setCursorPosition(add.length());
81 return;
82 }
83 setValueInInternalEditor( m_origValue );
84 m_lineedit->setCursorPosition(0); //ok?
85}
86
87void KexiDateTableEdit::setupContents( TQPainter *p, bool focused, const TQVariant& val,
88 TQString &txt, int &align, int &x, int &y_offset, int &w, int &h )
89{
90 Q_UNUSED(p);
91 Q_UNUSED(focused);
92 Q_UNUSED(x);
93 Q_UNUSED(w);
94 Q_UNUSED(h);
95#ifdef TQ_WS_WIN
96 y_offset = -1;
97#else
98 y_offset = 0;
99#endif
100 if (val.toDate().isValid())
101 txt = m_formatter.dateToString(val.toDate());
102// txt = val.toDate().toString(TQt::LocalDate);
103 align |= AlignLeft;
104}
105
107{
108// if (m_lineedit->text().replace(m_formatter.separator(),"").stripWhiteSpace().isEmpty())
109 if (m_formatter.isEmpty(m_lineedit->text())) //empty date is null
110 return true;
111 return dateValue().isNull();
112}
113
115{
116 return valueIsNull();//js OK? TODO (nonsense?)
117}
118
119TQDate KexiDateTableEdit::dateValue() const
120{
121 return m_formatter.stringToDate( m_lineedit->text() );
122}
123
125{
126 return m_formatter.stringToVariant( m_lineedit->text() );
127}
128
130{
131 if (m_formatter.isEmpty(m_lineedit->text())) //empty date is valid
132 return true;
133 return m_formatter.stringToDate( m_lineedit->text() ).isValid();
134}
135
136void KexiDateTableEdit::handleCopyAction(const TQVariant& value, const TQVariant& visibleValue)
137{
138 Q_UNUSED(visibleValue);
139 if (!value.isNull() && value.toDate().isValid())
140 tqApp->clipboard()->setText( m_formatter.dateToString(value.toDate()) );
141 else
142 tqApp->clipboard()->setText( TQString() );
143}
144
145void KexiDateTableEdit::handleAction(const TQString& actionName)
146{
147 const bool alreadyVisible = m_lineedit->isVisible();
148
149 if (actionName=="edit_paste") {
150 const TQVariant newValue( m_formatter.stringToDate(tqApp->clipboard()->text()) );
151 if (!alreadyVisible) { //paste as the entire text if the cell was not in edit mode
152 emit editRequested();
153 m_lineedit->clear();
154 }
155 setValueInInternalEditor( newValue );
156 }
157 else
159}
160
161/*
162void
163KexiDateTableEdit::slotDateChanged(TQDate date)
164{
165 m_edit->setDate(date);
166 repaint();
167}
168
169void
170KexiDateTableEdit::slotShowDatePicker()
171{
172 TQDate date = m_edit->date();
173
174 m_datePicker->setDate(date);
175 m_datePicker->setFocus();
176 m_datePicker->show();
177 m_datePicker->setFocus();
178}
179
181void KexiDateTableEdit::moveToFirstSection()
182{
183 if (!m_dte_date_obj)
184 return;
185#ifdef TQDateTimeEditor_HACK
186 if (m_dte_date)
187 m_dte_date->setFocusSection(0);
188#else
189#ifdef TQ_WS_WIN //tmp
190 TQKeyEvent ke_left(TQEvent::KeyPress, TQt::Key_Left, 0, 0);
191 for (int i=0; i<8; i++)
192 TQApplication::sendEvent( m_dte_date_obj, &ke_left );
193#endif
194#endif
195}
196
197bool KexiDateTableEdit::eventFilter( TQObject *o, TQEvent *e )
198{
199 if (o==m_datePicker) {
200 kdDebug() << e->type() << endl;
201 switch (e->type()) {
202 case TQEvent::Hide:
203 m_datePickerPopupMenu->hide();
204 break;
205 case TQEvent::KeyPress:
206 case TQEvent::KeyRelease: {
207 kdDebug() << "ok!" << endl;
208 TQKeyEvent *ke = (TQKeyEvent *)e;
209 if (ke->key()==Key_Enter || ke->key()==Key_Return) {
210 //accepting picker
211 acceptDate();
212 return true;
213 }
214 else if (ke->key()==Key_Escape) {
215 //canceling picker
216 m_datePickerPopupMenu->hide();
217 kdDebug() << "reject" << endl;
218 return true;
219 }
220 else m_datePickerPopupMenu->setFocus();
221 break;
222 }
223 default:
224 break;
225 }
226 }
227#ifdef TQ_WS_WIN //tmp
228 else if (e->type()==TQEvent::FocusIn && o->parent() && o->parent()->parent()==m_edit
229 && m_setNumberOnFocus >= 0 && m_dte_date_obj)
230 {
231 // there was a number character passed as 'add' parameter in init():
232 moveToFirstSection();
233 TQKeyEvent ke(TQEvent::KeyPress, int(TQt::Key_0)+m_setNumberOnFocus,
234 '0'+m_setNumberOnFocus, 0, TQString::number(m_setNumberOnFocus));
235 TQApplication::sendEvent( m_dte_date_obj, &ke );
236 m_setNumberOnFocus = -1;
237 }
238#endif
239#ifdef TQDateTimeEditor_HACK
240 else if (e->type()==TQEvent::KeyPress && m_dte_date) {
241 TQKeyEvent *ke = static_cast<TQKeyEvent*>(e);
242 if ((ke->key()==TQt::Key_Right && !m_sentEvent && cursorAtEnd())
243 || (ke->key()==TQt::Key_Left && !m_sentEvent && cursorAtStart()))
244 {
245 //the editor should send this key event:
246 m_sentEvent = true; //avoid recursion
247 TQApplication::sendEvent( this, ke );
248 m_sentEvent = false;
249 ke->ignore();
250 return true;
251 }
252 }
253#endif
254 return false;
255}
256
257void KexiDateTableEdit::acceptDate()
258{
259 m_edit->setDate(m_datePicker->date());
260 m_datePickerPopupMenu->hide();
261 kdDebug() << "accept" << endl;
262}
263
264bool KexiDateTableEdit::cursorAtStart()
265{
266#ifdef TQDateTimeEditor_HACK
267 return m_dte_date && m_edit->hasFocus() && m_dte_date->focusSection()==0;
268#else
269 return false;
270#endif
271}
272
273bool KexiDateTableEdit::cursorAtEnd()
274{
275#ifdef TQDateTimeEditor_HACK
276 return m_dte_date && m_edit->hasFocus()
277 && m_dte_date->focusSection()==int(m_dte_date->sectionCount()-1);
278#else
279 return false;
280#endif
281}
282
283void KexiDateTableEdit::clear()
284{
285 m_edit->setDate(TQDate());
286}*/
287
288KEXI_CELLEDITOR_FACTORY_ITEM_IMPL(KexiDateEditorFactoryItem, KexiDateTableEdit)
289
290#include "kexidatetableedit.moc"
virtual TQVariant visibleValue()
TQString dateToString(const TQDate &date) const
Converts date to string using predefined settings.
TQVariant stringToVariant(const TQString &str) const
bool isEmpty(const TQString &str) const
TQDate stringToDate(const TQString &str) const
Converts string str to date using predefined settings.
TQString inputMask() const
Editor class for Date type. It is a replacement TQDateEdit due to usability problems: people are accu...
virtual void setValueInternal(const TQVariant &add, bool removeOld)
initializes this editor with add value
KexiDateFormatter m_formatter
Used to format and convert date values.
virtual bool valueIsValid()
virtual TQVariant value()
virtual bool valueIsNull()
virtual void handleAction(const TQString &actionName)
void setValueInInternalEditor(const TQVariant &value)
helper
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 valueIsEmpty()
virtual void handleCopyAction(const TQVariant &value, const TQVariant &visibleValue)
KexiDateTableEdit(KexiTableViewColumn &column, TQWidget *parent=0)
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)