QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 #include "qgsfields.h"
18 #include <QStringList>
19 
20 QVector<QgsAttributeTableConfig::ColumnConfig> QgsAttributeTableConfig::columns() const
21 {
22  return mColumns;
23 }
24 
26 {
27  return mColumns.isEmpty();
28 }
29 
31 {
32  return mColumns.size();
33 }
34 
36 {
37  for ( int i = 0; i < mColumns.size(); ++i )
38  {
39  if ( mColumns.at( i ).hidden )
40  {
41  visibleColumn++;
42  continue;
43  }
44  if ( visibleColumn == i )
45  return i;
46  }
47  return -1;
48 }
49 
50 void QgsAttributeTableConfig::setColumns( const QVector<ColumnConfig> &columns )
51 {
52  mColumns = columns;
53 }
54 
56 {
57  QStringList columns;
58 
59  bool containsActionColumn = false;
60 
61  for ( int i = mColumns.count() - 1; i >= 0; --i )
62  {
63  const ColumnConfig &column = mColumns.at( i );
64  if ( column.type == Field )
65  {
66  if ( fields.indexOf( column.name ) == -1 )
67  {
68  mColumns.remove( i );
69  }
70  else
71  {
72  columns.append( column.name );
73  }
74  }
75  else if ( column.type == Action )
76  {
77  containsActionColumn = true;
78  }
79  }
80 
81  for ( const auto &field : fields )
82  {
83  if ( !columns.contains( field.name() ) )
84  {
85  ColumnConfig newColumn;
86  newColumn.hidden = false;
87  newColumn.type = Field;
88  newColumn.name = field.name();
89  if ( containsActionColumn )
90  {
91  mColumns.insert( mColumns.size() - 1, newColumn );
92  }
93  else
94  {
95  mColumns.append( newColumn );
96  }
97  }
98  }
99 
100  if ( !containsActionColumn )
101  {
102  ColumnConfig actionConfig;
103 
104  actionConfig.type = Action;
105  actionConfig.hidden = true;
106 
107  mColumns.append( actionConfig );
108  }
109 }
110 
112 {
113  const auto constMColumns = mColumns;
114  for ( const ColumnConfig &columnConfig : constMColumns )
115  {
116  if ( columnConfig.type == Action && !columnConfig.hidden )
117  return true;
118  }
119  return false;
120 }
121 
123 {
124  for ( int i = 0; i < mColumns.size(); ++i )
125  {
126  if ( mColumns.at( i ).type == Action )
127  {
128  mColumns[i].hidden = !visible;
129  }
130  }
131 }
132 
134 {
135  return mActionWidgetStyle;
136 }
137 
139 {
140  mActionWidgetStyle = actionWidgetStyle;
141 }
142 
143 
144 void QgsAttributeTableConfig::readXml( const QDomNode &node )
145 {
146  mColumns.clear();
147 
148  const QDomNode configNode = node.namedItem( QStringLiteral( "attributetableconfig" ) );
149  if ( !configNode.isNull() )
150  {
151  const QDomNode columnsNode = configNode.toElement().namedItem( QStringLiteral( "columns" ) );
152 
153  const QDomNodeList columns = columnsNode.childNodes();
154 
155  for ( int i = 0; i < columns.size(); ++i )
156  {
157  const QDomElement columnElement = columns.at( i ).toElement();
158 
159  ColumnConfig column;
160 
161  if ( columnElement.attribute( QStringLiteral( "type" ) ) == QLatin1String( "actions" ) )
162  {
163  column.type = Action;
164  }
165  else
166  {
167  column.type = Field;
168  column.name = columnElement.attribute( QStringLiteral( "name" ) );
169  }
170 
171  column.hidden = columnElement.attribute( QStringLiteral( "hidden" ) ) == QLatin1String( "1" );
172  column.width = columnElement.attribute( QStringLiteral( "width" ), QStringLiteral( "-1" ) ).toDouble();
173 
174  mColumns.append( column );
175  }
176 
177  if ( configNode.toElement().attribute( QStringLiteral( "actionWidgetStyle" ) ) == QLatin1String( "buttonList" ) )
178  mActionWidgetStyle = ButtonList;
179  else
180  mActionWidgetStyle = DropDown;
181  }
182  else
183  {
184  // Before QGIS 2.16 the attribute table would hide "Hidden" widgets.
185  // They are migrated to hidden columns here.
186  const QDomNodeList editTypeNodes = node.namedItem( QStringLiteral( "edittypes" ) ).childNodes();
187 
188  for ( int i = 0; i < editTypeNodes.size(); i++ )
189  {
190  const QDomElement editTypeElement = editTypeNodes.at( i ).toElement();
191 
192  if ( editTypeElement.attribute( QStringLiteral( "widgetv2type" ) ) == QLatin1String( "Hidden" ) )
193  {
194  ColumnConfig column;
195 
196  column.name = editTypeElement.attribute( QStringLiteral( "name" ) );
197  column.hidden = true;
198  column.type = Field;
199  mColumns.append( column );
200  }
201  }
202  }
203 
204  mSortExpression = configNode.toElement().attribute( QStringLiteral( "sortExpression" ) );
205  const Qt::SortOrder sortOrder = static_cast<Qt::SortOrder>( configNode.toElement().attribute( QStringLiteral( "sortOrder" ) ).toInt() );
207 }
208 
210 {
211  return mSortExpression;
212 }
213 
214 void QgsAttributeTableConfig::setSortExpression( const QString &sortExpression )
215 {
216  mSortExpression = sortExpression;
217 }
218 
220 {
221  return mColumns.at( column ).width;
222 }
223 
224 void QgsAttributeTableConfig::setColumnWidth( int column, int width )
225 {
226  mColumns[ column ].width = width;
227 }
228 
230 {
231  return mColumns.at( column ).hidden;
232 }
233 
234 void QgsAttributeTableConfig::setColumnHidden( int column, bool hidden )
235 {
236  mColumns[ column ].hidden = hidden;
237 }
238 
240 {
241  return mSortExpression != other.mSortExpression || mColumns != other.mColumns || mActionWidgetStyle != other.mActionWidgetStyle || mSortOrder != other.mSortOrder;
242 }
243 
245 {
246  return mSortOrder;
247 }
248 
249 void QgsAttributeTableConfig::setSortOrder( Qt::SortOrder sortOrder )
250 {
251  // fix https://hub.qgis.org/issues/15803
252  if ( sortOrder != Qt::AscendingOrder && sortOrder != Qt::DescendingOrder )
253  {
254  sortOrder = Qt::AscendingOrder;
255  }
256 
257  mSortOrder = sortOrder;
258 }
259 
260 void QgsAttributeTableConfig::writeXml( QDomNode &node ) const
261 {
262  QDomDocument doc( node.ownerDocument() );
263 
264  QDomElement configElement = doc.createElement( QStringLiteral( "attributetableconfig" ) );
265  configElement.setAttribute( QStringLiteral( "actionWidgetStyle" ), mActionWidgetStyle == ButtonList ? "buttonList" : "dropDown" );
266 
267  configElement.setAttribute( QStringLiteral( "sortExpression" ), mSortExpression );
268 
269  configElement.setAttribute( QStringLiteral( "sortOrder" ), mSortOrder );
270 
271  QDomElement columnsElement = doc.createElement( QStringLiteral( "columns" ) );
272 
273  const auto constMColumns = mColumns;
274  for ( const ColumnConfig &column : constMColumns )
275  {
276  QDomElement columnElement = doc.createElement( QStringLiteral( "column" ) );
277 
278  if ( column.type == Action )
279  {
280  columnElement.setAttribute( QStringLiteral( "type" ), QStringLiteral( "actions" ) );
281  }
282  else
283  {
284  columnElement.setAttribute( QStringLiteral( "type" ), QStringLiteral( "field" ) );
285  columnElement.setAttribute( QStringLiteral( "name" ), column.name );
286  }
287 
288  columnElement.setAttribute( QStringLiteral( "hidden" ), column.hidden );
289  columnElement.setAttribute( QStringLiteral( "width" ), QString::number( column.width ) );
290 
291  columnsElement.appendChild( columnElement );
292  }
293 
294  configElement.appendChild( columnsElement );
295 
296  node.appendChild( configElement );
297 }
298 
300 {
301  if ( columns().size() == other.columns().size() )
302  {
303  for ( int i = 0; i < columns().size(); i++ )
304  {
305  if ( columns().at( i ).name != other.columns().at( i ).name ||
306  columns().at( i ).type != other.columns().at( i ).type ||
307  columns().at( i ).hidden != other.columns().at( i ).hidden )
308  {
309  return false;
310  }
311  }
312  return true;
313  }
314 
315  return false;
316 }
317 
319 {
320  return type == other.type && name == other.name && hidden == other.hidden && width == other.width;
321 }
QgsAttributeTableConfig::sortOrder
Qt::SortOrder sortOrder() const
Gets the sort order.
Definition: qgsattributetableconfig.cpp:244
QgsAttributeTableConfig::operator!=
bool operator!=(const QgsAttributeTableConfig &other) const
Compare this configuration to other.
Definition: qgsattributetableconfig.cpp:239
qgsfields.h
QgsAttributeTableConfig::sortExpression
QString sortExpression() const
Gets the expression used for sorting.
Definition: qgsattributetableconfig.cpp:209
QgsAttributeTableConfig::ActionWidgetStyle
ActionWidgetStyle
The style of the action widget in the attribute table.
Definition: qgsattributetableconfig.h:76
QgsAttributeTableConfig::setSortOrder
void setSortOrder(Qt::SortOrder sortOrder)
Set the sort order.
Definition: qgsattributetableconfig.cpp:249
QgsAttributeTableConfig::actionWidgetVisible
bool actionWidgetVisible() const
Returns true if the action widget is visible.
Definition: qgsattributetableconfig.cpp:111
QgsAttributeTableConfig::setActionWidgetStyle
void setActionWidgetStyle(ActionWidgetStyle actionWidgetStyle)
Set the style of the action widget.
Definition: qgsattributetableconfig.cpp:138
QgsAttributeTableConfig::columnWidth
int columnWidth(int column) const
Returns the width of a column, or -1 if column should use default width.
Definition: qgsattributetableconfig.cpp:219
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:44
QgsAttributeTableConfig::ColumnConfig
Defines the configuration of a column in the attribute table.
Definition: qgsattributetableconfig.h:52
QgsAttributeTableConfig::ButtonList
@ ButtonList
A list of buttons.
Definition: qgsattributetableconfig.h:78
field
const QgsField & field
Definition: qgsfield.h:463
QgsAttributeTableConfig::actionWidgetStyle
ActionWidgetStyle actionWidgetStyle() const
Gets the style of the action widget.
Definition: qgsattributetableconfig.cpp:133
QgsField::name
QString name
Definition: qgsfield.h:60
QgsAttributeTableConfig::columnHidden
bool columnHidden(int column) const
Returns true if the specified column is hidden.
Definition: qgsattributetableconfig.cpp:229
QgsAttributeTableConfig::setActionWidgetVisible
void setActionWidgetVisible(bool visible)
Set if the action widget is visible.
Definition: qgsattributetableconfig.cpp:122
QgsAttributeTableConfig::Field
@ Field
This column represents a field.
Definition: qgsattributetableconfig.h:45
QgsAttributeTableConfig
This is a container for configuration of the attribute table. The configuration is specific for one v...
Definition: qgsattributetableconfig.h:36
QgsAttributeTableConfig::mapVisibleColumnToIndex
int mapVisibleColumnToIndex(int visibleColumn) const
Maps a visible column index to its original column index.
Definition: qgsattributetableconfig.cpp:35
QgsAttributeTableConfig::Action
@ Action
This column represents an action widget.
Definition: qgsattributetableconfig.h:46
QgsAttributeTableConfig::writeXml
void writeXml(QDomNode &node) const
Serialize to XML on layer save.
Definition: qgsattributetableconfig.cpp:260
QgsAttributeTableConfig::columns
QVector< QgsAttributeTableConfig::ColumnConfig > columns() const
Gets the list with all columns and their configuration.
Definition: qgsattributetableconfig.cpp:20
QgsAttributeTableConfig::setColumnWidth
void setColumnWidth(int column, int width)
Sets the width of a column.
Definition: qgsattributetableconfig.cpp:224
QgsAttributeTableConfig::setColumns
void setColumns(const QVector< QgsAttributeTableConfig::ColumnConfig > &columns)
Set the list of columns visible in the attribute table.
Definition: qgsattributetableconfig.cpp:50
QgsAttributeTableConfig::DropDown
@ DropDown
A tool button with a drop-down to select the current action.
Definition: qgsattributetableconfig.h:79
QgsAttributeTableConfig::ColumnConfig::width
int width
Width of column, or -1 for default width.
Definition: qgsattributetableconfig.h:70
QgsAttributeTableConfig::isEmpty
bool isEmpty() const
Returns true if the configuration is empty, ie it contains no columns.
Definition: qgsattributetableconfig.cpp:25
QgsAttributeTableConfig::ColumnConfig::name
QString name
The name of the attribute if this column represents a field.
Definition: qgsattributetableconfig.h:64
QgsAttributeTableConfig::setColumnHidden
void setColumnHidden(int column, bool hidden)
Sets whether the specified column should be hidden.
Definition: qgsattributetableconfig.cpp:234
QgsAttributeTableConfig::ColumnConfig::operator==
bool operator==(const QgsAttributeTableConfig::ColumnConfig &other) const
Definition: qgsattributetableconfig.cpp:318
QgsAttributeTableConfig::ColumnConfig::hidden
bool hidden
Flag that controls if the column is hidden.
Definition: qgsattributetableconfig.h:67
qgsattributetableconfig.h
QgsAttributeTableConfig::ColumnConfig::type
QgsAttributeTableConfig::Type type
The type of this column.
Definition: qgsattributetableconfig.h:61
QgsAttributeTableConfig::size
int size() const
Returns the number of columns in the configuration.
Definition: qgsattributetableconfig.cpp:30
QgsAttributeTableConfig::hasSameColumns
bool hasSameColumns(const QgsAttributeTableConfig &other) const
Compare this configuration's columns name, type, and order to other.
Definition: qgsattributetableconfig.cpp:299
QgsAttributeTableConfig::setSortExpression
void setSortExpression(const QString &sortExpression)
Set the sort expression used for sorting.
Definition: qgsattributetableconfig.cpp:214
QgsAttributeTableConfig::readXml
void readXml(const QDomNode &node)
Deserialize to XML on layer load.
Definition: qgsattributetableconfig.cpp:144
QgsFields::indexOf
int indexOf(const QString &fieldName) const
Gets the field index from the field name.
Definition: qgsfields.cpp:207
QgsAttributeTableConfig::update
void update(const QgsFields &fields)
Update the configuration with the given fields.
Definition: qgsattributetableconfig.cpp:55