Skip to content

Commit 79e5240

Browse files
committed
disable or do not display maplayeractions
in some cases
1 parent a7ca742 commit 79e5240

20 files changed

+274
-75
lines changed

src/app/qgisapp.cpp

+24-4
Original file line numberDiff line numberDiff line change
@@ -6452,8 +6452,12 @@ void QgisApp::refreshFeatureActions()
64526452
QList<QgsAction> actions = vlayer->actions()->actions( QStringLiteral( "Canvas" ) );
64536453
Q_FOREACH ( const QgsAction &action, actions )
64546454
{
6455+
if( vlayer->readOnly() && action.isEnabledOnlyWhenEditable() )
6456+
continue;
6457+
64556458
QString actionTitle = !action.shortTitle().isEmpty() ? action.shortTitle() : action.icon().isNull() ? action.name() : QStringLiteral( "" );
64566459
QAction *qAction = new QAction( action.icon(), actionTitle, mFeatureActionMenu );
6460+
qAction->setEnabled( !action.isEnabledOnlyWhenEditable() || vlayer->isEditable() );
64576461
qAction->setData( QVariant::fromValue<QgsAction>( action ) );
64586462
mFeatureActionMenu->addAction( qAction );
64596463

@@ -6473,6 +6477,10 @@ void QgisApp::refreshFeatureActions()
64736477

64746478
for ( int i = 0; i < registeredActions.size(); i++ )
64756479
{
6480+
if( vlayer->readOnly() && registeredActions.at( i )->isEnabledOnlyWhenEditable() )
6481+
continue;
6482+
6483+
registeredActions.at( i )->setEnabled( !registeredActions.at( i )->isEnabledOnlyWhenEditable() || vlayer->isEditable() );
64766484
mFeatureActionMenu->addAction( registeredActions.at( i ) );
64776485
if ( registeredActions.at( i ) == QgsGui::mapLayerActionRegistry()->defaultActionForLayer( vlayer ) )
64786486
{
@@ -7460,7 +7468,7 @@ void QgisApp::setupDuplicateFeaturesAction()
74607468
{
74617469
mDuplicateFeatureAction.reset( new QgsMapLayerAction( tr( "Duplicate feature" ),
74627470
nullptr, QgsMapLayerAction::SingleFeature,
7463-
QgsApplication::getThemeIcon( QStringLiteral( "/mActionDuplicateFeature.svg" ) ) ) );
7471+
QgsApplication::getThemeIcon( QStringLiteral( "/mActionDuplicateFeature.svg" ) ), true ) );
74647472

74657473
QgsGui::mapLayerActionRegistry()->addMapLayerAction( mDuplicateFeatureAction.get() );
74667474
connect( mDuplicateFeatureAction.get(), &QgsMapLayerAction::triggeredForFeature, this, [this]( QgsMapLayer * layer, const QgsFeature & feat )
@@ -7471,7 +7479,7 @@ void QgisApp::setupDuplicateFeaturesAction()
74717479

74727480
mDuplicateFeatureDigitizeAction.reset( new QgsMapLayerAction( tr( "Duplicate feature and digitize" ),
74737481
nullptr, QgsMapLayerAction::SingleFeature,
7474-
QgsApplication::getThemeIcon( QStringLiteral( "/mActionDuplicateFeatureDigitized.svg" ) ) ) );
7482+
QgsApplication::getThemeIcon( QStringLiteral( "/mActionDuplicateFeatureDigitized.svg" ) ), true ) );
74757483

74767484
QgsGui::mapLayerActionRegistry()->addMapLayerAction( mDuplicateFeatureDigitizeAction.get() );
74777485
connect( mDuplicateFeatureDigitizeAction.get(), &QgsMapLayerAction::triggeredForFeature, this, [this]( QgsMapLayer * layer, const QgsFeature & feat )
@@ -13164,7 +13172,13 @@ QgsFeature QgisApp::duplicateFeatures( QgsMapLayer *mlayer, const QgsFeature &fe
1316413172

1316513173
QgsVectorLayer *layer = qobject_cast<QgsVectorLayer *>( mlayer );
1316613174

13167-
layer->startEditing();
13175+
if( !layer->isEditable() )
13176+
{
13177+
//should never happen because the action should be disabled
13178+
QString msg = tr( "Cannot duplicate feature in not editable mode on layer %1" ).arg( layer->name() );
13179+
messageBar()->pushMessage( msg, Qgis::Warning, 3 );
13180+
return QgsFeature();
13181+
}
1316813182

1316913183
QgsFeatureList featureList;
1317013184

@@ -13212,7 +13226,13 @@ QgsFeature QgisApp::duplicateFeatureDigitized( QgsMapLayer *mlayer, const QgsFea
1321213226

1321313227
QgsVectorLayer *layer = qobject_cast<QgsVectorLayer *>( mlayer );
1321413228

13215-
layer->startEditing();
13229+
if( !layer->isEditable() )
13230+
{
13231+
//should never happen because the action should be disabled
13232+
QString msg = tr( "Cannot duplicate feature in not editable mode on layer %1" ).arg( layer->name() );
13233+
messageBar()->pushMessage( msg, Qgis::Warning, 3 );
13234+
return QgsFeature();
13235+
}
1321613236

1321713237
QgsMapToolDigitizeFeature *digitizeFeature = new QgsMapToolDigitizeFeature( mMapCanvas, mlayer, QgsMapToolCapture::CaptureNone );
1321813238

src/app/qgsattributeactiondialog.cpp

+14-5
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,19 @@ void QgsAttributeActionDialog::insertRow( int row, const QgsAction &action )
148148
// Notification message
149149
mAttributeActionTable->setItem( row, NotificationMessage, new QTableWidgetItem( action.notificationMessage() ) );
150150

151+
// EnabledOnlyWhenEditable
152+
item = new QTableWidgetItem();
153+
item->setFlags( item->flags() & ~( Qt::ItemIsEditable ) );
154+
item->setCheckState( action.isEnabledOnlyWhenEditable() ? Qt::Checked : Qt::Unchecked );
155+
mAttributeActionTable->setItem( row, EnabledOnlyWhenEditable, item );
156+
151157
updateButtons();
152158
}
153159

154-
void QgsAttributeActionDialog::insertRow( int row, QgsAction::ActionType type, const QString &name, const QString &actionText, const QString &iconPath, bool capture, const QString &shortTitle, const QSet<QString> &actionScopes, const QString &notificationMessage )
160+
void QgsAttributeActionDialog::insertRow( int row, QgsAction::ActionType type, const QString &name, const QString &actionText, const QString &iconPath, bool capture, const QString &shortTitle, const QSet<QString> &actionScopes, const QString &notificationMessage, bool isEnabledOnlyWhenEditable )
155161
{
156162
if ( uniqueName( name ) == name )
157-
insertRow( row, QgsAction( type, name, actionText, iconPath, capture, shortTitle, actionScopes, notificationMessage ) );
163+
insertRow( row, QgsAction( type, name, actionText, iconPath, capture, shortTitle, actionScopes, notificationMessage, isEnabledOnlyWhenEditable ) );
158164
}
159165

160166
void QgsAttributeActionDialog::moveUp()
@@ -223,7 +229,8 @@ QgsAction QgsAttributeActionDialog::rowToAction( int row ) const
223229
mAttributeActionTable->item( row, Capture )->checkState() == Qt::Checked,
224230
mAttributeActionTable->item( row, ShortTitle )->text(),
225231
mAttributeActionTable->item( row, ActionScopes )->data( Qt::UserRole ).value<QSet<QString>>(),
226-
mAttributeActionTable->item( row, NotificationMessage )->text()
232+
mAttributeActionTable->item( row, NotificationMessage )->text(),
233+
mAttributeActionTable->item( row, EnabledOnlyWhenEditable )->checkState() == Qt::Checked
227234
);
228235
return action;
229236
}
@@ -278,7 +285,7 @@ void QgsAttributeActionDialog::insert()
278285
{
279286
QString name = uniqueName( dlg.description() );
280287

281-
insertRow( pos, dlg.type(), name, dlg.actionText(), dlg.iconPath(), dlg.capture(), dlg.shortTitle(), dlg.actionScopes(), dlg.notificationMessage() );
288+
insertRow( pos, dlg.type(), name, dlg.actionText(), dlg.iconPath(), dlg.capture(), dlg.shortTitle(), dlg.actionScopes(), dlg.notificationMessage(), dlg.isEnabledOnlyWhenEditable() );
282289
}
283290
}
284291

