Skip to content

Commit

Permalink
disable or do not display maplayeractions
Browse files Browse the repository at this point in the history
in some cases
  • Loading branch information
signedav committed Feb 23, 2018
1 parent a7ca742 commit 79e5240
Show file tree
Hide file tree
Showing 20 changed files with 274 additions and 75 deletions.
28 changes: 24 additions & 4 deletions src/app/qgisapp.cpp
Expand Up @@ -6452,8 +6452,12 @@ void QgisApp::refreshFeatureActions()
QList<QgsAction> actions = vlayer->actions()->actions( QStringLiteral( "Canvas" ) );
Q_FOREACH ( const QgsAction &action, actions )
{
if( vlayer->readOnly() && action.isEnabledOnlyWhenEditable() )
continue;

QString actionTitle = !action.shortTitle().isEmpty() ? action.shortTitle() : action.icon().isNull() ? action.name() : QStringLiteral( "" );
QAction *qAction = new QAction( action.icon(), actionTitle, mFeatureActionMenu );
qAction->setEnabled( !action.isEnabledOnlyWhenEditable() || vlayer->isEditable() );
qAction->setData( QVariant::fromValue<QgsAction>( action ) );
mFeatureActionMenu->addAction( qAction );

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

for ( int i = 0; i < registeredActions.size(); i++ )
{
if( vlayer->readOnly() && registeredActions.at( i )->isEnabledOnlyWhenEditable() )
continue;

registeredActions.at( i )->setEnabled( !registeredActions.at( i )->isEnabledOnlyWhenEditable() || vlayer->isEditable() );
mFeatureActionMenu->addAction( registeredActions.at( i ) );
if ( registeredActions.at( i ) == QgsGui::mapLayerActionRegistry()->defaultActionForLayer( vlayer ) )
{
Expand Down Expand Up @@ -7460,7 +7468,7 @@ void QgisApp::setupDuplicateFeaturesAction()
{
mDuplicateFeatureAction.reset( new QgsMapLayerAction( tr( "Duplicate feature" ),
nullptr, QgsMapLayerAction::SingleFeature,
QgsApplication::getThemeIcon( QStringLiteral( "/mActionDuplicateFeature.svg" ) ) ) );
QgsApplication::getThemeIcon( QStringLiteral( "/mActionDuplicateFeature.svg" ) ), true ) );

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

mDuplicateFeatureDigitizeAction.reset( new QgsMapLayerAction( tr( "Duplicate feature and digitize" ),
nullptr, QgsMapLayerAction::SingleFeature,
QgsApplication::getThemeIcon( QStringLiteral( "/mActionDuplicateFeatureDigitized.svg" ) ) ) );
QgsApplication::getThemeIcon( QStringLiteral( "/mActionDuplicateFeatureDigitized.svg" ) ), true ) );

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

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

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

QgsFeatureList featureList;

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

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

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

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

Expand Down
19 changes: 14 additions & 5 deletions src/app/qgsattributeactiondialog.cpp
Expand Up @@ -148,13 +148,19 @@ void QgsAttributeActionDialog::insertRow( int row, const QgsAction &action )
// Notification message
mAttributeActionTable->setItem( row, NotificationMessage, new QTableWidgetItem( action.notificationMessage() ) );

// EnabledOnlyWhenEditable
item = new QTableWidgetItem();
item->setFlags( item->flags() & ~( Qt::ItemIsEditable ) );
item->setCheckState( action.isEnabledOnlyWhenEditable() ? Qt::Checked : Qt::Unchecked );
mAttributeActionTable->setItem( row, EnabledOnlyWhenEditable, item );

updateButtons();
}

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 )
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 )
{
if ( uniqueName( name ) == name )
insertRow( row, QgsAction( type, name, actionText, iconPath, capture, shortTitle, actionScopes, notificationMessage ) );
insertRow( row, QgsAction( type, name, actionText, iconPath, capture, shortTitle, actionScopes, notificationMessage, isEnabledOnlyWhenEditable ) );
}

void QgsAttributeActionDialog::moveUp()
Expand Down Expand Up @@ -223,7 +229,8 @@ QgsAction QgsAttributeActionDialog::rowToAction( int row ) const
mAttributeActionTable->item( row, Capture )->checkState() == Qt::Checked,
mAttributeActionTable->item( row, ShortTitle )->text(),
mAttributeActionTable->item( row, ActionScopes )->data( Qt::UserRole ).value<QSet<QString>>(),
mAttributeActionTable->item( row, NotificationMessage )->text()
mAttributeActionTable->item( row, NotificationMessage )->text(),
mAttributeActionTable->item( row, EnabledOnlyWhenEditable )->checkState() == Qt::Checked
);
return action;
}
Expand Down Expand Up @@ -278,7 +285,7 @@ void QgsAttributeActionDialog::insert()
{
QString name = uniqueName( dlg.description() );

insertRow( pos, dlg.type(), name, dlg.actionText(), dlg.iconPath(), dlg.capture(), dlg.shortTitle(), dlg.actionScopes(), dlg.notificationMessage() );
insertRow( pos, dlg.type(), name, dlg.actionText(), dlg.iconPath(), dlg.capture(), dlg.shortTitle(), dlg.actionScopes(), dlg.notificationMessage(), dlg.isEnabledOnlyWhenEditable() );
}
}

Expand Down Expand Up @@ -313,7 +320,7 @@ void QgsAttributeActionDialog::addDefaultActions()
insertRow( pos++, QgsAction::OpenUrl, tr( "Open file" ), QStringLiteral( "[% \"PATH\" %]" ), QLatin1String( "" ), false, tr( "Open file" ), QSet<QString>() << QStringLiteral( "Feature" ) << QStringLiteral( "Canvas" ), QString() );
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() );
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() );
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() );
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 );

}

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

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

