1
/* This file is part of the KDE project
2
Copyright (C) 2002 Peter Simonsson <psn@linux.se>
3
Copyright (C) 2003-2016 Jarosław Staniek <staniek@kde.org>
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.
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.
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.
21
#ifndef _KEXICOMBOBOXBASE_H_
22
#define _KEXICOMBOBOXBASE_H_
24
#include "kexidatatable_export.h"
25
#include "kexiinputtableedit.h"
28
#include <KDbLookupFieldSchema>
29
#include <KDbTristate>
31
class KDbTableViewColumn;
32
class KexiComboBoxPopup;
34
/*! @short A base class for handling data-aware combo boxes.
35
This class is used by KexiComboBoxTableEdit and KexiDBComboBox.
37
class KEXIDATATABLE_EXPORT KexiComboBoxBase
41
virtual ~KexiComboBoxBase();
43
//! \return column related to this combo; for KexiComboBoxTableEdit 0 is returned here
44
virtual const KDbTableViewColumn *column() const = 0;
46
//! \return database field related to this combo
47
virtual KDbField *field() const = 0;
49
//! \return the original value
50
virtual QVariant origValue() const = 0;
52
//! Note: Generally in current implementation this is integer > 0; may be null if no value is set
53
virtual QVariant value();
55
virtual QVariant visibleValue();
57
//! Reimplement this and call this impl.: used to clear internal editor
60
virtual tristate valueChangedInternal();
61
virtual bool valueIsNull();
62
virtual bool valueIsEmpty();
67
void createPopup(bool show);
71
//! Call this from slot
72
virtual void slotRecordAccepted(KDbRecordData *data, int record);
74
//! Call this from slot
75
virtual void slotRecordSelected(KDbRecordData* data);
77
//! Call this from slot
78
void slotInternalEditorValueChanged(const QVariant &v);
80
//! Implement this to return the internal editor
81
virtual QWidget *internalEditor() const = 0;
84
virtual void setValueInternal(const QVariant& add, bool removeOld);
86
//! Used to select record item for a user-entered value \a v.
87
//! Only for "lookup table" mode.
88
KDbRecordData* selectRecordForEnteredValueInLookupTable(const QVariant& v);
90
/*! \return value from \a returnFromColumn related to \a str value from column \a lookInColumn.
91
If \a allowNulls is true, NULL is returned if no matched column found, else:
93
Example: lookInColumn=0, returnFromColumn=1 --returns user-visible string
94
for column #1 for id-column #0 */
95
QString valueForString(const QString& str, int* record, int lookInColumn,
96
int returnFromColumn, bool allowNulls = false);
98
//! sets \a value for the line edit without setting a flag (m_userEnteredValue) that indicates that
99
//! the text has been entered by hand (by a user)
100
void setValueOrTextInInternalEditor(const QVariant& value);
102
//! \return lookup field schema for this combo box, if present and if is valid (i.e. has defined record source)
103
KDbLookupFieldSchema* lookupFieldSchema() const;
105
int recordToHighlightForLookupTable() const;
107
//! Implement this to perform "move cursor to end" in the internal editor
108
virtual void moveCursorToEndInInternalEditor() = 0;
110
//! Implement this to perform "select all" in the internal editor
111
virtual void selectAllInInternalEditor() = 0;
113
//! Implement this to perform "set value" in the internal editor
114
virtual void setValueInInternalEditor(const QVariant& value) = 0;
116
//! Implement this to return value from the internal editor
117
virtual QVariant valueFromInternalEditor() = 0;
119
//! Implement this as signal
120
virtual void editRequested() = 0;
122
//! Implement this as signal
123
virtual void acceptRequested() = 0;
125
//! Implement this to return a position \a pos mapped from parent (e.g. viewport)
126
//! to global coordinates. QPoint(-1, -1) should be returned if this cannot be computed.
127
virtual QPoint mapFromParentToGlobal(const QPoint& pos) const = 0;
129
//! Implement this to return a hint for popup width.
130
virtual int popupWidthHint() const = 0;
132
//! Implement this to update button state. Table view just updates on/off state
133
//! for the button depending on visibility of the popup
134
virtual void updateButton() {}
136
virtual KexiComboBoxPopup *popup() const = 0;
137
virtual void setPopup(KexiComboBoxPopup *popup) = 0;
139
virtual QVariant visibleValueForLookupField();
141
void updateTextForHighlightedRecord();
143
bool handleKeyPressForPopup(QKeyEvent *ke);
145
void acceptPopupSelection();
147
//! Used by KexiDBComboBox.
150
//! \return index of bound column.
151
int boundColumnIndex() const;
153
//! \return index of (actually, first as this is the current limitation) visible column.
154
int visibleColumnIndex() const;
156
//! A hack for createPopup(), used by forms only. Avoid magical disappearing of the popup in forms after 2nd and subsequent use.
157
//! fix creating popup for forms instead!
158
bool m_reinstantiatePopupOnShow;
160
QVariant m_visibleValue;
162
QVariant m_userEnteredValue; //!< value (usually a text) entered by hand (by the user)
164
bool m_internalEditorValueChanged; //!< true if user has text or other value inside editor
165
bool m_slotInternalEditorValueChanged_enabled; //!< Used in slotInternalEditorValueChanged()
166
bool m_setValueOrTextInInternalEditor_enabled; //!< Used in setValueOrTextInInternalEditor() and slotItemSelected()
167
bool m_mouseBtnPressedWhenPopupVisible; //!< Used only by KexiComboBoxTableEdit
168
bool m_insideCreatePopup; //!< true if we're inside createPopup(); used in slotItemSelected()
169
//! Set to false as soon as the item corresponding with the current
170
//! value is selected in the popup table. This avoids selecting item
171
//! for origValue() and thus loosing the recent choice.
172
bool m_updatePopupSelectionOnShow;
173
bool m_moveCursorToEndInInternalEditor_enabled;
174
bool m_selectAllInInternalEditor_enabled;
175
bool m_setValueInInternalEditor_enabled;
176
//! Used in setValueInternal() to control whether
177
//! we want to set visible value on setValueInternal()
178
//! - true for table view's combo box
179
bool m_setVisibleValueOnSetValueInternal;
180
//! Checked in createPopup(), true for form's combo box, so the popup is focused before showing;
181
//! false for table view's combo box, so the popup is focused after showing.
182
//! False by default.
183
bool m_focusPopupBeforeShow;