Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FEATURE][processing] Allow creation of group boxes in models
These are a visual guide, allowing designers to create logical groups
of algorithms relating to a single task ("eg 'Prepare data')

The title and color of the group boxes can be customized
  • Loading branch information
nyalldawson committed Apr 1, 2020
1 parent a7a64d4 commit 4fa9083
Show file tree
Hide file tree
Showing 13 changed files with 509 additions and 4 deletions.
@@ -0,0 +1,62 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/processing/models/qgsprocessingmodelgroupbox.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/





class QgsProcessingModelGroupBox : QgsProcessingModelComponent
{
%Docstring
Represents a group box in a model.

.. versionadded:: 3.14
%End

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

QgsProcessingModelGroupBox( const QString &description = QString() );
%Docstring
Constructor for QgsProcessingModelGroupBox with the specified ``description``.
%End

virtual QgsProcessingModelGroupBox *clone() const /Factory/;


QVariant toVariant() const;
%Docstring
Saves this group box to a QVariant.

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

bool loadVariant( const QVariantMap &map );
%Docstring
Loads this group box from a QVariantMap.

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

QString uuid() const;
%Docstring
Returns the unique ID associated with this group box.
%End

};


/************************************************************************
* This file has been generated automatically from *
* *
* src/core/processing/models/qgsprocessingmodelgroupbox.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
Expand Up @@ -276,6 +276,11 @@ Returns the label text color for the item for the specified ``state``.
virtual Qt::PenStyle strokeStyle( State state ) const;
%Docstring
Returns the stroke style to use while rendering the outline of the item.
%End

virtual Qt::Alignment titleAlignment() const;
%Docstring
Returns the title alignment
%End

virtual QPicture iconPicture() const;
Expand Down Expand Up @@ -536,6 +541,64 @@ Ownership of ``output`` is transferred to the item.

};


class QgsModelGroupBoxGraphicItem : QgsModelComponentGraphicItem
{
%Docstring
A graphic item representing a group box in the model designer.

.. warning::

Not stable API

.. versionadded:: 3.14
%End

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

QgsModelGroupBoxGraphicItem( QgsProcessingModelGroupBox *box /Transfer/,
QgsProcessingModelAlgorithm *model,
QGraphicsItem *parent /TransferThis/ );
%Docstring
Constructor for QgsModelGroupBoxGraphicItem for the specified group ``box``, with the specified ``parent`` item.

The ``model`` argument specifies the associated processing model. Ownership of ``model`` is not transferred, and
it must exist for the lifetime of this object.

Ownership of ``output`` is transferred to the item.
%End
~QgsModelGroupBoxGraphicItem();
virtual void contextMenuEvent( QGraphicsSceneContextMenuEvent *event );

virtual bool canDeleteComponent();

protected:

virtual QColor fillColor( State state ) const;

virtual QColor strokeColor( State state ) const;

virtual QColor textColor( State state ) const;

virtual Qt::PenStyle strokeStyle( State state ) const;

virtual Qt::Alignment titleAlignment() const;

virtual void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size );


protected slots:

virtual void deleteComponent();

virtual void editComponent();

};


/************************************************************************
* This file has been generated automatically from *
* *
Expand Down
Expand Up @@ -29,6 +29,7 @@ QGraphicsScene subclass representing the model designer.

enum ZValues
{
GroupBox,
ArrowLink,
ModelComponent,
MouseHandles,
Expand Down Expand Up @@ -173,6 +174,10 @@ Creates a new graphic item for a model output.
Creates a new graphic item for a model comment.
%End

QgsModelComponentGraphicItem *createGroupBoxGraphicItem( QgsProcessingModelAlgorithm *model, QgsProcessingModelGroupBox *box ) const /Factory/;
%Docstring
Creates a new graphic item for a model group box.
%End

};

Expand Down
2 changes: 1 addition & 1 deletion src/core/processing/models/qgsprocessingmodelgroupbox.cpp
Expand Up @@ -23,7 +23,7 @@ QgsProcessingModelGroupBox::QgsProcessingModelGroupBox( const QString &descripti
: QgsProcessingModelComponent( description )
, mUuid( QUuid::createUuid().toString() )
{
setSize( QSizeF( 100, 60 ) );
setSize( QSizeF( 400, 360 ) );
}

QgsProcessingModelGroupBox *QgsProcessingModelGroupBox::clone() const
Expand Down
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -291,6 +291,7 @@ SET(QGIS_GUI_SRCS
processing/models/qgsmodelgraphicitem.cpp
processing/models/qgsmodelgraphicsscene.cpp
processing/models/qgsmodelgraphicsview.cpp
processing/models/qgsmodelgroupboxdefinitionwidget.cpp
processing/models/qgsmodelsnapper.cpp
processing/models/qgsmodelundocommand.cpp
processing/models/qgsmodelviewmouseevent.cpp
Expand Down Expand Up @@ -984,6 +985,7 @@ SET(QGIS_GUI_HDRS
processing/models/qgsmodelgraphicitem.h
processing/models/qgsmodelgraphicsscene.h
processing/models/qgsmodelgraphicsview.h
processing/models/qgsmodelgroupboxdefinitionwidget.h
processing/models/qgsmodelsnapper.h
processing/models/qgsmodelundocommand.h
processing/models/qgsmodelviewmouseevent.h
Expand Down
131 changes: 130 additions & 1 deletion src/gui/processing/models/qgsmodelcomponentgraphicitem.cpp
Expand Up @@ -18,13 +18,15 @@
#include "qgsprocessingmodelparameter.h"
#include "qgsprocessingmodelchildalgorithm.h"
#include "qgsprocessingmodeloutput.h"
#include "qgsprocessingmodelgroupbox.h"
#include "qgsmodelgraphicsscene.h"
#include "qgsapplication.h"
#include "qgsmodelgraphicitem.h"
#include "qgsprocessingmodelalgorithm.h"
#include "qgsmodelgraphicsview.h"
#include "qgsmodelviewtool.h"
#include "qgsmodelviewmouseevent.h"
#include "qgsmodelgroupboxdefinitionwidget.h"

#include <QSvgRenderer>
#include <QPicture>
Expand Down Expand Up @@ -366,7 +368,7 @@ void QgsModelComponentGraphicItem::paint( QPainter *painter, const QStyleOptionG
{
QRectF labelRect = QRectF( rect.left() + TEXT_MARGIN, rect.top() + TEXT_MARGIN, rect.width() - 2 * TEXT_MARGIN - mButtonSize.width() - BUTTON_MARGIN, rect.height() - 2 * TEXT_MARGIN );
text = label();
painter->drawText( labelRect, Qt::TextWordWrap, text );
painter->drawText( labelRect, Qt::TextWordWrap | titleAlignment(), text );
}
else
{
Expand Down Expand Up @@ -472,6 +474,11 @@ Qt::PenStyle QgsModelComponentGraphicItem::strokeStyle( QgsModelComponentGraphic
return Qt::SolidLine;
}

Qt::Alignment QgsModelComponentGraphicItem::titleAlignment() const
{
return Qt::AlignLeft;
}

QPicture QgsModelComponentGraphicItem::iconPicture() const
{
return QPicture();
Expand Down Expand Up @@ -1160,7 +1167,129 @@ void QgsModelOutputGraphicItem::deleteComponent()
}


//
// QgsModelGroupBoxGraphicItem
//

QgsModelGroupBoxGraphicItem::QgsModelGroupBoxGraphicItem( QgsProcessingModelGroupBox *box, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent )
: QgsModelComponentGraphicItem( box, model, parent )
{
setZValue( QgsModelGraphicsScene::ZValues::GroupBox );
setLabel( box->description() );

QFont f = font();
f.setBold( true );
f.setPixelSize( 14 );
setFont( f );
}

void QgsModelGroupBoxGraphicItem::contextMenuEvent( QGraphicsSceneContextMenuEvent *event )
{
QMenu *popupmenu = new QMenu( event->widget() );
QAction *removeAction = popupmenu->addAction( QObject::tr( "Remove" ) );
connect( removeAction, &QAction::triggered, this, &QgsModelGroupBoxGraphicItem::deleteComponent );
QAction *editAction = popupmenu->addAction( QObject::tr( "Edit…" ) );
connect( editAction, &QAction::triggered, this, &QgsModelGroupBoxGraphicItem::editComponent );
popupmenu->exec( event->screenPos() );
}

QgsModelGroupBoxGraphicItem::~QgsModelGroupBoxGraphicItem() = default;

QColor QgsModelGroupBoxGraphicItem::fillColor( QgsModelComponentGraphicItem::State state ) const
{
QColor c( 230, 230, 230 );
switch ( state )
{
case Selected:
c = c.darker( 110 );
break;
case Hover:
c = c.darker( 105 );
break;

case Normal:
break;
}
return c;
}

QColor QgsModelGroupBoxGraphicItem::strokeColor( QgsModelComponentGraphicItem::State state ) const
{
switch ( state )
{
case Selected:
return QColor( 50, 50, 50 );
case Hover:
case Normal:
return QColor( 150, 150, 150 );
}
return QColor();
}

QColor QgsModelGroupBoxGraphicItem::textColor( QgsModelComponentGraphicItem::State ) const
{
return QColor( 100, 100, 100 );
}

Qt::PenStyle QgsModelGroupBoxGraphicItem::strokeStyle( QgsModelComponentGraphicItem::State ) const
{
return Qt::DotLine;
}

Qt::Alignment QgsModelGroupBoxGraphicItem::titleAlignment() const
{
return Qt::AlignHCenter;
}

void QgsModelGroupBoxGraphicItem::updateStoredComponentPosition( const QPointF &pos, const QSizeF &size )
{
if ( QgsProcessingModelGroupBox *box = dynamic_cast< QgsProcessingModelGroupBox * >( component() ) )
{
box->setPosition( pos );
box->setSize( size );
model()->addGroupBox( *box );
}
}

bool QgsModelGroupBoxGraphicItem::canDeleteComponent()
{
if ( dynamic_cast< QgsProcessingModelGroupBox * >( component() ) )
{
return true;
}
return false;
}

void QgsModelGroupBoxGraphicItem::deleteComponent()
{
if ( const QgsProcessingModelGroupBox *box = dynamic_cast< const QgsProcessingModelGroupBox * >( component() ) )
{
emit aboutToChange( tr( "Delete Group Box" ) );
model()->removeGroupBox( box->uuid() );
emit changed();
emit requestModelRepaint();
}
}

void QgsModelGroupBoxGraphicItem::editComponent()
{
if ( const QgsProcessingModelGroupBox *box = dynamic_cast< const QgsProcessingModelGroupBox * >( component() ) )
{
QgsModelGroupBoxDefinitionDialog dlg( *box, this->scene()->views().at( 0 ) );

if ( dlg.exec() )
{
emit aboutToChange( tr( "Edit Group Box" ) );
model()->addGroupBox( dlg.groupBox() );
emit changed();
emit requestModelRepaint();
}
}
}

//
// QgsModelCommentGraphicItem
//

QgsModelCommentGraphicItem::QgsModelCommentGraphicItem( QgsProcessingModelComment *comment, QgsModelComponentGraphicItem *parentItem, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent )
: QgsModelComponentGraphicItem( comment, model, parent )
Expand Down

0 comments on commit 4fa9083

Please sign in to comment.