Skip to content
Permalink
Browse files

Merge pull request #33797 from roya0045/fix_attr_table

[Bugfix] Allow users to do cell level operation in the attribute table
  • Loading branch information
elpaso committed Jan 23, 2020
2 parents 5e8a432 + 1a2df92 commit 65501aceaf023ac67560e7a3d2991afe6d7c9c8f
@@ -126,12 +126,13 @@ Saves geometry to the settings on close

signals:

void willShowContextMenu( QMenu *menu, const QgsFeatureId featureId );
void willShowContextMenu( QMenu *menu, const QModelIndex &atIndex );
%Docstring
Emitted in order to provide a hook to add additional* menu entries to the context menu.

:param menu: If additional QMenuItems are added, they will show up in the context menu.
:param featureId: The ID of the current feature
:param atIndex: The QModelIndex, to which the context menu belongs. Relative to the source model.
In most cases, this will be a :py:class:`QgsAttributeTableFilterModel`
%End

void columnResized( int column, int width );
@@ -138,12 +138,12 @@ Emitted whenever the display expression is successfully changed
%End


void willShowContextMenu( QgsActionMenu *menu, const QgsFeatureId featureId );
void willShowContextMenu( QgsActionMenu *menu, const QModelIndex &atIndex );
%Docstring
Emitted when the context menu is created to add the specific actions to it

:param menu: is the already created context menu
:param featureId: is the ID of the current feature
:param atIndex: is the position of the current feature in the model
%End

public slots:
@@ -363,7 +363,7 @@ void QgsAttributeTableView::contextMenuEvent( QContextMenuEvent *event )
mActionPopup->addAction( tr( "Select All" ), this, SLOT( selectAll() ), QKeySequence::SelectAll );

// let some other parts of the application add some actions
emit willShowContextMenu( mActionPopup, mFilterModel->rowToId( idx ) );
emit willShowContextMenu( mActionPopup, idx );

if ( !mActionPopup->actions().isEmpty() )
{
@@ -144,9 +144,10 @@ class GUI_EXPORT QgsAttributeTableView : public QTableView
* Emitted in order to provide a hook to add additional* menu entries to the context menu.
*
* \param menu If additional QMenuItems are added, they will show up in the context menu.
* \param featureId The ID of the current feature
* \param atIndex The QModelIndex, to which the context menu belongs. Relative to the source model.
* In most cases, this will be a QgsAttributeTableFilterModel
*/
void willShowContextMenu( QMenu *menu, const QgsFeatureId featureId );
void willShowContextMenu( QMenu *menu, const QModelIndex &atIndex );

/**
* Emitted when a column in the view has been resized.
@@ -657,19 +657,14 @@ void QgsDualView::hideEvent( QHideEvent *event )
saveRecentDisplayExpressions();
}

void QgsDualView::viewWillShowContextMenu( QMenu *menu, const QgsFeatureId featureId )
void QgsDualView::viewWillShowContextMenu( QMenu *menu, const QModelIndex &atIndex )
{
if ( !menu )
{
return;
}

QModelIndex sourceIndex = mFilterModel->fidToIndex( featureId );

if ( ! sourceIndex.isValid() )
{
return;
}
QModelIndex sourceIndex = mFilterModel->mapToSource( atIndex );

QAction *copyContentAction = new QAction( tr( "Copy Cell Content" ), this );
copyContentAction->setData( QVariant::fromValue<QModelIndex>( sourceIndex ) );
@@ -705,7 +700,11 @@ void QgsDualView::viewWillShowContextMenu( QMenu *menu, const QgsFeatureId featu
menu->addAction( action.name(), a, &QgsAttributeTableAction::execute );
}
}

QModelIndex rowSourceIndex = mFilterModel->fidToIndex( mFilterModel->rowToId( atIndex ) );
if ( ! rowSourceIndex.isValid() )
{
return;
}
//add actions from QgsMapLayerActionRegistry to context menu
QList<QgsMapLayerAction *> registeredActions = QgsGui::mapLayerActionRegistry()->mapLayerActions( mLayer );
if ( !registeredActions.isEmpty() )
@@ -716,20 +715,20 @@ void QgsDualView::viewWillShowContextMenu( QMenu *menu, const QgsFeatureId featu
const auto constRegisteredActions = registeredActions;
for ( QgsMapLayerAction *action : constRegisteredActions )
{
QgsAttributeTableMapLayerAction *a = new QgsAttributeTableMapLayerAction( action->text(), this, action, sourceIndex );
QgsAttributeTableMapLayerAction *a = new QgsAttributeTableMapLayerAction( action->text(), this, action, rowSourceIndex );
menu->addAction( action->text(), a, &QgsAttributeTableMapLayerAction::execute );
}
}

menu->addSeparator();
QgsAttributeTableAction *a = new QgsAttributeTableAction( tr( "Open Form" ), this, QString(), sourceIndex );
QgsAttributeTableAction *a = new QgsAttributeTableAction( tr( "Open Form" ), this, QString(), rowSourceIndex );
menu->addAction( tr( "Open Form" ), a, &QgsAttributeTableAction::featureForm );
}


void QgsDualView::widgetWillShowContextMenu( QgsActionMenu *menu, const QgsFeatureId featureId )
void QgsDualView::widgetWillShowContextMenu( QgsActionMenu *menu, const QModelIndex &atIndex )
{
emit showContextMenuExternally( menu, featureId );
emit showContextMenuExternally( menu, mFilterModel->rowToId( atIndex ) );
}


@@ -312,9 +312,9 @@ class GUI_EXPORT QgsDualView : public QStackedWidget, private Ui::QgsDualViewBas

void previewColumnChanged( QAction *previewAction, const QString &expression );

void viewWillShowContextMenu( QMenu *menu, const QgsFeatureId featureId );
void viewWillShowContextMenu( QMenu *menu, const QModelIndex &atIndex );

void widgetWillShowContextMenu( QgsActionMenu *menu, const QgsFeatureId featureId );
void widgetWillShowContextMenu( QgsActionMenu *menu, const QModelIndex &atIndex );

void showViewHeaderMenu( QPoint point );

@@ -325,7 +325,11 @@ void QgsFeatureListView::contextMenuEvent( QContextMenuEvent *event )

QgsActionMenu *menu = new QgsActionMenu( mModel->layerCache()->layer(), feature, QStringLiteral( "Feature" ), this );

emit willShowContextMenu( menu, feature.id() );
// Index is from feature list model, but we need an index from the
// filter model to be passed to listeners, using fid instead would
// have been much better in term of bugs (and headaches) but this
// belongs to the API unfortunately.
emit willShowContextMenu( menu, mModel->mapToSource( index ) );

menu->exec( event->globalPos() );
}
@@ -156,9 +156,9 @@ class GUI_EXPORT QgsFeatureListView : public QListView
/**
* Emitted when the context menu is created to add the specific actions to it
* \param menu is the already created context menu
* \param featureId is the ID of the current feature
* \param atIndex is the position of the current feature in the model
*/
void willShowContextMenu( QgsActionMenu *menu, const QgsFeatureId featureId );
void willShowContextMenu( QgsActionMenu *menu, const QModelIndex &atIndex );

public slots:

0 comments on commit 65501ac

Please sign in to comment.
You can’t perform that action at this time.