QGIS API Documentation 3.99.0-Master (26c88405ac0)
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 "qgis.h"
22#include "qgis_gui.h"
23#include "qgis_sip.h"
24
25#include <QComboBox>
26#include <QMenu>
27#include <QStandardItemModel>
28#include <QStyledItemDelegate>
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
216 {
217 return mModel;
218 }
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:
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
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.
void setSeparator(const QString &separator)
Set separator used to separate items in the display text.
void setItemCheckState(int index, Qt::CheckState state)
Sets the item check state to state.
void deselectAllOptions()
Removes selection from all items.
void selectAllOptions()
Selects all items.
void checkedItemsChanged(const QStringList &items)
Emitted whenever the checked items list changed.
void setCheckedItems(const QStringList &items)
Set items which should be checked/selected.
void toggleItemCheckState(int index)
Toggles the item check state.
QgsCheckableComboBox(QWidget *parent=nullptr)
Constructor for QgsCheckableComboBox.
QVariantList checkedItemsData() const
Returns userData (stored in the Qt::UserRole) associated with currently checked items.
QgsCheckableItemModel * mModel
QgsCheckableItemModel * model() const
Returns the custom item model which handles checking the items.
Qt::CheckState itemCheckState(int index) const
Returns the checked state of the item identified by index.
void addItemWithCheckState(const QString &text, Qt::CheckState state, const QVariant &userData=QVariant())
Adds an item to the combobox with the given text, check state (stored in the Qt::CheckStateRole) and ...
void showContextMenu(QPoint pos)
Display context menu which allows selecting/deselecting all items at once.
void resizeEvent(QResizeEvent *event) override
Handler for widget resizing.
void setDefaultText(const QString &text)
Set default text which will be displayed in the widget when no items selected.
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:134