Skip to content

Commit c789c62

Browse files
committed
Store the add feature method in the attribute table configuration to be able to have the default per layer (you still can change it anytime while working)
1 parent b356133 commit c789c62

File tree

6 files changed

+149
-61
lines changed

6 files changed

+149
-61
lines changed

python/PyQt6/core/auto_additions/qgsattributetableconfig.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33
QgsAttributeTableConfig.Action = QgsAttributeTableConfig.Type.Action
44
QgsAttributeTableConfig.ButtonList = QgsAttributeTableConfig.ActionWidgetStyle.ButtonList
55
QgsAttributeTableConfig.DropDown = QgsAttributeTableConfig.ActionWidgetStyle.DropDown
6+
# monkey patching scoped based enum
7+
QgsAttributeTableConfig.AddFeatureMethod.None.__doc__ = "No mod e set yet in the current layer"
8+
QgsAttributeTableConfig.AddFeatureMethod.Form.__doc__ = "Opens a new Attributeform-Dialog"
9+
QgsAttributeTableConfig.AddFeatureMethod.Table.__doc__ = "Adds a new row (or a form embedded in the attribute table depending on the view)"
10+
QgsAttributeTableConfig.AddFeatureMethod.__doc__ = """The way how to add features in the attribute table
11+
12+
* ``None``: No mod e set yet in the current layer
13+
* ``Form``: Opens a new Attributeform-Dialog
14+
* ``Table``: Adds a new row (or a form embedded in the attribute table depending on the view)
15+
16+
"""
17+
# --
618
try:
719
QgsAttributeTableConfig.ColumnConfig.__attribute_docs__ = {'type': 'The type of this column.', 'name': 'The name of the attribute if this column represents a field', 'hidden': 'Flag that controls if the column is hidden', 'width': 'Width of column, or -1 for default width'}
820
QgsAttributeTableConfig.ColumnConfig.__annotations__ = {'type': 'QgsAttributeTableConfig.Type', 'name': str, 'hidden': bool, 'width': int}

python/PyQt6/core/auto_generated/qgsattributetableconfig.sip.in

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ The configuration is specific for one vector layer.
4848
DropDown
4949
};
5050

51+
enum class AddFeatureMethod
52+
{
53+
None,
54+
Form,
55+
Table
56+
};
57+
5158
QgsAttributeTableConfig();
5259

5360
QVector<QgsAttributeTableConfig::ColumnConfig> columns() const;
@@ -139,6 +146,26 @@ Gets the expression used for sorting.
139146
Set the sort expression used for sorting.
140147
%End
141148

149+
AddFeatureMethod addFeatureMethod() const;
150+
%Docstring
151+
Gets the addFeatureMethod that defines how features are added (single
152+
form or embedded in a table).
153+
154+
.. seealso:: :py:func:`setAddFeatureMethod`
155+
156+
.. versionadded:: 4.2
157+
%End
158+
159+
void setAddFeatureMethod( const AddFeatureMethod addFeatureMethod );
160+
%Docstring
161+
Sets the ``addFeatureMethod`` that defines how features are added
162+
(single form or embedded in a table).
163+
164+
.. seealso:: :py:func:`addFeatureMethod`
165+
166+
.. versionadded:: 4.2
167+
%End
168+
142169

143170
int columnWidth( int column ) const;
144171
%Docstring

