Skip to content

Commit 2d1579d

Browse files
committedJun 5, 2017
Port algorithm help to QgsProcessingAlgorithm
1 parent 1e78855 commit 2d1579d

File tree

11 files changed

+118
-40
lines changed

11 files changed

+118
-40
lines changed
 

‎python/core/processing/qgsprocessingalgorithm.sip

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,32 @@ class QgsProcessingAlgorithm
7676
:rtype: list of str
7777
%End
7878

79+
virtual QString shortHelpString() const;
80+
%Docstring
81+
Returns a localised short helper string for the algorithm. This string should provide a basic description
82+
about what the algorithm does and the parameters and outputs associated with it.
83+
.. seealso:: helpString()
84+
.. seealso:: helpUrl()
85+
:rtype: str
86+
%End
87+
88+
virtual QString helpString() const;
89+
%Docstring
90+
Returns a localised help string for the algorithm. Algorithm subclasses should implement either
91+
helpString() or helpUrl().
92+
.. seealso:: helpUrl()
93+
.. seealso:: shortHelpString()
94+
:rtype: str
95+
%End
96+
97+
virtual QString helpUrl() const;
98+
%Docstring
99+
Returns a url pointing to the algorithm's help page.
100+
.. seealso:: helpString()
101+
.. seealso:: shortHelpString()
102+
:rtype: str
103+
%End
104+
79105
virtual QIcon icon() const;
80106
%Docstring
81107
Returns an icon for the algorithm.

‎python/plugins/processing/algs/gdal/GdalAlgorithm.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def processAlgorithm(self, context, feedback):
7777
commands[i] = c
7878
GdalUtils.runGdal(commands, feedback)
7979

80-
def shortHelp(self):
80+
def shortHelpString(self):
8181
helpPath = GdalUtils.gdalHelpPath()
8282
if helpPath == '':
8383
return
@@ -87,9 +87,9 @@ def shortHelp(self):
8787
else:
8888
url = helpPath + '{}.html'.format(self.commandName())
8989

