]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wxPython/lib/imagebrowser.py
4c0d340e3ec296ffbd7b6f55815d62e65444ac54
[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.6
9 # Date: March 27, 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 fl_fld = os.path.splitext(file_nm)
21 ext = fl_fld[1]
22 ext = string.lower(ext[1:])
23 if ext == 'bmp':
24 image = wxImage(file_nm, wxBITMAP_TYPE_BMP)
25 elif ext == 'gif':
26 image = wxImage(file_nm, wxBITMAP_TYPE_GIF)
27 elif ext == 'png':
28 image = wxImage(file_nm, wxBITMAP_TYPE_PNG)
29 elif ext == 'jpg':
30 image = wxImage(file_nm, wxBITMAP_TYPE_JPEG)
31 else:
32 image = None
33
34 return image
35
36 class ImageView:
37 def __init__(self, iparent, ipos, isize):
38 self.win = iparent
39 self.ImagePanel = wxPanel(size = isize, parent = iparent, id = -1, name = 'backgroundPanel', style = wxSIMPLE_BORDER | wxCLIP_CHILDREN, pos= ipos)
40 self.clear = wxColour(255, 255, 255)
41 self.ImagePanel.SetBackgroundColour(self.clear) # clear the panel
42
43 self.image_sizex = isize.width
44 self.image_sizey = isize.height
45 self.image_posx = ipos.x
46 self.image_posy = ipos.y
47
48 wxInitAllImageHandlers()
49
50 def SetValue(self, file_nm): # display the selected file in the panel
51 image = ConvertBMP(file_nm).ConvertToBitmap()
52 if image is None:
53 return
54
55 iwidth = image.GetWidth() # dimensions of image file
56 iheight = image.GetHeight()
57
58 diffx = (self.image_sizex - iwidth)/2 # center calc
59 if iwidth >= self.image_sizex -10: # if image width fits in window adjust
60 diffx = 5
61 iwidth = self.image_sizex - 10
62
63 diffy = (self.image_sizey - iheight)/2 # center calc
64 if iheight >= self.image_sizey - 10: # if image height fits in window adjust
65 diffy = 5
66 iheight = self.image_sizey - 10
67
68 self.ClearPanel()
69 wxStaticBitmap(self.win, -1, image, wxPoint(self.image_posx+diffx, self.image_posy+diffy), wxSize(iwidth, iheight ))
70
71 def ClearPanel(self): # clear the image panel
72 self.ImagePanel.SetBackgroundColour(self.clear)
73 self.ImagePanel.Refresh()
74
75 class ImageDialog(wxDialog):
76 def __init__(self, parent, set_dir = None):
77 wxDialog.__init__(self, parent, -1, "Image Browser", wxPyDefaultPosition, wxSize(400, 400))
78
79 self.x_pos = 30 # initial display positions
80 self.y_pos = 20
81 self.delta = 20
82
83 size = wxSize(80, 25)
84
85 self.set_dir = os.getcwd()
86
87 if set_dir != None:
88 if os.path.exists(set_dir): # set to working directory if nothing set
89 self.set_dir = set_dir
90
91 self.dir_x = self.x_pos
92 self.dir_y = self.y_pos
93 self.DisplayDir() # display the directory value
94
95 self.y_pos = self.y_pos + self.delta
96
97 mID = NewId()
98 wxButton(self, mID, ' Set Directory ', wxPoint(self.x_pos, self.y_pos), size).SetDefault()
99 EVT_BUTTON(self, mID, self.SetDirect)
100
101 self.type_posy = self.y_pos # save the y position for the image type combo
102
103 self.fl_ext = '*.bmp' # initial setting for file filtering
104 self.GetFiles() # get the file list
105
106 self.y_pos = self.y_pos + self.delta + 10
107
108 self.list_height = 150
109
110 # List of Labels
111 mID = NewId()
112 self.tb = tb = wxListBox(self, mID, wxPoint(self.x_pos, self.y_pos), wxSize(160, self.list_height), self.fl_list, wxLB_SINGLE)
113 EVT_LISTBOX(self, mID, self.OnListClick)
114 EVT_LISTBOX_DCLICK(self, mID, self.OnListDClick)
115
116 width, height = self.tb.GetSizeTuple()
117 image_posx = self.x_pos + width + 20 # positions for setting the image window
118 image_posy = self.y_pos
119 image_sizex = 150
120 image_sizey = self.list_height
121
122 self.fl_types = ["Bmp", "Gif", "Png", "Jpg"]
123 self.fl_ext_types = { "Bmp": "*.bmp", "Gif": "*.gif", "Png": "*.png", "Jpg": "*.jpg" }
124
125 self.set_type = self.fl_types[0] # initial file filter setting
126
127 mID = NewId()
128 self.sel_type = wxComboBox(self, mID, self.set_type, wxPoint(image_posx , self.type_posy), wxSize(150, -1), self.fl_types, wxCB_DROPDOWN)
129 EVT_COMBOBOX(self, mID, self.OnSetType)
130
131 self.image_view = ImageView(self, wxPoint(image_posx, image_posy), wxSize(image_sizex, image_sizey))
132
133 self.y_pos = self.y_pos + height + 20
134
135 mID = NewId()
136 wxButton(self, mID, ' Select ', wxPoint(100, self.y_pos), size).SetDefault()
137 EVT_BUTTON(self, mID, self.OnOk)
138
139 wxButton(self, wxID_CANCEL, 'Cancel', wxPoint(250, self.y_pos), size)
140
141 self.y_pos = self.y_pos + self.delta
142 fsize = wxSize(400, self.y_pos + 50) # resize dialog for final vertical position
143 self.SetSize(fsize)
144
145 self.ResetFiles()
146
147 def GetFiles(self): # get the file list using directory and extension values
148 self.fl_val = FindFiles(self, self.set_dir, self.fl_ext)
149 self.fl_list = self.fl_val.files
150
151 def DisplayDir(self): # display the working directory
152 wxStaticText(self, -1, self.set_dir, wxPoint(self.dir_x, self.dir_y), wxSize(250, -1))
153
154 def OnSetType(self, event):
155 val = event.GetString() # get file type value
156 self.fl_ext = self.fl_ext_types[val]
157 self.ResetFiles()
158
159 def OnListDClick(self, event):
160 self.OnOk(0)
161
162 def OnListClick(self, event):
163 val = event.GetSelection()
164 self.SetListValue(val)
165
166 def SetListValue(self, val):
167 file_nm = self.fl_list[val]
168 self.set_file = file_val = os.path.join(self.set_dir, file_nm)
169 self.image_view.SetValue(file_val)
170
171 def SetDirect(self, event): # set the new directory
172 dlg = wxDirDialog(self)
173 dlg.SetPath(self.set_dir)
174 if dlg.ShowModal() == wxID_OK:
175 self.set_dir = dlg.GetPath()
176 self.ResetFiles()
177 dlg.Destroy()
178
179 def ResetFiles(self): # refresh the display with files and initial image
180 self.image_view.ClearPanel()
181 self.DisplayDir()
182 self.GetFiles()
183 self.tb.Set(self.fl_list)
184 try:
185 self.tb.SetSelection(0)
186 self.SetListValue(0)
187 except:
188 pass
189
190 def GetFile(self):
191 return self.set_file
192
193 def OnCancel(self, event):
194 self.result = None
195 self.EndModal(wxID_CANCEL)
196
197 def OnOk(self, event):
198 self.result = self.set_file
199 self.EndModal(wxID_OK)
200
201
202 def OnFileDlg(self):
203 dlg = wxFileDialog(self, "Choose an Image File", ".", "", "Bmp (*.bmp)|*.bmp|JPEG (*.jpg)|*.jpg", wxOPEN)
204 if dlg.ShowModal() == wxID_OK:
205 path = dlg.GetPath()
206 else:
207 path = None
208 dlg.Destroy()
209 return path
210
211 class FindFiles:
212 def __init__(self, parent, dir, mask):
213 filelist = []
214 dirlist = [".."]
215 self.dir = dir
216 self.file = ""
217 mask = string.upper(mask)
218 self.MakeRegex(mask)
219 for i in os.listdir(dir):
220 if i == "." or i == "..":
221 continue
222 path = os.path.join(dir, i)
223 path = string.upper(path)
224 value = string.upper(i)
225
226 if self.regex.match(value) == len(value):
227 filelist.append(i)
228
229 self.files = filelist
230
231 def MakeRegex(self, pattern):
232 import regex
233 f = "" # Set up a regex for file names
234 for ch in pattern:
235 if ch == "*":
236 f = f + ".*"
237 elif ch == ".":
238 f = f + "\."
239 elif ch == "?":
240 f = f + "."
241 else:
242 f = f + ch
243
244 self.regex = regex.compile(f)
245
246 def StripExt(self, file_nm):
247 fl_fld = os.path.splitext(file_nm)
248 fl_name = fl_fld[0]
249 ext = fl_fld[1]
250 return ext[1:]