Skip to content
Permalink
Browse files
add context menu with select/deselect all options
  • Loading branch information
alexbruy committed Apr 8, 2017
1 parent c8e4bca commit 9fbb31a31c6564d6567c71fdc8ba29093155d7d1
Showing with 101 additions and 0 deletions.
  1. +18 −0 python/gui/qgscheckablecombobox.sip
  2. +57 −0 src/gui/qgscheckablecombobox.cpp
  3. +26 −0 src/gui/qgscheckablecombobox.h
@@ -72,6 +72,10 @@ class QgsCheckableComboBox : QComboBox
*/
virtual void hidePopup();

/** Filters events to enable context menu
*/
virtual bool eventFilter( QObject *object, QEvent *event );

signals:

/** This signal is emitted whenever the checked items list changed.
@@ -90,4 +94,18 @@ class QgsCheckableComboBox : QComboBox
/** Handler for widget resizing
*/
virtual void resizeEvent( QResizeEvent *event );

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();
};
@@ -17,7 +17,10 @@

#include "qgscheckablecombobox.h"

#include <QEvent>
#include <QMouseEvent>
#include <QLineEdit>
#include <QPoint>
#include <QAbstractItemView>


@@ -81,6 +84,16 @@ QgsCheckableComboBox::QgsCheckableComboBox( QWidget *parent )
lineEdit->setReadOnly( true );
setLineEdit( lineEdit );

mContextMenu = new QMenu( this );
mSelectAllAction = mContextMenu->addAction( tr( "Select all" ) );
mDeselectAllAction = mContextMenu->addAction( tr( "Deselect all" ) );
connect( mSelectAllAction, &QAction::triggered, this, &QgsCheckableComboBox::selectAllOptions );
connect( mDeselectAllAction, &QAction::triggered, this, &QgsCheckableComboBox::deselectAllOptions );

view()->viewport()->installEventFilter( this );
view()->setContextMenuPolicy( Qt::CustomContextMenu );
connect( view(), &QAbstractItemView::customContextMenuRequested, this, &QgsCheckableComboBox::showContextMenu );

QgsCheckableItemModel *myModel = qobject_cast<QgsCheckableItemModel *>( model() );
connect( myModel, &QgsCheckableItemModel::itemCheckStateChanged, this, &QgsCheckableComboBox::updateCheckedItems );
connect( model(), &QStandardItemModel::rowsInserted, this, [ = ]( const QModelIndex &, int, int ) { updateCheckedItems(); } );
@@ -162,6 +175,49 @@ void QgsCheckableComboBox::hidePopup()
}
}

void QgsCheckableComboBox::showContextMenu( const QPoint &pos )
{
Q_UNUSED( pos );

mContextMenu->exec( QCursor::pos() );
}

void QgsCheckableComboBox::selectAllOptions()
{
blockSignals( true );
for ( int i; i < count(); i++ )
{
setItemData( i, Qt::Checked, Qt::CheckStateRole );
}
blockSignals( false );
updateCheckedItems();
}

void QgsCheckableComboBox::deselectAllOptions()
{
blockSignals( true );
for ( int i; i < count(); i++ )
{
setItemData( i, Qt::Unchecked, Qt::CheckStateRole );
}
blockSignals( false );
updateCheckedItems();
}

bool QgsCheckableComboBox::eventFilter( QObject *object, QEvent *event )
{
Q_UNUSED( object );

if ( event->type() == QEvent::MouseButtonRelease )
{
if ( static_cast<QMouseEvent *>( event )->button() == Qt::RightButton )
{
return true;
}
}
return false;
}

void QgsCheckableComboBox::setCheckedItems( const QStringList &items )
{
Q_FOREACH ( const QString &text, items )
@@ -202,3 +258,4 @@ void QgsCheckableComboBox::updateDisplayText()
text = fontMetrics.elidedText( text, Qt::ElideRight, rect.width() );
setEditText( text );
}

@@ -19,11 +19,14 @@
#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
@@ -178,6 +181,10 @@ class GUI_EXPORT QgsCheckableComboBox : public QComboBox
*/
virtual void hidePopup();

/** 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.
@@ -198,12 +205,31 @@ class GUI_EXPORT QgsCheckableComboBox : public QComboBox
*/
virtual void resizeEvent( QResizeEvent *event );

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;

QMenu *mContextMenu = nullptr;
QAction *mSelectAllAction = nullptr;
QAction *mDeselectAllAction = nullptr;
};

#endif // QGSCHECKABLECOMBOBOX_H

0 comments on commit 9fbb31a

Please sign in to comment.