]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wxPython/lib/imagebrowser.py
b1a9e7b13ddaa31bf6133788fb237adaa5f42212
[wxWidgets.git] / wxPython / wxPython / 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
12 # 1.0 Release
13 # Create list of all available image file types
14 # View "All Image" File Types as default filter
15 # Sort the file list
16 # Use newer "re" function for patterns
17
18 #---------------------------------------------------------------------------
19
20 import os, sys
21 from wxPython.wx import *
22 dir_path = os.getcwd()
23
24 #---------------------------------------------------------------------------
25
26 def ConvertBMP(file_nm):
27 if file_nm is None:
28 return None
29
30 fl_fld = os.path.splitext(file_nm)
31 ext = fl_fld[1]
32 ext = ext[1:].lower()
33 if ext == 'bmp':
34 image = wxImage(file_nm, wxBITMAP_TYPE_BMP)
35 elif ext == 'gif':
36 image = wxImage(file_nm, wxBITMAP_TYPE_GIF)
37 elif ext == 'png':
38 image = wxImage(file_nm, wxBITMAP_TYPE_PNG)
39 elif ext == 'jpg':
40 image = wxImage(file_nm, wxBITMAP_TYPE_JPEG)
41 elif ext == 'pcx':
42 image = wxImage(file_nm, wxBITMAP_TYPE_PCX)
43 elif ext == 'tif':
44 image = wxImage(file_nm, wxBITMAP_TYPE_TIF)
45 elif ext == 'pnm':
46 image = wxImage(file_nm, wxBITMAP_TYPE_PNM)
47 else:
48 image = wxImage(file_nm, wxBITMAP_TYPE_ANY)
49
50 return image
51
52 def GetSize(file_nm): # for scaling image values
53 image = ConvertBMP(file_nm)
54 bmp = image.ConvertToBitmap()
55 size = bmp.GetWidth(), bmp.GetHeight()
56 return size
57
58 class ImageView(wxWindow):
59 def __init__(self, parent, id=-1, pos=wxDefaultPosition, size=wxDefaultSize):
60 wxWindow.__init__(self, parent, id, pos, size)
61 self.win = parent
62 self.image = None
63 self.back_color = 'WHITE'
64 self.border_color = 'BLACK'
65
66 self.image_sizex = size.width
67 self.image_sizey = size.height
68 self.image_posx = pos.x
69 self.image_posy = pos.y
70 EVT_PAINT(self, self.OnPaint)
71
72 wxInitAllImageHandlers()
73
74 def OnPaint(self, event):
75 dc = wxPaintDC(self)
76 self.DrawImage(dc)
77
78 def DrawImage(self, dc):
79 dc.BeginDrawing()
80 self.DrawImage(dc)
81 dc.EndDrawing()
82
83 def SetValue(self, file_nm): # display the selected file in the panel
84 image = ConvertBMP(file_nm)
85 self.image = image
86 self.Refresh()
87
88 def DrawBorder(self, dc):
89 brush = wxBrush(wxNamedColour(self.back_color), wxSOLID)
90 dc.SetBrush(brush)
91 dc.SetPen(wxPen(wxNamedColour(self.border_color), 1))
92 dc.DrawRectangle(0, 0, self.image_sizex, self.image_sizey)
93
94 def DrawImage(self, dc):
95 try:
96 image = self.image
97 except:
98 return
99
100 self.DrawBorder(dc)
101 if image is None:
102 return
103
104 bmp = image.ConvertToBitmap()
105
106 iwidth = bmp.GetWidth() # dimensions of image file
107 iheight = bmp.GetHeight()
108
109 diffx = (self.image_sizex - iwidth)/2 # center calc
110 if iwidth >= self.image_sizex -10: # if image width fits in window adjust
111 diffx = 5
112 iwidth = self.image_sizex - 10
113
114 diffy = (self.image_sizey - iheight)/2 # center calc
115 if iheight >= self.image_sizey - 10: # if image height fits in window adjust
116 diffy = 5
117 iheight = self.image_sizey - 10
118
119 image.Rescale(iwidth, iheight) # rescale to fit the window
120 image.ConvertToBitmap()
121 bmp = image.ConvertToBitmap()
122 dc.DrawBitmap(bmp, diffx, diffy) # draw the image to window
123
124
125 class ImageDialog(wxDialog):
126 def __init__(self, parent, set_dir = None):
127 wxDialog.__init__(self, parent, -1, "Image Browser", wxPyDefaultPosition, wxSize(400, 400))
128
129 self.x_pos = 30 # initial display positions
130 self.y_pos = 20
131 self.delta = 20
132
133 size = wxSize(80, 25)
134
135 self.set_dir = os.getcwd()
136 self.set_file = None
137
138 if set_dir != None:
139 if os.path.exists(set_dir): # set to working directory if nothing set
140 self.set_dir = set_dir
141
142 self.dir_x = self.x_pos
143 self.dir_y = self.y_pos
144 self.DisplayDir() # display the directory value
145
146 self.y_pos = self.y_pos + self.delta
147
148 mID = wxNewId()
149 wxButton(self, mID, ' Set Directory ', wxPoint(self.x_pos, self.y_pos), size).SetDefault()
150 EVT_BUTTON(self, mID, self.SetDirect)
151
152 self.type_posy = self.y_pos # save the y position for the image type combo
153
154 self.fl_ext = '*.bmp' # initial setting for file filtering
155 self.GetFiles() # get the file list
156
157 self.y_pos = self.y_pos + self.delta + 10
158
159 self.list_height = 150
160
161 # List of Labels
162 mID = wxNewId()
163 self.tb = tb = wxListBox(self, mID, wxPoint(self.x_pos, self.y_pos), wxSize(160, self.list_height), self.fl_list, wxLB_SINGLE)
164 EVT_LISTBOX(self, mID, self.OnListClick)
165 EVT_LISTBOX_DCLICK(self, mID, self.OnListDClick)
166
167 width, height = self.tb.GetSizeTuple()
168 image_posx = self.x_pos + width + 20 # positions for setting the image window
169 image_posy = self.y_pos
170 image_sizex = 150
171 image_sizey = self.list_height
172
173 self.fl_types = ["All Images", "Bmp", "Gif", "Png", "Jpg", "Ico", "Pnm", "Pcx", "Tif", "All Files"]
174 self.fl_ext_types = { "All Images": "All", "Bmp": "*.bmp", "Gif": "*.gif", "Png": "*.png", "Jpg": "*.jpg",
175 "Ico": "*.ico", "Pnm": "*.pnm", "Pcx": "*.pcx", "Tif": "*.tif", "All Files": "*.*" }
176
177 self.set_type = self.fl_types[0] # initial file filter setting
178 self.fl_ext = self.fl_ext_types[self.set_type]
179
180 mID = wxNewId()
181 self.sel_type = wxComboBox(self, mID, self.set_type, wxPoint(image_posx , self.type_posy), wxSize(150, -1), self.fl_types, wxCB_DROPDOWN)
182 EVT_COMBOBOX(self, mID, self.OnSetType)
183
184 self.image_view = ImageView(self, pos=wxPoint(image_posx, image_posy), size=wxSize(image_sizex, image_sizey))
185
186 self.y_pos = self.y_pos + height + 20
187
188 mID = wxNewId()
189 wxButton(self, mID, ' Select ', wxPoint(100, self.y_pos), size).SetDefault()
190 EVT_BUTTON(self, mID, self.OnOk)
191
192 wxButton(self, wxID_CANCEL, 'Cancel', wxPoint(250, self.y_pos), size)
193
194 self.y_pos = self.y_pos + self.delta
195 fsize = wxSize(400, self.y_pos + 50) # resize dialog for final vertical position
196 self.SetSize(fsize)
197
198 self.ResetFiles()
199
200 def GetFiles(self): # get the file list using directory and extension values
201 if self.fl_ext == "All":
202 all_files = []
203 for ftypes in self.fl_types[1:-1]: # get list of all available image types
204 filter = self.fl_ext_types[ftypes]
205 self.fl_val = FindFiles(self, self.set_dir, filter)
206 all_files = all_files + self.fl_val.files # add to list of files
207 self.fl_list = all_files
208 else:
209 self.fl_val = FindFiles(self, self.set_dir, self.fl_ext)
210 self.fl_list = self.fl_val.files
211
212 self.fl_list.sort() # sort the file list
213
214 def DisplayDir(self): # display the working directory
215 wxStaticText(self, -1, self.set_dir, wxPoint(self.dir_x, self.dir_y), wxSize(250, -1))
216
217 def OnSetType(self, event):
218 val = event.GetString() # get file type value
219 self.fl_ext = self.fl_ext_types[val]
220 self.ResetFiles()
221
222 def OnListDClick(self, event):
223 self.OnOk(0)
224
225 def OnListClick(self, event):
226 val = event.GetSelection()
227 self.SetListValue(val)
228
229 def SetListValue(self, val):
230 file_nm = self.fl_list[val]
231 self.set_file = file_val = os.path.join(self.set_dir, file_nm)
232 self.image_view.SetValue(file_val)
233
234 def SetDirect(self, event): # set the new directory
235 dlg = wxDirDialog(self)
236 dlg.SetPath(self.set_dir)
237 if dlg.ShowModal() == wxID_OK:
238 self.set_dir = dlg.GetPath()
239 self.ResetFiles()
240 dlg.Destroy()
241
242 def ResetFiles(self): # refresh the display with files and initial image
243 self.DisplayDir()
244 self.GetFiles()
245 self.tb.Set(self.fl_list)
246 try:
247 self.tb.SetSelection(0)
248 self.SetListValue(0)
249 except:
250 self.image_view.SetValue(None)
251
252 def GetFile(self):
253 return self.set_file
254
255 def GetDirectory(self):
256 return self.set_dir
257
258 def OnCancel(self, event):
259 self.result = None
260 self.EndModal(wxID_CANCEL)
261
262 def OnOk(self, event):
263 self.result = self.set_file
264 self.EndModal(wxID_OK)
265
266
267 def OnFileDlg(self):
268 dlg = wxFileDialog(self, "Choose an Image File", ".", "", "Bmp (*.bmp)|*.bmp|JPEG (*.jpg)|*.jpg", wxOPEN)
269 if dlg.ShowModal() == wxID_OK:
270 path = dlg.GetPath()
271 else:
272 path = None
273 dlg.Destroy()
274 return path
275
276 class FindFiles:
277 def __init__(self, parent, dir, mask):
278 filelist = []
279 dirlist = [".."]
280 self.dir = dir
281 self.file = ""
282 mask = mask.upper()
283 pattern = self.MakeRegex(mask)
284 for i in os.listdir(dir):
285 if i == "." or i == "..":
286 continue
287 path = os.path.join(dir, i)
288 path = path.upper()
289 value = i.upper()
290
291 if pattern.match(value) != None:
292 filelist.append(i)
293
294 self.files = filelist
295
296 def MakeRegex(self, pattern):
297 import re
298 f = "" # Set up a regex for file names
299 for ch in pattern:
300 if ch == "*":
301 f = f + ".*"
302 elif ch == ".":
303 f = f + "\."
304 elif ch == "?":
305 f = f + "."
306 else:
307 f = f + ch
308
309 return re.compile(f)
310
311 def StripExt(self, file_nm):
312 fl_fld = os.path.splitext(file_nm)
313 fl_name = fl_fld[0]
314 ext = fl_fld[1]
315 return ext[1:]