src/app/qgsattributetabledialog.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,16 @@ QgsAttributeTableDialog::QgsAttributeTableDialog( QgsVectorLayer *layer, QgsAttr
166166
mAddFeatureButton->setPopupMode( QToolButton::MenuButtonPopup );
167167
mAddFeatureButton->addAction( mActionAddFeature );
168168
mAddFeatureButton->addAction( mActionAddFeatureViaAttributeForm );
169+
170+
//set the add feature method according to the layer settings or as fall back use the last one
171+
QgsAttributeTableConfig::AddFeatureMethod method = mLayer->attributeTableConfig().addFeatureMethod();
172+
if ( method == QgsAttributeTableConfig::AddFeatureMethod::None )
173+
{
174+
const QString lastMethod = settings.value( u"/qgis/attributeTableLastAddFeatureMethod"_s ).toString();
175+
method = ( lastMethod == "attributeForm"_L1 ) ? QgsAttributeTableConfig::AddFeatureMethod::Form : QgsAttributeTableConfig::AddFeatureMethod::Table;
176+
}
169177
mAddFeatureButton->setDefaultAction(
170-
settings.value( u"/qgis/attributeTableLastAddFeatureMethod"_s ) == u"attributeForm"_s
178+
( method == QgsAttributeTableConfig::AddFeatureMethod::Form )
171179
? mActionAddFeatureViaAttributeForm
172180
: mActionAddFeature
173181
);
@@ -722,9 +730,13 @@ void QgsAttributeTableDialog::mActionAddFeatureViaAttributeTable_triggered()
722730
if ( !mLayer->isEditable() )
723731
return;
724732

733+
//remember as last used mode ...
725734
QgsSettings s;
726735
s.setValue( u"/qgis/attributeTableLastAddFeatureMethod"_s, u"attributeTable"_s );
736+
//... change the button's action ...
727737
mAddFeatureButton->setDefaultAction( mActionAddFeature );
738+
//... and set for the current layer
739+
mLayer->attributeTableConfig().setAddFeatureMethod( QgsAttributeTableConfig::AddFeatureMethod::Table );
728740

729741
QgsAttributeTableModel *masterModel = mMainView->masterModel();
730742

@@ -754,9 +766,13 @@ void QgsAttributeTableDialog::mActionAddFeatureViaAttributeForm_triggered()
754766
if ( !mLayer->isEditable() )
755767
return;
756768

769+
//remember as last used mode ...
757770
QgsSettings s;
758771
s.setValue( u"/qgis/attributeTableLastAddFeatureMethod"_s, u"attributeForm"_s );
772+
//... change the button's action ...
759773
mAddFeatureButton->setDefaultAction( mActionAddFeatureViaAttributeForm );
774+
//... and set for the current layer
775+
mLayer->attributeTableConfig().setAddFeatureMethod( QgsAttributeTableConfig::AddFeatureMethod::Form );
760776

761777
QgsFeature f;
762778
QgsFeatureAction action( tr( "Feature Added" ), f, mLayer, QUuid(), -1, this );

src/core/qgsattributetableconfig.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ void QgsAttributeTableConfig::readXml( const QDomNode &node )
183183
mActionWidgetStyle = ButtonList;
184184
else
185185
mActionWidgetStyle = DropDown;
186+
187+
if ( configNode.toElement().attribute( u"addFeatureMethod"_s ) == "form"_L1 )
188+
mAddFeatureMethod = AddFeatureMethod::Form;
189+
else if ( configNode.toElement().attribute( u"addFeatureMethod"_s ) == "table"_L1 )
190+
mAddFeatureMethod = AddFeatureMethod::Table;
186191
}
187192
else
188193
{
@@ -221,6 +226,16 @@ void QgsAttributeTableConfig::setSortExpression( const QString &sortExpression )
221226
mSortExpression = sortExpression;
222227
}
223228

229+
QgsAttributeTableConfig::AddFeatureMethod QgsAttributeTableConfig::addFeatureMethod() const
230+
{
231+
return mAddFeatureMethod;
232+
}
233+
234+
void QgsAttributeTableConfig::setAddFeatureMethod( const AddFeatureMethod addFeatureMethod )
235+
{
236+
mAddFeatureMethod = addFeatureMethod;
237+
}
238+
224239
int QgsAttributeTableConfig::columnWidth( int column ) const
225240
{
226241
return mColumns.at( column ).width;
@@ -243,7 +258,7 @@ void QgsAttributeTableConfig::setColumnHidden( int column, bool hidden )
243258

244259
bool QgsAttributeTableConfig::operator!=( const QgsAttributeTableConfig &other ) const
245260
{
246-
return mSortExpression != other.mSortExpression || mColumns != other.mColumns || mActionWidgetStyle != other.mActionWidgetStyle || mSortOrder != other.mSortOrder;
261+
return mSortExpression != other.mSortExpression || mColumns != other.mColumns || mActionWidgetStyle != other.mActionWidgetStyle || mSortOrder != other.mSortOrder || mAddFeatureMethod != other.mAddFeatureMethod;
247262
}
248263

249264
Qt::SortOrder QgsAttributeTableConfig::sortOrder() const
@@ -273,6 +288,8 @@ void QgsAttributeTableConfig::writeXml( QDomNode &node ) const
273288

274289
configElement.setAttribute( u"sortOrder"_s, mSortOrder );
275290

291+
configElement.setAttribute( u"addFeatureMethod"_s, mAddFeatureMethod == AddFeatureMethod::Form ? "form" : "table" );
292+
276293
QDomElement columnsElement = doc.createElement( u"columns"_s );
277294

278295
const auto constMColumns = mColumns;

src/core/qgsattributetableconfig.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ class CORE_EXPORT QgsAttributeTableConfig
7777
DropDown //!< A tool button with a drop-down to select the current action
7878
};
7979