QTableWidgetItem *item = mAttributeActionTable->item( row, ActionScopes );
QStringList actionScopes = actionProperties.actionScopes().toList();
Expand Down
5 changes: 3 additions & 2 deletions src/app/qgsattributeactiondialog.h
Expand Up @@ -44,7 +44,8 @@ class APP_EXPORT QgsAttributeActionDialog: public QWidget, private Ui::QgsAttrib
ActionText,
Capture,
ActionScopes,
NotificationMessage
NotificationMessage,
EnabledOnlyWhenEditable
};

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

private:
void insertRow( int row, const QgsAction &action );
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 );
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 );
void swapRows( int row1, int row2 );
QgsAction rowToAction( int row ) const;

Expand Down
7 changes: 6 additions & 1 deletion src/app/qgsattributeactionpropertiesdialog.cpp
Expand Up @@ -31,7 +31,7 @@
#include <QFileDialog>
#include <QImageWriter>

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 )
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 )
: QDialog( parent )
, mLayer( layer )
{
Expand All @@ -45,6 +45,7 @@ QgsAttributeActionPropertiesDialog::QgsAttributeActionPropertiesDialog( QgsActio
mActionText->setText( actionText );
mCaptureOutput->setChecked( capture );
mNotificationMessage->setText( notificationMessage );
mIsEnabledOnlyWhenEditable->setChecked( isEnabledOnlyWhenEditable );

init( actionScopes );
}
Expand Down Expand Up @@ -107,6 +108,10 @@ QString QgsAttributeActionPropertiesDialog::notificationMessage() const
return mNotificationMessage->text();
}

bool QgsAttributeActionPropertiesDialog::isEnabledOnlyWhenEditable() const
{
return mIsEnabledOnlyWhenEditable->isChecked();
}

bool QgsAttributeActionPropertiesDialog::capture() const
{
Expand Down
4 changes: 3 additions & 1 deletion src/app/qgsattributeactionpropertiesdialog.h
Expand Up @@ -28,7 +28,7 @@ class QgsAttributeActionPropertiesDialog: public QDialog, private Ui::QgsAttribu
Q_OBJECT

public:
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 );
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 );

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

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

QString notificationMessage() const;

bool isEnabledOnlyWhenEditable() const;

bool capture() const;

QgsExpressionContext createExpressionContext() const override;
Expand Down
43 changes: 24 additions & 19 deletions src/app/qgsattributetabledialog.cpp
Expand Up @@ -333,25 +333,6 @@ QgsAttributeTableDialog::QgsAttributeTableDialog( QgsVectorLayer *layer, QgsAttr
mActionSearchForm->setToolTip( tr( "Search is not supported when using custom UI forms" ) );
}

QList<QgsAction> actions = mLayer->actions()->actions( QStringLiteral( "Layer" ) );

if ( actions.isEmpty() )
{
mActionFeatureActions->setVisible( false );
}
else
{
QMenu *actionMenu = new QMenu();
Q_FOREACH ( const QgsAction &action, actions )
{
QAction *qAction = actionMenu->addAction( action.icon(), action.shortTitle() );
qAction->setToolTip( action.name() );
qAction->setData( QVariant::fromValue<QgsAction>( action ) );
connect( qAction, &QAction::triggered, this, &QgsAttributeTableDialog::layerActionTriggered );
}
mActionFeatureActions->setMenu( actionMenu );
}

editingToggled();
// Close and delete if the layer has been destroyed
connect( mLayer, &QObject::destroyed, this, &QWidget::close );
Expand Down Expand Up @@ -850,6 +831,30 @@ void QgsAttributeTableDialog::editingToggled()
}
// not necessary to set table read only if layer is not editable
// because model always reflects actual state when returning item flags

QList<QgsAction> actions = mLayer->actions()->actions( QStringLiteral( "Layer" ) );

if ( actions.isEmpty() )
{
mActionFeatureActions->setVisible( false );
}
else
{
QMenu *actionMenu = new QMenu();
Q_FOREACH ( const QgsAction &action, actions )
{
if( mLayer->readOnly() && action.isEnabledOnlyWhenEditable() )
continue;

QAction *qAction = actionMenu->addAction( action.icon(), action.shortTitle() );
qAction->setEnabled( !action.isEnabledOnlyWhenEditable() || mLayer->isEditable() );
qAction->setToolTip( action.name() );
qAction->setData( QVariant::fromValue<QgsAction>( action ) );
connect( qAction, &QAction::triggered, this, &QgsAttributeTableDialog::layerActionTriggered );
}
mActionFeatureActions->setMenu( actionMenu );
}

}

void QgsAttributeTableDialog::mActionAddAttribute_triggered()
Expand Down
4 changes: 4 additions & 0 deletions src/app/qgsfeatureaction.cpp
Expand Up @@ -82,8 +82,12 @@ QgsAttributeDialog *QgsFeatureAction::newDialog( bool cloneFeature )
if ( !action.runable() )
continue;

if( mLayer->readOnly() && action.isEnabledOnlyWhenEditable() )
continue;

QgsFeature &feat = const_cast<QgsFeature &>( *dialog->feature() );
QgsFeatureAction *a = new QgsFeatureAction( action.name(), feat, mLayer, action.id(), -1, dialog );
a->setEnabled( !action.isEnabledOnlyWhenEditable() || mLayer->isEditable() );
dialog->addAction( a );
connect( a, &QAction::triggered, a, &QgsFeatureAction::execute );

Expand Down

0 comments on commit 79e5240

Please sign in to comment.