Skip to content

Commit

Permalink
[processing] Move base class of ParametersPanel to c++
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Mar 24, 2020
1 parent 41fb4df commit 3d6e496
Show file tree
Hide file tree
Showing 8 changed files with 349 additions and 55 deletions.
@@ -0,0 +1,59 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/processing/qgsprocessingparameterswidget.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/





class QgsProcessingParametersWidget : QWidget
{
%Docstring
A widget which allows users to select the value for the parameters for an algorithm.

.. note::

Not stable API

.. versionadded:: 3.14
%End

%TypeHeaderCode
#include "qgsprocessingparameterswidget.h"
%End
public:

QgsProcessingParametersWidget( const QgsProcessingAlgorithm *algorithm, QWidget *parent /TransferThis/ = 0 );
%Docstring
Constructor for QgsProcessingParametersWidget, for the specified ``algorithm``.
%End

const QgsProcessingAlgorithm *algorithm() const;

protected:

virtual void initWidgets();

void addParameterWidget( const QgsProcessingParameterDefinition *parameter, QWidget *widget /Transfer/ );
void addParameterLabel( const QgsProcessingParameterDefinition *parameter, QWidget *label /Transfer/ );

void addOutputLabel( QWidget *label /Transfer/ );
void addOutputWidget( QWidget *widget /Transfer/ );

QLayout *mainLayout();


};


/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/processing/qgsprocessingparameterswidget.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
1 change: 1 addition & 0 deletions python/gui/gui_auto.sip
Expand Up @@ -311,6 +311,7 @@
%Include auto_generated/processing/qgsprocessingmultipleselectiondialog.sip
%Include auto_generated/processing/qgsprocessingoutputdestinationwidget.sip
%Include auto_generated/processing/qgsprocessingparameterdefinitionwidget.sip
%Include auto_generated/processing/qgsprocessingparameterswidget.sip
%Include auto_generated/processing/qgsprocessingrecentalgorithmlog.sip
%Include auto_generated/processing/qgsprocessingtoolboxmodel.sip
%Include auto_generated/processing/qgsprocessingtoolboxtreeview.sip
Expand Down
12 changes: 6 additions & 6 deletions python/plugins/processing/algs/gdal/GdalAlgorithmDialog.py
Expand Up @@ -62,7 +62,7 @@ def getParametersPanel(self, alg, parent):
class GdalParametersPanel(ParametersPanel):

def __init__(self, parent, alg):
ParametersPanel.__init__(self, parent, alg)
super().__init__(parent, alg)

w = QWidget()
layout = QVBoxLayout()
Expand All @@ -75,7 +75,7 @@ def __init__(self, parent, alg):
self.text.setReadOnly(True)
layout.addWidget(self.text)
w.setLayout(layout)
self.layoutMain.addWidget(w)
self.mainLayout().addWidget(w)

self.connectParameterSignals()
self.parametersHaveChanged()
Expand Down Expand Up @@ -122,11 +122,11 @@ def parametersHaveChanged(self):
context = createContext()
feedback = QgsProcessingFeedback()
try:
parameters = self.parent.getParameterValues()
for output in self.alg.destinationParameterDefinitions():
parameters = self.parent().getParameterValues()
for output in self.algorithm().destinationParameterDefinitions():
if not output.name() in parameters or parameters[output.name()] is None:
parameters[output.name()] = self.tr("[temporary file]")
for p in self.alg.parameterDefinitions():
for p in self.algorithm().parameterDefinitions():
if p.flags() & QgsProcessingParameterDefinition.FlagHidden:
continue

Expand All @@ -136,7 +136,7 @@ def parametersHaveChanged(self):
self.text.setPlainText('')
return

commands = self.alg.getConsoleCommands(parameters, context, feedback, executing=False)
commands = self.algorithm().getConsoleCommands(parameters, context, feedback, executing=False)
commands = [c for c in commands if c not in ['cmd.exe', '/C ']]
self.text.setPlainText(" ".join(commands))
except AlgorithmDialogBase.InvalidParameterValue as e:
Expand Down
68 changes: 19 additions & 49 deletions python/plugins/processing/gui/ParametersPanel.py
Expand Up @@ -26,7 +26,6 @@
__copyright__ = '(C) 2012, Victor Olaya'

import os
import warnings

from functools import partial

Expand All @@ -41,10 +40,10 @@
QgsVectorFileWriter)
from qgis.gui import (QgsProcessingContextGenerator,
QgsProcessingParameterWidgetContext,
QgsProcessingLayerOutputDestinationWidget)
QgsProcessingLayerOutputDestinationWidget,
QgsProcessingParametersWidget)
from qgis.utils import iface

