Skip to content

Commit a6c88c1

Browse files
committed
Add different widget style
1 parent 8b4cb04 commit a6c88c1

10 files changed

+183
-23
lines changed

python/core/qgsattributetableconfig.sip

+29
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ class QgsAttributeTableConfig
4444
bool mHidden; //!< Flag that controls if the column is hidden
4545
};
4646

47+
/**
48+
* The style of the action widget in the attribute table.
49+
*/
50+
enum ActionWidgetStyle
51+
{
52+
ButtonList, //!< A list of buttons
53+
DropDown //!< A tool button with a dropdown to select the current action
54+
};
55+
4756
QgsAttributeTableConfig();
4857

4958
/**
@@ -66,6 +75,26 @@ class QgsAttributeTableConfig
6675
*/
6776
void update( const QgsFields& fields );
6877

78+
/**
79+
* Returns true if the action widget is visible
80+
*/
81+
bool actionWidgetVisible() const;
82+
83+
/**
84+
* Set if the action widget is visible
85+
*/
86+
void setActionWidgetVisible( bool visible );
87+
88+
/**
89+
* Get the style of the action widget
90+
*/
91+
ActionWidgetStyle actionWidgetStyle() const;
92+
93+
/**
94+
* Set the style of the action widget
95+
*/
96+
void setActionWidgetStyle( const ActionWidgetStyle& actionWidgetStyle );
97+
6998
/**
7099
* Serialize to XML on layer save
71100
*/

src/app/qgsattributeactiondialog.cpp

+19-2
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ QgsAttributeActionDialog::QgsAttributeActionDialog( const QgsActionManager& acti
5656
connect( mAddButton, SIGNAL( clicked( bool ) ), this, SLOT( insert() ) );
5757
connect( mAddDefaultActionsButton, SIGNAL( clicked() ), this, SLOT( addDefaultActions() ) );
5858

59-
init( actions );
59+
init( actions, mLayer->attributeTableConfig() );
6060
}
6161

62-
void QgsAttributeActionDialog::init( const QgsActionManager& actions )
62+
void QgsAttributeActionDialog::init( const QgsActionManager& actions, const QgsAttributeTableConfig& attributeTableConfig )
6363
{
6464
// Start from a fresh slate.
6565
mAttributeActionTable->setRowCount( 0 );
@@ -72,6 +72,13 @@ void QgsAttributeActionDialog::init( const QgsActionManager& actions )
7272
}
7373

7474
updateButtons();
75+
76+
QgsAttributeTableConfig::ColumnConfig visibleActionWidgetConfig = QgsAttributeTableConfig::ColumnConfig();
77+
visibleActionWidgetConfig.mType = QgsAttributeTableConfig::Action;
78+
visibleActionWidgetConfig.mHidden = false;
79+
80+
mShowInAttributeTable->setChecked( attributeTableConfig.actionWidgetVisible() );
81+
mAttributeTableWidgetType->setCurrentIndex( attributeTableConfig.actionWidgetStyle() );
7582
}
7683

7784
QList<QgsAction> QgsAttributeActionDialog::actions() const
@@ -86,6 +93,16 @@ QList<QgsAction> QgsAttributeActionDialog::actions() const
8693
return actions;
8794
}
8895

