Added colourselect.py, imagebrowser.py and an updated calendar.py to

wxPython/lib from Lorne White.


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@9636 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robin Dunn
2001-04-02 05:57:32 +00:00
parent 471825d745
commit 49875c533b
8 changed files with 626 additions and 135 deletions

View File

@@ -26,10 +26,12 @@ print, sys.stdout.write, etc.
Added CreateTextSizer and CreateButtonSizer to wxDialog Added CreateTextSizer and CreateButtonSizer to wxDialog
Added wxPython/lib/infoframe.py from Chris Fama. It contains a class Added wxPython/lib/infoframe.py from Chris Fama. It contains a class
that can be used in place of wxPyOnDemandOutputWindow. that can be used in place of wxPyOnDemandOutputWindow.
Added colourselect.py, imagebrowser.py and an updated calendar.py to
wxPython/lib from Lorne White.

View File

@@ -0,0 +1,117 @@
#----------------------------------------------------------------------------
# Name: ColourSelect.py
# Purpose: Colour Selection control display testing on panel for wxPython demo
#
# Author: Lorne White (email: lorne.white@telusplanet.net)
#
# Version 0.5
# Date: Feb 26, 2001
# Licence: wxWindows license
#----------------------------------------------------------------------------
from wxPython.wx import *
from wxPython.lib.colourselect import *
import string
#---------------------------------------------------------------------------
class TestColourSelect(wxPanel):
def __init__(self, parent, log):
self.log = log
wxPanel.__init__(self, parent, -1)
wxStaticText(self, -1, "This example uses a colour selection control based on the wxButton and wxColourDialog Classes. Click Button to get Colour Values",
wxPoint(10, 20), wxSize(400, 60))
self.x_pos = 30
self.y_pos = 100
delta = 40
mID = NewId()
wxButton(self, mID, "Get All Colours", wxPoint(self.x_pos, self.y_pos), wxSize(80, 20))
EVT_BUTTON(self, mID, self.OnClick)
self.y_pos = self.y_pos + delta
wxStaticText(self, -1, "Default", wxPoint(self.x_pos, self.y_pos), wxSize(-1, -1)) # name
self.colour_def = ColourSelect(self, wxPoint(self.x_pos+100, self.y_pos)) # default colour selection control
self.y_pos = self.y_pos + delta
colours = [[255, 255, 0], [255, 0, 255], [0, 255, 0], [0, 0, 255]] # list of initial colours for display
self.names = names = [ "Default Size", "Another Size", "Another Colour", "Larger"] # display names
sizes = [ None, wxSize(60, 20), None, wxSize(60, 60)] # button sizes
self.set_val = []
for i in range(len(colours)):
wxStaticText(self, -1, names[i], wxPoint(self.x_pos, self.y_pos), wxSize(-1, -1)) # name
val = ColourSelect(self, wxPoint(self.x_pos+100, self.y_pos), colours[i], sizes[i]) # colour selection button
self.set_val.append(val) # store control for reference
self.y_pos = self.y_pos + delta
def OnClick(self, event):
result = []
colour = self.colour_def.GetColour() # default control value
result.append("Default: " + str(colour))
for i in range(len(self.set_val)):
val = self.set_val[i]
colour = val.GetColour() # get the colour selection button result
name = self.names[i]
result.append(name + ": " + str(colour)) # create string list for easy viewing of results
out_result = string.joinfields(result, ', ')
self.log.WriteText("Colour Results :" + out_result)
#---------------------------------------------------------------------------
def runTest(frame, nb, log):
win = TestColourSelect(nb, log)
return win
#---------------------------------------------------------------------------
overview = """\
A checkbox is a labelled box which is either on (checkmark is visible) or off (no checkmark).
wxCheckBox()
-----------------------
Default constructor.
wxCheckBox(wxWindow* parent, wxWindowID id, const wxString& label, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = 0, const wxValidator& val, const wxString& name = "checkBox")
Constructor, creating and showing a checkbox.
Parameters
-------------------
parent = Parent window. Must not be NULL.
id = Checkbox identifier. A value of -1 indicates a default value.
label = Text to be displayed next to the checkbox.
pos = Checkbox position. If the position (-1, -1) is specified then a default position is chosen.
size = Checkbox size. If the default size (-1, -1) is specified then a default size is chosen.
style = Window style. See wxCheckBox.
validator = Window validator.
name = Window name.
"""

