| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2020 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtWidgets 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 The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #ifndef QCOMBOBOX_H |
| 41 | #define QCOMBOBOX_H |
| 42 | |
| 43 | #include <QtWidgets/qtwidgetsglobal.h> |
| 44 | #include <QtWidgets/qwidget.h> |
| 45 | #include <QtWidgets/qabstractitemdelegate.h> |
| 46 | #include <QtCore/qabstractitemmodel.h> |
| 47 | #include <QtCore/qvariant.h> |
| 48 | #include <QtGui/qvalidator.h> |
| 49 | |
| 50 | QT_REQUIRE_CONFIG(combobox); |
| 51 | |
| 52 | QT_BEGIN_NAMESPACE |
| 53 | |
| 54 | class QAbstractItemView; |
| 55 | class QLineEdit; |
| 56 | class QComboBoxPrivate; |
| 57 | class QCompleter; |
| 58 | |
| 59 | class Q_WIDGETS_EXPORT QComboBox : public QWidget |
| 60 | { |
| 61 | Q_OBJECT |
| 62 | |
| 63 | Q_PROPERTY(bool editable READ isEditable WRITE setEditable) |
| 64 | Q_PROPERTY(int count READ count) |
| 65 | Q_PROPERTY(QString currentText READ currentText WRITE setCurrentText NOTIFY currentTextChanged USER true) |
| 66 | Q_PROPERTY(int currentIndex READ currentIndex WRITE setCurrentIndex NOTIFY currentIndexChanged) |
| 67 | Q_PROPERTY(QVariant currentData READ currentData) |
| 68 | Q_PROPERTY(int maxVisibleItems READ maxVisibleItems WRITE setMaxVisibleItems) |
| 69 | Q_PROPERTY(int maxCount READ maxCount WRITE setMaxCount) |
| 70 | Q_PROPERTY(InsertPolicy insertPolicy READ insertPolicy WRITE setInsertPolicy) |
| 71 | Q_PROPERTY(SizeAdjustPolicy sizeAdjustPolicy READ sizeAdjustPolicy WRITE setSizeAdjustPolicy) |
| 72 | Q_PROPERTY(int minimumContentsLength READ minimumContentsLength WRITE setMinimumContentsLength) |
| 73 | Q_PROPERTY(QSize iconSize READ iconSize WRITE setIconSize) |
| 74 | Q_PROPERTY(QString placeholderText READ placeholderText WRITE setPlaceholderText) |
| 75 | Q_PROPERTY(bool duplicatesEnabled READ duplicatesEnabled WRITE setDuplicatesEnabled) |
| 76 | Q_PROPERTY(bool frame READ hasFrame WRITE setFrame) |
| 77 | Q_PROPERTY(int modelColumn READ modelColumn WRITE setModelColumn) |
| 78 | |
| 79 | public: |
| 80 | explicit QComboBox(QWidget *parent = nullptr); |
| 81 | ~QComboBox(); |
| 82 | |
| 83 | int maxVisibleItems() const; |
| 84 | void setMaxVisibleItems(int maxItems); |
| 85 | |
| 86 | int count() const; |
| 87 | void setMaxCount(int max); |
| 88 | int maxCount() const; |
| 89 | |
| 90 | bool duplicatesEnabled() const; |
| 91 | void setDuplicatesEnabled(bool enable); |
| 92 | |
| 93 | void setFrame(bool); |
| 94 | bool hasFrame() const; |
| 95 | |
| 96 | inline int findText(const QString &text, |
| 97 | Qt::MatchFlags flags = static_cast<Qt::MatchFlags>(Qt::MatchExactly|Qt::MatchCaseSensitive)) const |
| 98 | { return findData(text, Qt::DisplayRole, flags); } |
| 99 | int findData(const QVariant &data, int role = Qt::UserRole, |
| 100 | Qt::MatchFlags flags = static_cast<Qt::MatchFlags>(Qt::MatchExactly|Qt::MatchCaseSensitive)) const; |
| 101 | |
| 102 | enum InsertPolicy { |
| 103 | NoInsert, |
| 104 | InsertAtTop, |
| 105 | InsertAtCurrent, |
| 106 | InsertAtBottom, |
| 107 | InsertAfterCurrent, |
| 108 | InsertBeforeCurrent, |
| 109 | InsertAlphabetically |
| 110 | }; |
| 111 | Q_ENUM(InsertPolicy) |
| 112 | |
| 113 | InsertPolicy insertPolicy() const; |
| 114 | void setInsertPolicy(InsertPolicy policy); |
| 115 | |
| 116 | enum SizeAdjustPolicy { |
| 117 | AdjustToContents, |
| 118 | AdjustToContentsOnFirstShow, |
| 119 | AdjustToMinimumContentsLengthWithIcon |
| 120 | }; |
| 121 | Q_ENUM(SizeAdjustPolicy) |
| 122 | |
| 123 | SizeAdjustPolicy sizeAdjustPolicy() const; |
| 124 | void setSizeAdjustPolicy(SizeAdjustPolicy policy); |
| 125 | int minimumContentsLength() const; |
| 126 | void setMinimumContentsLength(int characters); |
| 127 | QSize iconSize() const; |
| 128 | void setIconSize(const QSize &size); |
| 129 | |
| 130 | void setPlaceholderText(const QString &placeholderText); |
| 131 | QString placeholderText() const; |
| 132 | |
| 133 | bool isEditable() const; |
| 134 | void setEditable(bool editable); |
| 135 | void setLineEdit(QLineEdit *edit); |
| 136 | QLineEdit *lineEdit() const; |
| 137 | #ifndef QT_NO_VALIDATOR |
| 138 | void setValidator(const QValidator *v); |
| 139 | const QValidator *validator() const; |
| 140 | #endif |
| 141 | |
| 142 | #if QT_CONFIG(completer) |
| 143 | void setCompleter(QCompleter *c); |
| 144 | QCompleter *completer() const; |
| 145 | #endif |
| 146 | |
| 147 | QAbstractItemDelegate *itemDelegate() const; |
| 148 | void setItemDelegate(QAbstractItemDelegate *delegate); |
| 149 | |
| 150 | QAbstractItemModel *model() const; |
| 151 | virtual void setModel(QAbstractItemModel *model); |
| 152 | |
| 153 | QModelIndex rootModelIndex() const; |
| 154 | void setRootModelIndex(const QModelIndex &index); |
| 155 | |
| 156 | int modelColumn() const; |
| 157 | void setModelColumn(int visibleColumn); |
| 158 | |
| 159 | int currentIndex() const; |
| 160 | QString currentText() const; |
| 161 | QVariant currentData(int role = Qt::UserRole) const; |
| 162 | |
| 163 | QString itemText(int index) const; |
| 164 | QIcon itemIcon(int index) const; |
| 165 | QVariant itemData(int index, int role = Qt::UserRole) const; |
| 166 | |
| 167 | inline void addItem(const QString &text, const QVariant &userData = QVariant()); |
| 168 | inline void addItem(const QIcon &icon, const QString &text, |
| 169 | const QVariant &userData = QVariant()); |
| 170 | inline void addItems(const QStringList &texts) |
| 171 | { insertItems(count(), texts); } |
| 172 | |
| 173 | inline void insertItem(int index, const QString &text, const QVariant &userData = QVariant()); |
| 174 | void insertItem(int index, const QIcon &icon, const QString &text, |
| 175 | const QVariant &userData = QVariant()); |
| 176 | void insertItems(int index, const QStringList &texts); |
| 177 | void insertSeparator(int index); |
| 178 | |
| 179 | void removeItem(int index); |
| 180 | |
| 181 | void setItemText(int index, const QString &text); |
| 182 | void setItemIcon(int index, const QIcon &icon); |
| 183 | void setItemData(int index, const QVariant &value, int role = Qt::UserRole); |
| 184 | |
| 185 | QAbstractItemView *view() const; |
| 186 | void setView(QAbstractItemView *itemView); |
| 187 | |
| 188 | QSize sizeHint() const override; |
| 189 | QSize minimumSizeHint() const override; |
| 190 | |
| 191 | virtual void (); |
| 192 | virtual void (); |
| 193 | |
| 194 | bool event(QEvent *event) override; |
| 195 | QVariant inputMethodQuery(Qt::InputMethodQuery) const override; |
| 196 | Q_INVOKABLE QVariant inputMethodQuery(Qt::InputMethodQuery query, const QVariant &argument) const; |
| 197 | |
| 198 | public Q_SLOTS: |
| 199 | void clear(); |
| 200 | void clearEditText(); |
| 201 | void setEditText(const QString &text); |
| 202 | void setCurrentIndex(int index); |
| 203 | void setCurrentText(const QString &text); |
| 204 | |
| 205 | Q_SIGNALS: |
| 206 | void editTextChanged(const QString &); |
| 207 | void activated(int index); |
| 208 | void textActivated(const QString &); |
| 209 | void highlighted(int index); |
| 210 | void textHighlighted(const QString &); |
| 211 | void currentIndexChanged(int index); |
| 212 | void currentTextChanged(const QString &); |
| 213 | |
| 214 | protected: |
| 215 | void focusInEvent(QFocusEvent *e) override; |
| 216 | void focusOutEvent(QFocusEvent *e) override; |
| 217 | void changeEvent(QEvent *e) override; |
| 218 | void resizeEvent(QResizeEvent *e) override; |
| 219 | void paintEvent(QPaintEvent *e) override; |
| 220 | void showEvent(QShowEvent *e) override; |
| 221 | void hideEvent(QHideEvent *e) override; |
| 222 | void mousePressEvent(QMouseEvent *e) override; |
| 223 | void mouseReleaseEvent(QMouseEvent *e) override; |
| 224 | void keyPressEvent(QKeyEvent *e) override; |
| 225 | void keyReleaseEvent(QKeyEvent *e) override; |
| 226 | #if QT_CONFIG(wheelevent) |
| 227 | void wheelEvent(QWheelEvent *e) override; |
| 228 | #endif |
| 229 | #ifndef QT_NO_CONTEXTMENU |
| 230 | void (QContextMenuEvent *e) override; |
| 231 | #endif // QT_NO_CONTEXTMENU |
| 232 | void inputMethodEvent(QInputMethodEvent *) override; |
| 233 | virtual void initStyleOption(QStyleOptionComboBox *option) const; |
| 234 | |
| 235 | |
| 236 | protected: |
| 237 | QComboBox(QComboBoxPrivate &, QWidget *); |
| 238 | |
| 239 | private: |
| 240 | Q_DECLARE_PRIVATE(QComboBox) |
| 241 | Q_DISABLE_COPY(QComboBox) |
| 242 | Q_PRIVATE_SLOT(d_func(), void _q_itemSelected(const QModelIndex &item)) |
| 243 | Q_PRIVATE_SLOT(d_func(), void _q_emitHighlighted(const QModelIndex &)) |
| 244 | Q_PRIVATE_SLOT(d_func(), void _q_emitCurrentIndexChanged(const QModelIndex &index)) |
| 245 | Q_PRIVATE_SLOT(d_func(), void _q_editingFinished()) |
| 246 | Q_PRIVATE_SLOT(d_func(), void _q_returnPressed()) |
| 247 | Q_PRIVATE_SLOT(d_func(), void _q_resetButton()) |
| 248 | Q_PRIVATE_SLOT(d_func(), void _q_dataChanged(const QModelIndex &, const QModelIndex &)) |
| 249 | Q_PRIVATE_SLOT(d_func(), void _q_updateIndexBeforeChange()) |
| 250 | Q_PRIVATE_SLOT(d_func(), void _q_rowsInserted(const QModelIndex & parent, int start, int end)) |
| 251 | Q_PRIVATE_SLOT(d_func(), void _q_rowsRemoved(const QModelIndex & parent, int start, int end)) |
| 252 | Q_PRIVATE_SLOT(d_func(), void _q_modelDestroyed()) |
| 253 | Q_PRIVATE_SLOT(d_func(), void _q_modelReset()) |
| 254 | #if QT_CONFIG(completer) |
| 255 | Q_PRIVATE_SLOT(d_func(), void _q_completerActivated(const QModelIndex &index)) |
| 256 | #endif |
| 257 | }; |
| 258 | |
| 259 | inline void QComboBox::addItem(const QString &atext, const QVariant &auserData) |
| 260 | { insertItem(count(), atext, auserData); } |
| 261 | inline void QComboBox::addItem(const QIcon &aicon, const QString &atext, |
| 262 | const QVariant &auserData) |
| 263 | { insertItem(count(), aicon, atext, auserData); } |
| 264 | |
| 265 | inline void QComboBox::insertItem(int aindex, const QString &atext, |
| 266 | const QVariant &auserData) |
| 267 | { insertItem(aindex, QIcon(), atext, auserData); } |
| 268 | |
| 269 | QT_END_NAMESPACE |
| 270 | |
| 271 | #endif // QCOMBOBOX_H |
| 272 | |