1 /* This file is part of the KDE project
2    Copyright (C) 2005 Cedric Pasteur <cedric.pasteur@free.fr>
3    Copyright (C) 2004-2014 Jarosław Staniek <staniek@kde.org>
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 #ifndef KEXIDBLINEEDIT_H
22 #define KEXIDBLINEEDIT_H
23 
24 #include <QValidator>
25 #include <QEvent>
26 #include <QPaintEvent>
27 #include <QLineEdit>
28 #include <QPointer>
29 
30 #include <widget/dataviewcommon/kexiformdataiteminterface.h>
31 #include "kexidbtextwidgetinterface.h"
32 #include "kexidbutils.h"
33 #include <kexi_global.h>
34 #include <widget/tableview/kexitextformatter.h>
35 #include <formeditor/FormWidgetInterface.h>
36 
37 class KexiDBWidgetContextMenuExtender;
38 class KexiDBLineEditStyle;
39 
40 //! @short Line edit widget for Kexi forms
41 /*! Handles many data types. User input is validated by using validators
42  and/or input masks.
43 */
44 class KEXIFORMUTILS_EXPORT KexiDBLineEdit : public QLineEdit,
45                                             protected KexiDBTextWidgetInterface,
46                                             public KexiFormDataItemInterface,
47                                             public KexiSubwidgetInterface,
48                                             public KFormDesigner::FormWidgetInterface
49 {
50     Q_OBJECT
51     Q_PROPERTY(QString dataSource READ dataSource WRITE setDataSource)
52     Q_PROPERTY(QString dataSourcePartClass READ dataSourcePluginId WRITE setDataSourcePluginId)
53     Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly)
54     Q_PROPERTY(QString clickMessage READ placeholderText
55                                     WRITE setPlaceholderText) // Internal, equivalent of placeholderText
56                                                               // For backward compatibility Kexi projects
57                                                               // created with Qt < 4.7.
58     Q_PROPERTY(bool showClearButton READ isClearButtonEnabled
59                                     WRITE setClearButtonEnabled) // Internal, equivalent of clearButtonEnabled
60                                                                  // For backward compatibility Kexi projects
61                                                                  // created with Qt 4.
62 public:
63     explicit KexiDBLineEdit(QWidget *parent);
64     virtual ~KexiDBLineEdit();
65 
dataSource()66     inline QString dataSource() const {
67         return KexiFormDataItemInterface::dataSource();
68     }
dataSourcePluginId()69     inline QString dataSourcePluginId() const {
70         return KexiFormDataItemInterface::dataSourcePluginId();
71     }
72     virtual QVariant value();
73     virtual void setInvalidState(const QString& displayText);
74 
75     //! \return true if editor's value is null (not empty)
76     //! Used for checking if a given constraint within table of form is met.
77     virtual bool valueIsNull();
78 
79     //! \return true if editor's value is empty (not necessary null).
80     //! Only few data types can accept "EMPTY" property
81     //! (use KDbField::hasEmptyProperty() to check this).
82     //! Used for checking if a given constraint within table or form is met.
83     virtual bool valueIsEmpty();
84 
85     /*! \return true if the value is valid */
86     virtual bool valueIsValid();
87 
88     /*! \return 'readOnly' flag for this widget. */
89     virtual bool isReadOnly() const;
90 
91     /*! If \a displayDefaultValue is true, the value set by KexiDataItemInterface::setValue()
92      is displayed in a special way. Used by KexiFormDataProvider::fillDataItems().
93      \a widget is equal to 'this'.
94      Reimplemented after KexiFormDataItemInterface. */
95     virtual void setDisplayDefaultValue(QWidget* widget, bool displayDefaultValue);
96 
97     /*! \return the view widget of this item, e.g. line edit widget. */
98     virtual QWidget* widget();
99 
100     virtual bool cursorAtStart();
101     virtual bool cursorAtEnd();
102     virtual void clear();
103 
104     void setColumnInfo(KDbConnection *conn, KDbQueryColumnInfo* cinfo) override;
105 
106     /*! Handles action having standard name \a actionName.
107      Action could be: "edit_copy", "edit_paste", etc.
108      Reimplemented after KexiDataItemInterface. */
109     virtual void handleAction(const QString& actionName);
110 
111     /*! Called by top-level form on key press event to consume widget-specific shortcuts. */
112     virtual bool keyPressed(QKeyEvent *ke);
113 
114     //! Used when read only flag is true
originalText()115     QString originalText() const { return m_originalText; }
116 
117     //! Used when read only flag is true
118     int originalCursorPosition() const;
119 
120 public Q_SLOTS:
121     void setDataSource(const QString &ds);
122 
123     void setDataSourcePluginId(const QString &pluginId);
124 
125     virtual void setReadOnly(bool readOnly);
126 
127     //! Reimplemented, so "undo" means the same as "cancelEditor" action
128     virtual void undo();
129 
130     //! Implemented for KexiDataItemInterface
131     virtual void moveCursorToEnd();
132 
133     //! Implemented for KexiDataItemInterface
134     virtual void moveCursorToStart();
135 
136     //! Implemented for KexiDataItemInterface
137     virtual void selectAll();
138 
139     //! Implemented for KexiDataItemInterface
140     virtual bool fixup();
141 
142 protected Q_SLOTS:
143     void slotTextChanged(const QString&);
144 
145     void slotTextEdited(const QString& text);
146 
147     void slotCursorPositionChanged(int oldPos, int newPos);
148 
149     //! Used to protect m_readWriteValidator against after validator is destroyed
150     void slotReadWriteValidatorDestroyed(QObject*);
151 
152 protected:
153     virtual void paintEvent(QPaintEvent *);
154     virtual void setValueInternal(const QVariant& add, bool removeOld);
155     virtual bool event(QEvent *);
156     virtual void contextMenuEvent(QContextMenuEvent *e);
157     virtual void changeEvent(QEvent *e);
158 
159     //! Implemented for KexiSubwidgetInterface
160     virtual bool appendStretchRequired(KexiDBAutoField* autoField) const;
161 
162     void updateTextForDataSource();
163 
164     void updatePalette();
165 
166     //! Used to format text
167     KexiTextFormatter m_textFormatter;
168 
169     //! Used for read only flag to disable editing
170     QPointer<QValidator> m_readOnlyValidator;
171 
172     //! Used to remember the previous validator used for r/w mode, after setting
173     //! the read only flag
174     const QValidator* m_readWriteValidator;
175 
176     //! Used for extending context menu
177     KexiDBWidgetContextMenuExtender m_menuExtender;
178 
179     //! Used in isReadOnly, as sometimes we want to have the flag set tot true when QLineEdit::isReadOnly
180     //! is still false.
181     bool m_internalReadOnly;
182 
183     //! Used in slotTextChanged()
184     bool m_slotTextChanged_enabled;
185 
186     QString m_originalText;
187     int m_cursorPosition;
188     QPalette m_originalPalette; //!< Used for read-only case
189     bool m_paletteChangeEvent_enabled;
190     bool m_inStyleChangeEvent;
191     QPointer<KexiDBLineEditStyle> m_internalStyle;
192 };
193 
194 #endif
195