20#include "moc_qgssourcefieldsproperties.cpp"
37 layout()->setContentsMargins( 0, 0, 0, 0 );
48 connect( mAddAttributeButton, &QAbstractButton::clicked,
this, &QgsSourceFieldsProperties::addAttributeClicked );
49 connect( mDeleteAttributeButton, &QAbstractButton::clicked,
this, &QgsSourceFieldsProperties::deleteAttributeClicked );
50 connect( mCalculateFieldButton, &QAbstractButton::clicked,
this, &QgsSourceFieldsProperties::calculateFieldClicked );
51 connect( mSaveLayerEditsButton, &QAbstractButton::clicked,
this, &QgsSourceFieldsProperties::saveLayerEditsClicked );
61 mFieldsList->setSelectionBehavior( QAbstractItemView::SelectRows );
62 mFieldsList->setDragDropMode( QAbstractItemView::DragOnly );
63 mFieldsList->setHorizontalHeaderItem(
AttrIdCol,
new QTableWidgetItem( tr(
"Id" ) ) );
64 mFieldsList->setHorizontalHeaderItem(
AttrNameCol,
new QTableWidgetItem( tr(
"Name" ) ) );
65 mFieldsList->setHorizontalHeaderItem(
AttrTypeCol,
new QTableWidgetItem( tr(
"Type" ) ) );
66 mFieldsList->setHorizontalHeaderItem(
AttrTypeNameCol,
new QTableWidgetItem( tr(
"Type name" ) ) );
67 mFieldsList->setHorizontalHeaderItem(
AttrLengthCol,
new QTableWidgetItem( tr(
"Length" ) ) );
68 mFieldsList->setHorizontalHeaderItem(
AttrPrecCol,
new QTableWidgetItem( tr(
"Precision" ) ) );
69 mFieldsList->setHorizontalHeaderItem(
AttrCommentCol,
new QTableWidgetItem( tr(
"Comment" ) ) );
70 const auto configurationFlagsWi =
new QTableWidgetItem( tr(
"Configuration" ) );
71 configurationFlagsWi->setToolTip( tr(
"Configures the field" ) );
73 mFieldsList->setHorizontalHeaderItem(
AttrAliasCol,
new QTableWidgetItem( tr(
"Alias" ) ) );
75 mFieldsList->setSortingEnabled(
true );
76 mFieldsList->sortByColumn( 0, Qt::AscendingOrder );
77 mFieldsList->setSelectionBehavior( QAbstractItemView::SelectRows );
78 mFieldsList->setSelectionMode( QAbstractItemView::ExtendedSelection );
79 mFieldsList->horizontalHeader()->setStretchLastSection(
true );
80 mFieldsList->verticalHeader()->hide();
93 disconnect( mFieldsList, &QTableWidget::cellChanged,
this, &QgsSourceFieldsProperties::attributesListCellChanged );
97 mFieldsList->setRowCount( 0 );
99 for (
int i = 0; i < fields.
count(); ++i )
102 mFieldsList->resizeColumnsToContents();
103 connect( mFieldsList, &QTableWidget::cellChanged,
this, &QgsSourceFieldsProperties::attributesListCellChanged );
105 connect( mFieldsList, &QTableWidget::cellPressed,
this, &QgsSourceFieldsProperties::attributesListCellPressed );
108 updateFieldRenamingStatus();
111void QgsSourceFieldsProperties::updateFieldRenamingStatus()
115 for (
int row = 0; row < mFieldsList->rowCount(); ++row )
117 if ( canRenameFields )
118 mFieldsList->item( row,
AttrNameCol )->setFlags( mFieldsList->item( row,
AttrNameCol )->flags() | Qt::ItemIsEditable );
120 mFieldsList->item( row,
AttrNameCol )->setFlags( mFieldsList->item( row,
AttrNameCol )->flags() & ~Qt::ItemIsEditable );
124void QgsSourceFieldsProperties::updateExpression()
126 QToolButton *btn = qobject_cast<QToolButton *>( sender() );
129 const int index = btn->property(
"Index" ).toInt();
146void QgsSourceFieldsProperties::attributeAdded(
int idx )
148 const bool sorted = mFieldsList->isSortingEnabled();
150 mFieldsList->setSortingEnabled(
false );
153 const int row = mFieldsList->rowCount();
154 mFieldsList->insertRow( row );
155 setRow( row, idx, fields.
at( idx ) );
156 mFieldsList->setCurrentCell( row, idx );
158 const QColor expressionColor = QColor( 103, 0, 243, 44 );
159 const QColor joinColor = QColor( 0, 243, 79, 44 );
160 const QColor defaultColor = QColor( 252, 255, 79, 44 );
162 for (
int i = 0; i < mFieldsList->columnCount(); i++ )
172 mFieldsList->item( row, i )->setBackground( expressionColor );
176 mFieldsList->item( row, i )->setBackground( joinColor );
180 if ( defaultColor.isValid() )
181 mFieldsList->item( row, i )->setBackground( defaultColor );
187 mFieldsList->setSortingEnabled(
true );
191void QgsSourceFieldsProperties::attributeDeleted(
int idx )
203 QTableWidgetItem *dataItem =
new QTableWidgetItem();
204 dataItem->setData( Qt::DisplayRole, idx );
206 mFieldsList->setItem( row,
AttrIdCol, dataItem );
213 mFieldsList->setItem( row,
AttrNameCol,
new QTableWidgetItem( field.
name() ) );
214 mFieldsList->setItem( row,
AttrAliasCol,
new QTableWidgetItem( field.
alias() ) );
217 mFieldsList->setItem( row,
AttrLengthCol,
new QTableWidgetItem( QString::number( field.
length() ) ) );
218 mFieldsList->setItem( row,
AttrPrecCol,
new QTableWidgetItem( QString::number( field.
precision() ) ) );
222 QWidget *expressionWidget =
new QWidget;
223 expressionWidget->setLayout(
new QHBoxLayout );
224 QToolButton *editExpressionButton =
new QToolButton;
225 editExpressionButton->setProperty(
"Index", idx );
227 connect( editExpressionButton, &QAbstractButton::clicked,
this, &QgsSourceFieldsProperties::updateExpression );
228 expressionWidget->layout()->setContentsMargins( 0, 0, 0, 0 );
229 expressionWidget->layout()->addWidget( editExpressionButton );
231 expressionWidget->setStyleSheet(
"*[class~=\"QWidget\"] { background-color: rgba( 103, 0, 243, 0.12 ); } QToolButton { background-color: rgba( 203, 100, 243, 0.6 ); }" );
232 mFieldsList->setCellWidget( row,
AttrCommentCol, expressionWidget );
239 QList<int> notEditableCols = QList<int>()
249 const auto constNotEditableCols = notEditableCols;
250 for (
const int i : constNotEditableCols )
253 mFieldsList->item( row, i )->setFlags( mFieldsList->item( row, i )->flags() & ~Qt::ItemIsEditable );
255 mFieldsList->item( row, i )->setToolTip( tr(
"Edit alias in the Form config tab" ) );
258 if ( canRenameFields )
259 mFieldsList->item( row,
AttrNameCol )->setFlags( mFieldsList->item( row,
AttrNameCol )->flags() | Qt::ItemIsEditable );
261 mFieldsList->item( row,
AttrNameCol )->setFlags( mFieldsList->item( row,
AttrNameCol )->flags() & ~Qt::ItemIsEditable );
265 const QList<Qgis::FieldConfigurationFlag> flagList = qgsEnumList<Qgis::FieldConfigurationFlag>();
288 QMessageBox::critical(
this, tr(
"Add Field" ), tr(
"Failed to add field '%1' of type '%2'. Is the field name unique?" ).arg( field.
name(), field.
typeName() ) );
295 for (
int i = 0; i < mFieldsList->rowCount(); i++ )
297 const int idx = mFieldsList->item( i,
AttrIdCol )->data( Qt::DisplayRole ).toInt();
304 for (
int r = 0; r < model->rowCount(); ++r )
306 const QModelIndex index = model->index( r, 0 );
308 const bool active = model->
data( index, Qt::CheckStateRole ).value<Qt::CheckState>() == Qt::Checked ?
true :
false;
309 flags.setFlag( flag, active );
318void QgsSourceFieldsProperties::editingToggled()
321 updateFieldRenamingStatus();
324void QgsSourceFieldsProperties::addAttributeClicked()
332 if ( dialog.exec() == QDialog::Accepted )
339void QgsSourceFieldsProperties::deleteAttributeClicked()
341 QSet<int> providerFields;
342 QSet<int> expressionFields;
343 const auto constSelectedItems = mFieldsList->selectedItems();
344 for ( QTableWidgetItem *item : constSelectedItems )
346 if ( item->column() == 0 )
353 expressionFields << idx;
355 providerFields << idx;
359 if ( !expressionFields.isEmpty() )
362 if ( !providerFields.isEmpty() )
372void QgsSourceFieldsProperties::calculateFieldClicked()
380 if ( calc.exec() == QDialog::Accepted )
386void QgsSourceFieldsProperties::saveLayerEditsClicked()
391void QgsSourceFieldsProperties::attributesListCellChanged(
int row,
int column )
397 QTableWidgetItem *nameItem = mFieldsList->item( row, column );
410 QMessageBox::critical(
this, tr(
"Rename Field" ), tr(
"Failed to rename field to '%1'. Is the field name unique?" ).arg( nameItem->text() ) );
415void QgsSourceFieldsProperties::attributesListCellPressed(
int ,
int )
434 mToggleEditingButton->setChecked(
true );
435 mSaveLayerEditsButton->setEnabled(
true );
436 mSaveLayerEditsButton->setChecked(
true );
440 mToggleEditingButton->setChecked(
false );
441 mAddAttributeButton->setEnabled(
false );
442 mSaveLayerEditsButton->setEnabled(
false );
445 mDeleteAttributeButton->setEnabled( !mFieldsList->selectedItems().isEmpty() );
447 const auto constSelectedItems = mFieldsList->selectedItems();
448 for ( QTableWidgetItem *item : constSelectedItems )
450 if ( item->column() == 0 )
455 mDeleteAttributeButton->setEnabled(
false );
@ AddAttributes
Allows addition of new attributes (fields)
@ RenameAttributes
Supports renaming attributes (fields)
@ DeleteAttributes
Allows deletion of attributes (fields)
@ ChangeAttributeValues
Allows modification of attribute values.
QFlags< VectorProviderCapability > VectorProviderCapabilities
Vector data provider capabilities.
@ Expression
Field is calculated from an expression.
@ Join
Field originates from a joined layer.
FieldConfigurationFlag
Configuration flags for fields These flags are meant to be user-configurable and are not describing a...
@ NoFlag
No flag is defined.
QFlags< FieldConfigurationFlag > FieldConfigurationFlags
Configuration flags for fields These flags are meant to be user-configurable and are not describing a...
Dialog to add a source field attribute.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
QComboBox subclass which allows selecting multiple items.
QgsCheckableItemModel * model() const
Returns the custom item model which handles checking the items.
void addItemWithCheckState(const QString &text, Qt::CheckState state, const QVariant &userData=QVariant())
Adds an item to the combobox with the given text, check state (stored in the Qt::CheckStateRole) and ...
QStandardItemModel subclass which makes all items checkable by default.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Returns the data stored under the given role for the item referred to by the index.
A generic dialog for building expression strings.
static QgsExpressionContextScope * projectScope(const QgsProject *project)
Creates a new scope which contains variables and functions relating to a QGIS project.
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
A dialog class that provides calculation of new fields using existing fields, values and a set of ope...
Encapsulate a field in an attribute table or data source.
QString typeName() const
Gets the field type.
QString friendlyTypeString() const
Returns a user friendly, translated representation of the field type.
static QString readableConfigurationFlag(Qgis::FieldConfigurationFlag flag)
Returns the readable and translated value of the configuration flag.
Container of fields for a vector layer.
Qgis::FieldOrigin fieldOrigin(int fieldIdx) const
Returns the field's origin (value from an enumeration).
Q_INVOKABLE bool exists(int i) const
Returns if a field index is valid.
void clear()
Removes all fields.
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
QIcon iconForField(int fieldIdx, bool considerOrigin=false) const
Returns an icon corresponding to a field index, based on the field's type and source.
void editingStopped()
Emitted when edited changes have been successfully written to the data provider.
void editingStarted()
Emitted when editing on this layer has started.
static QgsProject * instance()
Returns the QgsProject singleton instance.
@ AttrConfigurationFlagsCol
QList< QTableWidgetItem * > mIndexedWidgets
QgsSourceFieldsProperties(QgsVectorLayer *layer, QWidget *parent=nullptr)
bool addAttribute(const QgsField &field)
Adds an attribute to the table (but does not commit it yet)
void setRow(int row, int idx, const QgsField &field)
This is the base class for vector data providers.
virtual Q_INVOKABLE Qgis::VectorProviderCapabilities capabilities() const
Returns flags containing the supported capabilities.
Represents a vector layer which manages a vector based data sets.
void setFieldConfigurationFlags(int index, Qgis::FieldConfigurationFlags flags)
Sets the configuration flags of the field at given index.
bool addAttribute(const QgsField &field)
Add an attribute field (but does not commit it) returns true if the field was added.
void attributeAdded(int idx)
Will be emitted, when a new attribute has been added to this vector layer.
QString expressionField(int index) const
Returns the expression used for a given expression field.
void updateExpressionField(int index, const QString &exp)
Changes the expression used to define an expression based (virtual) field.
void endEditCommand()
Finish edit command and add it to undo/redo stack.
void destroyEditCommand()
Destroy active command and reverts all changes in it.
bool isEditable() const FINAL
Returns true if the provider is in editing mode.
void attributeDeleted(int idx)
Will be emitted, when an attribute has been deleted from this vector layer.
Qgis::FieldConfigurationFlags fieldConfigurationFlags(int index) const
Returns the configuration flags of the field at given index.
Q_INVOKABLE bool commitChanges(bool stopEditing=true)
Attempts to commit to the underlying data provider any buffered changes made since the last to call t...
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.
bool deleteAttributes(const QList< int > &attrs)
Deletes a list of attribute fields (but does not commit it)
bool renameAttribute(int index, const QString &newName)
Renames an attribute field (but does not commit it).
void beginEditCommand(const QString &text)
Create edit command for undo/redo operations.
#define QgsDebugMsgLevel(str, level)