kexi

kexidblineedit.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2005 Cedric Pasteur <cedric.pasteur@free.fr>
3 Copyright (C) 2004-2007 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 "kexidblineedit.h"
22#include "kexidbautofield.h"
23
24#include <kdebug.h>
25#include <knumvalidator.h>
26#include <kdatetbl.h>
27
28#include <tqpopupmenu.h>
29#include <tqpainter.h>
30
31#include <kexiutils/utils.h>
32#include <kexidb/queryschema.h>
33#include <kexidb/fieldvalidator.h>
34#include <kexiutils/utils.h>
35
37//#define USE_KLineEdit_setReadOnly
38
40class KexiDBLineEdit_ReadOnlyValidator : public TQValidator
41{
42 public:
43 KexiDBLineEdit_ReadOnlyValidator( TQObject * parent )
44 : TQValidator(parent)
45 {
46 }
47 ~KexiDBLineEdit_ReadOnlyValidator() {}
48 virtual State validate( TQString &, int & ) const { return Invalid; }
49};
50
51//-----
52
53KexiDBLineEdit::KexiDBLineEdit(TQWidget *parent, const char *name)
54 : KLineEdit(parent, name)
57//moved , m_dateFormatter(0)
58//moved , m_timeFormatter(0)
59 , m_menuExtender(this, this)
60 , m_internalReadOnly(false)
61 , m_slotTextChanged_enabled(true)
62{
63#ifdef USE_KLineEdit_setReadOnly
65 TQPalette p(widget->palette());
66 p.setColor( lighterGrayBackgroundColor(palette()) );
67 widget->setPalette(p);
68#endif
69
70 connect(this, TQ_SIGNAL(textChanged(const TQString&)), this, TQ_SLOT(slotTextChanged(const TQString&)));
71}
72
73KexiDBLineEdit::~KexiDBLineEdit()
74{
75//moved delete m_dateFormatter;
76//moved delete m_timeFormatter;
77}
78
79void KexiDBLineEdit::setInvalidState( const TQString& displayText )
80{
81 KLineEdit::setReadOnly(true);
83 if (focusPolicy() & TQWidget::TabFocus)
84 setFocusPolicy(TQWidget::ClickFocus);
85 setText(displayText);
86}
87
88void KexiDBLineEdit::setValueInternal(const TQVariant& add, bool removeOld)
89{
90#if 0 //moved to KexiTextFormatter
91 TQVariant value;
92 if (removeOld)
93 value = add;
94 else {
95 if (add.toString().isEmpty())
96 value = m_origValue;
97 else
98 value = m_origValue.toString() + add.toString();
99 }
100
101 if (m_columnInfo) {
102 const KexiDB::Field::Type t = m_columnInfo->field->type();
103 if (t == KexiDB::Field::Boolean) {
105 setText( value.toBool() ? "1" : "0" );
106 return;
107 }
108 else if (t == KexiDB::Field::Date) {
109 setText( dateFormatter()->dateToString( value.toString().isEmpty() ? TQDate() : value.toDate() ) );
110 setCursorPosition(0); //ok?
111 return;
112 }
113 else if (t == KexiDB::Field::Time) {
114 setText(
115 timeFormatter()->timeToString(
116 //hack to avoid converting null variant to valid TQTime(0,0,0)
117 value.toString().isEmpty() ? value.toTime() : TQTime(99,0,0)
118 )
119 );
120 setCursorPosition(0); //ok?
121 return;
122 }
123 else if (t == KexiDB::Field::DateTime) {
124 if (value.toString().isEmpty() ) {
125 setText( TQString() );
126 }
127 else {
128 setText(
129 dateFormatter()->dateToString( value.toDateTime().date() ) + " " +
130 timeFormatter()->timeToString( value.toDateTime().time() )
131 );
132 }
133 setCursorPosition(0); //ok?
134 return;
135 }
136 }
137#endif
139 setText( m_textFormatter.valueToText(removeOld ? TQVariant() : m_origValue, add.toString()) );
140// setText( value.toString() );
141 setCursorPosition(0); //ok?
143}
144
146{
147 return m_textFormatter.textToValue( text() );
148#if 0 // moved to KexiTextFormatter
149 if (! m_columnInfo)
150 return TQVariant();
151 const KexiDB::Field::Type t = m_columnInfo->field->type();
152 switch (t) {
155 return text();
158 return text().toShort();
161 return text().toInt();
163 return text().toLongLong();
166 return text() == "1" ? TQVariant(true) : TQVariant(false);
167 case KexiDB::Field::Date:
168 return dateFormatter()->stringToVariant( text() );
169 case KexiDB::Field::Time:
170 return timeFormatter()->stringToVariant( text() );
171 case KexiDB::Field::DateTime:
172 return stringToDateTime(*dateFormatter(), *timeFormatter(), text());
174 return text().toFloat();
176 return text().toDouble();
177 default:
178 return TQVariant();
179 }
181 return text();
182#endif
183}
184
185void KexiDBLineEdit::slotTextChanged(const TQString&)
186{
188 return;
190}
191
193{
194 return valueIsEmpty(); //ok??? text().isNull();
195}
196
198{
199 return m_textFormatter.valueIsEmpty( text() );
200#if 0 // moved to KexiTextFormatter
201 if (text().isEmpty())
202 return true;
203
204 if (m_columnInfo) {
205 const KexiDB::Field::Type t = m_columnInfo->field->type();
206 if (t == KexiDB::Field::Date || )
207 return dateFormatter()->isEmpty( text() );
208 else if (t == KexiDB::Field::Time)
209 return timeFormatter()->isEmpty( text() );
210 else if (t == KexiDB::Field::Time)
211 return dateTimeIsEmpty( *dateFormatter(), *timeFormatter(), text() );
212 }
213
215 return text().isEmpty();
216#endif
217}
218
220{
221 return m_textFormatter.valueIsValid( text() );
222#if 0 // moved to KexiTextFormatter
223 if (!m_columnInfo)
224 return true;
226 if (valueIsEmpty()/*ok?*/)
227 return true;
228
229 const KexiDB::Field::Type t = m_columnInfo->field->type();
230 if (t == KexiDB::Field::Date)
231 return dateFormatter()->stringToVariant( text() ).isValid();
232 else if (t == KexiDB::Field::Time)
233 return timeFormatter()->stringToVariant( text() ).isValid();
234 else if (t == KexiDB::Field::DateTime)
235 return dateTimeIsValid( *dateFormatter(), *timeFormatter(), text() );
236
238 return true;
239#endif
240}
241
243{
244 return m_internalReadOnly;
245}
246
247void KexiDBLineEdit::setReadOnly( bool readOnly )
248{
249#ifdef USE_KLineEdit_setReadOnly
251 return KLineEdit::setReadOnly( readOnly );
252#else
253 m_internalReadOnly = readOnly;
254 if (m_internalReadOnly) {
255 m_readWriteValidator = validator();
257 m_readOnlyValidator = new KexiDBLineEdit_ReadOnlyValidator(this);
258 setValidator( m_readOnlyValidator );
259 }
260 else {
261 //revert to r/w validator
262 setValidator( m_readWriteValidator );
263 }
264 m_menuExtender.updatePopupMenuActions();
265#endif
266}
267
268TQPopupMenu * KexiDBLineEdit::createPopupMenu()
269{
270 TQPopupMenu *contextMenu = KLineEdit::createPopupMenu();
271 m_menuExtender.createTitle(contextMenu);
272 return contextMenu;
273}
274
275
277{
278 return this;
279}
280
282{
283 return cursorPosition()==0;
284}
285
287{
288 return cursorPosition()==(int)text().length();
289}
290
292{
294 KLineEdit::clear();
295}
296
297
299{
301 m_textFormatter.setField( cinfo ? cinfo->field : 0 );
302
303 if (!cinfo)
304 return;
305
307 setValidator( new KexiDB::FieldValidator(*cinfo->field, this) );
308
309#if 0 // moved to KexiTextFormatter
310 if (t==KexiDB::Field::Date) {
312 setInputMask( dateFormatter()->inputMask() );
313 }
314 else if (t==KexiDB::Field::Time) {
316// setInputMask("00:00:00");
317 setInputMask( timeFormatter()->inputMask() );
318 }
319 else if (t==KexiDB::Field::DateTime) {
320 setInputMask(
321 dateTimeInputMask( *dateFormatter(), *timeFormatter() ) );
322 }
323#endif
324 const TQString inputMask( m_textFormatter.inputMask() );
325 if (!inputMask.isEmpty())
326 setInputMask( inputMask );
327
329}
330
331/*todo
332void KexiDBLineEdit::paint( TQPainter *p )
333{
334 KexiDBTextWidgetInterface::paint( this, &p, text().isEmpty(), alignment(), hasFocus() );
335}*/
336
337void KexiDBLineEdit::paintEvent ( TQPaintEvent *pe )
338{
339 KLineEdit::paintEvent( pe );
340 TQPainter p(this);
341 KexiDBTextWidgetInterface::paint( this, &p, text().isEmpty(), alignment(), hasFocus() );
342}
343
344bool KexiDBLineEdit::event( TQEvent * e )
345{
346 const bool ret = KLineEdit::event( e );
347 KexiDBTextWidgetInterface::event(e, this, text().isEmpty());
348 if (e->type()==TQEvent::FocusOut) {
349 TQFocusEvent *fe = static_cast<TQFocusEvent*>(e);
350// if (fe->reason()!=TQFocusEvent::ActiveWindow && fe->reason()!=TQFocusEvent::Popup) {
351 if (fe->reason()==TQFocusEvent::Tab || fe->reason()==TQFocusEvent::Backtab) {
352 //display aligned to left after loosing the focus (only if this is tab/backtab event)
354 setCursorPosition(0); //ok?
355 }
356 }
357 return ret;
358}
359
360bool KexiDBLineEdit::appendStretchRequired(KexiDBAutoField* autoField) const
361{
362 return KexiDBAutoField::Top == autoField->labelPosition();
363}
364
365void KexiDBLineEdit::handleAction(const TQString& actionName)
366{
367 if (actionName=="edit_copy") {
368 copy();
369 }
370 else if (actionName=="edit_paste") {
371 paste();
372 }
373 else if (actionName=="edit_cut") {
374 cut();
375 }
377}
378
379void KexiDBLineEdit::setDisplayDefaultValue(TQWidget *widget, bool displayDefaultValue)
380{
382 // initialize display parameters for default / entered value
385 setFont(params->font);
386 TQPalette pal(palette());
387 pal.setColor(TQPalette::Active, TQColorGroup::Text, params->textColor);
388 setPalette(pal);
389}
390
392{
393 cancelEditor();
394}
395
397{
398 KLineEdit::end(false);
399}
400
402{
403 KLineEdit::home(false);
404}
405
407{
408 KLineEdit::selectAll();
409}
410
411bool KexiDBLineEdit::keyPressed(TQKeyEvent *ke)
412{
413 Q_UNUSED(ke);
414 return false;
415}
416
417#include "kexidblineedit.moc"
virtual void setDisplayDefaultValue(TQWidget *widget, bool displayDefaultValue)
virtual bool cursorAtEnd()
TQGuardedPtr< const TQValidator > m_readOnlyValidator
Used for read only flag to disable editing.
virtual void setColumnInfo(KexiDB::QueryColumnInfo *cinfo)
virtual TQVariant value()
virtual bool keyPressed(TQKeyEvent *ke)
virtual bool event(TQEvent *)
virtual void selectAll()
Implemented for KexiDataItemInterface.
virtual void clear()
clears item's data, so the data will contain NULL data
virtual void moveCursorToEnd()
Implemented for KexiDataItemInterface.
bool m_slotTextChanged_enabled
Used in slotTextChanged().
virtual void moveCursorToStart()
Implemented for KexiDataItemInterface.
virtual TQWidget * widget()
virtual void undo()
Reimplemented, so "undo" means the same as "cancelEditor" action.
virtual bool appendStretchRequired(KexiDBAutoField *autoField) const
Implemented for KexiSubwidgetInterface.
virtual bool valueIsEmpty()
virtual bool cursorAtStart()
virtual bool valueIsValid()
TQGuardedPtr< const TQValidator > m_readWriteValidator
Used to remember the previous validator used forf r/w mode, after setting the read only flag.
KexiDBWidgetContextMenuExtender m_menuExtender
Used for extending context menu.
virtual void setValueInternal(const TQVariant &add, bool removeOld)
virtual bool valueIsNull()
virtual bool isReadOnly() const
KexiTextFormatter m_textFormatter
Used to format text.
virtual void handleAction(const TQString &actionName)
virtual void setInvalidState(const TQString &displayText)
An interface providing common text editor's functionality.
void setColumnInfo(KexiDB::QueryColumnInfo *cinfo, TQWidget *w)
Called from KexiFormDataItemInterface::setColumnInfo(KexiDB::QueryColumnInfo* cinfo) implementation.
void event(TQEvent *e, TQWidget *w, bool textIsEmpty)
Called from event( TQEvent * e ) method of the data aware widget.
void paint(TQFrame *w, TQPainter *p, bool textIsEmpty, int alignment, bool hasFocus)
Called from paintEvent( TQPaintEvent *pe ) method of the data aware widget.
A validator for KexiDB data types.
@ ShortInteger
Definition field.h:86
Helper class that assigns additional information for the column in a query.
Definition queryschema.h:49
Stores set of display parameters used in utility functions.
An interface for declaring form widgets to be data-aware.
virtual void setColumnInfo(KexiDB::QueryColumnInfo *cinfo)
virtual void setDisplayDefaultValue(TQWidget *widget, bool displayDefaultValue)
KexiDisplayUtils::DisplayParameters * m_displayParametersForEnteredValue
used in setDisplayDefaultValue()
KexiDisplayUtils::DisplayParameters * m_displayParametersForDefaultValue
used in setDisplayDefaultValue()