Skip to content
Permalink
Browse files

Also show map layer actions in attribute table (fix #15127)

  • Loading branch information
nyalldawson committed Jul 5, 2016
1 parent cdf82a2 commit e985f2c9247e3ee2bc6715643766d15abc6e9c61
Showing with 76 additions and 6 deletions.
  1. +74 −6 src/gui/attributetable/qgsattributetableview.cpp
  2. +2 −0 src/gui/attributetable/qgsattributetableview.h
@@ -32,6 +32,7 @@
#include "qgslogger.h"
#include "qgsmapcanvas.h"
#include "qgsfeatureselectionmodel.h"
#include "qgsmaplayeractionregistry.h"

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

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

QToolButton* toolButton = nullptr;
QWidget* container = nullptr;

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

QList< QAction* > actionList;
QAction* defaultAction = nullptr;

// first add user created layer actions
QgsActionManager* actions = mFilterModel->layer()->actions();
for ( int i = 0; i < actions->size(); ++i )
{
const QgsAction& action = actions->at( i );
@@ -179,16 +186,44 @@ QWidget* QgsAttributeTableView::createActionWidget( QgsFeatureId fid )
QString actionTitle = !action.shortTitle().isEmpty() ? action.shortTitle() : action.icon().isNull() ? action.name() : "";
QAction* act = new QAction( action.icon(), actionTitle, container );
act->setToolTip( action.name() );
act->setData( i );
act->setData( "user_action" );
act->setProperty( "action_id", i );
act->setProperty( "fid", fid );

connect( act, SIGNAL( triggered( bool ) ), this, SLOT( actionTriggered() ) );
actionList << act;

if ( actions->defaultAction() == i )
defaultAction = act;
}

// next add any registered actions for this layer
Q_FOREACH ( QgsMapLayerAction* mapLayerAction,
QgsMapLayerActionRegistry::instance()->mapLayerActions( mFilterModel->layer(),
QgsMapLayerAction::SingleFeature ) )
{
QAction* action = new QAction( mapLayerAction->icon(), mapLayerAction->text(), container );
action->setData( "map_layer_action" );
action->setToolTip( mapLayerAction->text() );
action->setProperty( "fid", fid );
action->setProperty( "action", qVariantFromValue( qobject_cast<QObject *>( mapLayerAction ) ) );
connect( action, SIGNAL( triggered() ), this, SLOT( actionTriggered() ) );
actionList << action;

if ( !defaultAction &&
QgsMapLayerActionRegistry::instance()->defaultActionForLayer( mFilterModel->layer() ) == mapLayerAction )
defaultAction = action;
}

if ( !defaultAction && !actionList.isEmpty() )
defaultAction = actionList.at( 0 );

Q_FOREACH ( QAction* act, actionList )
{
if ( attributeTableConfig.actionWidgetStyle() == QgsAttributeTableConfig::DropDown )
{
toolButton->addAction( act );

if ( actions->defaultAction() == i )
if ( act == defaultAction )
toolButton->setDefaultAction( act );

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

if ( attributeTableConfig.actionWidgetStyle() == QgsAttributeTableConfig::ButtonList )
{
static_cast< QHBoxLayout* >( container->layout() )->addStretch();
}

if ( toolButton && !toolButton->actions().isEmpty() && actions->defaultAction() == -1 )
toolButton->setDefaultAction( toolButton->actions().at( 0 ) );

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

mFilterModel->layer()->actions()->doAction( action->data().toInt(), f );
if ( action->data().toString() == "user_action" )
{
mFilterModel->layer()->actions()->doAction( action->property( "action_id" ).toInt(), f );
}
else if ( action->data().toString() == "map_layer_action" )
{
QObject* object = action->property( "action" ).value<QObject *>();
QgsMapLayerAction* layerAction = qobject_cast<QgsMapLayerAction *>( object );
if ( layerAction )
{
layerAction->triggerForFeature( mFilterModel->layer(), &f );
}
}
}

void QgsAttributeTableView::columnSizeChanged( int index, int oldWidth, int newWidth )
@@ -386,6 +438,22 @@ void QgsAttributeTableView::onActionColumnItemPainted( const QModelIndex& index
{
if ( !indexWidget( index ) )
{
setIndexWidget( index, createActionWidget( mFilterModel->data( index, QgsAttributeTableModel::FeatureIdRole ).toLongLong() ) );
QWidget* widget = createActionWidget( mFilterModel->data( index, QgsAttributeTableModel::FeatureIdRole ).toLongLong() );
mActionWidgets.insert( index, widget );
setIndexWidget( index, widget );
}
}

void QgsAttributeTableView::recreateActionWidgets()
{
QMap< QModelIndex, QWidget* > newWidgets;
QMap< QModelIndex, QWidget* >::const_iterator it = mActionWidgets.constBegin();
for ( ; it != mActionWidgets.constEnd(); ++it )
{
it.value()->deleteLater(); //?
QWidget* widget = createActionWidget( mFilterModel->data( it.key(), QgsAttributeTableModel::FeatureIdRole ).toLongLong() );
newWidgets.insert( it.key(), widget );
setIndexWidget( it.key(), widget );
}
mActionWidgets = newWidgets;
}
@@ -156,6 +156,7 @@ class GUI_EXPORT QgsAttributeTableView : public QTableView
void actionTriggered();
void columnSizeChanged( int index, int oldWidth, int newWidth );
void onActionColumnItemPainted( const QModelIndex& index );
void recreateActionWidgets();

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

#endif

0 comments on commit e985f2c

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