Move some model designer code to c++

This commit is contained in:
Nyall Dawson 2024-04-16 13:53:02 +10:00
parent 7f4fe65bee
commit 133bad76ee
5 changed files with 58 additions and 32 deletions

View File

@ -94,6 +94,8 @@ Raise, unminimize and activate this window.
virtual bool saveModel( bool saveAs = false ) = 0;
virtual QgsProcessingAlgorithmDialogBase *createExecutionDialog() = 0 /TransferBack/;
QToolBar *toolbar();
QAction *actionOpen();
QAction *actionSaveInProject();

View File

@ -94,6 +94,8 @@ Raise, unminimize and activate this window.
virtual bool saveModel( bool saveAs = false ) = 0;
virtual QgsProcessingAlgorithmDialogBase *createExecutionDialog() = 0 /TransferBack/;
QToolBar *toolbar();
QAction *actionOpen();
QAction *actionSaveInProject();

View File

@ -100,7 +100,6 @@ class ModelerDialog(QgsModelDesignerDialog):
self.actionOpen().triggered.connect(self.openModel)
self.actionSaveInProject().triggered.connect(self.saveInProject)
self.actionRun().triggered.connect(self.runModel)
if model is not None:
_model = model.create()
@ -122,37 +121,9 @@ class ModelerDialog(QgsModelDesignerDialog):
self.context_generator = ContextGenerator(self.processing_context)
def runModel(self):
valid, errors = self.model().validate()
if not valid:
message_box = QMessageBox()
message_box.setWindowTitle(self.tr('Model is Invalid'))
message_box.setIcon(QMessageBox.Icon.Warning)
message_box.setText(self.tr('This model is not valid and contains one or more issues. Are you sure you want to run it in this state?'))
message_box.setStandardButtons(QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.Cancel)
message_box.setDefaultButton(QMessageBox.StandardButton.Cancel)
error_string = ''
for e in errors:
e = re.sub(r'<[^>]*>', '', e)
error_string += f'{e}\n'
message_box.setDetailedText(error_string)
if message_box.exec() == QMessageBox.StandardButton.Cancel:
return
def on_finished(successful, results):
self.setLastRunChildAlgorithmResults(dlg.results().get('CHILD_RESULTS', {}))
self.setLastRunChildAlgorithmInputs(dlg.results().get('CHILD_INPUTS', {}))
def createExecutionDialog(self):
dlg = AlgorithmDialog(self.model().create(), parent=self)
dlg.setLogLevel(QgsProcessingContext.LogLevel.ModelDebug)
dlg.setParameters(self.model().designerParameterValues())
dlg.algorithmFinished.connect(on_finished)
dlg.exec()
if dlg.wasExecuted():
self.model().setDesignerParameterValues(dlg.createProcessingParameters(flags=QgsProcessingParametersGenerator.Flags(QgsProcessingParametersGenerator.Flag.SkipDefaultValueParameters)))
return dlg
def saveInProject(self):
if not self.validateSave(QgsModelDesignerDialog.SaveAction.SaveInProject):

View File

@ -38,7 +38,7 @@
#include "qgsprocessinghelpeditorwidget.h"
#include "qgsscreenhelper.h"
#include "qgsmessagelog.h"
#include "qgsprocessingalgorithmdialogbase.h"
#include <QShortcut>
#include <QKeySequence>
#include <QFileDialog>
@ -157,6 +157,7 @@ QgsModelDesignerDialog::QgsModelDesignerDialog( QWidget *parent, Qt::WindowFlags
connect( mActionReorderOutputs, &QAction::triggered, this, &QgsModelDesignerDialog::reorderOutputs );
connect( mActionEditHelp, &QAction::triggered, this, &QgsModelDesignerDialog::editHelp );
connect( mReorderInputsButton, &QPushButton::clicked, this, &QgsModelDesignerDialog::reorderInputs );
connect( mActionRun, &QAction::triggered, this, &QgsModelDesignerDialog::run );
mActionSnappingEnabled->setChecked( settings.value( QStringLiteral( "/Processing/Modeler/enableSnapToGrid" ), false ).toBool() );
connect( mActionSnappingEnabled, &QAction::toggled, this, [ = ]( bool enabled )
@ -994,6 +995,52 @@ void QgsModelDesignerDialog::editHelp()
}
}
void QgsModelDesignerDialog::run()
{
QStringList errors;
const bool isValid = model()->validate( errors );
if ( !isValid )
{
QMessageBox messageBox;
messageBox.setWindowTitle( tr( "Model is Invalid" ) );
messageBox.setIcon( QMessageBox::Icon::Warning );
messageBox.setText( tr( "This model is not valid and contains one or more issues. Are you sure you want to run it in this state?" ) );
messageBox.setStandardButtons( QMessageBox::StandardButton::Yes | QMessageBox::StandardButton::Cancel );
messageBox.setDefaultButton( QMessageBox::StandardButton::Cancel );
QString errorString;
for ( const QString &error : std::as_const( errors ) )
{
QString cleanedError = error;
const thread_local QRegularExpression re( QStringLiteral( "<[^>]*>" ) );
cleanedError.replace( re, QString() );
errorString += QStringLiteral( "• %1\n" ).arg( cleanedError );
}
messageBox.setDetailedText( errorString );
if ( messageBox.exec() == QMessageBox::StandardButton::Cancel )
return;
}
std::unique_ptr< QgsProcessingAlgorithmDialogBase > dialog( createExecutionDialog() );
if ( !dialog )
return;
dialog->setLogLevel( Qgis::ProcessingLogLevel::ModelDebug );
dialog->setParameters( mModel->designerParameterValues() );
connect( dialog.get(), &QgsProcessingAlgorithmDialogBase::algorithmFinished, this, [this, &dialog]( bool, const QVariantMap & )
{
const QVariantMap dialogResults = dialog->results();
setLastRunChildAlgorithmResults( dialogResults.value( QStringLiteral( "CHILD_RESULTS" ), QVariantMap() ).toMap() );
setLastRunChildAlgorithmInputs( dialogResults.value( QStringLiteral( "CHILD_INPUTS" ), QVariantMap() ).toMap() );
mModel->setDesignerParameterValues( dialog->createProcessingParameters( QgsProcessingParametersGenerator::Flag::SkipDefaultValueParameters ) );
} );
dialog->exec();
}
void QgsModelDesignerDialog::validate()
{
QStringList issues;

View File

@ -30,6 +30,7 @@ class QUndoView;
class QgsModelViewToolPan;
class QgsModelViewToolSelect;
class QgsScreenHelper;
class QgsProcessingAlgorithmDialogBase;
///@cond NOT_STABLE
@ -126,6 +127,8 @@ class GUI_EXPORT QgsModelDesignerDialog : public QMainWindow, public Ui::QgsMode
virtual void exportAsScriptAlgorithm() = 0;
// cppcheck-suppress pureVirtualCall
virtual bool saveModel( bool saveAs = false ) = 0;
// cppcheck-suppress pureVirtualCall
virtual QgsProcessingAlgorithmDialogBase *createExecutionDialog() = 0 SIP_TRANSFERBACK;
QToolBar *toolbar() { return mToolbar; }
QAction *actionOpen() { return mActionOpen; }
@ -186,6 +189,7 @@ class GUI_EXPORT QgsModelDesignerDialog : public QMainWindow, public Ui::QgsMode
void reorderOutputs();
void setPanelVisibility( bool hidden );
void editHelp();
void run();
private: