-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
qgscheckablecombobox.h
238 lines (189 loc) · 6.74 KB
/
qgscheckablecombobox.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/***************************************************************************
qgscheckablecombobox.h
------------------------
begin : March 21, 2017
copyright : (C) 2017 by Alexander Bruy
email : alexander dot bruy at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSCHECKABLECOMBOBOX_H
#define QGSCHECKABLECOMBOBOX_H
#include <QComboBox>
#include <QMenu>
#include <QStandardItemModel>
#include <QStyledItemDelegate>
#include "qgis_gui.h"
class QEvent;
/** \class QgsCheckableItemModel
* \ingroup gui
* QStandardItemModel subclass which makes all items checkable
* by default.
* \since QGIS 3.0
* \note not available in Python bindings
**/
#ifndef SIP_RUN
class QgsCheckableItemModel : public QStandardItemModel
{
Q_OBJECT
public:
/** Constructor for QgsCheckableItemModel.
* \param parent parent object
*/
QgsCheckableItemModel( QObject *parent = nullptr );
/** Returns a combination of the item flags: items are enabled
* (ItemIsEnabled), selectable (ItemIsSelectable) and checkable
* (ItemIsUserCheckable).
* \param index item index
*/
virtual Qt::ItemFlags flags( const QModelIndex &index ) const;
/** Returns the data stored under the given role for the item
* referred to by the index.
* \param index item index
* \param role data role
*/
virtual QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const;
/** Sets the role data for the item at index to value.
* \param index item index
* \param value data value
* \param role data role
* \returns true on success, false otherwise
*/
virtual bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole );
signals:
/** This signal is emitted whenever the items checkstate has changed.
*/
void itemCheckStateChanged();
};
/** \class QgsCheckBoxDelegate
* \ingroup gui
* QStyledItemDelegate subclass for QgsCheckableComboBox. Needed for
* correct drawing of the checkable items on Mac and GTK.
* \since QGIS 3.0
* \note not available in Python bindings
**/
class QgsCheckBoxDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
/** Constructor for QgsCheckBoxDelegate.
* \param parent parent object
*/
QgsCheckBoxDelegate( QObject *parent = nullptr );
/** Renders the delegate using the given painter and style option
* for the item specified by index.
* \param painter painter to use
* \param option style option
* \param index item index
*/
virtual void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const;
};
#endif
/** \class QgsCheckableComboBox
* \ingroup gui
* QComboBox subclass which allows selecting multiple items.
* \since QGIS 3.0
**/
class GUI_EXPORT QgsCheckableComboBox : public QComboBox
{
Q_OBJECT
Q_PROPERTY( QString separator READ separator WRITE setSeparator )
Q_PROPERTY( QString defaultText READ defaultText WRITE setDefaultText )
Q_PROPERTY( QStringList checkedItems READ checkedItems WRITE setCheckedItems )
public:
/** Constructor for QgsCheckableComboBox.
* \param parent parent object
*/
QgsCheckableComboBox( QWidget *parent = nullptr );
/** Returns separator used to separate items in the display text.
* \see setSeparator()
*/
QString separator() const;
/** Set separator used to separate items in the display text.
* \param separator separator to use
* \see separator()
*/
void setSeparator( const QString &separator );
/** Returns default text which will be displayed in the widget
* when no items selected.
* \see setDefaultText()
*/
QString defaultText() const;
/** Set default text which will be displayed in the widget when
* no items selected.
* \param text default text
* \see defaultText()
*/
void setDefaultText( const QString &text );
/** Returns currently checked items.
* \see setCheckedItems()
*/
QStringList checkedItems() const;
/** Returns the checked state of the item identified by index
* \param index item index
* \see setItemCheckState()
* \see toggleItemCheckState()
*/
Qt::CheckState itemCheckState( int index ) const;
/** Sets the item check state to state
* \param index item index
* \param state check state
* \see itemCheckState()
* \see toggleItemCheckState()
*/
void setItemCheckState( int index, Qt::CheckState state );
/** Toggles the item check state
* \param index item index
* \see itemCheckState()
* \see setItemCheckState()
*/
void toggleItemCheckState( int index );
/** Hides the list of items in the combobox if it is currently
* visible and resets the internal state.
*/
virtual void hidePopup() override;
/** Filters events to enable context menu
*/
virtual bool eventFilter( QObject *object, QEvent *event ) override;
signals:
/** This signal is emitted whenever the checked items list changed.
*/
void checkedItemsChanged( const QStringList &items );
public slots:
/** Set items which should be checked/selected.
* \param items items to select
* \see checkedItems()
*/
void setCheckedItems( const QStringList &items );
protected:
/** Handler for widget resizing
*/
virtual void resizeEvent( QResizeEvent *event ) override;
protected slots:
/** Display context menu which allows to select/deselect
* all items at once.
*/
void showContextMenu( const QPoint &pos );
/** Selects all items.
*/
void selectAllOptions();
/** Removes selection from all items.
*/
void deselectAllOptions();
private:
void updateCheckedItems();
void updateDisplayText();
QString mSeparator;
QString mDefaultText;
bool mSkipHide = false;
QMenu *mContextMenu = nullptr;
QAction *mSelectAllAction = nullptr;
QAction *mDeselectAllAction = nullptr;
};
#endif // QGSCHECKABLECOMBOBOX_H