32
32
#include " qgslogger.h"
33
33
#include " qgsmapcanvas.h"
34
34
#include " qgsfeatureselectionmodel.h"
35
+ #include " qgsmaplayeractionregistry.h"
35
36
36
37
QgsAttributeTableView::QgsAttributeTableView ( QWidget *parent )
37
38
: QTableView( parent )
@@ -65,6 +66,7 @@ QgsAttributeTableView::QgsAttributeTableView( QWidget *parent )
65
66
connect ( verticalHeader (), SIGNAL ( sectionEntered ( int ) ), this , SLOT ( _q_selectRow ( int ) ) );
66
67
connect ( horizontalHeader (), SIGNAL ( sectionResized ( int , int , int ) ), this , SLOT ( columnSizeChanged ( int , int , int ) ) );
67
68
connect ( horizontalHeader (), SIGNAL ( sortIndicatorChanged ( int , Qt::SortOrder ) ), this , SLOT ( showHorizontalSortIndicator () ) );
69
+ connect ( QgsMapLayerActionRegistry::instance (), SIGNAL ( changed () ), this , SLOT ( recreateActionWidgets () ) );
68
70
}
69
71
70
72
bool QgsAttributeTableView::eventFilter ( QObject *object, QEvent *event )
@@ -151,14 +153,14 @@ void QgsAttributeTableView::setFeatureSelectionManager( QgsIFeatureSelectionMana
151
153
QWidget* QgsAttributeTableView::createActionWidget ( QgsFeatureId fid )
152
154
{
153
155
QgsAttributeTableConfig attributeTableConfig = mFilterModel ->layer ()->attributeTableConfig ();
154
- QgsActionManager* actions = mFilterModel ->layer ()->actions ();
155
156
156
157
QToolButton* toolButton = nullptr ;
157
158
QWidget* container = nullptr ;
158
159
159
160
if ( attributeTableConfig.actionWidgetStyle () == QgsAttributeTableConfig::DropDown )
160
161
{
161
162
toolButton = new QToolButton ();
163
+ toolButton->setToolButtonStyle ( Qt::ToolButtonTextBesideIcon );
162
164
toolButton->setPopupMode ( QToolButton::MenuButtonPopup );
163
165
container = toolButton;
164
166
}
@@ -169,6 +171,11 @@ QWidget* QgsAttributeTableView::createActionWidget( QgsFeatureId fid )
169
171
container->layout ()->setMargin ( 0 );
170
172
}
171
173
174
+ QList< QAction* > actionList;
175
+ QAction* defaultAction = nullptr ;
176
+
177
+ // first add user created layer actions
178
+ QgsActionManager* actions = mFilterModel ->layer ()->actions ();
172
179
for ( int i = 0 ; i < actions->size (); ++i )
173
180
{
174
181
const QgsAction& action = actions->at ( i );
@@ -179,16 +186,44 @@ QWidget* QgsAttributeTableView::createActionWidget( QgsFeatureId fid )
179
186
QString actionTitle = !action.shortTitle ().isEmpty () ? action.shortTitle () : action.icon ().isNull () ? action.name () : " " ;
180
187
QAction* act = new QAction ( action.icon (), actionTitle, container );
181
188
act->setToolTip ( action.name () );
182
- act->setData ( i );
189
+ act->setData ( " user_action" );
190
+ act->setProperty ( " action_id" , i );
183
191
act->setProperty ( " fid" , fid );
184
-
185
192
connect ( act, SIGNAL ( triggered ( bool ) ), this , SLOT ( actionTriggered () ) );
193
+ actionList << act;
186
194
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
+ {
187
222
if ( attributeTableConfig.actionWidgetStyle () == QgsAttributeTableConfig::DropDown )
188
223
{
189
224
toolButton->addAction ( act );
190
225
191
- if ( actions-> defaultAction () == i )
226
+ if ( act == defaultAction )
192
227
toolButton->setDefaultAction ( act );
193
228
194
229
container = toolButton;
@@ -201,6 +236,11 @@ QWidget* QgsAttributeTableView::createActionWidget( QgsFeatureId fid )
201
236
}
202
237
}
203
238
239
+ if ( attributeTableConfig.actionWidgetStyle () == QgsAttributeTableConfig::ButtonList )
240
+ {
241
+ static_cast < QHBoxLayout* >( container->layout () )->addStretch ();
242
+ }
243
+
204
244
if ( toolButton && !toolButton->actions ().isEmpty () && actions->defaultAction () == -1 )
205
245
toolButton->setDefaultAction ( toolButton->actions ().at ( 0 ) );
206
246
@@ -373,7 +413,19 @@ void QgsAttributeTableView::actionTriggered()
373
413
QgsFeature f;
374
414
mFilterModel ->layerCache ()->getFeatures ( QgsFeatureRequest ( fid ) ).nextFeature ( f );
375
415
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
+ }
377
429
}
378
430
379
431
void QgsAttributeTableView::columnSizeChanged ( int index, int oldWidth, int newWidth )
@@ -386,6 +438,22 @@ void QgsAttributeTableView::onActionColumnItemPainted( const QModelIndex& index
386
438
{
387
439
if ( !indexWidget ( index ) )
388
440
{
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 );
390
457
}
458
+ mActionWidgets = newWidgets;
391
459
}
0 commit comments