diff --git a/Code/Mantid/MantidPlot/mantidplot.py b/Code/Mantid/MantidPlot/mantidplot.py index 663ac35bd24fbbda6e939a589ad5af69b91def65..76099c59359bbdd67be61e82113de015ce68c6f2 100644 --- a/Code/Mantid/MantidPlot/mantidplot.py +++ b/Code/Mantid/MantidPlot/mantidplot.py @@ -867,7 +867,6 @@ class Screenshot(QtCore.QObject): thread """ # First save the screenshot - widget.show() widget.resize(widget.size()) QtCore.QCoreApplication.processEvents() diff --git a/Code/Mantid/docs/sphinxext/mantiddoc/directives/algorithm.py b/Code/Mantid/docs/sphinxext/mantiddoc/directives/algorithm.py index 7b05272a2aad1318485cffac163d4c701eeb2551..6f31ee6fe5156f98479743868c14d4f7c8cdcba8 100644 --- a/Code/Mantid/docs/sphinxext/mantiddoc/directives/algorithm.py +++ b/Code/Mantid/docs/sphinxext/mantiddoc/directives/algorithm.py @@ -9,6 +9,11 @@ REDIRECT_TEMPLATE = "redirect.html" DEPRECATE_USE_ALG_RE = re.compile(r'Use\s([A-Z][a-zA-Z0-9]+)\sinstead') +# Maximum height in pixels a screenshot image +# Any higher than this an an obvious gap starts to appear between the "Properties" header +# and the rest +SCREENSHOT_MAX_HEIGHT = 250 + #-------------------------------------------------------------------------- class AlgorithmDirective(BaseDirective): @@ -37,8 +42,8 @@ class AlgorithmDirective(BaseDirective): Called by Sphinx when the ..algorithm:: directive is encountered """ self._insert_pagetitle() - imgpath = self._create_screenshot() - self._insert_screenshot_link(imgpath) + picture = self._create_screenshot() + self._insert_screenshot_link(picture) self._insert_toc() self._insert_deprecation_warning() @@ -86,13 +91,12 @@ class AlgorithmDirective(BaseDirective): The file will be named "algorithmname-vX_dlg.png", e.g. Rebin-v1_dlg.png Returns: - str: The full path to the created image + screenshot: A mantiddoc.tools.Screenshot object """ - notfoundimage = "/images/ImageNotFound.png" try: screenshots_dir = self._screenshot_directory() except RuntimeError: - return notfoundimage + return None # Generate image from mantiddoc.tools.screenshot import algorithm_screenshot @@ -100,15 +104,15 @@ class AlgorithmDirective(BaseDirective): os.makedirs(screenshots_dir) try: - imgpath = algorithm_screenshot(self.algorithm_name(), screenshots_dir, version=self.algorithm_version()) - except Exception, exc: + picture = algorithm_screenshot(self.algorithm_name(), screenshots_dir, version=self.algorithm_version()) + except RuntimeError, exc: env = self.state.document.settings.env - env.warn(env.docname, "Unable to generate screenshot for '%s' - %s" % (algorithm_name, str(exc))) - imgpath = notfoundimage + env.warn(env.docname, "Unable to generate screenshot for '%s' - %s" % (self.algorithm_name(), str(exc))) + picture = None - return imgpath + return picture - def _insert_screenshot_link(self, img_path): + def _insert_screenshot_link(self, picture): """ Outputs an image link with a custom :class: style. The filename is extracted from the path given and then a relative link to the @@ -116,35 +120,42 @@ class AlgorithmDirective(BaseDirective): the root source directory is formed. Args: - img_path (str): The full path as on the filesystem to the image + picture (Screenshot): A Screenshot object """ env = self.state.document.settings.env format_str = ".. figure:: %s\n"\ - " :class: screenshot\n"\ - " :align: right\n"\ - " :width: 400px\n\n"\ - " %s\n\n" + " :class: screenshot\n"\ + " :width: %dpx\n"\ + " :align: right\n\n"\ + " %s\n\n" # Sphinx assumes that an absolute path is actually relative to the directory containing the # conf.py file and a relative path is relative to the directory where the current rst file # is located. + if picture: + filename = os.path.split(picture.imgpath)[1] + # Find the width of the image + width, height = picture.width, picture.height - filename = os.path.split(img_path)[1] - cfgdir = env.srcdir + if height > SCREENSHOT_MAX_HEIGHT: + aspect_ratio = float(width)/height + width = int(SCREENSHOT_MAX_HEIGHT*aspect_ratio) + else: + width = 200 try: screenshots_dir = self._screenshot_directory() - rel_path = os.path.relpath(screenshots_dir, cfgdir) + rel_path = os.path.relpath(screenshots_dir, env.srcdir) # This is a href link so is expected to be in unix style rel_path = rel_path.replace("\\","/") # stick a "/" as the first character so Sphinx computes relative location from source directory path = "/" + rel_path + "/" + filename except RuntimeError: # Use path as it is - path = img_path + path = "/images/ImageNotFound.png" - caption = "A screenshot of the **" + self.algorithm_name() + "** dialog." - self.add_rst(format_str % (path, caption)) + caption = "**" + self.algorithm_name() + "** dialog." + self.add_rst(format_str % (path, width, caption)) def _screenshot_directory(self): """ diff --git a/Code/Mantid/docs/sphinxext/mantiddoc/tools/screenshot.py b/Code/Mantid/docs/sphinxext/mantiddoc/tools/screenshot.py index 8d57701f19aa45687ae81adb7933cfcbd4a7cf18..06c480442988ac1f8346dd00b73b5bdb63fc7a70 100644 --- a/Code/Mantid/docs/sphinxext/mantiddoc/tools/screenshot.py +++ b/Code/Mantid/docs/sphinxext/mantiddoc/tools/screenshot.py @@ -10,6 +10,21 @@ :copyright: Copyright 2014 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory """ +#-------------------------------------------------------------------------- +class Screenshot(object): + """ + Takes a screenshot of widget records the filename + meta information + about it. + """ + + def __init__(self, widget, filename, directory): + from mantidplot import screenshot_to_dir, threadsafe_call + + self.imgpath = screenshot_to_dir(widget=widget, filename=filename, screenshot_dir=directory) + self.width = widget.width() + self.height = widget.height() + +#-------------------------------------------------------------------------- def algorithm_screenshot(name, directory, version = -1, ext = ".png"): """ @@ -30,7 +45,7 @@ def algorithm_screenshot(name, directory, version = -1, ext = ".png"): return "NoGUI-ImageNotGenerated.png" import mantidqtpython as mantidqt - from mantidplot import screenshot_to_dir, threadsafe_call + from mantidplot import threadsafe_call iface_mgr = mantidqt.MantidQt.API.InterfaceManager() # threadsafe_call required for MantidPlot @@ -39,7 +54,6 @@ def algorithm_screenshot(name, directory, version = -1, ext = ".png"): suffix = ("-v%d" % version) if version != -1 else "" filename = "%s%s_dlg%s" % (name, suffix, ext) - img_path = screenshot_to_dir(widget=dlg, filename=filename, screenshot_dir=directory) + picture = Screenshot(dlg, filename, directory) threadsafe_call(dlg.close) - - return img_path + return picture \ No newline at end of file