summaryrefslogtreecommitdiffstats
path: root/kexi/plugins/forms/widgets/kexidbautofield.h
blob: 809c1186ab674691edf0d996e36e880e763337e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* This file is part of the KDE project
   Copyright (C) 2005 Cedric Pasteur <[email protected]>
   Copyright (C) 2005 Christian Nitschkowski <[email protected]>
   Copyright (C) 2005-2006 Jaroslaw Staniek <[email protected]>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#ifndef KEXIDBAUTOFIELD_H
#define KEXIDBAUTOFIELD_H

#include <tqwidget.h>
#include <kexidb/field.h>
#include <formeditor/container.h>
#include <formeditor/widgetwithsubpropertiesinterface.h>
#include "kexiformdataiteminterface.h"

class TQBoxLayout;
class TQLabel;

//! Universal "Auto Field" widget for Kexi forms
/*! It acts as a container for most data-aware widgets. */
class KEXIFORMUTILS_EXPORT KexiDBAutoField : 
	public TQWidget,
	public KexiFormDataItemInterface,
	public KFormDesigner::DesignTimeDynamicChildWidgetHandler,
	public KFormDesigner::WidgetWithSubpropertiesInterface
{
	Q_OBJECT
  TQ_OBJECT
//'caption' is uncovered now	TQ_PROPERTY(TQString labelCaption READ caption WRITE setCaption DESIGNABLE true)
	TQ_OVERRIDE(TQString caption READ caption WRITE setCaption DESIGNABLE true)
	TQ_OVERRIDE(TQColor paletteForegroundColor READ paletteForegroundColor WRITE setPaletteForegroundColor DESIGNABLE true RESET unsetPalette)
	TQ_OVERRIDE(TQColor paletteBackgroundColor READ paletteBackgroundColor WRITE setPaletteBackgroundColor DESIGNABLE true RESET unsetPalette)
	TQ_PROPERTY(TQColor foregroundLabelColor READ foregroundLabelColor WRITE setForegroundLabelColor DESIGNABLE true RESET unsetPalette)
	TQ_PROPERTY(TQColor backgroundLabelColor READ backgroundLabelColor WRITE setBackgroundLabelColor DESIGNABLE true RESET unsetPalette)
	TQ_PROPERTY(bool autoCaption READ hasAutoCaption WRITE setAutoCaption DESIGNABLE true)
	TQ_PROPERTY(TQString dataSource READ dataSource WRITE setDataSource DESIGNABLE true)
	TQ_PROPERTY(TQCString dataSourceMimeType READ dataSourceMimeType WRITE setDataSourceMimeType DESIGNABLE true)
	TQ_PROPERTY( bool readOnly READ isReadOnly WRITE setReadOnly )
	TQ_PROPERTY(LabelPosition labelPosition READ labelPosition WRITE setLabelPosition DESIGNABLE true)
	TQ_PROPERTY(WidgetType widgetType READ widgetType WRITE setWidgetType DESIGNABLE true)
	/*internal, for design time only*/
	TQ_PROPERTY(int fieldTypeInternal READ fieldTypeInternal WRITE setFieldTypeInternal DESIGNABLE true STORED false)
	TQ_PROPERTY(TQString fieldCaptionInternal READ fieldCaptionInternal WRITE setFieldCaptionInternal DESIGNABLE true STORED false)
	Q_ENUMS( WidgetType LabelPosition )

	public:
		enum WidgetType { Auto = 100, Text, Integer, Double, Boolean, Date, Time, DateTime,
			MultiLineText, ComboBox, Image };
		enum LabelPosition { Left = 300, Top, NoLabel };

		KexiDBAutoField(const TQString &text, WidgetType type, LabelPosition pos, 
			TQWidget *parent = 0, const char *name = 0, bool designMode = true);
		KexiDBAutoField(TQWidget *parent = 0, const char *name = 0, bool designMode = true,
			LabelPosition pos = Left);

		virtual ~KexiDBAutoField();

		inline TQString dataSource() const { return KexiFormDataItemInterface::dataSource(); }
		inline TQCString dataSourceMimeType() const { return KexiFormDataItemInterface::dataSourceMimeType(); }
		virtual void setDataSource( const TQString &ds );
		virtual void setDataSourceMimeType(const TQCString &ds) { KexiFormDataItemInterface::setDataSourceMimeType(ds); }
		virtual void setColumnInfo(KexiDB::QueryColumnInfo* cinfo);

		virtual void setInvalidState(const TQString& text);
		virtual bool isReadOnly() const;
		virtual void setReadOnly( bool readOnly );

		virtual TQVariant value();
		virtual bool valueIsNull();
		virtual bool valueIsEmpty();
		virtual bool valueIsValid();
		virtual bool valueChanged();
		virtual void clear();

		//! Reimpelmented to also install \a listenter for internal editor
		virtual void installListener(KexiDataItemChangesListener* listener);

		WidgetType widgetType() const;
		void setWidgetType(WidgetType type);

		LabelPosition labelPosition() const;
		virtual void setLabelPosition(LabelPosition position);

		TQString caption() const;
		void setCaption(const TQString &caption);

		bool hasAutoCaption() const;
		void setAutoCaption(bool autoCaption);

		/*! If \a displayDefaultValue is true, the value set by KexiDataItemInterface::setValue() 
		 is displayed in a special way. Used by KexiFormDataProvider::fillDataItems(). 
		 \a widget is equal to 'this'.
		 Reimplemented after KexiFormDataItemInterface. */
		virtual void setDisplayDefaultValue(TQWidget* widget, bool displayDefaultValue);

		TQWidget* editor() const;
		TQLabel* label() const;

		virtual bool cursorAtStart();
		virtual bool cursorAtEnd();

		static WidgetType widgetTypeForFieldType(KexiDB::Field::Type type);

		/*! On design time it is not possible to pass a reference to KexiDB::Field object
		 so we're just providing field type. Only used when widget type is Auto.
		 @internal */
		void setFieldTypeInternal(int kexiDBFieldType);

		/*! On design time it is not possible to pass a reference to KexiDB::Field object
		 so we're just providing field caption. Only used when widget type is Auto.
		 @internal */
		void setFieldCaptionInternal(const TQString& text);

		/*! @internal */
		int fieldTypeInternal() const;

		/*! @internal */
		TQString fieldCaptionInternal() const;

		virtual TQSize sizeHint() const;
		virtual void setFocusPolicy ( TQ_FocusPolicy policy );

		//! Reimplemented to return internal editor's color.
		const TQColor & paletteForegroundColor() const;

		//! Reimplemented to set internal editor's color.
		void setPaletteForegroundColor( const TQColor & color );

		//! Reimplemented to return internal editor's color.
		const TQColor & paletteBackgroundColor() const;

		//! Reimplemented to set internal editor's color.
		virtual void setPaletteBackgroundColor( const TQColor & color );

		//! \return label's foreground color
		const TQColor & foregroundLabelColor() const;

		//! Sets label's foreground color
		virtual void setForegroundLabelColor( const TQColor & color );

		//! \return label's background color
		const TQColor & backgroundLabelColor() const;

		//! Sets label's background color
		virtual void setBackgroundLabelColor( const TQColor & color );

		//! Reimplemented to accept subproperties. @see KFormDesigner::WidgetWithSubpropertiesInterface
		virtual TQVariant property( const char * name ) const;

		//! Reimplemented to accept subproperties. @see KFormDesigner::WidgetWithSubpropertiesInterface
		virtual bool setProperty( const char * name, const TQVariant & value );

		/*! Called by the top-level form on key press event to consume widget-specific shortcuts. */
		virtual bool keyPressed(TQKeyEvent *ke);

	public slots:
		virtual void unsetPalette();

	protected slots:
//		void slotValueChanged();
		virtual void paletteChange( const TQPalette& oldPal );

		//! Implemented for KexiDataItemInterface
		virtual void moveCursorToEnd();

		//! Implemented for KexiDataItemInterface
		virtual void moveCursorToStart();

		//! Implemented for KexiDataItemInterface
		virtual void selectAll();

	protected:
		virtual void setValueInternal(const TQVariant&add, bool removeOld);
		void init(const TQString &text, WidgetType type, LabelPosition pos);
		virtual void createEditor();
		void changeText(const TQString &text, bool beautify = true);
//		virtual void paintEvent( TQPaintEvent* pe );
		void updateInformationAboutUnboundField();

		//! internal editor can be created too late, so certain properties should be copied
		void copyPropertiesToEditor();

		virtual bool eventFilter( TQObject *o, TQEvent *e );

		//! Used by @ref setLabelPositionInternal(LabelPosition)
		void setLabelPositionInternal(LabelPosition position, bool noLabel);

		//! Used by KexiDBAutoField::setColumnInfo() and KexiDBComboBox::setColumnInfo()
		void setColumnInfoInternal(KexiDB::QueryColumnInfo* cinfo, KexiDB::QueryColumnInfo* visibleColumnInfo);

	private:
		class Private;
		Private *d;
};

#endif