QGIS API Documentation 3.41.0-Master (cea29feecf2)
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 QgsProcessingHelpEditorTreeItem( const QString &name, const QString &description )
39 : QTreeWidgetItem()
40 , name( name )
41 , description( description )
42 {
43 setText( 0, description );
44 }
45
46 QString name;
47 QString description;
48};
49
50
51QgsProcessingHelpEditorWidget::QgsProcessingHelpEditorWidget( QWidget *parent )
52 : QWidget( parent )
53 , mCurrentName( ALGORITHM_DESCRIPTION )
54{
55 setupUi( this );
56
57 connect( mElementTree, &QTreeWidget::currentItemChanged, this, &QgsProcessingHelpEditorWidget::changeItem );
58 connect( mTextEdit, &QTextEdit::textChanged, this, [=] {
59 if ( !mCurrentName.isEmpty() )
60 {
61 if ( mEditStackedWidget->currentWidget() == mPagePlainText )
62 {
63 mHelpContent[mCurrentName] = mTextEdit->toPlainText();
64 updateHtmlView();
65 }
66 }
67 } );
68
69 connect( mRichTextEdit, &QgsRichTextEditor::textChanged, this, [=] {
70 if ( !mCurrentName.isEmpty() )
71 {
72 if ( mEditStackedWidget->currentWidget() == mPageRichEdit )
73 {
74 mHelpContent[mCurrentName] = mRichTextEdit->toHtml();
75 updateHtmlView();
76 }
77 }
78 } );
79}
80
81QgsProcessingHelpEditorWidget::~QgsProcessingHelpEditorWidget() = default;
82
83void QgsProcessingHelpEditorWidget::setAlgorithm( const QgsProcessingAlgorithm *algorithm )
84{
85 if ( !algorithm )
86 return;
87
88 mAlgorithm.reset( algorithm->create() );
89
90 if ( const QgsProcessingModelAlgorithm *model = dynamic_cast<const QgsProcessingModelAlgorithm *>( mAlgorithm.get() ) )
91 {
92 mHelpContent = model->helpContent();
93 }
94
95 mEditStackedWidget->setCurrentWidget( mPageRichEdit );
96 if ( mHelpContent.contains( ALGORITHM_DESCRIPTION ) )
97 {
98 mRichTextEdit->setText( mHelpContent.value( ALGORITHM_DESCRIPTION ).toString() );
99 }
100
101 mElementTree->addTopLevelItem( new QgsProcessingHelpEditorTreeItem( ALGORITHM_DESCRIPTION, tr( "Algorithm description" ) ) );
102 mElementTree->addTopLevelItem( new QgsProcessingHelpEditorTreeItem( ALGORITHM_SHORT_DESCRIPTION, tr( "Short description" ) ) );
103
104 QgsProcessingHelpEditorTreeItem *parametersItem = new QgsProcessingHelpEditorTreeItem( QString(), tr( "Input parameters" ) );
105 mElementTree->addTopLevelItem( parametersItem );
106
107 const QList<const QgsProcessingParameterDefinition *> definitions = mAlgorithm->parameterDefinitions();
108 for ( const QgsProcessingParameterDefinition *definition : definitions )
109 {
110 if ( definition->flags() & Qgis::ProcessingParameterFlag::Hidden || definition->isDestination() )
111 continue;
112
113 parametersItem->addChild( new QgsProcessingHelpEditorTreeItem( definition->name(), definition->description() ) );
114 }
115
116 QgsProcessingHelpEditorTreeItem *outputsItem = new QgsProcessingHelpEditorTreeItem( QString(), tr( "Outputs" ) );
117 mElementTree->addTopLevelItem( outputsItem );
118 const QList<const QgsProcessingOutputDefinition *> outputs = mAlgorithm->outputDefinitions();
119 for ( const QgsProcessingOutputDefinition *output : outputs )
120 {
121 outputsItem->addChild( new QgsProcessingHelpEditorTreeItem( output->name(), output->description() ) );
122 }
123
124 mElementTree->addTopLevelItem( new QgsProcessingHelpEditorTreeItem( ALGORITHM_EXAMPLES, tr( "Examples" ) ) );
125
126 mElementTree->addTopLevelItem( new QgsProcessingHelpEditorTreeItem( ALGORITHM_CREATOR, tr( "Algorithm author" ) ) );
127 mElementTree->addTopLevelItem( new QgsProcessingHelpEditorTreeItem( ALGORITHM_HELP_CREATOR, tr( "Help author" ) ) );
128 mElementTree->addTopLevelItem( new QgsProcessingHelpEditorTreeItem( ALGORITHM_VERSION, tr( "Algorithm version" ) ) );
129 mElementTree->addTopLevelItem( new QgsProcessingHelpEditorTreeItem( ALGORITHM_HELP_URL, tr( "Documentation help URL (for help button)" ) ) );
130
131 updateHtmlView();
132}
133
134QVariantMap QgsProcessingHelpEditorWidget::helpContent()
135{
136 storeCurrentValue();
137 return mHelpContent;
138}
139
140void QgsProcessingHelpEditorWidget::updateHtmlView()
141{
142 mTextPreview->setHtml( formattedHelp() );
143}
144
145void QgsProcessingHelpEditorWidget::changeItem( QTreeWidgetItem *, QTreeWidgetItem * )
146{
147 if ( QgsProcessingHelpEditorTreeItem *item = dynamic_cast<QgsProcessingHelpEditorTreeItem *>( mElementTree->currentItem() ) )
148 {
149 storeCurrentValue();
150
151 const QString name = item->name;
152 if ( !name.isEmpty() )
153 {
154 mTextEdit->setEnabled( true );
155 mRichTextEdit->setEnabled( true );
156
157 updateHtmlView();
158 mCurrentName = name;
159
160 const bool useRichTextEdit = name == ALGORITHM_EXAMPLES || name == ALGORITHM_DESCRIPTION;
161 if ( useRichTextEdit )
162 {
163 mEditStackedWidget->setCurrentWidget( mPageRichEdit );
164 if ( mHelpContent.contains( name ) )
165 mRichTextEdit->setText( mHelpContent.value( name ).toString() );
166 else
167 mRichTextEdit->setText( QString() );
168 }
169 else
170 {
171 mEditStackedWidget->setCurrentWidget( mPagePlainText );
172 if ( mHelpContent.contains( name ) )
173 mTextEdit->setText( mHelpContent.value( name ).toString() );
174 else
175 mTextEdit->clear();
176 }
177 }
178 else
179 {
180 mCurrentName.clear();
181 mTextEdit->clear();
182 mRichTextEdit->setText( QString() );
183 mTextEdit->setEnabled( false );
184 mRichTextEdit->setEnabled( false );
185 mEditStackedWidget->setCurrentWidget( mPagePlainText );
186 updateHtmlView();
187 }
188 }
189}
190
191QString QgsProcessingHelpEditorWidget::formattedHelp() const
192{
193 if ( !mAlgorithm )
194 return QString();
195
196 return QgsProcessingUtils::formatHelpMapAsHtml( mHelpContent, mAlgorithm.get() );
197}
198
199void QgsProcessingHelpEditorWidget::storeCurrentValue()
200{
201 if ( !mCurrentName.isEmpty() )
202 {
203 if ( mEditStackedWidget->currentWidget() == mPagePlainText )
204 mHelpContent[mCurrentName] = mTextEdit->toPlainText();
205 else
206 mHelpContent[mCurrentName] = mRichTextEdit->toHtml();
207 }
208}
209
210QgsProcessingHelpEditorDialog::QgsProcessingHelpEditorDialog( QWidget *parent, Qt::WindowFlags flags )
211 : QDialog( parent, flags )
212{
213 setObjectName( QStringLiteral( "QgsProcessingHelpEditorDialog" ) );
214
215 QVBoxLayout *vLayout = new QVBoxLayout();
216 mWidget = new QgsProcessingHelpEditorWidget();
217 vLayout->addWidget( mWidget, 1 );
218
219 QDialogButtonBox *buttonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel );
220 connect( buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
221 connect( buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
222 vLayout->addWidget( buttonBox );
223 setLayout( vLayout );
224
226}
227
228void QgsProcessingHelpEditorDialog::setAlgorithm( const QgsProcessingAlgorithm *algorithm )
229{
230 mWidget->setAlgorithm( algorithm );
231}
232
233QVariantMap QgsProcessingHelpEditorDialog::helpContent()
234{
235 return mWidget->helpContent();
236}
237
238
@ 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:210
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