23#include "moc_qgsattributetableconfig.cpp"
25using namespace Qt::StringLiterals;
34 return mColumns.isEmpty();
39 return mColumns.size();
44 for (
int i = 0; i < mColumns.size(); ++i )
46 if ( mColumns.at( i ).hidden )
51 if ( visibleColumn == i )
66 bool containsActionColumn =
false;
68 for (
int i = mColumns.count() - 1; i >= 0; --i )
84 containsActionColumn =
true;
88 for (
const auto &field : fields )
90 if ( !
columns.contains( field.name() ) )
95 newColumn.
name = field.name();
96 if ( containsActionColumn )
98 mColumns.insert( mColumns.size() - 1, newColumn );
102 mColumns.append( newColumn );
107 if ( !containsActionColumn )
112 actionConfig.
hidden =
true;
114 mColumns.append( actionConfig );
120 const auto constMColumns = mColumns;
121 for (
const ColumnConfig &columnConfig : constMColumns )
123 if ( columnConfig.type ==
Action && !columnConfig.hidden )
131 for (
int i = 0; i < mColumns.size(); ++i )
133 if ( mColumns.at( i ).type ==
Action )
135 mColumns[i].hidden = !visible;
142 return mActionWidgetStyle;
155 const QDomNode configNode = node.namedItem( u
"attributetableconfig"_s );
156 if ( !configNode.isNull() )
158 const QDomNode columnsNode = configNode.toElement().namedItem( u
"columns"_s );
160 const QDomNodeList
columns = columnsNode.childNodes();
162 for (
int i = 0; i <
columns.size(); ++i )
164 const QDomElement columnElement =
columns.at( i ).toElement();
168 if ( columnElement.attribute( u
"type"_s ) ==
"actions"_L1 )
175 column.
name = columnElement.attribute( u
"name"_s );
178 column.
hidden = columnElement.attribute( u
"hidden"_s ) ==
"1"_L1;
179 column.
width = columnElement.attribute( u
"width"_s, u
"-1"_s ).toDouble();
181 mColumns.append( column );
184 if ( configNode.toElement().attribute( u
"actionWidgetStyle"_s ) ==
"buttonList"_L1 )
195 const QDomNodeList editTypeNodes = node.namedItem( u
"edittypes"_s ).childNodes();
197 for (
int i = 0; i < editTypeNodes.size(); i++ )
199 const QDomElement editTypeElement = editTypeNodes.at( i ).toElement();
201 if ( editTypeElement.attribute( u
"widgetv2type"_s ) ==
"Hidden"_L1 )
205 column.
name = editTypeElement.attribute( u
"name"_s );
208 mColumns.append( column );
213 mSortExpression = configNode.toElement().attribute( u
"sortExpression"_s );
214 const Qt::SortOrder
sortOrder =
static_cast<Qt::SortOrder
>( configNode.toElement().attribute( u
"sortOrder"_s ).toInt() );
220 return mSortExpression;
230 return mAddFeatureMethod;
240 return mColumns.at( column ).width;
245 mColumns[column].width = width;
250 return mColumns.at( column ).hidden;
255 mColumns[column].hidden = hidden;
260 return mSortExpression != other.mSortExpression
261 || mColumns != other.mColumns
262 || mActionWidgetStyle != other.mActionWidgetStyle
263 || mSortOrder != other.mSortOrder
264 || mAddFeatureMethod != other.mAddFeatureMethod;
285 QDomDocument doc( node.ownerDocument() );
287 QDomElement configElement = doc.createElement( u
"attributetableconfig"_s );
288 configElement.setAttribute( u
"actionWidgetStyle"_s, mActionWidgetStyle ==
ButtonList ?
"buttonList" :
"dropDown" );
290 configElement.setAttribute( u
"sortExpression"_s, mSortExpression );
292 configElement.setAttribute( u
"sortOrder"_s, mSortOrder );
295 configElement.setAttribute( u
"addFeatureMethod"_s,
qgsEnumValueToKey( mAddFeatureMethod ) );
297 QDomElement columnsElement = doc.createElement( u
"columns"_s );
299 const auto constMColumns = mColumns;
302 QDomElement columnElement = doc.createElement( u
"column"_s );
304 if ( column.type ==
Action )
306 columnElement.setAttribute( u
"type"_s, u
"actions"_s );
310 columnElement.setAttribute( u
"type"_s, u
"field"_s );
311 columnElement.setAttribute( u
"name"_s, column.name );
314 columnElement.setAttribute( u
"hidden"_s, column.hidden );
315 columnElement.setAttribute( u
"width"_s, QString::number( column.width ) );
317 columnsElement.appendChild( columnElement );
320 configElement.appendChild( columnsElement );
322 node.appendChild( configElement );
329 for (
int i = 0; i <
columns().size(); i++ )
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.
@ 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.
AddFeatureMethod
The way to add features in the attribute table.
@ Unset
No method set for current layer.
Container of fields for a vector layer.
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.
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
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.