Skip to content

Commit

Permalink
[processing] open help in the default webbrowser to be consistent with
Browse files Browse the repository at this point in the history
the rest of QGIS dialogs
  • Loading branch information
alexbruy committed Jun 6, 2017
1 parent 77fa177 commit a137a7c
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 44 deletions.
7 changes: 6 additions & 1 deletion python/plugins/processing/ProcessingPlugin.py
Expand Up @@ -44,7 +44,7 @@
from processing.gui.ResultsDock import ResultsDock
from processing.gui.AlgorithmLocatorFilter import AlgorithmLocatorFilter
from processing.modeler.ModelerDialog import ModelerDialog
from processing.tools.system import tempFolder
from processing.tools.system import tempFolder, tempHelpFolder
from processing.gui.menus import removeMenus, initializeMenus, createMenus
from processing.core.ProcessingResults import resultsList

Expand Down Expand Up @@ -146,6 +146,11 @@ def unload(self):
if QDir(folder).exists():
shutil.rmtree(folder, True)

# also delete temporary help files
folder = tempHelpFolder()
if QDir(folder).exists():
shutil.rmtree(folder, True)

self.iface.unregisterMainWindowAction(self.toolboxAction)
self.iface.unregisterMainWindowAction(self.modelerAction)
self.iface.unregisterMainWindowAction(self.historyAction)
Expand Down
12 changes: 4 additions & 8 deletions python/plugins/processing/algs/gdal/GdalAlgorithm.py
Expand Up @@ -80,19 +80,15 @@ def processAlgorithm(self, parameters, context, feedback):
commands[i] = c
GdalUtils.runGdal(commands, feedback)

def shortHelpString(self):
def helpUrl(self):
helpPath = GdalUtils.gdalHelpPath()
if helpPath == '':
return
return None

if os.path.exists(helpPath):
url = QUrl.fromLocalFile(os.path.join(helpPath, '{}.html'.format(self.commandName()))).toString()
return QUrl.fromLocalFile(os.path.join(helpPath, '{}.html'.format(self.commandName()))).toString()
else:
url = helpPath + '{}.html'.format(self.commandName())

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

def commandName(self):
parameters = {}
Expand Down
3 changes: 3 additions & 0 deletions python/plugins/processing/core/GeoAlgorithm.py
Expand Up @@ -390,3 +390,6 @@ def executeAlgorithm(alg, parameters, context=None, feedback=None, model=None):
# lines.append(traceback.format_exc())
#QgsMessageLog.logMessage('\n'.join(lines), self.tr('Processing'), QgsMessageLog.CRITICAL)
#raise GeoAlgorithmExecutionException(str(e) + self.tr('\nSee log for more details'), lines, e)

def helpUrl(self):
return None
37 changes: 7 additions & 30 deletions python/plugins/processing/gui/AlgorithmDialogBase.py
Expand Up @@ -32,11 +32,9 @@
from qgis.PyQt import uic
from qgis.PyQt.QtCore import QCoreApplication, QByteArray, QUrl
from qgis.PyQt.QtWidgets import QApplication, QDialogButtonBox
from qgis.PyQt.QtNetwork import QNetworkRequest, QNetworkReply

