]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/imagebrowser.py
Forward port recent changes on the 2.8 branch to HEAD
[wxWidgets.git] / wxPython / wx / lib / imagebrowser.py
1 #----------------------------------------------------------------------------
2 # Name: BrowseImage.py
3 # Purpose: Display and Select Image Files
4 #
5 # Author: Lorne White
6 #
7 # Version: 1.0
8 # Date: January 29, 2002
9 # Licence: wxWindows license
10 #----------------------------------------------------------------------------
11 # 1.0 Release
12 # Create list of all available image file types
13 # View "All Image" File Types as default filter
14 # Sort the file list
15 # Use newer "re" function for patterns
16 #
17 #----------------------------------------------------------------------------
18 #
19 # 12/08/2003 - Jeff Grimmett (grimmtooth@softhome.net)
20 #
21 # o Updated for wx namespace
22 # o Corrected a nasty bug or two - see comments below.
23 # o There was a duplicate ImageView.DrawImage() method. Que?
24 #
25
26 #---------------------------------------------------------------------------
27
28 import os
29 import sys
30
31 import wx
32
33 dir_path = os.getcwd()
34
35 #---------------------------------------------------------------------------
36
37 def ConvertBMP(file_nm):
38 if file_nm is None:
39 return None
40
41 fl_fld = os.path.splitext(file_nm)
42 ext = fl_fld[1]
43 ext = ext[1:].lower()
44
45 image = wx.Image(file_nm, wx.BITMAP_TYPE_ANY)
46 return image
47
48
49 def GetSize(file_nm): # for scaling image values
50 image = ConvertBMP(file_nm)
51 bmp = image.ConvertToBitmap()
52 size = bmp.GetWidth(), bmp.GetHeight()
53 return size
54
55
56 class ImageView(wx.Window):
57 def __init__(self, parent, id=-1, pos=wx.DefaultPosition, size=wx.DefaultSize):
58 wx.Window.__init__(self, parent, id, pos, size)
59 self.win = parent
60 self.image = None
61 self.back_color = 'WHITE'
62 self.border_color = 'BLACK'
63
64 # Changed API of wx uses tuples for size and pos now.
65 self.image_sizex = size[0]
66 self.image_sizey = size[1]
67 self.image_posx = pos[0]
68 self.image_posy = pos[1]
69 self.Bind(wx.EVT_PAINT, self.OnPaint)
70
71 def OnPaint(self, event):
72 dc = wx.PaintDC(self)
73 self.DrawImage(dc)
74
75
76 def SetValue(self, file_nm): # display the selected file in the panel
77 image = ConvertBMP(file_nm)
78 self.image = image
79 self.Refresh()
80
81 def DrawBorder(self, dc):
82 brush = wx.Brush(wx.NamedColour(self.back_color), wx.SOLID)
83 dc.SetBrush(brush)
84 dc.SetPen(wx.Pen(wx.NamedColour(self.border_color), 1))
85 dc.DrawRectangle(0, 0, self.image_sizex, self.image_sizey)
86
87 def DrawImage(self, dc):
88 try:
89 image = self.image
90 except:
91 return
92
93 self.DrawBorder(dc)
94
95 if image is None:
96 return
97
98 try:
99 bmp = image.ConvertToBitmap()
100 except:
101 return
102
103 iwidth = bmp.GetWidth() # dimensions of image file
104 iheight = bmp.GetHeight()
105
106 diffx = (self.image_sizex - iwidth)/2 # center calc
107 if iwidth >= self.image_sizex -10: # if image width fits in window adjust
108 diffx = 5
109 iwidth = self.image_sizex - 10
110
111 diffy = (self.image_sizey - iheight)/2 # center calc
112 if iheight >= self.image_sizey - 10: # if image height fits in window adjust
113 diffy = 5
114 iheight = self.image_sizey - 10
115
116 image.Rescale(iwidth, iheight) # rescale to fit the window
117 image.ConvertToBitmap()
118 bmp = image.ConvertToBitmap()
119 dc.DrawBitmap(bmp, diffx, diffy) # draw the image to window
120
121
122 class ImageDialog(wx.Dialog):
123 def __init__(self, parent, set_dir = None):
124 wx.Dialog.__init__(self, parent, -1, "Image Browser", wx.DefaultPosition, (400, 400))
125
126 self.x_pos = 30 # initial display positions
127 self.y_pos = 20
128 self.delta = 20
129
130 size = wx.Size(80, -1)
131
132 self.set_dir = os.getcwd()
133 self.set_file = None
134
135 if set_dir != None:
136 if os.path.exists(set_dir): # set to working directory if nothing set
137 self.set_dir = set_dir
138
139 self.dir_x = self.x_pos
140 self.dir_y = self.y_pos
141 self.dir = wx.StaticText(self, -1, self.set_dir, (self.dir_x, self.dir_y), (250, -1))
142
143 self.y_pos = self.y_pos + self.delta
144
145 btn = wx.Button(self, 12331, ' Set Directory ', (self.x_pos, self.y_pos))
146 self.Bind(wx.EVT_BUTTON, self.SetDirect, btn)
147
148 self.type_posy = self.y_pos # save the y position for the image type combo
149
150 self.fl_ext = '*.bmp' # initial setting for file filtering
151 self.GetFiles() # get the file list
152
153 self.y_pos = self.y_pos + self.delta + 10
154 self.list_height = 150
155
156 # List of Labels
157 self.tb = tb = wx.ListBox(self, -1, (self.x_pos, self.y_pos),
158 (160, self.list_height), self.fl_list,
159 wx.LB_SINGLE )
160 self.Bind(wx.EVT_LISTBOX, self.OnListClick, tb)
161 self.Bind(wx.EVT_LISTBOX_DCLICK, self.OnListDClick, tb)
162
163 width, height = self.tb.GetSize()
164 image_posx = self.x_pos + width + 20 # positions for setting the image window
165 image_posy = self.y_pos
166 image_sizex = 150
167 image_sizey = self.list_height
168
169 self.fl_types = [
170 "All Images", "Bmp", "Gif", "Png", "Jpg", "Ico", "Pnm",
171 "Pcx", "Tif", "All Files"
172 ]
173
174 self.fl_ext_types = {
175 "All Images": "All",
176 "Bmp": "*.bmp",
177 "Gif": "*.gif",
178 "Png": "*.png",
179 "Jpg": "*.jpg",
180 "Ico": "*.ico",
181 "Pnm": "*.pnm",
182 "Pcx": "*.pcx",
183 "Tif": "*.tif",
184 "All Files": "*.*"
185 }
186
187 self.set_type = self.fl_types[0] # initial file filter setting
188 self.fl_ext = self.fl_ext_types[self.set_type]
189
190 self.sel_type = wx.ComboBox(self, -1, self.set_type, (image_posx , self.type_posy),
191 (150, -1), self.fl_types, wx.CB_DROPDOWN)
192 self.Bind(wx.EVT_COMBOBOX, self.OnSetType, self.sel_type)
193
194 self.image_view = ImageView( self, pos=(image_posx, image_posy),
195 size=(image_sizex, image_sizey))
196
197 self.y_pos = self.y_pos + height + 20
198
199 btn = wx.Button(self, -1, ' Select ', (100, self.y_pos), size)
200 self.Bind(wx.EVT_BUTTON, self.OnOk, btn)
201
202 wx.Button(self, wx.ID_CANCEL, 'Cancel', (250, self.y_pos), size)
203
204 self.y_pos = self.y_pos + self.delta
205 fsize = (400, self.y_pos + 50) # resize dialog for final vertical position
206 self.SetSize(fsize)
207
208 self.ResetFiles()
209
210 def GetFiles(self): # get the file list using directory and extension values
211 if self.fl_ext == "All":
212 all_files = []
213
214 for ftypes in self.fl_types[1:-1]: # get list of all available image types
215 filter = self.fl_ext_types[ftypes]
216 #print "filter = ", filter
217 self.fl_val = FindFiles(self, self.set_dir, filter)
218 all_files = all_files + self.fl_val.files # add to list of files
219
220 self.fl_list = all_files
221 else:
222 self.fl_val = FindFiles(self, self.set_dir, self.fl_ext)
223 self.fl_list = self.fl_val.files
224
225 self.fl_list.sort() # sort the file list
226
227 def DisplayDir(self): # display the working directory
228 if self.dir:
229 self.dir.SetLabel(self.set_dir)
230
231 def OnSetType(self, event):
232 val = event.GetString() # get file type value
233 self.fl_ext = self.fl_ext_types[val]
234 self.ResetFiles()
235
236 def OnListDClick(self, event):
237 self.OnOk(0)
238
239 def OnListClick(self, event):
240 val = event.GetSelection()
241 self.SetListValue(val)
242
243 def SetListValue(self, val):
244 file_nm = self.fl_list[val]
245 self.set_file = file_val = os.path.join(self.set_dir, file_nm)
246 self.image_view.SetValue(file_val)
247
248 def SetDirect(self, event): # set the new directory
249 dlg = wx.DirDialog(self)
250 dlg.SetPath(self.set_dir)
251
252 if dlg.ShowModal() == wx.ID_OK:
253 self.set_dir = dlg.GetPath()
254 self.ResetFiles()
255
256 dlg.Destroy()
257
258 def ResetFiles(self): # refresh the display with files and initial image
259 self.DisplayDir()
260 self.GetFiles()
261
262 # Changed 12/8/03 jmg
263 #
264 # o Clear listbox first
265 # o THEN check to see if there are any valid files of the selected
266 # type,
267 # o THEN if we have any files to display, set the listbox up,
268 #
269 # OTHERWISE
270 #
271 # o Leave it cleared
272 # o Clear the image viewer.
273 #
274 # This avoids a nasty assert error.
275 #
276 self.tb.Clear()
277
278 if len(self.fl_list):
279 self.tb.Set(self.fl_list)
280
281 try:
282 self.tb.SetSelection(0)
283 self.SetListValue(0)
284 except:
285 self.image_view.SetValue(None)
286 else:
287 self.image_view.SetValue(None)
288
289 def GetFile(self):
290 return self.set_file
291
292 def GetDirectory(self):
293 return self.set_dir
294
295 def OnCancel(self, event):
296 self.result = None
297 self.EndModal(wx.ID_CANCEL)
298
299 def OnOk(self, event):
300 self.result = self.set_file
301 self.EndModal(wx.ID_OK)
302
303
304 def OnFileDlg(self):
305 dlg = wx.FileDialog(self, "Choose an Image File", ".", "",
306 "Bmp (*.bmp)|*.bmp|JPEG (*.jpg)|*.jpg", wx.OPEN)
307
308 if dlg.ShowModal() == wx.ID_OK:
309 path = dlg.GetPath()
310 else:
311 path = None
312
313 dlg.Destroy()
314 return path
315
316 class FindFiles:
317 def __init__(self, parent, dir, mask):
318 filelist = []
319 dirlist = [".."]
320 self.dir = dir
321 self.file = ""
322 mask = mask.upper()
323 pattern = self.MakeRegex(mask)
324
325 for i in os.listdir(dir):
326 if i == "." or i == "..":
327 continue
328
329 path = os.path.join(dir, i)
330 path = path.upper()
331 value = i.upper()
332
333 if pattern.match(value) != None:
334 filelist.append(i)
335
336 self.files = filelist
337
338 def MakeRegex(self, pattern):
339 import re
340 f = "" # Set up a regex for file names
341
342 for ch in pattern:
343 if ch == "*":
344 f = f + ".*"
345 elif ch == ".":
346 f = f + "\."
347 elif ch == "?":
348 f = f + "."
349 else:
350 f = f + ch
351
352 return re.compile(f+'$')
353
354 def StripExt(self, file_nm):
355 fl_fld = os.path.splitext(file_nm)
356 fl_name = fl_fld[0]
357 ext = fl_fld[1]
358 return ext[1:]