Skip to content
Permalink
Browse files

Rework hiding of model controls to use a flag on the scene

  • Loading branch information
nyalldawson committed Mar 2, 2020
1 parent b567dde commit 1d25155d15d10ac7777cc8810846c9d2179f18fd
@@ -33,15 +33,50 @@ QGraphicsScene subclass representing the model designer.
ModelComponent,
};

enum Flag
{
FlagHideControls,
};
typedef QFlags<QgsModelGraphicsScene::Flag> Flags;


QgsModelGraphicsScene( QObject *parent /TransferThis/ = 0 );
%Docstring
Constructor for QgsModelGraphicsScene with the specified ``parent`` object.
%End

void setFlags( QgsModelGraphicsScene::Flags flags );
%Docstring
Sets the combination of ``flags`` controlling how the scene is rendered and behaves.

.. seealso:: :py:func:`setFlag`

.. seealso:: :py:func:`flags`
%End

void setFlag( QgsModelGraphicsScene::Flag flag, bool on = true );
%Docstring
Enables or disables a particular ``flag`` for the scene. Other existing
flags are not affected.

.. seealso:: :py:func:`setFlags`

.. seealso:: :py:func:`flags`
%End

QgsModelGraphicsScene::Flags flags() const;
%Docstring
Returns the current combination of flags set for the scene.

.. seealso:: :py:func:`setFlags`

.. seealso:: :py:func:`setFlag`
%End

};



