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