View File

@@ -0,0 +1,40 @@
#----------------------------------------------------------------------------
# Name: ImageBrowser.py
# Purpose: Image Selection dialog for wxPython demo
#
# Author: Lorne White (email: lorne.white@telusplanet.net)
#
# Version 0.5
# Date: Feb 26, 2001
# Licence: wxWindows license
#----------------------------------------------------------------------------
from wxPython.wx import *
from wxPython.lib.imagebrowser import *
import os
#---------------------------------------------------------------------------
def runTest(frame, nb, log):
dir = os.getcwd() # get working directory
initial_dir = os.path.join(dir, 'bitmaps') # set the initial directory for the demo bitmaps
win = ImageDialog(frame, initial_dir) # open the image browser dialog
win.Centre()
if win.ShowModal() == wxID_OK:
log.WriteText("You Selected File: " + win.GetFile()) # show the selected file
else:
log.WriteText("You pressed Cancel\n")
#---------------------------------------------------------------------------
overview = """\
"""

View File

@@ -20,7 +20,7 @@ from wxPython.html import wxHtmlWindow
_treeList = [ _treeList = [
('New since last release', ['LayoutAnchors', "FancyText", ('New since last release', ['ColourSelect', 'ImageBrowser',
]), ]),
('Managed Windows', ['wxFrame', 'wxDialog', 'wxMiniFrame']), ('Managed Windows', ['wxFrame', 'wxDialog', 'wxMiniFrame']),
@@ -57,7 +57,7 @@ _treeList = [
'wxMultipleChoiceDialog', 'wxPlotCanvas', 'wxFloatBar', 'wxMultipleChoiceDialog', 'wxPlotCanvas', 'wxFloatBar',
'PyShell', 'wxCalendar', 'wxMVCTree', 'wxVTKRenderWindow', 'PyShell', 'wxCalendar', 'wxMVCTree', 'wxVTKRenderWindow',
'FileBrowseButton', 'GenericButtons', 'wxEditor', 'FileBrowseButton', 'GenericButtons', 'wxEditor',
'PyShellWindow', 'PyShellWindow', 'ColourSelect', 'ImageBrowser',
]), ]),
('Cool Contribs', ['pyTree', 'hangman', 'SlashDot', 'XMLtreeview']), ('Cool Contribs', ['pyTree', 'hangman', 'SlashDot', 'XMLtreeview']),

View File

