mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-15 00:04:00 -04:00
add context menu with select/deselect all options
This commit is contained in:
parent
c8e4bca5d7
commit
9fbb31a31c
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user