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

Last change on this file since 3ec78a1 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
Line 
1import wx
2import logging
3
4logger = logging.getLogger(__name__)
5
6
7class ReportImageHandler:
8    # Singleton class that manages all report plot images
9    # To call the handler, call the static method set_figs
10
11    class _ReportImageHandler:
12
13        def __init__(self):
14            wx.FileSystem.AddHandler(wx.MemoryFSHandler())
15            self.img_holder = wx.MemoryFSHandler()
16            self.refs = {}
17            self.indices = []
18
19        def set_figs(self, figs, bitmaps, perspective):
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            """
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):
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            """
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
64    @staticmethod
65    def check_for_empty_instance():
66        if ReportImageHandler.instance is None:
67            ReportImageHandler.instance = \
68                ReportImageHandler._ReportImageHandler()
69
70    @staticmethod
71    def set_figs(figs, bitmaps, perspective):
72        ReportImageHandler.check_for_empty_instance()
73        return ReportImageHandler.instance.set_figs(figs, bitmaps, perspective)
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.