@@ -4,13 +4,13 @@
# #
# Author: Lorne White (email: lwhite1@planet.eon.net) # Author: Lorne White (email: lwhite1@planet.eon.net)
# #
# Created: # Version 0.9
# Version 0.8 2000/04/16 # Date: Feb 26, 2001
# Licence: wxWindows license # Licence: wxWindows license
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
from wxPython.wx import * from wxPython.wx import *
from wxPython.lib.calendar import wxCalendar, Month, PrtCalDraw from wxPython.lib.calendar import wxCalendar, Month, PrtCalDraw, CalenDlg
import os import os
dir_path = os.getcwd() dir_path = os.getcwd()
@@ -51,15 +51,19 @@ class TestPanel(wxPanel):
self.calend = wxCalendar(self, -1, wxPoint(100, 50), wxSize(200, 180)) self.calend = wxCalendar(self, -1, wxPoint(100, 50), wxSize(200, 180))
start_month = 11 # start_month = 2 # preselect the date for calendar
start_year = 1999 # start_year = 2001
start_month = self.calend.GetMonth() # get the current month & year
start_year = self.calend.GetYear()
# month list from DateTime module # month list from DateTime module
monthlist = GetMonthList() monthlist = GetMonthList()
self.date = wxComboBox(self, 10, Month[start_month], wxPoint(100, 20), wxSize(90, -1), monthlist, wxCB_DROPDOWN) mID = NewId()
EVT_COMBOBOX(self, 10, self.EvtComboBox) self.date = wxComboBox(self, mID, Month[start_month], wxPoint(100, 20), wxSize(90, -1), monthlist, wxCB_DROPDOWN)
EVT_COMBOBOX(self, mID, self.EvtComboBox)
# set start month and year # set start month and year
@@ -82,49 +86,61 @@ class TestPanel(wxPanel):
# scroll bar for month selection # scroll bar for month selection
self.scroll = wxScrollBar(self, 40, wxPoint(100, 240), wxSize(200, 20), wxSB_HORIZONTAL) mID = NewId()
self.scroll = wxScrollBar(self, mID, wxPoint(100, 240), wxSize(200, 20), wxSB_HORIZONTAL)
self.scroll.SetScrollbar(start_month-1, 1, 12, 1, TRUE) self.scroll.SetScrollbar(start_month-1, 1, 12, 1, TRUE)
EVT_COMMAND_SCROLL(self, 40, self.Scroll) EVT_COMMAND_SCROLL(self, mID, self.Scroll)
# spin control for year selection # spin control for year selection
self.dtext = wxTextCtrl(self, -1, str(start_year), wxPoint(200, 20), wxSize(60, -1)) self.dtext = wxTextCtrl(self, -1, str(start_year), wxPoint(200, 20), wxSize(60, -1))
h = self.dtext.GetSize().height h = self.dtext.GetSize().height
self.spin = wxSpinButton(self, 20, wxPoint(270, 20), wxSize(h*2, h)) mID = NewId()
self.spin = wxSpinButton(self, mID, wxPoint(270, 20), wxSize(h*2, h))
self.spin.SetRange(1980, 2010) self.spin.SetRange(1980, 2010)
self.spin.SetValue(start_year) self.spin.SetValue(start_year)
EVT_SPIN(self, 20, self.OnSpin) EVT_SPIN(self, mID, self.OnSpin)
# button for calendar dialog test # button for calendar dialog test
wxStaticText(self, -1, "Test Calendar Dialog", wxPoint(350, 50), wxSize(150, -1)) wxStaticText(self, -1, "Test Calendar Dialog", wxPoint(350, 50), wxSize(150, -1))
mID = NewId()
bmp = wxBitmap('bitmaps/Calend.bmp', wxBITMAP_TYPE_BMP) bmp = wxBitmap('bitmaps/Calend.bmp', wxBITMAP_TYPE_BMP)
self.but = wxBitmapButton(self, 60, bmp, wxPoint(380, 80))#, wxSize(30, 30)) self.but = wxBitmapButton(self, mID, bmp, wxPoint(380, 80))#, wxSize(30, 30))
EVT_BUTTON(self, 60, self.TestDlg) EVT_BUTTON(self, mID, self.TestDlg)
# button for calendar window test # button for calendar window test
wxStaticText(self, -1, "Test Calendar Window", wxPoint(350, 150), wxSize(150, -1)) wxStaticText(self, -1, "Test Calendar Window", wxPoint(350, 150), wxSize(150, -1))
mID = NewId()
bmp = wxBitmap('bitmaps/Calend.bmp', wxBITMAP_TYPE_BMP) bmp = wxBitmap('bitmaps/Calend.bmp', wxBITMAP_TYPE_BMP)
self.but = wxBitmapButton(self, 160, bmp, wxPoint(380, 180))#, wxSize(30, 30)) self.but = wxBitmapButton(self, mID, bmp, wxPoint(380, 180))#, wxSize(30, 30))
EVT_BUTTON(self, 160, self.TestFrame) EVT_BUTTON(self, mID, self.TestFrame)
wxStaticText(self, -1, "Test Calendar Print", wxPoint(350, 250), wxSize(150, -1)) wxStaticText(self, -1, "Test Calendar Print", wxPoint(350, 250), wxSize(150, -1))
mID = NewId()
bmp = wxBitmap('bitmaps/Calend.bmp', wxBITMAP_TYPE_BMP) bmp = wxBitmap('bitmaps/Calend.bmp', wxBITMAP_TYPE_BMP)
self.but = wxBitmapButton(self, 170, bmp, wxPoint(380, 280))#, wxSize(30, 30)) self.but = wxBitmapButton(self, mID, bmp, wxPoint(380, 280))#, wxSize(30, 30))
EVT_BUTTON(self, 170, self.OnPreview) EVT_BUTTON(self, mID, self.OnPreview)
# calendar dialog # calendar dialog
def TestDlg(self, event): def TestDlg(self, event): # test the date dialog
dlg = CalenDlg(self, self.log) dlg = CalenDlg(self)
dlg.Centre() dlg.Centre()
dlg.ShowModal() if dlg.ShowModal() == wxID_OK:
dlg.Destroy() result = dlg.result
day = result[1]
month = result[2]
year = result[3]
new_date = str(month) + '/'+ str(day) + '/'+ str(year)
self.log.WriteText('Date Selected: %s\n' % new_date)
else:
self.log.WriteText('No Date Selected')
# calendar window test # calendar window test
@@ -211,105 +227,6 @@ class TestPanel(wxPanel):
self.calend.SetCurrentDay() self.calend.SetCurrentDay()
self.ResetDisplay() self.ResetDisplay()
# test the calendar control in a dialog
class CalenDlg(wxDialog):
def __init__(self, parent, log):
self.log = log
wxDialog.__init__(self, parent, -1, "Test Calendar", wxPyDefaultPosition, wxSize(280, 300))
start_month = 2
start_year = 1999
# get month list from DateTime
monthlist = GetMonthList()
# select the month
self.date = wxComboBox(self, 100, Month[start_month], wxPoint(20, 20), wxSize(90, -1), monthlist, wxCB_DROPDOWN)
EVT_COMBOBOX(self, 100, self.EvtComboBox)
# alternate spin button to control the month
h = self.date.GetSize().height
self.m_spin = wxSpinButton(self, 120, wxPoint(120, 20), wxSize(h*2, h), wxSP_VERTICAL)
self.m_spin.SetRange(1, 12)
self.m_spin.SetValue(start_month)
EVT_SPIN(self, 120, self.OnMonthSpin)
# spin button to conrol the year
self.dtext = wxTextCtrl(self, -1, str(start_year), wxPoint(160, 20), wxSize(60, -1))
h = self.dtext.GetSize().height
self.y_spin = wxSpinButton(self, 20, wxPoint(220, 20), wxSize(h*2, h), wxSP_VERTICAL)
self.y_spin.SetRange(1980, 2010)
self.y_spin.SetValue(start_year)
EVT_SPIN(self, 20, self.OnYrSpin)
# set the calendar and attributes
self.calend = wxCalendar(self, -1, wxPoint(20, 60), wxSize(240, 200))
self.calend.SetMonth(start_month)
self.calend.SetYear(start_year)
self.calend.HideTitle()
self.calend.ShowWeekEnd()
self.ResetDisplay()
self.Connect(self.calend.GetId(), -1, 2100, self.MouseClick)
# log the mouse clicks
def MouseClick(self, evt):
text = '%s CLICK %02d/%02d/%d' % (evt.click, evt.day, evt.month, evt.year) # format date
self.log.WriteText('Date Selected: ' + text + '\n')
if evt.click == 'DLEFT':
self.EndModal(wxID_OK)
# month and year spin selection routines
def OnMonthSpin(self, event):
month = event.GetPosition()
if month >= 0 and month <= 12:
self.date.SetValue(Month[month])
self.calend.SetMonth(month)
self.calend.Refresh()
def OnYrSpin(self, event):
year = event.GetPosition()
self.dtext.SetValue(str(year))
self.calend.SetYear(year)
self.calend.Refresh()
def EvtComboBox(self, event):
name = event.GetString()
self.log.WriteText('EvtComboBox: %s\n' % name)
monthval = self.date.FindString(name)
self.m_spin.SetValue(monthval+1)
self.calend.SetMonth(monthval+1)
self.ResetDisplay()
# set the calendar for highlighted days
def ResetDisplay(self):
month = self.calend.GetMonth()
try:
set_days = test_days[month]
except:
set_days = [1, 5, 12]
self.calend.AddSelect([4, 11], 'BLUE', 'WHITE')
self.calend.SetSelDay(set_days)
self.calend.Refresh()
# test of full window calendar control functions # test of full window calendar control functions
class CalendFrame(wxFrame): class CalendFrame(wxFrame):
@@ -400,20 +317,26 @@ class CalendFrame(wxFrame):
tb = self.CreateToolBar(wxTB_HORIZONTAL|wxNO_BORDER) tb = self.CreateToolBar(wxTB_HORIZONTAL|wxNO_BORDER)
bmp_path = 'bitmaps/' bmp_path = 'bitmaps/'
SetToolPath(self, tb, 10, bmp_path + 'DbDec.bmp', 'Dec Year')
EVT_TOOL(self, 10, self.OnDecYear)
SetToolPath(self, tb, 15, bmp_path + 'Dec.bmp', 'Dec Month') mID = NewId()
EVT_TOOL(self, 15, self.OnDecMonth) SetToolPath(self, tb, mID, bmp_path + 'DbDec.bmp', 'Dec Year')
EVT_TOOL(self, mID, self.OnDecYear)
SetToolPath(self, tb, 30, bmp_path + 'Pt.bmp', 'Current Month') mID = NewId()
EVT_TOOL(self, 30, self.OnCurrent) SetToolPath(self, tb, mID, bmp_path + 'Dec.bmp', 'Dec Month')
EVT_TOOL(self, mID, self.OnDecMonth)
SetToolPath(self, tb, 40, bmp_path + 'Inc.bmp', 'Inc Month') mID = NewId()
EVT_TOOL(self, 40, self.OnIncMonth) SetToolPath(self, tb, mID, bmp_path + 'Pt.bmp', 'Current Month')
EVT_TOOL(self, mID, self.OnCurrent)
SetToolPath(self, tb, 45, bmp_path + 'DbInc.bmp', 'Inc Year') mID = NewId()
EVT_TOOL(self, 45, self.OnIncYear) SetToolPath(self, tb, mID, bmp_path + 'Inc.bmp', 'Inc Month')
EVT_TOOL(self, mID, self.OnIncMonth)
mID = NewId()
SetToolPath(self, tb, mID, bmp_path + 'DbInc.bmp', 'Inc Year')
EVT_TOOL(self, mID, self.OnIncYear)
tb.Realize() tb.Realize()

