Skip to content

Commit

Permalink
[feature][processing] Add an advanced button to the toolbox algorithm
Browse files Browse the repository at this point in the history
dialog, which contains an option to copy the equivalent Python
command as the parameters define in the dialog

While this command is also available from the history dialog, the
advanced button provides a way for users to generate these
commands WITHOUT actually having to run the algorithm in advance.

Sponsored by the Research Institute for Nature and Forest, Flemish Govt
  • Loading branch information
nyalldawson committed Dec 16, 2021
1 parent eea2ba3 commit 1405ba1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/gui/processing/qgsprocessingalgorithmdialogbase.cpp
Expand Up @@ -32,6 +32,7 @@
#include <QClipboard>
#include <QFileDialog>
#include <QMimeData>
#include <QMenu>


///@cond NOT_STABLE
Expand Down Expand Up @@ -125,6 +126,47 @@ QgsProcessingAlgorithmDialogBase::QgsProcessingAlgorithmDialogBase( QWidget *par
buttonCancel->setEnabled( false );
mButtonClose = mButtonBox->button( QDialogButtonBox::Close );

switch ( mMode )
{
case DialogMode::Single:
{
mAdvancedButton = new QPushButton( tr( "Advanced" ) );
mAdvancedMenu = new QMenu( this );
mAdvancedButton->setMenu( mAdvancedMenu );

QAction *copyAsPythonCommand = new QAction( tr( "Copy as Python Command" ), mAdvancedMenu );
copyAsPythonCommand->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mIconPythonFile.svg" ) ) );

mAdvancedMenu->addAction( copyAsPythonCommand );
connect( copyAsPythonCommand, &QAction::triggered, this, [this]
{
if ( const QgsProcessingAlgorithm *alg = algorithm() )
{
QgsProcessingContext *context = processingContext();
if ( !context )
return;

const QString command = alg->asPythonCommand( createProcessingParameters(), *context );
QMimeData *m = new QMimeData();
m->setText( command );
QClipboard *cb = QApplication::clipboard();

#ifdef Q_OS_LINUX
cb->setMimeData( m, QClipboard::Selection );
#endif
cb->setMimeData( m, QClipboard::Clipboard );
}
} );

mButtonBox->addButton( mAdvancedButton, QDialogButtonBox::ResetRole );
break;
}

case DialogMode::Batch:
break;

}

connect( mButtonRun, &QPushButton::clicked, this, &QgsProcessingAlgorithmDialogBase::runAlgorithm );
connect( mButtonChangeParameters, &QPushButton::clicked, this, &QgsProcessingAlgorithmDialogBase::showParameters );
connect( mButtonBox, &QDialogButtonBox::rejected, this, &QgsProcessingAlgorithmDialogBase::closeClicked );
Expand Down
2 changes: 2 additions & 0 deletions src/gui/processing/qgsprocessingalgorithmdialogbase.h
Expand Up @@ -419,6 +419,8 @@ class GUI_EXPORT QgsProcessingAlgorithmDialogBase : public QDialog, public QgsPr
QByteArray mSplitterState;
QToolButton *mButtonCollapse = nullptr;
QgsMessageBar *mMessageBar = nullptr;
QPushButton *mAdvancedButton = nullptr;
QMenu *mAdvancedMenu = nullptr;

bool mExecuted = false;
bool mExecutedAnyResult = false;
Expand Down

0 comments on commit 1405ba1

Please sign in to comment.