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