QGIS API Documentation 3.99.0-Master (26c88405ac0)
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 <QStringList>
21
22QVector<QgsAttributeTableConfig::ColumnConfig> QgsAttributeTableConfig::columns() const
23{
24 return mColumns;
25}
26
28{
29 return mColumns.isEmpty();
30}
31
33{
34 return mColumns.size();
35}
36
38{
39 for ( int i = 0; i < mColumns.size(); ++i )
40 {
41 if ( mColumns.at( i ).hidden )
42 {
43 visibleColumn++;
44 continue;
45 }
46 if ( visibleColumn == i )
47 return i;
48 }
49 return -1;
50}
51
52void QgsAttributeTableConfig::setColumns( const QVector<ColumnConfig> &columns )
53{
54 mColumns = columns;
55}
56
58{
59 QStringList columns;
60
61 bool containsActionColumn = false;
62
63 for ( int i = mColumns.count() - 1; i >= 0; --i )
64 {
65 const ColumnConfig &column = mColumns.at( i );
66 if ( column.type == Field )
67 {
68 if ( fields.indexOf( column.name ) == -1 )
69 {
70 mColumns.remove( i );
71 }
72 else
73 {
74 columns.append( column.name );
75 }
76 }
77 else if ( column.type == Action )
78 {
79 containsActionColumn = true;
80 }
81 }
82
83 for ( const auto &field : fields )
84 {
85 if ( !columns.contains( field.name() ) )
86 {
87 ColumnConfig newColumn;
88 newColumn.hidden = false;
89 newColumn.type = Field;
90 newColumn.name = field.name();
91 if ( containsActionColumn )
92 {
93 mColumns.insert( mColumns.size() - 1, newColumn );
94 }
95 else
96 {
97 mColumns.append( newColumn );
98 }
99 }
100 }
101
102 if ( !containsActionColumn )
103 {
104 ColumnConfig actionConfig;
105
106 actionConfig.type = Action;
107 actionConfig.hidden = true;
108
109 mColumns.append( actionConfig );
110 }
111}
112
114{
115 const auto constMColumns = mColumns;
116 for ( const ColumnConfig &columnConfig : constMColumns )
117 {
118 if ( columnConfig.type == Action && !columnConfig.hidden )
119 return true;
120 }
121 return false;
122}
123
125{
126 for ( int i = 0; i < mColumns.size(); ++i )
127 {
128 if ( mColumns.at( i ).type == Action )
129 {
130 mColumns[i].hidden = !visible;
131 }
132 }
133}
134
139
144
145
146void QgsAttributeTableConfig::readXml( const QDomNode &node )
147{
148 mColumns.clear();
149
150 const QDomNode configNode = node.namedItem( QStringLiteral( "attributetableconfig" ) );
151 if ( !configNode.isNull() )
152 {
153 const QDomNode columnsNode = configNode.toElement().namedItem( QStringLiteral( "columns" ) );
154
155 const QDomNodeList columns = columnsNode.childNodes();
156
157 for ( int i = 0; i < columns.size(); ++i )
158 {
159 const QDomElement columnElement = columns.at( i ).toElement();
160
161 ColumnConfig column;
162
163 if ( columnElement.attribute( QStringLiteral( "type" ) ) == QLatin1String( "actions" ) )
164 {
165 column.type = Action;
166 }
167 else
168 {
169 column.type = Field;
170 column.name = columnElement.attribute( QStringLiteral( "name" ) );
171 }
172
173 column.hidden = columnElement.attribute( QStringLiteral( "hidden" ) ) == QLatin1String( "1" );
174 column.width = columnElement.attribute( QStringLiteral( "width" ), QStringLiteral( "-1" ) ).toDouble();
175
176 mColumns.append( column );
177 }
178
179 if ( configNode.toElement().attribute( QStringLiteral( "actionWidgetStyle" ) ) == QLatin1String( "buttonList" ) )
180 mActionWidgetStyle = ButtonList;
181 else
182 mActionWidgetStyle = DropDown;
183 }
184 else
185 {
186 // Before QGIS 2.16 the attribute table would hide "Hidden" widgets.
187 // They are migrated to hidden columns here.
188 const QDomNodeList editTypeNodes = node.namedItem( QStringLiteral( "edittypes" ) ).childNodes();
189
190 for ( int i = 0; i < editTypeNodes.size(); i++ )
191 {
192 const QDomElement editTypeElement = editTypeNodes.at( i ).toElement();
193
194 if ( editTypeElement.attribute( QStringLiteral( "widgetv2type" ) ) == QLatin1String( "Hidden" ) )
195 {
196 ColumnConfig column;
197
198 column.name = editTypeElement.attribute( QStringLiteral( "name" ) );
199 column.hidden = true;
200 column.type = Field;
201 mColumns.append( column );
202 }
203 }
204 }
205
206 mSortExpression = configNode.toElement().attribute( QStringLiteral( "sortExpression" ) );
207 const Qt::SortOrder sortOrder = static_cast<Qt::SortOrder>( configNode.toElement().attribute( QStringLiteral( "sortOrder" ) ).toInt() );
209}
210
212{
213 return mSortExpression;
214}
215
217{
218 mSortExpression = sortExpression;
219}
220
222{
223 return mColumns.at( column ).width;
224}
225
226void QgsAttributeTableConfig::setColumnWidth( int column, int width )
227{
228 mColumns[ column ].width = width;
229}
230
232{
233 return mColumns.at( column ).hidden;
234}
235
236void QgsAttributeTableConfig::setColumnHidden( int column, bool hidden )
237{
238 mColumns[ column ].hidden = hidden;
239}
240
242{
243 return mSortExpression != other.mSortExpression || mColumns != other.mColumns || mActionWidgetStyle != other.mActionWidgetStyle || mSortOrder != other.mSortOrder;
244}
245
247{
248 return mSortOrder;
249}
250
252{
253 // fix https://hub.qgis.org/issues/15803
254 if ( sortOrder != Qt::AscendingOrder && sortOrder != Qt::DescendingOrder )
255 {
256 sortOrder = Qt::AscendingOrder;
257 }
258
259 mSortOrder = sortOrder;
260}
261
262void QgsAttributeTableConfig::writeXml( QDomNode &node ) const
263{
264 QDomDocument doc( node.ownerDocument() );
265
266 QDomElement configElement = doc.createElement( QStringLiteral( "attributetableconfig" ) );
267 configElement.setAttribute( QStringLiteral( "actionWidgetStyle" ), mActionWidgetStyle == ButtonList ? "buttonList" : "dropDown" );
268
269 configElement.setAttribute( QStringLiteral( "sortExpression" ), mSortExpression );
270
271 configElement.setAttribute( QStringLiteral( "sortOrder" ), mSortOrder );
272
273 QDomElement columnsElement = doc.createElement( QStringLiteral( "columns" ) );
274
275 const auto constMColumns = mColumns;
276 for ( const ColumnConfig &column : constMColumns )
277 {
278 QDomElement columnElement = doc.createElement( QStringLiteral( "column" ) );
279
280 if ( column.type == Action )
281 {
282 columnElement.setAttribute( QStringLiteral( "type" ), QStringLiteral( "actions" ) );
283 }
284 else
285 {
286 columnElement.setAttribute( QStringLiteral( "type" ), QStringLiteral( "field" ) );
287 columnElement.setAttribute( QStringLiteral( "name" ), column.name );
288 }
289
290 columnElement.setAttribute( QStringLiteral( "hidden" ), column.hidden );
291 columnElement.setAttribute( QStringLiteral( "width" ), QString::number( column.width ) );
292
293 columnsElement.appendChild( columnElement );
294 }
295
296 configElement.appendChild( columnsElement );
297
298 node.appendChild( configElement );
299}
300
302{
303 if ( columns().size() == other.columns().size() )
304 {
305 for ( int i = 0; i < columns().size(); i++ )
306 {
307 if ( columns().at( i ).name != other.columns().at( i ).name ||
308 columns().at( i ).type != other.columns().at( i ).type ||
309 columns().at( i ).hidden != other.columns().at( i ).hidden )
310 {
311 return false;
312 }
313 }
314 return true;
315 }
316
317 return false;
318}
319
321{
322 return type == other.type && name == other.name && hidden == other.hidden && width == other.width;
323}
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.
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.
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.
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.