applied static imports patch from Ricardo Pedroso
[swftools.git] / wx / gui / options / viewer.py
1 #!/usr/bin/env python
2 # -*- coding: UTF-8 -*-
3 #
4 # gpdf2swf.py
5 # graphical user interface for pdf2swf
6 #
7 # Part of the swftools package.
8
9 # Copyright (c) 2008,2009 Matthias Kramm <kramm@quiss.org> 
10 #
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
24
25 import os
26 import wx
27 import operator
28 #import imp
29 from lib.wordwrap import wordwrap
30 from gui.boldstatictext import BoldStaticText
31 import viewers
32 import gui.fields
33
34 import viewers.raw
35 import viewers.simple
36 import viewers.rfx
37 import viewers.flexpaper
38 import viewers.technoargia
39
40 class Viewers:
41     def __init__(self):
42         self.viewers = [
43             'raw',
44             'simple',
45             'rfx',
46             'flexpaper',
47             'technoargia',
48         ]
49         self.modules = [
50             viewers.raw,
51             viewers.simple,
52             viewers.rfx,
53             viewers.flexpaper,
54             viewers.technoargia,
55         ]
56  
57         #self.list_viewers()
58         #self.import_viewers()
59
60     #def list_viewers(self):
61     #    for file in os.listdir('viewers'):
62     #        if (file.startswith('.') or file.startswith('_')
63     #            or file.endswith(".pyc") or not file.endswith('.py')):
64     #            continue
65     #        self.viewers.append(os.path.splitext(file)[0])
66
67     #def import_viewers(self):
68     #    for file in self.viewers:
69     #        try:
70     #            _temp = imp.load_source("viewers.%s" % file, os.path.join(os.getcwdu(), "viewers/%s.py" % file))
71     #            self.modules.append(_temp)
72     #        except:
73     #            print "Could not load %s" % file
74
75
76
77 class ViewerBook(wx.Listbook):
78     def __init__(self, parent):
79         wx.Listbook.__init__(self, parent, wx.ID_ANY, style=
80                             wx.LB_DEFAULT
81                             )
82
83         self.__mod = []
84         self.__viewers = viewers = Viewers()
85
86         # make an image list
87         il = wx.ImageList(102, 102)
88         self.AssignImageList(il)
89
90         # Sort viewers by attribute order.
91         # The attribute order must be an int from 0..n
92         viewers.modules.sort(key=operator.attrgetter('order'))
93
94         # Make a bunch of panels for the list book
95         for idx, mod in enumerate(viewers.modules):
96             bmp = mod.preview.GetBitmap()
97             il.Add(bmp)
98
99             win = self.makePanel(mod)
100             self.AddPage(win, mod, imageId=idx)
101
102
103     def makePanel(self, mod):
104         p = wx.Panel(self)
105         sizer = wx.BoxSizer(wx.VERTICAL)
106
107         nameCtrl = BoldStaticText(p, label=mod.name)
108         sizer.Add(nameCtrl, 0, wx.EXPAND|wx.TOP|wx.BOTTOM, 10)
109
110         t = wordwrap(mod.desc, 400, wx.ClientDC(p))
111         descCtrl = wx.StaticText(p, label=t)
112         sizer.Add(descCtrl, 0, wx.EXPAND|wx.TOP|wx.BOTTOM, 10)
113
114         #swf_options = []
115
116         # Avoid showing an empty StaticBox
117         if (len(mod.swf_options) == 1
118                and isinstance(mod.swf_options[0], gui.fields.Hidden)
119            ):
120             hidden = True
121         else:
122             hidden = False
123
124         if mod.swf_options and not hidden:
125             box = wx.StaticBox(p, label=u"SWF")
126             bsizer = wx.StaticBoxSizer(box, wx.VERTICAL)
127
128             flex = wx.FlexGridSizer(rows=len(mod.swf_options), cols=2, hgap=0, vgap=0)
129             flex.AddGrowableCol(1)
130
131             for option in mod.swf_options:
132                 #swf_options.append(option)
133                 label, ctrl = option.draw(p)
134                 if label and ctrl:
135                     flex.Add(label, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 4)
136                     flex.Add(ctrl, 0, wx.EXPAND|wx.ALL, 4)
137
138             bsizer.Add(flex, 0, wx.EXPAND)
139             sizer.Add(bsizer, 0, wx.EXPAND)
140
141         #vie_options = []
142         if mod.viewer_options:
143             box = wx.StaticBox(p, label=u"Viewer")
144             bsizer = wx.StaticBoxSizer(box, wx.VERTICAL)
145
146             flex = wx.FlexGridSizer(rows=len(mod.viewer_options),
147                                     cols=2, hgap=0, vgap=0)
148             flex.AddGrowableCol(1)
149
150             for option in mod.viewer_options:
151                 #vie_options.append(option)
152                 label, ctrl = option.draw(p)
153                 if label and ctrl:
154                     flex.Add(label, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 4)
155                     flex.Add(ctrl, 0, wx.EXPAND|wx.ALL, 4)
156
157             bsizer.Add(flex, 0, wx.EXPAND)
158             sizer.Add(bsizer, 0, wx.EXPAND)
159
160         p.SetSizer(sizer)
161         return p
162
163     def AddPage(self, win, mod, select=False, imageId=-1):
164         wx.Listbook.AddPage(self, win, mod.name, select, imageId)
165         self.__mod.append(mod)
166
167     def __get_options(self):
168         page = self.GetSelection()
169         return self.__mod[page]
170
171     options = property(__get_options)
172
173     def pickle(self):
174         data = {}
175         data['selected_viewer'] = self.GetSelection()
176
177         for viewer, module in zip(self.__viewers.viewers, self.__viewers.modules):
178             data[viewer] = {}
179             for opt in module.swf_options:
180                 data[viewer][opt.name] = opt.value
181             for opt in module.viewer_options:
182                 data[viewer][opt.name] = opt.value
183
184         return data
185
186     def unpickle(self, data):
187         if not data:
188             return
189
190         selected_viewer = data.pop('selected_viewer')
191         self.SetSelection(selected_viewer)
192
193         _fields = {}
194         for viewer, module in zip(self.__viewers.viewers, self.__viewers.modules):
195             _fields[viewer] = {}
196
197             all_opts = module.swf_options + module.viewer_options
198             for field in all_opts:
199                 _fields[viewer][field.name] = field
200
201         for modname, opts in data.items():
202             for k, v in opts.items():
203                 _fields[modname][k].SetValue(v)
204