QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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
30class QEvent;
31
39#ifndef SIP_RUN
40class 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
93class QgsCheckBoxDelegate : public QStyledItemDelegate
94{
95 Q_OBJECT
96
97 public:
98
103 QgsCheckBoxDelegate( QObject *parent = nullptr );
104
112 void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
113};
114#endif
115
121class GUI_EXPORT QgsCheckableComboBox : public QComboBox
122{
123 Q_OBJECT
124
125 Q_PROPERTY( QString separator READ separator WRITE setSeparator )
126 Q_PROPERTY( QString defaultText READ defaultText WRITE setDefaultText )
127 Q_PROPERTY( QStringList checkedItems READ checkedItems WRITE setCheckedItems )
128
129 public:
130
134 QgsCheckableComboBox( QWidget *parent SIP_TRANSFERTHIS = nullptr );
135
140 QString separator() const;
141
147 void setSeparator( const QString &separator );
148
154 QString defaultText() const;
155
162 void setDefaultText( const QString &text );
163
170 void addItemWithCheckState( const QString &text, Qt::CheckState state, const QVariant &userData = QVariant() );
171
176 QStringList checkedItems() const;
177
183 QVariantList checkedItemsData() const;
184
191 Qt::CheckState itemCheckState( int index ) const;
192
200 void setItemCheckState( int index, Qt::CheckState state );
201
208 void toggleItemCheckState( int index );
209
215 QgsCheckableItemModel *model() const SIP_SKIP {return mModel;}
216
221 void hidePopup() override;
222
226 bool eventFilter( QObject *object, QEvent *event ) override;
227
228 signals:
229
233 void checkedItemsChanged( const QStringList &items );
234
235 public slots:
236
242 void setCheckedItems( const QStringList &items );
243
244 protected:
245
249 void resizeEvent( QResizeEvent *event ) override;
250
251 protected slots:
252
257 void showContextMenu( QPoint pos );
258
262 void selectAllOptions();
263
267 void deselectAllOptions();
268
269 protected:
270 QgsCheckableItemModel *mModel = nullptr;
271
272 private:
273 void updateCheckedItems();
274 void updateDisplayText();
275
276 QString mSeparator;
277 QString mDefaultText;
278
279 bool mSkipHide = false;
280
281 QMenu *mContextMenu = nullptr;
282 QAction *mSelectAllAction = nullptr;
283 QAction *mDeselectAllAction = nullptr;
284};
285
286#endif // QGSCHECKABLECOMBOBOX_H
QStyledItemDelegate subclass for QgsCheckableComboBox.
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.
QgsCheckBoxDelegate(QObject *parent=nullptr)
Constructor for QgsCheckBoxDelegate.
QComboBox subclass which allows selecting multiple items.
void checkedItemsChanged(const QStringList &items)
Emitted whenever the checked items list changed.
QgsCheckableItemModel * model() const
Returns the custom item model which handles checking the items.
QStandardItemModel subclass which makes all items checkable by default.
void itemCheckStateChanged(const QModelIndex &index)
Emitted whenever the item's checkstate has changed.
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
Sets the role data for the item at index to value.
Qt::ItemFlags flags(const QModelIndex &index) const override
Returns a combination of the item flags: items are enabled (ItemIsEnabled), selectable (ItemIsSelecta...
QgsCheckableItemModel(QObject *parent=nullptr)
Constructor for QgsCheckableItemModel.
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.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
#define SIP_SKIP
Definition: qgis_sip.h:126