Skip to content

Commit e985f2c

Browse files
committedJul 5, 2016
Also show map layer actions in attribute table (fix #15127)
1 parent cdf82a2 commit e985f2c

File tree

2 files changed

+76
-6
lines changed

2 files changed

+76
-6
lines changed
 

‎src/gui/attributetable/qgsattributetableview.cpp

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "qgslogger.h"
3333
#include "qgsmapcanvas.h"
3434
#include "qgsfeatureselectionmodel.h"
35+
#include "qgsmaplayeractionregistry.h"
3536

3637
QgsAttributeTableView::QgsAttributeTableView( QWidget *parent )
3738
: QTableView( parent )
@@ -65,6 +66,7 @@ QgsAttributeTableView::QgsAttributeTableView( QWidget *parent )
6566
connect( verticalHeader(), SIGNAL( sectionEntered( int ) ), this, SLOT( _q_selectRow( int ) ) );
6667
connect( horizontalHeader(), SIGNAL( sectionResized( int, int, int ) ), this, SLOT( columnSizeChanged( int, int, int ) ) );
6768
connect( horizontalHeader(), SIGNAL( sortIndicatorChanged( int, Qt::SortOrder ) ), this, SLOT( showHorizontalSortIndicator() ) );
69+
connect( QgsMapLayerActionRegistry::instance(), SIGNAL( changed() ), this, SLOT( recreateActionWidgets() ) );
6870
}
6971

7072
bool QgsAttributeTableView::eventFilter( QObject *object, QEvent *event )
@@ -151,14 +153,14 @@ void QgsAttributeTableView::setFeatureSelectionManager( QgsIFeatureSelectionMana
151153
QWidget* QgsAttributeTableView::createActionWidget( QgsFeatureId fid )
152154
{
153155
QgsAttributeTableConfig attributeTableConfig = mFilterModel->layer()->attributeTableConfig();
154-
QgsActionManager* actions = mFilterModel->layer()->actions();
155156

156157
QToolButton* toolButton = nullptr;
157158
QWidget* container = nullptr;
158159

159160
if ( attributeTableConfig.actionWidgetStyle() == QgsAttributeTableConfig::DropDown )
160161
{
161162
toolButton = new QToolButton();
163+
toolButton->setToolButtonStyle( Qt::ToolButtonTextBesideIcon );
162164
toolButton->setPopupMode( QToolButton::MenuButtonPopup );
163165
container = toolButton;
164166
}
@@ -169,6 +171,11 @@ QWidget* QgsAttributeTableView::createActionWidget( QgsFeatureId fid )
169171
container->layout()->setMargin( 0 );
170172
}
171173

174+
QList< QAction* > actionList;
175+
QAction* defaultAction = nullptr;
176+
177+
// first add user created layer actions
178+
QgsActionManager* actions = mFilterModel->layer()->actions();
172179
for ( int i = 0; i < actions->size(); ++i )
173180
{
174181
const QgsAction& action = actions->at( i );
@@ -179,16 +186,44 @@ QWidget* QgsAttributeTableView::createActionWidget( QgsFeatureId fid )
179186
QString actionTitle = !action.shortTitle().isEmpty() ? action.shortTitle() : action.icon().isNull() ? action.name() : "";
180187
QAction* act = new QAction( action.icon(), actionTitle, container );
181188
act->setToolTip( action.name() );
182-
act->setData( i );
189+
act->setData( "user_action" );
190+
act->setProperty( "action_id", i );
183191
act->setProperty( "fid", fid );
184-
185192
connect( act, SIGNAL( triggered( bool ) ), this, SLOT( actionTriggered() ) );
193+
actionList << act;
186194

195+
if ( actions->defaultAction() == i )
196+
defaultAction = act;
197+
}
198+
199+
// next add any registered actions for this layer
200+
Q_FOREACH ( QgsMapLayerAction* mapLayerAction,
201+
QgsMapLayerActionRegistry::instance()->mapLayerActions( mFilterModel->layer(),
202+
QgsMapLayerAction::SingleFeature ) )
203+
{
204+
QAction* action = new QAction( mapLayerAction->icon(), mapLayerAction->text(), container );
205+
action->setData( "map_layer_action" );
206+
action->setToolTip( mapLayerAction->text() );
207+
action->setProperty( "fid", fid );
208+
action->setProperty( "action", qVariantFromValue( qobject_cast<QObject *>( mapLayerAction ) ) );
209+
connect( action, SIGNAL( triggered() ), this, SLOT( actionTriggered() ) );
210+
actionList << action;
211+
212+
if ( !defaultAction &&
213+
QgsMapLayerActionRegistry::instance()->defaultActionForLayer( mFilterModel->layer() ) == mapLayerAction )
214+
defaultAction = action;
215+
}
216+
217+
if ( !defaultAction && !actionList.isEmpty() )
218+
defaultAction = actionList.at( 0 );
219+
220+
Q_FOREACH ( QAction* act, actionList )
221+
{
187222
if ( attributeTableConfig.actionWidgetStyle() == QgsAttributeTableConfig::DropDown )
188223
{
189224
toolButton->addAction( act );
190225

191-
if ( actions->defaultAction() == i )
226+
if ( act == defaultAction )
192227
toolButton->setDefaultAction( act );
193228

194229
container = toolButton;
@@ -201,6 +236,11 @@ QWidget* QgsAttributeTableView::createActionWidget( QgsFeatureId fid )
201236
}
202237
}
203238

239+
if ( attributeTableConfig.actionWidgetStyle() == QgsAttributeTableConfig::ButtonList )
240+
{
241+
static_cast< QHBoxLayout* >( container->layout() )->addStretch();
242+
}
243+
204244
if ( toolButton && !toolButton->actions().isEmpty() && actions->defaultAction() == -1 )
205245
toolButton->setDefaultAction( toolButton->actions().at( 0 ) );
206246

@@ -373,7 +413,19 @@ void QgsAttributeTableView::actionTriggered()
373413
QgsFeature f;
374414
mFilterModel->layerCache()->getFeatures( QgsFeatureRequest( fid ) ).nextFeature( f );
375415

376-
mFilterModel->layer()->actions()->doAction( action->data().toInt(), f );
416+
if ( action->data().toString() == "user_action" )
417+
{
418+
mFilterModel->layer()->actions()->doAction( action->property( "action_id" ).toInt(), f );
419+
}
420+
else if ( action->data().toString() == "map_layer_action" )
421+
{
422+
QObject* object = action->property( "action" ).value<QObject *>();
423+
QgsMapLayerAction* layerAction = qobject_cast<QgsMapLayerAction *>( object );
424+
if ( layerAction )
425+
{
426+
layerAction->triggerForFeature( mFilterModel->layer(), &f );
427+
}
428+
}
377429
}
378430

