QGIS API Documentation  3.6.0-Noosa (5873452)
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();
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 
178  Qt::CheckState itemCheckState( int index ) const;
179 
187  void setItemCheckState( int index, Qt::CheckState state );
188 
195  void toggleItemCheckState( int index );
196 
201  void hidePopup() override;
202 
206  bool eventFilter( QObject *object, QEvent *event ) override;
207 
208  signals:
209 
213  void checkedItemsChanged( const QStringList &items );
214 
215  public slots:
216 
222  void setCheckedItems( const QStringList &items );
223 
224  protected:
225 
229  void resizeEvent( QResizeEvent *event ) override;
230 
231  protected slots:
232 
237  void showContextMenu( QPoint pos );
238 
242  void selectAllOptions();
243 
247  void deselectAllOptions();
248 
249  private:
250  void updateCheckedItems();
251  void updateDisplayText();
252 
253  QString mSeparator;
254  QString mDefaultText;
255 
256  bool mSkipHide = false;
257 
258  QMenu *mContextMenu = nullptr;
259  QAction *mSelectAllAction = nullptr;
260  QAction *mDeselectAllAction = nullptr;
261 };
262 
263 #endif // QGSCHECKABLECOMBOBOX_H
QgsCheckableItemModel(QObject *parent=nullptr)
Constructor for QgsCheckableItemModel.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
QStyledItemDelegate subclass for QgsCheckableComboBox.
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
Sets the role data for the item at index to value.
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.
QComboBox subclass which allows selecting multiple items.
void itemCheckStateChanged()
This signal is emitted whenever the items checkstate has changed.
Qt::ItemFlags flags(const QModelIndex &index) const override
Returns a combination of the item flags: items are enabled (ItemIsEnabled), selectable (ItemIsSelecta...
QStandardItemModel subclass which makes all items checkable by default.