]>
Commit | Line | Data |
---|---|---|
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 | #---------------------------------------------------------------------------- | |
1fded56b | 11 | |
d14a1e28 RD |
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 | |
1fded56b | 17 | |
d14a1e28 RD |
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 | print "filter = ", filter | |
206 | self.fl_val = FindFiles(self, self.set_dir, filter) | |
207 | all_files = all_files + self.fl_val.files # add to list of files | |
208 | self.fl_list = all_files | |
209 | else: | |
210 | self.fl_val = FindFiles(self, self.set_dir, self.fl_ext) | |
211 | self.fl_list = self.fl_val.files | |
212 | ||
213 | self.fl_list.sort() # sort the file list | |
214 | ||
215 | def DisplayDir(self): # display the working directory | |
216 | wxStaticText(self, -1, self.set_dir, wxPoint(self.dir_x, self.dir_y), wxSize(250, -1)) | |
217 | ||
218 | def OnSetType(self, event): | |
219 | val = event.GetString() # get file type value | |
220 | self.fl_ext = self.fl_ext_types[val] | |
221 | self.ResetFiles() | |
222 | ||
223 | def OnListDClick(self, event): | |
224 | self.OnOk(0) | |
225 | ||
226 | def OnListClick(self, event): | |
227 | val = event.GetSelection() | |
228 | self.SetListValue(val) | |
229 | ||
230 | def SetListValue(self, val): | |
231 | file_nm = self.fl_list[val] | |
232 | self.set_file = file_val = os.path.join(self.set_dir, file_nm) | |
233 | self.image_view.SetValue(file_val) | |
234 | ||
235 | def SetDirect(self, event): # set the new directory | |
236 | dlg = wxDirDialog(self) | |
237 | dlg.SetPath(self.set_dir) | |
238 | if dlg.ShowModal() == wxID_OK: | |
239 | self.set_dir = dlg.GetPath() | |
240 | self.ResetFiles() | |
241 | dlg.Destroy() | |
242 | ||
243 | def ResetFiles(self): # refresh the display with files and initial image | |
244 | self.DisplayDir() | |
245 | self.GetFiles() | |
246 | self.tb.Set(self.fl_list) | |
247 | try: | |
248 | self.tb.SetSelection(0) | |
249 | self.SetListValue(0) | |
250 | except: | |
251 | self.image_view.SetValue(None) | |
252 | ||
253 | def GetFile(self): | |
254 | return self.set_file | |
255 | ||
256 | def GetDirectory(self): | |
257 | return self.set_dir | |
258 | ||
259 | def OnCancel(self, event): | |
260 | self.result = None | |
261 | self.EndModal(wxID_CANCEL) | |
262 | ||
263 | def OnOk(self, event): | |
264 | self.result = self.set_file | |
265 | self.EndModal(wxID_OK) | |
266 | ||
267 | ||
268 | def OnFileDlg(self): | |
269 | dlg = wxFileDialog(self, "Choose an Image File", ".", "", "Bmp (*.bmp)|*.bmp|JPEG (*.jpg)|*.jpg", wxOPEN) | |
270 | if dlg.ShowModal() == wxID_OK: | |
271 | path = dlg.GetPath() | |
272 | else: | |
273 | path = None | |
274 | dlg.Destroy() | |
275 | return path | |
276 | ||
277 | class FindFiles: | |
278 | def __init__(self, parent, dir, mask): | |
279 | filelist = [] | |
280 | dirlist = [".."] | |
281 | self.dir = dir | |
282 | self.file = "" | |
283 | mask = mask.upper() | |
284 | pattern = self.MakeRegex(mask) | |
285 | for i in os.listdir(dir): | |
286 | if i == "." or i == "..": | |
287 | continue | |
288 | path = os.path.join(dir, i) | |
289 | path = path.upper() | |
290 | value = i.upper() | |
291 | ||
292 | if pattern.match(value) != None: | |
293 | filelist.append(i) | |
294 | ||
295 | self.files = filelist | |
296 | ||
297 | def MakeRegex(self, pattern): | |
298 | import re | |
299 | f = "" # Set up a regex for file names | |
300 | for ch in pattern: | |
301 | if ch == "*": | |
302 | f = f + ".*" | |
303 | elif ch == ".": | |
304 | f = f + "\." | |
305 | elif ch == "?": | |
306 | f = f + "." | |
307 | else: | |
308 | f = f + ch | |
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:] |