27#include "moc_qgsprocessingdefaultstyledialog.cpp"
29using namespace Qt::StringLiterals;
36QgsProcessingDefaultStyleDelegate::QgsProcessingDefaultStyleDelegate( QObject *parent )
37 : QStyledItemDelegate( parent )
40QWidget *QgsProcessingDefaultStyleDelegate::createEditor( QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
42 if ( index.column() == 1 )
46 editor->setFilter( u
"%1 (*.qml *.QML)"_s.arg( tr(
"QGIS Layer Style File" ) ) );
50 return QStyledItemDelegate::createEditor( parent, option, index );
53void QgsProcessingDefaultStyleDelegate::setEditorData( QWidget *editor,
const QModelIndex &index )
const
55 if (
auto *fileWidget = qobject_cast<QgsFileWidget *>( editor ) )
57 fileWidget->setFilePath( index.model()->data( index, Qt::EditRole ).toString() );
61 QStyledItemDelegate::setEditorData( editor, index );
65void QgsProcessingDefaultStyleDelegate::setModelData( QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index )
const
67 if (
auto *fileWidget = qobject_cast<QgsFileWidget *>( editor ) )
69 model->setData( index, fileWidget->filePath(), Qt::EditRole );
73 QStyledItemDelegate::setModelData( editor, model, index );
77void QgsProcessingDefaultStyleDelegate::updateEditorGeometry( QWidget *editor,
const QStyleOptionViewItem &option,
const QModelIndex & )
const
79 editor->setGeometry( option.rect );
87 : QAbstractTableModel( parent )
101 item.name = output->name();
102 item.description = output->description();
104 mItems.append( item );
109int QgsProcessingDefaultStylesModel::rowCount(
const QModelIndex &parent )
const
111 if ( parent.isValid() )
114 return mItems.size();
117int QgsProcessingDefaultStylesModel::columnCount(
const QModelIndex &parent )
const
119 if ( parent.isValid() )
124QVariant QgsProcessingDefaultStylesModel::data(
const QModelIndex &index,
int role )
const
126 if ( !index.isValid() || index.row() >= mItems.size() )
129 const OutputItem &item = mItems.at( index.row() );
133 case Qt::DisplayRole:
135 case Qt::ToolTipRole:
136 switch ( index.column() )
138 case Column::OutputName:
140 return item.description.isEmpty() ? item.name : item.description;
143 case Column::StylePath:
144 return item.stylePath;
158bool QgsProcessingDefaultStylesModel::setData(
const QModelIndex &index,
const QVariant &value,
int role )
160 if ( index.isValid() && index.column() == Column::StylePath && role == Qt::EditRole )
162 mItems[index.row()].stylePath = value.toString();
163 emit dataChanged( index, index, { role, Qt::DisplayRole } );
169Qt::ItemFlags QgsProcessingDefaultStylesModel::flags(
const QModelIndex &index )
const
171 if ( !index.isValid() )
172 return Qt::NoItemFlags;
174 Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
175 if ( index.column() == Column::StylePath )
177 flags |= Qt::ItemIsEditable;
182QVariant QgsProcessingDefaultStylesModel::headerData(
int section, Qt::Orientation orientation,
int role )
const
184 switch ( orientation )
190 case Qt::DisplayRole:
194 case Column::OutputName:
195 return tr(
"Output" );
196 case Column::StylePath:
197 return tr(
"Style File" );
214 return QAbstractTableModel::headerData( section, orientation, role );
217void QgsProcessingDefaultStylesModel::saveStyles()
220 for (
const OutputItem &item : std::as_const( mItems ) )
238 setObjectName(
"QgsProcessingDefaultStyleDialog" );
241 setWindowTitle(
algorithm->displayName() );
243 mModel =
new QgsProcessingDefaultStylesModel( mAlgorithm,
this );
244 mDelegate =
new QgsProcessingDefaultStyleDelegate(
this );
245 mTableStyles->setModel( mModel );
246 mTableStyles->setEditTriggers( QAbstractItemView::AllEditTriggers );
247 mTableStyles->horizontalHeader()->setSectionResizeMode( QHeaderView::Interactive );
248 mTableStyles->setSelectionBehavior( QAbstractItemView::SelectRows );
250 mTableStyles->setItemDelegateForColumn( 1, mDelegate );
252 connect( mButtonBox, &QDialogButtonBox::accepted,
this, &QDialog::accept );
253 connect( mButtonBox, &QDialogButtonBox::rejected,
this, &QDialog::reject );
258 mModel->saveStyles();
264 QDialog::showEvent( event );
266 if ( !mInitialWidthsSet )
268 mInitialWidthsSet =
true;
270 int halfWidth = mTableStyles->viewport()->width() / 2;
271 mTableStyles->setColumnWidth( 0, halfWidth );
272 mTableStyles->setColumnWidth( 1, halfWidth );
static QgsProcessingRegistry * processingRegistry()
Returns the application's processing registry, used for managing processing providers,...
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Abstract base class for processing algorithms.
void showEvent(QShowEvent *event) override
QgsProcessingDefaultStyleDialog(const QgsProcessingAlgorithm *algorithm, QWidget *parent=nullptr)
Constructor for QgsProcessingDefaultStyleDialog.
A registry for default styles to apply to layers generated by Processing.
QString defaultStyleForOutput(const QString &algorithmId, const QString &outputName) const
Returns the QML path to the default style to use for the specified output from an algorithm,...
void saveStyles()
Saves registered styles to the configuration file.
void setDefaultStyleForOutput(const QString &algorithmId, const QString &outputName, const QString &qmlPath)
Sets the default style for an output from an algorithm.
Base class for the definition of processing outputs.
A pointcloud layer output for processing algorithms.
A raster layer output for processing algorithms.
A vector layer output for processing algorithms.
A vector tile layer output for processing algorithms.
QgsProcessingDefaultStyleRegistry * defaultStyleRegistry() const
Returns the default style registry, responsible for styling layers generated by Processing tools.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into allowing algorithms to be written in pure substantial changes are required in order to port existing x Processing algorithms for QGIS x The most significant changes are outlined not GeoAlgorithm For algorithms which operate on features one by consider subclassing the QgsProcessingFeatureBasedAlgorithm class This class allows much of the boilerplate code for looping over features from a vector layer to be bypassed and instead requires implementation of a processFeature method Ensure that your algorithm(or algorithm 's parent class) implements the new pure virtual createInstance(self) call
QList< const QgsProcessingOutputDefinition * > QgsProcessingOutputDefinitions
List of processing parameters.