View File

@@ -2,10 +2,11 @@
# Name: calendar.py # Name: calendar.py
# Purpose: Calendar display control # Purpose: Calendar display control
# #
# Author: Lorne White (email: lwhite1@planet.eon.net) # Author: Lorne White (email: lorne.white@telusplanet.net)
# #
# Created: # Created:
# Version 0.6 2000/03/30 # Version 0.8
# Date: Feb 27, 2001
# Licence: wxWindows license # Licence: wxWindows license
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
@@ -23,6 +24,14 @@ BusCalDays = [0, 1, 2, 3, 4, 5, 6]
# calendar drawing routing # calendar drawing routing
def GetMonthList():
monthlist = []
for i in range(13):
name = Month[i]
if name != None:
monthlist.append(name)
return monthlist
class CalDraw: class CalDraw:
def __init__(self, parent): def __init__(self, parent):
self.pwidth = 1 self.pwidth = 1
@@ -626,5 +635,105 @@ class wxCalendar(wxWindow):
def ClearDsp(self): def ClearDsp(self):
self.Clear() self.Clear()
class CalenDlg(wxDialog):
def __init__(self, parent, month=None, day = None, year=None):
wxDialog.__init__(self, parent, -1, "Event Calendar", wxPyDefaultPosition, wxSize(280, 360))
# set the calendar and attributes
self.calend = wxCalendar(self, -1, wxPoint(20, 60), wxSize(240, 200))
if month == None:
self.calend.SetCurrentDay()
start_month = self.calend.GetMonth()
start_year = self.calend.GetYear()
else:
start_month = month
start_year = year
self.calend.SetDayValue(day)
self.calend.HideTitle()
self.ResetDisplay()
# get month list from DateTime
monthlist = GetMonthList()
# select the month
mID = NewId()
self.date = wxComboBox(self, mID, Month[start_month], wxPoint(20, 20), wxSize(90, -1), monthlist, wxCB_DROPDOWN)
EVT_COMBOBOX(self, mID, self.EvtComboBox)
# alternate spin button to control the month
mID = NewId()
h = self.date.GetSize().height
self.m_spin = wxSpinButton(self, mID, wxPoint(130, 20), wxSize(h*2, h), wxSP_VERTICAL)
self.m_spin.SetRange(1, 12)
self.m_spin.SetValue(start_month)
EVT_SPIN(self, mID, self.OnMonthSpin)
# spin button to control the year
mID = NewId()
self.dtext = wxTextCtrl(self, -1, str(start_year), wxPoint(160, 20), wxSize(60, -1))
h = self.dtext.GetSize().height
self.y_spin = wxSpinButton(self, mID, wxPoint(220, 20), wxSize(h*2, h), wxSP_VERTICAL)
self.y_spin.SetRange(1980, 2010)
self.y_spin.SetValue(start_year)
EVT_SPIN(self, mID, self.OnYrSpin)
self.Connect(self.calend.GetId(), -1, 2100, self.MouseClick)
x_pos = 50
y_pos = 280
but_size = wxSize(60, 25)
mID = NewId()
wxButton(self, mID, ' Ok ', wxPoint(x_pos, y_pos), but_size)
EVT_BUTTON(self, mID, self.OnOk)
mID = NewId()
wxButton(self, mID, ' Close ', wxPoint(x_pos + 120, y_pos), but_size)
EVT_BUTTON(self, mID, self.OnCancel)
def OnOk(self, event):
self.EndModal(wxID_OK)
def OnCancel(self, event):
self.EndModal(wxID_CANCEL)
# log the mouse clicks
def MouseClick(self, evt):
self.result = [evt.click, str(evt.day), Month[evt.month], str(evt.year)] # result click type and date
if evt.click == 'DLEFT':
self.EndModal(wxID_OK)
# month and year spin selection routines
def OnMonthSpin(self, event):
month = event.GetPosition()
self.date.SetValue(Month[month])
self.calend.SetMonth(month)
self.calend.Refresh()
def OnYrSpin(self, event):
year = event.GetPosition()
self.dtext.SetValue(str(year))
self.calend.SetYear(year)
self.calend.Refresh()
def EvtComboBox(self, event):
name = event.GetString()
monthval = self.date.FindString(name)
self.m_spin.SetValue(monthval+1)
self.calend.SetMonth(monthval+1)
self.ResetDisplay()
# set the calendar for highlighted days
def ResetDisplay(self):
month = self.calend.GetMonth()
self.calend.Refresh()

