Skip to content

Commit

Permalink
fix the attribute table
Browse files Browse the repository at this point in the history
Fixes #33439 Fixes #33665 and keep fixed #32933
  • Loading branch information
roya0045 committed Jan 27, 2020
1 parent caa6a1f commit 947201b
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 24 deletions.
Expand Up @@ -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 );
Expand Down
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/gui/attributetable/qgsattributetableview.cpp
Expand Up @@ -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() )
{
Expand Down
5 changes: 3 additions & 2 deletions src/gui/attributetable/qgsattributetableview.h
Expand Up @@ -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.
Expand Down
23 changes: 11 additions & 12 deletions src/gui/attributetable/qgsdualview.cpp
Expand Up @@ -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 ) );
Expand Down Expand Up @@ -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() )
Expand All @@ -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 ) );
}


Expand Down
4 changes: 2 additions & 2 deletions src/gui/attributetable/qgsdualview.h
Expand Up @@ -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 );

Expand Down
6 changes: 5 additions & 1 deletion src/gui/attributetable/qgsfeaturelistview.cpp
Expand Up @@ -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() );
}
Expand Down
4 changes: 2 additions & 2 deletions src/gui/attributetable/qgsfeaturelistview.h
Expand Up @@ -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:

Expand Down

0 comments on commit 947201b

Please sign in to comment.