qcombobox.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the QtGui module of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:LGPL$
00010 ** Commercial Usage
00011 ** Licensees holding valid Qt Commercial licenses may use this file in
00012 ** accordance with the Qt Commercial License Agreement provided with the
00013 ** Software or, alternatively, in accordance with the terms contained in
00014 ** a written agreement between you and Nokia.
00015 **
00016 ** GNU Lesser General Public License Usage
00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
00018 ** General Public License version 2.1 as published by the Free Software
00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
00020 ** packaging of this file.  Please review the following information to
00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00023 **
00024 ** In addition, as a special exception, Nokia gives you certain additional
00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
00027 **
00028 ** GNU General Public License Usage
00029 ** Alternatively, this file may be used under the terms of the GNU
00030 ** General Public License version 3.0 as published by the Free Software
00031 ** Foundation and appearing in the file LICENSE.GPL included in the
00032 ** packaging of this file.  Please review the following information to
00033 ** ensure the GNU General Public License version 3.0 requirements will be
00034 ** met: http://www.gnu.org/copyleft/gpl.html.
00035 **
00036 ** If you have questions regarding the use of this file, please contact
00037 ** Nokia at qt-info@nokia.com.
00038 ** $QT_END_LICENSE$
00039 **
00040 ****************************************************************************/
00041 
00042 #ifndef QCOMBOBOX_H
00043 #define QCOMBOBOX_H
00044 
00045 #include <QtGui/qwidget.h>
00046 #include <QtGui/qabstractitemdelegate.h>
00047 #include <QtCore/qabstractitemmodel.h>
00048 #include <QtCore/qvariant.h>
00049 
00050 QT_BEGIN_HEADER
00051 
00052 QT_BEGIN_NAMESPACE
00053 
00054 QT_MODULE(Gui)
00055 #ifndef QT_NO_COMBOBOX
00056 
00057 class QAbstractItemView;
00058 class QLineEdit;
00059 class QComboBoxPrivate;
00060 class QCompleter;
00061 
00062 class Q_GUI_EXPORT QComboBox : public QWidget
00063 {
00064     Q_OBJECT
00065 
00066     Q_ENUMS(InsertPolicy)
00067     Q_ENUMS(SizeAdjustPolicy)
00068     Q_PROPERTY(bool editable READ isEditable WRITE setEditable)
00069     Q_PROPERTY(int count READ count)
00070     Q_PROPERTY(QString currentText READ currentText)
00071     Q_PROPERTY(int currentIndex READ currentIndex WRITE setCurrentIndex NOTIFY currentIndexChanged)
00072     Q_PROPERTY(int maxVisibleItems READ maxVisibleItems WRITE setMaxVisibleItems)
00073     Q_PROPERTY(int maxCount READ maxCount WRITE setMaxCount)
00074     Q_PROPERTY(InsertPolicy insertPolicy READ insertPolicy WRITE setInsertPolicy)
00075     Q_PROPERTY(SizeAdjustPolicy sizeAdjustPolicy READ sizeAdjustPolicy WRITE setSizeAdjustPolicy)
00076     Q_PROPERTY(int minimumContentsLength READ minimumContentsLength WRITE setMinimumContentsLength)
00077     Q_PROPERTY(QSize iconSize READ iconSize WRITE setIconSize)
00078 
00079 #ifndef QT_NO_COMPLETER
00080     Q_PROPERTY(bool autoCompletion READ autoCompletion WRITE setAutoCompletion DESIGNABLE false)
00081     Q_PROPERTY(Qt::CaseSensitivity autoCompletionCaseSensitivity READ autoCompletionCaseSensitivity WRITE setAutoCompletionCaseSensitivity DESIGNABLE false)
00082 #endif // QT_NO_COMPLETER
00083 
00084     Q_PROPERTY(bool duplicatesEnabled READ duplicatesEnabled WRITE setDuplicatesEnabled)
00085     Q_PROPERTY(bool frame READ hasFrame WRITE setFrame)
00086     Q_PROPERTY(int modelColumn READ modelColumn WRITE setModelColumn)
00087 
00088 public:
00089     explicit QComboBox(QWidget *parent = 0);
00090     ~QComboBox();
00091 
00092     int maxVisibleItems() const;
00093     void setMaxVisibleItems(int maxItems);
00094 
00095     int count() const;
00096     void setMaxCount(int max);
00097     int maxCount() const;
00098 
00099 #ifndef QT_NO_COMPLETER
00100     bool autoCompletion() const;
00101     void setAutoCompletion(bool enable);
00102 
00103     Qt::CaseSensitivity autoCompletionCaseSensitivity() const;
00104     void setAutoCompletionCaseSensitivity(Qt::CaseSensitivity sensitivity);
00105 #endif
00106 
00107     bool duplicatesEnabled() const;
00108     void setDuplicatesEnabled(bool enable);
00109 
00110     void setFrame(bool);
00111     bool hasFrame() const;
00112 
00113     inline int findText(const QString &text,
00114                         Qt::MatchFlags flags = static_cast<Qt::MatchFlags>(Qt::MatchExactly|Qt::MatchCaseSensitive)) const
00115         { return findData(text, Qt::DisplayRole, flags); }
00116     int findData(const QVariant &data, int role = Qt::UserRole,
00117                  Qt::MatchFlags flags = static_cast<Qt::MatchFlags>(Qt::MatchExactly|Qt::MatchCaseSensitive)) const;
00118 
00119     enum InsertPolicy {
00120         NoInsert,
00121         InsertAtTop,
00122         InsertAtCurrent,
00123         InsertAtBottom,
00124         InsertAfterCurrent,
00125         InsertBeforeCurrent,
00126         InsertAlphabetically
00127 #if defined(QT3_SUPPORT) && !defined(Q_MOC_RUN)
00128         ,
00129         NoInsertion = NoInsert,
00130         AtTop = InsertAtTop,
00131         AtCurrent = InsertAtCurrent,
00132         AtBottom = InsertAtBottom,
00133         AfterCurrent = InsertAfterCurrent,
00134         BeforeCurrent = InsertBeforeCurrent
00135 #endif
00136     };
00137 #ifdef QT3_SUPPORT
00138     typedef InsertPolicy Policy;
00139 #endif
00140 
00141     InsertPolicy insertPolicy() const;
00142     void setInsertPolicy(InsertPolicy policy);
00143 
00144     enum SizeAdjustPolicy {
00145         AdjustToContents,
00146         AdjustToContentsOnFirstShow,
00147         AdjustToMinimumContentsLength, // ### Qt 5: remove
00148         AdjustToMinimumContentsLengthWithIcon
00149     };
00150 
00151     SizeAdjustPolicy sizeAdjustPolicy() const;
00152     void setSizeAdjustPolicy(SizeAdjustPolicy policy);
00153     int minimumContentsLength() const;
00154     void setMinimumContentsLength(int characters);
00155     QSize iconSize() const;
00156     void setIconSize(const QSize &size);
00157 
00158     bool isEditable() const;
00159     void setEditable(bool editable);
00160     void setLineEdit(QLineEdit *edit);
00161     QLineEdit *lineEdit() const;
00162 #ifndef QT_NO_VALIDATOR
00163     void setValidator(const QValidator *v);
00164     const QValidator *validator() const;
00165 #endif
00166 
00167 #ifndef QT_NO_COMPLETER
00168     void setCompleter(QCompleter *c);
00169     QCompleter *completer() const;
00170 #endif
00171 
00172     QAbstractItemDelegate *itemDelegate() const;
00173     void setItemDelegate(QAbstractItemDelegate *delegate);
00174 
00175     QAbstractItemModel *model() const;
00176     void setModel(QAbstractItemModel *model);
00177 
00178     QModelIndex rootModelIndex() const;
00179     void setRootModelIndex(const QModelIndex &index);
00180 
00181     int modelColumn() const;
00182     void setModelColumn(int visibleColumn);
00183 
00184     int currentIndex() const;
00185 
00186     QString currentText() const;
00187 
00188     QString itemText(int index) const;
00189     QIcon itemIcon(int index) const;
00190     QVariant itemData(int index, int role = Qt::UserRole) const;
00191 
00192     inline void addItem(const QString &text, const QVariant &userData = QVariant());
00193     inline void addItem(const QIcon &icon, const QString &text,
00194                         const QVariant &userData = QVariant());
00195     inline void addItems(const QStringList &texts)
00196         { insertItems(count(), texts); }
00197 
00198     inline void insertItem(int index, const QString &text, const QVariant &userData = QVariant());
00199     void insertItem(int index, const QIcon &icon, const QString &text,
00200                     const QVariant &userData = QVariant());
00201     void insertItems(int index, const QStringList &texts);
00202     void insertSeparator(int index);
00203 
00204     void removeItem(int index);
00205 
00206     void setItemText(int index, const QString &text);
00207     void setItemIcon(int index, const QIcon &icon);
00208     void setItemData(int index, const QVariant &value, int role = Qt::UserRole);
00209 
00210     QAbstractItemView *view() const;
00211     void setView(QAbstractItemView *itemView);
00212 
00213     QSize sizeHint() const;
00214     QSize minimumSizeHint() const;
00215 
00216     virtual void showPopup();
00217     virtual void hidePopup();
00218 
00219     bool event(QEvent *event);
00220 
00221 public Q_SLOTS:
00222     void clear();
00223     void clearEditText();
00224     void setEditText(const QString &text);
00225     void setCurrentIndex(int index);
00226 
00227 Q_SIGNALS:
00228     void editTextChanged(const QString &);
00229     void activated(int index);
00230     void activated(const QString &);
00231     void highlighted(int index);
00232     void highlighted(const QString &);
00233     void currentIndexChanged(int index);
00234     void currentIndexChanged(const QString &);
00235 
00236 protected:
00237     void focusInEvent(QFocusEvent *e);
00238     void focusOutEvent(QFocusEvent *e);
00239     void changeEvent(QEvent *e);
00240     void resizeEvent(QResizeEvent *e);
00241     void paintEvent(QPaintEvent *e);
00242     void showEvent(QShowEvent *e);
00243     void hideEvent(QHideEvent *e);
00244     void mousePressEvent(QMouseEvent *e);
00245     void mouseReleaseEvent(QMouseEvent *e);
00246     void keyPressEvent(QKeyEvent *e);
00247     void keyReleaseEvent(QKeyEvent *e);
00248 #ifndef QT_NO_WHEELEVENT
00249     void wheelEvent(QWheelEvent *e);
00250 #endif
00251     void contextMenuEvent(QContextMenuEvent *e);
00252     void inputMethodEvent(QInputMethodEvent *);
00253     QVariant inputMethodQuery(Qt::InputMethodQuery) const;
00254     void initStyleOption(QStyleOptionComboBox *option) const;
00255 
00256 #ifdef QT3_SUPPORT
00257 public:
00258     QT3_SUPPORT_CONSTRUCTOR QComboBox(QWidget *parent, const char *name);
00259     QT3_SUPPORT_CONSTRUCTOR QComboBox(bool rw, QWidget *parent, const char *name = 0);
00260     inline QT3_SUPPORT int currentItem() const { return currentIndex(); }
00261     inline QT3_SUPPORT void setCurrentItem(int index) { setCurrentIndex(index); }
00262     inline QT3_SUPPORT InsertPolicy insertionPolicy() const { return insertPolicy(); }
00263     inline QT3_SUPPORT void setInsertionPolicy(InsertPolicy policy) { setInsertPolicy(policy); }
00264     inline QT3_SUPPORT bool editable() const { return isEditable(); }
00265     inline QT3_SUPPORT void popup() { showPopup(); }
00266     inline QT3_SUPPORT void setCurrentText(const QString& text) {
00267         int i = findText(text);
00268         if (i != -1)
00269             setCurrentIndex(i);
00270         else if (isEditable())
00271             setEditText(text);
00272         else
00273             setItemText(currentIndex(), text);
00274     }
00275     inline QT3_SUPPORT QString text(int index) const { return itemText(index); }
00276 
00277     inline QT3_SUPPORT QPixmap pixmap(int index) const
00278     { return itemIcon(index).pixmap(iconSize(), isEnabled() ? QIcon::Normal : QIcon::Disabled); }
00279     inline QT3_SUPPORT void insertStringList(const QStringList &list, int index = -1)
00280         { insertItems((index < 0 ? count() : index), list); }
00281     inline QT3_SUPPORT void insertItem(const QString &text, int index = -1)
00282         { insertItem((index < 0 ? count() : index), text); }
00283     inline QT3_SUPPORT void insertItem(const QPixmap &pix, int index = -1)
00284         { insertItem((index < 0 ? count() : index), QIcon(pix), QString()); }
00285     inline QT3_SUPPORT void insertItem(const QPixmap &pix, const QString &text, int index = -1)
00286         { insertItem((index < 0 ? count() : index), QIcon(pix), text); }
00287     inline QT3_SUPPORT void changeItem(const QString &text, int index)
00288         { setItemText(index, text); }
00289     inline QT3_SUPPORT void changeItem(const QPixmap &pix, int index)
00290         { setItemIcon(index, QIcon(pix)); }
00291     inline QT3_SUPPORT void changeItem(const QPixmap &pix, const QString &text, int index)
00292         { setItemIcon(index, QIcon(pix)); setItemText(index, text); }
00293     inline QT3_SUPPORT void clearValidator() { setValidator(0); }
00294     inline QT3_SUPPORT void clearEdit() { clearEditText(); }
00295 
00296 Q_SIGNALS:
00297     QT_MOC_COMPAT void textChanged(const QString &);
00298 #endif
00299 
00300 protected:
00301     QComboBox(QComboBoxPrivate &, QWidget *);
00302 
00303 private:
00304     Q_DECLARE_PRIVATE(QComboBox)
00305     Q_DISABLE_COPY(QComboBox)
00306     Q_PRIVATE_SLOT(d_func(), void _q_itemSelected(const QModelIndex &item))
00307     Q_PRIVATE_SLOT(d_func(), void _q_emitHighlighted(const QModelIndex &))
00308     Q_PRIVATE_SLOT(d_func(), void _q_emitCurrentIndexChanged(const QModelIndex &index))
00309     Q_PRIVATE_SLOT(d_func(), void _q_editingFinished())
00310     Q_PRIVATE_SLOT(d_func(), void _q_returnPressed())
00311     Q_PRIVATE_SLOT(d_func(), void _q_resetButton())
00312     Q_PRIVATE_SLOT(d_func(), void _q_dataChanged(const QModelIndex &, const QModelIndex &))
00313     Q_PRIVATE_SLOT(d_func(), void _q_updateIndexBeforeChange())
00314     Q_PRIVATE_SLOT(d_func(), void _q_rowsInserted(const QModelIndex & parent, int start, int end))
00315     Q_PRIVATE_SLOT(d_func(), void _q_rowsRemoved(const QModelIndex & parent, int start, int end))
00316     Q_PRIVATE_SLOT(d_func(), void _q_modelDestroyed())
00317     Q_PRIVATE_SLOT(d_func(), void _q_modelReset())
00318 #ifdef QT_KEYPAD_NAVIGATION
00319     Q_PRIVATE_SLOT(d_func(), void _q_completerActivated())
00320 #endif
00321 };
00322 
00323 inline void QComboBox::addItem(const QString &atext, const QVariant &auserData)
00324 { insertItem(count(), atext, auserData); }
00325 inline void QComboBox::addItem(const QIcon &aicon, const QString &atext,
00326                                const QVariant &auserData)
00327 { insertItem(count(), aicon, atext, auserData); }
00328 
00329 inline void QComboBox::insertItem(int aindex, const QString &atext,
00330                                   const QVariant &auserData)
00331 { insertItem(aindex, QIcon(), atext, auserData); }
00332 
00333 #endif // QT_NO_COMBOBOX
00334 
00335 QT_END_NAMESPACE
00336 
00337 QT_END_HEADER
00338 
00339 #endif // QCOMBOBOX_H