Skip to content

Commit

Permalink
Move designer edit buttons to c++
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Mar 2, 2020
1 parent 8f6a611 commit 0dbb082
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 32 deletions.
2 changes: 2 additions & 0 deletions images/images.qrc
Expand Up @@ -817,6 +817,8 @@
<file>themes/default/mActionAddManualTable.svg</file>
<file>themes/default/mIconModelerCollapse.svg</file>
<file>themes/default/mIconModelerExpand.svg</file>
<file>themes/default/mActionDeleteModelComponent.svg</file>
<file>themes/default/mActionEditModelComponent.svg</file>
</qresource>
<qresource prefix="/images/tips">
<file alias="symbol_levels.png">qgis_tips/symbol_levels.png</file>
Expand Down
1 change: 1 addition & 0 deletions images/themes/default/mActionDeleteModelComponent.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions images/themes/default/mActionEditModelComponent.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Expand Up @@ -10,7 +10,6 @@




class QgsModelComponentGraphicItem : QGraphicsObject
{
%Docstring
Expand Down Expand Up @@ -63,6 +62,22 @@ Emitted by the item to request a repaint of the parent model scene.
%Docstring
Emitted when the definition of the associated component is changed
by the item.
%End

protected slots:

virtual void editComponent();
%Docstring
Called when the component should be edited.

The default implementation does nothing.
%End

virtual void deleteComponent();
%Docstring
Called when the component should be deleted.

The default implementation does nothing.
%End

};
Expand Down
35 changes: 5 additions & 30 deletions python/plugins/processing/modeler/ModelerGraphicItem.py
Expand Up @@ -35,7 +35,6 @@
from qgis.gui import (
QgsProcessingParameterDefinitionDialog,
QgsProcessingParameterWidgetContext,
QgsModelDesignerFlatButtonGraphicItem,
QgsModelDesignerFoldButtonGraphicItem,
QgsModelComponentGraphicItem
)
Expand All @@ -50,7 +49,6 @@
class ModelerGraphicItem(QgsModelComponentGraphicItem):

BUTTON_WIDTH = 16
BUTTON_HEIGHT = 16

repaintArrows = pyqtSignal()
updateArrowPaths = pyqtSignal()
Expand All @@ -63,29 +61,6 @@ def __init__(self, element, model):
self.picture = None
self.hover_over_item = False

svg = QSvgRenderer(os.path.join(pluginPath, 'images', 'edit.svg'))
picture = QPicture()
painter = QPainter(picture)
svg.render(painter)
painter.end()
pt = QPointF(self.component().size().width() / 2 -
ModelerGraphicItem.BUTTON_WIDTH / 2,
self.component().size().height() / 2 -
ModelerGraphicItem.BUTTON_HEIGHT / 2)
self.editButton = QgsModelDesignerFlatButtonGraphicItem(self, picture, pt)
self.editButton.clicked.connect(self.editComponent)
svg = QSvgRenderer(os.path.join(pluginPath, 'images', 'delete.svg'))
picture = QPicture()
painter = QPainter(picture)
svg.render(painter)
painter.end()
pt = QPointF(self.component().size().width() / 2 -
ModelerGraphicItem.BUTTON_WIDTH / 2,
ModelerGraphicItem.BUTTON_HEIGHT / 2 -
self.component().size().height() / 2)
self.deleteButton = QgsModelDesignerFlatButtonGraphicItem(self, picture, pt)
self.deleteButton.clicked.connect(self.removeComponent)

def boundingRect(self):
fm = QFontMetricsF(self.item_font)
unfolded = isinstance(self.component(),
Expand Down Expand Up @@ -326,7 +301,7 @@ def editComponent(self):
self.text = new_param.description()
self.requestModelRepaint.emit()

def removeComponent(self):
def deleteComponent(self):
if self.model().childAlgorithmsDependOnParameter(self.component().parameterName()):
QMessageBox.warning(None, 'Could not remove input',
'Algorithms depend on the selected input.\n'
Expand All @@ -343,7 +318,7 @@ def removeComponent(self):
def contextMenuEvent(self, event):
popupmenu = QMenu()
removeAction = popupmenu.addAction('Remove')
removeAction.triggered.connect(self.removeComponent)
removeAction.triggered.connect(self.deleteComponent)
editAction = popupmenu.addAction('Edit')
editAction.triggered.connect(self.editComponent)
popupmenu.exec_(event.screenPos())
Expand Down Expand Up @@ -422,7 +397,7 @@ def updateAlgorithm(self, alg):
(i + 1.5) * self.component().size().height()))
self.model().setChildAlgorithm(alg)