from qgis.PyQt import uic
from qgis.PyQt.QtCore import QCoreApplication
from qgis.PyQt.QtWidgets import (
QLabel,
Expand All @@ -55,34 +54,16 @@
from processing.gui.wrappers import WidgetWrapperFactory, WidgetWrapper
from processing.tools.dataobjects import createContext

pluginPath = os.path.split(os.path.dirname(__file__))[0]

with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
WIDGET, BASE = uic.loadUiType(
os.path.join(pluginPath, 'ui', 'widgetParametersPanel.ui'))


class ParametersPanel(BASE, WIDGET):
NOT_SELECTED = QCoreApplication.translate('ParametersPanel', '[Not selected]')
class ParametersPanel(QgsProcessingParametersWidget):

def __init__(self, parent, alg, in_place=False):
super(ParametersPanel, self).__init__(None)
self.setupUi(self)
super().__init__(alg, parent)
self.in_place = in_place

self.grpAdvanced.hide()

self.scrollAreaWidgetContents.setContentsMargins(4, 4, 4, 4)
self.layoutMain = self.scrollAreaWidgetContents.layout()
self.layoutAdvanced = self.grpAdvanced.layout()

self.parent = parent
self.alg = alg
self.wrappers = {}
self.outputWidgets = {}
self.checkBoxes = {}
self.dependentItems = {}

self.processing_context = createContext()

Expand Down Expand Up @@ -110,30 +91,26 @@ def layerRegistryChanged(self, layers):
pass

def initWidgets(self):
# If there are advanced parameters — show corresponding groupbox
for param in self.alg.parameterDefinitions():
if param.flags() & QgsProcessingParameterDefinition.FlagAdvanced:
self.grpAdvanced.show()
break
super().initWidgets()

widget_context = QgsProcessingParameterWidgetContext()
widget_context.setProject(QgsProject.instance())
if iface is not None:
widget_context.setMapCanvas(iface.mapCanvas())
widget_context.setBrowserModel(iface.browserModel())
widget_context.setMessageBar(self.parent.messageBar())
if isinstance(self.alg, QgsProcessingModelAlgorithm):
widget_context.setModel(self.alg)
widget_context.setMessageBar(self.parent().messageBar())
if isinstance(self.algorithm(), QgsProcessingModelAlgorithm):
widget_context.setModel(self.algorithm())

# Create widgets and put them in layouts
for param in self.alg.parameterDefinitions():
for param in self.algorithm().parameterDefinitions():
if param.flags() & QgsProcessingParameterDefinition.FlagHidden:
continue

if param.isDestination():
continue
else:
wrapper = WidgetWrapperFactory.create_wrapper(param, self.parent)
wrapper = WidgetWrapperFactory.create_wrapper(param, self.parent())
wrapper.setWidgetContext(widget_context)
wrapper.registerProcessingContextGenerator(self.context_generator)
self.wrappers[param.name()] = wrapper
Expand Down Expand Up @@ -166,25 +143,18 @@ def initWidgets(self):
label = wrapper.label

if label is not None:
if param.flags() & QgsProcessingParameterDefinition.FlagAdvanced:
self.layoutAdvanced.addWidget(label)
else:
self.layoutMain.insertWidget(
self.layoutMain.count() - 2, label)
self.addParameterLabel(param, label)
elif is_python_wrapper:
desc = param.description()
if isinstance(param, QgsProcessingParameterExtent):
desc += self.tr(' (xmin, xmax, ymin, ymax)')
if param.flags() & QgsProcessingParameterDefinition.FlagOptional:
desc += self.tr(' [optional]')
widget.setText(desc)
if param.flags() & QgsProcessingParameterDefinition.FlagAdvanced:
self.layoutAdvanced.addWidget(widget)
else:
self.layoutMain.insertWidget(
self.layoutMain.count() - 2, widget)

for output in self.alg.destinationParameterDefinitions():
self.addParameterWidget(param, widget)

for output in self.algorithm().destinationParameterDefinitions():
if output.flags() & QgsProcessingParameterDefinition.FlagHidden:
continue

Expand All @@ -195,8 +165,8 @@ def initWidgets(self):
widget = QgsProcessingLayerOutputDestinationWidget(output, False)
widget.setWidgetContext(widget_context)

self.layoutMain.insertWidget(self.layoutMain.count() - 1, label)
self.layoutMain.insertWidget(self.layoutMain.count() - 1, widget)
self.addOutputLabel(label)
self.addOutputWidget(widget)
if isinstance(output, (QgsProcessingParameterRasterDestination, QgsProcessingParameterFeatureSink,
QgsProcessingParameterVectorDestination)):
check = QCheckBox()
Expand All @@ -209,7 +179,7 @@ def skipOutputChanged(widget, checkbox, skipped):
# Do not try to open formats that are write-only.
value = widget.value()
if value and isinstance(value, QgsProcessingOutputLayerDefinition) and isinstance(output, (
QgsProcessingParameterFeatureSink, QgsProcessingParameterVectorDestination)):
QgsProcessingParameterFeatureSink, QgsProcessingParameterVectorDestination)):
filename = value.sink.staticValue()
if filename not in ('memory:', ''):
path, ext = os.path.splitext(filename)
Expand All @@ -225,7 +195,7 @@ def skipOutputChanged(widget, checkbox, skipped):
check.setChecked(not widget.outputIsSkipped())
check.setEnabled(not widget.outputIsSkipped())
widget.skipOutputChanged.connect(partial(skipOutputChanged, widget, check))
self.layoutMain.insertWidget(self.layoutMain.count() - 1, check)
self.addOutputWidget(check)
self.checkBoxes[output.name()] = check

