22 QgsProcessingTinInputLayersWidget::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 Delegate( mTableView ) );
39 QVariant 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 );
56 void 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 );
79 void QgsProcessingTinInputLayersWidget::setProject(
QgsProject *project )
81 mInputLayersModel.setProject( project );
84 void QgsProcessingTinInputLayersWidget::onLayerChanged(
QgsMapLayer *layer )
86 QgsVectorLayer *newLayer = qobject_cast<QgsVectorLayer *>( layer );
88 if ( !newLayer || !newLayer->
isValid() )
96 mComboFields->setLayer( newLayer );
97 mComboFields->setCurrentIndex( 0 );
101 void 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 );
133 void QgsProcessingTinInputLayersWidget::QgsProcessingTinInputLayersWidget::onLayersRemove()
135 mInputLayersModel.removeLayer( mTableView->selectionModel()->currentIndex().row() );
140 QgsProcessingTinInputLayersWidget::QgsProcessingTinInputLayersModel::QgsProcessingTinInputLayersModel(
QgsProject *project ):
144 int QgsProcessingTinInputLayersWidget::QgsProcessingTinInputLayersModel::rowCount(
const QModelIndex &parent )
const
147 return mInputLayers.count();
150 int QgsProcessingTinInputLayersWidget::QgsProcessingTinInputLayersModel::columnCount(
const QModelIndex &parent )
const
156 QVariant QgsProcessingTinInputLayersWidget::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;
236 bool QgsProcessingTinInputLayersWidget::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 ) );
247 Qt::ItemFlags QgsProcessingTinInputLayersWidget::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 );
258 QVariant QgsProcessingTinInputLayersWidget::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 );
289 void QgsProcessingTinInputLayersWidget::QgsProcessingTinInputLayersModel::removeLayer(
int index )
291 if ( index < 0 || index >= mInputLayers.count() )
293 beginRemoveRows( QModelIndex(), index, index );
294 mInputLayers.removeAt( index );
298 void QgsProcessingTinInputLayersWidget::QgsProcessingTinInputLayersModel::clear()
300 mInputLayers.clear();
303 QList<QgsProcessingParameterTinInputLayers::InputLayer> QgsProcessingTinInputLayersWidget::QgsProcessingTinInputLayersModel::layers()
const
308 void QgsProcessingTinInputLayersWidget::QgsProcessingTinInputLayersModel::setProject(
QgsProject *project )
313 QWidget *QgsProcessingTinInputLayersWidget::Delegate::createEditor( QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
317 QComboBox *comboType =
new QComboBox( parent );
323 void QgsProcessingTinInputLayersWidget::Delegate::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 );
336 void QgsProcessingTinInputLayersWidget::Delegate::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 );
347 QString QgsProcessingTinInputLayersWidgetWrapper::parameterType()
const
349 return QStringLiteral(
"tininputlayers" );
354 return new QgsProcessingTinInputLayersWidgetWrapper( parameter, type );
357 QStringList QgsProcessingTinInputLayersWidgetWrapper::compatibleParameterTypes()
const
363 QStringList QgsProcessingTinInputLayersWidgetWrapper::compatibleOutputTypes()
const {
return QStringList();}
365 QWidget *QgsProcessingTinInputLayersWidgetWrapper::createWidget()
367 mWidget =
new QgsProcessingTinInputLayersWidget( widgetContext().project() );
368 connect( mWidget, &QgsProcessingTinInputLayersWidget::changed,
this, [ = ]
370 emit widgetValueHasChanged(
this );
376 void QgsProcessingTinInputLayersWidgetWrapper::setWidgetValue(
const QVariant &value,
QgsProcessingContext &context )
380 mWidget->setValue( value );
381 mWidget->setProject( context.
project() );
384 QVariant QgsProcessingTinInputLayersWidgetWrapper::widgetValue()
const
387 return mWidget->value();