96+
bool QgsAttributeActionDialog::showWidgetInAttributeTable() const
97+
{
98+
return mShowInAttributeTable->isChecked();
99+
}
100+
101+
QgsAttributeTableConfig::ActionWidgetStyle QgsAttributeActionDialog::attributeTableWidgetStyle() const
102+
{
103+
return static_cast<QgsAttributeTableConfig::ActionWidgetStyle>( mAttributeTableWidgetType->currentIndex() );
104+
}
105+
89106
void QgsAttributeActionDialog::insertRow( int row, const QgsAction& action )
90107
{
91108
QTableWidgetItem* item;

src/app/qgsattributeactiondialog.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ back to QgsVectorLayer.
2626
#include "ui_qgsattributeactiondialogbase.h"
2727
#include "qgsactionmanager.h"
2828
#include "qgsfield.h"
29+
#include "qgsattributetableconfig.h"
2930
#include <QMap>
3031

3132
class APP_EXPORT QgsAttributeActionDialog: public QWidget, private Ui::QgsAttributeActionDialogBase
@@ -48,11 +49,13 @@ class APP_EXPORT QgsAttributeActionDialog: public QWidget, private Ui::QgsAttrib
4849

4950
~QgsAttributeActionDialog() {}
5051

51-
void init( const QgsActionManager& action );
52+
void init( const QgsActionManager& action , const QgsAttributeTableConfig& attributeTableConfig );
5253

5354
QList<QgsAction> actions() const;
5455

55-
void apply();
56+
bool showWidgetInAttributeTable() const;
57+
58+
QgsAttributeTableConfig::ActionWidgetStyle attributeTableWidgetStyle() const;
5659

5760
private slots:
5861
void moveUp();

src/app/qgsvectorlayerproperties.cpp

+16-1
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ void QgsVectorLayerProperties::syncToLayer()
449449
// load appropriate symbology page (V1 or V2)
450450
updateSymbologyPage();
451451

452-
mActionDialog->init( *mLayer->actions() );
452+
mActionDialog->init( *mLayer->actions(), mLayer->attributeTableConfig() );
453453

454454
if ( labelingDialog )
455455
labelingDialog->adaptToLayer();
@@ -558,6 +558,21 @@ void QgsVectorLayerProperties::apply()
558558
{
559559
mLayer->actions()->addAction( action );
560560
}
561+
QgsAttributeTableConfig attributeTableConfig = mLayer->attributeTableConfig();
562+
attributeTableConfig.setActionWidgetStyle( mActionDialog->attributeTableWidgetStyle() );
563+
QVector<QgsAttributeTableConfig::ColumnConfig> columns = attributeTableConfig.columns();
564+
565+
for ( int i = 0; i < columns.size(); ++i )
566+
{
567+
if ( columns.at( i ).mType == QgsAttributeTableConfig::Action )
568+
{
569+
columns[i].mHidden = !mActionDialog->showWidgetInAttributeTable();
570+
}
571+
}
572+
573+
attributeTableConfig.setColumns( columns );
574+
575+
mLayer->setAttributeTableConfig( attributeTableConfig );
561576

562577
Q_NOWARN_DEPRECATED_PUSH
563578
if ( mOptsPage_LabelsOld )

src/core/qgsattributetableconfig.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,37 @@ void QgsAttributeTableConfig::update( const QgsFields& fields )
8282
}
8383
}
8484

85+
bool QgsAttributeTableConfig::actionWidgetVisible() const
86+
{
87+
Q_FOREACH ( const ColumnConfig& columnConfig, mColumns )
88+
{
89+
if ( columnConfig.mType == Action && columnConfig.mHidden == false )
90+
return true;
91+
}
92+
return false;
93+
}
94+
95+
void QgsAttributeTableConfig::setActionWidgetVisible( bool visible )
96+
{
97+
for ( int i = 0; i < mColumns.size(); ++i )
98+
{
99+
if ( mColumns.at( i ).mType == Action )
100+
{
101+
mColumns[i].mHidden = !visible;
102+
}
103+
}
104+
}
105+
106+
QgsAttributeTableConfig::ActionWidgetStyle QgsAttributeTableConfig::actionWidgetStyle() const
107+
{
108+
return mActionWidgetStyle;
109+
}
110+
111+
void QgsAttributeTableConfig::setActionWidgetStyle( const ActionWidgetStyle& actionWidgetStyle )
112+
{
113+
mActionWidgetStyle = actionWidgetStyle;
114+
}
115+
85116

86117
void QgsAttributeTableConfig::readXml( const QDomNode& node )
87118
{
@@ -115,13 +146,19 @@ void QgsAttributeTableConfig::readXml( const QDomNode& node )
115146
mColumns.append( column );
116147
}
117148
}
149+
150+
if ( configNode.toElement().attribute( "actionWidgetStyle" ) == "buttonList" )
151+
mActionWidgetStyle = ButtonList;
152+
else
153+
mActionWidgetStyle = DropDown;
118154
}
119155

