disable or do not display maplayeractions

in some cases
This commit is contained in:
David 2018-02-22 14:46:44 +00:00
parent a7ca742d30
commit 79e524083c
20 changed files with 274 additions and 75 deletions

View File

@ -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 );
@ -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 ) )
{
@ -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 )
@ -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 )
@ -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;
@ -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 );

View File

@ -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()
@ -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;
}
@ -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() );
}
}
@ -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 );
}
@ -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
);
@ -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();

View File

@ -44,7 +44,8 @@ class APP_EXPORT QgsAttributeActionDialog: public QWidget, private Ui::QgsAttrib
ActionText,
Capture,
ActionScopes,
NotificationMessage
NotificationMessage,
EnabledOnlyWhenEditable
};
public:
@ -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;

View File

@ -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 )
{
@ -45,6 +45,7 @@ QgsAttributeActionPropertiesDialog::QgsAttributeActionPropertiesDialog( QgsActio
mActionText->setText( actionText );
mCaptureOutput->setChecked( capture );
mNotificationMessage->setText( notificationMessage );
mIsEnabledOnlyWhenEditable->setChecked( isEnabledOnlyWhenEditable );
init( actionScopes );
}
@ -107,6 +108,10 @@ QString QgsAttributeActionPropertiesDialog::notificationMessage() const
return mNotificationMessage->text();
}
bool QgsAttributeActionPropertiesDialog::isEnabledOnlyWhenEditable() const
{
return mIsEnabledOnlyWhenEditable->isChecked();
}
bool QgsAttributeActionPropertiesDialog::capture() const
{

View File

@ -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 );
@ -46,6 +46,8 @@ class QgsAttributeActionPropertiesDialog: public QDialog, private Ui::QgsAttribu
QString notificationMessage() const;
bool isEnabledOnlyWhenEditable() const;
bool capture() const;
QgsExpressionContext createExpressionContext() const override;

View File

@ -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 );
@ -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()

View File

@ -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 );

View File