@@ -313,7 +320,7 @@ void QgsAttributeActionDialog::addDefaultActions()
313320
insertRow( pos++, QgsAction::OpenUrl, tr( "Open file" ), QStringLiteral( "[% \"PATH\" %]" ), QLatin1String( "" ), false, tr( "Open file" ), QSet<QString>() << QStringLiteral( "Feature" ) << QStringLiteral( "Canvas" ), QString() );
314321
insertRow( pos++, QgsAction::OpenUrl, tr( "Search on web based on attribute's value" ), QStringLiteral( "http://www.google.com/search?q=[% \"ATTRIBUTE\" %]" ), QLatin1String( "" ), false, tr( "Search Web" ), QSet<QString>() << QStringLiteral( "Field" ), QString() );
315322
insertRow( pos++, QgsAction::GenericPython, tr( "List feature ids" ), QStringLiteral( "from qgis.PyQt import QtWidgets\n\nlayer = QgsProject.instance().mapLayer('[% @layer_id %]')\nif layer.selectedFeatureCount():\n ids = layer.selectedFeatureIds()\nelse:\n ids = [f.id() for f in layer.getFeatures()]\n\nQtWidgets.QMessageBox.information(None, \"Feature ids\", ', '.join([str(id) for id in ids]))" ), QLatin1String( "" ), false, tr( "List feature ids" ), QSet<QString>() << QStringLiteral( "Layer" ), QString() );
316-
insertRow( pos++, QgsAction::GenericPython, tr( "Duplicate selected features" ), QStringLiteral( "project = QgsProject.instance()\nlayer = QgsProject.instance().mapLayer('[% @layer_id %]')\nlayer.startEditing()\nfeatures=[]\nif len('[% $id %]')>0:\n features.append( layer.getFeature( [% $id %] ) )\nelse:\n for x in layer.selectedFeatures():\n features.append( x )\nfeature_count=0\nchildren_info=''\nfeatureids=[]\nfor f in features:\n result=QgsVectorLayerUtils.duplicateFeature(layer, f, project, 0 )\n featureids.append( result[0].id() )\n feature_count+=1\n for ch_layer in result[1].layers():\n children_info+='{number_of_children} children on layer {children_layer}\\n'.format( number_of_children=str( len( result[1].duplicatedFeatures(ch_layer) ) ), children_layer=ch_layer.name() )\n ch_layer.selectByIds( result[1].duplicatedFeatures(ch_layer) )\nlayer.selectByIds( featureids )\nqgis.utils.iface.messageBar().pushMessage( '{number_of_features} features on layer {layer} duplicated with\\n{children_info}'.format( number_of_features=str( feature_count ), layer=layer.name(), children_info=children_info ) )" ), QLatin1String( "" ), false, tr( "Duplicate selected" ), QSet<QString>() << QStringLiteral( "Layer" ), QString() );
323+
insertRow( pos++, QgsAction::GenericPython, tr( "Duplicate selected features" ), QStringLiteral( "project = QgsProject.instance()\nlayer = QgsProject.instance().mapLayer('[% @layer_id %]')\nif not layer.isEditable():\n qgis.utils.iface.messageBar().pushMessage( 'Cannot duplicate feature in not editable mode on layer {layer}'.format( layer=layer.name() ) )\nelse:\n features=[]\n if len('[% $id %]')>0:\n features.append( layer.getFeature( [% $id %] ) )\n else:\n for x in layer.selectedFeatures():\n features.append( x )\n feature_count=0\n children_info=''\n featureids=[]\n for f in features:\n result=QgsVectorLayerUtils.duplicateFeature(layer, f, project, 0 )\n featureids.append( result[0].id() )\n feature_count+=1\n for ch_layer in result[1].layers():\n children_info+='{number_of_children} children on layer {children_layer}\\n'.format( number_of_children=str( len( result[1].duplicatedFeatures(ch_layer) ) ), children_layer=ch_layer.name() )\n ch_layer.selectByIds( result[1].duplicatedFeatures(ch_layer) )\n layer.selectByIds( featureids )\n qgis.utils.iface.messageBar().pushMessage( '{number_of_features} features on layer {layer} duplicated with\\n{children_info}'.format( number_of_features=str( feature_count ), layer=layer.name(), children_info=children_info ) )" ), QLatin1String( "" ), false, tr( "Duplicate selected" ), QSet<QString>() << QStringLiteral( "Layer" ), QString(), true );
317324

