20 QgsProcessingTinInputLayersWidget::QgsProcessingTinInputLayersWidget(
QgsProject *project ):
21 mInputLayersModel( project )
27 connect( mButtonAdd, &QToolButton::clicked,
this, &QgsProcessingTinInputLayersWidget::onCurrentLayerAdded );
28 connect( mButtonRemove, &QToolButton::clicked,
this, &QgsProcessingTinInputLayersWidget::onLayersRemove );
29 connect( &mInputLayersModel, &QgsProcessingTinInputLayersModel::dataChanged,
this, &QgsProcessingTinInputLayersWidget::changed );
31 onLayerChanged( mComboLayers->currentLayer() );
33 mTableView->setModel( &mInputLayersModel );
34 mTableView->setItemDelegateForColumn( 1,
new Delegate( mTableView ) );
37 QVariant QgsProcessingTinInputLayersWidget::value()
const
39 const QList<QgsProcessingParameterTinInputLayers::InputLayer> &layers = mInputLayersModel.layers();
45 layerMap[QStringLiteral(
"source" )] = layer.source;
46 layerMap[QStringLiteral(
"type" )] = layer.type;
47 layerMap[QStringLiteral(
"attributeIndex" )] = layer.attributeIndex;
48 list.append( layerMap );
54 void QgsProcessingTinInputLayersWidget::setValue(
const QVariant &value )
56 mInputLayersModel.clear();
57 if ( !value.isValid() || value.type() != QVariant::List )
60 QVariantList list = value.toList();
62 for (
const QVariant &layerValue : list )
64 if ( layerValue.type() != QVariant::Map )
66 const QVariantMap layerMap = layerValue.toMap();
68 layer.
source = layerMap.value( QStringLiteral(
"source" ) ).toString();
70 layer.
attributeIndex = layerMap.value( QStringLiteral(
"attributeIndex" ) ).toInt();
71 mInputLayersModel.addLayer( layer );
77 void QgsProcessingTinInputLayersWidget::setProject(
QgsProject *project )
79 mInputLayersModel.setProject( project );
82 void QgsProcessingTinInputLayersWidget::onLayerChanged(
QgsMapLayer *layer )
84 QgsVectorLayer *newLayer = qobject_cast<QgsVectorLayer *>( layer );
86 if ( !newLayer || !newLayer->
isValid() )
94 mComboFields->setLayer( newLayer );
95 mComboFields->setCurrentIndex( 0 );
99 void QgsProcessingTinInputLayersWidget::onCurrentLayerAdded()
101 QgsVectorLayer *currentLayer = qobject_cast<QgsVectorLayer *>( mComboLayers->currentLayer() );
105 layer.
source = mComboLayers->currentLayer()->id();
121 if ( mCheckBoxUseZCoordinate->isChecked() && mCheckBoxUseZCoordinate->isEnabled() )
126 mInputLayersModel.addLayer( layer );
131 void QgsProcessingTinInputLayersWidget::QgsProcessingTinInputLayersWidget::onLayersRemove()
133 mInputLayersModel.removeLayer( mTableView->selectionModel()->currentIndex().row() );
138 QgsProcessingTinInputLayersWidget::QgsProcessingTinInputLayersModel::QgsProcessingTinInputLayersModel(
QgsProject *project ):
142 int QgsProcessingTinInputLayersWidget::QgsProcessingTinInputLayersModel::rowCount(
const QModelIndex &parent )
const
145 return mInputLayers.count();
148 int QgsProcessingTinInputLayersWidget::QgsProcessingTinInputLayersModel::columnCount(
const QModelIndex &parent )
const
154 QVariant QgsProcessingTinInputLayersWidget::QgsProcessingTinInputLayersModel::data(
const QModelIndex &index,
int role )
const
156 if ( !index.isValid() )
159 if ( index.row() >= mInputLayers.count() )
164 case Qt::DisplayRole:
167 switch ( index.column() )
171 return layer->
name();
176 switch ( mInputLayers.at( index.row() ).type )
179 return tr(
"Vertices" );
182 return tr(
"Break Lines" );
190 int attributeindex = mInputLayers.at( index.row() ).attributeIndex;
191 if ( attributeindex < 0 )
192 return tr(
"Z coordinate" );
195 if ( attributeindex < layer->fields().count() )
198 return tr(
"Invalid field" );
204 case Qt::ForegroundRole:
205 if ( index.column() == 2 )
207 int attributeindex = mInputLayers.at( index.row() ).attributeIndex;
208 if ( attributeindex < 0 )
209 return QColor( Qt::darkGray );
213 if ( index.column() == 2 )
215 int attributeindex = mInputLayers.at( index.row() ).attributeIndex;
216 if ( attributeindex < 0 )
219 font.setItalic(
true );
225 if ( index.column() == 1 )
226 return mInputLayers.at( index.row() ).type;
234 bool QgsProcessingTinInputLayersWidget::QgsProcessingTinInputLayersModel::setData(
const QModelIndex &index,
const QVariant &value,
int role )
236 if ( index.column() == 1 && role == Qt::EditRole )
239 emit dataChanged( QAbstractTableModel::index( index.row(), 1 ), QAbstractTableModel::index( index.row(), 1 ) );
245 Qt::ItemFlags QgsProcessingTinInputLayersWidget::QgsProcessingTinInputLayersModel::flags(
const QModelIndex &index )
const
247 if ( !index.isValid() )
248 return Qt::NoItemFlags;
250 if ( index.column() == 1 )
251 return QAbstractTableModel::flags( index ) | Qt::ItemIsEditable;
253 return QAbstractTableModel::flags( index );
256 QVariant QgsProcessingTinInputLayersWidget::QgsProcessingTinInputLayersModel::headerData(
int section, Qt::Orientation orientation,
int role )
const
258 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
263 return tr(
"Vector Layer" );
269 return tr(
"Z Value Attribute" );
282 beginInsertRows( QModelIndex(), mInputLayers.count() - 1, mInputLayers.count() - 1 );
283 mInputLayers.append( layer );
287 void QgsProcessingTinInputLayersWidget::QgsProcessingTinInputLayersModel::removeLayer(
int index )
289 if ( index < 0 || index >= mInputLayers.count() )
291 beginRemoveRows( QModelIndex(), index, index );
292 mInputLayers.removeAt( index );
296 void QgsProcessingTinInputLayersWidget::QgsProcessingTinInputLayersModel::clear()
298 mInputLayers.clear();
301 QList<QgsProcessingParameterTinInputLayers::InputLayer> QgsProcessingTinInputLayersWidget::QgsProcessingTinInputLayersModel::layers()
const
306 void QgsProcessingTinInputLayersWidget::QgsProcessingTinInputLayersModel::setProject(
QgsProject *project )
311 QWidget *QgsProcessingTinInputLayersWidget::Delegate::createEditor( QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
315 QComboBox *comboType =
new QComboBox( parent );
321 void QgsProcessingTinInputLayersWidget::Delegate::setEditorData( QWidget *editor,
const QModelIndex &index )
const
323 QComboBox *comboType = qobject_cast<QComboBox *>( editor );
324 Q_ASSERT( comboType );
327 int comboIndex = comboType->findData( type );
328 if ( comboIndex >= 0 )
329 comboType->setCurrentIndex( comboIndex );
331 comboType->setCurrentIndex( 0 );
334 void QgsProcessingTinInputLayersWidget::Delegate::setModelData( QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index )
const
336 QComboBox *comboType = qobject_cast<QComboBox *>( editor );
337 Q_ASSERT( comboType );
338 model->setData( index, comboType->currentData(), Qt::EditRole );
345 QString QgsProcessingTinInputLayersWidgetWrapper::parameterType()
const
347 return QStringLiteral(
"tininputlayers" );
352 return new QgsProcessingTinInputLayersWidgetWrapper( parameter, type );
355 QStringList QgsProcessingTinInputLayersWidgetWrapper::compatibleParameterTypes()
const {
return QStringList();}
357 QStringList QgsProcessingTinInputLayersWidgetWrapper::compatibleOutputTypes()
const {
return QStringList();}
359 QWidget *QgsProcessingTinInputLayersWidgetWrapper::createWidget()
361 mWidget =
new QgsProcessingTinInputLayersWidget( widgetContext().project() );
362 connect( mWidget, &QgsProcessingTinInputLayersWidget::changed,
this, [ = ]
364 emit widgetValueHasChanged(
this );
370 void QgsProcessingTinInputLayersWidgetWrapper::setWidgetValue(
const QVariant &value,
QgsProcessingContext &context )
374 mWidget->setValue( value );
375 mWidget->setProject( context.
project() );
378 QVariant QgsProcessingTinInputLayersWidgetWrapper::widgetValue()
const
381 return mWidget->value();