Skip to content

Commit e7f13f5

Browse files
committedJun 23, 2017
Port model help code to c++
1 parent 9c47e1b commit e7f13f5

File tree

10 files changed

+121
-14
lines changed

10 files changed

+121
-14
lines changed
 

‎python/core/processing/qgsprocessingmodelalgorithm.sip

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,10 @@ Copies are protected to avoid slicing
566566

567567
virtual QString svgIconPath() const;
568568

569+
virtual QString shortHelpString() const;
570+
571+
virtual QString helpUrl() const;
572+
569573

570574
virtual bool canExecute( QString *errorMessage /Out/ = 0 ) const;
571575

@@ -767,6 +771,22 @@ Copies are protected to avoid slicing
767771
:rtype: bool
768772
%End
769773

774+
QVariantMap &helpContent();
775+
%Docstring
776+
Returns the model's help contents (a free-form map of values describing the algorithm's
777+
use and metadata).
778+
.. seealso:: setHelpContent()
779+
:rtype: QVariantMap
780+
%End
781+
782+
783+
void setHelpContent( const QVariantMap &contents );
784+
%Docstring
785+
Sets the model's help ``contents`` (a free-form map of values describing the algorithm's
786+
use and metadata).
787+
.. seealso:: helpContent()
788+
%End
789+
770790
protected:
771791

772792
virtual QVariantMap processAlgorithm( const QVariantMap &parameters,

‎python/core/processing/qgsprocessingutils.sip

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ class QgsProcessingUtils
151151
:rtype: str
152152
%End
153153

154+
static QString formatHelpMapAsHtml( const QVariantMap &map, const QgsProcessingAlgorithm *algorithm );
155+
%Docstring
156+
Returns a HTML formatted version of the help text encoded in a variant ``map`` for
157+
a specified ``algorithm``.
158+
:rtype: str
159+
%End
160+
154161
};
155162

156163

‎python/plugins/processing/gui/HelpEditionDialog.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def __init__(self, alg):
5656
self.alg = alg
5757
self.descriptions = {}
5858
if isinstance(self.alg, ModelerAlgorithm):
59-
self.descriptions = self.alg.helpContent
59+
self.descriptions = self.alg.helpContent()
6060
else:
6161
if self.alg.descriptionFile is not None:
6262
helpfile = alg.descriptionFile + '.help'

‎python/plugins/processing/modeler/ModelerAlgorithm.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ def __init__(self):
180180
super().__init__()
181181

182182
self.descriptionFile = None
183-
self.helpContent = {}
184183

185184
# Geoalgorithms in this model. A dict of Algorithm objects, with names as keys
186185
self.algs = {}
@@ -235,17 +234,6 @@ def asPythonCommand(self, parameters, context):
235234
else:
236235
return None
237236

238-
def helpUrl(self):
239-
try:
240-
return getHtmlFromDescriptionsDict(self, self.helpContent)
241-
except:
242-
return None
243-
244-
def shortHelpString(self):
245-
if 'ALG_DESC' in self.helpContent:
246-
return str(self.helpContent['ALG_DESC'])
247-
return None
248-
249237
def toPython(self):
250238
s = ['##%s=name' % self.name()]
251239
for param in list(self.parameterComponents().values()):

‎python/plugins/processing/modeler/ModelerDialog.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ def editHelp(self):
279279
dlg = HelpEditionDialog(alg)
280280
dlg.exec_()
281281
if dlg.descriptions:
282-
self.model.helpContent = dlg.descriptions
282+
self.model.setHelpContent(dlg.descriptions)
283283
self.hasChanged = True
284284

285285
def runModel(self):

‎src/core/processing/qgsprocessingmodelalgorithm.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "qgsprocessingmodelalgorithm.h"
1919
#include "qgsprocessingregistry.h"
2020
#include "qgsprocessingfeedback.h"
21+
#include "qgsprocessingutils.h"
2122
#include "qgsxmlutils.h"
2223
#include "qgsexception.h"
2324
#include <QFile>
@@ -292,6 +293,18 @@ QString QgsProcessingModelAlgorithm::svgIconPath() const
292293
return QgsApplication::iconPath( QStringLiteral( "processingModel.svg" ) );
293294
}
294295

