QGIS API Documentation 4.1.0-Master (64dc32379c2)
Loading...
Searching...
No Matches
qgsattributetableconfig.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsattributetableconfig.cpp - QgsAttributeTableConfig
3
4 ---------------------
5 begin : 27.4.2016
6 copyright : (C) 2016 by mku
7 email : [your-email-here]
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
17
18#include "qgsfields.h"
19
20#include <QString>
21#include <QStringList>
22
23#include "moc_qgsattributetableconfig.cpp"
24
25using namespace Qt::StringLiterals;
26
27QVector<QgsAttributeTableConfig::ColumnConfig> QgsAttributeTableConfig::columns() const
28{
29 return mColumns;
30}
31
33{
34 return mColumns.isEmpty();
35}
36
38{
39 return mColumns.size();
40}
41
43{
44 for ( int i = 0; i < mColumns.size(); ++i )
45 {
46 if ( mColumns.at( i ).hidden )
47 {
48 visibleColumn++;
49 continue;
50 }
51 if ( visibleColumn == i )
52 return i;
53 }
54 return -1;
55}
56
57void QgsAttributeTableConfig::setColumns( const QVector<ColumnConfig> &columns )
58{
59 mColumns = columns;
60}
61
63{
64 QStringList columns;
65
66 bool containsActionColumn = false;
67
68 for ( int i = mColumns.count() - 1; i >= 0; --i )
69 {
70 const ColumnConfig &column = mColumns.at( i );
71 if ( column.type == Field )
72 {
73 if ( fields.indexOf( column.name ) == -1 )
74 {
75 mColumns.remove( i );
76 }
77 else
78 {
79 columns.append( column.name );
80 }
81 }
82 else if ( column.type == Action )
83 {
84 containsActionColumn = true;
85 }
86 }
87
88 for ( const auto &field : fields )
89 {
90 if ( !columns.contains( field.name() ) )
91 {
92 ColumnConfig newColumn;
93 newColumn.hidden = false;
94 newColumn.type = Field;
95 newColumn.name = field.name();
96 if ( containsActionColumn )
97 {
98 mColumns.insert( mColumns.size() - 1, newColumn );
99 }
100 else
101 {
102 mColumns.append( newColumn );
103 }
104 }
105 }
106
107 if ( !containsActionColumn )
108 {
109 ColumnConfig actionConfig;
110
111 actionConfig.type = Action;
112 actionConfig.hidden = true;
113
114 mColumns.append( actionConfig );
115 }
116}
117
119{
120 const auto constMColumns = mColumns;
121 for ( const ColumnConfig &columnConfig : constMColumns )
122 {
123 if ( columnConfig.type == Action && !columnConfig.hidden )
124 return true;
125 }
126 return false;
127}
128
130{
131 for ( int i = 0; i < mColumns.size(); ++i )
132 {
133 if ( mColumns.at( i ).type == Action )
134 {
135 mColumns[i].hidden = !visible;
136 }
137 }
138}
139
144
149
150
151void QgsAttributeTableConfig::readXml( const QDomNode &node )
152{
153 mColumns.clear();
154
155 const QDomNode configNode = node.namedItem( u"attributetableconfig"_s );
156 if ( !configNode.isNull() )
157 {
158 const QDomNode columnsNode = configNode.toElement().namedItem( u"columns"_s );
159
160 const QDomNodeList columns = columnsNode.childNodes();
161
162 for ( int i = 0; i < columns.size(); ++i )
163 {
164 const QDomElement columnElement = columns.at( i ).toElement();
165
166 ColumnConfig column;
167
168 if ( columnElement.attribute( u"type"_s ) == "actions"_L1 )
169 {
170 column.type = Action;
171 }
172 else
173 {
174 column.type = Field;
175 column.name = columnElement.attribute( u"name"_s );
176 }
177
178 column.hidden = columnElement.attribute( u"hidden"_s ) == "1"_L1;
179 column.width = columnElement.attribute( u"width"_s, u"-1"_s ).toDouble();
180
181 mColumns.append( column );
182 }
183
184 if ( configNode.toElement().attribute( u"actionWidgetStyle"_s ) == "buttonList"_L1 )
185 mActionWidgetStyle = ButtonList;
186 else
187 mActionWidgetStyle = DropDown;
188
189 mAddFeatureMethod = qgsEnumKeyToValue( configNode.toElement().attribute( u"addFeatureMethod"_s ), AddFeatureMethod::Unset );
190 }
191 else
192 {
193 // Before QGIS 2.16 the attribute table would hide "Hidden" widgets.
194 // They are migrated to hidden columns here.
195 const QDomNodeList editTypeNodes = node.namedItem( u"edittypes"_s ).childNodes();
196
197 for ( int i = 0; i < editTypeNodes.size(); i++ )
198 {
199 const QDomElement editTypeElement = editTypeNodes.at( i ).toElement();
200
201 if ( editTypeElement.attribute( u"widgetv2type"_s ) == "Hidden"_L1 )
202 {
203 ColumnConfig column;
204
205 column.name = editTypeElement.attribute( u"name"_s );
206 column.hidden = true;
207 column.type = Field;
208 mColumns.append( column );
209 }
210 }
211 }
212
213 mSortExpression = configNode.toElement().attribute( u"sortExpression"_s );
214 const Qt::SortOrder sortOrder = static_cast<Qt::SortOrder>( configNode.toElement().attribute( u"sortOrder"_s ).toInt() );
216}
217
219{
220 return mSortExpression;
221}
222
224{
225 mSortExpression = sortExpression;
226}
227
232
237
239{
240 return mColumns.at( column ).width;
241}
242
243void QgsAttributeTableConfig::setColumnWidth( int column, int width )
244{
245 mColumns[column].width = width;
246}
247
249{
250 return mColumns.at( column ).hidden;
251}
252
253void QgsAttributeTableConfig::setColumnHidden( int column, bool hidden )
254{
255 mColumns[column].hidden = hidden;
256}
257
259{
260 return mSortExpression != other.mSortExpression
261 || mColumns != other.mColumns
262 || mActionWidgetStyle != other.mActionWidgetStyle
263 || mSortOrder != other.mSortOrder
264 || mAddFeatureMethod != other.mAddFeatureMethod;
265}
266
268{
269 return mSortOrder;
270}
271
273{
274 // fix https://hub.qgis.org/issues/15803
275 if ( sortOrder != Qt::AscendingOrder && sortOrder != Qt::DescendingOrder )
276 {
277 sortOrder = Qt::AscendingOrder;
278 }
279
280 mSortOrder = sortOrder;
281}
282
283void QgsAttributeTableConfig::writeXml( QDomNode &node ) const
284{
285 QDomDocument doc( node.ownerDocument() );
286
287 QDomElement configElement = doc.createElement( u"attributetableconfig"_s );
288 configElement.setAttribute( u"actionWidgetStyle"_s, mActionWidgetStyle == ButtonList ? "buttonList" : "dropDown" );
289
290 configElement.setAttribute( u"sortExpression"_s, mSortExpression );
291
292 configElement.setAttribute( u"sortOrder"_s, mSortOrder );
293
294 if ( mAddFeatureMethod != AddFeatureMethod::Unset )
295 configElement.setAttribute( u"addFeatureMethod"_s, qgsEnumValueToKey( mAddFeatureMethod ) );
296
297 QDomElement columnsElement = doc.createElement( u"columns"_s );
298
299 const auto constMColumns = mColumns;
300 for ( const ColumnConfig &column : constMColumns )
301 {
302 QDomElement columnElement = doc.createElement( u"column"_s );
303
304 if ( column.type == Action )
305 {
306 columnElement.setAttribute( u"type"_s, u"actions"_s );
307 }
308 else
309 {
310 columnElement.setAttribute( u"type"_s, u"field"_s );
311 columnElement.setAttribute( u"name"_s, column.name );
312 }
313
314 columnElement.setAttribute( u"hidden"_s, column.hidden );
315 columnElement.setAttribute( u"width"_s, QString::number( column.width ) );
316
317 columnsElement.appendChild( columnElement );
318 }
319
320 configElement.appendChild( columnsElement );
321
322 node.appendChild( configElement );
323}
324
326{
327 if ( columns().size() == other.columns().size() )
328 {
329 for ( int i = 0; i < columns().size(); i++ )
330 {
331 if ( columns().at( i ).name != other.columns().at( i ).name || columns().at( i ).type != other.columns().at( i ).type || columns().at( i ).hidden != other.columns().at( i ).hidden )
332 {
333 return false;
334 }
335 }
336 return true;
337 }
338
339 return false;
340}
341
343{
344 return type == other.type && name == other.name && hidden == other.hidden && width == other.width;
345}
void setAddFeatureMethod(const AddFeatureMethod addFeatureMethod)
Sets the addFeatureMethod that defines how features are added (single form or embedded in a table).
void setActionWidgetVisible(bool visible)
Set if the action widget is visible.
bool isEmpty() const
Returns true if the configuration is empty, ie it contains no columns.
void setSortExpression(const QString &sortExpression)
Set the sort expression used for sorting.
@ Action
This column represents an action widget.
@ Field
This column represents a field.
void readXml(const QDomNode &node)
Deserialize to XML on layer load.
Qt::SortOrder sortOrder() const
Gets the sort order.
QVector< QgsAttributeTableConfig::ColumnConfig > columns() const
Gets the list with all columns and their configuration.
int mapVisibleColumnToIndex(int visibleColumn) const
Maps a visible column index to its original column index.
void update(const QgsFields &fields)
Update the configuration with the given fields.
AddFeatureMethod addFeatureMethod() const
Returns the method that defines how features are added (single form or embedded in a table).
ActionWidgetStyle
The style of the action widget in the attribute table.
@ DropDown
A tool button with a drop-down to select the current action.
bool actionWidgetVisible() const
Returns true if the action widget is visible.
void setActionWidgetStyle(ActionWidgetStyle actionWidgetStyle)
Set the style of the action widget.
void setSortOrder(Qt::SortOrder sortOrder)
Set the sort order.
int columnWidth(int column) const
Returns the width of a column, or -1 if column should use default width.
QgsAttributeTableConfig()=default
void setColumns(const QVector< QgsAttributeTableConfig::ColumnConfig > &columns)
Set the list of columns visible in the attribute table.
bool columnHidden(int column) const
Returns true if the specified column is hidden.
void setColumnHidden(int column, bool hidden)
Sets whether the specified column should be hidden.
bool operator!=(const QgsAttributeTableConfig &other) const
ActionWidgetStyle actionWidgetStyle() const
Gets the style of the action widget.
QString sortExpression() const
Gets the expression used for sorting.
void setColumnWidth(int column, int width)
Sets the width of a column.
void writeXml(QDomNode &node) const
Serialize to XML on layer save.
bool hasSameColumns(const QgsAttributeTableConfig &other) const
Compare this configuration's columns name, type, and order to other.
int size() const
Returns the number of columns in the configuration.
AddFeatureMethod
The way to add features in the attribute table.
Container of fields for a vector layer.
Definition qgsfields.h:46
Q_INVOKABLE int indexOf(const QString &fieldName) const
Gets the field index from the field name.
T qgsEnumKeyToValue(const QString &key, const T &defaultValue, bool tryValueAsKey=true, bool *returnOk=nullptr)
Returns the value corresponding to the given key of an enum.
Definition qgis.h:7349
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:7330
Defines the configuration of a column in the attribute table.
QgsAttributeTableConfig::Type type
The type of this column.
bool operator==(const QgsAttributeTableConfig::ColumnConfig &other) const
bool hidden
Flag that controls if the column is hidden.
int width
Width of column, or -1 for default width.
QString name
The name of the attribute if this column represents a field.