def removeComponent(self):
def deleteComponent(self):
if not self.model().removeChildAlgorithm(self.component().childId()):
QMessageBox.warning(None, 'Could not remove element',
'Other elements depend on the selected one.\n'
Expand All @@ -434,7 +409,7 @@ def removeComponent(self):
def contextMenuEvent(self, event):
popupmenu = QMenu()
removeAction = popupmenu.addAction('Remove')
removeAction.triggered.connect(self.removeComponent)
removeAction.triggered.connect(self.deleteComponent)
editAction = popupmenu.addAction('Edit')
editAction.triggered.connect(self.editComponent)

Expand Down Expand Up @@ -485,7 +460,7 @@ def editComponent(self):
model_output.setMandatory(not (dlg.param.flags() & QgsProcessingParameterDefinition.FlagOptional))
self.model().updateDestinationParameters()

def removeComponent(self):
def deleteComponent(self):
self.model().childAlgorithm(self.component().childId()).removeModelOutput(self.component().name())
self.model().updateDestinationParameters()
self.changed.emit()
Expand Down
25 changes: 25 additions & 0 deletions src/gui/processing/models/qgsmodelcomponentgraphicitem.cpp
Expand Up @@ -19,6 +19,11 @@
#include "qgsprocessingmodelchildalgorithm.h"
#include "qgsprocessingmodeloutput.h"
#include "qgsmodelgraphicsscene.h"
#include "qgsapplication.h"
#include "qgsmodelgraphicitem.h"
#include <QSvgRenderer>
#include <QPicture>
#include <QPainter>

///@cond NOT_STABLE

Expand All @@ -32,6 +37,26 @@ QgsModelComponentGraphicItem::QgsModelComponentGraphicItem( QgsProcessingModelCo
setFlag( QGraphicsItem::ItemIsSelectable, true );
setFlag( QGraphicsItem::ItemSendsGeometryChanges, true );
setZValue( QgsModelGraphicsScene::ZValues::ModelComponent );

QSvgRenderer svg( QgsApplication::iconPath( QStringLiteral( "mActionEditModelComponent.svg" ) ) );
QPicture editPicture;
QPainter painter( &editPicture );
svg.render( &painter );
painter.end();
mEditButton = new QgsModelDesignerFlatButtonGraphicItem( this, editPicture,
QPointF( component->size().width() / 2.0 - mButtonSize.width() / 2.0,
component->size().height() / 2.0 - mButtonSize.height() / 2.0 ) );
connect( mEditButton, &QgsModelDesignerFlatButtonGraphicItem::clicked, this, &QgsModelComponentGraphicItem::editComponent );

QSvgRenderer svg2( QgsApplication::iconPath( QStringLiteral( "mActionDeleteModelComponent.svg" ) ) );
QPicture deletePicture;
painter.begin( &deletePicture );
svg2.render( &painter );
painter.end();
mDeleteButton = new QgsModelDesignerFlatButtonGraphicItem( this, deletePicture,
QPointF( component->size().width() / 2.0 - mButtonSize.width() / 2.0,
mButtonSize.height() / 2.0 - component->size().height() / 2.0 ) );
connect( mDeleteButton, &QgsModelDesignerFlatButtonGraphicItem::clicked, this, &QgsModelComponentGraphicItem::deleteComponent );
}

QgsProcessingModelComponent *QgsModelComponentGraphicItem::component()
Expand Down
24 changes: 23 additions & 1 deletion src/gui/processing/models/qgsmodelcomponentgraphicitem.h
Expand Up @@ -25,7 +25,7 @@ class QgsProcessingModelParameter;
class QgsProcessingModelChildAlgorithm;
class QgsProcessingModelOutput;
class QgsProcessingModelAlgorithm;

class QgsModelDesignerFlatButtonGraphicItem;

///@cond NOT_STABLE

Expand Down Expand Up @@ -80,11 +80,33 @@ class GUI_EXPORT QgsModelComponentGraphicItem : public QGraphicsObject
*/
void changed();

protected slots:

/**
* Called when the component should be edited.
*
* The default implementation does nothing.
*/
virtual void editComponent() {}

/**
* Called when the component should be deleted.
*
* The default implementation does nothing.
*/
virtual void deleteComponent() {}

private:

std::unique_ptr< QgsProcessingModelComponent > mComponent;
QgsProcessingModelAlgorithm *mModel = nullptr;

QgsModelDesignerFlatButtonGraphicItem *mEditButton = nullptr;
QgsModelDesignerFlatButtonGraphicItem *mDeleteButton = nullptr;

static constexpr double DEFAULT_BUTTON_WIDTH = 16;
static constexpr double DEFAULT_BUTTON_HEIGHT = 16;
QSizeF mButtonSize { DEFAULT_BUTTON_WIDTH, DEFAULT_BUTTON_HEIGHT };

};

Expand Down

0 comments on commit 0dbb082

Please sign in to comment.