296+
QString QgsProcessingModelAlgorithm::shortHelpString() const
297+
{
298+
if ( mHelpContent.contains( QStringLiteral( "ALG_DESC" ) ) )
299+
return mHelpContent.value( QStringLiteral( "ALG_DESC" ) ).toString();
300+
return QString();
301+
}
302+
303+
QString QgsProcessingModelAlgorithm::helpUrl() const
304+
{
305+
return QgsProcessingUtils::formatHelpMapAsHtml( mHelpContent, this );
306+
}
307+
295308
QVariantMap QgsProcessingModelAlgorithm::parametersForChildAlgorithm( const ChildAlgorithm &child, const QVariantMap &modelParameters, const QMap< QString, QVariantMap > &results ) const
296309
{
297310
QVariantMap childParams;
@@ -468,6 +481,16 @@ QVariantMap QgsProcessingModelAlgorithm::processAlgorithm( const QVariantMap &pa
468481
return finalResults;
469482
}
470483

484+
QVariantMap QgsProcessingModelAlgorithm::helpContent() const
485+
{
486+
return mHelpContent;
487+
}
488+
489+
void QgsProcessingModelAlgorithm::setHelpContent( const QVariantMap &helpContent )
490+
{
491+
mHelpContent = helpContent;
492+
}
493+
471494
void QgsProcessingModelAlgorithm::setName( const QString &name )
472495
{
473496
mModelName = name;
@@ -559,6 +582,7 @@ QVariant QgsProcessingModelAlgorithm::toVariant() const
559582
QVariantMap map;
560583
map.insert( QStringLiteral( "model_name" ), mModelName );
561584
map.insert( QStringLiteral( "model_group" ), mModelGroup );
585+
map.insert( QStringLiteral( "help" ), mHelpContent );
562586

563587
QVariantMap childMap;
564588
QMap< QString, ChildAlgorithm >::const_iterator childIt = mChildAlgorithms.constBegin();
@@ -592,6 +616,7 @@ bool QgsProcessingModelAlgorithm::loadVariant( const QVariant &model )
592616

593617
mModelName = map.value( QStringLiteral( "model_name" ) ).toString();
594618
mModelGroup = map.value( QStringLiteral( "model_group" ) ).toString();
619+
mHelpContent = map.value( QStringLiteral( "help" ) ).toMap();
595620

596621
mChildAlgorithms.clear();
597622
QVariantMap childMap = map.value( QStringLiteral( "children" ) ).toMap();

‎src/core/processing/qgsprocessingmodelalgorithm.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,8 @@ class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm
569569
QString group() const override;
570570
QIcon icon() const override;
571571
QString svgIconPath() const override;
572+
QString shortHelpString() const override;
573+
QString helpUrl() const override;
572574

573575
bool canExecute( QString *errorMessage SIP_OUT = nullptr ) const override;
574576

@@ -757,6 +759,27 @@ class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm
757759
*/
758760
bool fromFile( const QString &path );
759761

762+
/**
763+
* Returns the model's help contents (a free-form map of values describing the algorithm's
764+
* use and metadata).
765+
* \see setHelpContent()
766+
*/
767+
QVariantMap &helpContent() { return mHelpContent; }
768+
769+
/**
770+
* Returns the model's help contents (a free-form map of values describing the algorithm's
771+
* use and metadata).
772+
* \see setHelpContent()
773+
*/
774+
SIP_SKIP QVariantMap helpContent() const;
775+
776+
/**
777+
* Sets the model's help \a contents (a free-form map of values describing the algorithm's
778+
* use and metadata).
779+
* \see helpContent()
780+
*/
781+
void setHelpContent( const QVariantMap &contents );
782+
760783
protected:
761784

762785
QVariantMap processAlgorithm( const QVariantMap &parameters,
@@ -772,6 +795,8 @@ class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm
772795
//! Map of parameter name to model parameter component
773796
QMap< QString, ModelParameter > mParameterComponents;
774797

798+
QVariantMap mHelpContent;
799+
775800
void dependsOnChildAlgorithmsRecursive( const QString &childId, QSet<QString> &depends ) const;
776801
void dependentChildAlgorithmsRecursive( const QString &childId, QSet<QString> &depends ) const;
777802

‎src/core/processing/qgsprocessingutils.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "qgsvectorfilewriter.h"
2525
#include "qgsmemoryproviderutils.h"
2626
#include "qgsprocessingparameters.h"
27+
#include "qgsprocessingalgorithm.h"
2728

2829
QList<QgsRasterLayer *> QgsProcessingUtils::compatibleRasterLayers( QgsProject *project, bool sort )
2930
{
@@ -428,6 +429,38 @@ QString QgsProcessingUtils::generateTempFilename( const QString &basename )
428429
return path + '/' + basename;
429430
}
430431

432+
QString QgsProcessingUtils::formatHelpMapAsHtml( const QVariantMap &map, const QgsProcessingAlgorithm *algorithm )
433+
{
434+
auto getText = [map]( const QString & key )->QString
435+
{
436+
if ( map.contains( key ) )
437+
return map.value( key ).toString();
438+
return QString();
439+
};
440+
441+
QString s = QObject::tr( "<html><body><h2>Algorithm description</h2>\n " );
442+
s += QStringLiteral( "<p>" ) + getText( QStringLiteral( "ALG_DESC" ) ) + QStringLiteral( "</p>\n" );
443+
s += QObject::tr( "<h2>Input parameters</h2>\n" );
444+
445+
Q_FOREACH ( const QgsProcessingParameterDefinition *def, algorithm->parameterDefinitions() )
446+
{
447+
s += QStringLiteral( "<h3>" ) + def->description() + QStringLiteral( "</h3>\n" );
448+
s += QStringLiteral( "<p>" ) + getText( def->name() ) + QStringLiteral( "</p>\n" );
449+
}
450+
s += QObject::tr( "<h2>Outputs</h2>\n" );
451+
Q_FOREACH ( const QgsProcessingOutputDefinition *def, algorithm->outputDefinitions() )
452+
{
453+
s += QStringLiteral( "<h3>" ) + def->description() + QStringLiteral( "</h3>\n" );
454+
s += QStringLiteral( "<p>" ) + getText( def->name() ) + QStringLiteral( "</p>\n" );
455+
}
456+
s += "<br>";
457+
s += QObject::tr( "<p align=\"right\">Algorithm author: %1</p>" ).arg( getText( QStringLiteral( "ALG_CREATOR" ) ) );
458+
s += QObject::tr( "<p align=\"right\">Help author: %1</p>" ).arg( getText( QStringLiteral( "ALG_HELP_CREATOR" ) ) );
459+
s += QObject::tr( "<p align=\"right\">Algorithm version: %1</p>" ).arg( getText( QStringLiteral( "ALG_VERSION" ) ) );
460+
s += QStringLiteral( "</body></html>" );
461+
return s;
462+
}
463+
431464

432465
//
433466
// QgsProcessingFeatureSource

‎src/core/processing/qgsprocessingutils.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@ class CORE_EXPORT QgsProcessingUtils
183183
*/
184184
static QString generateTempFilename( const QString &basename );
185185

186+
/**
187+
* Returns a HTML formatted version of the help text encoded in a variant \a map for
188+
* a specified \a algorithm.
189+
*/
190+
static QString formatHelpMapAsHtml( const QVariantMap &map, const QgsProcessingAlgorithm *algorithm );
191+
186192
private:
187193

188194
static bool canUseLayer( const QgsRasterLayer *layer );

‎tests/src/core/testqgsprocessing.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3732,6 +3732,8 @@ void TestQgsProcessing::modelerAlgorithm()
37323732

37333733
// to/from XML
37343734
QgsProcessingModelAlgorithm alg5( "test", "testGroup" );
3735+
alg5.helpContent().insert( "author", "me" );
3736+
alg5.helpContent().insert( "usage", "run" );
37353737
QgsProcessingModelAlgorithm::ChildAlgorithm alg5c1;
37363738
alg5c1.setChildId( "cx1" );
37373739
alg5c1.setAlgorithmId( "buffer" );
@@ -3772,6 +3774,7 @@ void TestQgsProcessing::modelerAlgorithm()
37723774
QVERIFY( alg6.loadVariant( QgsXmlUtils::readVariant( doc.firstChildElement() ) ) );
37733775
QCOMPARE( alg6.name(), QStringLiteral( "test" ) );
37743776
QCOMPARE( alg6.group(), QStringLiteral( "testGroup" ) );
3777+
QCOMPARE( alg6.helpContent(), alg5.helpContent() );
37753778
QgsProcessingModelAlgorithm::ChildAlgorithm alg6c1 = alg6.childAlgorithm( "cx1" );
37763779
QCOMPARE( alg6c1.childId(), QStringLiteral( "cx1" ) );
37773780
QCOMPARE( alg6c1.algorithmId(), QStringLiteral( "buffer" ) );

0 commit comments

Comments
 (0)
Please sign in to comment.