View File

@@ -0,0 +1,50 @@
#!/bin/env python
#----------------------------------------------------------------------------
# Name: ColourSelect.py
# Purpose: Colour Box Selection Control
#
# Author: Lorne White, Lorne.White@telusplanet.net
#
# Created: Feb 25, 2001
# Licence: wxWindows license
#----------------------------------------------------------------------------
from wxPython.wx import *
# creates a colour wxButton with selectable color
# button click provides a colour selection box
# button colour will change to new colour
# GetColour method to get the selected colour
class ColourSelect:
def __init__(self, parent, position = wxPoint(20, 20), bcolour = [0, 0, 0], size = wxSize(20, 20)):
self.win = parent
if size is None:
size = wxSize(20, 20)
mID = NewId()
self.b = b = wxButton(parent, mID, "", position, size)
EVT_BUTTON(parent, mID, self.OnClick)
self.set_colour_val = set_colour = wxColor(bcolour[0], bcolour[1], bcolour[2])
b.SetBackgroundColour(set_colour)
b.SetForegroundColour(wxWHITE)
self.set_colour = bcolour
def SetColour(self, bcolour):
self.b.SetBackgroundColour(bcolour)
def GetColour(self):
return self.set_colour
def OnClick(self, event):
data = wxColourData()
data.SetChooseFull(true)
data.SetColour(self.set_colour_val)
dlg = wxColourDialog(self.win, data)
if dlg.ShowModal() == wxID_OK:
data = dlg.GetColourData()
self.set_colour = set = data.GetColour().Get()
self.set_colour_val = bcolour = wxColour(set[0],set[1],set[2])
self.b.SetBackgroundColour(bcolour)
dlg.Destroy()