318325
}
319326

@@ -330,6 +337,7 @@ void QgsAttributeActionDialog::itemDoubleClicked( QTableWidgetItem *item )
330337
mAttributeActionTable->item( row, Capture )->checkState() == Qt::Checked,
331338
mAttributeActionTable->item( row, ActionScopes )->data( Qt::UserRole ).value<QSet<QString>>(),
332339
mAttributeActionTable->item( row, NotificationMessage )->text(),
340+
mAttributeActionTable->item( row, EnabledOnlyWhenEditable )->checkState() == Qt::Checked,
333341
mLayer
334342
);
335343

@@ -344,6 +352,7 @@ void QgsAttributeActionDialog::itemDoubleClicked( QTableWidgetItem *item )
344352
mAttributeActionTable->item( row, ActionText )->setText( actionProperties.actionText() );
345353
mAttributeActionTable->item( row, Capture )->setCheckState( actionProperties.capture() ? Qt::Checked : Qt::Unchecked );
346354
mAttributeActionTable->item( row, NotificationMessage )->setText( actionProperties.notificationMessage() );
355+
mAttributeActionTable->item( row, EnabledOnlyWhenEditable )->setCheckState( actionProperties.isEnabledOnlyWhenEditable() ? Qt::Checked : Qt::Unchecked );
347356