90-
return self._formatHelp('''This algorithm is based on the GDAL {} module.
90+
return '''This algorithm is based on the GDAL {} module.
9191
For more info, see the <a href={}> module help</a>
92-
'''.format(self.commandName(), url))
92+
'''.format(self.commandName(), url)
9393

9494
def commandName(self):
9595
for output in self.outputs:

‎python/plugins/processing/algs/grass7/Grass7Algorithm.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,15 @@ def icon(self):
112112
def svgIconPath(self):
113113
return QgsApplication.iconPath("providerGrass.svg")
114114

115-
def help(self):
115+
def helpUrl(self):
116116
helpPath = Grass7Utils.grassHelpPath()
117117
if helpPath == '':
118-
return False, None
118+
return None
119119

120120
if os.path.exists(helpPath):
121-
return False, QUrl.fromLocalFile(os.path.join(helpPath, '{}.html'.format(self.grass7Name))).toString()
121+
return QUrl.fromLocalFile(os.path.join(helpPath, '{}.html'.format(self.grass7Name))).toString()
122122
else:
123-
return False, helpPath + '{}.html'.format(self.grass7Name)
123+
return helpPath + '{}.html'.format(self.grass7Name)
124124

125125
def getParameterDescriptions(self):
126126
descs = {}

‎python/plugins/processing/core/GeoAlgorithm.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,8 @@ def __init__(self):
7676

7777
# methods to overwrite when creating a custom geoalgorithm
7878

79-
def _formatHelp(self, text):
80-
return "<h2>%s</h2>%s" % (self.displayName(), "".join(["<p>%s</p>" % s for s in text.split("\n")]))
81-
82-
def help(self):
83-
return False, None
84-
85-
def shortHelp(self):
86-
text = shortHelp.get(self.id(), None)
87-
if text is not None:
88-
text = self._formatHelp(text)
89-
return text
79+
def shortHelpString(self):
80+
return shortHelp.get(self.id(), None)
9081

9182
def processAlgorithm(self, context, feedback):
9283
"""Here goes the algorithm itself.

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def __init__(self, alg):
104104
# if desktop.physicalDpiX() > 96:
105105
# self.txtHelp.setZoomFactor(desktop.physicalDpiX() / 96)
106106

107-
algHelp = self.alg.shortHelp()
107+
algHelp = self.formatHelp(self.alg)
108108
if algHelp is None:
109109
self.textShortHelp.setVisible(False)
110110
else:
@@ -123,18 +123,18 @@ def linkClicked(url):
123123

124124
self.textShortHelp.anchorClicked.connect(linkClicked)
125125

126-
isText, algHelp = self.alg.help()
127-
if algHelp is not None:
128-
algHelp = algHelp if isText else QUrl(algHelp)
126+
if self.alg.helpString() is not None:
129127
try:
130-
if isText:
131-
self.txtHelp.setHtml(algHelp)
132-
else:
133-
html = self.tr('<p>Downloading algorithm help... Please wait.</p>')
134-
self.txtHelp.setHtml(html)
135-
rq = QNetworkRequest(algHelp)
136-
self.reply = QgsNetworkAccessManager.instance().get(rq)
137-
self.reply.finished.connect(self.requestFinished)
128+
self.txtHelp.setHtml(self.alg.helpString())
129+
except Exception:
130+
self.tabWidget.removeTab(2)
131+
elif self.alg.helpUrl() is not None:
132+
try:
133+
html = self.tr('<p>Downloading algorithm help... Please wait.</p>')
134+
self.txtHelp.setHtml(html)
135+
rq = QNetworkRequest(QUrl(self.alg.helpUrl()))
136+
self.reply = QgsNetworkAccessManager.instance().get(rq)
137+
self.reply.finished.connect(self.requestFinished)
138138
except Exception:
139139
self.tabWidget.removeTab(2)
140140
else:
@@ -143,6 +143,12 @@ def linkClicked(url):
143143
self.showDebug = ProcessingConfig.getSetting(
144144
ProcessingConfig.SHOW_DEBUG_IN_DIALOG)
145145

146+
def formatHelp(self, alg):
147+
text = alg.shortHelpString()
148+
if not text:
149+
return None
150+
return "<h2>%s</h2>%s" % (alg.displayName(), "".join(["<p>%s</p>" % s for s in text.split("\n")]))
151+
146152
def requestFinished(self):
147153
"""Change the webview HTML content"""
148154
reply = self.sender()

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -539,15 +539,15 @@ def updateModelerView(self):
539539
if self.modelerdialog:
540540
self.modelerdialog.repaintModel()
541541

542-
def help(self):
542+
def helpString(self):
543543
try:
544-
return True, getHtmlFromDescriptionsDict(self, self.helpContent)
544+
return getHtmlFromDescriptionsDict(self, self.helpContent)
545545
except:
546-
return False, None
546+
return None
547547

548-
def shortHelp(self):
548+
def shortHelpString(self):
549549
if 'ALG_DESC' in self.helpContent:
550-
return self._formatHelp(str(self.helpContent['ALG_DESC']))
550+
return str(self.helpContent['ALG_DESC'])
551551
return None
552552

553553
def getParameterDescriptions(self):

‎python/plugins/processing/script/ScriptAlgorithm.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,16 +198,16 @@ def processAlgorithm(self, context, feedback):
198198
for out in self.outputs:
199199
out.setValue(ns[out.name])
200200

201-
def help(self):
201+
def helpString(self):
202202
if self.descriptionFile is None:
203203
return False, None
204204
helpfile = self.descriptionFile + '.help'
205205
if os.path.exists(helpfile):
206-
return True, getHtmlFromHelpFile(self, helpfile)
206+
return getHtmlFromHelpFile(self, helpfile)
207207
else:
208-
return False, None
208+
return None
209209

210-
def shortHelp(self):
210+
def shortHelpString(self):
211211
if self.descriptionFile is None:
212212
return None
213213
helpFile = str(self.descriptionFile) + '.help'
@@ -216,7 +216,7 @@ def shortHelp(self):
216216
try:
217217
descriptions = json.load(f)
218218
if 'ALG_DESC' in descriptions:
219-
return self._formatHelp(str(descriptions['ALG_DESC']))
219+
return str(descriptions['ALG_DESC'])
220220
except:
221221
return None
222222
return None

‎src/core/processing/qgsnativealgorithms.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ QgsCentroidAlgorithm::QgsCentroidAlgorithm()
7070
addOutput( new QgsProcessingOutputVectorLayer( QStringLiteral( "OUTPUT_LAYER" ), QObject::tr( "Centroids" ), QgsProcessingParameterDefinition::TypeVectorPoint ) );
7171
}
7272

73+
QString QgsCentroidAlgorithm::shortHelpString() const
74+
{
75+
return QObject::tr( "This algorithm creates a new point layer, with points representing the centroid of the geometries in an input layer.\n\n"
76+
"The attributes associated to each point in the output layer are the same ones associated to the original features." );
77+
}
78+
7379
QVariantMap QgsCentroidAlgorithm::run( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const
7480
{
7581
QgsVectorLayer *layer = qobject_cast< QgsVectorLayer *>( parameterAsLayer( parameters, QStringLiteral( "INPUT" ), context ) );
@@ -129,6 +135,15 @@ QgsBufferAlgorithm::QgsBufferAlgorithm()
129135
addOutput( new QgsProcessingOutputVectorLayer( QStringLiteral( "OUTPUT_LAYER" ), QObject::tr( "Buffered" ), QgsProcessingParameterDefinition::TypeVectorPoint ) );
130136
}
131137

138+
QString QgsBufferAlgorithm::shortHelpString() const
139+
{
140+
return QObject::tr( "This algorithm computes a buffer area for all the features in an input layer, using a fixed or dynamic distance.\n\n"
141+
"The segments parameter controls the number of line segments to use to approximate a quarter circle when creating rounded offsets.\n\n"
142+
"The end cap style parameter controls how line endings are handled in the buffer.\n\n"
143+
"The join style parameter specifies whether round, mitre or beveled joins should be used when offsetting corners in a line.\n\n"
144+
"The mitre limit parameter is only applicable for mitre join styles, and controls the maximum distance from the offset curve to use when creating a mitred join." );
145+
}
146+
132147
QVariantMap QgsBufferAlgorithm::run( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const
133148
{
134149
QgsVectorLayer *layer = qobject_cast< QgsVectorLayer *>( parameterAsLayer( parameters, QStringLiteral( "INPUT" ), context ) );

‎src/core/processing/qgsnativealgorithms.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class QgsCentroidAlgorithm : public QgsProcessingAlgorithm
5757
QString displayName() const override { return QObject::tr( "Centroids" ); }
5858
virtual QStringList tags() const override { return QObject::tr( "centroid,center,average,point,middle" ).split( ',' ); }
5959
QString group() const override { return QObject::tr( "Vector geometry tools" ); }
60+
QString shortHelpString() const override;
6061

6162
virtual QVariantMap run( const QVariantMap &parameters,
6263
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const override;
@@ -77,6 +78,7 @@ class QgsBufferAlgorithm : public QgsProcessingAlgorithm
7778
QString displayName() const override { return QObject::tr( "Buffer" ); }
7879
virtual QStringList tags() const override { return QObject::tr( "buffer,grow" ).split( ',' ); }
7980
QString group() const override { return QObject::tr( "Vector geometry tools" ); }
81+
QString shortHelpString() const override;
8082

8183
virtual QVariantMap run( const QVariantMap &parameters,
8284
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const override;

‎src/core/processing/qgsprocessingalgorithm.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ QString QgsProcessingAlgorithm::id() const
3636
return name();
3737
}
3838

39+
QString QgsProcessingAlgorithm::shortHelpString() const
40+
{
41+
return QString();
42+
}
43+
44+
QString QgsProcessingAlgorithm::helpString() const
45+
{
46+
return QString();
47+
}
48+
49+
QString QgsProcessingAlgorithm::helpUrl() const
50+
{
51+
return QString();
52+
}
53+
3954
QIcon QgsProcessingAlgorithm::icon() const
4055
{
4156
return QgsApplication::getThemeIcon( "/processingAlgorithm.svg" );

‎src/core/processing/qgsprocessingalgorithm.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,29 @@ class CORE_EXPORT QgsProcessingAlgorithm
9595
*/
9696
virtual QStringList tags() const { return QStringList(); }
9797

98+
/**
99+
* Returns a localised short helper string for the algorithm. This string should provide a basic description
100+
* about what the algorithm does and the parameters and outputs associated with it.
101+
* \see helpString()
102+
* \see helpUrl()
103+
*/
104+
virtual QString shortHelpString() const;
105+
106+
/**
107+
* Returns a localised help string for the algorithm. Algorithm subclasses should implement either
108+
* helpString() or helpUrl().
109+
* \see helpUrl()
110+
* \see shortHelpString()
111+
*/
112+
virtual QString helpString() const;
113+
114+
/**
115+
* Returns a url pointing to the algorithm's help page.
116+
* \see helpString()
117+
* \see shortHelpString()
118+
*/
119+
virtual QString helpUrl() const;
120+
98121
/**
99122
* Returns an icon for the algorithm.
100123
* \see svgIconPath()

0 commit comments

Comments
 (0)
Please sign in to comment.