25 #include "qgsnative.h"
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();
108 void 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 );
121 void QgsSourceFieldsProperties::updateExpression()
123 QToolButton *btn = qobject_cast<QToolButton *>( sender() );
126 const int index = btn->property(
"Index" ).toInt();
143 void 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 );
187 void QgsSourceFieldsProperties::attributeDeleted(
int idx )
199 QTableWidgetItem *dataItem =
new QTableWidgetItem();
200 dataItem->setData( Qt::DisplayRole, idx );
202 mFieldsList->setItem( row,
AttrIdCol, dataItem );
211 mFieldsList->setItem( row,
AttrTypeCol,
new QTableWidgetItem( QVariant::typeToName(
field.
type() ) ) );
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 );
316 void QgsSourceFieldsProperties::editingToggled()
319 updateFieldRenamingStatus();
322 void QgsSourceFieldsProperties::addAttributeClicked()
330 if ( dialog.exec() == QDialog::Accepted )
337 void 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() )
370 void QgsSourceFieldsProperties::calculateFieldClicked()
378 if ( calc.exec() == QDialog::Accepted )
384 void 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() ) );
412 void 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 );