36 layout()->setContentsMargins( 0, 0, 0, 0 );
46 connect( mAddAttributeButton, &QAbstractButton::clicked,
this, &QgsSourceFieldsProperties::addAttributeClicked );
47 connect( mDeleteAttributeButton, &QAbstractButton::clicked,
this, &QgsSourceFieldsProperties::deleteAttributeClicked );
48 connect( mCalculateFieldButton, &QAbstractButton::clicked,
this, &QgsSourceFieldsProperties::calculateFieldClicked );
58 mFieldsList->setSelectionBehavior( QAbstractItemView::SelectRows );
59 mFieldsList->setDragDropMode( QAbstractItemView::DragOnly );
60 mFieldsList->setHorizontalHeaderItem(
AttrIdCol,
new QTableWidgetItem( tr(
"Id" ) ) );
61 mFieldsList->setHorizontalHeaderItem(
AttrNameCol,
new QTableWidgetItem( tr(
"Name" ) ) );
62 mFieldsList->setHorizontalHeaderItem(
AttrTypeCol,
new QTableWidgetItem( tr(
"Type" ) ) );
63 mFieldsList->setHorizontalHeaderItem(
AttrTypeNameCol,
new QTableWidgetItem( tr(
"Type name" ) ) );
64 mFieldsList->setHorizontalHeaderItem(
AttrLengthCol,
new QTableWidgetItem( tr(
"Length" ) ) );
65 mFieldsList->setHorizontalHeaderItem(
AttrPrecCol,
new QTableWidgetItem( tr(
"Precision" ) ) );
66 mFieldsList->setHorizontalHeaderItem(
AttrCommentCol,
new QTableWidgetItem( tr(
"Comment" ) ) );
67 const auto configurationFlagsWi =
new QTableWidgetItem( tr(
"Configuration" ) );
68 configurationFlagsWi->setToolTip( tr(
"Configures the field" ) );
70 mFieldsList->setHorizontalHeaderItem(
AttrAliasCol,
new QTableWidgetItem( tr(
"Alias" ) ) );
72 mFieldsList->setSortingEnabled(
true );
73 mFieldsList->sortByColumn( 0, Qt::AscendingOrder );
74 mFieldsList->setSelectionBehavior( QAbstractItemView::SelectRows );
75 mFieldsList->setSelectionMode( QAbstractItemView::ExtendedSelection );
76 mFieldsList->horizontalHeader()->setStretchLastSection(
true );
77 mFieldsList->verticalHeader()->hide();
90 disconnect( mFieldsList, &QTableWidget::cellChanged,
this, &QgsSourceFieldsProperties::attributesListCellChanged );
94 mFieldsList->setRowCount( 0 );
96 for (
int i = 0; i < fields.
count(); ++i )
99 mFieldsList->resizeColumnsToContents();
100 connect( mFieldsList, &QTableWidget::cellChanged,
this, &QgsSourceFieldsProperties::attributesListCellChanged );
102 connect( mFieldsList, &QTableWidget::cellPressed,
this, &QgsSourceFieldsProperties::attributesListCellPressed );
105 updateFieldRenamingStatus();
108void QgsSourceFieldsProperties::updateFieldRenamingStatus()
112 for (
int row = 0; row < mFieldsList->rowCount(); ++row )
114 if ( canRenameFields )
115 mFieldsList->item( row,
AttrNameCol )->setFlags( mFieldsList->item( row,
AttrNameCol )->flags() | Qt::ItemIsEditable );
117 mFieldsList->item( row,
AttrNameCol )->setFlags( mFieldsList->item( row,
AttrNameCol )->flags() & ~Qt::ItemIsEditable );
121void QgsSourceFieldsProperties::updateExpression()
123 QToolButton *btn = qobject_cast<QToolButton *>( sender() );
126 const int index = btn->property(
"Index" ).toInt();
143void QgsSourceFieldsProperties::attributeAdded(
int idx )
145 const bool sorted = mFieldsList->isSortingEnabled();
147 mFieldsList->setSortingEnabled(
false );
150 const int row = mFieldsList->rowCount();
151 mFieldsList->insertRow( row );
152 setRow( row, idx, fields.
at( idx ) );
153 mFieldsList->setCurrentCell( row, idx );
155 const QColor expressionColor = QColor( 103, 0, 243, 44 );
156 const QColor joinColor = QColor( 0, 243, 79, 44 );
157 const QColor defaultColor = QColor( 252, 255, 79, 44 );
159 for (
int i = 0; i < mFieldsList->columnCount(); i++ )
167 if ( i == 7 )
continue;
168 mFieldsList->item( row, i )->setBackground( expressionColor );
172 mFieldsList->item( row, i )->setBackground( joinColor );
176 if ( defaultColor.isValid() )
177 mFieldsList->item( row, i )->setBackground( defaultColor );
183 mFieldsList->setSortingEnabled(
true );
187void QgsSourceFieldsProperties::attributeDeleted(
int idx )
199 QTableWidgetItem *dataItem =
new QTableWidgetItem();
200 dataItem->setData( Qt::DisplayRole, idx );
202 mFieldsList->setItem( row,
AttrIdCol, dataItem );
218 QWidget *expressionWidget =
new QWidget;
219 expressionWidget->setLayout(
new QHBoxLayout );
220 QToolButton *editExpressionButton =
new QToolButton;
221 editExpressionButton->setProperty(
"Index", idx );
223 connect( editExpressionButton, &QAbstractButton::clicked,
this, &QgsSourceFieldsProperties::updateExpression );
224 expressionWidget->layout()->setContentsMargins( 0, 0, 0, 0 );
225 expressionWidget->layout()->addWidget( editExpressionButton );
227 expressionWidget->setStyleSheet(
"background-color: rgb( 200, 200, 255 )" );
228 mFieldsList->setCellWidget( row,
AttrCommentCol, expressionWidget );
235 QList<int> notEditableCols = QList<int>()
245 const auto constNotEditableCols = notEditableCols;
246 for (
const int i : constNotEditableCols )
249 mFieldsList->item( row, i )->setFlags( mFieldsList->item( row, i )->flags() & ~Qt::ItemIsEditable );
251 mFieldsList->item( row, i )->setToolTip( tr(
"Edit alias in the Form config tab" ) );
254 if ( canRenameFields )
255 mFieldsList->item( row,
AttrNameCol )->setFlags( mFieldsList->item( row,
AttrNameCol )->flags() | Qt::ItemIsEditable );
257 mFieldsList->item( row,
AttrNameCol )->setFlags( mFieldsList->item( row,
AttrNameCol )->flags() & ~Qt::ItemIsEditable );
261 const QList<QgsField::ConfigurationFlag> flagList = qgsEnumList<QgsField::ConfigurationFlag>();
269 QVariant::fromValue( flag ) );
286 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() ) );
293 for (
int i = 0; i < mFieldsList->rowCount(); i++ )
295 const int idx = mFieldsList->item( i,
AttrIdCol )->data( Qt::DisplayRole ).toInt();
302 for (
int r = 0; r < model->rowCount(); ++r )
304 const QModelIndex index = model->index( r, 0 );
306 const bool active = model->
data( index, Qt::CheckStateRole ).value<Qt::CheckState>() == Qt::Checked ?
true :
false;
307 flags.setFlag( flag, active );
316void QgsSourceFieldsProperties::editingToggled()
319 updateFieldRenamingStatus();
322void QgsSourceFieldsProperties::addAttributeClicked()
330 if ( dialog.exec() == QDialog::Accepted )
337void QgsSourceFieldsProperties::deleteAttributeClicked()
339 QSet<int> providerFields;
340 QSet<int> expressionFields;
341 const auto constSelectedItems = mFieldsList->selectedItems();
342 for ( QTableWidgetItem *item : constSelectedItems )
344 if ( item->column() == 0 )
351 expressionFields << idx;
353 providerFields << idx;
357 if ( !expressionFields.isEmpty() )
360 if ( !providerFields.isEmpty() )
370void QgsSourceFieldsProperties::calculateFieldClicked()
378 if ( calc.exec() == QDialog::Accepted )
384void QgsSourceFieldsProperties::attributesListCellChanged(
int row,
int column )
390 QTableWidgetItem *nameItem = mFieldsList->item( row, column );
393 nameItem->text().isEmpty() ||
407 QMessageBox::critical(
this, tr(
"Rename Field" ), tr(
"Failed to rename field to '%1'. Is the field name unique?" ).arg( nameItem->text() ) );
412void QgsSourceFieldsProperties::attributesListCellPressed(
int ,
int )
423 const QgsVectorDataProvider::Capabilities cap = provider->
capabilities();
431 mToggleEditingButton->setChecked(
true );
435 mToggleEditingButton->setChecked(
false );
436 mAddAttributeButton->setEnabled(
false );
439 mDeleteAttributeButton->setEnabled( !mFieldsList->selectedItems().isEmpty() );
441 const auto constSelectedItems = mFieldsList->selectedItems();
442 for ( QTableWidgetItem *item : constSelectedItems )
444 if ( item->column() == 0 )
449 mDeleteAttributeButton->setEnabled(
false );
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.
static QString readableConfigurationFlag(QgsField::ConfigurationFlag flag)
Returns the readable and translated value of the configuration flag.
QString friendlyTypeString() const
Returns a user friendly, translated representation of the field type.
ConfigurationFlag
Configuration flags for fields These flags are meant to be user-configurable and are not describing a...
@ None
No flag is defined.
Container of fields for a vector layer.
@ OriginExpression
Field is calculated from an expression.
@ OriginJoin
Field comes from a joined layer (originIndex / 1000 = index of the join, originIndex % 1000 = index w...
int count() const
Returns number of items.
FieldOrigin fieldOrigin(int fieldIdx) const
Returns the field's origin (value from an enumeration).
bool exists(int i) const
Returns if a field index is valid.
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.
@ DeleteAttributes
Allows deletion of attributes (fields)
@ AddAttributes
Allows addition of new attributes (fields)
@ RenameAttributes
Supports renaming attributes (fields). Since QGIS 2.16.
@ ChangeAttributeValues
Allows modification of attribute values.
virtual Q_INVOKABLE QgsVectorDataProvider::Capabilities capabilities() const
Returns flags containing the supported capabilities.
Represents a vector layer which manages a vector based data sets.
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.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
QString expressionField(int index) const
Returns the expression used for a given expression field.
void setFieldConfigurationFlags(int index, QgsField::ConfigurationFlags flags)
Sets the configuration flags of the field at given index.
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.
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.
QgsField::ConfigurationFlags fieldConfigurationFlags(int index) const
Returns the configuration flags of the field at given index.
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.