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