23using namespace Qt::StringLiterals;
32 return mColumns.isEmpty();
37 return mColumns.size();
42 for (
int i = 0; i < mColumns.size(); ++i )
44 if ( mColumns.at( i ).hidden )
49 if ( visibleColumn == i )
64 bool containsActionColumn =
false;
66 for (
int i = mColumns.count() - 1; i >= 0; --i )
82 containsActionColumn =
true;
86 for (
const auto &field : fields )
88 if ( !
columns.contains( field.name() ) )
93 newColumn.
name = field.name();
94 if ( containsActionColumn )
96 mColumns.insert( mColumns.size() - 1, newColumn );
100 mColumns.append( newColumn );
105 if ( !containsActionColumn )
110 actionConfig.
hidden =
true;
112 mColumns.append( actionConfig );
118 const auto constMColumns = mColumns;
119 for (
const ColumnConfig &columnConfig : constMColumns )
121 if ( columnConfig.type ==
Action && !columnConfig.hidden )
129 for (
int i = 0; i < mColumns.size(); ++i )
131 if ( mColumns.at( i ).type ==
Action )
133 mColumns[i].hidden = !visible;
140 return mActionWidgetStyle;
153 const QDomNode configNode = node.namedItem( u
"attributetableconfig"_s );
154 if ( !configNode.isNull() )
156 const QDomNode columnsNode = configNode.toElement().namedItem( u
"columns"_s );
158 const QDomNodeList
columns = columnsNode.childNodes();
160 for (
int i = 0; i <
columns.size(); ++i )
162 const QDomElement columnElement =
columns.at( i ).toElement();
166 if ( columnElement.attribute( u
"type"_s ) ==
"actions"_L1 )
173 column.
name = columnElement.attribute( u
"name"_s );
176 column.
hidden = columnElement.attribute( u
"hidden"_s ) ==
"1"_L1;
177 column.
width = columnElement.attribute( u
"width"_s, u
"-1"_s ).toDouble();
179 mColumns.append( column );
182 if ( configNode.toElement().attribute( u
"actionWidgetStyle"_s ) ==
"buttonList"_L1 )
191 const QDomNodeList editTypeNodes = node.namedItem( u
"edittypes"_s ).childNodes();
193 for (
int i = 0; i < editTypeNodes.size(); i++ )
195 const QDomElement editTypeElement = editTypeNodes.at( i ).toElement();
197 if ( editTypeElement.attribute( u
"widgetv2type"_s ) ==
"Hidden"_L1 )
201 column.
name = editTypeElement.attribute( u
"name"_s );
204 mColumns.append( column );
209 mSortExpression = configNode.toElement().attribute( u
"sortExpression"_s );
210 const Qt::SortOrder
sortOrder =
static_cast<Qt::SortOrder
>( configNode.toElement().attribute( u
"sortOrder"_s ).toInt() );
216 return mSortExpression;
226 return mColumns.at( column ).width;
231 mColumns[ column ].width = width;
236 return mColumns.at( column ).hidden;
241 mColumns[ column ].hidden = hidden;
246 return mSortExpression != other.mSortExpression || mColumns != other.mColumns || mActionWidgetStyle != other.mActionWidgetStyle || mSortOrder != other.mSortOrder;
267 QDomDocument doc( node.ownerDocument() );
269 QDomElement configElement = doc.createElement( u
"attributetableconfig"_s );
270 configElement.setAttribute( u
"actionWidgetStyle"_s, mActionWidgetStyle ==
ButtonList ?
"buttonList" :
"dropDown" );
272 configElement.setAttribute( u
"sortExpression"_s, mSortExpression );
274 configElement.setAttribute( u
"sortOrder"_s, mSortOrder );
276 QDomElement columnsElement = doc.createElement( u
"columns"_s );
278 const auto constMColumns = mColumns;
281 QDomElement columnElement = doc.createElement( u
"column"_s );
283 if ( column.type ==
Action )
285 columnElement.setAttribute( u
"type"_s, u
"actions"_s );
289 columnElement.setAttribute( u
"type"_s, u
"field"_s );
290 columnElement.setAttribute( u
"name"_s, column.name );
293 columnElement.setAttribute( u
"hidden"_s, column.hidden );
294 columnElement.setAttribute( u
"width"_s, QString::number( column.width ) );
296 columnsElement.appendChild( columnElement );
299 configElement.appendChild( columnsElement );
301 node.appendChild( configElement );
308 for (
int i = 0; i <
columns().size(); i++ )
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.
@ ButtonList
A list of buttons.
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.
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.