]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wxPython/lib/imagebrowser.py
b1a9e7b13ddaa31bf6133788fb237adaa5f42212
1 #----------------------------------------------------------------------------
3 # Purpose: Display and Select Image Files
8 # Date: January 29, 2002
9 # Licence: wxWindows license
10 #----------------------------------------------------------------------------
13 # Create list of all available image file types
14 # View "All Image" File Types as default filter
16 # Use newer "re" function for patterns
18 #---------------------------------------------------------------------------
21 from wxPython
.wx
import *
22 dir_path
= os
.getcwd()
24 #---------------------------------------------------------------------------
26 def ConvertBMP(file_nm
):
30 fl_fld
= os
.path
.splitext(file_nm
)
34 image
= wxImage(file_nm
, wxBITMAP_TYPE_BMP
)
36 image
= wxImage(file_nm
, wxBITMAP_TYPE_GIF
)
38 image
= wxImage(file_nm
, wxBITMAP_TYPE_PNG
)
40 image
= wxImage(file_nm
, wxBITMAP_TYPE_JPEG
)
42 image
= wxImage(file_nm
, wxBITMAP_TYPE_PCX
)
44 image
= wxImage(file_nm
, wxBITMAP_TYPE_TIF
)
46 image
= wxImage(file_nm
, wxBITMAP_TYPE_PNM
)
48 image
= wxImage(file_nm
, wxBITMAP_TYPE_ANY
)
52 def GetSize(file_nm
): # for scaling image values
53 image
= ConvertBMP(file_nm
)
54 bmp
= image
.ConvertToBitmap()
55 size
= bmp
.GetWidth(), bmp
.GetHeight()
58 class ImageView(wxWindow
):
59 def __init__(self
, parent
, id=-1, pos
=wxDefaultPosition
, size
=wxDefaultSize
):
60 wxWindow
.__init
__(self
, parent
, id, pos
, size
)
63 self
.back_color
= 'WHITE'
64 self
.border_color
= 'BLACK'
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
)
72 wxInitAllImageHandlers()
74 def OnPaint(self
, event
):
78 def DrawImage(self
, dc
):
83 def SetValue(self
, file_nm
): # display the selected file in the panel
84 image
= ConvertBMP(file_nm
)
88 def DrawBorder(self
, dc
):
89 brush
= wxBrush(wxNamedColour(self
.back_color
), wxSOLID
)
91 dc
.SetPen(wxPen(wxNamedColour(self
.border_color
), 1))
92 dc
.DrawRectangle(0, 0, self
.image_sizex
, self
.image_sizey
)
94 def DrawImage(self
, dc
):
104 bmp
= image
.ConvertToBitmap()
106 iwidth
= bmp
.GetWidth() # dimensions of image file
107 iheight
= bmp
.GetHeight()
109 diffx
= (self
.image_sizex
- iwidth
)/2 # center calc
110 if iwidth
>= self
.image_sizex
-10: # if image width fits in window adjust
112 iwidth
= self
.image_sizex
- 10
114 diffy
= (self
.image_sizey
- iheight
)/2 # center calc
115 if iheight
>= self
.image_sizey
- 10: # if image height fits in window adjust
117 iheight
= self
.image_sizey
- 10
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
125 class ImageDialog(wxDialog
):
126 def __init__(self
, parent
, set_dir
= None):
127 wxDialog
.__init
__(self
, parent
, -1, "Image Browser", wxPyDefaultPosition
, wxSize(400, 400))
129 self
.x_pos
= 30 # initial display positions
133 size
= wxSize(80, 25)
135 self
.set_dir
= os
.getcwd()
139 if os
.path
.exists(set_dir
): # set to working directory if nothing set
140 self
.set_dir
= set_dir
142 self
.dir_x
= self
.x_pos
143 self
.dir_y
= self
.y_pos
144 self
.DisplayDir() # display the directory value
146 self
.y_pos
= self
.y_pos
+ self
.delta
149 wxButton(self
, mID
, ' Set Directory ', wxPoint(self
.x_pos
, self
.y_pos
), size
).SetDefault()
150 EVT_BUTTON(self
, mID
, self
.SetDirect
)
152 self
.type_posy
= self
.y_pos
# save the y position for the image type combo
154 self
.fl_ext
= '*.bmp' # initial setting for file filtering
155 self
.GetFiles() # get the file list
157 self
.y_pos
= self
.y_pos
+ self
.delta
+ 10
159 self
.list_height
= 150
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
)
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
171 image_sizey
= self
.list_height
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": "*.*" }
177 self
.set_type
= self
.fl_types
[0] # initial file filter setting
178 self
.fl_ext
= self
.fl_ext_types
[self
.set_type
]
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
)
184 self
.image_view
= ImageView(self
, pos
=wxPoint(image_posx
, image_posy
), size
=wxSize(image_sizex
, image_sizey
))
186 self
.y_pos
= self
.y_pos
+ height
+ 20
189 wxButton(self
, mID
, ' Select ', wxPoint(100, self
.y_pos
), size
).SetDefault()
190 EVT_BUTTON(self
, mID
, self
.OnOk
)
192 wxButton(self
, wxID_CANCEL
, 'Cancel', wxPoint(250, self
.y_pos
), size
)
194 self
.y_pos
= self
.y_pos
+ self
.delta
195 fsize
= wxSize(400, self
.y_pos
+ 50) # resize dialog for final vertical position
200 def GetFiles(self
): # get the file list using directory and extension values
201 if self
.fl_ext
== "All":
203 for ftypes
in self
.fl_types
[1:-1]: # get list of all available image types
204 filter = self
.fl_ext_types
[ftypes
]
205 self
.fl_val
= FindFiles(self
, self
.set_dir
, filter)
206 all_files
= all_files
+ self
.fl_val
.files
# add to list of files
207 self
.fl_list
= all_files
209 self
.fl_val
= FindFiles(self
, self
.set_dir
, self
.fl_ext
)
210 self
.fl_list
= self
.fl_val
.files
212 self
.fl_list
.sort() # sort the file list
214 def DisplayDir(self
): # display the working directory
215 wxStaticText(self
, -1, self
.set_dir
, wxPoint(self
.dir_x
, self
.dir_y
), wxSize(250, -1))
217 def OnSetType(self
, event
):
218 val
= event
.GetString() # get file type value
219 self
.fl_ext
= self
.fl_ext_types
[val
]
222 def OnListDClick(self
, event
):
225 def OnListClick(self
, event
):
226 val
= event
.GetSelection()
227 self
.SetListValue(val
)
229 def SetListValue(self
, val
):
230 file_nm
= self
.fl_list
[val
]
231 self
.set_file
= file_val
= os
.path
.join(self
.set_dir
, file_nm
)
232 self
.image_view
.SetValue(file_val
)
234 def SetDirect(self
, event
): # set the new directory
235 dlg
= wxDirDialog(self
)
236 dlg
.SetPath(self
.set_dir
)
237 if dlg
.ShowModal() == wxID_OK
:
238 self
.set_dir
= dlg
.GetPath()
242 def ResetFiles(self
): # refresh the display with files and initial image
245 self
.tb
.Set(self
.fl_list
)
247 self
.tb
.SetSelection(0)
250 self
.image_view
.SetValue(None)
255 def GetDirectory(self
):
258 def OnCancel(self
, event
):
260 self
.EndModal(wxID_CANCEL
)
262 def OnOk(self
, event
):
263 self
.result
= self
.set_file
264 self
.EndModal(wxID_OK
)
268 dlg
= wxFileDialog(self
, "Choose an Image File", ".", "", "Bmp (*.bmp)|*.bmp|JPEG (*.jpg)|*.jpg", wxOPEN
)
269 if dlg
.ShowModal() == wxID_OK
:
277 def __init__(self
, parent
, dir, mask
):
283 pattern
= self
.MakeRegex(mask
)
284 for i
in os
.listdir(dir):
285 if i
== "." or i
== "..":
287 path
= os
.path
.join(dir, i
)
291 if pattern
.match(value
) != None:
294 self
.files
= filelist
296 def MakeRegex(self
, pattern
):
298 f
= "" # Set up a regex for file names
311 def StripExt(self
, file_nm
):
312 fl_fld
= os
.path
.splitext(file_nm
)