Skip to content

Commit

Permalink
Merge pull request #34928 from gacarrillor/adjust_run_button_status_i…
Browse files Browse the repository at this point in the history
…n_alg_dialog

[processing][needs-docs] Adjust status of controls executing an algorithm dialog
  • Loading branch information
m-kuhn committed Mar 18, 2020
2 parents 29db15a + 871a3c0 commit 01c7cb1
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 3 deletions.
Expand Up @@ -73,6 +73,11 @@ Returns the main widget for the dialog, usually a panel for configuring algorith
void showLog();
%Docstring
Switches the dialog to the log page.
%End

void showParameters();
%Docstring
Switches the dialog to the parameters page.
%End

bool wasExecuted() const;
Expand Down Expand Up @@ -195,6 +200,11 @@ Returns the dialog's run button.
QPushButton *cancelButton();
%Docstring
Returns the dialog's cancel button.
%End

QPushButton *changeParametersButton();
%Docstring
Returns the dialog's change parameters button.
%End

QDialogButtonBox *buttonBox();
Expand All @@ -221,6 +231,12 @@ Sets whether the algorithm was executed through the dialog.
.. seealso:: :py:func:`setResults`
%End

void setExecutedAnyResult( bool executedAnyResult );
%Docstring
Sets whether the algorithm was executed through the dialog (no matter the result).
%End


void setResults( const QVariantMap &results );
%Docstring
Sets the algorithm results.
Expand All @@ -238,6 +254,29 @@ Displays an info ``message`` in the dialog's log.
void resetGui();
%Docstring
Resets the dialog's gui, ready for another algorithm execution.
%End

virtual void resetAdditionalGui();
%Docstring
For subclasses to register their own GUI controls to be reset, ready
for another algorithm execution.
%End

void updateRunButtonVisibility();
%Docstring
Sets visibility for mutually exclusive buttons Run and Change Parameters.
%End

void blockControlsWhileRunning();
%Docstring
Blocks run and changeParameters buttons and parameters tab while the
algorithm is running.
%End

virtual void blockAdditionalControlsWhileRunning();
%Docstring
For subclasses to register their own GUI controls to be blocked while
the algorithm is running.
%End

QgsMessageBar *messageBar();
Expand Down
14 changes: 13 additions & 1 deletion python/plugins/processing/gui/AlgorithmDialog.py
Expand Up @@ -90,6 +90,8 @@ def __init__(self, alg, in_place=False, parent=None):
self.buttonBox().button(QDialogButtonBox.Close).setText(QCoreApplication.translate("AlgorithmDialog", "Cancel"))
self.setWindowTitle(self.windowTitle() + ' | ' + self.active_layer.name())

self.updateRunButtonVisibility()

def getParametersPanel(self, alg, parent):
return ParametersPanel(parent, alg, self.in_place)

Expand All @@ -99,6 +101,14 @@ def runAsBatch(self):
dlg.show()
dlg.exec_()

def resetAdditionalGui(self):
if not self.in_place:
self.runAsBatchButton.setEnabled(True)

def blockAdditionalControlsWhileRunning(self):
if not self.in_place:
self.runAsBatchButton.setEnabled(False)

def setParameters(self, parameters):
self.mainWidget().setParameters(parameters)

Expand Down Expand Up @@ -189,7 +199,9 @@ def runAlgorithm(self):
QMessageBox.warning(
self, self.tr('Unable to execute algorithm'), msg)
return
self.runButton().setEnabled(False)

self.blockControlsWhileRunning()
self.setExecutedAnyResult(True)
self.cancelButton().setEnabled(False)
buttons = self.mainWidget().iterateButtons
self.iterateParam = None
Expand Down
12 changes: 10 additions & 2 deletions python/plugins/processing/gui/BatchAlgorithmDialog.py
Expand Up @@ -77,6 +77,8 @@ def __init__(self, alg, parent=None):
self.btnRunSingle.clicked.connect(self.runAsSingle)
self.buttonBox().addButton(self.btnRunSingle, QDialogButtonBox.ResetRole) # reset role to ensure left alignment

self.updateRunButtonVisibility()

def runAsSingle(self):
self.close()

Expand All @@ -85,6 +87,12 @@ def runAsSingle(self):
dlg.show()
dlg.exec_()

def resetAdditionalGui(self):
self.btnRunSingle.setEnabled(True)

def blockAdditionalControlsWhileRunning(self):
self.btnRunSingle.setEnabled(False)

