17#include "moc_qgsprocessingtininputlayerswidget.cpp"
23QgsProcessingTinInputLayersWidget::QgsProcessingTinInputLayersWidget(
QgsProject *project )
24 : mInputLayersModel( project )
30 connect( mButtonAdd, &QToolButton::clicked,
this, &QgsProcessingTinInputLayersWidget::onCurrentLayerAdded );
31 connect( mButtonRemove, &QToolButton::clicked,
this, &QgsProcessingTinInputLayersWidget::onLayersRemove );
32 connect( &mInputLayersModel, &QgsProcessingTinInputLayersModel::dataChanged,
this, &QgsProcessingTinInputLayersWidget::changed );
34 onLayerChanged( mComboLayers->currentLayer() );
36 mTableView->setModel( &mInputLayersModel );
37 mTableView->setItemDelegateForColumn( 1,
new QgsProcessingTinInputLayersDelegate( mTableView ) );
40QVariant QgsProcessingTinInputLayersWidget::value()
const
42 const QList<QgsProcessingParameterTinInputLayers::InputLayer> &layers = mInputLayersModel.layers();
48 layerMap[QStringLiteral(
"source" )] = layer.source;
49 layerMap[QStringLiteral(
"type" )] =
static_cast<int>( layer.type );
50 layerMap[QStringLiteral(
"attributeIndex" )] = layer.attributeIndex;
51 list.append( layerMap );
57void QgsProcessingTinInputLayersWidget::setValue(
const QVariant &value )
59 mInputLayersModel.clear();
60 if ( !value.isValid() || value.userType() != QMetaType::Type::QVariantList )
63 const QVariantList list = value.toList();
65 for (
const QVariant &layerValue : list )
67 if ( layerValue.userType() != QMetaType::Type::QVariantMap )
69 const QVariantMap layerMap = layerValue.toMap();
71 layer.
source = layerMap.value( QStringLiteral(
"source" ) ).toString();
73 layer.
attributeIndex = layerMap.value( QStringLiteral(
"attributeIndex" ) ).toInt();
74 mInputLayersModel.addLayer( layer );
80void QgsProcessingTinInputLayersWidget::setProject(
QgsProject *project )
82 mInputLayersModel.setProject( project );
85void QgsProcessingTinInputLayersWidget::onLayerChanged(
QgsMapLayer *layer )
87 QgsVectorLayer *newLayer = qobject_cast<QgsVectorLayer *>( layer );
89 if ( !newLayer || !newLayer->
isValid() )
97 mComboFields->setLayer( newLayer );
98 mComboFields->setCurrentIndex( 0 );
102void QgsProcessingTinInputLayersWidget::onCurrentLayerAdded()
104 QgsVectorLayer *currentLayer = qobject_cast<QgsVectorLayer *>( mComboLayers->currentLayer() );
108 layer.
source = mComboLayers->currentLayer()->id();
124 if ( mCheckBoxUseZCoordinate->isChecked() && mCheckBoxUseZCoordinate->isEnabled() )
129 mInputLayersModel.addLayer( layer );
134void QgsProcessingTinInputLayersWidget::QgsProcessingTinInputLayersWidget::onLayersRemove()
136 mInputLayersModel.removeLayer( mTableView->selectionModel()->currentIndex().row() );
141QgsProcessingTinInputLayersModel::QgsProcessingTinInputLayersModel(
QgsProject *project )
142 : mProject( project )
145int QgsProcessingTinInputLayersModel::rowCount(
const QModelIndex &parent )
const
148 return mInputLayers.count();
151int QgsProcessingTinInputLayersModel::columnCount(
const QModelIndex &parent )
const
157QVariant QgsProcessingTinInputLayersModel::data(
const QModelIndex &index,
int role )
const
159 if ( !index.isValid() )
162 if ( index.row() >= mInputLayers.count() )
167 case Qt::DisplayRole:
170 switch ( index.column() )
174 return layer->
name();
179 switch ( mInputLayers.at( index.row() ).type )
182 return tr(
"Vertices" );
185 return tr(
"Break Lines" );
193 const int attributeindex = mInputLayers.at( index.row() ).attributeIndex;
194 if ( attributeindex < 0 )
195 return tr(
"Z coordinate" );
198 if ( attributeindex < layer->fields().count() )
201 return tr(
"Invalid field" );
207 case Qt::ForegroundRole:
208 if ( index.column() == 2 )
210 const int attributeindex = mInputLayers.at( index.row() ).attributeIndex;
211 if ( attributeindex < 0 )
212 return QColor( Qt::darkGray );
216 if ( index.column() == 2 )
218 const int attributeindex = mInputLayers.at( index.row() ).attributeIndex;
219 if ( attributeindex < 0 )
222 font.setItalic(
true );
228 if ( index.column() == 1 )
229 return static_cast<int>( mInputLayers.at( index.row() ).type );
237bool QgsProcessingTinInputLayersModel::setData(
const QModelIndex &index,
const QVariant &value,
int role )
239 if ( index.column() == 1 && role == Qt::EditRole )
242 emit dataChanged( QAbstractTableModel::index( index.row(), 1 ), QAbstractTableModel::index( index.row(), 1 ) );
248Qt::ItemFlags QgsProcessingTinInputLayersModel::flags(
const QModelIndex &index )
const
250 if ( !index.isValid() )
251 return Qt::NoItemFlags;
253 if ( index.column() == 1 )
254 return QAbstractTableModel::flags( index ) | Qt::ItemIsEditable;
256 return QAbstractTableModel::flags( index );
259QVariant QgsProcessingTinInputLayersModel::headerData(
int section, Qt::Orientation orientation,
int role )
const
261 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
266 return tr(
"Vector Layer" );
272 return tr(
"Z Value Attribute" );
285 beginInsertRows( QModelIndex(), mInputLayers.count() - 1, mInputLayers.count() - 1 );
286 mInputLayers.append( layer );
290void QgsProcessingTinInputLayersModel::removeLayer(
int index )
292 if ( index < 0 || index >= mInputLayers.count() )
294 beginRemoveRows( QModelIndex(), index, index );
295 mInputLayers.removeAt( index );
299void QgsProcessingTinInputLayersModel::clear()
301 mInputLayers.clear();
304QList<QgsProcessingParameterTinInputLayers::InputLayer> QgsProcessingTinInputLayersModel::layers()
const
309void QgsProcessingTinInputLayersModel::setProject(
QgsProject *project )
314QWidget *QgsProcessingTinInputLayersDelegate::createEditor( QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
318 QComboBox *comboType =
new QComboBox( parent );
324void QgsProcessingTinInputLayersDelegate::setEditorData( QWidget *editor,
const QModelIndex &index )
const
326 QComboBox *comboType = qobject_cast<QComboBox *>( editor );
327 Q_ASSERT( comboType );
329 const int comboIndex = comboType->findData(
static_cast<int>( 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, [=] {
369 emit widgetValueHasChanged(
this );
375void QgsProcessingTinInputLayersWidgetWrapper::setWidgetValue(
const QVariant &value,
QgsProcessingContext &context )
379 mWidget->setValue( value );
380 mWidget->setProject( context.
project() );
383QVariant QgsProcessingTinInputLayersWidgetWrapper::widgetValue()
const
386 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.
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.
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.