@ -511,6 +511,9 @@ void QgsIdentifyResultsDialog::addFeature( QgsVectorLayer *vlayer, const QgsFeat
if ( !action.runable() )
continue;
if( action.isEnabledOnlyWhenEditable() )
continue;
QTreeWidgetItem *twi = new QTreeWidgetItem( QStringList() << QLatin1String( "" ) << action.name() );
twi->setIcon( 0, QgsApplication::getThemeIcon( QStringLiteral( "/mAction.svg" ) ) );
twi->setData( 0, Qt::UserRole, "action" );
@ -521,6 +524,9 @@ void QgsIdentifyResultsDialog::addFeature( QgsVectorLayer *vlayer, const QgsFeat
//add actions from QgsMapLayerActionRegistry
for ( int i = 0; i < registeredActions.size(); i++ )
{
if( registeredActions.at( i )->isEnabledOnlyWhenEditable() )
continue;
QgsMapLayerAction *action = registeredActions.at( i );
QTreeWidgetItem *twi = new QTreeWidgetItem( QStringList() << QLatin1String( "" ) << action->text() );
twi->setIcon( 0, QgsApplication::getThemeIcon( QStringLiteral( "/mAction.svg" ) ) );
@ -1092,6 +1098,9 @@ void QgsIdentifyResultsDialog::contextMenuEvent( QContextMenuEvent *event )
if ( !action.runable() )
continue;
if( action.isEnabledOnlyWhenEditable() )
continue;
QgsFeatureAction *a = new QgsFeatureAction( action.name(), mFeatures[ featIdx ], vlayer, action.id(), idx, this );
mActionPopup->addAction( QgsApplication::getThemeIcon( QStringLiteral( "/mAction.svg" ) ), action.name(), a, SLOT( execute() ) );
}
@ -1110,9 +1119,12 @@ void QgsIdentifyResultsDialog::contextMenuEvent( QContextMenuEvent *event )
int featIdx = featItem->data( 0, Qt::UserRole + 1 ).toInt();
QList<QgsMapLayerAction *>::iterator actionIt;
QList<QgsMapLayerAction *>::const_iterator actionIt;
for ( actionIt = registeredActions.begin(); actionIt != registeredActions.end(); ++actionIt )
{
if( ( *actionIt )->isEnabledOnlyWhenEditable() )
continue;
QgsIdentifyResultsDialogMapLayerAction *a = new QgsIdentifyResultsDialogMapLayerAction( ( *actionIt )->text(), this, ( *actionIt ), vlayer, &( mFeatures[ featIdx ] ) );
mActionPopup->addAction( QgsApplication::getThemeIcon( QStringLiteral( "/mAction.svg" ) ), ( *actionIt )->text(), a, SLOT( execute() ) );
}

View File

@ -124,6 +124,7 @@ void QgsAction::readXml( const QDomNode &actionNode )
mCaptureOutput = actionElement.attributeNode( QStringLiteral( "capture" ) ).value().toInt() != 0;
mShortTitle = actionElement.attributeNode( QStringLiteral( "shortTitle" ) ).value();
mNotificationMessage = actionElement.attributeNode( QStringLiteral( "notificationMessage" ) ).value();
mIsEnabledOnlyWhenEditable = actionElement.attributeNode( QStringLiteral( "isEnabledOnlyWhenEditable" ) ).value().toInt() != 0;
mId = QUuid( actionElement.attributeNode( QStringLiteral( "id" ) ).value() );
if ( mId.isNull() )
mId = QUuid::createUuid();
@ -139,6 +140,7 @@ void QgsAction::writeXml( QDomNode &actionsNode ) const
actionSetting.setAttribute( QStringLiteral( "action" ), mCommand );
actionSetting.setAttribute( QStringLiteral( "capture" ), mCaptureOutput );
actionSetting.setAttribute( QStringLiteral( "notificationMessage" ), mNotificationMessage );
actionSetting.setAttribute( QStringLiteral( "isEnabledOnlyWhenEditable" ), mIsEnabledOnlyWhenEditable );
actionSetting.setAttribute( QStringLiteral( "id" ), mId.toString() );
Q_FOREACH ( const QString &scope, mActionScopes )

View File

@ -58,12 +58,13 @@ class CORE_EXPORT QgsAction
* \param command The action text. Its interpretation depends on the type
* \param capture If this is set to true, the output will be captured when an action is run
*/
QgsAction( ActionType type, const QString &description, const QString &command, bool capture = false )
QgsAction( ActionType type, const QString &description, const QString &command, bool capture = false, bool enabledOnlyWhenEditable = false )
: mType( type )
, mDescription( description )
, mCommand( command )
, mCaptureOutput( capture )
, mId( QUuid::createUuid() )
, mIsEnabledOnlyWhenEditable( enabledOnlyWhenEditable )
{}
/**
@ -78,7 +79,7 @@ class CORE_EXPORT QgsAction
* \param actionScopes A set of scopes in which this action will be available
* \param notificationMessage A particular message which reception will trigger the action
*/
QgsAction( ActionType type, const QString &description, const QString &action, const QString &icon, bool capture, const QString &shortTitle = QString(), const QSet<QString> &actionScopes = QSet<QString>(), const QString &notificationMessage = QString() )
QgsAction( ActionType type, const QString &description, const QString &action, const QString &icon, bool capture, const QString &shortTitle = QString(), const QSet<QString> &actionScopes = QSet<QString>(), const QString &notificationMessage = QString(), bool enabledOnlyWhenEditable = false )
: mType( type )
, mDescription( description )
, mShortTitle( shortTitle )
@ -88,6 +89,7 @@ class CORE_EXPORT QgsAction
, mActionScopes( actionScopes )
, mNotificationMessage( notificationMessage )
, mId( QUuid::createUuid() )
, mIsEnabledOnlyWhenEditable( enabledOnlyWhenEditable )
{}
//! The name of the action. This may be a longer description.
@ -138,6 +140,11 @@ class CORE_EXPORT QgsAction
//! Whether to capture output for display when this action is run
bool capture() const { return mCaptureOutput; }
//! Return whether only enabled in editable mode
bool isEnabledOnlyWhenEditable() const { return mIsEnabledOnlyWhenEditable; }
//! Checks if the action is runable on the current platform
bool runable() const;
@ -216,6 +223,7 @@ class CORE_EXPORT QgsAction
mutable std::shared_ptr<QAction> mAction;
QUuid mId;
QgsExpressionContextScope mExpressionContextScope;
bool mIsEnabledOnlyWhenEditable;
};
Q_DECLARE_METATYPE( QgsAction )

View File

@ -135,6 +135,10 @@ void QgsAttributeTableView::setModel( QgsAttributeTableFilterModel *filterModel
connect( mFeatureSelectionModel, static_cast<void ( QgsFeatureSelectionModel::* )()>( &QgsFeatureSelectionModel::requestRepaint ),
this, static_cast<void ( QgsAttributeTableView::* )()>( &QgsAttributeTableView::repaintRequested ) );
}
connect( mFilterModel->layer(), &QgsVectorLayer::editingStarted, this, &QgsAttributeTableView::recreateActionWidgets );
connect( mFilterModel->layer(), &QgsVectorLayer::editingStopped, this, &QgsAttributeTableView::recreateActionWidgets );
connect( mFilterModel->layer(), &QgsVectorLayer::readOnlyChanged, this, &QgsAttributeTableView::recreateActionWidgets );
}
void QgsAttributeTableView::setFeatureSelectionManager( QgsIFeatureSelectionManager *featureSelectionManager )
@ -176,8 +180,12 @@ QWidget *QgsAttributeTableView::createActionWidget( QgsFeatureId fid )
QList<QgsAction> actions = mFilterModel->layer()->actions()->actions( QStringLiteral( "Feature" ) );
Q_FOREACH ( const QgsAction &action, actions )
{
if( mFilterModel->layer()->readOnly() && action.isEnabledOnlyWhenEditable() )
continue;
QString actionTitle = !action.shortTitle().isEmpty() ? action.shortTitle() : action.icon().isNull() ? action.name() : QLatin1String( "" );
QAction *act = new QAction( action.icon(), actionTitle, container );
act->setEnabled( !action.isEnabledOnlyWhenEditable() || mFilterModel->layer()->isEditable() );
act->setToolTip( action.name() );
act->setData( "user_action" );
act->setProperty( "fid", fid );
@ -194,7 +202,11 @@ QWidget *QgsAttributeTableView::createActionWidget( QgsFeatureId fid )
QgsGui::mapLayerActionRegistry()->mapLayerActions( mFilterModel->layer(),
QgsMapLayerAction::SingleFeature ) )
{
if( mFilterModel->layer()->readOnly() && mapLayerAction->isEnabledOnlyWhenEditable() )
continue;
QAction *action = new QAction( mapLayerAction->icon(), mapLayerAction->text(), container );
action->setEnabled( !mapLayerAction->isEnabledOnlyWhenEditable() || mFilterModel->layer()->isEditable() );
action->setData( "map_layer_action" );
action->setToolTip( mapLayerAction->text() );
action->setProperty( "fid", fid );

View File

@ -571,12 +571,16 @@ void QgsDualView::viewWillShowContextMenu( QMenu *menu, const QModelIndex &atInd
if ( !action.runable() )
continue;
if( vl->readOnly() && action.isEnabledOnlyWhenEditable() )
continue;
QgsAttributeTableAction *a = new QgsAttributeTableAction( action.name(), this, action.id(), sourceIndex );
#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
menu->addAction( action.name(), a, SLOT( execute() ) );
QAction *ma = menu->addAction( action.name(), a, SLOT( execute() ) );
#else
menu->addAction( action.name(), a, &QgsAttributeTableAction::execute );
QAction *ma = menu->addAction( action.name(), a, &QgsAttributeTableAction::execute );
#endif
ma->setEnabled( !action.isEnabledOnlyWhenEditable() || vl->isEditable() );
}
}
@ -589,12 +593,16 @@ void QgsDualView::viewWillShowContextMenu( QMenu *menu, const QModelIndex &atInd
Q_FOREACH ( QgsMapLayerAction *action, registeredActions )
{
if( vl->readOnly() && action->isEnabledOnlyWhenEditable() )
continue;
QgsAttributeTableMapLayerAction *a = new QgsAttributeTableMapLayerAction( action->text(), this, action, sourceIndex );
#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
menu->addAction( action->text(), a, SLOT( execut() ) );
QAction *ma = menu->addAction( action->text(), a, SLOT( execut() ) );
#else
menu->addAction( action->text(), a, &QgsAttributeTableMapLayerAction::execute );
QAction *ma = menu->addAction( action->text(), a, &QgsAttributeTableMapLayerAction::execute );
#endif
ma->setEnabled( !action->isEnabledOnlyWhenEditable() || vl->isEditable() );
}
}