348357
QTableWidgetItem *item = mAttributeActionTable->item( row, ActionScopes );
349358
QStringList actionScopes = actionProperties.actionScopes().toList();

src/app/qgsattributeactiondialog.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ class APP_EXPORT QgsAttributeActionDialog: public QWidget, private Ui::QgsAttrib
4444
ActionText,
4545
Capture,
4646
ActionScopes,
47-
NotificationMessage
47+
NotificationMessage,
48+
EnabledOnlyWhenEditable
4849
};
4950

5051
public:
@@ -70,7 +71,7 @@ class APP_EXPORT QgsAttributeActionDialog: public QWidget, private Ui::QgsAttrib
7071

7172
private:
7273
void insertRow( int row, const QgsAction &action );
73-
void insertRow( int row, QgsAction::ActionType type, const QString &name, const QString &actionText, const QString &iconPath, bool capture, const QString &shortTitle, const QSet<QString> &actionScopes, const QString &notificationMessage );
74+
void insertRow( int row, QgsAction::ActionType type, const QString &name, const QString &actionText, const QString &iconPath, bool capture, const QString &shortTitle, const QSet<QString> &actionScopes, const QString &notificationMessage, const bool isEnabledOnlyWhenEditable=false );
7475
void swapRows( int row1, int row2 );
7576
QgsAction rowToAction( int row ) const;
7677

src/app/qgsattributeactionpropertiesdialog.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#include <QFileDialog>
3232
#include <QImageWriter>
3333

34-
QgsAttributeActionPropertiesDialog::QgsAttributeActionPropertiesDialog( QgsAction::ActionType type, const QString &description, const QString &shortTitle, const QString &iconPath, const QString &actionText, bool capture, const QSet<QString> &actionScopes, const QString &notificationMessage, QgsVectorLayer *layer, QWidget *parent )
34+
QgsAttributeActionPropertiesDialog::QgsAttributeActionPropertiesDialog( QgsAction::ActionType type, const QString &description, const QString &shortTitle, const QString &iconPath, const QString &actionText, bool capture, const QSet<QString> &actionScopes, const QString &notificationMessage, bool isEnabledOnlyWhenEditable, QgsVectorLayer *layer, QWidget *parent )
3535
: QDialog( parent )
3636
, mLayer( layer )
3737
{
@@ -45,6 +45,7 @@ QgsAttributeActionPropertiesDialog::QgsAttributeActionPropertiesDialog( QgsActio
4545
mActionText->setText( actionText );
4646
mCaptureOutput->setChecked( capture );
4747
mNotificationMessage->setText( notificationMessage );
48+
mIsEnabledOnlyWhenEditable->setChecked( isEnabledOnlyWhenEditable );
4849

4950
init( actionScopes );
5051
}
@@ -107,6 +108,10 @@ QString QgsAttributeActionPropertiesDialog::notificationMessage() const
107108
return mNotificationMessage->text();
108109
}
109110

111+
bool QgsAttributeActionPropertiesDialog::isEnabledOnlyWhenEditable() const
112+
{
113+
return mIsEnabledOnlyWhenEditable->isChecked();
114+
}
110115

