QGIS API Documentation  3.0.2-Girona (307d082)
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.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 
95 class QgsCheckBoxDelegate : public QStyledItemDelegate
96 {
97  Q_OBJECT
98 
99  public:
100 
105  QgsCheckBoxDelegate( QObject *parent = nullptr );
106 
114  void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
115 };
116 #endif
117 
125 class GUI_EXPORT QgsCheckableComboBox : public QComboBox
126 {
127  Q_OBJECT
128 
129  Q_PROPERTY( QString separator READ separator WRITE setSeparator )
130  Q_PROPERTY( QString defaultText READ defaultText WRITE setDefaultText )
131  Q_PROPERTY( QStringList checkedItems READ checkedItems WRITE setCheckedItems )
132 
133  public:
134 
138  QgsCheckableComboBox( QWidget *parent SIP_TRANSFERTHIS = nullptr );
139 
144  QString separator() const;
145 
151  void setSeparator( const QString &separator );
152 
158  QString defaultText() const;
159 
166  void setDefaultText( const QString &text );
167 
172  QStringList checkedItems() const;
173 
180  Qt::CheckState itemCheckState( int index ) const;
181 
189  void setItemCheckState( int index, Qt::CheckState state );
190 
197  void toggleItemCheckState( int index );
198 
203  void hidePopup() override;
204 
208  bool eventFilter( QObject *object, QEvent *event ) override;
209 
210  signals:
211 
215  void checkedItemsChanged( const QStringList &items );
216 
217  public slots:
218 
224  void setCheckedItems( const QStringList &items );
225 
226  protected:
227 
231  void resizeEvent( QResizeEvent *event ) override;
232 
233  protected slots:
234 
239  void showContextMenu( QPoint pos );
240 
244  void selectAllOptions();
245 
249  void deselectAllOptions();
250 
251  private:
252  void updateCheckedItems();
253  void updateDisplayText();
254 
255  QString mSeparator;
256  QString mDefaultText;
257 
258  bool mSkipHide = false;
259 
260  QMenu *mContextMenu = nullptr;
261  QAction *mSelectAllAction = nullptr;
262  QAction *mDeselectAllAction = nullptr;
263 };
264 
265 #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.