21#include "moc_qgsprocessingtininputlayerswidget.cpp"
25QgsProcessingTinInputLayersWidget::QgsProcessingTinInputLayersWidget(
QgsProject *project )
26 : mInputLayersModel( project )
32 connect( mButtonAdd, &QToolButton::clicked,
this, &QgsProcessingTinInputLayersWidget::onCurrentLayerAdded );
33 connect( mButtonRemove, &QToolButton::clicked,
this, &QgsProcessingTinInputLayersWidget::onLayersRemove );
34 connect( &mInputLayersModel, &QgsProcessingTinInputLayersModel::dataChanged,
this, &QgsProcessingTinInputLayersWidget::changed );
36 onLayerChanged( mComboLayers->currentLayer() );
38 mTableView->setModel( &mInputLayersModel );
39 mTableView->setItemDelegateForColumn( 1,
new QgsProcessingTinInputLayersDelegate( mTableView ) );
42QVariant QgsProcessingTinInputLayersWidget::value()
const
44 const QList<QgsProcessingParameterTinInputLayers::InputLayer> &layers = mInputLayersModel.layers();
50 layerMap[QStringLiteral(
"source" )] = layer.source;
51 layerMap[QStringLiteral(
"type" )] =
static_cast<int>( layer.type );
52 layerMap[QStringLiteral(
"attributeIndex" )] = layer.attributeIndex;
53 list.append( layerMap );
59void QgsProcessingTinInputLayersWidget::setValue(
const QVariant &value )
61 mInputLayersModel.clear();
62 if ( !value.isValid() || value.userType() != QMetaType::Type::QVariantList )
65 const QVariantList list = value.toList();
67 for (
const QVariant &layerValue : list )
69 if ( layerValue.userType() != QMetaType::Type::QVariantMap )
71 const QVariantMap layerMap = layerValue.toMap();
73 layer.
source = layerMap.value( QStringLiteral(
"source" ) ).toString();
75 layer.
attributeIndex = layerMap.value( QStringLiteral(
"attributeIndex" ) ).toInt();
76 mInputLayersModel.addLayer( layer );
82void QgsProcessingTinInputLayersWidget::setProject(
QgsProject *project )
84 mInputLayersModel.setProject( project );
87void QgsProcessingTinInputLayersWidget::onLayerChanged(
QgsMapLayer *layer )
89 QgsVectorLayer *newLayer = qobject_cast<QgsVectorLayer *>( layer );
91 if ( !newLayer || !newLayer->
isValid() )
99 mComboFields->setLayer( newLayer );
100 mComboFields->setCurrentIndex( 0 );
104void QgsProcessingTinInputLayersWidget::onCurrentLayerAdded()
106 QgsVectorLayer *currentLayer = qobject_cast<QgsVectorLayer *>( mComboLayers->currentLayer() );
110 layer.
source = mComboLayers->currentLayer()->id();
126 if ( mCheckBoxUseZCoordinate->isChecked() && mCheckBoxUseZCoordinate->isEnabled() )
131 mInputLayersModel.addLayer( layer );
136void QgsProcessingTinInputLayersWidget::QgsProcessingTinInputLayersWidget::onLayersRemove()
138 mInputLayersModel.removeLayer( mTableView->selectionModel()->currentIndex().row() );
143QgsProcessingTinInputLayersModel::QgsProcessingTinInputLayersModel(
QgsProject *project )
144 : mProject( project )
147int QgsProcessingTinInputLayersModel::rowCount(
const QModelIndex &parent )
const
150 return mInputLayers.count();
153int QgsProcessingTinInputLayersModel::columnCount(
const QModelIndex &parent )
const
159QVariant QgsProcessingTinInputLayersModel::data(
const QModelIndex &index,
int role )
const
161 if ( !index.isValid() )
164 if ( index.row() >= mInputLayers.count() )
169 case Qt::DisplayRole:
172 switch ( index.column() )
176 return layer->
name();
181 switch ( mInputLayers.at( index.row() ).type )
184 return tr(
"Vertices" );
187 return tr(
"Break Lines" );
195 const int attributeindex = mInputLayers.at( index.row() ).attributeIndex;
196 if ( attributeindex < 0 )
197 return tr(
"Z coordinate" );
200 if ( attributeindex < layer->fields().count() )
203 return tr(
"Invalid field" );
209 case Qt::ForegroundRole:
210 if ( index.column() == 2 )
212 const int attributeindex = mInputLayers.at( index.row() ).attributeIndex;
213 if ( attributeindex < 0 )
214 return QColor( Qt::darkGray );
218 if ( index.column() == 2 )
220 const int attributeindex = mInputLayers.at( index.row() ).attributeIndex;
221 if ( attributeindex < 0 )
224 font.setItalic(
true );
230 if ( index.column() == 1 )
231 return static_cast<int>( mInputLayers.at( index.row() ).type );
239bool QgsProcessingTinInputLayersModel::setData(
const QModelIndex &index,
const QVariant &value,
int role )
241 if ( index.column() == 1 && role == Qt::EditRole )
244 emit dataChanged( QAbstractTableModel::index( index.row(), 1 ), QAbstractTableModel::index( index.row(), 1 ) );
250Qt::ItemFlags QgsProcessingTinInputLayersModel::flags(
const QModelIndex &index )
const
252 if ( !index.isValid() )
253 return Qt::NoItemFlags;
255 if ( index.column() == 1 )
256 return QAbstractTableModel::flags( index ) | Qt::ItemIsEditable;
258 return QAbstractTableModel::flags( index );
261QVariant QgsProcessingTinInputLayersModel::headerData(
int section, Qt::Orientation orientation,
int role )
const
263 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
268 return tr(
"Vector Layer" );
274 return tr(
"Z Value Attribute" );
287 beginInsertRows( QModelIndex(), mInputLayers.count() - 1, mInputLayers.count() - 1 );
288 mInputLayers.append( layer );
292void QgsProcessingTinInputLayersModel::removeLayer(
int index )
294 if ( index < 0 || index >= mInputLayers.count() )
296 beginRemoveRows( QModelIndex(), index, index );
297 mInputLayers.removeAt( index );
301void QgsProcessingTinInputLayersModel::clear()
303 mInputLayers.clear();
306QList<QgsProcessingParameterTinInputLayers::InputLayer> QgsProcessingTinInputLayersModel::layers()
const
311void QgsProcessingTinInputLayersModel::setProject(
QgsProject *project )
316QWidget *QgsProcessingTinInputLayersDelegate::createEditor( QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
320 QComboBox *comboType =
new QComboBox( parent );
326void QgsProcessingTinInputLayersDelegate::setEditorData( QWidget *editor,
const QModelIndex &index )
const
328 QComboBox *comboType = qobject_cast<QComboBox *>( editor );
329 Q_ASSERT( comboType );
331 const int comboIndex = comboType->findData(
static_cast<int>( type ) );
332 if ( comboIndex >= 0 )
333 comboType->setCurrentIndex( comboIndex );
335 comboType->setCurrentIndex( 0 );
338void QgsProcessingTinInputLayersDelegate::setModelData( QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index )
const
340 QComboBox *comboType = qobject_cast<QComboBox *>( editor );
341 Q_ASSERT( comboType );
342 model->setData( index, comboType->currentData(), Qt::EditRole );
349QString QgsProcessingTinInputLayersWidgetWrapper::parameterType()
const
351 return QStringLiteral(
"tininputlayers" );
356 return new QgsProcessingTinInputLayersWidgetWrapper( parameter, type );
359QWidget *QgsProcessingTinInputLayersWidgetWrapper::createWidget()
361 mWidget =
new QgsProcessingTinInputLayersWidget( widgetContext().project() );
362 connect( mWidget, &QgsProcessingTinInputLayersWidget::changed,
this, [
this] {
363 emit widgetValueHasChanged(
this );
369void QgsProcessingTinInputLayersWidgetWrapper::setWidgetValue(
const QVariant &value,
QgsProcessingContext &context )
373 mWidget->setValue( value );
374 mWidget->setProject( context.
project() );
377QVariant QgsProcessingTinInputLayersWidgetWrapper::widgetValue()
const
380 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.