def runAlgorithm(self):
alg_parameters = []

Expand All @@ -106,7 +114,8 @@ def runAlgorithm(self):

with OverrideCursor(Qt.WaitCursor):

self.mainWidget().setEnabled(False)
self.blockControlsWhileRunning()
self.setExecutedAnyResult(True)
self.cancelButton().setEnabled(True)

# Make sure the Log tab is visible before executing the algorithm
Expand Down Expand Up @@ -178,7 +187,6 @@ def finish(self, algorithm_results, errors):
self.loadHTMLResults(results['results'], count)

self.createSummaryTable(algorithm_results, errors)
self.mainWidget().setEnabled(True)
self.resetGui()

def loadHTMLResults(self, results, num):
Expand Down
62 changes: 62 additions & 0 deletions src/gui/processing/qgsprocessingalgorithmdialogbase.cpp
Expand Up @@ -105,6 +105,10 @@ QgsProcessingAlgorithmDialogBase::QgsProcessingAlgorithmDialogBase( QWidget *par
buttonCancel->setEnabled( false );
mButtonClose = mButtonBox->button( QDialogButtonBox::Close );

mButtonChangeParameters = new QPushButton( tr( "Change Parameters" ) );
mButtonBox->addButton( mButtonChangeParameters, QDialogButtonBox::ActionRole );

connect( mButtonChangeParameters, &QPushButton::clicked, this, &QgsProcessingAlgorithmDialogBase::showParameters );
connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsProcessingAlgorithmDialogBase::openHelp );
connect( mButtonCollapse, &QToolButton::clicked, this, &QgsProcessingAlgorithmDialogBase::toggleCollapsed );
connect( splitter, &QSplitter::splitterMoved, this, &QgsProcessingAlgorithmDialogBase::splitterChanged );
Expand All @@ -113,6 +117,8 @@ QgsProcessingAlgorithmDialogBase::QgsProcessingAlgorithmDialogBase( QWidget *par
connect( mButtonCopyLog, &QToolButton::clicked, this, &QgsProcessingAlgorithmDialogBase::copyLogToClipboard );
connect( mButtonClearLog, &QToolButton::clicked, this, &QgsProcessingAlgorithmDialogBase::clearLog );

connect( mTabWidget, &QTabWidget::currentChanged, this, &QgsProcessingAlgorithmDialogBase::mTabWidget_currentChanged );

mMessageBar = new QgsMessageBar();
mMessageBar->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Fixed );
verticalLayout->insertWidget( 0, mMessageBar );
Expand Down Expand Up @@ -239,6 +245,11 @@ void QgsProcessingAlgorithmDialogBase::showLog()
mTabWidget->setCurrentIndex( 1 );
}

void QgsProcessingAlgorithmDialogBase::showParameters()
{
mTabWidget->setCurrentIndex( 0 );
}

QPushButton *QgsProcessingAlgorithmDialogBase::runButton()
{
return mButtonRun;
Expand All @@ -249,6 +260,11 @@ QPushButton *QgsProcessingAlgorithmDialogBase::cancelButton()
return buttonCancel;
}

QPushButton *QgsProcessingAlgorithmDialogBase::changeParametersButton()
{
return mButtonChangeParameters;
}

void QgsProcessingAlgorithmDialogBase::clearProgress()
{
progressBar->setMaximum( 0 );
Expand All @@ -259,6 +275,11 @@ void QgsProcessingAlgorithmDialogBase::setExecuted( bool executed )
mExecuted = executed;
}

void QgsProcessingAlgorithmDialogBase::setExecutedAnyResult( bool executedAnyResult )
{
mExecutedAnyResult = executedAnyResult;
}

void QgsProcessingAlgorithmDialogBase::setResults( const QVariantMap &results )
{
mResults = results;
Expand Down Expand Up @@ -311,6 +332,11 @@ void QgsProcessingAlgorithmDialogBase::splitterChanged( int, int )
}
}

void QgsProcessingAlgorithmDialogBase::mTabWidget_currentChanged( int )
{
updateRunButtonVisibility();
}

void QgsProcessingAlgorithmDialogBase::linkClicked( const QUrl &url )
{
QDesktopServices::openUrl( url.toString() );
Expand Down Expand Up @@ -553,7 +579,43 @@ void QgsProcessingAlgorithmDialogBase::resetGui()
progressBar->setMaximum( 100 );
progressBar->setValue( 0 );
mButtonRun->setEnabled( true );
mButtonChangeParameters->setEnabled( true );
mButtonClose->setEnabled( true );
if ( mMainWidget )
{
mMainWidget->setEnabled( true );
}
updateRunButtonVisibility();
resetAdditionalGui();
}