self.outputWidgets[output.name()] = widget
Expand All @@ -234,7 +204,7 @@ def skipOutputChanged(widget, checkbox, skipped):
wrapper.postInitialize(list(self.wrappers.values()))

def setParameters(self, parameters):
for param in self.alg.parameterDefinitions():
for param in self.algorithm().parameterDefinitions():
if param.flags() & QgsProcessingParameterDefinition.FlagHidden:
continue

Expand Down
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -270,6 +270,7 @@ SET(QGIS_GUI_SRCS
processing/qgsprocessingmultipleselectiondialog.cpp
processing/qgsprocessingoutputdestinationwidget.cpp
processing/qgsprocessingparameterdefinitionwidget.cpp
processing/qgsprocessingparameterswidget.cpp
processing/qgsprocessingrecentalgorithmlog.cpp
processing/qgsprocessingtoolboxmodel.cpp
processing/qgsprocessingtoolboxtreeview.cpp
Expand Down Expand Up @@ -950,6 +951,7 @@ SET(QGIS_GUI_HDRS
processing/qgsprocessingmultipleselectiondialog.h
processing/qgsprocessingoutputdestinationwidget.h
processing/qgsprocessingparameterdefinitionwidget.h
processing/qgsprocessingparameterswidget.h
processing/qgsprocessingrecentalgorithmlog.h
processing/qgsprocessingtoolboxmodel.h
processing/qgsprocessingtoolboxtreeview.h
Expand Down
84 changes: 84 additions & 0 deletions src/gui/processing/qgsprocessingparameterswidget.cpp
@@ -0,0 +1,84 @@
/***************************************************************************
qgsprocessingparameterswidget.cpp
------------------------------------
Date : March 2020
Copyright : (C) 2020 Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsprocessingparameterswidget.h"
#include "qgsprocessingalgorithm.h"
#include "qgsprocessingparameters.h"

///@cond NOT_STABLE

QgsProcessingParametersWidget::QgsProcessingParametersWidget( const QgsProcessingAlgorithm *algorithm, QWidget *parent )
: QWidget( parent )
, mAlgorithm( algorithm )
{
Q_ASSERT( mAlgorithm );

setupUi( this );

grpAdvanced->hide();
scrollAreaWidgetContents->setContentsMargins( 4, 4, 4, 4 );
}

const QgsProcessingAlgorithm *QgsProcessingParametersWidget::algorithm() const
{
return mAlgorithm;
}

void QgsProcessingParametersWidget::initWidgets()
{
// if there are advanced parameters — show corresponding groupbox
const QgsProcessingParameterDefinitions defs = mAlgorithm->parameterDefinitions();
for ( const QgsProcessingParameterDefinition *param : defs )
{
if ( param->flags() & QgsProcessingParameterDefinition::FlagAdvanced )
{
grpAdvanced->show();
break;
}
}
}

void QgsProcessingParametersWidget::addParameterWidget( const QgsProcessingParameterDefinition *parameter, QWidget *widget )
{
if ( parameter->flags() & QgsProcessingParameterDefinition::FlagAdvanced )
mAdvancedGroupLayout->addWidget( widget );
else
mScrollAreaLayout->insertWidget( mScrollAreaLayout->count() - 2, widget );
}

void QgsProcessingParametersWidget::addParameterLabel( const QgsProcessingParameterDefinition *parameter, QWidget *label )
{
if ( parameter->flags() & QgsProcessingParameterDefinition::FlagAdvanced )
mAdvancedGroupLayout->addWidget( label );
else
mScrollAreaLayout->insertWidget( mScrollAreaLayout->count() - 2, label );
}

void QgsProcessingParametersWidget::addOutputLabel( QWidget *label )
{
mScrollAreaLayout->insertWidget( mScrollAreaLayout->count() - 1, label );
}

void QgsProcessingParametersWidget::addOutputWidget( QWidget *widget )
{
mScrollAreaLayout->insertWidget( mScrollAreaLayout->count() - 1, widget );
}

QLayout *QgsProcessingParametersWidget::mainLayout()
{
return mScrollAreaLayout;
}

///@endcond

0 comments on commit 3d6e496

Please sign in to comment.