Skip to content

Commit

Permalink
Added sanity check of links between graphicitems showing error in dia…
Browse files Browse the repository at this point in the history
…log. Fixes #40724 #39260
  • Loading branch information
luipir authored and github-actions[bot] committed Feb 11, 2021
1 parent dbd1c41 commit 7716385
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
Expand Up @@ -148,7 +148,7 @@ Sets the message ``bar`` associated with the scene.
.. seealso:: :py:func:`messageBar`
%End

void showWarning( const QString &shortMessage, const QString &title, const QString &longMessage, Qgis::MessageLevel level = Qgis::Warning );
void showWarning( const QString &shortMessage, const QString &title, const QString &longMessage, Qgis::MessageLevel level = Qgis::Warning ) const;
%Docstring
Shows a warning message, allowing users to click a button to see the full details (``longMessage``).
%End
Expand Down
3 changes: 2 additions & 1 deletion python/plugins/processing/modeler/ModelerDialog.py
Expand Up @@ -221,8 +221,9 @@ def repaintModel(self, showControls=True):
scene.setFlag(QgsModelGraphicsScene.FlagHideComments)

context = createContext()
scene.createItems(self.model(), context)
self.setModelScene(scene)
# create items later that setModelScene to setup link to messageBar to the scene
scene.createItems(self.model(), context)

def create_widget_context(self):
"""
Expand Down
19 changes: 18 additions & 1 deletion src/gui/processing/models/qgsmodelcomponentgraphicitem.cpp
Expand Up @@ -983,12 +983,21 @@ QString QgsModelChildAlgorithmGraphicItem::linkPointText( Qt::Edge edge, int ind
if ( const QgsProcessingModelChildAlgorithm *child = dynamic_cast< const QgsProcessingModelChildAlgorithm * >( component() ) )
{
if ( !child->algorithm() )
return 0;
return QString();

switch ( edge )
{
case Qt::BottomEdge:
{
if ( index >= child->algorithm()->outputDefinitions().length() )
{
// something goes wrong and tried to link to an not existing output
QgsMessageLog::logMessage(
tr( "Cannot link output for child: %1" ).arg( child->algorithm()->name() ),
"QgsModelChildAlgorithmGraphicItem", Qgis::Warning, true );
return QString();
}

const QgsProcessingOutputDefinition *output = child->algorithm()->outputDefinitions().at( index );
QString title = output->description();
if ( mResults.contains( output->name() ) )
Expand All @@ -1006,6 +1015,14 @@ QString QgsModelChildAlgorithmGraphicItem::linkPointText( Qt::Edge edge, int ind
return param->flags() & QgsProcessingParameterDefinition::FlagHidden || param->isDestination();
} ), params.end() );

if ( index >= params.length() )
{
// something goes wrong and tried to link to an not existing source parameter
QgsMessageLog::logMessage(
tr( "Cannot link source for child: %1" ).arg( child->algorithm()->name() ),
"QgsModelChildAlgorithmGraphicItem", Qgis::Warning, true );
return QString();
}

QString title = params.at( index )->description();
if ( !mInputs.value( params.at( index )->name() ).toString().isEmpty() )
Expand Down
18 changes: 16 additions & 2 deletions src/gui/processing/models/qgsmodelgraphicsscene.cpp
Expand Up @@ -22,6 +22,7 @@
#include "qgsmessagebar.h"
#include "qgsmessagebaritem.h"
#include "qgsmessageviewer.h"
#include "qgsapplication.h"
#include <QGraphicsSceneMouseEvent>
#include <QPushButton>

Expand Down Expand Up @@ -409,6 +410,20 @@ QList<QgsModelGraphicsScene::LinkSource> QgsModelGraphicsScene::linkSourcesForPa
LinkSource l;
l.item = mChildAlgorithmItems.value( source.outputChildId() );
l.edge = Qt::BottomEdge;

// do sanity check of linked index
if ( i >= model->childAlgorithm( source.outputChildId() ).algorithm()->outputDefinitions().length() )
{
QString short_message = tr( "Check output links for alg: %1" ).arg( model->childAlgorithm( source.outputChildId() ).algorithm()->name() );
QString long_message = tr( "Cannot link output for alg: %1" ).arg( model->childAlgorithm( source.outputChildId() ).algorithm()->name() );
QString title( tr( "Algorithm link error" ) );
if ( messageBar() )
showWarning( const_cast<QString &>( short_message ), const_cast<QString &>( title ), const_cast<QString &>( long_message ) );
else
QgsMessageLog::logMessage( long_message, "QgsModelGraphicsScene", Qgis::Warning, true );
break;
}

l.linkIndex = i;
res.append( l );
}
Expand Down Expand Up @@ -467,7 +482,7 @@ void QgsModelGraphicsScene::setMessageBar( QgsMessageBar *messageBar )
mMessageBar = messageBar;
}

void QgsModelGraphicsScene::showWarning( const QString &shortMessage, const QString &title, const QString &longMessage, Qgis::MessageLevel level )
void QgsModelGraphicsScene::showWarning( const QString &shortMessage, const QString &title, const QString &longMessage, Qgis::MessageLevel level ) const
{
QgsMessageBarItem *messageWidget = mMessageBar->createMessage( QString(), shortMessage );
QPushButton *detailsButton = new QPushButton( tr( "Details" ) );
Expand All @@ -484,4 +499,3 @@ void QgsModelGraphicsScene::showWarning( const QString &shortMessage, const QStr
}

///@endcond

2 changes: 1 addition & 1 deletion src/gui/processing/models/qgsmodelgraphicsscene.h
Expand Up @@ -164,7 +164,7 @@ class GUI_EXPORT QgsModelGraphicsScene : public QGraphicsScene
/**
* Shows a warning message, allowing users to click a button to see the full details (\a longMessage).
*/
void showWarning( const QString &shortMessage, const QString &title, const QString &longMessage, Qgis::MessageLevel level = Qgis::Warning );
void showWarning( const QString &shortMessage, const QString &title, const QString &longMessage, Qgis::MessageLevel level = Qgis::Warning ) const;

signals:

Expand Down

0 comments on commit 7716385

Please sign in to comment.