void QgsProcessingAlgorithmDialogBase::updateRunButtonVisibility()
{
// Activate run button if current tab is Parameters
bool runButtonVisible = mTabWidget->currentIndex() == 0;
mButtonRun->setVisible( runButtonVisible );
mButtonChangeParameters->setVisible( !runButtonVisible && mExecutedAnyResult && mButtonChangeParameters->isEnabled() );
}

void QgsProcessingAlgorithmDialogBase::resetAdditionalGui()
{

}

void QgsProcessingAlgorithmDialogBase::blockControlsWhileRunning()
{
mButtonRun->setEnabled( false );
mButtonChangeParameters->setEnabled( false );
if ( mMainWidget )
{
mMainWidget->setEnabled( false );
}
blockAdditionalControlsWhileRunning();
}

void QgsProcessingAlgorithmDialogBase::blockAdditionalControlsWhileRunning()
{

}

QgsMessageBar *QgsProcessingAlgorithmDialogBase::messageBar()
Expand Down
42 changes: 42 additions & 0 deletions src/gui/processing/qgsprocessingalgorithmdialogbase.h
Expand Up @@ -133,6 +133,11 @@ class GUI_EXPORT QgsProcessingAlgorithmDialogBase : public QDialog, private Ui::
*/
void showLog();

/**
* Switches the dialog to the parameters page.
*/
void showParameters();

/**
* Returns TRUE if an algorithm was executed in the dialog.
* \see results()
Expand Down Expand Up @@ -244,6 +249,11 @@ class GUI_EXPORT QgsProcessingAlgorithmDialogBase : public QDialog, private Ui::
*/
QPushButton *cancelButton();

/**
* Returns the dialog's change parameters button.
*/
QPushButton *changeParametersButton();

/**
* Returns the dialog's button box.
*/
Expand All @@ -266,6 +276,12 @@ class GUI_EXPORT QgsProcessingAlgorithmDialogBase : public QDialog, private Ui::
*/
void setExecuted( bool executed );

/**
* Sets whether the algorithm was executed through the dialog (no matter the result).
*/
void setExecutedAnyResult( bool executedAnyResult );


/**
* Sets the algorithm results.
* \see results()
Expand All @@ -283,6 +299,29 @@ class GUI_EXPORT QgsProcessingAlgorithmDialogBase : public QDialog, private Ui::
*/
void resetGui();

/**
* For subclasses to register their own GUI controls to be reset, ready
* for another algorithm execution.
*/
virtual void resetAdditionalGui();

/**
* Sets visibility for mutually exclusive buttons Run and Change Parameters.
*/
void updateRunButtonVisibility();

/**
* Blocks run and changeParameters buttons and parameters tab while the
* algorithm is running.
*/
void blockControlsWhileRunning();

/**
* For subclasses to register their own GUI controls to be blocked while
* the algorithm is running.
*/
virtual void blockAdditionalControlsWhileRunning();

/**
* Returns the dialog's message bar.
*/
Expand Down Expand Up @@ -324,6 +363,7 @@ class GUI_EXPORT QgsProcessingAlgorithmDialogBase : public QDialog, private Ui::
void toggleCollapsed();

void splitterChanged( int pos, int index );
void mTabWidget_currentChanged( int index );
void linkClicked( const QUrl &url );
void algExecuted( bool successful, const QVariantMap &results );
void taskTriggered( QgsTask *task );
Expand All @@ -333,11 +373,13 @@ class GUI_EXPORT QgsProcessingAlgorithmDialogBase : public QDialog, private Ui::

QPushButton *mButtonRun = nullptr;
QPushButton *mButtonClose = nullptr;
QPushButton *mButtonChangeParameters = nullptr;
QByteArray mSplitterState;
QToolButton *mButtonCollapse = nullptr;
QgsMessageBar *mMessageBar = nullptr;

bool mExecuted = false;
bool mExecutedAnyResult = false;
QVariantMap mResults;
QWidget *mMainWidget = nullptr;
std::unique_ptr< QgsProcessingAlgorithm > mAlgorithm;
Expand Down

0 comments on commit 01c7cb1

Please sign in to comment.