QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
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:
49 QgsCheckableItemModel( QObject *parent = nullptr );
50
57 Qt::ItemFlags flags( const QModelIndex &index ) const override;
58
65 QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
66
74 bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
75
76 signals:
77
81 void itemCheckStateChanged( const QModelIndex &index );
82};
83
84
92class QgsCheckBoxDelegate : public QStyledItemDelegate
93{
94 Q_OBJECT
95
96 public:
101 QgsCheckBoxDelegate( QObject *parent = nullptr );
102
110 void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
111};
112#endif
113
119class GUI_EXPORT QgsCheckableComboBox : public QComboBox
120{
121 Q_OBJECT
122
123 Q_PROPERTY( QString separator READ separator WRITE setSeparator )
124 Q_PROPERTY( QString defaultText READ defaultText WRITE setDefaultText )
125 Q_PROPERTY( QStringList checkedItems READ checkedItems WRITE setCheckedItems )
126
127 public:
131 QgsCheckableComboBox( QWidget *parent SIP_TRANSFERTHIS = nullptr );
132
137 QString separator() const;
138
144 void setSeparator( const QString &separator );
145
151 QString defaultText() const;
152
159 void setDefaultText( const QString &text );
160
167 void addItemWithCheckState( const QString &text, Qt::CheckState state, const QVariant &userData = QVariant() );
168
173 QStringList checkedItems() const;
174
180 QVariantList checkedItemsData() const;
181
188 Qt::CheckState itemCheckState( int index ) const;
189
197 void setItemCheckState( int index, Qt::CheckState state );
198
205 void toggleItemCheckState( int index );
206
213 {
214 return mModel;
215 }
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:
248 void resizeEvent( QResizeEvent *event ) override;
249
250 protected slots:
251
256 void showContextMenu( QPoint pos );
257
261 void selectAllOptions();
262
266 void deselectAllOptions();
267
268 protected:
269 QgsCheckableItemModel *mModel = nullptr;
270
271 private:
272 void updateCheckedItems();
273 void updateDisplayText();
274
275 QString mSeparator;
276 QString mDefaultText;
277
278 bool mSkipHide = false;
279
280 QMenu *mContextMenu = nullptr;
281 QAction *mSelectAllAction = nullptr;
282 QAction *mDeselectAllAction = nullptr;
283};
284
285#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.
QComboBox subclass which allows selecting multiple items.
void checkedItemsChanged(const QStringList &items)
Emitted whenever the checked items list changed.
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...
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