111116
bool QgsAttributeActionPropertiesDialog::capture() const
112117
{

src/app/qgsattributeactionpropertiesdialog.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class QgsAttributeActionPropertiesDialog: public QDialog, private Ui::QgsAttribu
2828
Q_OBJECT
2929

3030
public:
31-
QgsAttributeActionPropertiesDialog( QgsAction::ActionType type, const QString &description, const QString &shortTitle, const QString &iconPath, const QString &actionText, bool capture, const QSet<QString> &actionScopes, const QString &notificationMessage, QgsVectorLayer *layer, QWidget *parent = nullptr );
31+
QgsAttributeActionPropertiesDialog( QgsAction::ActionType type, const QString &description, const QString &shortTitle, const QString &iconPath, const QString &actionText, bool capture, const QSet<QString> &actionScopes, const QString &notificationMessage, bool isEnabledOnlyWhenEditable, QgsVectorLayer *layer, QWidget *parent = nullptr );
3232

3333
QgsAttributeActionPropertiesDialog( QgsVectorLayer *layer, QWidget *parent = nullptr );
3434

@@ -46,6 +46,8 @@ class QgsAttributeActionPropertiesDialog: public QDialog, private Ui::QgsAttribu
4646

4747
QString notificationMessage() const;
4848

49+
bool isEnabledOnlyWhenEditable() const;
50+
4951
bool capture() const;
5052

5153
QgsExpressionContext createExpressionContext() const override;

src/app/qgsattributetabledialog.cpp

+24-19
Original file line numberDiff line numberDiff line change
@@ -333,25 +333,6 @@ QgsAttributeTableDialog::QgsAttributeTableDialog( QgsVectorLayer *layer, QgsAttr
333333
mActionSearchForm->setToolTip( tr( "Search is not supported when using custom UI forms" ) );
334334
}
335335

336-
QList<QgsAction> actions = mLayer->actions()->actions( QStringLiteral( "Layer" ) );
337-
338-
if ( actions.isEmpty() )
339-
{
340-
mActionFeatureActions->setVisible( false );
341-
}
342-
else
343-
{
344-
QMenu *actionMenu = new QMenu();
345-
Q_FOREACH ( const QgsAction &action, actions )
346-
{
347-
QAction *qAction = actionMenu->addAction( action.icon(), action.shortTitle() );
348-
qAction->setToolTip( action.name() );
349-
qAction->setData( QVariant::fromValue<QgsAction>( action ) );
350-
connect( qAction, &QAction::triggered, this, &QgsAttributeTableDialog::layerActionTriggered );
351-
}
352-
mActionFeatureActions->setMenu( actionMenu );
353-
}
354-
355336
editingToggled();
356337
// Close and delete if the layer has been destroyed
357338
connect( mLayer, &QObject::destroyed, this, &QWidget::close );
@@ -850,6 +831,30 @@ void QgsAttributeTableDialog::editingToggled()
850831
}
851832
// not necessary to set table read only if layer is not editable
852833
// because model always reflects actual state when returning item flags
834+
835+
QList<QgsAction> actions = mLayer->actions()->actions( QStringLiteral( "Layer" ) );
836+
837+
if ( actions.isEmpty() )
838+
{
839+
mActionFeatureActions->setVisible( false );
840+
}
841+
else
842+
{
843+
QMenu *actionMenu = new QMenu();
844+
Q_FOREACH ( const QgsAction &action, actions )
845+
{
846+
if( mLayer->readOnly() && action.isEnabledOnlyWhenEditable() )
847+
continue;
848+
849+
QAction *qAction = actionMenu->addAction( action.icon(), action.shortTitle() );
850+
qAction->setEnabled( !action.isEnabledOnlyWhenEditable() || mLayer->isEditable() );
851+
qAction->setToolTip( action.name() );
852+
qAction->setData( QVariant::fromValue<QgsAction>( action ) );
853+
connect( qAction, &QAction::triggered, this, &QgsAttributeTableDialog::layerActionTriggered );
854+
}
855+
mActionFeatureActions->setMenu( actionMenu );
856+
}
857+
853858
}
854859

855860
void QgsAttributeTableDialog::mActionAddAttribute_triggered()

src/app/qgsfeatureaction.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,12 @@ QgsAttributeDialog *QgsFeatureAction::newDialog( bool cloneFeature )
8282
if ( !action.runable() )
8383
continue;
8484

85+
if( mLayer->readOnly() && action.isEnabledOnlyWhenEditable() )
86+
continue;
87+
8588
QgsFeature &feat = const_cast<QgsFeature &>( *dialog->feature() );
8689
QgsFeatureAction *a = new QgsFeatureAction( action.name(), feat, mLayer, action.id(), -1, dialog );
90+
a->setEnabled( !action.isEnabledOnlyWhenEditable() || mLayer->isEditable() );
8791
dialog->addAction( a );
8892
connect( a, &QAction::triggered, a, &QgsFeatureAction::execute );
8993

0 commit comments

Comments
 (0)