mirror of
https://github.com/wxWidgets/Phoenix.git
synced 2026-01-05 03:20:08 +01:00
Phoenix patch from Werner for imagebrowser.
- corrected all deprecation warnings - wrapped all user visible strings with wx.GetTranslation macro - reformatted existing doc to Sphinx format - added a little bit of doc - added a unittest git-svn-id: https://svn.wxwidgets.org/svn/wx/wxPython/Phoenix/trunk@73800 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -40,13 +40,46 @@
|
||||
# Sort the file list
|
||||
# Use newer "re" function for patterns
|
||||
#
|
||||
|
||||
# Tags: phoenix-port, documented, unittest
|
||||
#---------------------------------------------------------------------------
|
||||
"""
|
||||
This module provides the :class:`ImageDialog` which allows to view and select
|
||||
an image.
|
||||
|
||||
|
||||
Description
|
||||
===========
|
||||
|
||||
The :class:`ImageDialog` allows the user to view images and select one.
|
||||
|
||||
Usage
|
||||
=====
|
||||
|
||||
A simple usage would be::
|
||||
|
||||
import wx
|
||||
import wx.lib.mixins.inspection as wit
|
||||
import wx.lib.imagebrowser as ib
|
||||
|
||||
app = wit.InspectableApp()
|
||||
|
||||
with ib.ImageDialog(None) as dlg:
|
||||
if dlg.ShowModal() == wx.ID_OK:
|
||||
# show the selected file
|
||||
print "You Selected File: " + dlg.GetFile()
|
||||
else:
|
||||
print "You pressed Cancel\n"
|
||||
|
||||
app.MainLoop()
|
||||
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import wx
|
||||
|
||||
_ = wx.GetTranslation
|
||||
|
||||
#---------------------------------------------------------------------------
|
||||
|
||||
BAD_IMAGE = -1
|
||||
@@ -59,6 +92,13 @@ ID_BOX_FRAME = wx.NewId()
|
||||
ID_CROP_FRAME = wx.NewId()
|
||||
|
||||
def ConvertBMP(file_nm):
|
||||
"""
|
||||
Convert file
|
||||
|
||||
:param string `file_nm`: path to file
|
||||
|
||||
:return: :class:`Image`: or BAD_IMAGE
|
||||
"""
|
||||
if file_nm is None:
|
||||
return None
|
||||
|
||||
@@ -76,17 +116,19 @@ def ConvertBMP(file_nm):
|
||||
return BAD_IMAGE
|
||||
|
||||
|
||||
def GetCheckeredBitmap(blocksize=8,ntiles=4,rgb0='\xFF', rgb1='\xCC'):
|
||||
"""Creates a square RGB checkered bitmap using the two specified colors.
|
||||
|
||||
Inputs:
|
||||
|
||||
- blocksize: the number of pixels in each solid color square
|
||||
- ntiles: the number of tiles along width and height. Each tile is 2x2 blocks.
|
||||
- rbg0, rgb1: the first and second colors, as 3-byte strings.
|
||||
If only 1 byte is provided, it is treated as a grey value.
|
||||
def GetCheckeredBitmap(blocksize=8, ntiles=4, rgb0='\xFF', rgb1='\xCC'):
|
||||
"""
|
||||
Creates a square RGB checkered bitmap using the two specified colors.
|
||||
|
||||
The bitmap returned will have width = height = blocksize*ntiles*2
|
||||
|
||||
:param int `blocksize`: the number of pixels in each solid color square
|
||||
:param int `ntiles1: the number of tiles along width and height. Each tile is 2x2 blocks.
|
||||
:param `rbg0`: the first color, as 3-byte strings.
|
||||
:param `rgb1`: the second color, as 3-byte strings.
|
||||
If only 1 byte is provided, it is treated as a grey value.
|
||||
:return: :class:`BitmapFromBuffer`
|
||||
|
||||
"""
|
||||
size = blocksize*ntiles*2
|
||||
|
||||
@@ -319,25 +361,25 @@ class ImagePanel(wx.Panel):
|
||||
bmp = GetNamedBitmap('White')
|
||||
btn = wx.BitmapButton(self, ID_WHITE_BG, bmp, style=wx.BU_EXACTFIT)
|
||||
self.Bind(wx.EVT_BUTTON, self.OnSetImgBackground, btn)
|
||||
btn.SetToolTipString("Set background to white")
|
||||
btn.SetToolTip(_("Set background to white"))
|
||||
hbox_ctrls.Add(btn, 0, wx.ALIGN_LEFT|wx.LEFT, 4)
|
||||
|
||||
bmp = GetNamedBitmap('Grey')
|
||||
btn = wx.BitmapButton(self, ID_GREY_BG, bmp, style=wx.BU_EXACTFIT)
|
||||
self.Bind(wx.EVT_BUTTON, self.OnSetImgBackground, btn)
|
||||
btn.SetToolTipString("Set background to grey")
|
||||
btn.SetToolTip(_("Set background to grey"))
|
||||
hbox_ctrls.Add(btn, 0, wx.ALIGN_LEFT|wx.LEFT, 4)
|
||||
|
||||
bmp = GetNamedBitmap('Black')
|
||||
btn = wx.BitmapButton(self, ID_BLACK_BG, bmp, style=wx.BU_EXACTFIT)
|
||||
self.Bind(wx.EVT_BUTTON, self.OnSetImgBackground, btn)
|
||||
btn.SetToolTipString("Set background to black")
|
||||
btn.SetToolTip(_("Set background to black"))
|
||||
hbox_ctrls.Add(btn, 0, wx.ALIGN_LEFT|wx.LEFT, 4)
|
||||
|
||||
bmp = GetNamedBitmap('Checked')
|
||||
btn = wx.BitmapButton(self, ID_CHECK_BG, bmp, style=wx.BU_EXACTFIT)
|
||||
self.Bind(wx.EVT_BUTTON, self.OnSetImgBackground, btn)
|
||||
btn.SetToolTipString("Set background to chekered pattern")
|
||||
btn.SetToolTip(_("Set background to chekered pattern"))
|
||||
hbox_ctrls.Add(btn, 0, wx.ALIGN_LEFT|wx.LEFT, 4)
|
||||
|
||||
|
||||
@@ -346,19 +388,19 @@ class ImagePanel(wx.Panel):
|
||||
bmp = GetNamedBitmap('NoFrame')
|
||||
btn = wx.BitmapButton(self, ID_NO_FRAME, bmp, style=wx.BU_EXACTFIT)
|
||||
self.Bind(wx.EVT_BUTTON, self.OnSetBorderMode, btn)
|
||||
btn.SetToolTipString("No framing around image")
|
||||
btn.SetToolTip(_("No framing around image"))
|
||||
hbox_ctrls.Add(btn, 0, wx.ALIGN_LEFT|wx.LEFT, 4)
|
||||
|
||||
bmp = GetNamedBitmap('BoxFrame')
|
||||
btn = wx.BitmapButton(self, ID_BOX_FRAME, bmp, style=wx.BU_EXACTFIT)
|
||||
self.Bind(wx.EVT_BUTTON, self.OnSetBorderMode, btn)
|
||||
btn.SetToolTipString("Frame image with a box")
|
||||
btn.SetToolTip(_("Frame image with a box"))
|
||||
hbox_ctrls.Add(btn, 0, wx.ALIGN_LEFT|wx.LEFT, 4)
|
||||
|
||||
bmp = GetNamedBitmap('CropFrame')
|
||||
btn = wx.BitmapButton(self, ID_CROP_FRAME, bmp, style=wx.BU_EXACTFIT|wx.BORDER_SIMPLE)
|
||||
self.Bind(wx.EVT_BUTTON, self.OnSetBorderMode, btn)
|
||||
btn.SetToolTipString("Frame image with a dimmed background")
|
||||
btn.SetToolTip(_("Frame image with a dimmed background"))
|
||||
hbox_ctrls.Add(btn, 0, wx.ALIGN_LEFT|wx.LEFT, 4)
|
||||
|
||||
|
||||
@@ -382,8 +424,21 @@ class ImagePanel(wx.Panel):
|
||||
|
||||
|
||||
class ImageDialog(wx.Dialog):
|
||||
"""
|
||||
:class:`ImageDialog` derived from :class:`Dialog` allows the user
|
||||
to display images and to select an image.
|
||||
"""
|
||||
def __init__(self, parent, set_dir = None):
|
||||
wx.Dialog.__init__(self, parent, -1, "Image Browser", wx.DefaultPosition, (400, 400),style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)
|
||||
"""
|
||||
Default class constructor.
|
||||
|
||||
:param Window `parent`: parent window.
|
||||
:param string `set_dir`: path to set as working directory
|
||||
|
||||
"""
|
||||
wx.Dialog.__init__(self, parent, -1, _("Image Browser"),
|
||||
wx.DefaultPosition, (400, 400),
|
||||
style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)
|
||||
|
||||
self.set_dir = os.getcwd()
|
||||
self.set_file = None
|
||||
@@ -407,15 +462,15 @@ class ImageDialog(wx.Dialog):
|
||||
|
||||
up_bmp = wx.ArtProvider.GetBitmap(wx.ART_GO_DIR_UP, wx.ART_BUTTON, (16,16))
|
||||
btn = wx.BitmapButton(self, -1, up_bmp)
|
||||
btn.SetHelpText("Up one level")
|
||||
btn.SetToolTipString("Up one level")
|
||||
btn.SetHelpText(_("Up one level"))
|
||||
btn.SetToolTip(_("Up one level"))
|
||||
self.Bind(wx.EVT_BUTTON, self.OnUpDirectory, btn)
|
||||
hbox_loc.Add(btn, 0, wx.ALIGN_CENTER_VERTICAL|wx.RIGHT, 2)
|
||||
|
||||
folder_bmp = wx.ArtProvider.GetBitmap(wx.ART_FOLDER_OPEN, wx.ART_BUTTON, (16,16))
|
||||
btn = wx.BitmapButton(self, -1, folder_bmp)
|
||||
btn.SetHelpText("Browse for a &folder...")
|
||||
btn.SetToolTipString("Browse for a folder...")
|
||||
btn.SetHelpText(_("Browse for a &folder..."))
|
||||
btn.SetToolTip(_("Browse for a folder..."))
|
||||
self.Bind(wx.EVT_BUTTON, self.OnChooseDirectory, btn)
|
||||
hbox_loc.Add(btn, 0, wx.ALIGN_CENTER_VERTICAL|wx.RIGHT, 5)
|
||||
|
||||
@@ -496,7 +551,17 @@ class ImageDialog(wx.Dialog):
|
||||
self.ResetFiles()
|
||||
|
||||
def ChangeFileTypes(self, ft_tuple):
|
||||
# Change list of file types to be supported
|
||||
"""
|
||||
Change list of file types to be supported
|
||||
|
||||
:param tuple `ft_tuple`: tuple of files types, e.g. to support just
|
||||
.gif and .png you would use:
|
||||
|
||||
(("GIF (*.gif)", "*.gif"),
|
||||
("PNG (*.png)", "*.png"))
|
||||
|
||||
"""
|
||||
|
||||
self.fl_ext_types = ft_tuple
|
||||
self.set_type, self.fl_ext = self.fl_ext_types[0] # initial file filter setting
|
||||
self.fl_types = [ x[0] for x in self.fl_ext_types ]
|
||||
@@ -505,7 +570,8 @@ class ImageDialog(wx.Dialog):
|
||||
self.sel_type.SetSelection(0)
|
||||
self.fl_ext_types = dict(self.fl_ext_types)
|
||||
|
||||
def GetFiles(self): # get the file list using directory and extension values
|
||||
def GetFiles(self):
|
||||
# get the file list using directory and extension values
|
||||
if self.fl_ext == "All":
|
||||
all_files = []
|
||||
|
||||
@@ -530,7 +596,8 @@ class ImageDialog(wx.Dialog):
|
||||
self.fl_ndirs = len(self.fl_val.dirs)
|
||||
self.fl_list = sorted(self.fl_val.dirs) + self.fl_list
|
||||
|
||||
def DisplayDir(self): # display the working directory
|
||||
def DisplayDir(self):
|
||||
# display the working directory
|
||||
if self.dir:
|
||||
ipt = self.dir.GetInsertionPoint()
|
||||
self.dir.SetValue(self.set_dir)
|
||||
@@ -584,7 +651,8 @@ class ImageDialog(wx.Dialog):
|
||||
self.set_dir = sdir
|
||||
self.ResetFiles()
|
||||
|
||||
def OnChooseDirectory(self, event): # set the new directory
|
||||
def OnChooseDirectory(self, event):
|
||||
# set the new directory
|
||||
dlg = wx.DirDialog(self)
|
||||
dlg.SetPath(self.set_dir)
|
||||
|
||||
@@ -594,7 +662,8 @@ class ImageDialog(wx.Dialog):
|
||||
|
||||
dlg.Destroy()
|
||||
|
||||
def ResetFiles(self): # refresh the display with files and initial image
|
||||
def ResetFiles(self):
|
||||
# refresh the display with files and initial image
|
||||
self.DisplayDir()
|
||||
self.GetFiles()
|
||||
|
||||
@@ -632,9 +701,20 @@ class ImageDialog(wx.Dialog):
|
||||
self.image_view.SetValue(None)
|
||||
|
||||
def GetFile(self):
|
||||
"""
|
||||
Get selected file
|
||||
|
||||
:return: File selected or None
|
||||
"""
|
||||
return self.set_file
|
||||
|
||||
def GetDirectory(self):
|
||||
"""
|
||||
Get directory
|
||||
|
||||
:return: get the current directory
|
||||
|
||||
"""
|
||||
return self.set_dir
|
||||
|
||||
def OnCancel(self, event):
|
||||
|
||||
Reference in New Issue
Block a user