kexi

kexidbdateedit.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2005 Cedric Pasteur <cedric.pasteur@free.fr>
3 Copyright (C) 2004-2005 Jaroslaw Staniek <js@iidea.pl>
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this program; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include "kexidbdateedit.h"
22#include <tqlayout.h>
23#include <tqtoolbutton.h>
24#include <tdepopupmenu.h>
25#include <kdatepicker.h>
26#include <kdatetbl.h>
27
28#include <kexiutils/utils.h>
29#include <kexidb/queryschema.h>
30
31KexiDBDateEdit::KexiDBDateEdit(const TQDate &date, TQWidget *parent, const char *name)
32 : TQWidget(parent, name), KexiFormDataItemInterface()
33{
34 m_invalidState = false;
35 m_cleared = false;
36 m_readOnly = false;
37
38 m_edit = new TQDateEdit(date, this);
39 m_edit->setAutoAdvance(true);
40 m_edit->setSizePolicy(TQSizePolicy::Minimum, TQSizePolicy::MinimumExpanding);
41 connect( m_edit, TQ_SIGNAL(valueChanged(const TQDate&)), this, TQ_SLOT(slotValueChanged(const TQDate&)) );
42 connect( m_edit, TQ_SIGNAL(valueChanged(const TQDate&)), this, TQ_SIGNAL(dateChanged(const TQDate&)) );
43
44 TQToolButton* btn = new TQToolButton(this);
45 btn->setText("...");
46 btn->setFixedWidth( TQFontMetrics(btn->font()).width(" ... ") );
47 btn->setPopupDelay(1); //1 ms
48
49#ifdef TQDateTimeEditor_HACK
50 m_dte_date = KexiUtils::findFirstChild<TQDateTimeEditor>(m_edit, "TQDateTimeEditor");
51#else
52 m_dte_date = 0;
53#endif
54
55 m_datePickerPopupMenu = new TDEPopupMenu(0, "date_popup");
56 connect(m_datePickerPopupMenu, TQ_SIGNAL(aboutToShow()), this, TQ_SLOT(slotShowDatePicker()));
57 m_datePicker = new KDatePicker(m_datePickerPopupMenu, TQDate::currentDate(), 0);
58
59 KDateTable *dt = KexiUtils::findFirstChild<KDateTable>(m_datePicker, "KDateTable");
60 if (dt)
61 connect(dt, TQ_SIGNAL(tableClicked()), this, TQ_SLOT(acceptDate()));
62 m_datePicker->setCloseButton(true);
63 m_datePicker->installEventFilter(this);
64 m_datePickerPopupMenu->insertItem(m_datePicker);
65 btn->setPopup(m_datePickerPopupMenu);
66
67 TQHBoxLayout* layout = new TQHBoxLayout(this);
68 layout->addWidget(m_edit, 1);
69 layout->addWidget(btn, 0);
70
71 setFocusProxy(m_edit);
72}
73
74KexiDBDateEdit::~KexiDBDateEdit()
75{
76}
77
78void KexiDBDateEdit::setInvalidState( const TQString& )
79{
80 setEnabled(false);
81 setReadOnly(true);
82 m_invalidState = true;
84 if (focusPolicy() & TabFocus)
85 setFocusPolicy(TQWidget::ClickFocus);
86}
87
88void
89KexiDBDateEdit::setEnabled(bool enabled)
90{
91 // prevent the user from reenabling the widget when it is in invalid state
92 if(enabled && m_invalidState)
93 return;
94 TQWidget::setEnabled(enabled);
95}
96
97void KexiDBDateEdit::setValueInternal(const TQVariant &add, bool removeOld)
98{
99 int setNumberOnFocus = -1;
100 TQDate d;
101 TQString addString(add.toString());
102 if (removeOld) {
103 if (!addString.isEmpty() && addString[0].latin1()>='0' && addString[0].latin1() <='9') {
104 setNumberOnFocus = addString[0].latin1()-'0';
105 d = TQDate(setNumberOnFocus*1000, 1, 1);
106 }
107 }
108 else
109 d = m_origValue.toDate();
110
111 m_edit->setDate(d);
112}
113
114TQVariant
116{
117 return TQVariant(m_edit->date());
118}
119
121{
122 return !m_edit->date().isValid() || m_edit->date().isNull();
123}
124
126{
127 return m_cleared;
128}
129
131{
134 return m_readOnly;
135}
136
137void KexiDBDateEdit::setReadOnly(bool set)
138{
139 m_readOnly = set;
140}
141
142TQWidget*
144{
145 return this;
146}
147
149{
150#ifdef TQDateTimeEditor_HACK
151 return m_dte_date && m_edit->hasFocus() && m_dte_date->focusSection()==0;
152#else
153 return false;
154#endif
155}
156
158{
159#ifdef TQDateTimeEditor_HACK
160 return m_dte_date && m_edit->hasFocus()
161 && m_dte_date->focusSection()==int(m_dte_date->sectionCount()-1);
162#else
163 return false;
164#endif
165}
166
168{
169 m_edit->setDate(TQDate());
170 m_cleared = true;
171}
172
173void
174KexiDBDateEdit::slotValueChanged(const TQDate&)
175{
176 m_cleared = false;
177}
178
179void
180KexiDBDateEdit::slotShowDatePicker()
181{
182 TQDate date = m_edit->date();
183
184 m_datePicker->setDate(date);
185 m_datePicker->setFocus();
186 m_datePicker->show();
187 m_datePicker->setFocus();
188}
189
190void
191KexiDBDateEdit::acceptDate()
192{
193 m_edit->setDate(m_datePicker->date());
194 m_datePickerPopupMenu->hide();
195}
196
197bool
198KexiDBDateEdit::eventFilter(TQObject *o, TQEvent *e)
199{
200 if (o != m_datePicker)
201 return false;
202
203 switch (e->type()) {
204 case TQEvent::Hide:
205 m_datePickerPopupMenu->hide();
206 break;
207 case TQEvent::KeyPress:
208 case TQEvent::KeyRelease: {
209 TQKeyEvent *ke = (TQKeyEvent *)e;
210 if (ke->key()==TQt::Key_Enter || ke->key()==TQt::Key_Return) {
211 //accepting picker
212 acceptDate();
213 return true;
214 }
215 else if (ke->key()==TQt::Key_Escape) {
216 //canceling picker
217 m_datePickerPopupMenu->hide();
218 return true;
219 }
220 else
221 m_datePickerPopupMenu->setFocus();
222 break;
223 }
224 default:
225 break;
226 }
227 return false;
228}
229
230#include "kexidbdateedit.moc"
virtual bool valueIsNull()
virtual TQVariant value()
virtual bool valueIsEmpty()
virtual bool isReadOnly() const
virtual TQWidget * widget()
virtual void setValueInternal(const TQVariant &add, bool removeOld)
virtual void clear()
clears item's data, so the data will contain NULL data
virtual bool cursorAtStart()
virtual bool cursorAtEnd()
virtual void setInvalidState(const TQString &displayText)
An interface for declaring form widgets to be data-aware.
type * findFirstChild(TQObject *o, const char *className, const char *objName=0)