Skip to content
Permalink
Browse files

Added sanity check of links between graphicitems showing error in dia…

…log. Fixes #40724 #39260

(cherry picked from commit 2f7d6de)
  • Loading branch information
luipir authored and nyalldawson committed Feb 19, 2021
1 parent c74fc74 commit 0afe72976d34fd273f00e6ff23141f1f64c2f383
@@ -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
@@ -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):
"""
@@ -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() ) )
@@ -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() )
@@ -22,6 +22,7 @@
#include "qgsmessagebar.h"
#include "qgsmessagebaritem.h"
#include "qgsmessageviewer.h"
#include "qgsapplication.h"
#include <QGraphicsSceneMouseEvent>
#include <QPushButton>

@@ -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 );
}
@@ -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" ) );
@@ -484,4 +499,3 @@ void QgsModelGraphicsScene::showWarning( const QString &shortMessage, const QStr
}

///@endcond

@@ -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:

0 comments on commit 0afe729

Please sign in to comment.