Skip to content

Commit

Permalink
add context menu with select/deselect all options
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Apr 8, 2017
1 parent c8e4bca commit 9fbb31a
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
18 changes: 18 additions & 0 deletions python/gui/qgscheckablecombobox.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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();
};
57 changes: 57 additions & 0 deletions src/gui/qgscheckablecombobox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

#include "qgscheckablecombobox.h"

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


Expand Down Expand Up @@ -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(); } );
Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -202,3 +258,4 @@ void QgsCheckableComboBox::updateDisplayText()
text = fontMetrics.elidedText( text, Qt::ElideRight, rect.width() );
setEditText( text );
}

26 changes: 26 additions & 0 deletions src/gui/qgscheckablecombobox.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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.