22QgsProcessingTinInputLayersWidget::QgsProcessingTinInputLayersWidget(
QgsProject *project ):
23 mInputLayersModel( project )
29 connect( mButtonAdd, &QToolButton::clicked,
this, &QgsProcessingTinInputLayersWidget::onCurrentLayerAdded );
30 connect( mButtonRemove, &QToolButton::clicked,
this, &QgsProcessingTinInputLayersWidget::onLayersRemove );
31 connect( &mInputLayersModel, &QgsProcessingTinInputLayersModel::dataChanged,
this, &QgsProcessingTinInputLayersWidget::changed );
33 onLayerChanged( mComboLayers->currentLayer() );
35 mTableView->setModel( &mInputLayersModel );
36 mTableView->setItemDelegateForColumn( 1,
new QgsProcessingTinInputLayersDelegate( mTableView ) );
39QVariant QgsProcessingTinInputLayersWidget::value()
const
41 const QList<QgsProcessingParameterTinInputLayers::InputLayer> &layers = mInputLayersModel.layers();
47 layerMap[QStringLiteral(
"source" )] = layer.source;
48 layerMap[QStringLiteral(
"type" )] = layer.type;
49 layerMap[QStringLiteral(
"attributeIndex" )] = layer.attributeIndex;
50 list.append( layerMap );
56void QgsProcessingTinInputLayersWidget::setValue(
const QVariant &value )
58 mInputLayersModel.clear();
59 if ( !value.isValid() || value.type() != QVariant::List )
62 const QVariantList list = value.toList();
64 for (
const QVariant &layerValue : list )
66 if ( layerValue.type() != QVariant::Map )
68 const QVariantMap layerMap = layerValue.toMap();
70 layer.
source = layerMap.value( QStringLiteral(
"source" ) ).toString();
72 layer.
attributeIndex = layerMap.value( QStringLiteral(
"attributeIndex" ) ).toInt();
73 mInputLayersModel.addLayer( layer );
79void QgsProcessingTinInputLayersWidget::setProject(
QgsProject *project )
81 mInputLayersModel.setProject( project );
84void QgsProcessingTinInputLayersWidget::onLayerChanged(
QgsMapLayer *layer )
86 QgsVectorLayer *newLayer = qobject_cast<QgsVectorLayer *>( layer );
88 if ( !newLayer || !newLayer->
isValid() )
96 mComboFields->setLayer( newLayer );
97 mComboFields->setCurrentIndex( 0 );
101void QgsProcessingTinInputLayersWidget::onCurrentLayerAdded()
103 QgsVectorLayer *currentLayer = qobject_cast<QgsVectorLayer *>( mComboLayers->currentLayer() );
107 layer.
source = mComboLayers->currentLayer()->id();
123 if ( mCheckBoxUseZCoordinate->isChecked() && mCheckBoxUseZCoordinate->isEnabled() )
128 mInputLayersModel.addLayer( layer );
133void QgsProcessingTinInputLayersWidget::QgsProcessingTinInputLayersWidget::onLayersRemove()
135 mInputLayersModel.removeLayer( mTableView->selectionModel()->currentIndex().row() );
140QgsProcessingTinInputLayersModel::QgsProcessingTinInputLayersModel(
QgsProject *project ):
144int QgsProcessingTinInputLayersModel::rowCount(
const QModelIndex &parent )
const
147 return mInputLayers.count();
150int QgsProcessingTinInputLayersModel::columnCount(
const QModelIndex &parent )
const
156QVariant QgsProcessingTinInputLayersModel::data(
const QModelIndex &index,
int role )
const
158 if ( !index.isValid() )
161 if ( index.row() >= mInputLayers.count() )
166 case Qt::DisplayRole:
169 switch ( index.column() )
173 return layer->
name();
178 switch ( mInputLayers.at( index.row() ).type )
181 return tr(
"Vertices" );
184 return tr(
"Break Lines" );
192 const int attributeindex = mInputLayers.at( index.row() ).attributeIndex;
193 if ( attributeindex < 0 )
194 return tr(
"Z coordinate" );
197 if ( attributeindex < layer->fields().count() )
200 return tr(
"Invalid field" );
206 case Qt::ForegroundRole:
207 if ( index.column() == 2 )
209 const int attributeindex = mInputLayers.at( index.row() ).attributeIndex;
210 if ( attributeindex < 0 )
211 return QColor( Qt::darkGray );
215 if ( index.column() == 2 )
217 const int attributeindex = mInputLayers.at( index.row() ).attributeIndex;
218 if ( attributeindex < 0 )
221 font.setItalic(
true );
227 if ( index.column() == 1 )
228 return mInputLayers.at( index.row() ).type;
236bool QgsProcessingTinInputLayersModel::setData(
const QModelIndex &index,
const QVariant &value,
int role )
238 if ( index.column() == 1 && role == Qt::EditRole )
241 emit dataChanged( QAbstractTableModel::index( index.row(), 1 ), QAbstractTableModel::index( index.row(), 1 ) );
247Qt::ItemFlags QgsProcessingTinInputLayersModel::flags(
const QModelIndex &index )
const
249 if ( !index.isValid() )
250 return Qt::NoItemFlags;
252 if ( index.column() == 1 )
253 return QAbstractTableModel::flags( index ) | Qt::ItemIsEditable;
255 return QAbstractTableModel::flags( index );
258QVariant QgsProcessingTinInputLayersModel::headerData(
int section, Qt::Orientation orientation,
int role )
const
260 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
265 return tr(
"Vector Layer" );
271 return tr(
"Z Value Attribute" );
284 beginInsertRows( QModelIndex(), mInputLayers.count() - 1, mInputLayers.count() - 1 );
285 mInputLayers.append( layer );
289void QgsProcessingTinInputLayersModel::removeLayer(
int index )
291 if ( index < 0 || index >= mInputLayers.count() )
293 beginRemoveRows( QModelIndex(), index, index );
294 mInputLayers.removeAt( index );
298void QgsProcessingTinInputLayersModel::clear()
300 mInputLayers.clear();
303QList<QgsProcessingParameterTinInputLayers::InputLayer> QgsProcessingTinInputLayersModel::layers()
const
308void QgsProcessingTinInputLayersModel::setProject(
QgsProject *project )
313QWidget *QgsProcessingTinInputLayersDelegate::createEditor( QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
317 QComboBox *comboType =
new QComboBox( parent );
323void QgsProcessingTinInputLayersDelegate::setEditorData( QWidget *editor,
const QModelIndex &index )
const
325 QComboBox *comboType = qobject_cast<QComboBox *>( editor );
326 Q_ASSERT( comboType );
329 const int comboIndex = comboType->findData( type );
330 if ( comboIndex >= 0 )
331 comboType->setCurrentIndex( comboIndex );
333 comboType->setCurrentIndex( 0 );
336void QgsProcessingTinInputLayersDelegate::setModelData( QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index )
const
338 QComboBox *comboType = qobject_cast<QComboBox *>( editor );
339 Q_ASSERT( comboType );
340 model->setData( index, comboType->currentData(), Qt::EditRole );
347QString QgsProcessingTinInputLayersWidgetWrapper::parameterType()
const
349 return QStringLiteral(
"tininputlayers" );
354 return new QgsProcessingTinInputLayersWidgetWrapper( parameter, type );
357QStringList QgsProcessingTinInputLayersWidgetWrapper::compatibleParameterTypes()
const
363QStringList QgsProcessingTinInputLayersWidgetWrapper::compatibleOutputTypes()
const {
return QStringList();}
365QWidget *QgsProcessingTinInputLayersWidgetWrapper::createWidget()
367 mWidget =
new QgsProcessingTinInputLayersWidget( widgetContext().project() );
368 connect( mWidget, &QgsProcessingTinInputLayersWidget::changed,
this, [ = ]
370 emit widgetValueHasChanged(
this );
376void QgsProcessingTinInputLayersWidgetWrapper::setWidgetValue(
const QVariant &value,
QgsProcessingContext &context )
380 mWidget->setValue( value );
381 mWidget->setProject( context.
project() );
384QVariant QgsProcessingTinInputLayersWidgetWrapper::widgetValue()
const
387 return mWidget->value();
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.
WidgetType
Types of dialogs which Processing widgets can be created for.
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.
This is the 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 data sets.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.
Q_INVOKABLE Qgis::GeometryType geometryType() const
Returns point, line or polygon.
static bool hasZ(Qgis::WkbType type)
Tests whether a WKB type contains the z-dimension.