QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgscheckablecombobox.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgscheckablecombobox.h
3  ------------------------
4  begin : March 21, 2017
5  copyright : (C) 2017 by Alexander Bruy
6  email : alexander dot bruy at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef QGSCHECKABLECOMBOBOX_H
19 #define QGSCHECKABLECOMBOBOX_H
20 
21 #include <QComboBox>
22 #include <QMenu>
23 #include <QStandardItemModel>
24 #include <QStyledItemDelegate>
25 
26 #include "qgis_sip.h"
27 #include "qgis_gui.h"
28 #include "qgis.h"
29 
30 class QEvent;
31 
40 #ifndef SIP_RUN
41 class QgsCheckableItemModel : public QStandardItemModel
42 {
43  Q_OBJECT
44 
45  public:
46 
51  QgsCheckableItemModel( QObject *parent = nullptr );
52 
59  Qt::ItemFlags flags( const QModelIndex &index ) const override;
60 
67  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
68 
76  bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
77 
78  signals:
79 
83  void itemCheckStateChanged( const QModelIndex &index );
84 };
85 
86 
95 class QgsCheckBoxDelegate : public QStyledItemDelegate
96 {
97  Q_OBJECT
98 
99  public:
100 
105  QgsCheckBoxDelegate( QObject *parent = nullptr );
106 
114  void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
115 };
116 #endif
117 
124 class GUI_EXPORT QgsCheckableComboBox : public QComboBox
125 {
126  Q_OBJECT
127 
128  Q_PROPERTY( QString separator READ separator WRITE setSeparator )
129  Q_PROPERTY( QString defaultText READ defaultText WRITE setDefaultText )
130  Q_PROPERTY( QStringList checkedItems READ checkedItems WRITE setCheckedItems )
131 
132  public:
133 
137  QgsCheckableComboBox( QWidget *parent SIP_TRANSFERTHIS = nullptr );
138 
143  QString separator() const;
144 
150  void setSeparator( const QString &separator );
151 
157  QString defaultText() const;
158 
165  void setDefaultText( const QString &text );
166 
173  void addItemWithCheckState( const QString &text, Qt::CheckState state, const QVariant &userData = QVariant() );
174 
179  QStringList checkedItems() const;
180 
186  QVariantList checkedItemsData() const;
187 
194  Qt::CheckState itemCheckState( int index ) const;
195 
203  void setItemCheckState( int index, Qt::CheckState state );
204 
211  void toggleItemCheckState( int index );
212 
218  QgsCheckableItemModel *model() const SIP_SKIP {return mModel;}
219 
224  void hidePopup() override;
225 
229  bool eventFilter( QObject *object, QEvent *event ) override;
230 
231  signals:
232 
236  void checkedItemsChanged( const QStringList &items );
237 
238  public slots:
239 
245  void setCheckedItems( const QStringList &items );
246 
247  protected:
248 
252  void resizeEvent( QResizeEvent *event ) override;
253 
254  protected slots:
255 
260  void showContextMenu( QPoint pos );
261 
265  void selectAllOptions();
266 
270  void deselectAllOptions();
271 
272  protected:
273  QgsCheckableItemModel *mModel = nullptr;
274 
275  private:
276  void updateCheckedItems();
277  void updateDisplayText();
278 
279  QString mSeparator;
280  QString mDefaultText;
281 
282  bool mSkipHide = false;
283 
284  QMenu *mContextMenu = nullptr;
285  QAction *mSelectAllAction = nullptr;
286  QAction *mDeselectAllAction = nullptr;
287 };
288 
289 #endif // QGSCHECKABLECOMBOBOX_H
QgsCheckableItemModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const override
Returns a combination of the item flags: items are enabled (ItemIsEnabled), selectable (ItemIsSelecta...
Definition: qgscheckablecombobox.cpp:33
qgis.h
QgsCheckableItemModel::QgsCheckableItemModel
QgsCheckableItemModel(QObject *parent=nullptr)
Constructor for QgsCheckableItemModel.
Definition: qgscheckablecombobox.cpp:28
QgsCheckBoxDelegate
QStyledItemDelegate subclass for QgsCheckableComboBox.
Definition: qgscheckablecombobox.h:96
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
qgis_sip.h
QgsCheckBoxDelegate::QgsCheckBoxDelegate
QgsCheckBoxDelegate(QObject *parent=nullptr)
Constructor for QgsCheckBoxDelegate.
Definition: qgscheckablecombobox.cpp:64
QgsCheckableItemModel::data
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Returns the data stored under the given role for the item referred to by the index.
Definition: qgscheckablecombobox.cpp:38
QgsCheckableComboBox
QComboBox subclass which allows selecting multiple items.
Definition: qgscheckablecombobox.h:125
QgsCheckableComboBox::model
QgsCheckableItemModel * model() const
Returns the custom item model which handles checking the items.
Definition: qgscheckablecombobox.h:218
QgsCheckableComboBox::checkedItemsChanged
void checkedItemsChanged(const QStringList &items)
Emitted whenever the checked items list changed.
QgsCheckableItemModel
QStandardItemModel subclass which makes all items checkable by default.
Definition: qgscheckablecombobox.h:42
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
QgsCheckableItemModel::setData
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
Sets the role data for the item at index to value.
Definition: qgscheckablecombobox.cpp:50
QgsCheckableItemModel::itemCheckStateChanged
void itemCheckStateChanged(const QModelIndex &index)
Emitted whenever the item's checkstate has changed.
QgsCheckBoxDelegate::paint
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Renders the delegate using the given painter and style option for the item specified by index.
Definition: qgscheckablecombobox.cpp:69