source: sasview/src/sas/sasgui/guiframe/report_image_handler.py @ aa9928e

magnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249
Last change on this file since aa9928e was d0ce666f, checked in by krzywon, 6 years ago

Remove image references when report is closed and point to correct file locations when saving reports to PDF.

  • Property mode set to 100644
File size: 2.8 KB
RevLine 
[0cf4f84]1import wx
[d0ce666f]2import logging
3
4logger = logging.getLogger(__name__)
[0cf4f84]5
6
7class ReportImageHandler:
[fa412df]8    # Singleton class that manages all report plot images
9    # To call the handler, call the static method set_figs
[0cf4f84]10
11    class _ReportImageHandler:
12
13        def __init__(self):
14            wx.FileSystem.AddHandler(wx.MemoryFSHandler())
[d0ce666f]15            self.img_holder = wx.MemoryFSHandler()
[0cf4f84]16            self.refs = {}
17            self.indices = []
18
19        def set_figs(self, figs, bitmaps, perspective):
[fa412df]20            """
21            Save figures and images to memory and return refernces
22            :param figs: A list of matplotlib Figures
23            :param bitmaps: A list of bitmaps
24            :param perspective: A String with the perspective name
25            :return: A tuple of a list of Figures and a list of memory refs
26            """
[0cf4f84]27            imgs = []
28            refs = []
29            if figs is None or len(figs) == 0:
30                figs = [None]
31            for fig in figs:
32                if fig is not None:
33                    ind = figs.index(fig)
34                    bitmap = bitmaps[ind]
35
36                # name of the fig
37                name = self.create_unique_name(perspective)
38                # AddFile, image can be retrieved with 'memory:filename'
39                ref = 'memory:' + name
40                self.refs[ref] = fig
41                self.img_holder.AddFile(name, bitmap, wx.BITMAP_TYPE_PNG)
42                refs.append(ref)
43                imgs.append(fig)
44            return imgs, refs
45
46        def create_unique_name(self, perspective, index=None):
[fa412df]47            """
48            Create a unique key for each item in memory
49            :param perspective: The perspective name as a string
50            :param index: The base index used for incrementing the name
51            :return: A unique file name not currently in use
52            """
[0cf4f84]53            if not index:
54                index = len(self.indices)
55            if index in self.indices:
56                name = self.create_unique_name(index + 1)
57            else:
58                self.indices.append(index)
59                name = 'img_{}_{:03d}.png'.format(str(perspective), index)
60            return name
61
62    instance = None
63
[fa412df]64    @staticmethod
[d0ce666f]65    def check_for_empty_instance():
66        if ReportImageHandler.instance is None:
67            ReportImageHandler.instance = \
68                ReportImageHandler._ReportImageHandler()
69
70    @staticmethod
[fa412df]71    def set_figs(figs, bitmaps, perspective):
[d0ce666f]72        ReportImageHandler.check_for_empty_instance()
[fa412df]73        return ReportImageHandler.instance.set_figs(figs, bitmaps, perspective)
[d0ce666f]74
75    @staticmethod
76    def remove_figure(fig_url):
77        try:
78            ReportImageHandler.check_for_empty_instance()
79            ReportImageHandler.instance.refs.pop(fig_url)
80        except Exception as e:
81            logger.warn(e)
Note: See TracBrowser for help on using the repository browser.