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