2001-09-06 17:19:48 -04:00
|
|
|
""" pySketch
|
|
|
|
|
|
|
|
A simple object-oriented drawing program.
|
|
|
|
|
|
|
|
This is completely free software; please feel free to adapt or use this in
|
|
|
|
any way you like.
|
|
|
|
|
|
|
|
Author: Erik Westra (ewestra@wave.co.nz)
|
|
|
|
|
|
|
|
#########################################################################
|
|
|
|
|
|
|
|
NOTE
|
|
|
|
|
|
|
|
pySketch requires wxPython version 2.3. If you are running an earlier
|
|
|
|
version, you need to patch your copy of wxPython to fix a bug which will
|
|
|
|
cause the "Edit Text Object" dialog box to crash.
|
|
|
|
|
|
|
|
To patch an earlier version of wxPython, edit the wxPython/windows.py file,
|
|
|
|
find the wxPyValidator.__init__ method and change the line which reads:
|
|
|
|
|
|
|
|
self._setSelf(self, wxPyValidator, 0)
|
|
|
|
|
|
|
|
to:
|
|
|
|
|
|
|
|
self._setSelf(self, wxPyValidator, 1)
|
|
|
|
|
|
|
|
This fixes a known bug in wxPython 2.2.5 (and possibly earlier) which has
|
|
|
|
now been fixed in wxPython 2.3.
|
|
|
|
|
|
|
|
#########################################################################
|
|
|
|
|
|
|
|
TODO:
|
|
|
|
|
|
|
|
* Add ARGV checking to see if a document was double-clicked on.
|
|
|
|
|
|
|
|
Known Bugs:
|
|
|
|
|
|
|
|
* Scrolling the window causes the drawing panel to be mucked up until you
|
2003-03-25 01:35:27 -05:00
|
|
|
refresh it. I've got no idea why.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
* I suspect that the reference counting for some wxPoint objects is
|
|
|
|
getting mucked up; when the user quits, we get errors about being
|
2003-03-25 01:35:27 -05:00
|
|
|
unable to call del on a 'None' object.
|
2001-09-06 17:19:48 -04:00
|
|
|
"""
|
2004-03-12 15:03:37 -05:00
|
|
|
import sys
|
2003-03-25 01:35:27 -05:00
|
|
|
import cPickle, os.path
|
2004-08-25 15:57:40 -04:00
|
|
|
import wx
|
2005-05-24 16:55:42 -04:00
|
|
|
from wx.lib.buttons import GenBitmapButton
|
|
|
|
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
import traceback, types
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------------
|
|
|
|
# System Constants
|
|
|
|
#----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
# Our menu item IDs:
|
|
|
|
|
|
|
|
menu_UNDO = 10001 # Edit menu items.
|
|
|
|
menu_SELECT_ALL = 10002
|
|
|
|
menu_DUPLICATE = 10003
|
|
|
|
menu_EDIT_TEXT = 10004
|
|
|
|
menu_DELETE = 10005
|
|
|
|
|
|
|
|
menu_SELECT = 10101 # Tools menu items.
|
|
|
|
menu_LINE = 10102
|
|
|
|
menu_RECT = 10103
|
|
|
|
menu_ELLIPSE = 10104
|
|
|
|
menu_TEXT = 10105
|
|
|
|
|
|
|
|
menu_MOVE_FORWARD = 10201 # Object menu items.
|
|
|
|
menu_MOVE_TO_FRONT = 10202
|
|
|
|
menu_MOVE_BACKWARD = 10203
|
|
|
|
menu_MOVE_TO_BACK = 10204
|
|
|
|
|
|
|
|
menu_ABOUT = 10205 # Help menu items.
|
|
|
|
|
|
|
|
# Our tool IDs:
|
|
|
|
|
|
|
|
id_SELECT = 11001
|
|
|
|
id_LINE = 11002
|
|
|
|
id_RECT = 11003
|
|
|
|
id_ELLIPSE = 11004
|
|
|
|
id_TEXT = 11005
|
|
|
|
|
|
|
|
# Our tool option IDs:
|
|
|
|
|
|
|
|
id_FILL_OPT = 12001
|
|
|
|
id_PEN_OPT = 12002
|
|
|
|
id_LINE_OPT = 12003
|
|
|
|
|
|
|
|
id_LINESIZE_0 = 13001
|
|
|
|
id_LINESIZE_1 = 13002
|
|
|
|
id_LINESIZE_2 = 13003
|
|
|
|
id_LINESIZE_3 = 13004
|
|
|
|
id_LINESIZE_4 = 13005
|
|
|
|
id_LINESIZE_5 = 13006
|
|
|
|
|
|
|
|
# DrawObject type IDs:
|
|
|
|
|
|
|
|
obj_LINE = 1
|
|
|
|
obj_RECT = 2
|
|
|
|
obj_ELLIPSE = 3
|
|
|
|
obj_TEXT = 4
|
|
|
|
|
|
|
|
# Selection handle IDs:
|
|
|
|
|
|
|
|
handle_NONE = 1
|
|
|
|
handle_TOP_LEFT = 2
|
|
|
|
handle_TOP_RIGHT = 3
|
|
|
|
handle_BOTTOM_LEFT = 4
|
|
|
|
handle_BOTTOM_RIGHT = 5
|
|
|
|
handle_START_POINT = 6
|
|
|
|
handle_END_POINT = 7
|
|
|
|
|
|
|
|
# Dragging operations:
|
|
|
|
|
|
|
|
drag_NONE = 1
|
|
|
|
drag_RESIZE = 2
|
|
|
|
drag_MOVE = 3
|
|
|
|
drag_DRAG = 4
|
|
|
|
|
|
|
|
# Visual Feedback types:
|
|
|
|
|
|
|
|
feedback_LINE = 1
|
|
|
|
feedback_RECT = 2
|
|
|
|
feedback_ELLIPSE = 3
|
|
|
|
|
|
|
|
# Mouse-event action parameter types:
|
|
|
|
|
|
|
|
param_RECT = 1
|
|
|
|
param_LINE = 2
|
|
|
|
|
|
|
|
# Size of the drawing page, in pixels.
|
|
|
|
|
|
|
|
PAGE_WIDTH = 1000
|
|
|
|
PAGE_HEIGHT = 1000
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------------
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
class DrawingFrame(wx.Frame):
|
2001-09-06 17:19:48 -04:00
|
|
|
""" A frame showing the contents of a single document. """
|
|
|
|
|
|
|
|
# ==========================================
|
|
|
|
# == Initialisation and Window Management ==
|
|
|
|
# ==========================================
|
|
|
|
|
|
|
|
def __init__(self, parent, id, title, fileName=None):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Standard constructor.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
'parent', 'id' and 'title' are all passed to the standard wx.Frame
|
2003-03-25 01:35:27 -05:00
|
|
|
constructor. 'fileName' is the name and path of a saved file to
|
|
|
|
load into this frame, if any.
|
|
|
|
"""
|
2004-08-25 15:57:40 -04:00
|
|
|
wx.Frame.__init__(self, parent, id, title,
|
|
|
|
style = wx.DEFAULT_FRAME_STYLE | wx.WANTS_CHARS |
|
|
|
|
wx.NO_FULL_REPAINT_ON_RESIZE)
|
2003-03-25 01:35:27 -05:00
|
|
|
|
|
|
|
# Setup our menu bar.
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
menuBar = wx.MenuBar()
|
2003-03-25 01:35:27 -05:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
self.fileMenu = wx.Menu()
|
|
|
|
self.fileMenu.Append(wx.ID_NEW, "New\tCTRL-N")
|
|
|
|
self.fileMenu.Append(wx.ID_OPEN, "Open...\tCTRL-O")
|
|
|
|
self.fileMenu.Append(wx.ID_CLOSE, "Close\tCTRL-W")
|
2003-03-25 01:35:27 -05:00
|
|
|
self.fileMenu.AppendSeparator()
|
2004-08-25 15:57:40 -04:00
|
|
|
self.fileMenu.Append(wx.ID_SAVE, "Save\tCTRL-S")
|
|
|
|
self.fileMenu.Append(wx.ID_SAVEAS, "Save As...")
|
|
|
|
self.fileMenu.Append(wx.ID_REVERT, "Revert...")
|
2003-03-25 01:35:27 -05:00
|
|
|
self.fileMenu.AppendSeparator()
|
2004-08-25 15:57:40 -04:00
|
|
|
self.fileMenu.Append(wx.ID_EXIT, "Quit\tCTRL-Q")
|
2003-03-25 01:35:27 -05:00
|
|
|
|
|
|
|
menuBar.Append(self.fileMenu, "File")
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
self.editMenu = wx.Menu()
|
2003-03-25 01:35:27 -05:00
|
|
|
self.editMenu.Append(menu_UNDO, "Undo\tCTRL-Z")
|
|
|
|
self.editMenu.AppendSeparator()
|
|
|
|
self.editMenu.Append(menu_SELECT_ALL, "Select All\tCTRL-A")
|
|
|
|
self.editMenu.AppendSeparator()
|
|
|
|
self.editMenu.Append(menu_DUPLICATE, "Duplicate\tCTRL-D")
|
|
|
|
self.editMenu.Append(menu_EDIT_TEXT, "Edit...\tCTRL-E")
|
|
|
|
self.editMenu.Append(menu_DELETE, "Delete\tDEL")
|
|
|
|
|
|
|
|
menuBar.Append(self.editMenu, "Edit")
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
self.toolsMenu = wx.Menu()
|
|
|
|
self.toolsMenu.Append(menu_SELECT, "Selection", kind=wx.ITEM_CHECK)
|
|
|
|
self.toolsMenu.Append(menu_LINE, "Line", kind=wx.ITEM_CHECK)
|
|
|
|
self.toolsMenu.Append(menu_RECT, "Rectangle", kind=wx.ITEM_CHECK)
|
|
|
|
self.toolsMenu.Append(menu_ELLIPSE, "Ellipse", kind=wx.ITEM_CHECK)
|
|
|
|
self.toolsMenu.Append(menu_TEXT, "Text", kind=wx.ITEM_CHECK)
|
2003-03-25 01:35:27 -05:00
|
|
|
|
|
|
|
menuBar.Append(self.toolsMenu, "Tools")
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
self.objectMenu = wx.Menu()
|
2003-03-25 01:35:27 -05:00
|
|
|
self.objectMenu.Append(menu_MOVE_FORWARD, "Move Forward")
|
|
|
|
self.objectMenu.Append(menu_MOVE_TO_FRONT, "Move to Front\tCTRL-F")
|
|
|
|
self.objectMenu.Append(menu_MOVE_BACKWARD, "Move Backward")
|
|
|
|
self.objectMenu.Append(menu_MOVE_TO_BACK, "Move to Back\tCTRL-B")
|
|
|
|
|
|
|
|
menuBar.Append(self.objectMenu, "Object")
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
self.helpMenu = wx.Menu()
|
2003-03-25 01:35:27 -05:00
|
|
|
self.helpMenu.Append(menu_ABOUT, "About pySketch...")
|
|
|
|
|
|
|
|
menuBar.Append(self.helpMenu, "Help")
|
|
|
|
|
|
|
|
self.SetMenuBar(menuBar)
|
|
|
|
|
|
|
|
# Create our toolbar.
|
|
|
|
|
2005-05-24 16:55:42 -04:00
|
|
|
tsize = (16,16)
|
2004-08-25 15:57:40 -04:00
|
|
|
self.toolbar = self.CreateToolBar(wx.TB_HORIZONTAL |
|
|
|
|
wx.NO_BORDER | wx.TB_FLAT)
|
2003-03-25 01:35:27 -05:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
self.toolbar.AddSimpleTool(wx.ID_NEW,
|
2005-05-26 20:55:06 -04:00
|
|
|
wx.ArtProvider.GetBitmap(wx.ART_NEW, wx.ART_TOOLBAR, tsize),
|
2003-03-25 01:35:27 -05:00
|
|
|
"New")
|
2004-08-25 15:57:40 -04:00
|
|
|
self.toolbar.AddSimpleTool(wx.ID_OPEN,
|
2005-05-24 16:55:42 -04:00
|
|
|
wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN, wx.ART_TOOLBAR, tsize),
|
2003-03-25 01:35:27 -05:00
|
|
|
"Open")
|
2004-08-25 15:57:40 -04:00
|
|
|
self.toolbar.AddSimpleTool(wx.ID_SAVE,
|
2005-05-24 16:55:42 -04:00
|
|
|
wx.ArtProvider.GetBitmap(wx.ART_FILE_SAVE, wx.ART_TOOLBAR, tsize),
|
2003-03-25 01:35:27 -05:00
|
|
|
"Save")
|
|
|
|
self.toolbar.AddSeparator()
|
|
|
|
self.toolbar.AddSimpleTool(menu_UNDO,
|
2005-05-24 16:55:42 -04:00
|
|
|
wx.ArtProvider.GetBitmap(wx.ART_UNDO, wx.ART_TOOLBAR, tsize),
|
2003-03-25 01:35:27 -05:00
|
|
|
"Undo")
|
|
|
|
self.toolbar.AddSeparator()
|
|
|
|
self.toolbar.AddSimpleTool(menu_DUPLICATE,
|
2004-08-25 15:57:40 -04:00
|
|
|
wx.Bitmap("images/duplicate.bmp",
|
|
|
|
wx.BITMAP_TYPE_BMP),
|
2003-03-25 01:35:27 -05:00
|
|
|
"Duplicate")
|
|
|
|
self.toolbar.AddSeparator()
|
|
|
|
self.toolbar.AddSimpleTool(menu_MOVE_FORWARD,
|
2004-08-25 15:57:40 -04:00
|
|
|
wx.Bitmap("images/moveForward.bmp",
|
|
|
|
wx.BITMAP_TYPE_BMP),
|
2003-03-25 01:35:27 -05:00
|
|
|
"Move Forward")
|
|
|
|
self.toolbar.AddSimpleTool(menu_MOVE_BACKWARD,
|
2004-08-25 15:57:40 -04:00
|
|
|
wx.Bitmap("images/moveBack.bmp",
|
|
|
|
wx.BITMAP_TYPE_BMP),
|
2003-03-25 01:35:27 -05:00
|
|
|
"Move Backward")
|
|
|
|
|
|
|
|
self.toolbar.Realize()
|
|
|
|
|
|
|
|
# Associate each menu/toolbar item with the method that handles that
|
|
|
|
# item.
|
2004-08-25 15:57:40 -04:00
|
|
|
menuHandlers = [
|
|
|
|
(wx.ID_NEW, self.doNew),
|
|
|
|
(wx.ID_OPEN, self.doOpen),
|
|
|
|
(wx.ID_CLOSE, self.doClose),
|
|
|
|
(wx.ID_SAVE, self.doSave),
|
|
|
|
(wx.ID_SAVEAS, self.doSaveAs),
|
|
|
|
(wx.ID_REVERT, self.doRevert),
|
|
|
|
(wx.ID_EXIT, self.doExit),
|
|
|
|
|
|
|
|
(menu_UNDO, self.doUndo),
|
|
|
|
(menu_SELECT_ALL, self.doSelectAll),
|
|
|
|
(menu_DUPLICATE, self.doDuplicate),
|
|
|
|
(menu_EDIT_TEXT, self.doEditText),
|
|
|
|
(menu_DELETE, self.doDelete),
|
|
|
|
|
|
|
|
(menu_SELECT, self.doChooseSelectTool),
|
|
|
|
(menu_LINE, self.doChooseLineTool),
|
|
|
|
(menu_RECT, self.doChooseRectTool),
|
|
|
|
(menu_ELLIPSE, self.doChooseEllipseTool),
|
|
|
|
(menu_TEXT, self.doChooseTextTool),
|
|
|
|
|
|
|
|
(menu_MOVE_FORWARD, self.doMoveForward),
|
|
|
|
(menu_MOVE_TO_FRONT, self.doMoveToFront),
|
|
|
|
(menu_MOVE_BACKWARD, self.doMoveBackward),
|
|
|
|
(menu_MOVE_TO_BACK, self.doMoveToBack),
|
|
|
|
|
|
|
|
(menu_ABOUT, self.doShowAbout)]
|
|
|
|
for combo in menuHandlers:
|
|
|
|
id, handler = combo
|
|
|
|
self.Bind(wx.EVT_MENU, handler, id = id)
|
|
|
|
|
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
# Install our own method to handle closing the window. This allows us
|
|
|
|
# to ask the user if he/she wants to save before closing the window, as
|
|
|
|
# well as keeping track of which windows are currently open.
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
self.Bind(wx.EVT_CLOSE, self.doClose)
|
2003-03-25 01:35:27 -05:00
|
|
|
|
|
|
|
# Install our own method for handling keystrokes. We use this to let
|
|
|
|
# the user move the selected object(s) around using the arrow keys.
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
self.Bind(wx.EVT_CHAR_HOOK, self.onKeyEvent)
|
2003-03-25 01:35:27 -05:00
|
|
|
|
|
|
|
# Setup our top-most panel. This holds the entire contents of the
|
|
|
|
# window, excluding the menu bar.
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
self.topPanel = wx.Panel(self, -1, style=wx.SIMPLE_BORDER)
|
2003-03-25 01:35:27 -05:00
|
|
|
|
|
|
|
# Setup our tool palette, with all our drawing tools and option icons.
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
self.toolPalette = wx.BoxSizer(wx.VERTICAL)
|
2003-03-25 01:35:27 -05:00
|
|
|
|
|
|
|
self.selectIcon = ToolPaletteIcon(self.topPanel, id_SELECT,
|
|
|
|
"select", "Selection Tool")
|
|
|
|
self.lineIcon = ToolPaletteIcon(self.topPanel, id_LINE,
|
|
|
|
"line", "Line Tool")
|
|
|
|
self.rectIcon = ToolPaletteIcon(self.topPanel, id_RECT,
|
|
|
|
"rect", "Rectangle Tool")
|
|
|
|
self.ellipseIcon = ToolPaletteIcon(self.topPanel, id_ELLIPSE,
|
|
|
|
"ellipse", "Ellipse Tool")
|
|
|
|
self.textIcon = ToolPaletteIcon(self.topPanel, id_TEXT,
|
|
|
|
"text", "Text Tool")
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
toolSizer = wx.GridSizer(0, 2, 5, 5)
|
2003-03-25 01:35:27 -05:00
|
|
|
toolSizer.Add(self.selectIcon)
|
2004-03-12 15:03:37 -05:00
|
|
|
toolSizer.Add((0, 0)) # Gap to make tool icons line up nicely.
|
2003-03-25 01:35:27 -05:00
|
|
|
toolSizer.Add(self.lineIcon)
|
|
|
|
toolSizer.Add(self.rectIcon)
|
|
|
|
toolSizer.Add(self.ellipseIcon)
|
|
|
|
toolSizer.Add(self.textIcon)
|
|
|
|
|
|
|
|
self.optionIndicator = ToolOptionIndicator(self.topPanel)
|
|
|
|
self.optionIndicator.SetToolTip(
|
2004-08-25 15:57:40 -04:00
|
|
|
wx.ToolTip("Shows Current Pen/Fill/Line Size Settings"))
|
2003-03-25 01:35:27 -05:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
optionSizer = wx.BoxSizer(wx.HORIZONTAL)
|
2003-03-25 01:35:27 -05:00
|
|
|
|
|
|
|
self.penOptIcon = ToolPaletteIcon(self.topPanel, id_PEN_OPT,
|
|
|
|
"penOpt", "Set Pen Colour")
|
|
|
|
self.fillOptIcon = ToolPaletteIcon(self.topPanel, id_FILL_OPT,
|
|
|
|
"fillOpt", "Set Fill Colour")
|
|
|
|
self.lineOptIcon = ToolPaletteIcon(self.topPanel, id_LINE_OPT,
|
|
|
|
"lineOpt", "Set Line Size")
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
margin = wx.LEFT | wx.RIGHT
|
2003-03-25 01:35:27 -05:00
|
|
|
optionSizer.Add(self.penOptIcon, 0, margin, 1)
|
|
|
|
optionSizer.Add(self.fillOptIcon, 0, margin, 1)
|
|
|
|
optionSizer.Add(self.lineOptIcon, 0, margin, 1)
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
margin = wx.TOP | wx.LEFT | wx.RIGHT | wx.ALIGN_CENTRE
|
2003-03-25 01:35:27 -05:00
|
|
|
self.toolPalette.Add(toolSizer, 0, margin, 5)
|
2004-03-12 15:03:37 -05:00
|
|
|
self.toolPalette.Add((0, 0), 0, margin, 5) # Spacer.
|
2003-03-25 01:35:27 -05:00
|
|
|
self.toolPalette.Add(self.optionIndicator, 0, margin, 5)
|
|
|
|
self.toolPalette.Add(optionSizer, 0, margin, 5)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
# Make the tool palette icons respond when the user clicks on them.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
self.selectIcon.Bind(wx.EVT_BUTTON, self.onToolIconClick)
|
|
|
|
self.lineIcon.Bind(wx.EVT_BUTTON, self.onToolIconClick)
|
|
|
|
self.rectIcon.Bind(wx.EVT_BUTTON, self.onToolIconClick)
|
|
|
|
self.ellipseIcon.Bind(wx.EVT_BUTTON, self.onToolIconClick)
|
|
|
|
self.textIcon.Bind(wx.EVT_BUTTON, self.onToolIconClick)
|
|
|
|
self.penOptIcon.Bind(wx.EVT_BUTTON, self.onPenOptionIconClick)
|
|
|
|
self.fillOptIcon.Bind(wx.EVT_BUTTON, self.onFillOptionIconClick)
|
|
|
|
self.lineOptIcon.Bind(wx.EVT_BUTTON, self.onLineOptionIconClick)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
# Setup the main drawing area.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
self.drawPanel = wx.ScrolledWindow(self.topPanel, -1,
|
|
|
|
style=wx.SUNKEN_BORDER)
|
|
|
|
self.drawPanel.SetBackgroundColour(wx.WHITE)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self.drawPanel.EnableScrolling(True, True)
|
|
|
|
self.drawPanel.SetScrollbars(20, 20, PAGE_WIDTH / 20, PAGE_HEIGHT / 20)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
self.drawPanel.Bind(wx.EVT_LEFT_DOWN, self.onMouseEvent)
|
|
|
|
self.drawPanel.Bind(wx.EVT_LEFT_DCLICK, self.onDoubleClickEvent)
|
|
|
|
self.drawPanel.Bind(wx.EVT_RIGHT_DOWN, self.onRightClick)
|
|
|
|
self.drawPanel.Bind(wx.EVT_MOTION, self.onMouseEvent)
|
|
|
|
self.drawPanel.Bind(wx.EVT_LEFT_UP, self.onMouseEvent)
|
|
|
|
self.drawPanel.Bind(wx.EVT_PAINT, self.onPaintEvent)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
# Position everything in the window.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
topSizer = wx.BoxSizer(wx.HORIZONTAL)
|
2003-03-25 01:35:27 -05:00
|
|
|
topSizer.Add(self.toolPalette, 0)
|
2004-08-25 15:57:40 -04:00
|
|
|
topSizer.Add(self.drawPanel, 1, wx.EXPAND)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self.topPanel.SetAutoLayout(True)
|
|
|
|
self.topPanel.SetSizer(topSizer)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2004-07-06 17:00:59 -04:00
|
|
|
self.SetSizeHints(250, 200)
|
2004-08-25 15:57:40 -04:00
|
|
|
self.SetSize(wx.Size(600, 400))
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
# Select an initial tool.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self.curTool = None
|
|
|
|
self._setCurrentTool(self.selectIcon)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
# Setup our frame to hold the contents of a sketch document.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self.dirty = False
|
|
|
|
self.fileName = fileName
|
|
|
|
self.contents = [] # front-to-back ordered list of DrawingObjects.
|
|
|
|
self.selection = [] # List of selected DrawingObjects.
|
|
|
|
self.undoInfo = None # Saved contents for undo.
|
|
|
|
self.dragMode = drag_NONE # Current mouse-drag mode.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
if self.fileName != None:
|
|
|
|
self.loadContents()
|
|
|
|
|
|
|
|
self._adjustMenus()
|
|
|
|
|
|
|
|
# Finally, set our initial pen, fill and line options.
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
self.penColour = wx.BLACK
|
|
|
|
self.fillColour = wx.WHITE
|
2003-03-25 01:35:27 -05:00
|
|
|
self.lineSize = 1
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
# ============================
|
|
|
|
# == Event Handling Methods ==
|
|
|
|
# ============================
|
|
|
|
|
|
|
|
def onToolIconClick(self, event):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Respond to the user clicking on one of our tool icons.
|
|
|
|
"""
|
2004-03-12 15:03:37 -05:00
|
|
|
iconID = event.GetEventObject().GetId()
|
2003-03-25 01:35:27 -05:00
|
|
|
if iconID == id_SELECT: self.doChooseSelectTool()
|
|
|
|
elif iconID == id_LINE: self.doChooseLineTool()
|
|
|
|
elif iconID == id_RECT: self.doChooseRectTool()
|
|
|
|
elif iconID == id_ELLIPSE: self.doChooseEllipseTool()
|
|
|
|
elif iconID == id_TEXT: self.doChooseTextTool()
|
2004-08-25 15:57:40 -04:00
|
|
|
else: wx.Bell(); print "1"
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def onPenOptionIconClick(self, event):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Respond to the user clicking on the "Pen Options" icon.
|
|
|
|
"""
|
2004-08-25 15:57:40 -04:00
|
|
|
data = wx.ColourData()
|
2003-03-25 01:35:27 -05:00
|
|
|
if len(self.selection) == 1:
|
|
|
|
data.SetColour(self.selection[0].getPenColour())
|
|
|
|
else:
|
|
|
|
data.SetColour(self.penColour)
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
dialog = wx.ColourDialog(self, data)
|
|
|
|
if dialog.ShowModal() == wx.ID_OK:
|
2003-03-25 01:35:27 -05:00
|
|
|
c = dialog.GetColourData().GetColour()
|
2004-08-25 15:57:40 -04:00
|
|
|
self._setPenColour(wx.Colour(c.Red(), c.Green(), c.Blue()))
|
2001-09-06 17:19:48 -04:00
|
|
|
dialog.Destroy()
|
|
|
|
|
|
|
|
|
|
|
|
def onFillOptionIconClick(self, event):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Respond to the user clicking on the "Fill Options" icon.
|
|
|
|
"""
|
2004-08-25 15:57:40 -04:00
|
|
|
data = wx.ColourData()
|
2003-03-25 01:35:27 -05:00
|
|
|
if len(self.selection) == 1:
|
|
|
|
data.SetColour(self.selection[0].getFillColour())
|
|
|
|
else:
|
|
|
|
data.SetColour(self.fillColour)
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
dialog = wx.ColourDialog(self, data)
|
|
|
|
if dialog.ShowModal() == wx.ID_OK:
|
2003-03-25 01:35:27 -05:00
|
|
|
c = dialog.GetColourData().GetColour()
|
2004-08-25 15:57:40 -04:00
|
|
|
self._setFillColour(wx.Colour(c.Red(), c.Green(), c.Blue()))
|
2001-09-06 17:19:48 -04:00
|
|
|
dialog.Destroy()
|
|
|
|
|
|
|
|
def onLineOptionIconClick(self, event):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Respond to the user clicking on the "Line Options" icon.
|
|
|
|
"""
|
|
|
|
if len(self.selection) == 1:
|
|
|
|
menu = self._buildLineSizePopup(self.selection[0].getLineSize())
|
|
|
|
else:
|
|
|
|
menu = self._buildLineSizePopup(self.lineSize)
|
|
|
|
|
|
|
|
pos = self.lineOptIcon.GetPosition()
|
|
|
|
pos.y = pos.y + self.lineOptIcon.GetSize().height
|
2001-09-06 17:19:48 -04:00
|
|
|
self.PopupMenu(menu, pos)
|
|
|
|
menu.Destroy()
|
|
|
|
|
|
|
|
|
|
|
|
def onKeyEvent(self, event):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Respond to a keypress event.
|
|
|
|
|
|
|
|
We make the arrow keys move the selected object(s) by one pixel in
|
|
|
|
the given direction.
|
|
|
|
"""
|
2004-08-25 15:57:40 -04:00
|
|
|
if event.GetKeyCode() == wx.WXK_UP:
|
2003-03-25 01:35:27 -05:00
|
|
|
self._moveObject(0, -1)
|
2004-08-25 15:57:40 -04:00
|
|
|
elif event.GetKeyCode() == wx.WXK_DOWN:
|
2003-03-25 01:35:27 -05:00
|
|
|
self._moveObject(0, 1)
|
2004-08-25 15:57:40 -04:00
|
|
|
elif event.GetKeyCode() == wx.WXK_LEFT:
|
2003-03-25 01:35:27 -05:00
|
|
|
self._moveObject(-1, 0)
|
2004-08-25 15:57:40 -04:00
|
|
|
elif event.GetKeyCode() == wx.WXK_RIGHT:
|
2003-03-25 01:35:27 -05:00
|
|
|
self._moveObject(1, 0)
|
|
|
|
else:
|
|
|
|
event.Skip()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def onMouseEvent(self, event):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Respond to the user clicking on our main drawing panel.
|
|
|
|
|
|
|
|
How we respond depends on the currently selected tool.
|
|
|
|
"""
|
|
|
|
if not (event.LeftDown() or event.Dragging() or event.LeftUp()):
|
|
|
|
return # Ignore mouse movement without click/drag.
|
|
|
|
|
|
|
|
if self.curTool == self.selectIcon:
|
|
|
|
feedbackType = feedback_RECT
|
|
|
|
action = self.selectByRectangle
|
|
|
|
actionParam = param_RECT
|
|
|
|
selecting = True
|
|
|
|
dashedLine = True
|
|
|
|
elif self.curTool == self.lineIcon:
|
|
|
|
feedbackType = feedback_LINE
|
|
|
|
action = self.createLine
|
|
|
|
actionParam = param_LINE
|
|
|
|
selecting = False
|
|
|
|
dashedLine = False
|
|
|
|
elif self.curTool == self.rectIcon:
|
|
|
|
feedbackType = feedback_RECT
|
|
|
|
action = self.createRect
|
|
|
|
actionParam = param_RECT
|
|
|
|
selecting = False
|
|
|
|
dashedLine = False
|
|
|
|
elif self.curTool == self.ellipseIcon:
|
|
|
|
feedbackType = feedback_ELLIPSE
|
|
|
|
action = self.createEllipse
|
|
|
|
actionParam = param_RECT
|
|
|
|
selecting = False
|
|
|
|
dashedLine = False
|
|
|
|
elif self.curTool == self.textIcon:
|
|
|
|
feedbackType = feedback_RECT
|
|
|
|
action = self.createText
|
|
|
|
actionParam = param_RECT
|
|
|
|
selecting = False
|
|
|
|
dashedLine = True
|
|
|
|
else:
|
2004-08-25 15:57:40 -04:00
|
|
|
wx.Bell(); print "2"
|
2003-03-25 01:35:27 -05:00
|
|
|
return
|
|
|
|
|
|
|
|
if event.LeftDown():
|
|
|
|
mousePt = self._getEventCoordinates(event)
|
|
|
|
if selecting:
|
|
|
|
obj, handle = self._getObjectAndSelectionHandleAt(mousePt)
|
|
|
|
|
|
|
|
if selecting and (obj != None) and (handle != handle_NONE):
|
|
|
|
|
|
|
|
# The user clicked on an object's selection handle. Let the
|
|
|
|
# user resize the clicked-on object.
|
|
|
|
|
|
|
|
self.dragMode = drag_RESIZE
|
|
|
|
self.resizeObject = obj
|
|
|
|
|
|
|
|
if obj.getType() == obj_LINE:
|
|
|
|
self.resizeFeedback = feedback_LINE
|
|
|
|
pos = obj.getPosition()
|
2004-08-25 15:57:40 -04:00
|
|
|
startPt = wx.Point(pos.x + obj.getStartPt().x,
|
2003-03-25 01:35:27 -05:00
|
|
|
pos.y + obj.getStartPt().y)
|
2004-08-25 15:57:40 -04:00
|
|
|
endPt = wx.Point(pos.x + obj.getEndPt().x,
|
2003-03-25 01:35:27 -05:00
|
|
|
pos.y + obj.getEndPt().y)
|
|
|
|
if handle == handle_START_POINT:
|
|
|
|
self.resizeAnchor = endPt
|
|
|
|
self.resizeFloater = startPt
|
|
|
|
else:
|
|
|
|
self.resizeAnchor = startPt
|
|
|
|
self.resizeFloater = endPt
|
|
|
|
else:
|
|
|
|
self.resizeFeedback = feedback_RECT
|
|
|
|
pos = obj.getPosition()
|
|
|
|
size = obj.getSize()
|
2004-08-25 15:57:40 -04:00
|
|
|
topLeft = wx.Point(pos.x, pos.y)
|
|
|
|
topRight = wx.Point(pos.x + size.width, pos.y)
|
|
|
|
botLeft = wx.Point(pos.x, pos.y + size.height)
|
|
|
|
botRight = wx.Point(pos.x + size.width, pos.y + size.height)
|
2003-03-25 01:35:27 -05:00
|
|
|
|
|
|
|
if handle == handle_TOP_LEFT:
|
|
|
|
self.resizeAnchor = botRight
|
|
|
|
self.resizeFloater = topLeft
|
|
|
|
elif handle == handle_TOP_RIGHT:
|
|
|
|
self.resizeAnchor = botLeft
|
|
|
|
self.resizeFloater = topRight
|
|
|
|
elif handle == handle_BOTTOM_LEFT:
|
|
|
|
self.resizeAnchor = topRight
|
|
|
|
self.resizeFloater = botLeft
|
|
|
|
elif handle == handle_BOTTOM_RIGHT:
|
|
|
|
self.resizeAnchor = topLeft
|
|
|
|
self.resizeFloater = botRight
|
|
|
|
|
|
|
|
self.curPt = mousePt
|
|
|
|
self.resizeOffsetX = self.resizeFloater.x - mousePt.x
|
|
|
|
self.resizeOffsetY = self.resizeFloater.y - mousePt.y
|
2004-08-25 15:57:40 -04:00
|
|
|
endPt = wx.Point(self.curPt.x + self.resizeOffsetX,
|
2003-03-25 01:35:27 -05:00
|
|
|
self.curPt.y + self.resizeOffsetY)
|
|
|
|
self._drawVisualFeedback(self.resizeAnchor, endPt,
|
|
|
|
self.resizeFeedback, False)
|
|
|
|
|
|
|
|
elif selecting and (self._getObjectAt(mousePt) != None):
|
|
|
|
|
|
|
|
# The user clicked on an object to select it. If the user
|
|
|
|
# drags, he/she will move the object.
|
|
|
|
|
|
|
|
self.select(self._getObjectAt(mousePt))
|
|
|
|
self.dragMode = drag_MOVE
|
|
|
|
self.moveOrigin = mousePt
|
|
|
|
self.curPt = mousePt
|
|
|
|
self._drawObjectOutline(0, 0)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
# The user is dragging out a selection rect or new object.
|
|
|
|
|
|
|
|
self.dragOrigin = mousePt
|
|
|
|
self.curPt = mousePt
|
2004-08-25 15:57:40 -04:00
|
|
|
self.drawPanel.SetCursor(wx.CROSS_CURSOR)
|
2003-03-25 01:35:27 -05:00
|
|
|
self.drawPanel.CaptureMouse()
|
|
|
|
self._drawVisualFeedback(mousePt, mousePt, feedbackType,
|
|
|
|
dashedLine)
|
|
|
|
self.dragMode = drag_DRAG
|
|
|
|
|
|
|
|
event.Skip()
|
|
|
|
return
|
|
|
|
|
|
|
|
if event.Dragging():
|
|
|
|
if self.dragMode == drag_RESIZE:
|
|
|
|
|
|
|
|
# We're resizing an object.
|
|
|
|
|
|
|
|
mousePt = self._getEventCoordinates(event)
|
|
|
|
if (self.curPt.x != mousePt.x) or (self.curPt.y != mousePt.y):
|
|
|
|
# Erase previous visual feedback.
|
2004-08-25 15:57:40 -04:00
|
|
|
endPt = wx.Point(self.curPt.x + self.resizeOffsetX,
|
2003-03-25 01:35:27 -05:00
|
|
|
self.curPt.y + self.resizeOffsetY)
|
|
|
|
self._drawVisualFeedback(self.resizeAnchor, endPt,
|
|
|
|
self.resizeFeedback, False)
|
|
|
|
self.curPt = mousePt
|
|
|
|
# Draw new visual feedback.
|
2004-08-25 15:57:40 -04:00
|
|
|
endPt = wx.Point(self.curPt.x + self.resizeOffsetX,
|
2003-03-25 01:35:27 -05:00
|
|
|
self.curPt.y + self.resizeOffsetY)
|
|
|
|
self._drawVisualFeedback(self.resizeAnchor, endPt,
|
|
|
|
self.resizeFeedback, False)
|
|
|
|
|
|
|
|
elif self.dragMode == drag_MOVE:
|
|
|
|
|
|
|
|
# We're moving a selected object.
|
|
|
|
|
|
|
|
mousePt = self._getEventCoordinates(event)
|
|
|
|
if (self.curPt.x != mousePt.x) or (self.curPt.y != mousePt.y):
|
|
|
|
# Erase previous visual feedback.
|
|
|
|
self._drawObjectOutline(self.curPt.x - self.moveOrigin.x,
|
|
|
|
self.curPt.y - self.moveOrigin.y)
|
|
|
|
self.curPt = mousePt
|
|
|
|
# Draw new visual feedback.
|
|
|
|
self._drawObjectOutline(self.curPt.x - self.moveOrigin.x,
|
|
|
|
self.curPt.y - self.moveOrigin.y)
|
|
|
|
|
|
|
|
elif self.dragMode == drag_DRAG:
|
|
|
|
|
|
|
|
# We're dragging out a new object or selection rect.
|
|
|
|
|
|
|
|
mousePt = self._getEventCoordinates(event)
|
|
|
|
if (self.curPt.x != mousePt.x) or (self.curPt.y != mousePt.y):
|
|
|
|
# Erase previous visual feedback.
|
|
|
|
self._drawVisualFeedback(self.dragOrigin, self.curPt,
|
|
|
|
feedbackType, dashedLine)
|
|
|
|
self.curPt = mousePt
|
|
|
|
# Draw new visual feedback.
|
|
|
|
self._drawVisualFeedback(self.dragOrigin, self.curPt,
|
|
|
|
feedbackType, dashedLine)
|
|
|
|
|
|
|
|
event.Skip()
|
|
|
|
return
|
|
|
|
|
|
|
|
if event.LeftUp():
|
|
|
|
if self.dragMode == drag_RESIZE:
|
|
|
|
|
|
|
|
# We're resizing an object.
|
|
|
|
|
|
|
|
mousePt = self._getEventCoordinates(event)
|
|
|
|
# Erase last visual feedback.
|
2004-08-25 15:57:40 -04:00
|
|
|
endPt = wx.Point(self.curPt.x + self.resizeOffsetX,
|
2003-03-25 01:35:27 -05:00
|
|
|
self.curPt.y + self.resizeOffsetY)
|
|
|
|
self._drawVisualFeedback(self.resizeAnchor, endPt,
|
|
|
|
self.resizeFeedback, False)
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
resizePt = wx.Point(mousePt.x + self.resizeOffsetX,
|
2003-03-25 01:35:27 -05:00
|
|
|
mousePt.y + self.resizeOffsetY)
|
|
|
|
|
|
|
|
if (self.resizeFloater.x != resizePt.x) or \
|
|
|
|
(self.resizeFloater.y != resizePt.y):
|
|
|
|
self._resizeObject(self.resizeObject,
|
|
|
|
self.resizeAnchor,
|
|
|
|
self.resizeFloater,
|
|
|
|
resizePt)
|
|
|
|
else:
|
|
|
|
self.drawPanel.Refresh() # Clean up after empty resize.
|
|
|
|
|
|
|
|
elif self.dragMode == drag_MOVE:
|
|
|
|
|
|
|
|
# We're moving a selected object.
|
|
|
|
|
|
|
|
mousePt = self._getEventCoordinates(event)
|
|
|
|
# Erase last visual feedback.
|
|
|
|
self._drawObjectOutline(self.curPt.x - self.moveOrigin.x,
|
|
|
|
self.curPt.y - self.moveOrigin.y)
|
|
|
|
if (self.moveOrigin.x != mousePt.x) or \
|
|
|
|
(self.moveOrigin.y != mousePt.y):
|
|
|
|
self._moveObject(mousePt.x - self.moveOrigin.x,
|
|
|
|
mousePt.y - self.moveOrigin.y)
|
|
|
|
else:
|
|
|
|
self.drawPanel.Refresh() # Clean up after empty drag.
|
|
|
|
|
|
|
|
elif self.dragMode == drag_DRAG:
|
|
|
|
|
|
|
|
# We're dragging out a new object or selection rect.
|
|
|
|
|
|
|
|
mousePt = self._getEventCoordinates(event)
|
|
|
|
# Erase last visual feedback.
|
|
|
|
self._drawVisualFeedback(self.dragOrigin, self.curPt,
|
|
|
|
feedbackType, dashedLine)
|
|
|
|
self.drawPanel.ReleaseMouse()
|
2004-08-25 15:57:40 -04:00
|
|
|
self.drawPanel.SetCursor(wx.STANDARD_CURSOR)
|
2003-03-25 01:35:27 -05:00
|
|
|
# Perform the appropriate action for the current tool.
|
|
|
|
if actionParam == param_RECT:
|
|
|
|
x1 = min(self.dragOrigin.x, self.curPt.x)
|
|
|
|
y1 = min(self.dragOrigin.y, self.curPt.y)
|
|
|
|
x2 = max(self.dragOrigin.x, self.curPt.x)
|
|
|
|
y2 = max(self.dragOrigin.y, self.curPt.y)
|
|
|
|
|
|
|
|
startX = x1
|
|
|
|
startY = y1
|
|
|
|
width = x2 - x1
|
|
|
|
height = y2 - y1
|
|
|
|
|
|
|
|
if not selecting:
|
|
|
|
if ((x2-x1) < 8) or ((y2-y1) < 8): return # Too small.
|
|
|
|
|
|
|
|
action(x1, y1, x2-x1, y2-y1)
|
|
|
|
elif actionParam == param_LINE:
|
|
|
|
action(self.dragOrigin.x, self.dragOrigin.y,
|
|
|
|
self.curPt.x, self.curPt.y)
|
|
|
|
|
|
|
|
self.dragMode = drag_NONE # We've finished with this mouse event.
|
|
|
|
event.Skip()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def onDoubleClickEvent(self, event):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Respond to a double-click within our drawing panel.
|
|
|
|
"""
|
|
|
|
mousePt = self._getEventCoordinates(event)
|
|
|
|
obj = self._getObjectAt(mousePt)
|
|
|
|
if obj == None: return
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
# Let the user edit the given object.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
if obj.getType() == obj_TEXT:
|
|
|
|
editor = EditTextObjectDialog(self, "Edit Text Object")
|
|
|
|
editor.objectToDialog(obj)
|
2004-08-25 15:57:40 -04:00
|
|
|
if editor.ShowModal() == wx.ID_CANCEL:
|
2003-03-25 01:35:27 -05:00
|
|
|
editor.Destroy()
|
|
|
|
return
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self._saveUndoInfo()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
editor.dialogToObject(obj)
|
|
|
|
editor.Destroy()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self.dirty = True
|
|
|
|
self.drawPanel.Refresh()
|
|
|
|
self._adjustMenus()
|
|
|
|
else:
|
2004-08-25 15:57:40 -04:00
|
|
|
wx.Bell(); print "3"
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def onRightClick(self, event):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Respond to the user right-clicking within our drawing panel.
|
|
|
|
|
|
|
|
We select the clicked-on item, if necessary, and display a pop-up
|
|
|
|
menu of available options which can be applied to the selected
|
|
|
|
item(s).
|
|
|
|
"""
|
|
|
|
mousePt = self._getEventCoordinates(event)
|
|
|
|
obj = self._getObjectAt(mousePt)
|
|
|
|
|
|
|
|
if obj == None: return # Nothing selected.
|
|
|
|
|
|
|
|
# Select the clicked-on object.
|
|
|
|
|
|
|
|
self.select(obj)
|
|
|
|
|
|
|
|
# Build our pop-up menu.
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
menu = wx.Menu()
|
2003-03-25 01:35:27 -05:00
|
|
|
menu.Append(menu_DUPLICATE, "Duplicate")
|
|
|
|
menu.Append(menu_EDIT_TEXT, "Edit...")
|
|
|
|
menu.Append(menu_DELETE, "Delete")
|
|
|
|
menu.AppendSeparator()
|
|
|
|
menu.Append(menu_MOVE_FORWARD, "Move Forward")
|
|
|
|
menu.Append(menu_MOVE_TO_FRONT, "Move to Front")
|
|
|
|
menu.Append(menu_MOVE_BACKWARD, "Move Backward")
|
|
|
|
menu.Append(menu_MOVE_TO_BACK, "Move to Back")
|
|
|
|
|
|
|
|
menu.Enable(menu_EDIT_TEXT, obj.getType() == obj_TEXT)
|
|
|
|
menu.Enable(menu_MOVE_FORWARD, obj != self.contents[0])
|
|
|
|
menu.Enable(menu_MOVE_TO_FRONT, obj != self.contents[0])
|
|
|
|
menu.Enable(menu_MOVE_BACKWARD, obj != self.contents[-1])
|
|
|
|
menu.Enable(menu_MOVE_TO_BACK, obj != self.contents[-1])
|
|
|
|
|
2006-02-09 10:37:39 -05:00
|
|
|
self.Bind(wx.EVT_MENU, self.doDuplicate, id=menu_DUPLICATE)
|
|
|
|
self.Bind(wx.EVT_MENU, self.doEditText, id=menu_EDIT_TEXT)
|
|
|
|
self.Bind(wx.EVT_MENU, self.doDelete, id=menu_DELETE)
|
|
|
|
self.Bind(wx.EVT_MENU, self.doMoveForward, id=menu_MOVE_FORWARD)
|
|
|
|
self.Bind(wx.EVT_MENU, self.doMoveToFront, id=menu_MOVE_TO_FRONT)
|
|
|
|
self.Bind(wx.EVT_MENU, self.doMoveBackward, id=menu_MOVE_BACKWARD)
|
|
|
|
self.Bind(wx.EVT_MENU, self.doMoveToBack, id=menu_MOVE_TO_BACK)
|
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
# Show the pop-up menu.
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
clickPt = wx.Point(mousePt.x + self.drawPanel.GetPosition().x,
|
2003-03-25 01:35:27 -05:00
|
|
|
mousePt.y + self.drawPanel.GetPosition().y)
|
|
|
|
self.drawPanel.PopupMenu(menu, clickPt)
|
|
|
|
menu.Destroy()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def onPaintEvent(self, event):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Respond to a request to redraw the contents of our drawing panel.
|
|
|
|
"""
|
2004-08-25 15:57:40 -04:00
|
|
|
dc = wx.PaintDC(self.drawPanel)
|
2003-03-25 01:35:27 -05:00
|
|
|
self.drawPanel.PrepareDC(dc)
|
|
|
|
dc.BeginDrawing()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
for i in range(len(self.contents)-1, -1, -1):
|
|
|
|
obj = self.contents[i]
|
|
|
|
if obj in self.selection:
|
|
|
|
obj.draw(dc, True)
|
|
|
|
else:
|
|
|
|
obj.draw(dc, False)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
dc.EndDrawing()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
# ==========================
|
|
|
|
# == Menu Command Methods ==
|
|
|
|
# ==========================
|
|
|
|
|
|
|
|
def doNew(self, event):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Respond to the "New" menu command.
|
|
|
|
"""
|
|
|
|
global _docList
|
|
|
|
newFrame = DrawingFrame(None, -1, "Untitled")
|
|
|
|
newFrame.Show(True)
|
|
|
|
_docList.append(newFrame)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def doOpen(self, event):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Respond to the "Open" menu command.
|
|
|
|
"""
|
|
|
|
global _docList
|
|
|
|
|
|
|
|
curDir = os.getcwd()
|
2004-08-25 15:57:40 -04:00
|
|
|
fileName = wx.FileSelector("Open File", default_extension="psk",
|
|
|
|
flags = wx.OPEN | wx.FILE_MUST_EXIST)
|
2003-03-25 01:35:27 -05:00
|
|
|
if fileName == "": return
|
|
|
|
fileName = os.path.join(os.getcwd(), fileName)
|
|
|
|
os.chdir(curDir)
|
|
|
|
|
|
|
|
title = os.path.basename(fileName)
|
|
|
|
|
|
|
|
if (self.fileName == None) and (len(self.contents) == 0):
|
|
|
|
# Load contents into current (empty) document.
|
|
|
|
self.fileName = fileName
|
|
|
|
self.SetTitle(os.path.basename(fileName))
|
|
|
|
self.loadContents()
|
|
|
|
else:
|
|
|
|
# Open a new frame for this document.
|
|
|
|
newFrame = DrawingFrame(None, -1, os.path.basename(fileName),
|
|
|
|
fileName=fileName)
|
|
|
|
newFrame.Show(True)
|
|
|
|
_docList.append(newFrame)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def doClose(self, event):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Respond to the "Close" menu command.
|
|
|
|
"""
|
|
|
|
global _docList
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
if self.dirty:
|
|
|
|
if not self.askIfUserWantsToSave("closing"): return
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
_docList.remove(self)
|
|
|
|
self.Destroy()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def doSave(self, event):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Respond to the "Save" menu command.
|
|
|
|
"""
|
|
|
|
if self.fileName != None:
|
|
|
|
self.saveContents()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def doSaveAs(self, event):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Respond to the "Save As" menu command.
|
|
|
|
"""
|
|
|
|
if self.fileName == None:
|
|
|
|
default = ""
|
|
|
|
else:
|
|
|
|
default = self.fileName
|
|
|
|
|
|
|
|
curDir = os.getcwd()
|
2004-08-25 15:57:40 -04:00
|
|
|
fileName = wx.FileSelector("Save File As", "Saving",
|
2003-03-25 01:35:27 -05:00
|
|
|
default_filename=default,
|
|
|
|
default_extension="psk",
|
|
|
|
wildcard="*.psk",
|
2004-08-25 15:57:40 -04:00
|
|
|
flags = wx.SAVE | wx.OVERWRITE_PROMPT)
|
2003-03-25 01:35:27 -05:00
|
|
|
if fileName == "": return # User cancelled.
|
|
|
|
fileName = os.path.join(os.getcwd(), fileName)
|
|
|
|
os.chdir(curDir)
|
|
|
|
|
|
|
|
title = os.path.basename(fileName)
|
|
|
|
self.SetTitle(title)
|
|
|
|
|
|
|
|
self.fileName = fileName
|
|
|
|
self.saveContents()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def doRevert(self, event):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Respond to the "Revert" menu command.
|
|
|
|
"""
|
|
|
|
if not self.dirty: return
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
if wx.MessageBox("Discard changes made to this document?", "Confirm",
|
|
|
|
style = wx.OK | wx.CANCEL | wx.ICON_QUESTION,
|
|
|
|
parent=self) == wx.CANCEL: return
|
2003-03-25 01:35:27 -05:00
|
|
|
self.loadContents()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def doExit(self, event):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Respond to the "Quit" menu command.
|
|
|
|
"""
|
|
|
|
global _docList, _app
|
|
|
|
for doc in _docList:
|
|
|
|
if not doc.dirty: continue
|
|
|
|
doc.Raise()
|
|
|
|
if not doc.askIfUserWantsToSave("quitting"): return
|
|
|
|
_docList.remove(doc)
|
|
|
|
doc.Destroy()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
_app.ExitMainLoop()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def doUndo(self, event):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Respond to the "Undo" menu command.
|
|
|
|
"""
|
|
|
|
if self.undoInfo == None: return
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
undoData = self.undoInfo
|
|
|
|
self._saveUndoInfo() # For undoing the undo...
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self.contents = []
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
for type, data in undoData["contents"]:
|
|
|
|
obj = DrawingObject(type)
|
|
|
|
obj.setData(data)
|
|
|
|
self.contents.append(obj)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self.selection = []
|
|
|
|
for i in undoData["selection"]:
|
|
|
|
self.selection.append(self.contents[i])
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self.dirty = True
|
|
|
|
self.drawPanel.Refresh()
|
|
|
|
self._adjustMenus()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def doSelectAll(self, event):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Respond to the "Select All" menu command.
|
|
|
|
"""
|
|
|
|
self.selectAll()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def doDuplicate(self, event):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Respond to the "Duplicate" menu command.
|
|
|
|
"""
|
|
|
|
self._saveUndoInfo()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
objs = []
|
|
|
|
for obj in self.contents:
|
|
|
|
if obj in self.selection:
|
|
|
|
newObj = DrawingObject(obj.getType())
|
|
|
|
newObj.setData(obj.getData())
|
|
|
|
pos = obj.getPosition()
|
2004-08-25 15:57:40 -04:00
|
|
|
newObj.setPosition(wx.Point(pos.x + 10, pos.y + 10))
|
2003-03-25 01:35:27 -05:00
|
|
|
objs.append(newObj)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self.contents = objs + self.contents
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self.selectMany(objs)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def doEditText(self, event):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Respond to the "Edit Text" menu command.
|
|
|
|
"""
|
|
|
|
if len(self.selection) != 1: return
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
obj = self.selection[0]
|
|
|
|
if obj.getType() != obj_TEXT: return
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
editor = EditTextObjectDialog(self, "Edit Text Object")
|
|
|
|
editor.objectToDialog(obj)
|
2004-08-25 15:57:40 -04:00
|
|
|
if editor.ShowModal() == wx.ID_CANCEL:
|
2003-03-25 01:35:27 -05:00
|
|
|
editor.Destroy()
|
|
|
|
return
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self._saveUndoInfo()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
editor.dialogToObject(obj)
|
|
|
|
editor.Destroy()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self.dirty = True
|
|
|
|
self.drawPanel.Refresh()
|
|
|
|
self._adjustMenus()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def doDelete(self, event):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Respond to the "Delete" menu command.
|
|
|
|
"""
|
|
|
|
self._saveUndoInfo()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
for obj in self.selection:
|
|
|
|
self.contents.remove(obj)
|
|
|
|
del obj
|
|
|
|
self.deselectAll()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def doChooseSelectTool(self, event=None):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Respond to the "Select Tool" menu command.
|
|
|
|
"""
|
|
|
|
self._setCurrentTool(self.selectIcon)
|
2004-08-25 15:57:40 -04:00
|
|
|
self.drawPanel.SetCursor(wx.STANDARD_CURSOR)
|
2003-03-25 01:35:27 -05:00
|
|
|
self._adjustMenus()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def doChooseLineTool(self, event=None):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Respond to the "Line Tool" menu command.
|
|
|
|
"""
|
|
|
|
self._setCurrentTool(self.lineIcon)
|
2004-08-25 15:57:40 -04:00
|
|
|
self.drawPanel.SetCursor(wx.CROSS_CURSOR)
|
2003-03-25 01:35:27 -05:00
|
|
|
self.deselectAll()
|
|
|
|
self._adjustMenus()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def doChooseRectTool(self, event=None):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Respond to the "Rect Tool" menu command.
|
|
|
|
"""
|
|
|
|
self._setCurrentTool(self.rectIcon)
|
2004-08-25 15:57:40 -04:00
|
|
|
self.drawPanel.SetCursor(wx.CROSS_CURSOR)
|
2003-03-25 01:35:27 -05:00
|
|
|
self.deselectAll()
|
|
|
|
self._adjustMenus()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def doChooseEllipseTool(self, event=None):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Respond to the "Ellipse Tool" menu command.
|
|
|
|
"""
|
|
|
|
self._setCurrentTool(self.ellipseIcon)
|
2004-08-25 15:57:40 -04:00
|
|
|
self.drawPanel.SetCursor(wx.CROSS_CURSOR)
|
2003-03-25 01:35:27 -05:00
|
|
|
self.deselectAll()
|
|
|
|
self._adjustMenus()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def doChooseTextTool(self, event=None):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Respond to the "Text Tool" menu command.
|
|
|
|
"""
|
|
|
|
self._setCurrentTool(self.textIcon)
|
2004-08-25 15:57:40 -04:00
|
|
|
self.drawPanel.SetCursor(wx.CROSS_CURSOR)
|
2003-03-25 01:35:27 -05:00
|
|
|
self.deselectAll()
|
|
|
|
self._adjustMenus()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def doMoveForward(self, event):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Respond to the "Move Forward" menu command.
|
|
|
|
"""
|
|
|
|
if len(self.selection) != 1: return
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self._saveUndoInfo()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
obj = self.selection[0]
|
|
|
|
index = self.contents.index(obj)
|
|
|
|
if index == 0: return
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
del self.contents[index]
|
|
|
|
self.contents.insert(index-1, obj)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self.drawPanel.Refresh()
|
|
|
|
self._adjustMenus()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def doMoveToFront(self, event):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Respond to the "Move to Front" menu command.
|
|
|
|
"""
|
|
|
|
if len(self.selection) != 1: return
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self._saveUndoInfo()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
obj = self.selection[0]
|
|
|
|
self.contents.remove(obj)
|
|
|
|
self.contents.insert(0, obj)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self.drawPanel.Refresh()
|
|
|
|
self._adjustMenus()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def doMoveBackward(self, event):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Respond to the "Move Backward" menu command.
|
|
|
|
"""
|
|
|
|
if len(self.selection) != 1: return
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self._saveUndoInfo()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
obj = self.selection[0]
|
|
|
|
index = self.contents.index(obj)
|
|
|
|
if index == len(self.contents) - 1: return
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
del self.contents[index]
|
|
|
|
self.contents.insert(index+1, obj)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self.drawPanel.Refresh()
|
|
|
|
self._adjustMenus()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def doMoveToBack(self, event):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Respond to the "Move to Back" menu command.
|
|
|
|
"""
|
|
|
|
if len(self.selection) != 1: return
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self._saveUndoInfo()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
obj = self.selection[0]
|
|
|
|
self.contents.remove(obj)
|
|
|
|
self.contents.append(obj)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self.drawPanel.Refresh()
|
|
|
|
self._adjustMenus()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def doShowAbout(self, event):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Respond to the "About pySketch" menu command.
|
|
|
|
"""
|
2004-08-25 15:57:40 -04:00
|
|
|
dialog = wx.Dialog(self, -1, "About pySketch") # ,
|
|
|
|
#style=wx.DIALOG_MODAL | wx.STAY_ON_TOP)
|
|
|
|
dialog.SetBackgroundColour(wx.WHITE)
|
2003-03-25 01:35:27 -05:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
panel = wx.Panel(dialog, -1)
|
|
|
|
panel.SetBackgroundColour(wx.WHITE)
|
2003-03-25 01:35:27 -05:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
panelSizer = wx.BoxSizer(wx.VERTICAL)
|
2003-03-25 01:35:27 -05:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
boldFont = wx.Font(panel.GetFont().GetPointSize(),
|
2003-03-25 01:35:27 -05:00
|
|
|
panel.GetFont().GetFamily(),
|
2004-08-25 15:57:40 -04:00
|
|
|
wx.NORMAL, wx.BOLD)
|
2003-03-25 01:35:27 -05:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
logo = wx.StaticBitmap(panel, -1, wx.Bitmap("images/logo.bmp",
|
|
|
|
wx.BITMAP_TYPE_BMP))
|
2003-03-25 01:35:27 -05:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
lab1 = wx.StaticText(panel, -1, "pySketch")
|
|
|
|
lab1.SetFont(wx.Font(36, boldFont.GetFamily(), wx.ITALIC, wx.BOLD))
|
2003-03-25 01:35:27 -05:00
|
|
|
lab1.SetSize(lab1.GetBestSize())
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
imageSizer = wx.BoxSizer(wx.HORIZONTAL)
|
|
|
|
imageSizer.Add(logo, 0, wx.ALL | wx.ALIGN_CENTRE_VERTICAL, 5)
|
|
|
|
imageSizer.Add(lab1, 0, wx.ALL | wx.ALIGN_CENTRE_VERTICAL, 5)
|
2003-03-25 01:35:27 -05:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
lab2 = wx.StaticText(panel, -1, "A simple object-oriented drawing " + \
|
2003-03-25 01:35:27 -05:00
|
|
|
"program.")
|
|
|
|
lab2.SetFont(boldFont)
|
|
|
|
lab2.SetSize(lab2.GetBestSize())
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
lab3 = wx.StaticText(panel, -1, "pySketch is completely free " + \
|
2003-03-25 01:35:27 -05:00
|
|
|
"software; please")
|
|
|
|
lab3.SetFont(boldFont)
|
|
|
|
lab3.SetSize(lab3.GetBestSize())
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
lab4 = wx.StaticText(panel, -1, "feel free to adapt or use this " + \
|
2003-03-25 01:35:27 -05:00
|
|
|
"in any way you like.")
|
|
|
|
lab4.SetFont(boldFont)
|
|
|
|
lab4.SetSize(lab4.GetBestSize())
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
lab5 = wx.StaticText(panel, -1, "Author: Erik Westra " + \
|
2003-03-25 01:35:27 -05:00
|
|
|
"(ewestra@wave.co.nz)")
|
|
|
|
lab5.SetFont(boldFont)
|
|
|
|
lab5.SetSize(lab5.GetBestSize())
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
btnOK = wx.Button(panel, wx.ID_OK, "OK")
|
2003-03-25 01:35:27 -05:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
panelSizer.Add(imageSizer, 0, wx.ALIGN_CENTRE)
|
2004-03-12 15:03:37 -05:00
|
|
|
panelSizer.Add((10, 10)) # Spacer.
|
2004-08-25 15:57:40 -04:00
|
|
|
panelSizer.Add(lab2, 0, wx.ALIGN_CENTRE)
|
2004-03-12 15:03:37 -05:00
|
|
|
panelSizer.Add((10, 10)) # Spacer.
|
2004-08-25 15:57:40 -04:00
|
|
|
panelSizer.Add(lab3, 0, wx.ALIGN_CENTRE)
|
|
|
|
panelSizer.Add(lab4, 0, wx.ALIGN_CENTRE)
|
2004-03-12 15:03:37 -05:00
|
|
|
panelSizer.Add((10, 10)) # Spacer.
|
2004-08-25 15:57:40 -04:00
|
|
|
panelSizer.Add(lab5, 0, wx.ALIGN_CENTRE)
|
2004-03-12 15:03:37 -05:00
|
|
|
panelSizer.Add((10, 10)) # Spacer.
|
2004-08-25 15:57:40 -04:00
|
|
|
panelSizer.Add(btnOK, 0, wx.ALL | wx.ALIGN_CENTRE, 5)
|
2003-03-25 01:35:27 -05:00
|
|
|
|
|
|
|
panel.SetAutoLayout(True)
|
|
|
|
panel.SetSizer(panelSizer)
|
|
|
|
panelSizer.Fit(panel)
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
topSizer = wx.BoxSizer(wx.HORIZONTAL)
|
|
|
|
topSizer.Add(panel, 0, wx.ALL, 10)
|
2003-03-25 01:35:27 -05:00
|
|
|
|
|
|
|
dialog.SetAutoLayout(True)
|
|
|
|
dialog.SetSizer(topSizer)
|
|
|
|
topSizer.Fit(dialog)
|
|
|
|
|
|
|
|
dialog.Centre()
|
|
|
|
|
|
|
|
btn = dialog.ShowModal()
|
|
|
|
dialog.Destroy()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
# =============================
|
|
|
|
# == Object Creation Methods ==
|
|
|
|
# =============================
|
|
|
|
|
|
|
|
def createLine(self, x1, y1, x2, y2):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Create a new line object at the given position and size.
|
|
|
|
"""
|
|
|
|
self._saveUndoInfo()
|
|
|
|
|
|
|
|
topLeftX = min(x1, x2)
|
|
|
|
topLeftY = min(y1, y2)
|
|
|
|
botRightX = max(x1, x2)
|
|
|
|
botRightY = max(y1, y2)
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
obj = DrawingObject(obj_LINE, position=wx.Point(topLeftX, topLeftY),
|
|
|
|
size=wx.Size(botRightX-topLeftX,
|
2003-03-25 01:35:27 -05:00
|
|
|
botRightY-topLeftY),
|
|
|
|
penColour=self.penColour,
|
|
|
|
fillColour=self.fillColour,
|
|
|
|
lineSize=self.lineSize,
|
2004-08-25 15:57:40 -04:00
|
|
|
startPt = wx.Point(x1 - topLeftX, y1 - topLeftY),
|
|
|
|
endPt = wx.Point(x2 - topLeftX, y2 - topLeftY))
|
2003-03-25 01:35:27 -05:00
|
|
|
self.contents.insert(0, obj)
|
|
|
|
self.dirty = True
|
|
|
|
self.doChooseSelectTool()
|
|
|
|
self.select(obj)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def createRect(self, x, y, width, height):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Create a new rectangle object at the given position and size.
|
|
|
|
"""
|
|
|
|
self._saveUndoInfo()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
obj = DrawingObject(obj_RECT, position=wx.Point(x, y),
|
|
|
|
size=wx.Size(width, height),
|
2003-03-25 01:35:27 -05:00
|
|
|
penColour=self.penColour,
|
|
|
|
fillColour=self.fillColour,
|
|
|
|
lineSize=self.lineSize)
|
|
|
|
self.contents.insert(0, obj)
|
|
|
|
self.dirty = True
|
|
|
|
self.doChooseSelectTool()
|
|
|
|
self.select(obj)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def createEllipse(self, x, y, width, height):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Create a new ellipse object at the given position and size.
|
|
|
|
"""
|
|
|
|
self._saveUndoInfo()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
obj = DrawingObject(obj_ELLIPSE, position=wx.Point(x, y),
|
|
|
|
size=wx.Size(width, height),
|
2003-03-25 01:35:27 -05:00
|
|
|
penColour=self.penColour,
|
|
|
|
fillColour=self.fillColour,
|
|
|
|
lineSize=self.lineSize)
|
|
|
|
self.contents.insert(0, obj)
|
|
|
|
self.dirty = True
|
|
|
|
self.doChooseSelectTool()
|
|
|
|
self.select(obj)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def createText(self, x, y, width, height):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Create a new text object at the given position and size.
|
|
|
|
"""
|
|
|
|
editor = EditTextObjectDialog(self, "Create Text Object")
|
2004-08-25 15:57:40 -04:00
|
|
|
if editor.ShowModal() == wx.ID_CANCEL:
|
2003-03-25 01:35:27 -05:00
|
|
|
editor.Destroy()
|
|
|
|
return
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self._saveUndoInfo()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
obj = DrawingObject(obj_TEXT, position=wx.Point(x, y),
|
|
|
|
size=wx.Size(width, height))
|
2003-03-25 01:35:27 -05:00
|
|
|
editor.dialogToObject(obj)
|
|
|
|
editor.Destroy()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self.contents.insert(0, obj)
|
|
|
|
self.dirty = True
|
|
|
|
self.doChooseSelectTool()
|
|
|
|
self.select(obj)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
# =======================
|
|
|
|
# == Selection Methods ==
|
|
|
|
# =======================
|
|
|
|
|
|
|
|
def selectAll(self):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Select every DrawingObject in our document.
|
|
|
|
"""
|
|
|
|
self.selection = []
|
|
|
|
for obj in self.contents:
|
|
|
|
self.selection.append(obj)
|
|
|
|
self.drawPanel.Refresh()
|
|
|
|
self._adjustMenus()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def deselectAll(self):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Deselect every DrawingObject in our document.
|
|
|
|
"""
|
|
|
|
self.selection = []
|
|
|
|
self.drawPanel.Refresh()
|
|
|
|
self._adjustMenus()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def select(self, obj):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Select the given DrawingObject within our document.
|
|
|
|
"""
|
|
|
|
self.selection = [obj]
|
|
|
|
self.drawPanel.Refresh()
|
|
|
|
self._adjustMenus()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def selectMany(self, objs):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Select the given list of DrawingObjects.
|
|
|
|
"""
|
|
|
|
self.selection = objs
|
|
|
|
self.drawPanel.Refresh()
|
|
|
|
self._adjustMenus()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def selectByRectangle(self, x, y, width, height):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Select every DrawingObject in the given rectangular region.
|
|
|
|
"""
|
|
|
|
self.selection = []
|
|
|
|
for obj in self.contents:
|
|
|
|
if obj.objectWithinRect(x, y, width, height):
|
|
|
|
self.selection.append(obj)
|
|
|
|
self.drawPanel.Refresh()
|
|
|
|
self._adjustMenus()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
# ======================
|
|
|
|
# == File I/O Methods ==
|
|
|
|
# ======================
|
|
|
|
|
|
|
|
def loadContents(self):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Load the contents of our document into memory.
|
|
|
|
"""
|
|
|
|
f = open(self.fileName, "rb")
|
|
|
|
objData = cPickle.load(f)
|
|
|
|
f.close()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
for type, data in objData:
|
|
|
|
obj = DrawingObject(type)
|
|
|
|
obj.setData(data)
|
|
|
|
self.contents.append(obj)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self.dirty = False
|
|
|
|
self.selection = []
|
|
|
|
self.undoInfo = None
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self.drawPanel.Refresh()
|
|
|
|
self._adjustMenus()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def saveContents(self):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Save the contents of our document to disk.
|
|
|
|
"""
|
|
|
|
objData = []
|
|
|
|
for obj in self.contents:
|
|
|
|
objData.append([obj.getType(), obj.getData()])
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
f = open(self.fileName, "wb")
|
|
|
|
cPickle.dump(objData, f)
|
|
|
|
f.close()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self.dirty = False
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def askIfUserWantsToSave(self, action):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Give the user the opportunity to save the current document.
|
|
|
|
|
|
|
|
'action' is a string describing the action about to be taken. If
|
|
|
|
the user wants to save the document, it is saved immediately. If
|
|
|
|
the user cancels, we return False.
|
|
|
|
"""
|
|
|
|
if not self.dirty: return True # Nothing to do.
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
response = wx.MessageBox("Save changes before " + action + "?",
|
|
|
|
"Confirm", wx.YES_NO | wx.CANCEL, self)
|
2003-03-25 01:35:27 -05:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
if response == wx.YES:
|
2003-03-25 01:35:27 -05:00
|
|
|
if self.fileName == None:
|
2004-08-25 15:57:40 -04:00
|
|
|
fileName = wx.FileSelector("Save File As", "Saving",
|
2003-03-25 01:35:27 -05:00
|
|
|
default_extension="psk",
|
|
|
|
wildcard="*.psk",
|
2004-08-25 15:57:40 -04:00
|
|
|
flags = wx.SAVE | wx.OVERWRITE_PROMPT)
|
2003-03-25 01:35:27 -05:00
|
|
|
if fileName == "": return False # User cancelled.
|
|
|
|
self.fileName = fileName
|
|
|
|
|
|
|
|
self.saveContents()
|
|
|
|
return True
|
2004-08-25 15:57:40 -04:00
|
|
|
elif response == wx.NO:
|
2003-03-25 01:35:27 -05:00
|
|
|
return True # User doesn't want changes saved.
|
2004-08-25 15:57:40 -04:00
|
|
|
elif response == wx.CANCEL:
|
2003-03-25 01:35:27 -05:00
|
|
|
return False # User cancelled.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
# =====================
|
|
|
|
# == Private Methods ==
|
|
|
|
# =====================
|
|
|
|
|
|
|
|
def _adjustMenus(self):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Adjust our menus and toolbar to reflect the current state of the
|
|
|
|
world.
|
|
|
|
"""
|
|
|
|
canSave = (self.fileName != None) and self.dirty
|
|
|
|
canRevert = (self.fileName != None) and self.dirty
|
|
|
|
canUndo = self.undoInfo != None
|
|
|
|
selection = len(self.selection) > 0
|
|
|
|
onlyOne = len(self.selection) == 1
|
|
|
|
isText = onlyOne and (self.selection[0].getType() == obj_TEXT)
|
|
|
|
front = onlyOne and (self.selection[0] == self.contents[0])
|
|
|
|
back = onlyOne and (self.selection[0] == self.contents[-1])
|
|
|
|
|
|
|
|
# Enable/disable our menu items.
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
self.fileMenu.Enable(wx.ID_SAVE, canSave)
|
|
|
|
self.fileMenu.Enable(wx.ID_REVERT, canRevert)
|
2003-03-25 01:35:27 -05:00
|
|
|
|
|
|
|
self.editMenu.Enable(menu_UNDO, canUndo)
|
|
|
|
self.editMenu.Enable(menu_DUPLICATE, selection)
|
|
|
|
self.editMenu.Enable(menu_EDIT_TEXT, isText)
|
|
|
|
self.editMenu.Enable(menu_DELETE, selection)
|
|
|
|
|
|
|
|
self.toolsMenu.Check(menu_SELECT, self.curTool == self.selectIcon)
|
|
|
|
self.toolsMenu.Check(menu_LINE, self.curTool == self.lineIcon)
|
|
|
|
self.toolsMenu.Check(menu_RECT, self.curTool == self.rectIcon)
|
|
|
|
self.toolsMenu.Check(menu_ELLIPSE, self.curTool == self.ellipseIcon)
|
|
|
|
self.toolsMenu.Check(menu_TEXT, self.curTool == self.textIcon)
|
|
|
|
|
|
|
|
self.objectMenu.Enable(menu_MOVE_FORWARD, onlyOne and not front)
|
|
|
|
self.objectMenu.Enable(menu_MOVE_TO_FRONT, onlyOne and not front)
|
|
|
|
self.objectMenu.Enable(menu_MOVE_BACKWARD, onlyOne and not back)
|
|
|
|
self.objectMenu.Enable(menu_MOVE_TO_BACK, onlyOne and not back)
|
|
|
|
|
|
|
|
# Enable/disable our toolbar icons.
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
self.toolbar.EnableTool(wx.ID_NEW, True)
|
|
|
|
self.toolbar.EnableTool(wx.ID_OPEN, True)
|
|
|
|
self.toolbar.EnableTool(wx.ID_SAVE, canSave)
|
2003-03-25 01:35:27 -05:00
|
|
|
self.toolbar.EnableTool(menu_UNDO, canUndo)
|
|
|
|
self.toolbar.EnableTool(menu_DUPLICATE, selection)
|
|
|
|
self.toolbar.EnableTool(menu_MOVE_FORWARD, onlyOne and not front)
|
|
|
|
self.toolbar.EnableTool(menu_MOVE_BACKWARD, onlyOne and not back)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def _setCurrentTool(self, newToolIcon):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Set the currently selected tool.
|
|
|
|
"""
|
|
|
|
if self.curTool == newToolIcon: return # Nothing to do.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
if self.curTool != None:
|
|
|
|
self.curTool.deselect()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
newToolIcon.select()
|
|
|
|
self.curTool = newToolIcon
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def _setPenColour(self, colour):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Set the default or selected object's pen colour.
|
|
|
|
"""
|
|
|
|
if len(self.selection) > 0:
|
|
|
|
self._saveUndoInfo()
|
|
|
|
for obj in self.selection:
|
|
|
|
obj.setPenColour(colour)
|
|
|
|
self.drawPanel.Refresh()
|
|
|
|
else:
|
|
|
|
self.penColour = colour
|
|
|
|
self.optionIndicator.setPenColour(colour)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def _setFillColour(self, colour):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Set the default or selected object's fill colour.
|
|
|
|
"""
|
|
|
|
if len(self.selection) > 0:
|
|
|
|
self._saveUndoInfo()
|
|
|
|
for obj in self.selection:
|
|
|
|
obj.setFillColour(colour)
|
|
|
|
self.drawPanel.Refresh()
|
|
|
|
else:
|
|
|
|
self.fillColour = colour
|
|
|
|
self.optionIndicator.setFillColour(colour)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def _setLineSize(self, size):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Set the default or selected object's line size.
|
|
|
|
"""
|
|
|
|
if len(self.selection) > 0:
|
|
|
|
self._saveUndoInfo()
|
|
|
|
for obj in self.selection:
|
|
|
|
obj.setLineSize(size)
|
|
|
|
self.drawPanel.Refresh()
|
|
|
|
else:
|
|
|
|
self.lineSize = size
|
|
|
|
self.optionIndicator.setLineSize(size)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def _saveUndoInfo(self):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Remember the current state of the document, to allow for undo.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
We make a copy of the document's contents, so that we can return to
|
|
|
|
the previous contents if the user does something and then wants to
|
|
|
|
undo the operation.
|
|
|
|
"""
|
|
|
|
savedContents = []
|
|
|
|
for obj in self.contents:
|
|
|
|
savedContents.append([obj.getType(), obj.getData()])
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
savedSelection = []
|
|
|
|
for i in range(len(self.contents)):
|
|
|
|
if self.contents[i] in self.selection:
|
|
|
|
savedSelection.append(i)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self.undoInfo = {"contents" : savedContents,
|
|
|
|
"selection" : savedSelection}
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def _resizeObject(self, obj, anchorPt, oldPt, newPt):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Resize the given object.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
'anchorPt' is the unchanging corner of the object, while the
|
|
|
|
opposite corner has been resized. 'oldPt' are the current
|
|
|
|
coordinates for this corner, while 'newPt' are the new coordinates.
|
|
|
|
The object should fit within the given dimensions, though if the
|
|
|
|
new point is less than the anchor point the object will need to be
|
|
|
|
moved as well as resized, to avoid giving it a negative size.
|
|
|
|
"""
|
|
|
|
if obj.getType() == obj_TEXT:
|
|
|
|
# Not allowed to resize text objects -- they're sized to fit text.
|
2004-08-25 15:57:40 -04:00
|
|
|
wx.Bell(); print "4"
|
2003-03-25 01:35:27 -05:00
|
|
|
return
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self._saveUndoInfo()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
topLeft = wx.Point(min(anchorPt.x, newPt.x),
|
2003-03-25 01:35:27 -05:00
|
|
|
min(anchorPt.y, newPt.y))
|
2004-08-25 15:57:40 -04:00
|
|
|
botRight = wx.Point(max(anchorPt.x, newPt.x),
|
2003-03-25 01:35:27 -05:00
|
|
|
max(anchorPt.y, newPt.y))
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
newWidth = botRight.x - topLeft.x
|
|
|
|
newHeight = botRight.y - topLeft.y
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
if obj.getType() == obj_LINE:
|
|
|
|
# Adjust the line so that its start and end points match the new
|
|
|
|
# overall object size.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
startPt = obj.getStartPt()
|
|
|
|
endPt = obj.getEndPt()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
slopesDown = ((startPt.x < endPt.x) and (startPt.y < endPt.y)) or \
|
|
|
|
((startPt.x > endPt.x) and (startPt.y > endPt.y))
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
# Handle the user flipping the line.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
hFlip = ((anchorPt.x < oldPt.x) and (anchorPt.x > newPt.x)) or \
|
|
|
|
((anchorPt.x > oldPt.x) and (anchorPt.x < newPt.x))
|
|
|
|
vFlip = ((anchorPt.y < oldPt.y) and (anchorPt.y > newPt.y)) or \
|
|
|
|
((anchorPt.y > oldPt.y) and (anchorPt.y < newPt.y))
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
if (hFlip and not vFlip) or (vFlip and not hFlip):
|
|
|
|
slopesDown = not slopesDown # Line flipped.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
if slopesDown:
|
2004-08-25 15:57:40 -04:00
|
|
|
obj.setStartPt(wx.Point(0, 0))
|
|
|
|
obj.setEndPt(wx.Point(newWidth, newHeight))
|
2003-03-25 01:35:27 -05:00
|
|
|
else:
|
2004-08-25 15:57:40 -04:00
|
|
|
obj.setStartPt(wx.Point(0, newHeight))
|
|
|
|
obj.setEndPt(wx.Point(newWidth, 0))
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
# Finally, adjust the bounds of the object to match the new dimensions.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
obj.setPosition(topLeft)
|
2004-08-25 15:57:40 -04:00
|
|
|
obj.setSize(wx.Size(botRight.x - topLeft.x, botRight.y - topLeft.y))
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self.drawPanel.Refresh()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def _moveObject(self, offsetX, offsetY):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Move the currently selected object(s) by the given offset.
|
|
|
|
"""
|
|
|
|
self._saveUndoInfo()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
for obj in self.selection:
|
|
|
|
pos = obj.getPosition()
|
|
|
|
pos.x = pos.x + offsetX
|
|
|
|
pos.y = pos.y + offsetY
|
|
|
|
obj.setPosition(pos)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self.drawPanel.Refresh()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def _buildLineSizePopup(self, lineSize):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Build the pop-up menu used to set the line size.
|
|
|
|
|
|
|
|
'lineSize' is the current line size value. The corresponding item
|
|
|
|
is checked in the pop-up menu.
|
|
|
|
"""
|
2004-08-25 15:57:40 -04:00
|
|
|
menu = wx.Menu()
|
|
|
|
menu.Append(id_LINESIZE_0, "no line", kind=wx.ITEM_CHECK)
|
|
|
|
menu.Append(id_LINESIZE_1, "1-pixel line", kind=wx.ITEM_CHECK)
|
|
|
|
menu.Append(id_LINESIZE_2, "2-pixel line", kind=wx.ITEM_CHECK)
|
|
|
|
menu.Append(id_LINESIZE_3, "3-pixel line", kind=wx.ITEM_CHECK)
|
|
|
|
menu.Append(id_LINESIZE_4, "4-pixel line", kind=wx.ITEM_CHECK)
|
|
|
|
menu.Append(id_LINESIZE_5, "5-pixel line", kind=wx.ITEM_CHECK)
|
2003-03-25 01:35:27 -05:00
|
|
|
|
|
|
|
if lineSize == 0: menu.Check(id_LINESIZE_0, True)
|
|
|
|
elif lineSize == 1: menu.Check(id_LINESIZE_1, True)
|
|
|
|
elif lineSize == 2: menu.Check(id_LINESIZE_2, True)
|
|
|
|
elif lineSize == 3: menu.Check(id_LINESIZE_3, True)
|
|
|
|
elif lineSize == 4: menu.Check(id_LINESIZE_4, True)
|
|
|
|
elif lineSize == 5: menu.Check(id_LINESIZE_5, True)
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
self.Bind(wx.EVT_MENU, self._lineSizePopupSelected, id=id_LINESIZE_0, id2=id_LINESIZE_5)
|
2003-03-25 01:35:27 -05:00
|
|
|
|
|
|
|
return menu
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def _lineSizePopupSelected(self, event):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Respond to the user selecting an item from the line size popup menu
|
|
|
|
"""
|
|
|
|
id = event.GetId()
|
|
|
|
if id == id_LINESIZE_0: self._setLineSize(0)
|
|
|
|
elif id == id_LINESIZE_1: self._setLineSize(1)
|
|
|
|
elif id == id_LINESIZE_2: self._setLineSize(2)
|
|
|
|
elif id == id_LINESIZE_3: self._setLineSize(3)
|
|
|
|
elif id == id_LINESIZE_4: self._setLineSize(4)
|
|
|
|
elif id == id_LINESIZE_5: self._setLineSize(5)
|
|
|
|
else:
|
2004-08-25 15:57:40 -04:00
|
|
|
wx.Bell(); print "5"
|
2003-03-25 01:35:27 -05:00
|
|
|
return
|
|
|
|
|
|
|
|
self.optionIndicator.setLineSize(self.lineSize)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def _getEventCoordinates(self, event):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Return the coordinates associated with the given mouse event.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
The coordinates have to be adjusted to allow for the current scroll
|
|
|
|
position.
|
|
|
|
"""
|
2001-09-06 17:19:48 -04:00
|
|
|
originX, originY = self.drawPanel.GetViewStart()
|
|
|
|
unitX, unitY = self.drawPanel.GetScrollPixelsPerUnit()
|
2004-08-25 15:57:40 -04:00
|
|
|
return wx.Point(event.GetX() + (originX * unitX),
|
2003-03-25 01:35:27 -05:00
|
|
|
event.GetY() + (originY * unitY))
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def _getObjectAndSelectionHandleAt(self, pt):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Return the object and selection handle at the given point.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
We draw selection handles (small rectangles) around the currently
|
|
|
|
selected object(s). If the given point is within one of the
|
|
|
|
selection handle rectangles, we return the associated object and a
|
|
|
|
code indicating which selection handle the point is in. If the
|
|
|
|
point isn't within any selection handle at all, we return the tuple
|
|
|
|
(None, handle_NONE).
|
|
|
|
"""
|
|
|
|
for obj in self.selection:
|
|
|
|
handle = obj.getSelectionHandleContainingPoint(pt.x, pt.y)
|
|
|
|
if handle != handle_NONE:
|
|
|
|
return obj, handle
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
return None, handle_NONE
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def _getObjectAt(self, pt):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Return the first object found which is at the given point.
|
|
|
|
"""
|
|
|
|
for obj in self.contents:
|
|
|
|
if obj.objectContainsPoint(pt.x, pt.y):
|
|
|
|
return obj
|
|
|
|
return None
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def _drawObjectOutline(self, offsetX, offsetY):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Draw an outline of the currently selected object.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
The selected object's outline is drawn at the object's position
|
|
|
|
plus the given offset.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
Note that the outline is drawn by *inverting* the window's
|
|
|
|
contents, so calling _drawObjectOutline twice in succession will
|
|
|
|
restore the window's contents back to what they were previously.
|
|
|
|
"""
|
|
|
|
if len(self.selection) != 1: return
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
position = self.selection[0].getPosition()
|
|
|
|
size = self.selection[0].getSize()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
dc = wx.ClientDC(self.drawPanel)
|
2003-03-25 01:35:27 -05:00
|
|
|
self.drawPanel.PrepareDC(dc)
|
|
|
|
dc.BeginDrawing()
|
2004-08-25 15:57:40 -04:00
|
|
|
dc.SetPen(wx.BLACK_DASHED_PEN)
|
|
|
|
dc.SetBrush(wx.TRANSPARENT_BRUSH)
|
|
|
|
dc.SetLogicalFunction(wx.INVERT)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
dc.DrawRectangle(position.x + offsetX, position.y + offsetY,
|
|
|
|
size.width, size.height)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
dc.EndDrawing()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def _drawVisualFeedback(self, startPt, endPt, type, dashedLine):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Draw visual feedback for a drawing operation.
|
|
|
|
|
|
|
|
The visual feedback consists of a line, ellipse, or rectangle based
|
|
|
|
around the two given points. 'type' should be one of the following
|
|
|
|
predefined feedback type constants:
|
|
|
|
|
|
|
|
feedback_RECT -> draw rectangular feedback.
|
|
|
|
feedback_LINE -> draw line feedback.
|
|
|
|
feedback_ELLIPSE -> draw elliptical feedback.
|
|
|
|
|
|
|
|
if 'dashedLine' is True, the feedback is drawn as a dashed rather
|
|
|
|
than a solid line.
|
|
|
|
|
|
|
|
Note that the feedback is drawn by *inverting* the window's
|
|
|
|
contents, so calling _drawVisualFeedback twice in succession will
|
|
|
|
restore the window's contents back to what they were previously.
|
|
|
|
"""
|
2004-08-25 15:57:40 -04:00
|
|
|
dc = wx.ClientDC(self.drawPanel)
|
2003-03-25 01:35:27 -05:00
|
|
|
self.drawPanel.PrepareDC(dc)
|
|
|
|
dc.BeginDrawing()
|
|
|
|
if dashedLine:
|
2004-08-25 15:57:40 -04:00
|
|
|
dc.SetPen(wx.BLACK_DASHED_PEN)
|
2003-03-25 01:35:27 -05:00
|
|
|
else:
|
2004-08-25 15:57:40 -04:00
|
|
|
dc.SetPen(wx.BLACK_PEN)
|
|
|
|
dc.SetBrush(wx.TRANSPARENT_BRUSH)
|
|
|
|
dc.SetLogicalFunction(wx.INVERT)
|
2003-03-25 01:35:27 -05:00
|
|
|
|
|
|
|
if type == feedback_RECT:
|
|
|
|
dc.DrawRectangle(startPt.x, startPt.y,
|
|
|
|
endPt.x - startPt.x,
|
|
|
|
endPt.y - startPt.y)
|
|
|
|
elif type == feedback_LINE:
|
|
|
|
dc.DrawLine(startPt.x, startPt.y, endPt.x, endPt.y)
|
|
|
|
elif type == feedback_ELLIPSE:
|
|
|
|
dc.DrawEllipse(startPt.x, startPt.y,
|
|
|
|
endPt.x - startPt.x,
|
|
|
|
endPt.y - startPt.y)
|
|
|
|
|
|
|
|
dc.EndDrawing()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class DrawingObject:
|
|
|
|
""" An object within the drawing panel.
|
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
A pySketch document consists of a front-to-back ordered list of
|
|
|
|
DrawingObjects. Each DrawingObject has the following properties:
|
|
|
|
|
|
|
|
'type' What type of object this is (text, line, etc).
|
|
|
|
'position' The position of the object within the document.
|
|
|
|
'size' The size of the object within the document.
|
|
|
|
'penColour' The colour to use for drawing the object's outline.
|
|
|
|
'fillColour' Colour to use for drawing object's interior.
|
|
|
|
'lineSize' Line width (in pixels) to use for object's outline.
|
|
|
|
'startPt' The point, relative to the object's position, where
|
|
|
|
an obj_LINE object's line should start.
|
|
|
|
'endPt' The point, relative to the object's position, where
|
|
|
|
an obj_LINE object's line should end.
|
|
|
|
'text' The object's text (obj_TEXT objects only).
|
|
|
|
'textFont' The text object's font name.
|
|
|
|
'textSize' The text object's point size.
|
|
|
|
'textBoldface' If True, this text object will be drawn in
|
|
|
|
boldface.
|
|
|
|
'textItalic' If True, this text object will be drawn in italic.
|
|
|
|
'textUnderline' If True, this text object will be drawn underlined.
|
|
|
|
"""
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
# ==================
|
|
|
|
# == Constructors ==
|
|
|
|
# ==================
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
def __init__(self, type, position=wx.Point(0, 0), size=wx.Size(0, 0),
|
|
|
|
penColour=wx.BLACK, fillColour=wx.WHITE, lineSize=1,
|
|
|
|
text=None, startPt=wx.Point(0, 0), endPt=wx.Point(0,0)):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Standard constructor.
|
|
|
|
|
|
|
|
'type' is the type of object being created. This should be one of
|
|
|
|
the following constants:
|
|
|
|
|
|
|
|
obj_LINE
|
|
|
|
obj_RECT
|
|
|
|
obj_ELLIPSE
|
|
|
|
obj_TEXT
|
|
|
|
|
|
|
|
The remaining parameters let you set various options for the newly
|
|
|
|
created DrawingObject.
|
|
|
|
"""
|
|
|
|
self.type = type
|
|
|
|
self.position = position
|
|
|
|
self.size = size
|
|
|
|
self.penColour = penColour
|
|
|
|
self.fillColour = fillColour
|
|
|
|
self.lineSize = lineSize
|
|
|
|
self.startPt = startPt
|
|
|
|
self.endPt = endPt
|
|
|
|
self.text = text
|
2004-08-25 15:57:40 -04:00
|
|
|
self.textFont = wx.SystemSettings_GetFont(
|
|
|
|
wx.SYS_DEFAULT_GUI_FONT).GetFaceName()
|
2003-03-25 01:35:27 -05:00
|
|
|
self.textSize = 12
|
|
|
|
self.textBoldface = False
|
|
|
|
self.textItalic = False
|
|
|
|
self.textUnderline = False
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
# =============================
|
|
|
|
# == Object Property Methods ==
|
|
|
|
# =============================
|
|
|
|
|
|
|
|
def getData(self):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Return a copy of the object's internal data.
|
|
|
|
|
|
|
|
This is used to save this DrawingObject to disk.
|
|
|
|
"""
|
|
|
|
return [self.type, self.position.x, self.position.y,
|
|
|
|
self.size.width, self.size.height,
|
|
|
|
self.penColour.Red(),
|
|
|
|
self.penColour.Green(),
|
|
|
|
self.penColour.Blue(),
|
|
|
|
self.fillColour.Red(),
|
|
|
|
self.fillColour.Green(),
|
|
|
|
self.fillColour.Blue(),
|
|
|
|
self.lineSize,
|
|
|
|
self.startPt.x, self.startPt.y,
|
|
|
|
self.endPt.x, self.endPt.y,
|
|
|
|
self.text,
|
|
|
|
self.textFont,
|
|
|
|
self.textSize,
|
|
|
|
self.textBoldface,
|
|
|
|
self.textItalic,
|
|
|
|
self.textUnderline]
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def setData(self, data):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Set the object's internal data.
|
|
|
|
|
|
|
|
'data' is a copy of the object's saved data, as returned by
|
|
|
|
getData() above. This is used to restore a previously saved
|
|
|
|
DrawingObject.
|
|
|
|
"""
|
|
|
|
#data = copy.deepcopy(data) # Needed?
|
|
|
|
|
|
|
|
self.type = data[0]
|
2004-08-25 15:57:40 -04:00
|
|
|
self.position = wx.Point(data[1], data[2])
|
|
|
|
self.size = wx.Size(data[3], data[4])
|
|
|
|
self.penColour = wx.Colour(red=data[5],
|
2003-03-25 01:35:27 -05:00
|
|
|
green=data[6],
|
|
|
|
blue=data[7])
|
2004-08-25 15:57:40 -04:00
|
|
|
self.fillColour = wx.Colour(red=data[8],
|
2003-03-25 01:35:27 -05:00
|
|
|
green=data[9],
|
|
|
|
blue=data[10])
|
|
|
|
self.lineSize = data[11]
|
2004-08-25 15:57:40 -04:00
|
|
|
self.startPt = wx.Point(data[12], data[13])
|
|
|
|
self.endPt = wx.Point(data[14], data[15])
|
2003-03-25 01:35:27 -05:00
|
|
|
self.text = data[16]
|
|
|
|
self.textFont = data[17]
|
|
|
|
self.textSize = data[18]
|
|
|
|
self.textBoldface = data[19]
|
|
|
|
self.textItalic = data[20]
|
|
|
|
self.textUnderline = data[21]
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def getType(self):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Return this DrawingObject's type.
|
|
|
|
"""
|
|
|
|
return self.type
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def setPosition(self, position):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Set the origin (top-left corner) for this DrawingObject.
|
|
|
|
"""
|
|
|
|
self.position = position
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def getPosition(self):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Return this DrawingObject's position.
|
|
|
|
"""
|
|
|
|
return self.position
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def setSize(self, size):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Set the size for this DrawingObject.
|
|
|
|
"""
|
|
|
|
self.size = size
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def getSize(self):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Return this DrawingObject's size.
|
|
|
|
"""
|
|
|
|
return self.size
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def setPenColour(self, colour):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Set the pen colour used for this DrawingObject.
|
|
|
|
"""
|
|
|
|
self.penColour = colour
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def getPenColour(self):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Return this DrawingObject's pen colour.
|
|
|
|
"""
|
|
|
|
return self.penColour
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def setFillColour(self, colour):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Set the fill colour used for this DrawingObject.
|
|
|
|
"""
|
|
|
|
self.fillColour = colour
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def getFillColour(self):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Return this DrawingObject's fill colour.
|
|
|
|
"""
|
|
|
|
return self.fillColour
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def setLineSize(self, lineSize):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Set the linesize used for this DrawingObject.
|
|
|
|
"""
|
|
|
|
self.lineSize = lineSize
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def getLineSize(self):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Return this DrawingObject's line size.
|
|
|
|
"""
|
|
|
|
return self.lineSize
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def setStartPt(self, startPt):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Set the starting point for this line DrawingObject.
|
|
|
|
"""
|
|
|
|
self.startPt = startPt
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def getStartPt(self):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Return the starting point for this line DrawingObject.
|
|
|
|
"""
|
|
|
|
return self.startPt
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def setEndPt(self, endPt):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Set the ending point for this line DrawingObject.
|
|
|
|
"""
|
|
|
|
self.endPt = endPt
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def getEndPt(self):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Return the ending point for this line DrawingObject.
|
|
|
|
"""
|
|
|
|
return self.endPt
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def setText(self, text):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Set the text for this DrawingObject.
|
|
|
|
"""
|
|
|
|
self.text = text
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def getText(self):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Return this DrawingObject's text.
|
|
|
|
"""
|
|
|
|
return self.text
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def setTextFont(self, font):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Set the typeface for this text DrawingObject.
|
|
|
|
"""
|
|
|
|
self.textFont = font
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def getTextFont(self):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Return this text DrawingObject's typeface.
|
|
|
|
"""
|
|
|
|
return self.textFont
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def setTextSize(self, size):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Set the point size for this text DrawingObject.
|
|
|
|
"""
|
|
|
|
self.textSize = size
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def getTextSize(self):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Return this text DrawingObject's text size.
|
|
|
|
"""
|
|
|
|
return self.textSize
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def setTextBoldface(self, boldface):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Set the boldface flag for this text DrawingObject.
|
|
|
|
"""
|
|
|
|
self.textBoldface = boldface
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def getTextBoldface(self):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Return this text DrawingObject's boldface flag.
|
|
|
|
"""
|
|
|
|
return self.textBoldface
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def setTextItalic(self, italic):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Set the italic flag for this text DrawingObject.
|
|
|
|
"""
|
|
|
|
self.textItalic = italic
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def getTextItalic(self):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Return this text DrawingObject's italic flag.
|
|
|
|
"""
|
|
|
|
return self.textItalic
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def setTextUnderline(self, underline):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Set the underling flag for this text DrawingObject.
|
|
|
|
"""
|
|
|
|
self.textUnderline = underline
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def getTextUnderline(self):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Return this text DrawingObject's underline flag.
|
|
|
|
"""
|
|
|
|
return self.textUnderline
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
# ============================
|
|
|
|
# == Object Drawing Methods ==
|
|
|
|
# ============================
|
|
|
|
|
|
|
|
def draw(self, dc, selected):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Draw this DrawingObject into our window.
|
|
|
|
|
|
|
|
'dc' is the device context to use for drawing. If 'selected' is
|
|
|
|
True, the object is currently selected and should be drawn as such.
|
|
|
|
"""
|
|
|
|
if self.type != obj_TEXT:
|
|
|
|
if self.lineSize == 0:
|
2004-08-25 15:57:40 -04:00
|
|
|
dc.SetPen(wx.Pen(self.penColour, self.lineSize, wx.TRANSPARENT))
|
2003-03-25 01:35:27 -05:00
|
|
|
else:
|
2004-08-25 15:57:40 -04:00
|
|
|
dc.SetPen(wx.Pen(self.penColour, self.lineSize, wx.SOLID))
|
|
|
|
dc.SetBrush(wx.Brush(self.fillColour, wx.SOLID))
|
2003-03-25 01:35:27 -05:00
|
|
|
else:
|
|
|
|
dc.SetTextForeground(self.penColour)
|
|
|
|
dc.SetTextBackground(self.fillColour)
|
|
|
|
|
|
|
|
self._privateDraw(dc, self.position, selected)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
# =======================
|
|
|
|
# == Selection Methods ==
|
|
|
|
# =======================
|
|
|
|
|
|
|
|
def objectContainsPoint(self, x, y):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Returns True iff this object contains the given point.
|
|
|
|
|
|
|
|
This is used to determine if the user clicked on the object.
|
|
|
|
"""
|
|
|
|
# Firstly, ignore any points outside of the object's bounds.
|
|
|
|
|
|
|
|
if x < self.position.x: return False
|
|
|
|
if x > self.position.x + self.size.x: return False
|
|
|
|
if y < self.position.y: return False
|
|
|
|
if y > self.position.y + self.size.y: return False
|
|
|
|
|
|
|
|
if self.type in [obj_RECT, obj_TEXT]:
|
|
|
|
# Rectangles and text are easy -- they're always selected if the
|
|
|
|
# point is within their bounds.
|
|
|
|
return True
|
|
|
|
|
|
|
|
# Now things get tricky. There's no straightforward way of knowing
|
|
|
|
# whether the point is within the object's bounds...to get around this,
|
|
|
|
# we draw the object into a memory-based bitmap and see if the given
|
|
|
|
# point was drawn. This could no doubt be done more efficiently by
|
|
|
|
# some tricky maths, but this approach works and is simple enough.
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
bitmap = wx.EmptyBitmap(self.size.x + 10, self.size.y + 10)
|
|
|
|
dc = wx.MemoryDC()
|
2003-03-25 01:35:27 -05:00
|
|
|
dc.SelectObject(bitmap)
|
|
|
|
dc.BeginDrawing()
|
2004-08-25 15:57:40 -04:00
|
|
|
dc.SetBackground(wx.WHITE_BRUSH)
|
2003-03-25 01:35:27 -05:00
|
|
|
dc.Clear()
|
2004-08-25 15:57:40 -04:00
|
|
|
dc.SetPen(wx.Pen(wx.BLACK, self.lineSize + 5, wx.SOLID))
|
|
|
|
dc.SetBrush(wx.BLACK_BRUSH)
|
|
|
|
self._privateDraw(dc, wx.Point(5, 5), True)
|
2003-03-25 01:35:27 -05:00
|
|
|
dc.EndDrawing()
|
|
|
|
pixel = dc.GetPixel(x - self.position.x + 5, y - self.position.y + 5)
|
|
|
|
if (pixel.Red() == 0) and (pixel.Green() == 0) and (pixel.Blue() == 0):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def getSelectionHandleContainingPoint(self, x, y):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Return the selection handle containing the given point, if any.
|
|
|
|
|
|
|
|
We return one of the predefined selection handle ID codes.
|
|
|
|
"""
|
|
|
|
if self.type == obj_LINE:
|
|
|
|
# We have selection handles at the start and end points.
|
|
|
|
if self._pointInSelRect(x, y, self.position.x + self.startPt.x,
|
|
|
|
self.position.y + self.startPt.y):
|
|
|
|
return handle_START_POINT
|
|
|
|
elif self._pointInSelRect(x, y, self.position.x + self.endPt.x,
|
|
|
|
self.position.y + self.endPt.y):
|
|
|
|
return handle_END_POINT
|
|
|
|
else:
|
|
|
|
return handle_NONE
|
|
|
|
else:
|
|
|
|
# We have selection handles at all four corners.
|
|
|
|
if self._pointInSelRect(x, y, self.position.x, self.position.y):
|
|
|
|
return handle_TOP_LEFT
|
|
|
|
elif self._pointInSelRect(x, y, self.position.x + self.size.width,
|
|
|
|
self.position.y):
|
|
|
|
return handle_TOP_RIGHT
|
|
|
|
elif self._pointInSelRect(x, y, self.position.x,
|
|
|
|
self.position.y + self.size.height):
|
|
|
|
return handle_BOTTOM_LEFT
|
|
|
|
elif self._pointInSelRect(x, y, self.position.x + self.size.width,
|
|
|
|
self.position.y + self.size.height):
|
|
|
|
return handle_BOTTOM_RIGHT
|
|
|
|
else:
|
|
|
|
return handle_NONE
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def objectWithinRect(self, x, y, width, height):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Return True iff this object falls completely within the given rect.
|
|
|
|
"""
|
|
|
|
if x > self.position.x: return False
|
|
|
|
if x + width < self.position.x + self.size.width: return False
|
|
|
|
if y > self.position.y: return False
|
|
|
|
if y + height < self.position.y + self.size.height: return False
|
|
|
|
return True
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
# =====================
|
|
|
|
# == Utility Methods ==
|
|
|
|
# =====================
|
|
|
|
|
|
|
|
def fitToText(self):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Resize a text DrawingObject so that it fits it's text exactly.
|
|
|
|
"""
|
|
|
|
if self.type != obj_TEXT: return
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
if self.textBoldface: weight = wx.BOLD
|
|
|
|
else: weight = wx.NORMAL
|
|
|
|
if self.textItalic: style = wx.ITALIC
|
|
|
|
else: style = wx.NORMAL
|
|
|
|
font = wx.Font(self.textSize, wx.DEFAULT, style, weight,
|
2003-03-25 01:35:27 -05:00
|
|
|
self.textUnderline, self.textFont)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
dummyWindow = wx.Frame(None, -1, "")
|
2003-03-25 01:35:27 -05:00
|
|
|
dummyWindow.SetFont(font)
|
|
|
|
width, height = dummyWindow.GetTextExtent(self.text)
|
|
|
|
dummyWindow.Destroy()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
self.size = wx.Size(width, height)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
# =====================
|
|
|
|
# == Private Methods ==
|
|
|
|
# =====================
|
|
|
|
|
|
|
|
def _privateDraw(self, dc, position, selected):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Private routine to draw this DrawingObject.
|
|
|
|
|
|
|
|
'dc' is the device context to use for drawing, while 'position' is
|
|
|
|
the position in which to draw the object. If 'selected' is True,
|
|
|
|
the object is drawn with selection handles. This private drawing
|
|
|
|
routine assumes that the pen and brush have already been set by the
|
|
|
|
caller.
|
|
|
|
"""
|
|
|
|
if self.type == obj_LINE:
|
|
|
|
dc.DrawLine(position.x + self.startPt.x,
|
|
|
|
position.y + self.startPt.y,
|
|
|
|
position.x + self.endPt.x,
|
|
|
|
position.y + self.endPt.y)
|
|
|
|
elif self.type == obj_RECT:
|
|
|
|
dc.DrawRectangle(position.x, position.y,
|
|
|
|
self.size.width, self.size.height)
|
|
|
|
elif self.type == obj_ELLIPSE:
|
|
|
|
dc.DrawEllipse(position.x, position.y,
|
|
|
|
self.size.width, self.size.height)
|
|
|
|
elif self.type == obj_TEXT:
|
2004-08-25 15:57:40 -04:00
|
|
|
if self.textBoldface: weight = wx.BOLD
|
|
|
|
else: weight = wx.NORMAL
|
|
|
|
if self.textItalic: style = wx.ITALIC
|
|
|
|
else: style = wx.NORMAL
|
|
|
|
font = wx.Font(self.textSize, wx.DEFAULT, style, weight,
|
2003-03-25 01:35:27 -05:00
|
|
|
self.textUnderline, self.textFont)
|
|
|
|
dc.SetFont(font)
|
|
|
|
dc.DrawText(self.text, position.x, position.y)
|
|
|
|
|
|
|
|
if selected:
|
2004-08-25 15:57:40 -04:00
|
|
|
dc.SetPen(wx.TRANSPARENT_PEN)
|
|
|
|
dc.SetBrush(wx.BLACK_BRUSH)
|
2003-03-25 01:35:27 -05:00
|
|
|
|
|
|
|
if self.type == obj_LINE:
|
|
|
|
# Draw selection handles at the start and end points.
|
|
|
|
self._drawSelHandle(dc, position.x + self.startPt.x,
|
|
|
|
position.y + self.startPt.y)
|
|
|
|
self._drawSelHandle(dc, position.x + self.endPt.x,
|
|
|
|
position.y + self.endPt.y)
|
|
|
|
else:
|
|
|
|
# Draw selection handles at all four corners.
|
|
|
|
self._drawSelHandle(dc, position.x, position.y)
|
|
|
|
self._drawSelHandle(dc, position.x + self.size.width,
|
|
|
|
position.y)
|
|
|
|
self._drawSelHandle(dc, position.x,
|
|
|
|
position.y + self.size.height)
|
|
|
|
self._drawSelHandle(dc, position.x + self.size.width,
|
|
|
|
position.y + self.size.height)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def _drawSelHandle(self, dc, x, y):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Draw a selection handle around this DrawingObject.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
'dc' is the device context to draw the selection handle within,
|
|
|
|
while 'x' and 'y' are the coordinates to use for the centre of the
|
|
|
|
selection handle.
|
|
|
|
"""
|
|
|
|
dc.DrawRectangle(x - 3, y - 3, 6, 6)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def _pointInSelRect(self, x, y, rX, rY):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Return True iff (x, y) is within the selection handle at (rX, ry).
|
|
|
|
"""
|
|
|
|
if x < rX - 3: return False
|
|
|
|
elif x > rX + 3: return False
|
|
|
|
elif y < rY - 3: return False
|
|
|
|
elif y > rY + 3: return False
|
|
|
|
else: return True
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------------
|
|
|
|
|
2005-05-24 16:55:42 -04:00
|
|
|
class ToolPaletteIcon(GenBitmapButton):
|
2001-09-06 17:19:48 -04:00
|
|
|
""" An icon appearing in the tool palette area of our sketching window.
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
Note that this is actually implemented as a wx.Bitmap rather
|
|
|
|
than as a wx.Icon. wx.Icon has a very specific meaning, and isn't
|
2003-03-25 01:35:27 -05:00
|
|
|
appropriate for this more general use.
|
2001-09-06 17:19:48 -04:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, parent, iconID, iconName, toolTip):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Standard constructor.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
'parent' is the parent window this icon will be part of.
|
|
|
|
'iconID' is the internal ID used for this icon.
|
|
|
|
'iconName' is the name used for this icon.
|
|
|
|
'toolTip' is the tool tip text to show for this icon.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
The icon name is used to get the appropriate bitmap for this icon.
|
|
|
|
"""
|
2004-08-25 15:57:40 -04:00
|
|
|
bmp = wx.Bitmap("images/" + iconName + "Icon.bmp", wx.BITMAP_TYPE_BMP)
|
2005-05-24 16:55:42 -04:00
|
|
|
GenBitmapButton.__init__(self, parent, iconID, bmp, wx.DefaultPosition,
|
2004-08-25 15:57:40 -04:00
|
|
|
wx.Size(bmp.GetWidth(), bmp.GetHeight()))
|
|
|
|
self.SetToolTip(wx.ToolTip(toolTip))
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self.iconID = iconID
|
|
|
|
self.iconName = iconName
|
|
|
|
self.isSelected = False
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def select(self):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Select the icon.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
The icon's visual representation is updated appropriately.
|
|
|
|
"""
|
|
|
|
if self.isSelected: return # Nothing to do!
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
bmp = wx.Bitmap("images/" + self.iconName + "IconSel.bmp",
|
|
|
|
wx.BITMAP_TYPE_BMP)
|
2004-03-12 15:03:37 -05:00
|
|
|
self.SetBitmapLabel(bmp)
|
2003-03-25 01:35:27 -05:00
|
|
|
self.isSelected = True
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def deselect(self):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Deselect the icon.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
The icon's visual representation is updated appropriately.
|
|
|
|
"""
|
|
|
|
if not self.isSelected: return # Nothing to do!
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
bmp = wx.Bitmap("images/" + self.iconName + "Icon.bmp",
|
|
|
|
wx.BITMAP_TYPE_BMP)
|
2004-03-12 15:03:37 -05:00
|
|
|
self.SetBitmapLabel(bmp)
|
2003-03-25 01:35:27 -05:00
|
|
|
self.isSelected = False
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------------
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
class ToolOptionIndicator(wx.Window):
|
2001-09-06 17:19:48 -04:00
|
|
|
""" A visual indicator which shows the current tool options.
|
|
|
|
"""
|
|
|
|
def __init__(self, parent):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Standard constructor.
|
|
|
|
"""
|
2004-08-25 15:57:40 -04:00
|
|
|
wx.Window.__init__(self, parent, -1, wx.DefaultPosition, wx.Size(52, 32))
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
self.penColour = wx.BLACK
|
|
|
|
self.fillColour = wx.WHITE
|
2003-03-25 01:35:27 -05:00
|
|
|
self.lineSize = 1
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
self.Bind(wx.EVT_PAINT, self.OnPaint)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def setPenColour(self, penColour):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Set the indicator's current pen colour.
|
|
|
|
"""
|
|
|
|
self.penColour = penColour
|
|
|
|
self.Refresh()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def setFillColour(self, fillColour):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Set the indicator's current fill colour.
|
|
|
|
"""
|
|
|
|
self.fillColour = fillColour
|
|
|
|
self.Refresh()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def setLineSize(self, lineSize):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Set the indicator's current pen colour.
|
|
|
|
"""
|
|
|
|
self.lineSize = lineSize
|
|
|
|
self.Refresh()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def OnPaint(self, event):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Paint our tool option indicator.
|
|
|
|
"""
|
2004-08-25 15:57:40 -04:00
|
|
|
dc = wx.PaintDC(self)
|
2003-03-25 01:35:27 -05:00
|
|
|
dc.BeginDrawing()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
if self.lineSize == 0:
|
2004-08-25 15:57:40 -04:00
|
|
|
dc.SetPen(wx.Pen(self.penColour, self.lineSize, wx.TRANSPARENT))
|
2003-03-25 01:35:27 -05:00
|
|
|
else:
|
2004-08-25 15:57:40 -04:00
|
|
|
dc.SetPen(wx.Pen(self.penColour, self.lineSize, wx.SOLID))
|
|
|
|
dc.SetBrush(wx.Brush(self.fillColour, wx.SOLID))
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
dc.DrawRectangle(5, 5, self.GetSize().width - 10,
|
|
|
|
self.GetSize().height - 10)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
dc.EndDrawing()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------------
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
class EditTextObjectDialog(wx.Dialog):
|
2001-09-06 17:19:48 -04:00
|
|
|
""" Dialog box used to edit the properties of a text object.
|
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
The user can edit the object's text, font, size, and text style.
|
2001-09-06 17:19:48 -04:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, parent, title):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Standard constructor.
|
|
|
|
"""
|
2004-08-25 15:57:40 -04:00
|
|
|
wx.Dialog.__init__(self, parent, -1, title)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
self.textCtrl = wx.TextCtrl(self, 1001, "", style=wx.TE_PROCESS_ENTER,
|
2003-03-25 01:35:27 -05:00
|
|
|
validator=TextObjectValidator())
|
|
|
|
extent = self.textCtrl.GetFullTextExtent("Hy")
|
|
|
|
lineHeight = extent[1] + extent[3]
|
2004-08-25 15:57:40 -04:00
|
|
|
self.textCtrl.SetSize(wx.Size(-1, lineHeight * 4))
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
self.Bind(wx.EVT_TEXT_ENTER, self._doEnter, id=1001)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
fonts = wx.FontEnumerator()
|
2003-03-25 01:35:27 -05:00
|
|
|
fonts.EnumerateFacenames()
|
|
|
|
self.fontList = fonts.GetFacenames()
|
|
|
|
self.fontList.sort()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
fontLabel = wx.StaticText(self, -1, "Font:")
|
|
|
|
self._setFontOptions(fontLabel, weight=wx.BOLD)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
self.fontCombo = wx.ComboBox(self, -1, "", wx.DefaultPosition,
|
|
|
|
wx.DefaultSize, self.fontList,
|
|
|
|
style = wx.CB_READONLY)
|
2003-03-25 01:35:27 -05:00
|
|
|
self.fontCombo.SetSelection(0) # Default to first available font.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self.sizeList = ["8", "9", "10", "12", "14", "16",
|
|
|
|
"18", "20", "24", "32", "48", "72"]
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
sizeLabel = wx.StaticText(self, -1, "Size:")
|
|
|
|
self._setFontOptions(sizeLabel, weight=wx.BOLD)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
self.sizeCombo = wx.ComboBox(self, -1, "", wx.DefaultPosition,
|
|
|
|
wx.DefaultSize, self.sizeList,
|
|
|
|
style=wx.CB_READONLY)
|
2003-03-25 01:35:27 -05:00
|
|
|
self.sizeCombo.SetSelection(3) # Default to 12 point text.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
gap = wx.LEFT | wx.TOP | wx.RIGHT
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
comboSizer = wx.BoxSizer(wx.HORIZONTAL)
|
|
|
|
comboSizer.Add(fontLabel, 0, gap | wx.ALIGN_CENTRE_VERTICAL, 5)
|
2003-03-25 01:35:27 -05:00
|
|
|
comboSizer.Add(self.fontCombo, 0, gap, 5)
|
2004-03-12 15:03:37 -05:00
|
|
|
comboSizer.Add((5, 5)) # Spacer.
|
2004-08-25 15:57:40 -04:00
|
|
|
comboSizer.Add(sizeLabel, 0, gap | wx.ALIGN_CENTRE_VERTICAL, 5)
|
2003-03-25 01:35:27 -05:00
|
|
|
comboSizer.Add(self.sizeCombo, 0, gap, 5)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
self.boldCheckbox = wx.CheckBox(self, -1, "Bold")
|
|
|
|
self.italicCheckbox = wx.CheckBox(self, -1, "Italic")
|
|
|
|
self.underlineCheckbox = wx.CheckBox(self, -1, "Underline")
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
self._setFontOptions(self.boldCheckbox, weight=wx.BOLD)
|
|
|
|
self._setFontOptions(self.italicCheckbox, style=wx.ITALIC)
|
2003-03-25 01:35:27 -05:00
|
|
|
self._setFontOptions(self.underlineCheckbox, underline=True)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
styleSizer = wx.BoxSizer(wx.HORIZONTAL)
|
2003-03-25 01:35:27 -05:00
|
|
|
styleSizer.Add(self.boldCheckbox, 0, gap, 5)
|
|
|
|
styleSizer.Add(self.italicCheckbox, 0, gap, 5)
|
|
|
|
styleSizer.Add(self.underlineCheckbox, 0, gap, 5)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
self.okButton = wx.Button(self, wx.ID_OK, "OK")
|
|
|
|
self.cancelButton = wx.Button(self, wx.ID_CANCEL, "Cancel")
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
btnSizer = wx.BoxSizer(wx.HORIZONTAL)
|
2003-03-25 01:35:27 -05:00
|
|
|
btnSizer.Add(self.okButton, 0, gap, 5)
|
|
|
|
btnSizer.Add(self.cancelButton, 0, gap, 5)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
sizer = wx.BoxSizer(wx.VERTICAL)
|
|
|
|
sizer.Add(self.textCtrl, 1, gap | wx.EXPAND, 5)
|
2004-03-12 15:03:37 -05:00
|
|
|
sizer.Add((10, 10)) # Spacer.
|
2004-08-25 15:57:40 -04:00
|
|
|
sizer.Add(comboSizer, 0, gap | wx.ALIGN_CENTRE, 5)
|
|
|
|
sizer.Add(styleSizer, 0, gap | wx.ALIGN_CENTRE, 5)
|
2004-03-12 15:03:37 -05:00
|
|
|
sizer.Add((10, 10)) # Spacer.
|
2004-08-25 15:57:40 -04:00
|
|
|
sizer.Add(btnSizer, 0, gap | wx.ALIGN_CENTRE, 5)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self.SetAutoLayout(True)
|
|
|
|
self.SetSizer(sizer)
|
|
|
|
sizer.Fit(self)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self.textCtrl.SetFocus()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def objectToDialog(self, obj):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Copy the properties of the given text object into the dialog box.
|
|
|
|
"""
|
|
|
|
self.textCtrl.SetValue(obj.getText())
|
|
|
|
self.textCtrl.SetSelection(0, len(obj.getText()))
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
for i in range(len(self.fontList)):
|
|
|
|
if self.fontList[i] == obj.getTextFont():
|
|
|
|
self.fontCombo.SetSelection(i)
|
|
|
|
break
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
for i in range(len(self.sizeList)):
|
|
|
|
if self.sizeList[i] == str(obj.getTextSize()):
|
|
|
|
self.sizeCombo.SetSelection(i)
|
|
|
|
break
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self.boldCheckbox.SetValue(obj.getTextBoldface())
|
|
|
|
self.italicCheckbox.SetValue(obj.getTextItalic())
|
|
|
|
self.underlineCheckbox.SetValue(obj.getTextUnderline())
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def dialogToObject(self, obj):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Copy the properties from the dialog box into the given text object.
|
|
|
|
"""
|
|
|
|
obj.setText(self.textCtrl.GetValue())
|
|
|
|
obj.setTextFont(self.fontCombo.GetValue())
|
|
|
|
obj.setTextSize(int(self.sizeCombo.GetValue()))
|
|
|
|
obj.setTextBoldface(self.boldCheckbox.GetValue())
|
|
|
|
obj.setTextItalic(self.italicCheckbox.GetValue())
|
|
|
|
obj.setTextUnderline(self.underlineCheckbox.GetValue())
|
|
|
|
obj.fitToText()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
# ======================
|
|
|
|
# == Private Routines ==
|
|
|
|
# ======================
|
|
|
|
|
|
|
|
def _setFontOptions(self, ctrl, family=None, pointSize=-1,
|
2004-08-25 15:57:40 -04:00
|
|
|
style=wx.NORMAL, weight=wx.NORMAL,
|
2003-03-25 01:35:27 -05:00
|
|
|
underline=False):
|
|
|
|
""" Change the font settings for the given control.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
The meaning of the 'family', 'pointSize', 'style', 'weight' and
|
2004-08-25 15:57:40 -04:00
|
|
|
'underline' parameters are the same as for the wx.Font constructor.
|
2003-03-25 01:35:27 -05:00
|
|
|
If the family and/or pointSize isn't specified, the current default
|
|
|
|
value is used.
|
|
|
|
"""
|
|
|
|
if family == None: family = ctrl.GetFont().GetFamily()
|
|
|
|
if pointSize == -1: pointSize = ctrl.GetFont().GetPointSize()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
ctrl.SetFont(wx.Font(pointSize, family, style, weight, underline))
|
2003-03-25 01:35:27 -05:00
|
|
|
ctrl.SetSize(ctrl.GetBestSize()) # Adjust size to reflect font change.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def _doEnter(self, event):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Respond to the user hitting the ENTER key.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
We simulate clicking on the "OK" button.
|
|
|
|
"""
|
|
|
|
if self.Validate(): self.Show(False)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------------
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
class TextObjectValidator(wx.PyValidator):
|
2001-09-06 17:19:48 -04:00
|
|
|
""" This validator is used to ensure that the user has entered something
|
2003-03-25 01:35:27 -05:00
|
|
|
into the text object editor dialog's text field.
|
2001-09-06 17:19:48 -04:00
|
|
|
"""
|
|
|
|
def __init__(self):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Standard constructor.
|
|
|
|
"""
|
2004-08-25 15:57:40 -04:00
|
|
|
wx.PyValidator.__init__(self)
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def Clone(self):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Standard cloner.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
Note that every validator must implement the Clone() method.
|
|
|
|
"""
|
|
|
|
return TextObjectValidator()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def Validate(self, win):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Validate the contents of the given text control.
|
|
|
|
"""
|
2004-03-12 15:03:37 -05:00
|
|
|
textCtrl = self.GetWindow()
|
2003-03-25 01:35:27 -05:00
|
|
|
text = textCtrl.GetValue()
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
if len(text) == 0:
|
2004-08-25 15:57:40 -04:00
|
|
|
wx.MessageBox("A text object must contain some text!", "Error")
|
2003-03-25 01:35:27 -05:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def TransferToWindow(self):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Transfer data from validator to window.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
The default implementation returns False, indicating that an error
|
|
|
|
occurred. We simply return True, as we don't do any data transfer.
|
|
|
|
"""
|
2004-08-25 15:57:40 -04:00
|
|
|
return True # Prevent wx.Dialog from complaining.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def TransferFromWindow(self):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Transfer data from window to validator.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
The default implementation returns False, indicating that an error
|
|
|
|
occurred. We simply return True, as we don't do any data transfer.
|
|
|
|
"""
|
2004-08-25 15:57:40 -04:00
|
|
|
return True # Prevent wx.Dialog from complaining.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class ExceptionHandler:
|
|
|
|
""" A simple error-handling class to write exceptions to a text file.
|
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
Under MS Windows, the standard DOS console window doesn't scroll and
|
|
|
|
closes as soon as the application exits, making it hard to find and
|
|
|
|
view Python exceptions. This utility class allows you to handle Python
|
|
|
|
exceptions in a more friendly manner.
|
2001-09-06 17:19:48 -04:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Standard constructor.
|
|
|
|
"""
|
|
|
|
self._buff = ""
|
|
|
|
if os.path.exists("errors.txt"):
|
|
|
|
os.remove("errors.txt") # Delete previous error log, if any.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
def write(self, s):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Write the given error message to a text file.
|
|
|
|
|
|
|
|
Note that if the error message doesn't end in a carriage return, we
|
|
|
|
have to buffer up the inputs until a carriage return is received.
|
|
|
|
"""
|
|
|
|
if (s[-1] != "\n") and (s[-1] != "\r"):
|
|
|
|
self._buff = self._buff + s
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
s = self._buff + s
|
|
|
|
self._buff = ""
|
|
|
|
|
|
|
|
if s[:9] == "Traceback":
|
|
|
|
# Tell the user than an exception occurred.
|
2004-08-25 15:57:40 -04:00
|
|
|
wx.MessageBox("An internal error has occurred.\nPlease " + \
|
2003-03-25 01:35:27 -05:00
|
|
|
"refer to the 'errors.txt' file for details.",
|
2004-08-25 15:57:40 -04:00
|
|
|
"Error", wx.OK | wx.CENTRE | wx.ICON_EXCLAMATION)
|
2003-03-25 01:35:27 -05:00
|
|
|
|
|
|
|
f = open("errors.txt", "a")
|
|
|
|
f.write(s)
|
|
|
|
f.close()
|
|
|
|
except:
|
|
|
|
pass # Don't recursively crash on errors.
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------------
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
class SketchApp(wx.App):
|
2001-09-06 17:19:48 -04:00
|
|
|
""" The main pySketch application object.
|
|
|
|
"""
|
|
|
|
def OnInit(self):
|
2003-03-25 01:35:27 -05:00
|
|
|
""" Initialise the application.
|
|
|
|
"""
|
|
|
|
global _docList
|
|
|
|
_docList = []
|
|
|
|
|
|
|
|
if len(sys.argv) == 1:
|
|
|
|
# No file name was specified on the command line -> start with a
|
|
|
|
# blank document.
|
|
|
|
frame = DrawingFrame(None, -1, "Untitled")
|
|
|
|
frame.Centre()
|
|
|
|
frame.Show(True)
|
|
|
|
_docList.append(frame)
|
|
|
|
else:
|
|
|
|
# Load the file(s) specified on the command line.
|
|
|
|
for arg in sys.argv[1:]:
|
|
|
|
fileName = os.path.join(os.getcwd(), arg)
|
|
|
|
if os.path.isfile(fileName):
|
|
|
|
frame = DrawingFrame(None, -1,
|
|
|
|
os.path.basename(fileName),
|
|
|
|
fileName=fileName)
|
|
|
|
frame.Show(True)
|
|
|
|
_docList.append(frame)
|
|
|
|
|
|
|
|
return True
|
2001-09-06 17:19:48 -04:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
def main():
|
|
|
|
""" Start up the pySketch application.
|
|
|
|
"""
|
|
|
|
global _app
|
|
|
|
|
|
|
|
# Redirect python exceptions to a log file.
|
|
|
|
|
|
|
|
sys.stderr = ExceptionHandler()
|
|
|
|
|
|
|
|
# Create and start the pySketch application.
|
|
|
|
|
|
|
|
_app = SketchApp(0)
|
|
|
|
_app.MainLoop()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|
|
|
|
|
2004-08-25 15:57:40 -04:00
|
|
|
|