120156
void QgsAttributeTableConfig::writeXml( QDomNode& node ) const
121157
{
122158
QDomDocument doc( node.ownerDocument() );
123159

124160
QDomElement configElement = doc.createElement( "attributetableconfig" );
161+
configElement.setAttribute( "actionWidgetStyle", mActionWidgetStyle == ButtonList ? "buttonList" : "dropDown" );
125162

126163
QDomElement columnsElement = doc.createElement( "columns" );
127164

src/core/qgsattributetableconfig.h

+31
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ class CORE_EXPORT QgsAttributeTableConfig
4949
bool mHidden; //!< Flag that controls if the column is hidden
5050
};
5151

52+
/**
53+
* The style of the action widget in the attribute table.
54+
*/
55+
enum ActionWidgetStyle
56+
{
57+
ButtonList, //!< A list of buttons
58+
DropDown //!< A tool button with a dropdown to select the current action
59+
};
60+
5261
QgsAttributeTableConfig();
5362

5463
/**
@@ -71,6 +80,26 @@ class CORE_EXPORT QgsAttributeTableConfig
7180
*/
7281
void update( const QgsFields& fields );
7382

83+
/**
84+
* Returns true if the action widget is visible
85+
*/
86+
bool actionWidgetVisible() const;
87+
88+
/**
89+
* Set if the action widget is visible
90+
*/
91+
void setActionWidgetVisible( bool visible );
92+
93+
/**
94+
* Get the style of the action widget
95+
*/
96+
ActionWidgetStyle actionWidgetStyle() const;
97+
98+
/**
99+
* Set the style of the action widget
100+
*/
101+
void setActionWidgetStyle( const ActionWidgetStyle& actionWidgetStyle );
102+
74103
/**
75104
* Serialize to XML on layer save
76105
*/
@@ -81,8 +110,10 @@ class CORE_EXPORT QgsAttributeTableConfig
81110
*/
82111
void readXml( const QDomNode& node );
83112

113+
84114
private:
85115
QVector<ColumnConfig> mColumns;
116+
ActionWidgetStyle mActionWidgetStyle;
86117
};
87118

88119
Q_DECLARE_METATYPE( QgsAttributeTableConfig::ColumnConfig )

src/gui/attributetable/qgsattributetableview.cpp

+35-10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <QHeaderView>
1919
#include <QMenu>
2020
#include <QToolButton>
21+
#include <QHBoxLayout>
2122

2223
#include "qgsactionmanager.h"
2324
#include "qgsattributetableview.h"
@@ -122,6 +123,7 @@ void QgsAttributeTableView::setModel( QgsAttributeTableFilterModel* filterModel
122123

123124
mActionWidget = createActionWidget( 0 );
124125
mActionWidget->setVisible( false );
126+
updateActionImage( mActionWidget );
125127
}
126128

