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