QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
qgsprocessinghelpeditorwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprocessinghelpeditorwidget.h
3 ------------------------
4 Date : February 2022
5 Copyright : (C) 2022 Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17#include "moc_qgsprocessinghelpeditorwidget.cpp"
19#include "qgsgui.h"
20#include <QTreeWidgetItem>
21#include <QDialogButtonBox>
22#include <QVBoxLayout>
23
25
26const QString QgsProcessingHelpEditorWidget::ALGORITHM_DESCRIPTION = QStringLiteral( "ALG_DESC" );
27const QString QgsProcessingHelpEditorWidget::ALGORITHM_CREATOR = QStringLiteral( "ALG_CREATOR" );
28const QString QgsProcessingHelpEditorWidget::ALGORITHM_HELP_CREATOR = QStringLiteral( "ALG_HELP_CREATOR" );
29const QString QgsProcessingHelpEditorWidget::ALGORITHM_VERSION = QStringLiteral( "ALG_VERSION" );
30const QString QgsProcessingHelpEditorWidget::ALGORITHM_SHORT_DESCRIPTION = QStringLiteral( "SHORT_DESCRIPTION" );
31const QString QgsProcessingHelpEditorWidget::ALGORITHM_HELP_URL = QStringLiteral( "HELP_URL" );
32const QString QgsProcessingHelpEditorWidget::ALGORITHM_EXAMPLES = QStringLiteral( "EXAMPLES" );
33
34
35class QgsProcessingHelpEditorTreeItem : public QTreeWidgetItem
36{
37 public:
38
39 QgsProcessingHelpEditorTreeItem( const QString &name, const QString &description )
40 : QTreeWidgetItem()
41 , name( name )
42 , description( description )
43 {
44 setText( 0, description );
45 }
46
47 QString name;
48 QString description;
49
50};
51
52
53QgsProcessingHelpEditorWidget::QgsProcessingHelpEditorWidget( QWidget *parent )
54 : QWidget( parent )
55 , mCurrentName( ALGORITHM_DESCRIPTION )
56{
57 setupUi( this );
58
59 connect( mElementTree, &QTreeWidget::currentItemChanged, this, &QgsProcessingHelpEditorWidget::changeItem );
60 connect( mTextEdit, &QTextEdit::textChanged, this, [ = ]
61 {
62 if ( !mCurrentName.isEmpty() )
63 {
64 if ( mEditStackedWidget->currentWidget() == mPagePlainText )
65 {
66 mHelpContent[ mCurrentName] = mTextEdit->toPlainText();
67 updateHtmlView();
68 }
69 }
70 } );
71
72 connect( mRichTextEdit, &QgsRichTextEditor::textChanged, this, [ = ]
73 {
74 if ( !mCurrentName.isEmpty() )
75 {
76 if ( mEditStackedWidget->currentWidget() == mPageRichEdit )
77 {
78 mHelpContent[ mCurrentName] = mRichTextEdit->toHtml();
79 updateHtmlView();
80 }
81 }
82 } );
83}
84
85QgsProcessingHelpEditorWidget::~QgsProcessingHelpEditorWidget() = default;
86
87void QgsProcessingHelpEditorWidget::setAlgorithm( const QgsProcessingAlgorithm *algorithm )
88{
89 if ( !algorithm )
90 return;
91
92 mAlgorithm.reset( algorithm->create() );
93
94 if ( const QgsProcessingModelAlgorithm *model = dynamic_cast< const QgsProcessingModelAlgorithm *>( mAlgorithm.get() ) )
95 {
96 mHelpContent = model->helpContent();
97 }
98
99 mEditStackedWidget->setCurrentWidget( mPageRichEdit );
100 if ( mHelpContent.contains( ALGORITHM_DESCRIPTION ) )
101 {
102 mRichTextEdit->setText( mHelpContent.value( ALGORITHM_DESCRIPTION ).toString() );
103 }
104
105 mElementTree->addTopLevelItem( new QgsProcessingHelpEditorTreeItem( ALGORITHM_DESCRIPTION, tr( "Algorithm description" ) ) );
106 mElementTree->addTopLevelItem( new QgsProcessingHelpEditorTreeItem( ALGORITHM_SHORT_DESCRIPTION, tr( "Short description" ) ) );
107
108 QgsProcessingHelpEditorTreeItem *parametersItem = new QgsProcessingHelpEditorTreeItem( QString(), tr( "Input parameters" ) );
109 mElementTree->addTopLevelItem( parametersItem );
110
111 const QList< const QgsProcessingParameterDefinition * > definitions = mAlgorithm->parameterDefinitions();
112 for ( const QgsProcessingParameterDefinition *definition : definitions )
113 {
114 if ( definition->flags() & Qgis::ProcessingParameterFlag::Hidden || definition->isDestination() )
115 continue;
116
117 parametersItem->addChild( new QgsProcessingHelpEditorTreeItem( definition->name(), definition->description() ) );
118 }
119
120 QgsProcessingHelpEditorTreeItem *outputsItem = new QgsProcessingHelpEditorTreeItem( QString(), tr( "Outputs" ) );
121 mElementTree->addTopLevelItem( outputsItem );
122 const QList< const QgsProcessingOutputDefinition * > outputs = mAlgorithm->outputDefinitions();
123 for ( const QgsProcessingOutputDefinition *output : outputs )
124 {
125 outputsItem->addChild( new QgsProcessingHelpEditorTreeItem( output->name(), output->description() ) );
126 }
127
128 mElementTree->addTopLevelItem( new QgsProcessingHelpEditorTreeItem( ALGORITHM_EXAMPLES, tr( "Examples" ) ) );
129
130 mElementTree->addTopLevelItem( new QgsProcessingHelpEditorTreeItem( ALGORITHM_CREATOR, tr( "Algorithm author" ) ) );
131 mElementTree->addTopLevelItem( new QgsProcessingHelpEditorTreeItem( ALGORITHM_HELP_CREATOR, tr( "Help author" ) ) );
132 mElementTree->addTopLevelItem( new QgsProcessingHelpEditorTreeItem( ALGORITHM_VERSION, tr( "Algorithm version" ) ) );
133 mElementTree->addTopLevelItem( new QgsProcessingHelpEditorTreeItem( ALGORITHM_HELP_URL, tr( "Documentation help URL (for help button)" ) ) );
134
135 updateHtmlView();
136}
137
138QVariantMap QgsProcessingHelpEditorWidget::helpContent()
139{
140 storeCurrentValue();
141 return mHelpContent;
142}
143
144void QgsProcessingHelpEditorWidget::updateHtmlView()
145{
146 mTextPreview->setHtml( formattedHelp() );
147}
148
149void QgsProcessingHelpEditorWidget::changeItem( QTreeWidgetItem *, QTreeWidgetItem * )
150{
151 if ( QgsProcessingHelpEditorTreeItem *item = dynamic_cast< QgsProcessingHelpEditorTreeItem *>( mElementTree->currentItem() ) )
152 {
153 storeCurrentValue();
154
155 const QString name = item->name;
156 if ( !name.isEmpty() )
157 {
158 mTextEdit->setEnabled( true );
159 mRichTextEdit->setEnabled( true );
160
161 updateHtmlView();
162 mCurrentName = name;
163
164 const bool useRichTextEdit = name == ALGORITHM_EXAMPLES || name == ALGORITHM_DESCRIPTION;
165 if ( useRichTextEdit )
166 {
167 mEditStackedWidget->setCurrentWidget( mPageRichEdit );
168 if ( mHelpContent.contains( name ) )
169 mRichTextEdit->setText( mHelpContent.value( name ).toString() );
170 else
171 mRichTextEdit->setText( QString() );
172 }
173 else
174 {
175 mEditStackedWidget->setCurrentWidget( mPagePlainText );
176 if ( mHelpContent.contains( name ) )
177 mTextEdit->setText( mHelpContent.value( name ).toString() );
178 else
179 mTextEdit->clear();
180 }
181 }
182 else
183 {
184 mCurrentName.clear();
185 mTextEdit->clear();
186 mRichTextEdit->setText( QString() );
187 mTextEdit->setEnabled( false );
188 mRichTextEdit->setEnabled( false );
189 mEditStackedWidget->setCurrentWidget( mPagePlainText );
190 updateHtmlView();
191 }
192 }
193}
194
195QString QgsProcessingHelpEditorWidget::formattedHelp() const
196{
197 if ( !mAlgorithm )
198 return QString();
199
200 return QgsProcessingUtils::formatHelpMapAsHtml( mHelpContent, mAlgorithm.get() );
201}
202
203void QgsProcessingHelpEditorWidget::storeCurrentValue()
204{
205 if ( !mCurrentName.isEmpty() )
206 {
207 if ( mEditStackedWidget->currentWidget() == mPagePlainText )
208 mHelpContent[ mCurrentName] = mTextEdit->toPlainText();
209 else
210 mHelpContent[ mCurrentName] = mRichTextEdit->toHtml();
211 }
212}
213
214QgsProcessingHelpEditorDialog::QgsProcessingHelpEditorDialog( QWidget *parent, Qt::WindowFlags flags )
215 : QDialog( parent, flags )
216{
217 setObjectName( QStringLiteral( "QgsProcessingHelpEditorDialog" ) );
218
219 QVBoxLayout *vLayout = new QVBoxLayout();
220 mWidget = new QgsProcessingHelpEditorWidget();
221 vLayout->addWidget( mWidget, 1 );
222
223 QDialogButtonBox *buttonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel );
224 connect( buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
225 connect( buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
226 vLayout->addWidget( buttonBox );
227 setLayout( vLayout );
228
230}
231
232void QgsProcessingHelpEditorDialog::setAlgorithm( const QgsProcessingAlgorithm *algorithm )
233{
234 mWidget->setAlgorithm( algorithm );
235}
236
237QVariantMap QgsProcessingHelpEditorDialog::helpContent()
238{
239 return mWidget->helpContent();
240}
241
242
244
@ Hidden
Parameter is hidden and should not be shown to users.
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Definition qgsgui.cpp:209
Abstract base class for processing algorithms.
QgsProcessingAlgorithm * create(const QVariantMap &configuration=QVariantMap()) const
Creates a copy of the algorithm, ready for execution.
Base class for the definition of processing outputs.
Base class for the definition of processing parameters.
static QString formatHelpMapAsHtml(const QVariantMap &map, const QgsProcessingAlgorithm *algorithm)
Returns a HTML formatted version of the help text encoded in a variant map for a specified algorithm.
void textChanged()
Emitted when the text contents are changed.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into allowing algorithms to be written in pure substantial changes are required in order to port existing x Processing algorithms for QGIS x The most significant changes are outlined not GeoAlgorithm For algorithms which operate on features one by consider subclassing the QgsProcessingFeatureBasedAlgorithm class This class allows much of the boilerplate code for looping over features from a vector layer to be bypassed and instead requires implementation of a processFeature method Ensure that your algorithm(or algorithm 's parent class) implements the new pure virtual createInstance(self) call