29 return mColumns.isEmpty();
34 return mColumns.size();
39 for (
int i = 0; i < mColumns.size(); ++i )
41 if ( mColumns.at( i ).hidden )
46 if ( visibleColumn == i )
61 bool containsActionColumn =
false;
63 for (
int i = mColumns.count() - 1; i >= 0; --i )
79 containsActionColumn =
true;
83 for (
const auto &field : fields )
85 if ( !
columns.contains( field.name() ) )
90 newColumn.
name = field.name();
91 if ( containsActionColumn )
93 mColumns.insert( mColumns.size() - 1, newColumn );
97 mColumns.append( newColumn );
102 if ( !containsActionColumn )
107 actionConfig.
hidden =
true;
109 mColumns.append( actionConfig );
115 const auto constMColumns = mColumns;
116 for (
const ColumnConfig &columnConfig : constMColumns )
118 if ( columnConfig.type ==
Action && !columnConfig.hidden )
126 for (
int i = 0; i < mColumns.size(); ++i )
128 if ( mColumns.at( i ).type ==
Action )
130 mColumns[i].hidden = !visible;
137 return mActionWidgetStyle;
150 const QDomNode configNode = node.namedItem( QStringLiteral(
"attributetableconfig" ) );
151 if ( !configNode.isNull() )
153 const QDomNode columnsNode = configNode.toElement().namedItem( QStringLiteral(
"columns" ) );
155 const QDomNodeList
columns = columnsNode.childNodes();
157 for (
int i = 0; i <
columns.size(); ++i )
159 const QDomElement columnElement =
columns.at( i ).toElement();
163 if ( columnElement.attribute( QStringLiteral(
"type" ) ) == QLatin1String(
"actions" ) )
170 column.
name = columnElement.attribute( QStringLiteral(
"name" ) );
173 column.
hidden = columnElement.attribute( QStringLiteral(
"hidden" ) ) == QLatin1String(
"1" );
174 column.
width = columnElement.attribute( QStringLiteral(
"width" ), QStringLiteral(
"-1" ) ).toDouble();
176 mColumns.append( column );
179 if ( configNode.toElement().attribute( QStringLiteral(
"actionWidgetStyle" ) ) == QLatin1String(
"buttonList" ) )
188 const QDomNodeList editTypeNodes = node.namedItem( QStringLiteral(
"edittypes" ) ).childNodes();
190 for (
int i = 0; i < editTypeNodes.size(); i++ )
192 const QDomElement editTypeElement = editTypeNodes.at( i ).toElement();
194 if ( editTypeElement.attribute( QStringLiteral(
"widgetv2type" ) ) == QLatin1String(
"Hidden" ) )
198 column.
name = editTypeElement.attribute( QStringLiteral(
"name" ) );
201 mColumns.append( column );
206 mSortExpression = configNode.toElement().attribute( QStringLiteral(
"sortExpression" ) );
207 const Qt::SortOrder
sortOrder =
static_cast<Qt::SortOrder
>( configNode.toElement().attribute( QStringLiteral(
"sortOrder" ) ).toInt() );
213 return mSortExpression;
223 return mColumns.at( column ).width;
228 mColumns[ column ].width = width;
233 return mColumns.at( column ).hidden;
238 mColumns[ column ].hidden = hidden;
243 return mSortExpression != other.mSortExpression || mColumns != other.mColumns || mActionWidgetStyle != other.mActionWidgetStyle || mSortOrder != other.mSortOrder;
264 QDomDocument doc( node.ownerDocument() );
266 QDomElement configElement = doc.createElement( QStringLiteral(
"attributetableconfig" ) );
267 configElement.setAttribute( QStringLiteral(
"actionWidgetStyle" ), mActionWidgetStyle ==
ButtonList ?
"buttonList" :
"dropDown" );
269 configElement.setAttribute( QStringLiteral(
"sortExpression" ), mSortExpression );
271 configElement.setAttribute( QStringLiteral(
"sortOrder" ), mSortOrder );
273 QDomElement columnsElement = doc.createElement( QStringLiteral(
"columns" ) );
275 const auto constMColumns = mColumns;
278 QDomElement columnElement = doc.createElement( QStringLiteral(
"column" ) );
280 if ( column.type ==
Action )
282 columnElement.setAttribute( QStringLiteral(
"type" ), QStringLiteral(
"actions" ) );
286 columnElement.setAttribute( QStringLiteral(
"type" ), QStringLiteral(
"field" ) );
287 columnElement.setAttribute( QStringLiteral(
"name" ), column.name );
290 columnElement.setAttribute( QStringLiteral(
"hidden" ), column.hidden );
291 columnElement.setAttribute( QStringLiteral(
"width" ), QString::number( column.width ) );
293 columnsElement.appendChild( columnElement );
296 configElement.appendChild( columnsElement );
298 node.appendChild( configElement );
305 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.