80+
/**
81+
* The way how to add features in the attribute table
82+
*/
83+
enum class AddFeatureMethod
84+
{
85+
None, //!< No mod e set yet in the current layer
86+
Form, //!< Opens a new Attributeform-Dialog
87+
Table //!< Adds a new row (or a form embedded in the attribute table depending on the view)
88+
};
89+
8090
QgsAttributeTableConfig() = default;
8191

8292
/**
@@ -167,6 +177,20 @@ class CORE_EXPORT QgsAttributeTableConfig
167177
*/
168178
void setSortExpression( const QString &sortExpression );
169179

180+
/**
181+
* Gets the addFeatureMethod that defines how features are added (single form or embedded in a table).
182+
* \see setAddFeatureMethod()
183+
* \since QGIS 4.2
184+
*/
185+
AddFeatureMethod addFeatureMethod() const;
186+
187+
/**
188+
* Sets the \a addFeatureMethod that defines how features are added (single form or embedded in a table).
189+
* \see addFeatureMethod()
190+
* \since QGIS 4.2
191+
*/
192+
void setAddFeatureMethod( const AddFeatureMethod addFeatureMethod );
193+
170194
#ifndef SIP_RUN
171195

172196
/**
@@ -318,6 +342,8 @@ class CORE_EXPORT QgsAttributeTableConfig
318342
ActionWidgetStyle mActionWidgetStyle = DropDown;
319343
QString mSortExpression;
320344
Qt::SortOrder mSortOrder = Qt::AscendingOrder;
345+
AddFeatureMethod mAddFeatureMethod = AddFeatureMethod::None;
346+
321347
};
322348

323349
Q_DECLARE_METATYPE( QgsAttributeTableConfig::ColumnConfig )

src/ui/qgsattributetabledialog.ui

Lines changed: 49 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,6 @@
2929
<property name="spacing">
3030
<number>3</number>
3131
</property>
32-
<item row="6" column="0">
33-
<widget class="QgsDualView" name="mMainView">
34-
<property name="currentIndex">
35-
<number>-1</number>
36-
</property>
37-
</widget>
38-
</item>
3932
<item row="5" column="0">
4033
<widget class="QFrame" name="mUpdateExpressionBox">
4134
<layout class="QHBoxLayout" name="horizontalLayout_3">
@@ -79,7 +72,7 @@
7972
</sizepolicy>
8073
</property>
8174
<property name="focusPolicy">
82-
<enum>Qt::StrongFocus</enum>
75+
<enum>Qt::FocusPolicy::StrongFocus</enum>
8376
</property>
8477
</widget>
8578
</item>
@@ -100,6 +93,53 @@
10093
</layout>
10194
</widget>
10295
</item>
96+
<item row="6" column="0">
97+
<widget class="QgsDualView" name="mMainView">
98+
<property name="currentIndex">
99+
<number>-1</number>
100+
</property>
101+
</widget>
102+
</item>
103+
<item row="4" column="0">
104+
<widget class="QToolBar" name="mToolbar">
105+
<property name="iconSize">
106+
<size>
107+
<width>18</width>
108+
<height>18</height>
109+
</size>
110+
</property>
111+
<property name="floatable">
112+
<bool>false</bool>
113+
</property>
114+
<addaction name="mActionToggleEditing"/>
115+
<addaction name="mActionToggleMultiEdit"/>
116+
<addaction name="mActionSaveEdits"/>
117+
<addaction name="mActionReload"/>
118+
<addaction name="separator"/>
119+
<addaction name="mActionAddFeature"/>
120+
<addaction name="mActionDeleteSelected"/>
121+
<addaction name="mActionCutSelectedRows"/>
122+
<addaction name="mActionCopySelectedRows"/>
123+
<addaction name="mActionPasteFeatures"/>
124+
<addaction name="separator"/>
125+
<addaction name="mActionExpressionSelect"/>
126+
<addaction name="mActionSelectAll"/>
127+
<addaction name="mActionInvertSelection"/>
128+
<addaction name="mActionRemoveSelection"/>
129+
<addaction name="mActionSearchForm"/>
130+
<addaction name="mActionSelectedToTop"/>
131+
<addaction name="mActionPanMapToSelectedRows"/>
132+
<addaction name="mActionZoomMapToSelectedRows"/>
133+
<addaction name="separator"/>
134+
<addaction name="mActionAddAttribute"/>
135+
<addaction name="mActionRemoveAttribute"/>
136+
<addaction name="mActionOrganizeColumns"/>
137+
<addaction name="mActionOpenFieldCalculator"/>
138+
<addaction name="separator"/>
139+
<addaction name="mActionSetStyles"/>
140+
<addaction name="separator"/>
141+
</widget>
142+
</item>
103143
<item row="8" column="0">
104144
<layout class="QHBoxLayout">
105145
<property name="leftMargin">
@@ -175,46 +215,6 @@
175215
</item>
176216
</layout>
177217
</item>
178-
<item row="4" column="0">
179-
<widget class="QToolBar" name="mToolbar">
180-
<property name="iconSize">
181-
<size>
182-
<width>18</width>
183-
<height>18</height>
184-
</size>
185-
</property>
186-
<property name="floatable">
187-
<bool>false</bool>
188-
</property>
189-
<addaction name="mActionToggleEditing"/>
190-
<addaction name="mActionToggleMultiEdit"/>
191-
<addaction name="mActionSaveEdits"/>
192-
<addaction name="mActionReload"/>
193-
<addaction name="separator"/>
194-
<addaction name="mActionAddFeature"/>
195-
<addaction name="mActionDeleteSelected"/>
196-
<addaction name="mActionCutSelectedRows"/>
197-
<addaction name="mActionCopySelectedRows"/>
198-
<addaction name="mActionPasteFeatures"/>
199-
<addaction name="separator"/>
200-
<addaction name="mActionExpressionSelect"/>
201-
<addaction name="mActionSelectAll"/>
202-
<addaction name="mActionInvertSelection"/>
203-
<addaction name="mActionRemoveSelection"/>
204-
<addaction name="mActionSearchForm"/>
205-
<addaction name="mActionSelectedToTop"/>
206-
<addaction name="mActionPanMapToSelectedRows"/>
207-
<addaction name="mActionZoomMapToSelectedRows"/>
208-
<addaction name="separator"/>
209-
<addaction name="mActionAddAttribute"/>
210-
<addaction name="mActionRemoveAttribute"/>
211-
<addaction name="mActionOrganizeColumns"/>
212-
<addaction name="mActionOpenFieldCalculator"/>
213-
<addaction name="separator"/>
214-
<addaction name="mActionSetStyles"/>
215-
<addaction name="separator"/>
216-
</widget>
217-
</item>
218218
</layout>
219219
<action name="mActionSearchForm">
220220
<property name="checkable">
@@ -297,7 +297,7 @@
297297
<normaloff>:/images/themes/default/mActionNewTableRow.svg</normaloff>:/images/themes/default/mActionNewTableRow.svg</iconset>
298298
</property>
299299
<property name="text">
300-
<string>Add feature</string>
300+
<string>Add feature via attribute table</string>
301301
</property>
302302
</action>
303303
<action name="mActionDeleteSelected">
@@ -516,15 +516,6 @@
516516
<string>Organize Columns</string>
517517
</property>
518518
</action>
519-
<action name="mActionAddFeatureViaAttributeTable">
520-
<property name="icon">
521-
<iconset resource="../../images/images.qrc">
522-
<normaloff>:/images/themes/default/mActionNewTableRow.svg</normaloff>:/images/themes/default/mActionNewTableRow.svg</iconset>
523-
</property>
524-
<property name="text">
525-
<string>Add feature via attribute table</string>
526-
</property>
527-
</action>
528519
<action name="mActionAddFeatureViaAttributeForm">
529520
<property name="icon">
530521
<iconset resource="../../images/images.qrc">
@@ -573,7 +564,6 @@
573564
</tabstops>
574565
<resources>
575566
<include location="../../images/images.qrc"/>
576-
<include location="../../images/images.qrc"/>
577567
</resources>
578568
<connections/>
579569
<buttongroups>

0 commit comments

Comments
 (0)