Skip to content

Commit

Permalink
Bit less Python
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Mar 6, 2020
1 parent 8845b53 commit b84bf68
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 25 deletions.
Expand Up @@ -10,6 +10,8 @@





class QgsModelDesignerDialog : QMainWindow
{
%Docstring
Expand Down Expand Up @@ -47,7 +49,6 @@ Model designer dialog base class
QLineEdit *textName();
QLineEdit *textGroup();
QTreeWidget *inputsTree();
QgsProcessingToolboxTreeView *algorithmsTree();

QgsMessageBar *messageBar();
QGraphicsView *view();
Expand Down
Expand Up @@ -405,6 +405,7 @@ level group containing recently used algorithms.
Returns the underlying source Processing toolbox model.
%End


void setFilters( QgsProcessingToolboxProxyModel::Filters filters );
%Docstring
Set ``filters`` that affect how toolbox content is filtered.
Expand Down
26 changes: 3 additions & 23 deletions python/plugins/processing/modeler/ModelerDialog.py
Expand Up @@ -49,8 +49,7 @@
QgsProcessingModelParameter,
QgsProcessingParameterType,
)
from qgis.gui import (QgsProcessingToolboxProxyModel,
QgsProcessingParameterDefinitionDialog,
from qgis.gui import (QgsProcessingParameterDefinitionDialog,
QgsProcessingParameterWidgetContext,
QgsModelGraphicsScene,
QgsModelDesignerDialog)
Expand All @@ -68,22 +67,6 @@
pluginPath = os.path.split(os.path.dirname(__file__))[0]


class ModelerToolboxModel(QgsProcessingToolboxProxyModel):

def __init__(self, parent=None, registry=None, recentLog=None):
super().__init__(parent, registry, recentLog)

def flags(self, index):
f = super().flags(index)
source_index = self.mapToSource(index)
if self.toolboxModel().isAlgorithm(source_index):
f = f | Qt.ItemIsDragEnabled
return f

def supportedDragActions(self):
return Qt.CopyAction


class ModelerDialog(QgsModelDesignerDialog):
ALG_ITEM = 'ALG_ITEM'
PROVIDER_ITEM = 'PROVIDER_ITEM'
Expand Down Expand Up @@ -137,9 +120,6 @@ def __init__(self, model=None, parent=None):
self.view().ensureVisible(0, 0, 10, 10)
self.view().scale(QgsApplication.desktop().logicalDpiX() / 96, QgsApplication.desktop().logicalDpiX() / 96)

self.algorithms_model = ModelerToolboxModel(self, QgsApplication.processingRegistry())
self.algorithmsTree().setToolboxProxyModel(self.algorithms_model)

# Connect signals and slots
self.inputsTree().doubleClicked.connect(self._addInput)

Expand Down Expand Up @@ -473,8 +453,8 @@ def getPositionForAlgorithmItem(self):
maxX = max([alg.position().x() for alg in list(self.model().childAlgorithms().values())])
maxY = max([alg.position().y() for alg in list(self.model().childAlgorithms().values())])
newX = min(MARGIN + BOX_WIDTH + maxX, self.CANVAS_SIZE - BOX_WIDTH)
newY = min(MARGIN + BOX_HEIGHT + maxY, self.CANVAS_SIZE -
BOX_HEIGHT)
newY = min(MARGIN + BOX_HEIGHT + maxY, self.CANVAS_SIZE
- BOX_HEIGHT)
else:
newX = MARGIN + BOX_WIDTH / 2
newY = MARGIN * 2 + BOX_HEIGHT + BOX_HEIGHT / 2
Expand Down
27 changes: 27 additions & 0 deletions src/gui/processing/models/qgsmodeldesignerdialog.cpp
Expand Up @@ -33,6 +33,30 @@
///@cond NOT_STABLE


QgsModelerToolboxModel::QgsModelerToolboxModel( QObject *parent )
: QgsProcessingToolboxProxyModel( parent )
{

}

Qt::ItemFlags QgsModelerToolboxModel::flags( const QModelIndex &index ) const
{
Qt::ItemFlags f = QgsProcessingToolboxProxyModel::flags( index );
const QModelIndex sourceIndex = mapToSource( index );
if ( toolboxModel()->isAlgorithm( sourceIndex ) )
{
f = f | Qt::ItemIsDragEnabled;
}
return f;
}

Qt::DropActions QgsModelerToolboxModel::supportedDragActions() const
{
return Qt::CopyAction;
}



QgsModelDesignerDialog::QgsModelDesignerDialog( QWidget *parent, Qt::WindowFlags flags )
: QMainWindow( parent, flags )
{
Expand Down Expand Up @@ -89,6 +113,9 @@ QgsModelDesignerDialog::QgsModelDesignerDialog( QWidget *parent, Qt::WindowFlags
mAlgorithmsTree->setDragDropMode( QTreeWidget::DragOnly );
mAlgorithmsTree->setDropIndicatorShown( true );

mAlgorithmsModel = new QgsModelerToolboxModel( this );
mAlgorithmsTree->setToolboxProxyModel( mAlgorithmsModel );

connect( mView, &QgsModelGraphicsView::algorithmDropped, this, [ = ]( const QString & algorithmId, const QPointF & pos )
{
addAlgorithm( algorithmId, pos );
Expand Down
17 changes: 16 additions & 1 deletion src/gui/processing/models/qgsmodeldesignerdialog.h
Expand Up @@ -20,11 +20,26 @@
#include "qgis_gui.h"
#include "ui_qgsmodeldesignerdialogbase.h"

#include "qgsprocessingtoolboxmodel.h"

class QgsMessageBar;
class QgsProcessingModelAlgorithm;

///@cond NOT_STABLE

#ifndef SIP_RUN

class GUI_EXPORT QgsModelerToolboxModel : public QgsProcessingToolboxProxyModel
{
public:
explicit QgsModelerToolboxModel( QObject *parent = nullptr );
Qt::ItemFlags flags( const QModelIndex &index ) const override;
Qt::DropActions supportedDragActions() const override;

};

#endif

/**
* \ingroup gui
* \brief Model designer dialog base class
Expand Down Expand Up @@ -55,7 +70,6 @@ class GUI_EXPORT QgsModelDesignerDialog : public QMainWindow, public Ui::QgsMode
QLineEdit *textName() { return mNameEdit; }
QLineEdit *textGroup() { return mGroupEdit; }
QTreeWidget *inputsTree() { return mInputsTreeWidget; }
QgsProcessingToolboxTreeView *algorithmsTree() { return mAlgorithmsTree; }

QgsMessageBar *messageBar() { return mMessageBar; }
QGraphicsView *view() { return mView; }
Expand All @@ -76,6 +90,7 @@ class GUI_EXPORT QgsModelDesignerDialog : public QMainWindow, public Ui::QgsMode
private:

QgsMessageBar *mMessageBar = nullptr;
QgsModelerToolboxModel *mAlgorithmsModel = nullptr;

};

Expand Down
5 changes: 5 additions & 0 deletions src/gui/processing/qgsprocessingtoolboxmodel.cpp
Expand Up @@ -666,6 +666,11 @@ QgsProcessingToolboxModel *QgsProcessingToolboxProxyModel::toolboxModel()
return mModel;
}

const QgsProcessingToolboxModel *QgsProcessingToolboxProxyModel::toolboxModel() const
{
return mModel;
}

void QgsProcessingToolboxProxyModel::setFilters( QgsProcessingToolboxProxyModel::Filters filters )
{
mFilters = filters;
Expand Down
6 changes: 6 additions & 0 deletions src/gui/processing/qgsprocessingtoolboxmodel.h
Expand Up @@ -450,6 +450,12 @@ class GUI_EXPORT QgsProcessingToolboxProxyModel: public QSortFilterProxyModel
*/
QgsProcessingToolboxModel *toolboxModel();

/**
* Returns the underlying source Processing toolbox model.
* \note Not available in Python bindings
*/
const QgsProcessingToolboxModel *toolboxModel() const SIP_SKIP;

/**
* Set \a filters that affect how toolbox content is filtered.
* \see filters()
Expand Down

0 comments on commit b84bf68

Please sign in to comment.