/************************************************************************
* This file has been generated automatically from *
* *
@@ -88,7 +88,8 @@
QgsProcessingToolboxProxyModel,
QgsProcessingParameterDefinitionDialog,
QgsVariableEditorWidget,
QgsProcessingParameterWidgetContext)
QgsProcessingParameterWidgetContext,
QgsModelGraphicsScene)
from processing.gui.HelpEditionDialog import HelpEditionDialog
from processing.gui.AlgorithmDialog import AlgorithmDialog
from processing.modeler.ModelerParameterDefinitionDialog import ModelerParameterDefinitionDialog
@@ -783,7 +784,10 @@ def repaintModel(self, controls=True):
self.scene = ModelerScene(self, dialog=self)
self.scene.setSceneRect(QRectF(0, 0, self.CANVAS_SIZE,
self.CANVAS_SIZE))
self.scene.paintModel(self.model, controls)

if not controls:
self.scene.setFlag(QgsModelGraphicsScene.FlagHideControls)
self.scene.paintModel(self.model)
self.view.setScene(self.scene)

def addInput(self):
@@ -56,9 +56,8 @@ class ModelerGraphicItem(QgsModelComponentGraphicItem):
BUTTON_WIDTH = 16
BUTTON_HEIGHT = 16

def __init__(self, element, model, controls, scene=None):
def __init__(self, element, model, scene=None):
super().__init__(element, model, None)
self.controls = controls
self.scene = scene
self.box_width = ModelerGraphicItem.BOX_WIDTH
self.box_height = ModelerGraphicItem.BOX_HEIGHT
@@ -101,44 +100,41 @@ def __init__(self, element, model, controls, scene=None):
self.text = element.description()
self.arrows = []

if controls:
svg = QSvgRenderer(os.path.join(pluginPath, 'images', 'edit.svg'))
picture = QPicture()
painter = QPainter(picture)
svg.render(painter)
painter.end()
pt = QPointF(self.box_width / 2 -
ModelerGraphicItem.BUTTON_WIDTH / 2,
self.box_height / 2 -
ModelerGraphicItem.BUTTON_HEIGHT / 2)
self.editButton = QgsModelDesignerFlatButtonGraphicItem(self, picture, pt)
self.editButton.clicked.connect(self.editElement)
svg = QSvgRenderer(os.path.join(pluginPath, 'images', 'delete.svg'))
picture = QPicture()
painter = QPainter(picture)
svg.render(painter)
painter.end()
pt = QPointF(self.box_width / 2 -
ModelerGraphicItem.BUTTON_WIDTH / 2,
ModelerGraphicItem.BUTTON_HEIGHT / 2 -
self.box_height / 2)
self.deleteButton = QgsModelDesignerFlatButtonGraphicItem(self, picture, pt)
self.deleteButton.clicked.connect(self.removeElement)
svg = QSvgRenderer(os.path.join(pluginPath, 'images', 'edit.svg'))
picture = QPicture()
painter = QPainter(picture)
svg.render(painter)
painter.end()
pt = QPointF(self.box_width / 2 -
ModelerGraphicItem.BUTTON_WIDTH / 2,
self.box_height / 2 -
ModelerGraphicItem.BUTTON_HEIGHT / 2)
self.editButton = QgsModelDesignerFlatButtonGraphicItem(self, picture, pt)
self.editButton.clicked.connect(self.editElement)
svg = QSvgRenderer(os.path.join(pluginPath, 'images', 'delete.svg'))
picture = QPicture()
painter = QPainter(picture)
svg.render(painter)
painter.end()
pt = QPointF(self.box_width / 2 -
ModelerGraphicItem.BUTTON_WIDTH / 2,
ModelerGraphicItem.BUTTON_HEIGHT / 2 -
self.box_height / 2)
self.deleteButton = QgsModelDesignerFlatButtonGraphicItem(self, picture, pt)
self.deleteButton.clicked.connect(self.removeElement)

if isinstance(element, QgsProcessingModelChildAlgorithm):
alg = element.algorithm()
if [a for a in alg.parameterDefinitions() if not a.isDestination()]:
pt = self.getLinkPointForParameter(-1)
pt = QPointF(0, pt.y())
if controls:
self.inButton = QgsModelDesignerFoldButtonGraphicItem(self, self.component().parametersCollapsed(), pt)
self.inButton.folded.connect(self.foldInput)
self.inButton = QgsModelDesignerFoldButtonGraphicItem(self, self.component().parametersCollapsed(), pt)
self.inButton.folded.connect(self.foldInput)
if alg.outputDefinitions():
pt = self.getLinkPointForOutput(-1)
pt = QPointF(0, pt.y())
if controls:
self.outButton = QgsModelDesignerFoldButtonGraphicItem(self, self.component().outputsCollapsed(), pt)
self.outButton.folded.connect(self.foldOutput)
self.outButton = QgsModelDesignerFoldButtonGraphicItem(self, self.component().outputsCollapsed(), pt)
self.outButton.folded.connect(self.foldOutput)

def foldInput(self, folded):
self.component().setParametersCollapsed(folded)
@@ -88,12 +88,12 @@ def getItemsFromParamValue(self, value, child_id, context):
items.extend(self.getItemsFromParamValue(variables[v].source, child_id, context))
return items

def paintModel(self, model, controls=True):
def paintModel(self, model):
self.model = model
context = createContext()
# Inputs
for inp in list(model.parameterComponents().values()):
item = ModelerGraphicItem(inp.clone(), model, controls, scene=self)
item = ModelerGraphicItem(inp.clone(), model, scene=self)
item.setFlag(QGraphicsItem.ItemIsMovable, True)
item.setFlag(QGraphicsItem.ItemIsSelectable, True)
self.addItem(item)
@@ -128,7 +128,7 @@ def paintModel(self, model, controls=True):

# We add the algs
for alg in list(model.childAlgorithms().values()):
item = ModelerGraphicItem(alg.clone(), model, controls, scene=self)
item = ModelerGraphicItem(alg.clone(), model, scene=self)
item.setFlag(QGraphicsItem.ItemIsMovable, True)
item.setFlag(QGraphicsItem.ItemIsSelectable, True)
self.addItem(item)
@@ -169,7 +169,7 @@ def paintModel(self, model, controls=True):

for key, out in outputs.items():
if out is not None:
item = ModelerGraphicItem(out.clone(), model, controls, scene=self)
item = ModelerGraphicItem(out.clone(), model, scene=self)
item.setFlag(QGraphicsItem.ItemIsMovable, True)
item.setFlag(QGraphicsItem.ItemIsSelectable, True)
self.addItem(item)
@@ -15,6 +15,7 @@

#include "qgsmodelgraphicitem.h"
#include "qgsapplication.h"
#include "qgsmodelgraphicsscene.h"
#include <QPainter>
#include <QSvgRenderer>

@@ -33,6 +34,12 @@ QgsModelDesignerFlatButtonGraphicItem::QgsModelDesignerFlatButtonGraphicItem( QG

void QgsModelDesignerFlatButtonGraphicItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *, QWidget * )
{
if ( QgsModelGraphicsScene *modelScene = qobject_cast< QgsModelGraphicsScene * >( scene() ) )
{
if ( modelScene->flags() & QgsModelGraphicsScene::FlagHideControls )
return;
}

if ( mHoverState )
{
painter->setPen( QPen( Qt::transparent, 1.0 ) );
@@ -23,5 +23,13 @@ QgsModelGraphicsScene::QgsModelGraphicsScene( QObject *parent )

}

void QgsModelGraphicsScene::setFlag( QgsModelGraphicsScene::Flag flag, bool on )
{
if ( on )
mFlags |= flag;
else
mFlags &= ~flag;
}

///@endcond

@@ -42,14 +42,48 @@ class GUI_EXPORT QgsModelGraphicsScene : public QGraphicsScene
ModelComponent = 1, //!< Model components (e.g. algorithms, inputs and outputs)
};

//! Flags for controlling how the scene is rendered and scene behavior
enum Flag
{
FlagHideControls = 1 << 1, //!< If set, item interactive controls will be hidden
};
Q_DECLARE_FLAGS( Flags, Flag )

/**
* Constructor for QgsModelGraphicsScene with the specified \a parent object.
*/
QgsModelGraphicsScene( QObject *parent SIP_TRANSFERTHIS = nullptr );

/**
* Sets the combination of \a flags controlling how the scene is rendered and behaves.
* \see setFlag()
* \see flags()
*/
void setFlags( QgsModelGraphicsScene::Flags flags ) { mFlags = flags; }

/**
* Enables or disables a particular \a flag for the scene. Other existing
* flags are not affected.
* \see setFlags()
* \see flags()
*/
void setFlag( QgsModelGraphicsScene::Flag flag, bool on = true );

/**
* Returns the current combination of flags set for the scene.
* \see setFlags()
* \see setFlag()
*/
QgsModelGraphicsScene::Flags flags() const { return mFlags; }

private:

Flags mFlags = nullptr;

};

Q_DECLARE_METATYPE( QgsModelGraphicsScene::Flags )

///@endcond

#endif // QGSMODELCOMPONENTGRAPHICITEM_H

0 comments on commit 1d25155

Please sign in to comment.
You can’t perform that action at this time.