from qgis.utils import iface
from qgis.core import (QgsNetworkAccessManager,
QgsProject,
from qgis.core import (QgsProject,
QgsProcessingFeedback,
QgsSettings)

Expand Down Expand Up @@ -103,6 +101,7 @@ def __init__(self, alg):
self.buttonCancel.setEnabled(False)

self.btnClose = self.buttonBox.button(QDialogButtonBox.Close)
self.buttonBox.helpRequested.connect(self.openHelp)

# don't collapse parameters panel
self.splitter.setCollapsible(0, False)
Expand All @@ -128,23 +127,6 @@ def linkClicked(url):

self.textShortHelp.anchorClicked.connect(linkClicked)

if self.alg.helpString() is not None:
try:
self.txtHelp.setHtml(self.alg.helpString())
except Exception:
self.tabWidget.removeTab(2)
elif self.alg.helpUrl() is not None:
try:
html = self.tr('<p>Downloading algorithm help... Please wait.</p>')
self.txtHelp.setHtml(html)
rq = QNetworkRequest(QUrl(self.alg.helpUrl()))
self.reply = QgsNetworkAccessManager.instance().get(rq)
self.reply.finished.connect(self.requestFinished)
except Exception:
self.tabWidget.removeTab(2)
else:
self.tabWidget.removeTab(2)

self.showDebug = ProcessingConfig.getSetting(
ProcessingConfig.SHOW_DEBUG_IN_DIALOG)

Expand All @@ -154,16 +136,6 @@ def formatHelp(self, alg):
return None
return "<h2>%s</h2>%s" % (alg.displayName(), "".join(["<p>%s</p>" % s for s in text.split("\n")]))

def requestFinished(self):
"""Change the webview HTML content"""
reply = self.sender()
if reply.error() != QNetworkReply.NoError:
html = self.tr('<h2>No help available for this algorithm</h2><p>{}</p>'.format(reply.errorString()))
else:
html = str(reply.readAll())
reply.deleteLater()
self.txtHelp.setHtml(html)

def closeEvent(self, event):
self._saveGeometry()
super(AlgorithmDialogBase, self).closeEvent(event)
Expand Down Expand Up @@ -237,6 +209,11 @@ def reject(self):
def finish(self, context):
pass

def openHelp(self):
algHelp = self.alg.helpUrl()
if algHelp is not None:
webbrowser.open(algHelp)

def _saveGeometry(self):
self.settings.setValue("/Processing/dialogBaseSplitter", self.splitter.saveState())
self.settings.setValue("/Processing/dialogBase", self.saveGeometry())
Expand Down
13 changes: 11 additions & 2 deletions python/plugins/processing/gui/Help2Html.py
Expand Up @@ -27,11 +27,14 @@

__revision__ = '$Format:%H$'

from qgis.PyQt.QtCore import QCoreApplication
import os
import re
import json

from qgis.PyQt.QtCore import QCoreApplication, QUrl

from processing.tools import system

ALG_DESC = 'ALG_DESC'
ALG_CREATOR = 'ALG_CREATOR'
ALG_HELP_CREATOR = 'ALG_HELP_CREATOR'
Expand Down Expand Up @@ -63,7 +66,13 @@ def getHtmlFromHelpFile(alg, helpFile):
try:
with open(helpFile) as f:
descriptions = json.load(f)
return getHtmlFromDescriptionsDict(alg, descriptions)

content = getHtmlFromDescriptionsDict(alg, descriptions)
algGroup, algName = alg.id().split(':')
filePath = os.path.join(system.tempHelpFolder(), "{}_{}.html".format(algGroup, algName))
with open(filePath, 'w', encoding='utf-8') as f:
f.write(content)
return QUrl.fromLocalFile(filePath).toString()
except:
return None

Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/modeler/ModelerAlgorithm.py
Expand Up @@ -539,7 +539,7 @@ def updateModelerView(self):
if self.modelerdialog:
self.modelerdialog.repaintModel()

def helpString(self):
def helpUrl(self):
try:
return getHtmlFromDescriptionsDict(self, self.helpContent)
except:
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/script/ScriptAlgorithm.py
Expand Up @@ -198,9 +198,9 @@ def processAlgorithm(self, parameters, context, feedback):
for out in self.outputs:
out.setValue(ns[out.name])

def helpString(self):
def helpUrl(self):
if self.descriptionFile is None:
return False, None
return None
helpfile = self.descriptionFile + '.help'
if os.path.exists(helpfile):
return getHtmlFromHelpFile(self, helpfile)
Expand Down
8 changes: 8 additions & 0 deletions python/plugins/processing/tools/system.py
Expand Up @@ -142,6 +142,14 @@ def mkdir(newdir):
os.mkdir(newdir)


def tempHelpFolder():
tmp = os.path.join(str(QDir.tempPath()), 'processing_help')
if not QDir(tmp).exists():
QDir().mkpath(tmp)

return str(os.path.abspath(tmp))


def escapeAndJoin(strList):
joined = ''
for s in strList:
Expand Down

0 comments on commit a137a7c

Please sign in to comment.