Skip to content

Commit

Permalink
Fix missing actions menu when only internal actions are present
Browse files Browse the repository at this point in the history
Follow up bcf0e48
  • Loading branch information
nyalldawson committed Dec 20, 2022
1 parent fa2edbb commit f567046
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 10 deletions.
7 changes: 7 additions & 0 deletions python/gui/auto_generated/qgsactionmenu.sip.in
Expand Up @@ -99,6 +99,13 @@ Returns an expression context scope used to resolve underlying actions.
Returns menu actions

.. versionadded:: 3.12
%End

bool isEmpty() const;
%Docstring
Returns ``True`` if the menu has no valid actions.

.. versionadded:: 3.30
%End

signals:
Expand Down
21 changes: 16 additions & 5 deletions src/gui/qgsactionmenu.cpp
Expand Up @@ -113,6 +113,8 @@ void QgsActionMenu::reloadActions()
{
clear();

mVisibleActionCount = 0;

mActions = mLayer->actions()->actions( mActionScope );

const auto constMActions = mActions;
Expand Down Expand Up @@ -143,26 +145,30 @@ void QgsActionMenu::reloadActions()
}
connect( qAction, &QAction::triggered, this, &QgsActionMenu::triggerAction );
addAction( qAction );

mVisibleActionCount++;
}

const QList<QgsMapLayerAction *> mapLayerActions = QgsGui::mapLayerActionRegistry()->mapLayerActions( mLayer, QgsMapLayerAction::SingleFeature );

if ( !mapLayerActions.isEmpty() )
{
//add a separator between user defined and standard actions
addSeparator();
if ( mVisibleActionCount > 0 )
addSeparator();

for ( int i = 0; i < mapLayerActions.size(); ++i )
{
QgsMapLayerAction *qaction = mapLayerActions.at( i );
QgsMapLayerAction *mapLayerAction = mapLayerActions.at( i );

if ( qaction->isEnabledOnlyWhenEditable() && ( mMode == QgsAttributeEditorContext::AddFeatureMode || mMode == QgsAttributeEditorContext::IdentifyMode ) )
if ( mapLayerAction->isEnabledOnlyWhenEditable() && ( mMode == QgsAttributeEditorContext::AddFeatureMode || mMode == QgsAttributeEditorContext::IdentifyMode ) )
continue;

QAction *qAction = new QAction( qaction->icon(), qaction->text(), this );
qAction->setData( QVariant::fromValue<ActionData>( ActionData( qaction, mFeatureId, mLayer ) ) );
QAction *qAction = new QAction( mapLayerAction->icon(), mapLayerAction->text(), this );
qAction->setData( QVariant::fromValue<ActionData>( ActionData( mapLayerAction, mFeatureId, mLayer ) ) );
addAction( qAction );
connect( qAction, &QAction::triggered, this, &QgsActionMenu::triggerAction );
mVisibleActionCount++;
}
}

Expand Down Expand Up @@ -210,3 +216,8 @@ QList<QgsAction> QgsActionMenu::menuActions()
{
return mActions;
}

bool QgsActionMenu::isEmpty() const
{
return mVisibleActionCount == 0;
}
10 changes: 9 additions & 1 deletion src/gui/qgsactionmenu.h
Expand Up @@ -119,6 +119,13 @@ class GUI_EXPORT QgsActionMenu : public QMenu
*/
QList<QgsAction> menuActions();

/**
* Returns TRUE if the menu has no valid actions.
*
* \since QGIS 3.30
*/
bool isEmpty() const;

signals:
void reinit();

Expand All @@ -132,12 +139,13 @@ class GUI_EXPORT QgsActionMenu : public QMenu
QgsFeature feature();

QgsVectorLayer *mLayer = nullptr;
int mVisibleActionCount = 0;
QList<QgsAction> mActions;
QgsFeature mFeature;
QgsFeatureId mFeatureId;
QString mActionScope;
QgsExpressionContextScope mExpressionContextScope;
QgsAttributeEditorContext::Mode mMode;
QgsAttributeEditorContext::Mode mMode = QgsAttributeEditorContext::SingleEditMode;
};


Expand Down
21 changes: 17 additions & 4 deletions src/gui/qgsattributedialog.cpp
Expand Up @@ -125,11 +125,11 @@ void QgsAttributeDialog::init( QgsVectorLayer *layer, QgsFeature *feature, const
connect( layer, &QObject::destroyed, this, &QWidget::close );

mMenu = new QgsActionMenu( layer, mAttributeForm->feature(), QStringLiteral( "Feature" ), this );
if ( !mMenu->menuActions().isEmpty() )
if ( !mMenu->isEmpty() )
{
QMenuBar *menuBar = new QMenuBar( this );
menuBar->addMenu( mMenu );
layout()->setMenuBar( menuBar );
mMenuBar = new QMenuBar( this );
mMenuBar->addMenu( mMenu );
layout()->setMenuBar( mMenuBar );
}

restoreGeometry();
Expand All @@ -140,6 +140,19 @@ void QgsAttributeDialog::setMode( QgsAttributeEditorContext::Mode mode )
{
mAttributeForm->setMode( mode );
mMenu->setMode( mode );

if ( !mMenu->isEmpty() && !mMenuBar )
{
mMenuBar = new QMenuBar( this );
mMenuBar->addMenu( mMenu );
layout()->setMenuBar( mMenuBar );
}
else if ( mMenu->isEmpty() && mMenuBar )
{
layout()->setMenuBar( nullptr );
delete mMenuBar;
mMenuBar = nullptr;
}
}

bool QgsAttributeDialog::event( QEvent *e )
Expand Down
1 change: 1 addition & 0 deletions src/gui/qgsattributedialog.h
Expand Up @@ -129,6 +129,7 @@ class GUI_EXPORT QgsAttributeDialog : public QDialog
bool mEditable;

QgsActionMenu *mMenu;
QMenuBar *mMenuBar = nullptr;

static int sFormCounter;

Expand Down

0 comments on commit f567046

Please sign in to comment.