23#include "moc_qgsprocessingtininputlayerswidget.cpp"
25using namespace Qt::StringLiterals;
29QgsProcessingTinInputLayersWidget::QgsProcessingTinInputLayersWidget(
QgsProject *project )
30 : mInputLayersModel( project )
36 connect( mButtonAdd, &QToolButton::clicked,
this, &QgsProcessingTinInputLayersWidget::onCurrentLayerAdded );
37 connect( mButtonRemove, &QToolButton::clicked,
this, &QgsProcessingTinInputLayersWidget::onLayersRemove );
38 connect( &mInputLayersModel, &QgsProcessingTinInputLayersModel::dataChanged,
this, &QgsProcessingTinInputLayersWidget::changed );
40 onLayerChanged( mComboLayers->currentLayer() );
42 mTableView->setModel( &mInputLayersModel );
43 mTableView->setItemDelegateForColumn( 1,
new QgsProcessingTinInputLayersDelegate( mTableView ) );
46QVariant QgsProcessingTinInputLayersWidget::value()
const
48 const QList<QgsProcessingParameterTinInputLayers::InputLayer> &layers = mInputLayersModel.layers();
54 layerMap[u
"source"_s] = layer.source;
55 layerMap[u
"type"_s] =
static_cast<int>( layer.type );
56 layerMap[u
"attributeIndex"_s] = layer.attributeIndex;
57 list.append( layerMap );
63void QgsProcessingTinInputLayersWidget::setValue(
const QVariant &value )
65 mInputLayersModel.clear();
66 if ( !value.isValid() || value.userType() != QMetaType::Type::QVariantList )
69 const QVariantList list = value.toList();
71 for (
const QVariant &layerValue : list )
73 if ( layerValue.userType() != QMetaType::Type::QVariantMap )
75 const QVariantMap layerMap = layerValue.toMap();
77 layer.
source = layerMap.value( u
"source"_s ).toString();
79 layer.
attributeIndex = layerMap.value( u
"attributeIndex"_s ).toInt();
80 mInputLayersModel.addLayer( layer );
86void QgsProcessingTinInputLayersWidget::setProject(
QgsProject *project )
88 mInputLayersModel.setProject( project );
91void QgsProcessingTinInputLayersWidget::onLayerChanged(
QgsMapLayer *layer )
93 QgsVectorLayer *newLayer = qobject_cast<QgsVectorLayer *>( layer );
95 if ( !newLayer || !newLayer->
isValid() )
103 mComboFields->setLayer( newLayer );
104 mComboFields->setCurrentIndex( 0 );
108void QgsProcessingTinInputLayersWidget::onCurrentLayerAdded()
110 QgsVectorLayer *currentLayer = qobject_cast<QgsVectorLayer *>( mComboLayers->currentLayer() );
114 layer.
source = mComboLayers->currentLayer()->id();
130 if ( mCheckBoxUseZCoordinate->isChecked() && mCheckBoxUseZCoordinate->isEnabled() )
135 mInputLayersModel.addLayer( layer );
140void QgsProcessingTinInputLayersWidget::QgsProcessingTinInputLayersWidget::onLayersRemove()
142 mInputLayersModel.removeLayer( mTableView->selectionModel()->currentIndex().row() );
147QgsProcessingTinInputLayersModel::QgsProcessingTinInputLayersModel(
QgsProject *project )
148 : mProject( project )
151int QgsProcessingTinInputLayersModel::rowCount(
const QModelIndex &parent )
const
154 return mInputLayers.count();
157int QgsProcessingTinInputLayersModel::columnCount(
const QModelIndex &parent )
const
163QVariant QgsProcessingTinInputLayersModel::data(
const QModelIndex &index,
int role )
const
165 if ( !index.isValid() )
168 if ( index.row() >= mInputLayers.count() )
173 case Qt::DisplayRole:
176 switch ( index.column() )
180 return layer->
name();
185 switch ( mInputLayers.at( index.row() ).type )
188 return tr(
"Vertices" );
191 return tr(
"Break Lines" );
199 const int attributeindex = mInputLayers.at( index.row() ).attributeIndex;
200 if ( attributeindex < 0 )
201 return tr(
"Z coordinate" );
204 if ( attributeindex < layer->fields().count() )
207 return tr(
"Invalid field" );
213 case Qt::ForegroundRole:
214 if ( index.column() == 2 )
216 const int attributeindex = mInputLayers.at( index.row() ).attributeIndex;
217 if ( attributeindex < 0 )
218 return QColor( Qt::darkGray );
222 if ( index.column() == 2 )
224 const int attributeindex = mInputLayers.at( index.row() ).attributeIndex;
225 if ( attributeindex < 0 )
228 font.setItalic(
true );
234 if ( index.column() == 1 )
235 return static_cast<int>( mInputLayers.at( index.row() ).type );
243bool QgsProcessingTinInputLayersModel::setData(
const QModelIndex &index,
const QVariant &value,
int role )
245 if ( index.column() == 1 && role == Qt::EditRole )
248 emit dataChanged( QAbstractTableModel::index( index.row(), 1 ), QAbstractTableModel::index( index.row(), 1 ) );
254Qt::ItemFlags QgsProcessingTinInputLayersModel::flags(
const QModelIndex &index )
const
256 if ( !index.isValid() )
257 return Qt::NoItemFlags;
259 if ( index.column() == 1 )
260 return QAbstractTableModel::flags( index ) | Qt::ItemIsEditable;
262 return QAbstractTableModel::flags( index );
265QVariant QgsProcessingTinInputLayersModel::headerData(
int section, Qt::Orientation orientation,
int role )
const
267 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
272 return tr(
"Vector Layer" );
278 return tr(
"Z Value Attribute" );
291 beginInsertRows( QModelIndex(), mInputLayers.count() - 1, mInputLayers.count() - 1 );
292 mInputLayers.append( layer );
296void QgsProcessingTinInputLayersModel::removeLayer(
int index )
298 if ( index < 0 || index >= mInputLayers.count() )
300 beginRemoveRows( QModelIndex(), index, index );
301 mInputLayers.removeAt( index );
305void QgsProcessingTinInputLayersModel::clear()
307 mInputLayers.clear();
310QList<QgsProcessingParameterTinInputLayers::InputLayer> QgsProcessingTinInputLayersModel::layers()
const
315void QgsProcessingTinInputLayersModel::setProject(
QgsProject *project )
320QWidget *QgsProcessingTinInputLayersDelegate::createEditor( QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
324 QComboBox *comboType =
new QComboBox( parent );
330void QgsProcessingTinInputLayersDelegate::setEditorData( QWidget *editor,
const QModelIndex &index )
const
332 QComboBox *comboType = qobject_cast<QComboBox *>( editor );
333 Q_ASSERT( comboType );
335 const int comboIndex = comboType->findData(
static_cast<int>( type ) );
336 if ( comboIndex >= 0 )
337 comboType->setCurrentIndex( comboIndex );
339 comboType->setCurrentIndex( 0 );
342void QgsProcessingTinInputLayersDelegate::setModelData( QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index )
const
344 QComboBox *comboType = qobject_cast<QComboBox *>( editor );
345 Q_ASSERT( comboType );
346 model->setData( index, comboType->currentData(), Qt::EditRole );
353QString QgsProcessingTinInputLayersWidgetWrapper::parameterType()
const
355 return u
"tininputlayers"_s;
360 return new QgsProcessingTinInputLayersWidgetWrapper( parameter, type );
363QWidget *QgsProcessingTinInputLayersWidgetWrapper::createWidget()
365 mWidget =
new QgsProcessingTinInputLayersWidget( widgetContext().project() );
366 connect( mWidget, &QgsProcessingTinInputLayersWidget::changed,
this, [
this] {
367 emit widgetValueHasChanged(
this );
373void QgsProcessingTinInputLayersWidgetWrapper::setWidgetValue(
const QVariant &value,
QgsProcessingContext &context )
377 mWidget->setValue( value );
378 mWidget->setProject( context.
project() );
381QVariant QgsProcessingTinInputLayersWidgetWrapper::widgetValue()
const
384 return mWidget->value();
ProcessingTinInputLayerType
Defines the type of input layer for a Processing TIN input.
@ BreakLines
Input that adds vertices and break lines.
@ Vertices
Input that adds only vertices.
ProcessingMode
Types of modes which Processing widgets can be created for.
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
void layerChanged(QgsMapLayer *layer)
Emitted whenever the currently selected layer changes.
Base class for all map layer types.
Contains information about the context in which a processing algorithm is executed.
QgsProject * project() const
Returns the project in which the algorithm is being executed.
Base class for the definition of processing parameters.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
static QgsProject * instance()
Returns the QgsProject singleton instance.
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
Base class for vector data providers.
Qgis::WkbType wkbType() const override=0
Returns the geometry type which is returned by this layer.
Represents a vector layer which manages a vector based dataset.
Q_INVOKABLE Qgis::GeometryType geometryType() const
Returns point, line or polygon.
QgsVectorDataProvider * dataProvider() final
Returns the layer's data provider, it may be nullptr.
static Q_INVOKABLE bool hasZ(Qgis::WkbType type)
Tests whether a WKB type contains the z-dimension.