Merge branch 'horizontals'
[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 # this two are only to satisfy some plugins requirements
35 import gui.plugin
36 import shutil
37
38 #import viewers.raw
39 #import viewers.simple
40 #import viewers.rfx
41 #import viewers.flexpaper
42 #import viewers.technoargia
43
44 class Viewers:
45     def __init__(self):
46         #self.viewers = [
47         #    'raw',
48         #    'simple',
49         #    'rfx',
50         #    'flexpaper',
51         #    'technoargia',
52         #]
53         self.viewers = []
54         #self.modules = [
55         #    viewers.raw,
56         #    viewers.simple,
57         #    viewers.rfx,
58         #    viewers.flexpaper,
59         #    viewers.technoargia,
60         #]
61         self.modules = []
62  
63         self.list_viewers()
64         self.import_viewers()
65
66     def list_viewers(self):
67         for file in os.listdir('viewers'):
68             if (file.startswith('.') or file.startswith('_')
69                 or file.endswith(".pyc") or not file.endswith('.py')):
70                 continue
71             self.viewers.append(os.path.splitext(file)[0])
72
73     def import_viewers(self):
74         for file in self.viewers:
75             try:
76                 _temp = imp.load_source("viewers.%s" % file, os.path.join(os.getcwdu(), "viewers/%s.py" % file))
77                 self.modules.append(_temp)
78             except Exception, e:
79                 print "Could not load %s (%s)" % (file, e)
80
81
82
83 class ViewerBook(wx.Listbook):
84     def __init__(self, parent):
85         wx.Listbook.__init__(self, parent, wx.ID_ANY, style=
86                             wx.LB_DEFAULT
87                             )
88
89         self.__mod = []
90         self.__viewers = viewers = Viewers()
91
92         # make an image list
93         il = wx.ImageList(102, 102)
94         self.AssignImageList(il)
95
96         # Sort viewers by attribute order.
97         # The attribute order must be an int from 0..n
98         viewers.modules.sort(key=operator.attrgetter('order'))
99
100         # Make a bunch of panels for the list book
101         for idx, mod in enumerate(viewers.modules):
102             bmp = mod.preview.GetBitmap()
103             il.Add(bmp)
104
105             win = self.makePanel(mod)
106             self.AddPage(win, mod, imageId=idx)
107             if hasattr(mod, "default"):
108                 self.SetSelection(idx)
109
110
111     def makePanel(self, mod):
112         p = wx.Panel(self)
113         sizer = wx.BoxSizer(wx.VERTICAL)
114
115         nameCtrl = BoldStaticText(p, label=mod.name)
116         sizer.Add(nameCtrl, 0, wx.EXPAND|wx.TOP|wx.BOTTOM, 10)
117
118         t = wordwrap(mod.desc, 400, wx.ClientDC(p))
119         descCtrl = wx.StaticText(p, label=t)
120         sizer.Add(descCtrl, 0, wx.EXPAND|wx.TOP|wx.BOTTOM, 10)
121
122         #swf_options = []
123
124         # Avoid showing an empty StaticBox
125         if (len(mod.swf_options) == 1
126                and isinstance(mod.swf_options[0], gui.fields.Hidden)
127            ):
128             hidden = True
129         else:
130             hidden = False
131
132         if mod.swf_options and not hidden:
133             box = wx.StaticBox(p, label=u"SWF")
134             bsizer = wx.StaticBoxSizer(box, wx.VERTICAL)
135
136             flex = wx.FlexGridSizer(rows=len(mod.swf_options), cols=2, hgap=0, vgap=0)
137             flex.AddGrowableCol(1)
138
139             for option in mod.swf_options:
140                 #swf_options.append(option)
141                 label, ctrl = option.draw(p)
142                 if label and ctrl:
143                     flex.Add(label, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 4)
144                     flex.Add(ctrl, 0, wx.EXPAND|wx.ALL, 4)
145
146             bsizer.Add(flex, 0, wx.EXPAND)
147             sizer.Add(bsizer, 0, wx.EXPAND)
148
149         #vie_options = []
150         if mod.viewer_options:
151             box = wx.StaticBox(p, label=u"Viewer")
152             bsizer = wx.StaticBoxSizer(box, wx.VERTICAL)
153
154             flex = wx.FlexGridSizer(rows=len(mod.viewer_options),
155                                     cols=2, hgap=0, vgap=0)
156             flex.AddGrowableCol(1)
157
158             for option in mod.viewer_options:
159                 #vie_options.append(option)
160                 label, ctrl = option.draw(p)
161                 if label and ctrl:
162                     flex.Add(label, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 4)
163                     flex.Add(ctrl, 0, wx.EXPAND|wx.ALL, 4)
164
165             bsizer.Add(flex, 0, wx.EXPAND)
166             sizer.Add(bsizer, 0, wx.EXPAND)
167
168         p.SetSizer(sizer)
169         return p
170
171     def AddPage(self, win, mod, select=False, imageId=-1):
172         wx.Listbook.AddPage(self, win, mod.name, select, imageId)
173         self.__mod.append(mod)
174
175     def __get_options(self):
176         page = self.GetSelection()
177         return self.__mod[page]
178
179     options = property(__get_options)
180
181     def pickle(self):
182         data = {}
183         data['selected_viewer'] = self.GetSelection()
184
185         for viewer, module in zip(self.__viewers.viewers, self.__viewers.modules):
186             data[viewer] = {}
187             for opt in module.swf_options:
188                 data[viewer][opt.name] = opt.value
189             for opt in module.viewer_options:
190                 data[viewer][opt.name] = opt.value
191
192         return data
193
194     def unpickle(self, data):
195         if not data:
196             return
197
198         selected_viewer = data.pop('selected_viewer')
199         self.SetSelection(selected_viewer)
200
201         _fields = {}
202         for viewer, module in zip(self.__viewers.viewers, self.__viewers.modules):
203             _fields[viewer] = {}
204
205             all_opts = module.swf_options + module.viewer_options
206             for field in all_opts:
207                 _fields[viewer][field.name] = field
208
209         for modname, opts in data.items():
210             for k, v in opts.items():
211                 _fields[modname][k].SetValue(v)
212