View File

@@ -0,0 +1,250 @@
#!/bin/env python
#----------------------------------------------------------------------------
# Name: BrowseImage.py
# Purpose: Display and Select Image Files
#
# Author: Lorne White
#
# Version: 0.6
# Date: March 27, 2001
# Licence: wxWindows license
#----------------------------------------------------------------------------
import os, sys, string
from wxPython.wx import *
dir_path = os.getcwd()
#---------------------------------------------------------------------------
def ConvertBMP(file_nm):
fl_fld = os.path.splitext(file_nm)
ext = fl_fld[1]
ext = string.lower(ext[1:])
if ext == 'bmp':
image = wxImage(file_nm, wxBITMAP_TYPE_BMP)
elif ext == 'gif':
image = wxImage(file_nm, wxBITMAP_TYPE_GIF)
elif ext == 'png':
image = wxImage(file_nm, wxBITMAP_TYPE_PNG)
elif ext == 'jpg':
image = wxImage(file_nm, wxBITMAP_TYPE_JPEG)
else:
image = None
return image
class ImageView:
def __init__(self, iparent, ipos, isize):
self.win = iparent
self.ImagePanel = wxPanel(size = isize, parent = iparent, id = -1, name = 'backgroundPanel', style = wxSIMPLE_BORDER | wxCLIP_CHILDREN, pos= ipos)
self.clear = wxColour(255, 255, 255)
self.ImagePanel.SetBackgroundColour(self.clear) # clear the panel
self.image_sizex = isize.width
self.image_sizey = isize.height
self.image_posx = ipos.x
self.image_posy = ipos.y
wxInitAllImageHandlers()
def SetValue(self, file_nm): # display the selected file in the panel
image = ConvertBMP(file_nm).ConvertToBitmap()
if image is None:
return
iwidth = image.GetWidth() # dimensions of image file
iheight = image.GetHeight()
diffx = (self.image_sizex - iwidth)/2 # center calc
if iwidth >= self.image_sizex -10: # if image width fits in window adjust
diffx = 5
iwidth = self.image_sizex - 10
diffy = (self.image_sizey - iheight)/2 # center calc
if iheight >= self.image_sizey - 10: # if image height fits in window adjust
diffy = 5
iheight = self.image_sizey - 10
self.ClearPanel()
wxStaticBitmap(self.win, -1, image, wxPoint(self.image_posx+diffx, self.image_posy+diffy), wxSize(iwidth, iheight ))
def ClearPanel(self): # clear the image panel
self.ImagePanel.SetBackgroundColour(self.clear)
self.ImagePanel.Refresh()
class ImageDialog(wxDialog):
def __init__(self, parent, set_dir = None):
wxDialog.__init__(self, parent, -1, "Image Browser", wxPyDefaultPosition, wxSize(400, 400))
self.x_pos = 30 # initial display positions
self.y_pos = 20
self.delta = 20
size = wxSize(80, 25)
self.set_dir = os.getcwd()
if set_dir != None:
if os.path.exists(set_dir): # set to working directory if nothing set
self.set_dir = set_dir
self.dir_x = self.x_pos
self.dir_y = self.y_pos
self.DisplayDir() # display the directory value
self.y_pos = self.y_pos + self.delta
mID = NewId()
wxButton(self, mID, ' Set Directory ', wxPoint(self.x_pos, self.y_pos), size).SetDefault()
EVT_BUTTON(self, mID, self.SetDirect)
self.type_posy = self.y_pos # save the y position for the image type combo
self.fl_ext = '*.bmp' # initial setting for file filtering
self.GetFiles() # get the file list
self.y_pos = self.y_pos + self.delta + 10
self.list_height = 150
# List of Labels
mID = NewId()
self.tb = tb = wxListBox(self, mID, wxPoint(self.x_pos, self.y_pos), wxSize(160, self.list_height), self.fl_list, wxLB_SINGLE)
EVT_LISTBOX(self, mID, self.OnListClick)
EVT_LISTBOX_DCLICK(self, mID, self.OnListDClick)
width, height = self.tb.GetSizeTuple()
image_posx = self.x_pos + width + 20 # positions for setting the image window
image_posy = self.y_pos
image_sizex = 150
image_sizey = self.list_height
self.fl_types = ["Bmp", "Gif", "Png", "Jpg"]
self.fl_ext_types = { "Bmp": "*.bmp", "Gif": "*.gif", "Png": "*.png", "Jpg": "*.jpg" }
self.set_type = self.fl_types[0] # initial file filter setting
mID = NewId()
self.sel_type = wxComboBox(self, mID, self.set_type, wxPoint(image_posx , self.type_posy), wxSize(150, -1), self.fl_types, wxCB_DROPDOWN)
EVT_COMBOBOX(self, mID, self.OnSetType)
self.image_view = ImageView(self, wxPoint(image_posx, image_posy), wxSize(image_sizex, image_sizey))
self.y_pos = self.y_pos + height + 20
mID = NewId()
wxButton(self, mID, ' Select ', wxPoint(100, self.y_pos), size).SetDefault()
EVT_BUTTON(self, mID, self.OnOk)
wxButton(self, wxID_CANCEL, 'Cancel', wxPoint(250, self.y_pos), size)
self.y_pos = self.y_pos + self.delta
fsize = wxSize(400, self.y_pos + 50) # resize dialog for final vertical position
self.SetSize(fsize)
self.ResetFiles()
def GetFiles(self): # get the file list using directory and extension values
self.fl_val = FindFiles(self, self.set_dir, self.fl_ext)
self.fl_list = self.fl_val.files
def DisplayDir(self): # display the working directory
wxStaticText(self, -1, self.set_dir, wxPoint(self.dir_x, self.dir_y), wxSize(250, -1))
def OnSetType(self, event):
val = event.GetString() # get file type value
self.fl_ext = self.fl_ext_types[val]
self.ResetFiles()
def OnListDClick(self, event):
self.OnOk(0)
def OnListClick(self, event):
val = event.GetSelection()
self.SetListValue(val)
def SetListValue(self, val):
file_nm = self.fl_list[val]
self.set_file = file_val = os.path.join(self.set_dir, file_nm)
self.image_view.SetValue(file_val)
def SetDirect(self, event): # set the new directory
dlg = wxDirDialog(self)
dlg.SetPath(self.set_dir)
if dlg.ShowModal() == wxID_OK:
self.set_dir = dlg.GetPath()
self.ResetFiles()
dlg.Destroy()
def ResetFiles(self): # refresh the display with files and initial image
self.image_view.ClearPanel()
self.DisplayDir()
self.GetFiles()
self.tb.Set(self.fl_list)
try:
self.tb.SetSelection(0)
self.SetListValue(0)
except:
pass
def GetFile(self):
return self.set_file
def OnCancel(self, event):
self.result = None
self.EndModal(wxID_CANCEL)
def OnOk(self, event):
self.result = self.set_file
self.EndModal(wxID_OK)
def OnFileDlg(self):
dlg = wxFileDialog(self, "Choose an Image File", ".", "", "Bmp (*.bmp)|*.bmp|JPEG (*.jpg)|*.jpg", wxOPEN)
if dlg.ShowModal() == wxID_OK:
path = dlg.GetPath()
else:
path = None
dlg.Destroy()
return path
class FindFiles:
def __init__(self, parent, dir, mask):
filelist = []
dirlist = [".."]
self.dir = dir
self.file = ""
mask = string.upper(mask)
self.MakeRegex(mask)
for i in os.listdir(dir):
if i == "." or i == "..":
continue
path = os.path.join(dir, i)
path = string.upper(path)
value = string.upper(i)
if self.regex.match(value) == len(value):
filelist.append(i)
self.files = filelist
def MakeRegex(self, pattern):
import regex
f = "" # Set up a regex for file names
for ch in pattern:
if ch == "*":
f = f + ".*"
elif ch == ".":
f = f + "\."
elif ch == "?":
f = f + "."
else:
f = f + ch
self.regex = regex.compile(f)
def StripExt(self, file_nm):
fl_fld = os.path.splitext(file_nm)
fl_name = fl_fld[0]
ext = fl_fld[1]
return ext[1:]