]>
Commit | Line | Data |
---|---|---|
49875c53 RD |
1 | #---------------------------------------------------------------------------- |
2 | # Name: BrowseImage.py | |
3 | # Purpose: Display and Select Image Files | |
4 | # | |
5 | # Author: Lorne White | |
6 | # | |
faf82e9b RD |
7 | # Version: 1.0 |
8 | # Date: January 29, 2002 | |
49875c53 RD |
9 | # Licence: wxWindows license |
10 | #---------------------------------------------------------------------------- | |
11 | ||
faf82e9b 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 | |
17 | ||
18 | #--------------------------------------------------------------------------- | |
19 | ||
49875c53 RD |
20 | import os, sys, string |
21 | from wxPython.wx import * | |
22 | dir_path = os.getcwd() | |
23 | ||
24 | #--------------------------------------------------------------------------- | |
25 | ||
26 | def ConvertBMP(file_nm): | |
53fe40ba RD |
27 | if file_nm is None: |
28 | return None | |
29 | ||
49875c53 RD |
30 | fl_fld = os.path.splitext(file_nm) |
31 | ext = fl_fld[1] | |
32 | ext = string.lower(ext[1:]) | |
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) | |
faf82e9b RD |
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) | |
49875c53 | 47 | else: |
faf82e9b | 48 | image = wxImage(file_nm, wxBITMAP_TYPE_ANY) |
49875c53 RD |
49 | |
50 | return image | |
51 | ||
53fe40ba RD |
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) | |
49875c53 RD |
71 | |
72 | wxInitAllImageHandlers() | |
73 | ||
53fe40ba RD |
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 | ||
49875c53 | 83 | def SetValue(self, file_nm): # display the selected file in the panel |
53fe40ba RD |
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: | |
49875c53 RD |
98 | return |
99 | ||
53fe40ba RD |
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() | |
49875c53 RD |
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 | |
49875c53 | 118 | |
53fe40ba RD |
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 | ||
49875c53 RD |
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 | ||
137 | if set_dir != None: | |
138 | if os.path.exists(set_dir): # set to working directory if nothing set | |
139 | self.set_dir = set_dir | |
140 | ||
141 | self.dir_x = self.x_pos | |
142 | self.dir_y = self.y_pos | |
143 | self.DisplayDir() # display the directory value | |
144 | ||
145 | self.y_pos = self.y_pos + self.delta | |
146 | ||
147 | mID = NewId() | |
148 | wxButton(self, mID, ' Set Directory ', wxPoint(self.x_pos, self.y_pos), size).SetDefault() | |
149 | EVT_BUTTON(self, mID, self.SetDirect) | |
150 | ||
151 | self.type_posy = self.y_pos # save the y position for the image type combo | |
152 | ||
153 | self.fl_ext = '*.bmp' # initial setting for file filtering | |
154 | self.GetFiles() # get the file list | |
155 | ||
156 | self.y_pos = self.y_pos + self.delta + 10 | |
157 | ||
158 | self.list_height = 150 | |
159 | ||
160 | # List of Labels | |
161 | mID = NewId() | |
162 | self.tb = tb = wxListBox(self, mID, wxPoint(self.x_pos, self.y_pos), wxSize(160, self.list_height), self.fl_list, wxLB_SINGLE) | |
163 | EVT_LISTBOX(self, mID, self.OnListClick) | |
164 | EVT_LISTBOX_DCLICK(self, mID, self.OnListDClick) | |
165 | ||
166 | width, height = self.tb.GetSizeTuple() | |
167 | image_posx = self.x_pos + width + 20 # positions for setting the image window | |
168 | image_posy = self.y_pos | |
169 | image_sizex = 150 | |
170 | image_sizey = self.list_height | |
171 | ||
faf82e9b RD |
172 | self.fl_types = ["All Images", "Bmp", "Gif", "Png", "Jpg", "Ico", "Pnm", "Pcx", "Tif", "All Files"] |
173 | self.fl_ext_types = { "All Images": "All", "Bmp": "*.bmp", "Gif": "*.gif", "Png": "*.png", "Jpg": "*.jpg", | |
174 | "Ico": "*.ico", "Pnm": "*.pnm", "Pcx": "*.pcx", "Tif": "*.tif", "All Files": "*.*" } | |
49875c53 RD |
175 | |
176 | self.set_type = self.fl_types[0] # initial file filter setting | |
faf82e9b | 177 | self.fl_ext = self.fl_ext_types[self.set_type] |
49875c53 RD |
178 | |
179 | mID = NewId() | |
180 | self.sel_type = wxComboBox(self, mID, self.set_type, wxPoint(image_posx , self.type_posy), wxSize(150, -1), self.fl_types, wxCB_DROPDOWN) | |
181 | EVT_COMBOBOX(self, mID, self.OnSetType) | |
182 | ||
53fe40ba | 183 | self.image_view = ImageView(self, pos=wxPoint(image_posx, image_posy), size=wxSize(image_sizex, image_sizey)) |
49875c53 RD |
184 | |
185 | self.y_pos = self.y_pos + height + 20 | |
186 | ||
187 | mID = NewId() | |
188 | wxButton(self, mID, ' Select ', wxPoint(100, self.y_pos), size).SetDefault() | |
189 | EVT_BUTTON(self, mID, self.OnOk) | |
190 | ||
191 | wxButton(self, wxID_CANCEL, 'Cancel', wxPoint(250, self.y_pos), size) | |
192 | ||
193 | self.y_pos = self.y_pos + self.delta | |
194 | fsize = wxSize(400, self.y_pos + 50) # resize dialog for final vertical position | |
195 | self.SetSize(fsize) | |
196 | ||
197 | self.ResetFiles() | |
198 | ||
199 | def GetFiles(self): # get the file list using directory and extension values | |
faf82e9b RD |
200 | if self.fl_ext == "All": |
201 | all_files = [] | |
202 | for ftypes in self.fl_types[1:-1]: # get list of all available image types | |
203 | filter = self.fl_ext_types[ftypes] | |
204 | self.fl_val = FindFiles(self, self.set_dir, filter) | |
205 | all_files = all_files + self.fl_val.files # add to list of files | |
206 | self.fl_list = all_files | |
207 | else: | |
208 | self.fl_val = FindFiles(self, self.set_dir, self.fl_ext) | |
209 | self.fl_list = self.fl_val.files | |
210 | ||
211 | self.fl_list.sort() # sort the file list | |
49875c53 RD |
212 | |
213 | def DisplayDir(self): # display the working directory | |
214 | wxStaticText(self, -1, self.set_dir, wxPoint(self.dir_x, self.dir_y), wxSize(250, -1)) | |
215 | ||
216 | def OnSetType(self, event): | |
217 | val = event.GetString() # get file type value | |
218 | self.fl_ext = self.fl_ext_types[val] | |
219 | self.ResetFiles() | |
220 | ||
221 | def OnListDClick(self, event): | |
222 | self.OnOk(0) | |
223 | ||
224 | def OnListClick(self, event): | |
225 | val = event.GetSelection() | |
226 | self.SetListValue(val) | |
227 | ||
228 | def SetListValue(self, val): | |
229 | file_nm = self.fl_list[val] | |
230 | self.set_file = file_val = os.path.join(self.set_dir, file_nm) | |
231 | self.image_view.SetValue(file_val) | |
232 | ||
233 | def SetDirect(self, event): # set the new directory | |
234 | dlg = wxDirDialog(self) | |
235 | dlg.SetPath(self.set_dir) | |
236 | if dlg.ShowModal() == wxID_OK: | |
237 | self.set_dir = dlg.GetPath() | |
238 | self.ResetFiles() | |
239 | dlg.Destroy() | |
240 | ||
241 | def ResetFiles(self): # refresh the display with files and initial image | |
49875c53 RD |
242 | self.DisplayDir() |
243 | self.GetFiles() | |
244 | self.tb.Set(self.fl_list) | |
245 | try: | |
246 | self.tb.SetSelection(0) | |
247 | self.SetListValue(0) | |
248 | except: | |
53fe40ba | 249 | self.image_view.SetValue(None) |
49875c53 RD |
250 | |
251 | def GetFile(self): | |
252 | return self.set_file | |
53fe40ba RD |
253 | |
254 | def GetDirectory(self): | |
255 | return self.set_dir | |
49875c53 RD |
256 | |
257 | def OnCancel(self, event): | |
258 | self.result = None | |
259 | self.EndModal(wxID_CANCEL) | |
260 | ||
261 | def OnOk(self, event): | |
262 | self.result = self.set_file | |
263 | self.EndModal(wxID_OK) | |
264 | ||
265 | ||
266 | def OnFileDlg(self): | |
267 | dlg = wxFileDialog(self, "Choose an Image File", ".", "", "Bmp (*.bmp)|*.bmp|JPEG (*.jpg)|*.jpg", wxOPEN) | |
268 | if dlg.ShowModal() == wxID_OK: | |
269 | path = dlg.GetPath() | |
270 | else: | |
271 | path = None | |
272 | dlg.Destroy() | |
273 | return path | |
274 | ||
275 | class FindFiles: | |
276 | def __init__(self, parent, dir, mask): | |
277 | filelist = [] | |
278 | dirlist = [".."] | |
279 | self.dir = dir | |
280 | self.file = "" | |
281 | mask = string.upper(mask) | |
faf82e9b | 282 | pattern = self.MakeRegex(mask) |
49875c53 RD |
283 | for i in os.listdir(dir): |
284 | if i == "." or i == "..": | |
285 | continue | |
286 | path = os.path.join(dir, i) | |
287 | path = string.upper(path) | |
288 | value = string.upper(i) | |
289 | ||
faf82e9b | 290 | if pattern.match(value) != None: |
49875c53 RD |
291 | filelist.append(i) |
292 | ||
293 | self.files = filelist | |
294 | ||
295 | def MakeRegex(self, pattern): | |
faf82e9b | 296 | import re |
49875c53 RD |
297 | f = "" # Set up a regex for file names |
298 | for ch in pattern: | |
299 | if ch == "*": | |
300 | f = f + ".*" | |
301 | elif ch == ".": | |
302 | f = f + "\." | |
303 | elif ch == "?": | |
304 | f = f + "." | |
305 | else: | |
306 | f = f + ch | |
307 | ||
faf82e9b | 308 | return re.compile(f) |
49875c53 RD |
309 | |
310 | def StripExt(self, file_nm): | |
311 | fl_fld = os.path.splitext(file_nm) | |
312 | fl_name = fl_fld[0] | |
313 | ext = fl_fld[1] | |
314 | return ext[1:] |