1/****************************************************************************
2**
3** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4** Contact: http://www.qt-project.org/legal
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Digia. For licensing terms and
14** conditions see http://qt.digia.com/licensing. For further information
15** use the contact form at http://qt.digia.com/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 2.1 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 2.1 requirements
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24**
25** In addition, as a special exception, Digia gives you certain additional
26** rights. These rights are described in the Digia Qt LGPL Exception
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28**
29** GNU General Public License Usage
30** Alternatively, this file may be used under the terms of the GNU
31** General Public License version 3.0 as published by the Free Software
32** Foundation and appearing in the file LICENSE.GPL included in the
33** packaging of this file. Please review the following information to
34** ensure the GNU General Public License version 3.0 requirements will be
35** met: http://www.gnu.org/copyleft/gpl.html.
36**
37**
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#ifndef QCOMBOBOX_H
43#define QCOMBOBOX_H
44
45#include <QtGui/qwidget.h>
46#include <QtGui/qabstractitemdelegate.h>
47#include <QtCore/qabstractitemmodel.h>
48#include <QtCore/qvariant.h>
49
50QT_BEGIN_HEADER
51
52QT_BEGIN_NAMESPACE
53
54QT_MODULE(Gui)
55#ifndef QT_NO_COMBOBOX
56
57class QAbstractItemView;
58class QLineEdit;
59class QComboBoxPrivate;
60class QCompleter;
61
62class Q_GUI_EXPORT QComboBox : public QWidget
63{
64 Q_OBJECT
65
66 Q_ENUMS(InsertPolicy)
67 Q_ENUMS(SizeAdjustPolicy)
68 Q_PROPERTY(bool editable READ isEditable WRITE setEditable)
69 Q_PROPERTY(int count READ count)
70 Q_PROPERTY(QString currentText READ currentText USER true)
71 Q_PROPERTY(int currentIndex READ currentIndex WRITE setCurrentIndex NOTIFY currentIndexChanged)
72 Q_PROPERTY(int maxVisibleItems READ maxVisibleItems WRITE setMaxVisibleItems)
73 Q_PROPERTY(int maxCount READ maxCount WRITE setMaxCount)
74 Q_PROPERTY(InsertPolicy insertPolicy READ insertPolicy WRITE setInsertPolicy)
75 Q_PROPERTY(SizeAdjustPolicy sizeAdjustPolicy READ sizeAdjustPolicy WRITE setSizeAdjustPolicy)
76 Q_PROPERTY(int minimumContentsLength READ minimumContentsLength WRITE setMinimumContentsLength)
77 Q_PROPERTY(QSize iconSize READ iconSize WRITE setIconSize)
78
79#ifndef QT_NO_COMPLETER
80 Q_PROPERTY(bool autoCompletion READ autoCompletion WRITE setAutoCompletion DESIGNABLE false)
81 Q_PROPERTY(Qt::CaseSensitivity autoCompletionCaseSensitivity READ autoCompletionCaseSensitivity WRITE setAutoCompletionCaseSensitivity DESIGNABLE false)
82#endif // QT_NO_COMPLETER
83
84 Q_PROPERTY(bool duplicatesEnabled READ duplicatesEnabled WRITE setDuplicatesEnabled)
85 Q_PROPERTY(bool frame READ hasFrame WRITE setFrame)
86 Q_PROPERTY(int modelColumn READ modelColumn WRITE setModelColumn)
87
88public:
89 explicit QComboBox(QWidget *parent = 0);
90 ~QComboBox();
91
92 int maxVisibleItems() const;
93 void setMaxVisibleItems(int maxItems);
94
95 int count() const;
96 void setMaxCount(int max);
97 int maxCount() const;
98
99#ifndef QT_NO_COMPLETER
100 bool autoCompletion() const;
101 void setAutoCompletion(bool enable);
102
103 Qt::CaseSensitivity autoCompletionCaseSensitivity() const;
104 void setAutoCompletionCaseSensitivity(Qt::CaseSensitivity sensitivity);
105#endif
106
107 bool duplicatesEnabled() const;
108 void setDuplicatesEnabled(bool enable);
109
110 void setFrame(bool);
111 bool hasFrame() const;
112
113 inline int findText(const QString &text,
114 Qt::MatchFlags flags = static_cast<Qt::MatchFlags>(Qt::MatchExactly|Qt::MatchCaseSensitive)) const
115 { return findData(text, Qt::DisplayRole, flags); }
116 int findData(const QVariant &data, int role = Qt::UserRole,
117 Qt::MatchFlags flags = static_cast<Qt::MatchFlags>(Qt::MatchExactly|Qt::MatchCaseSensitive)) const;
118
119 enum InsertPolicy {
120 NoInsert,
121 InsertAtTop,
122 InsertAtCurrent,
123 InsertAtBottom,
124 InsertAfterCurrent,
125 InsertBeforeCurrent,
126 InsertAlphabetically
127#if defined(QT3_SUPPORT) && !defined(Q_MOC_RUN)
128 ,
129 NoInsertion = NoInsert,
130 AtTop = InsertAtTop,
131 AtCurrent = InsertAtCurrent,
132 AtBottom = InsertAtBottom,
133 AfterCurrent = InsertAfterCurrent,
134 BeforeCurrent = InsertBeforeCurrent
135#endif
136 };
137#ifdef QT3_SUPPORT
138 typedef InsertPolicy Policy;
139#endif
140
141 InsertPolicy insertPolicy() const;
142 void setInsertPolicy(InsertPolicy policy);
143
144 enum SizeAdjustPolicy {
145 AdjustToContents,
146 AdjustToContentsOnFirstShow,
147 AdjustToMinimumContentsLength, // ### Qt 5: remove
148 AdjustToMinimumContentsLengthWithIcon
149 };
150
151 SizeAdjustPolicy sizeAdjustPolicy() const;
152 void setSizeAdjustPolicy(SizeAdjustPolicy policy);
153 int minimumContentsLength() const;
154 void setMinimumContentsLength(int characters);
155 QSize iconSize() const;
156 void setIconSize(const QSize &size);
157
158 bool isEditable() const;
159 void setEditable(bool editable);
160 void setLineEdit(QLineEdit *edit);
161 QLineEdit *lineEdit() const;
162#ifndef QT_NO_VALIDATOR
163 void setValidator(const QValidator *v);
164 const QValidator *validator() const;
165#endif
166
167#ifndef QT_NO_COMPLETER
168 void setCompleter(QCompleter *c);
169 QCompleter *completer() const;
170#endif
171
172 QAbstractItemDelegate *itemDelegate() const;
173 void setItemDelegate(QAbstractItemDelegate *delegate);
174
175 QAbstractItemModel *model() const;
176 void setModel(QAbstractItemModel *model);
177
178 QModelIndex rootModelIndex() const;
179 void setRootModelIndex(const QModelIndex &index);
180
181 int modelColumn() const;
182 void setModelColumn(int visibleColumn);
183
184 int currentIndex() const;
185
186 QString currentText() const;
187
188 QString itemText(int index) const;
189 QIcon itemIcon(int index) const;
190 QVariant itemData(int index, int role = Qt::UserRole) const;
191
192 inline void addItem(const QString &text, const QVariant &userData = QVariant());
193 inline void addItem(const QIcon &icon, const QString &text,
194 const QVariant &userData = QVariant());
195 inline void addItems(const QStringList &texts)
196 { insertItems(count(), texts); }
197
198 inline void insertItem(int index, const QString &text, const QVariant &userData = QVariant());
199 void insertItem(int index, const QIcon &icon, const QString &text,
200 const QVariant &userData = QVariant());
201 void insertItems(int index, const QStringList &texts);
202 void insertSeparator(int index);
203
204 void removeItem(int index);
205
206 void setItemText(int index, const QString &text);
207 void setItemIcon(int index, const QIcon &icon);
208 void setItemData(int index, const QVariant &value, int role = Qt::UserRole);
209
210 QAbstractItemView *view() const;
211 void setView(QAbstractItemView *itemView);
212
213 QSize sizeHint() const;
214 QSize minimumSizeHint() const;
215
216 virtual void showPopup();
217 virtual void hidePopup();
218
219 bool event(QEvent *event);
220
221public Q_SLOTS:
222 void clear();
223 void clearEditText();
224 void setEditText(const QString &text);
225 void setCurrentIndex(int index);
226
227Q_SIGNALS:
228 void editTextChanged(const QString &);
229 void activated(int index);
230 void activated(const QString &);
231 void highlighted(int index);
232 void highlighted(const QString &);
233 void currentIndexChanged(int index);
234 void currentIndexChanged(const QString &);
235
236protected:
237 void focusInEvent(QFocusEvent *e);
238 void focusOutEvent(QFocusEvent *e);
239 void changeEvent(QEvent *e);
240 void resizeEvent(QResizeEvent *e);
241 void paintEvent(QPaintEvent *e);
242 void showEvent(QShowEvent *e);
243 void hideEvent(QHideEvent *e);
244 void mousePressEvent(QMouseEvent *e);
245 void mouseReleaseEvent(QMouseEvent *e);
246 void keyPressEvent(QKeyEvent *e);
247 void keyReleaseEvent(QKeyEvent *e);
248#ifndef QT_NO_WHEELEVENT
249 void wheelEvent(QWheelEvent *e);
250#endif
251 void contextMenuEvent(QContextMenuEvent *e);
252 void inputMethodEvent(QInputMethodEvent *);
253 QVariant inputMethodQuery(Qt::InputMethodQuery) const;
254 void initStyleOption(QStyleOptionComboBox *option) const;
255
256#ifdef QT3_SUPPORT
257public:
258 QT3_SUPPORT_CONSTRUCTOR QComboBox(QWidget *parent, const char *name);
259 QT3_SUPPORT_CONSTRUCTOR QComboBox(bool rw, QWidget *parent, const char *name = 0);
260 inline QT3_SUPPORT int currentItem() const { return currentIndex(); }
261 inline QT3_SUPPORT void setCurrentItem(int index) { setCurrentIndex(index); }
262 inline QT3_SUPPORT InsertPolicy insertionPolicy() const { return insertPolicy(); }
263 inline QT3_SUPPORT void setInsertionPolicy(InsertPolicy policy) { setInsertPolicy(policy); }
264 inline QT3_SUPPORT bool editable() const { return isEditable(); }
265 inline QT3_SUPPORT void popup() { showPopup(); }
266 inline QT3_SUPPORT void setCurrentText(const QString& text) {
267 int i = findText(text);
268 if (i != -1)
269 setCurrentIndex(i);
270 else if (isEditable())
271 setEditText(text);
272 else
273 setItemText(currentIndex(), text);
274 }
275 inline QT3_SUPPORT QString text(int index) const { return itemText(index); }
276
277 inline QT3_SUPPORT QPixmap pixmap(int index) const
278 { return itemIcon(index).pixmap(iconSize(), isEnabled() ? QIcon::Normal : QIcon::Disabled); }
279 inline QT3_SUPPORT void insertStringList(const QStringList &list, int index = -1)
280 { insertItems((index < 0 ? count() : index), list); }
281 inline QT3_SUPPORT void insertItem(const QString &text, int index = -1)
282 { insertItem((index < 0 ? count() : index), text); }
283 inline QT3_SUPPORT void insertItem(const QPixmap &pix, int index = -1)
284 { insertItem((index < 0 ? count() : index), QIcon(pix), QString()); }
285 inline QT3_SUPPORT void insertItem(const QPixmap &pix, const QString &text, int index = -1)
286 { insertItem((index < 0 ? count() : index), QIcon(pix), text); }
287 inline QT3_SUPPORT void changeItem(const QString &text, int index)
288 { setItemText(index, text); }
289 inline QT3_SUPPORT void changeItem(const QPixmap &pix, int index)
290 { setItemIcon(index, QIcon(pix)); }
291 inline QT3_SUPPORT void changeItem(const QPixmap &pix, const QString &text, int index)
292 { setItemIcon(index, QIcon(pix)); setItemText(index, text); }
293 inline QT3_SUPPORT void clearValidator() { setValidator(0); }
294 inline QT3_SUPPORT void clearEdit() { clearEditText(); }
295
296Q_SIGNALS:
297 QT_MOC_COMPAT void textChanged(const QString &);
298#endif
299
300protected:
301 QComboBox(QComboBoxPrivate &, QWidget *);
302
303private:
304 Q_DECLARE_PRIVATE(QComboBox)
305 Q_DISABLE_COPY(QComboBox)
306 Q_PRIVATE_SLOT(d_func(), void _q_itemSelected(const QModelIndex &item))
307 Q_PRIVATE_SLOT(d_func(), void _q_emitHighlighted(const QModelIndex &))
308 Q_PRIVATE_SLOT(d_func(), void _q_emitCurrentIndexChanged(const QModelIndex &index))
309 Q_PRIVATE_SLOT(d_func(), void _q_editingFinished())
310 Q_PRIVATE_SLOT(d_func(), void _q_returnPressed())
311 Q_PRIVATE_SLOT(d_func(), void _q_resetButton())
312 Q_PRIVATE_SLOT(d_func(), void _q_dataChanged(const QModelIndex &, const QModelIndex &))
313 Q_PRIVATE_SLOT(d_func(), void _q_updateIndexBeforeChange())
314 Q_PRIVATE_SLOT(d_func(), void _q_rowsInserted(const QModelIndex & parent, int start, int end))
315 Q_PRIVATE_SLOT(d_func(), void _q_rowsRemoved(const QModelIndex & parent, int start, int end))
316 Q_PRIVATE_SLOT(d_func(), void _q_modelDestroyed())
317 Q_PRIVATE_SLOT(d_func(), void _q_modelReset())
318#ifdef QT_KEYPAD_NAVIGATION
319 Q_PRIVATE_SLOT(d_func(), void _q_completerActivated())
320#endif
321};
322
323inline void QComboBox::addItem(const QString &atext, const QVariant &auserData)
324{ insertItem(count(), atext, auserData); }
325inline void QComboBox::addItem(const QIcon &aicon, const QString &atext,
326 const QVariant &auserData)
327{ insertItem(count(), aicon, atext, auserData); }
328
329inline void QComboBox::insertItem(int aindex, const QString &atext,
330 const QVariant &auserData)
331{ insertItem(aindex, QIcon(), atext, auserData); }
332
333#endif // QT_NO_COMBOBOX
334
335QT_END_NAMESPACE
336
337QT_END_HEADER
338
339#endif // QCOMBOBOX_H
340