379431
void QgsAttributeTableView::columnSizeChanged( int index, int oldWidth, int newWidth )
@@ -386,6 +438,22 @@ void QgsAttributeTableView::onActionColumnItemPainted( const QModelIndex& index
386438
{
387439
if ( !indexWidget( index ) )
388440
{
389-
setIndexWidget( index, createActionWidget( mFilterModel->data( index, QgsAttributeTableModel::FeatureIdRole ).toLongLong() ) );
441+
QWidget* widget = createActionWidget( mFilterModel->data( index, QgsAttributeTableModel::FeatureIdRole ).toLongLong() );
442+
mActionWidgets.insert( index, widget );
443+
setIndexWidget( index, widget );
444+
}
445+
}
446+
447+
void QgsAttributeTableView::recreateActionWidgets()
448+
{
449+
QMap< QModelIndex, QWidget* > newWidgets;
450+
QMap< QModelIndex, QWidget* >::const_iterator it = mActionWidgets.constBegin();
451+
for ( ; it != mActionWidgets.constEnd(); ++it )
452+
{
453+
it.value()->deleteLater(); //?
454+
QWidget* widget = createActionWidget( mFilterModel->data( it.key(), QgsAttributeTableModel::FeatureIdRole ).toLongLong() );
455+
newWidgets.insert( it.key(), widget );
456+
setIndexWidget( it.key(), widget );
390457
}
458+
mActionWidgets = newWidgets;
391459
}

‎src/gui/attributetable/qgsattributetableview.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ class GUI_EXPORT QgsAttributeTableView : public QTableView
156156
void actionTriggered();
157157
void columnSizeChanged( int index, int oldWidth, int newWidth );
158158
void onActionColumnItemPainted( const QModelIndex& index );
159+
void recreateActionWidgets();
159160

160161
private:
161162
void updateActionImage( QWidget* widget );
@@ -169,6 +170,7 @@ class GUI_EXPORT QgsAttributeTableView : public QTableView
169170
QMenu *mActionPopup;
170171
int mRowSectionAnchor;
171172
QItemSelectionModel::SelectionFlag mCtrlDragSelectionFlag;
173+
QMap< QModelIndex, QWidget* > mActionWidgets;
172174
};
173175

174176
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.