From 02257426e5a784b3096f229111292b70831a15f0 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Tue, 28 Jan 2025 15:13:51 +1000 Subject: [PATCH] Remove dedicated "show editing toolbar" action from 3d windows Instead add right-click menu to main 3d window toolbar to toggle optional toolbar visibilty, just like we do for the main QGIS window. This avoids UI bloat in 3d map windows, and removes an action which will be rarely used. --- src/app/3d/qgs3dmapcanvaswidget.cpp | 44 +++++++++++++++++++++++++++-- src/app/3d/qgs3dmapcanvaswidget.h | 5 ++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/app/3d/qgs3dmapcanvaswidget.cpp b/src/app/3d/qgs3dmapcanvaswidget.cpp index 6e8b57ee3327..de90a987da83 100644 --- a/src/app/3d/qgs3dmapcanvaswidget.cpp +++ b/src/app/3d/qgs3dmapcanvaswidget.cpp @@ -69,6 +69,9 @@ Qgs3DMapCanvasWidget::Qgs3DMapCanvasWidget( const QString &name, bool isDocked ) { const QgsSettings setting; + mToolbarMenu = new QMenu( tr( "Toolbars" ), this ); + mToolbarMenu->setObjectName( QStringLiteral( "mToolbarMenu" ) ); + QToolBar *toolBar = new QToolBar( this ); toolBar->setIconSize( QgisApp::instance()->iconSize( isDocked ) ); @@ -79,6 +82,7 @@ Qgs3DMapCanvasWidget::Qgs3DMapCanvasWidget( const QString &name, bool isDocked ) // Editing toolbar mEditingToolBar = new QToolBar( this ); + mEditingToolBar->setWindowTitle( tr( "Editing Toolbar" ) ); mEditingToolBar->setVisible( false ); QAction *actionPointCloudChangeAttributeTool = mEditingToolBar->addAction( QIcon( QgsApplication::iconPath( "mActionSelectPolygon.svg" ) ), tr( "Change Point Cloud Attribute" ), this, &Qgs3DMapCanvasWidget::changePointCloudAttribute ); @@ -90,8 +94,6 @@ Qgs3DMapCanvasWidget::Qgs3DMapCanvasWidget( const QString &name, bool isDocked ) mSpinChangeAttributeValue = new QgsDoubleSpinBox(); mEditingToolBar->addWidget( new QLabel( tr( "Value" ) ) ); mEditingToolBar->addWidget( mSpinChangeAttributeValue ); - QAction *actionEditingToolbar = toolBar->addAction( QIcon( QgsApplication::iconPath( "mIconPointCloudLayer.svg" ) ), tr( "Show Editing Toolbar" ), this, [this] { mEditingToolBar->setVisible( !mEditingToolBar->isVisible() ); } ); - actionEditingToolbar->setCheckable( true ); connect( mCboChangeAttribute, qOverload( &QComboBox::currentIndexChanged ), this, [this]( int ) { onPointCloudChangeAttributeSettingsChanged(); } ); connect( mSpinChangeAttributeValue, qOverload( &QgsDoubleSpinBox::valueChanged ), this, [this]( double ) { onPointCloudChangeAttributeSettingsChanged(); } ); @@ -342,6 +344,24 @@ Qgs3DMapCanvasWidget::Qgs3DMapCanvasWidget( const QString &name, bool isDocked ) } ); updateLayerRelatedActions( QgisApp::instance()->activeLayer() ); + + QList toolbarMenuActions; + // Set action names so that they can be used in customization + for ( QToolBar *toolBar : { mEditingToolBar } ) + { + toolBar->toggleViewAction()->setObjectName( "mActionToggle" + toolBar->objectName().mid( 1 ) ); + toolbarMenuActions << toolBar->toggleViewAction(); + } + + // sort actions in toolbar menu + std::sort( toolbarMenuActions.begin(), toolbarMenuActions.end(), []( QAction *a, QAction *b ) { + return QString::localeAwareCompare( a->text(), b->text() ) < 0; + } ); + + mToolbarMenu->addActions( toolbarMenuActions ); + + toolBar->installEventFilter( this ); + mEditingToolBar->installEventFilter( this ); } Qgs3DMapCanvasWidget::~Qgs3DMapCanvasWidget() @@ -454,6 +474,26 @@ void Qgs3DMapCanvasWidget::updateLayerRelatedActions( QgsMapLayer *layer ) enableEditingTools( pcLayer->isEditable() ); } +bool Qgs3DMapCanvasWidget::eventFilter( QObject *watched, QEvent *event ) +{ + if ( qobject_cast< QToolBar * >( watched ) ) + { + if ( event->type() != QEvent::MouseButtonPress ) + return QObject::eventFilter( watched, event ); + + QMouseEvent *mouseEvent = dynamic_cast( event ); + if ( !mouseEvent ) + return QObject::eventFilter( watched, event ); + + if ( mouseEvent->button() != Qt::RightButton ) + return QObject::eventFilter( watched, event ); + + mToolbarMenu->exec( mouseEvent->globalPos() ); + return false; + } + return QObject::eventFilter( watched, event ); +} + void Qgs3DMapCanvasWidget::toggleNavigationWidget( bool visibility ) { mNavigationWidget->setVisible( visibility ); diff --git a/src/app/3d/qgs3dmapcanvaswidget.h b/src/app/3d/qgs3dmapcanvaswidget.h index 85011ebe03d3..f60659d50db7 100644 --- a/src/app/3d/qgs3dmapcanvaswidget.h +++ b/src/app/3d/qgs3dmapcanvaswidget.h @@ -78,6 +78,9 @@ class APP_EXPORT Qgs3DMapCanvasWidget : public QWidget void updateLayerRelatedActions( QgsMapLayer *layer ); + bool eventFilter( QObject *watched, QEvent *event ) override; + + private slots: void resetView(); void configure(); @@ -161,6 +164,8 @@ class APP_EXPORT Qgs3DMapCanvasWidget : public QWidget QToolBar *mEditingToolBar = nullptr; QComboBox *mCboChangeAttribute = nullptr; QgsDoubleSpinBox *mSpinChangeAttributeValue = nullptr; + + QMenu *mToolbarMenu = nullptr; }; #endif // QGS3DMAPCANVASWIDGET_H