]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/imagebrowser.py
1 #----------------------------------------------------------------------------
3 # Purpose: Display and Select Image Files
8 # Date: January 29, 2002
9 # Licence: wxWindows license
10 #----------------------------------------------------------------------------
12 # Create list of all available image file types
13 # View "All Image" File Types as default filter
15 # Use newer "re" function for patterns
17 #----------------------------------------------------------------------------
19 # 12/08/2003 - Jeff Grimmett (grimmtooth@softhome.net)
21 # o Updated for wx namespace
22 # o Corrected a nasty bug or two - see comments below.
23 # o There was a duplicate ImageView.DrawImage() method. Que?
26 #---------------------------------------------------------------------------
33 dir_path
= os
.getcwd()
35 #---------------------------------------------------------------------------
37 def ConvertBMP(file_nm
):
41 fl_fld
= os
.path
.splitext(file_nm
)
45 image
= wx
.Image(file_nm
, wx
.BITMAP_TYPE_ANY
)
49 def GetSize(file_nm
): # for scaling image values
50 image
= ConvertBMP(file_nm
)
51 bmp
= image
.ConvertToBitmap()
52 size
= bmp
.GetWidth(), bmp
.GetHeight()
56 class ImageView(wx
.Window
):
57 def __init__(self
, parent
, id=-1, pos
=wx
.DefaultPosition
, size
=wx
.DefaultSize
):
58 wx
.Window
.__init
__(self
, parent
, id, pos
, size
)
61 self
.back_color
= 'WHITE'
62 self
.border_color
= 'BLACK'
64 # Changed API of wx uses tuples for size and pos now.
65 self
.image_sizex
= size
[0]
66 self
.image_sizey
= size
[1]
67 self
.image_posx
= pos
[0]
68 self
.image_posy
= pos
[1]
69 self
.Bind(wx
.EVT_PAINT
, self
.OnPaint
)
71 def OnPaint(self
, event
):
76 def SetValue(self
, file_nm
): # display the selected file in the panel
77 image
= ConvertBMP(file_nm
)
81 def DrawBorder(self
, dc
):
82 brush
= wx
.Brush(wx
.NamedColour(self
.back_color
), wx
.SOLID
)
84 dc
.SetPen(wx
.Pen(wx
.NamedColour(self
.border_color
), 1))
85 dc
.DrawRectangle((0, 0), (self
.image_sizex
, self
.image_sizey
))
87 def DrawImage(self
, dc
):
98 bmp
= image
.ConvertToBitmap()
100 iwidth
= bmp
.GetWidth() # dimensions of image file
101 iheight
= bmp
.GetHeight()
103 diffx
= (self
.image_sizex
- iwidth
)/2 # center calc
104 if iwidth
>= self
.image_sizex
-10: # if image width fits in window adjust
106 iwidth
= self
.image_sizex
- 10
108 diffy
= (self
.image_sizey
- iheight
)/2 # center calc
109 if iheight
>= self
.image_sizey
- 10: # if image height fits in window adjust
111 iheight
= self
.image_sizey
- 10
113 image
.Rescale(iwidth
, iheight
) # rescale to fit the window
114 image
.ConvertToBitmap()
115 bmp
= image
.ConvertToBitmap()
116 dc
.DrawBitmap(bmp
, (diffx
, diffy
)) # draw the image to window
119 class ImageDialog(wx
.Dialog
):
120 def __init__(self
, parent
, set_dir
= None):
121 wx
.Dialog
.__init
__(self
, parent
, -1, "Image Browser", wx
.DefaultPosition
, (400, 400))
123 self
.x_pos
= 30 # initial display positions
127 size
= wxSize(80, 25)
129 self
.set_dir
= os
.getcwd()
133 if os
.path
.exists(set_dir
): # set to working directory if nothing set
134 self
.set_dir
= set_dir
136 self
.dir_x
= self
.x_pos
137 self
.dir_y
= self
.y_pos
138 self
.DisplayDir() # display the directory value
140 self
.y_pos
= self
.y_pos
+ self
.delta
142 btn
= wx
.Button(self
, -1, ' Set Directory ', (self
.x_pos
, self
.y_pos
), size
).SetDefault()
143 self
.Bind(wx
.EVT_BUTTON
, self
.SetDirect
, btn
)
145 self
.type_posy
= self
.y_pos
# save the y position for the image type combo
147 self
.fl_ext
= '*.bmp' # initial setting for file filtering
148 self
.GetFiles() # get the file list
150 self
.y_pos
= self
.y_pos
+ self
.delta
+ 10
152 self
.list_height
= 150
155 self
.tb
= tb
= wx
.ListBox(self
, -1, (self
.x_pos
, self
.y_pos
),
156 (160, self
.list_height
), self
.fl_list
,
158 self
.Bind(wx
.EVT_LISTBOX
, self
.OnListClick
, tb
)
159 self
.Bind(wx
.EVT_LISTBOX_DCLICK
, self
.OnListDClick
, tb
)
161 width
, height
= self
.tb
.GetSize()
162 image_posx
= self
.x_pos
+ width
+ 20 # positions for setting the image window
163 image_posy
= self
.y_pos
165 image_sizey
= self
.list_height
168 "All Images", "Bmp", "Gif", "Png", "Jpg", "Ico", "Pnm",
169 "Pcx", "Tif", "All Files"
172 self
.fl_ext_types
= {
185 self
.set_type
= self
.fl_types
[0] # initial file filter setting
186 self
.fl_ext
= self
.fl_ext_types
[self
.set_type
]
188 self
.sel_type
= wx
.ComboBox(self
, -1, self
.set_type
, (image_posx
, self
.type_posy
),
189 (150, -1), self
.fl_types
, wx
.CB_DROPDOWN
)
190 self
.Bind(wx
.EVT_COMBOBOX
, self
.OnSetType
, self
.sel_type
)
192 self
.image_view
= ImageView( self
, pos
=(image_posx
, image_posy
),
193 size
=(image_sizex
, image_sizey
))
195 self
.y_pos
= self
.y_pos
+ height
+ 20
197 btn
= wx
.Button(self
, -1, ' Select ', (100, self
.y_pos
), size
).SetDefault()
198 self
.Bind(wx
.EVT_BUTTON
, self
.OnOk
, btn
)
200 wx
.Button(self
, wx
.ID_CANCEL
, 'Cancel', (250, self
.y_pos
), size
)
202 self
.y_pos
= self
.y_pos
+ self
.delta
203 fsize
= (400, self
.y_pos
+ 50) # resize dialog for final vertical position
208 def GetFiles(self
): # get the file list using directory and extension values
209 if self
.fl_ext
== "All":
212 for ftypes
in self
.fl_types
[1:-1]: # get list of all available image types
213 filter = self
.fl_ext_types
[ftypes
]
214 #print "filter = ", filter
215 self
.fl_val
= FindFiles(self
, self
.set_dir
, filter)
216 all_files
= all_files
+ self
.fl_val
.files
# add to list of files
218 self
.fl_list
= all_files
220 self
.fl_val
= FindFiles(self
, self
.set_dir
, self
.fl_ext
)
221 self
.fl_list
= self
.fl_val
.files
223 self
.fl_list
.sort() # sort the file list
225 def DisplayDir(self
): # display the working directory
226 wx
.StaticText(self
, -1, self
.set_dir
, (self
.dir_x
, self
.dir_y
), (250, -1))
228 def OnSetType(self
, event
):
229 val
= event
.GetString() # get file type value
230 self
.fl_ext
= self
.fl_ext_types
[val
]
233 def OnListDClick(self
, event
):
236 def OnListClick(self
, event
):
237 val
= event
.GetSelection()
238 self
.SetListValue(val
)
240 def SetListValue(self
, val
):
241 file_nm
= self
.fl_list
[val
]
242 self
.set_file
= file_val
= os
.path
.join(self
.set_dir
, file_nm
)
243 self
.image_view
.SetValue(file_val
)
245 def SetDirect(self
, event
): # set the new directory
246 dlg
= wx
.DirDialog(self
)
247 dlg
.SetPath(self
.set_dir
)
249 if dlg
.ShowModal() == wx
.ID_OK
:
250 self
.set_dir
= dlg
.GetPath()
255 def ResetFiles(self
): # refresh the display with files and initial image
259 # Changed 12/8/03 jmg
261 # o Clear listbox first
262 # o THEN check to see if there are any valid files of the selected
264 # o THEN if we have any files to display, set the listbox up,
269 # o Clear the image viewer.
271 # This avoids a nasty assert error.
275 if len(self
.fl_list
):
276 self
.tb
.Set(self
.fl_list
)
279 self
.tb
.SetSelection(0)
282 self
.image_view
.SetValue(None)
284 self
.image_view
.SetValue(None)
289 def GetDirectory(self
):
292 def OnCancel(self
, event
):
294 self
.EndModal(wx
.ID_CANCEL
)
296 def OnOk(self
, event
):
297 self
.result
= self
.set_file
298 self
.EndModal(wx
.ID_OK
)
302 dlg
= wx
.FileDialog(self
, "Choose an Image File", ".", "",
303 "Bmp (*.bmp)|*.bmp|JPEG (*.jpg)|*.jpg", wx
.OPEN
)
305 if dlg
.ShowModal() == wx
.ID_OK
:
314 def __init__(self
, parent
, dir, mask
):
320 pattern
= self
.MakeRegex(mask
)
322 for i
in os
.listdir(dir):
323 if i
== "." or i
== "..":
326 path
= os
.path
.join(dir, i
)
330 if pattern
.match(value
) != None:
333 self
.files
= filelist
335 def MakeRegex(self
, pattern
):
337 f
= "" # Set up a regex for file names
349 return re
.compile(f
+'$')
351 def StripExt(self
, file_nm
):
352 fl_fld
= os
.path
.splitext(file_nm
)