Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[feature] Add action to processing algorithm advanced menu to copy the
current settings as JSON and paste json settings from clipboard

The utility of this is two fold:

1. It provides a way for users to copy the settings defined in the
dialog in a text format, so they can store these easily and then
restore later by pasting the value

2. It provides an easy way for users to copy the settings into
the json format consumed by qgis_process (following
#46497), so that it is easy
for users to see the expected format even for complex parameters
(like tin interpolation parameters)
  • Loading branch information
nyalldawson committed Dec 16, 2021
1 parent 53d08d7 commit 86e94ae
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
Expand Up @@ -131,6 +131,13 @@ Sets the logging ``level`` to use when running algorithms from the dialog.
.. seealso:: :py:func:`logLevel`

.. versionadded:: 3.20
%End

virtual void setParameters( const QVariantMap &values );
%Docstring
Sets the parameter ``values`` to show in the dialog.

.. versionadded:: 3.24
%End

public slots:
Expand Down
55 changes: 53 additions & 2 deletions src/gui/processing/qgsprocessingalgorithmdialogbase.cpp
Expand Up @@ -25,6 +25,7 @@
#include "qgsstringutils.h"
#include "qgsapplication.h"
#include "qgspanelwidget.h"
#include "qgsjsonutils.h"
#include <QToolButton>
#include <QDesktopServices>
#include <QScrollBar>
Expand All @@ -33,6 +34,7 @@
#include <QFileDialog>
#include <QMimeData>
#include <QMenu>
#include <nlohmann/json.hpp>


///@cond NOT_STABLE
Expand Down Expand Up @@ -160,8 +162,8 @@ QgsProcessingAlgorithmDialogBase::QgsProcessingAlgorithmDialogBase( QWidget *par

mCopyAsQgisProcessCommand = new QAction( tr( "Copy as qgis_process Command" ), mAdvancedMenu );
mCopyAsQgisProcessCommand->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mActionTerminal.svg" ) ) );

mAdvancedMenu->addAction( mCopyAsQgisProcessCommand );

connect( mCopyAsQgisProcessCommand, &QAction::triggered, this, [this]
{
if ( const QgsProcessingAlgorithm *alg = algorithm() )
Expand Down Expand Up @@ -190,6 +192,51 @@ QgsProcessingAlgorithmDialogBase::QgsProcessingAlgorithmDialogBase( QWidget *par
}
} );

mAdvancedMenu->addSeparator();

QAction *copyAsJson = new QAction( tr( "Copy as JSON" ), mAdvancedMenu );
copyAsJson->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mActionEditCopy.svg" ) ) );

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

const QVariantMap properties = alg->asMap( createProcessingParameters(), *context );
const QString json = QString::fromStdString( QgsJsonUtils::jsonFromVariant( properties ).dump( 2 ) );

QMimeData *m = new QMimeData();
m->setText( json );
QClipboard *cb = QApplication::clipboard();

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

mPasteJsonAction = new QAction( tr( "Paste Settings" ), mAdvancedMenu );
mPasteJsonAction->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mActionEditPaste.svg" ) ) );

mAdvancedMenu->addAction( mPasteJsonAction );
connect( mPasteJsonAction, &QAction::triggered, this, [this]
{
const QString text = QApplication::clipboard()->text();
if ( text.isEmpty() )
return;

const QVariantMap parameterValues = QgsJsonUtils::parseJson( text ).toMap().value( QStringLiteral( "inputs" ) ).toMap();
if ( parameterValues.isEmpty() )
return;

setParameters( parameterValues );
} );

mButtonBox->addButton( mAdvancedButton, QDialogButtonBox::ResetRole );
break;
}
Expand All @@ -200,7 +247,8 @@ QgsProcessingAlgorithmDialogBase::QgsProcessingAlgorithmDialogBase( QWidget *par

connect( mAdvancedMenu, &QMenu::aboutToShow, this, [ = ]
{
mCopyAsQgisProcessCommand->setEnabled( algorithm()&& !( algorithm()->flags() & QgsProcessingAlgorithm::FlagNotAvailableInStandaloneTool ) );
mCopyAsQgisProcessCommand->setEnabled( algorithm() && !( algorithm()->flags() & QgsProcessingAlgorithm::FlagNotAvailableInStandaloneTool ) );
mPasteJsonAction->setEnabled( !QApplication::clipboard()->text().isEmpty() );
} );

connect( mButtonRun, &QPushButton::clicked, this, &QgsProcessingAlgorithmDialogBase::runAlgorithm );
Expand All @@ -225,6 +273,9 @@ QgsProcessingAlgorithmDialogBase::QgsProcessingAlgorithmDialogBase( QWidget *par

QgsProcessingAlgorithmDialogBase::~QgsProcessingAlgorithmDialogBase() = default;

void QgsProcessingAlgorithmDialogBase::setParameters( const QVariantMap & )
{}

void QgsProcessingAlgorithmDialogBase::setAlgorithm( QgsProcessingAlgorithm *algorithm )
{
mAlgorithm.reset( algorithm );
Expand Down
8 changes: 8 additions & 0 deletions src/gui/processing/qgsprocessingalgorithmdialogbase.h
Expand Up @@ -191,6 +191,13 @@ class GUI_EXPORT QgsProcessingAlgorithmDialogBase : public QDialog, public QgsPr
*/
void setLogLevel( QgsProcessingContext::LogLevel level );

/**
* Sets the parameter \a values to show in the dialog.
*
* \since QGIS 3.24
*/
virtual void setParameters( const QVariantMap &values );

public slots:

/**
Expand Down Expand Up @@ -422,6 +429,7 @@ class GUI_EXPORT QgsProcessingAlgorithmDialogBase : public QDialog, public QgsPr
QPushButton *mAdvancedButton = nullptr;
QMenu *mAdvancedMenu = nullptr;
QAction *mCopyAsQgisProcessCommand = nullptr;
QAction *mPasteJsonAction = nullptr;

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

0 comments on commit 86e94ae

Please sign in to comment.