View File

@ -44,6 +44,9 @@ void QgsActionMenu::init()
setTitle( tr( "&Actions" ) );
connect( QgsGui::mapLayerActionRegistry(), &QgsMapLayerActionRegistry::changed, this, &QgsActionMenu::reloadActions );
connect( mLayer, &QgsVectorLayer::editingStarted, this, &QgsActionMenu::reloadActions );
connect( mLayer, &QgsVectorLayer::editingStopped, this, &QgsActionMenu::reloadActions );
connect( mLayer, &QgsVectorLayer::readOnlyChanged, this, &QgsActionMenu::reloadActions );
reloadActions();
}
@ -63,6 +66,12 @@ void QgsActionMenu::setFeature( const QgsFeature &feature )
mFeature = feature;
}
void QgsActionMenu::setMode( const QgsAttributeForm::Mode mode )
{
mMode=mode;
reloadActions();
}
void QgsActionMenu::triggerAction()
{
if ( !feature().isValid() )
@ -107,10 +116,17 @@ void QgsActionMenu::reloadActions()
Q_FOREACH ( const QgsAction &action, mActions )
{
if( mLayer->readOnly() && action.isEnabledOnlyWhenEditable() )
continue;
if( action.isEnabledOnlyWhenEditable() && mMode==QgsAttributeForm::AddFeatureMode )
continue;
QgsAction act( action );
act.setExpressionContextScope( mExpressionContextScope );
QAction *qAction = new QAction( action.icon(), action.name(), this );
qAction->setEnabled( !action.isEnabledOnlyWhenEditable() || mLayer->isEditable() );
qAction->setData( QVariant::fromValue<ActionData>( ActionData( act, mFeatureId, mLayer ) ) );
qAction->setIcon( action.icon() );
@ -138,7 +154,15 @@ void QgsActionMenu::reloadActions()
for ( int i = 0; i < mapLayerActions.size(); ++i )
{
QgsMapLayerAction *qaction = mapLayerActions.at( i );
if( mLayer->readOnly() && qaction->isEnabledOnlyWhenEditable() )
continue;
if( qaction->isEnabledOnlyWhenEditable() && mMode==QgsAttributeForm::AddFeatureMode )
continue;
QAction *qAction = new QAction( qaction->icon(), qaction->text(), this );
qAction->setEnabled( !qaction->isEnabledOnlyWhenEditable() || mLayer->isEditable() );
qAction->setData( QVariant::fromValue<ActionData>( ActionData( qaction, mFeatureId, mLayer ) ) );
addAction( qAction );
connect( qAction, &QAction::triggered, this, &QgsActionMenu::triggerAction );

View File

@ -23,6 +23,7 @@
#include "qgsfeature.h"
#include "qgsaction.h"
#include "qgis_gui.h"
#include "qgsattributeform.h"
class QgsMapLayer;
class QgsMapLayerAction;
@ -91,6 +92,13 @@ class GUI_EXPORT QgsActionMenu : public QMenu
*/
void setFeature( const QgsFeature &feature );
/**
* Change the mode of the actions
*
* \param mode The mode of the attribute form
*/
void setMode( const QgsAttributeForm::Mode mode );
/**
* Sets an expression context scope used to resolve underlying actions.
*
@ -122,6 +130,7 @@ class GUI_EXPORT QgsActionMenu : public QMenu
QgsFeatureId mFeatureId;
QString mActionScope;
QgsExpressionContextScope mExpressionContextScope;
QgsAttributeForm::Mode mMode;
};

View File

@ -21,7 +21,6 @@
#include "qgsattributeform.h"
#include "qgshighlight.h"
#include "qgsapplication.h"
#include "qgsactionmenu.h"
#include "qgssettings.h"
QgsAttributeDialog::QgsAttributeDialog( QgsVectorLayer *vl, QgsFeature *thepFeature, bool featureOwner, QWidget *parent, bool showDialogButtons, const QgsAttributeEditorContext &context )
@ -92,21 +91,23 @@ void QgsAttributeDialog::init( QgsVectorLayer *layer, QgsFeature *feature, const
connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsAttributeDialog::accept );
connect( layer, &QObject::destroyed, this, &QWidget::close );
QgsActionMenu *menu = new QgsActionMenu( layer, mAttributeForm->feature(), QStringLiteral( "Feature" ), this );
if ( !menu->actions().isEmpty() )
mMenu = new QgsActionMenu( layer, mAttributeForm->feature(), QStringLiteral( "Feature" ), this );
if ( !mMenu->actions().isEmpty() )
{
QMenuBar *menuBar = new QMenuBar( this );
menuBar->addMenu( menu );
menuBar->addMenu( mMenu );
layout()->setMenuBar( menuBar );
}
else
{
delete menu;
}
focusNextChild();
}
void QgsAttributeDialog::setMode( QgsAttributeForm::Mode mode )
{
mAttributeForm->setMode( mode );
mMenu->setMode( mode );
}
bool QgsAttributeDialog::event( QEvent *e )
{
if ( e->type() == QEvent::WindowActivate && mHighlight )

View File

@ -21,6 +21,7 @@
#include "qgis.h"
#include "qgsattributeform.h"
#include "qgstrackedvectorlayertools.h"
#include "qgsactionmenu.h"
#include <QDialog>
#include <QMenuBar>
@ -77,7 +78,7 @@ class GUI_EXPORT QgsAttributeDialog : public QDialog
* \param mode form mode. For example, if set to QgsAttributeForm::AddFeatureMode, the dialog will be editable even with an invalid feature and
* will add a new feature when the form is accepted.
*/
void setMode( QgsAttributeForm::Mode mode ) { mAttributeForm->setMode( mode ); }
void setMode( QgsAttributeForm::Mode mode );
/**
* Sets the edit command message (Undo) that will be used when the dialog is accepted
@ -119,6 +120,8 @@ class GUI_EXPORT QgsAttributeDialog : public QDialog
// true if this dialog is editable
bool mEditable;
QgsActionMenu *mMenu;
static int sFormCounter;
static QString sSettingsPath;

View File

@ -16,31 +16,34 @@
#include "qgsmaplayeractionregistry.h"
#include "qgsgui.h"
QgsMapLayerAction::QgsMapLayerAction( const QString &name, QObject *parent, Targets targets, const QIcon &icon )
QgsMapLayerAction::QgsMapLayerAction( const QString &name, QObject *parent, Targets targets, const QIcon &icon, const bool enabledOnlyWhenEditable )
: QAction( icon, name, parent )
, mSingleLayer( false )
, mSpecificLayerType( false )
, mLayerType( QgsMapLayer::VectorLayer )
, mTargets( targets )
, mEnabledOnlyWhenEditable( enabledOnlyWhenEditable )
{
}
QgsMapLayerAction::QgsMapLayerAction( const QString &name, QObject *parent, QgsMapLayer *layer, Targets targets, const QIcon &icon )
QgsMapLayerAction::QgsMapLayerAction( const QString &name, QObject *parent, QgsMapLayer *layer, Targets targets, const QIcon &icon, const bool enabledOnlyWhenEditable )
: QAction( icon, name, parent )
, mSingleLayer( true )
, mActionLayer( layer )
, mSpecificLayerType( false )
, mLayerType( QgsMapLayer::VectorLayer )
, mTargets( targets )
, mEnabledOnlyWhenEditable( enabledOnlyWhenEditable )
{
}
QgsMapLayerAction::QgsMapLayerAction( const QString &name, QObject *parent, QgsMapLayer::LayerType layerType, Targets targets, const QIcon &icon )
QgsMapLayerAction::QgsMapLayerAction( const QString &name, QObject *parent, QgsMapLayer::LayerType layerType, Targets targets, const QIcon &icon, const bool enabledOnlyWhenEditable )
: QAction( icon, name, parent )
, mSingleLayer( false )
, mSpecificLayerType( true )
, mLayerType( layerType )
, mTargets( targets )
, mEnabledOnlyWhenEditable( enabledOnlyWhenEditable )
{
}

View File

@ -50,13 +50,13 @@ class GUI_EXPORT QgsMapLayerAction : public QAction
* Creates a map layer action which can run on any layer
* \note using AllActions as a target probably does not make a lot of sense. This default action was settled for API compatibility reasons.
*/
QgsMapLayerAction( const QString &name, QObject *parent SIP_TRANSFERTHIS, Targets targets = AllActions, const QIcon &icon = QIcon() );
QgsMapLayerAction( const QString &name, QObject *parent SIP_TRANSFERTHIS, Targets targets = AllActions, const QIcon &icon = QIcon(), const bool enabledOnlyWhenEditable = false );
//! Creates a map layer action which can run only on a specific layer
QgsMapLayerAction( const QString &name, QObject *parent SIP_TRANSFERTHIS, QgsMapLayer *layer, Targets targets = AllActions, const QIcon &icon = QIcon() );
QgsMapLayerAction( const QString &name, QObject *parent SIP_TRANSFERTHIS, QgsMapLayer *layer, Targets targets = AllActions, const QIcon &icon = QIcon(), const bool enabledOnlyWhenEditable = false );
//! Creates a map layer action which can run on a specific type of layer
QgsMapLayerAction( const QString &name, QObject *parent SIP_TRANSFERTHIS, QgsMapLayer::LayerType layerType, Targets targets = AllActions, const QIcon &icon = QIcon() );
QgsMapLayerAction( const QString &name, QObject *parent SIP_TRANSFERTHIS, QgsMapLayer::LayerType layerType, Targets targets = AllActions, const QIcon &icon = QIcon(), const bool enabledOnlyWhenEditable = false );
~QgsMapLayerAction() override;
@ -77,6 +77,9 @@ class GUI_EXPORT QgsMapLayerAction : public QAction
//! Return availibity of action
const Targets &targets() const {return mTargets;}
//! Return whether only enabled in editable mode
bool isEnabledOnlyWhenEditable() const { return mEnabledOnlyWhenEditable; }
signals:
//! Triggered when action has been run for a specific list of features
void triggeredForFeatures( QgsMapLayer *layer, const QList<QgsFeature> &featureList );
@ -101,6 +104,8 @@ class GUI_EXPORT QgsMapLayerAction : public QAction
// determine if the action can be run on layer and/or single feature and/or multiple features
Targets mTargets;
bool mEnabledOnlyWhenEditable;
};
Q_DECLARE_OPERATORS_FOR_FLAGS( QgsMapLayerAction::Targets )

View File

@ -44,7 +44,7 @@
<property name="title">
<string>Action list</string>
</property>
<property name="syncGroup">
<property name="syncGroup" stdset="0">
<string notr="true">actiongroup</string>
</property>
<layout class="QGridLayout" name="gridLayout_3">
@ -146,7 +146,7 @@
<enum>QAbstractItemView::SelectRows</enum>
</property>
<property name="columnCount">
<number>7</number>
<number>8</number>
</property>
<column>
<property name="text">
@ -186,6 +186,11 @@
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;If not empty, this will enable provider notification listening and the action will be executed when the notification message matched the specified value. &lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</column>
<column>
<property name="text">
<string>Only when editable</string>
</property>
</column>
</widget>
</item>
<item row="1" column="2">
@ -275,6 +280,33 @@
</tabstops>
<resources>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
</resources>
<connections/>
</ui>

View File

@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>702</width>
<height>547</height>
<height>554</height>
</rect>
</property>
<property name="windowTitle">
@ -39,10 +39,28 @@
<string>Action text</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="2" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_11">
<item>
<widget class="QLabel" name="label_6">
<property name="text">
<string>Execute if notification matches</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="mNotificationMessage">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;If specified, listen to data source notification and performs action if notification message matches the specified value.&lt;/p&gt;&lt;p&gt;E.g. to match message beginning with &lt;span style=&quot; font-weight:600;&quot;&gt;whatever &lt;/span&gt;use &lt;span style=&quot; font-weight:600;&quot;&gt;^whatever&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="1" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QgsFieldExpressionWidget" name="mFieldExpression">
<widget class="QgsFieldExpressionWidget" name="mFieldExpression" native="true">
<property name="focusPolicy">
<enum>Qt::TabFocus</enum>
</property>
@ -110,23 +128,12 @@
</item>
</layout>
</item>
<item row="2" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_11">
<item>
<widget class="QLabel" name="label_6">
<property name="text">
<string>Execute if notification matches</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="mNotificationMessage">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;If specified, listen to data source notification and performs action if notification message matches the specified value.&lt;/p&gt;&lt;p&gt;E.g. to match message beginning with &lt;span style=&quot; font-weight:600;&quot;&gt;whatever &lt;/span&gt;use &lt;span style=&quot; font-weight:600;&quot;&gt;^whatever&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
</layout>
<item row="3" column="0">
<widget class="QCheckBox" name="mIsEnabledOnlyWhenEditable">
<property name="text">
<string>Enable only when editable</string>
</property>
</widget>
</item>
</layout>
</widget>
@ -317,6 +324,33 @@
</tabstops>
<resources>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
<include location="../../images/images.qrc"/>
</resources>
<connections>
<connection>