QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
29 class QEvent;
30 
39 #ifndef SIP_RUN
40 class QgsCheckableItemModel : public QStandardItemModel
41 {
42  Q_OBJECT
43 
44  public:
45 
50  QgsCheckableItemModel( QObject *parent = nullptr );
51 
58  Qt::ItemFlags flags( const QModelIndex &index ) const override;
59 
66  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
67 
75  bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
76 
77  signals:
78 
82  void itemCheckStateChanged( const QModelIndex &index );
83 };
84 
85 
94 class QgsCheckBoxDelegate : public QStyledItemDelegate
95 {
96  Q_OBJECT
97 
98  public:
99 
104  QgsCheckBoxDelegate( QObject *parent = nullptr );
105 
113  void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
114 };
115 #endif
116 
123 class GUI_EXPORT QgsCheckableComboBox : public QComboBox
124 {
125  Q_OBJECT
126 
127  Q_PROPERTY( QString separator READ separator WRITE setSeparator )
128  Q_PROPERTY( QString defaultText READ defaultText WRITE setDefaultText )
129  Q_PROPERTY( QStringList checkedItems READ checkedItems WRITE setCheckedItems )
130 
131  public:
132 
136  QgsCheckableComboBox( QWidget *parent SIP_TRANSFERTHIS = nullptr );
137 
142  QString separator() const;
143 
149  void setSeparator( const QString &separator );
150 
156  QString defaultText() const;
157 
164  void setDefaultText( const QString &text );
165 
170  QStringList checkedItems() const;
171 
177  QVariantList checkedItemsData() const;
178 
185  Qt::CheckState itemCheckState( int index ) const;
186 
194  void setItemCheckState( int index, Qt::CheckState state );
195 
202  void toggleItemCheckState( int index );
203 
208  void hidePopup() override;
209 
213  bool eventFilter( QObject *object, QEvent *event ) override;
214 
215  signals:
216 
220  void checkedItemsChanged( const QStringList &items );
221 
222  public slots:
223 
229  void setCheckedItems( const QStringList &items );
230 
231  protected:
232 
236  void resizeEvent( QResizeEvent *event ) override;
237 
238  protected slots:
239 
244  void showContextMenu( QPoint pos );
245 
249  void selectAllOptions();
250 
254  void deselectAllOptions();
255 
256  private:
257  void updateCheckedItems();
258  void updateDisplayText();
259 
260  QString mSeparator;
261  QString mDefaultText;
262 
263  bool mSkipHide = false;
264 
265  QMenu *mContextMenu = nullptr;
266  QAction *mSelectAllAction = nullptr;
267  QAction *mDeselectAllAction = nullptr;
268 };
269 
270 #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
QgsCheckableItemModel::QgsCheckableItemModel
QgsCheckableItemModel(QObject *parent=nullptr)
Constructor for QgsCheckableItemModel.
Definition: qgscheckablecombobox.cpp:28
QgsCheckBoxDelegate
Definition: qgscheckablecombobox.h:94
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
Definition: qgscheckablecombobox.h:123
QgsCheckableItemModel
Definition: qgscheckablecombobox.h:40
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