127129
void QgsAttributeTableView::setFeatureSelectionManager( QgsIFeatureSelectionManager* featureSelectionManager )
@@ -137,11 +139,25 @@ void QgsAttributeTableView::setFeatureSelectionManager( QgsIFeatureSelectionMana
137139

138140
QWidget* QgsAttributeTableView::createActionWidget( QgsFeatureId fid )
139141
{
140-
QToolButton* toolButton = new QToolButton( this );
141-
toolButton->setPopupMode( QToolButton::MenuButtonPopup );
142-
142+
QgsAttributeTableConfig attributeTableConfig = mFilterModel->layer()->attributeTableConfig();
143143
QgsActionManager* actions = mFilterModel->layer()->actions();
144144

145+
QToolButton* toolButton = nullptr;
146+
QWidget* container = nullptr;
147+
148+
if ( attributeTableConfig.actionWidgetStyle() == QgsAttributeTableConfig::DropDown )
149+
{
150+
toolButton = new QToolButton( this );
151+
toolButton->setPopupMode( QToolButton::MenuButtonPopup );
152+
container = toolButton;
153+
}
154+
else
155+
{
156+
container = new QWidget( this );
157+
container->setLayout( new QHBoxLayout() );
158+
container->layout()->setMargin( 0 );
159+
}
160+
145161
for ( int i = 0; i < actions->size(); ++i )
146162
{
147163
const QgsAction& action = actions->at( i );
@@ -156,18 +172,27 @@ QWidget* QgsAttributeTableView::createActionWidget( QgsFeatureId fid )
156172

157173
connect( act, SIGNAL( triggered( bool ) ), this, SLOT( actionTriggered() ) );
158174

159-
toolButton->addAction( act );
175+
if ( attributeTableConfig.actionWidgetStyle() == QgsAttributeTableConfig::DropDown )
176+
{
177+
toolButton->addAction( act );
178+
179+
if ( actions->defaultAction() == i )
180+
toolButton->setDefaultAction( act );
160181

161-
if ( actions->defaultAction() == i )
162-
toolButton->setDefaultAction( act );
182+
container = toolButton;
183+
}
184+
else
185+
{
186+
QToolButton* btn = new QToolButton;
187+
btn->setDefaultAction( act );
188+
container->layout()->addWidget( btn );
189+
}
163190
}
164191

165-
if ( !toolButton->actions().isEmpty() && actions->defaultAction() == -1 )
192+
if ( toolButton && !toolButton->actions().isEmpty() && actions->defaultAction() == -1 )
166193
toolButton->setDefaultAction( toolButton->actions().first() );
167194

168-
updateActionImage( toolButton );
169-
170-
return toolButton;
195+
return container;
171196
}
172197

173198
void QgsAttributeTableView::closeEvent( QCloseEvent *e )

src/gui/attributetable/qgsorganizetablecolumnsdialog.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ QgsOrganizeTableColumnsDialog::QgsOrganizeTableColumnsDialog( const QgsVectorLay
4747
setupUi( this );
4848
if ( vl )
4949
{
50-
QgsAttributeTableConfig config = vl->attributeTableConfig();
51-
config.update( vl->fields() );
50+
mConfig = vl->attributeTableConfig();
51+
mConfig.update( vl->fields() );
5252

5353
mFieldsList->clear();
5454

55-
Q_FOREACH ( const QgsAttributeTableConfig::ColumnConfig& columnConfig, config.columns() )
55+
Q_FOREACH ( const QgsAttributeTableConfig::ColumnConfig& columnConfig, mConfig.columns() )
5656
{
5757
QListWidgetItem* item;
5858
if ( columnConfig.mType == QgsAttributeTableConfig::Action )
@@ -110,7 +110,7 @@ QgsAttributeTableConfig QgsOrganizeTableColumnsDialog::config() const
110110
columns.append( columnConfig );
111111
}
112112

113-
QgsAttributeTableConfig config;
113+
QgsAttributeTableConfig config = mConfig;
114114
config.setColumns( columns );
115115
return config;
116116
}

src/gui/attributetable/qgsorganizetablecolumnsdialog.h

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ class GUI_EXPORT QgsOrganizeTableColumnsDialog : public QDialog, private Ui::Qgs
4747
* Get the updated configuration
4848
*/
4949
QgsAttributeTableConfig config() const;
50+
51+
private:
52+
QgsAttributeTableConfig mConfig;
5053
};
5154

5255
#endif

src/ui/qgsattributeactiondialogbase.ui

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>609</width>
9+
<width>653</width>
1010
<height>731</height>
1111
</rect>
1212
</property>
@@ -227,15 +227,15 @@
227227
</widget>
228228
</item>
229229
<item row="0" column="1">
230-
<widget class="QComboBox" name="comboBox">
230+
<widget class="QComboBox" name="mAttributeTableWidgetType">
231231
<item>
232232
<property name="text">
233-
<string>Combo Box</string>
233+
<string>Separate Buttons</string>
234234
</property>
235235
</item>
236236
<item>
237237
<property name="text">
238-
<string>Separate Buttons</string>
238+
<string>Combo Box</string>
239239
</property>
240240
</item>
241241
</widget>

0 commit comments

Comments
 (0)