📄 pysketch.py
字号:
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()
def onDoubleClickEvent(self, event):
""" Respond to a double-click within our drawing panel.
"""
mousePt = self._getEventCoordinates(event)
obj = self._getObjectAt(mousePt)
if obj == None: return
# Let the user edit the given object.
if obj.getType() == obj_TEXT:
editor = EditTextObjectDialog(self, "Edit Text Object")
editor.objectToDialog(obj)
if editor.ShowModal() == wx.ID_CANCEL:
editor.Destroy()
return
self._saveUndoInfo()
editor.dialogToObject(obj)
editor.Destroy()
self.dirty = True
self.drawPanel.Refresh()
self._adjustMenus()
else:
wx.Bell(); print "3"
def onRightClick(self, event):
""" 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.
menu = wx.Menu()
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])
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)
# Show the pop-up menu.
clickPt = wx.Point(mousePt.x + self.drawPanel.GetPosition().x,
mousePt.y + self.drawPanel.GetPosition().y)
self.drawPanel.PopupMenu(menu, clickPt)
menu.Destroy()
def onPaintEvent(self, event):
""" Respond to a request to redraw the contents of our drawing panel.
"""
dc = wx.PaintDC(self.drawPanel)
self.drawPanel.PrepareDC(dc)
dc.BeginDrawing()
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)
dc.EndDrawing()
# ==========================
# == Menu Command Methods ==
# ==========================
def doNew(self, event):
""" Respond to the "New" menu command.
"""
global _docList
newFrame = DrawingFrame(None, -1, "Untitled")
newFrame.Show(True)
_docList.append(newFrame)
def doOpen(self, event):
""" Respond to the "Open" menu command.
"""
global _docList
curDir = os.getcwd()
fileName = wx.FileSelector("Open File", default_extension="psk",
flags = wx.OPEN | wx.FILE_MUST_EXIST)
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)
def doClose(self, event):
""" Respond to the "Close" menu command.
"""
global _docList
if self.dirty:
if not self.askIfUserWantsToSave("closing"): return
_docList.remove(self)
self.Destroy()
def doSave(self, event):
""" Respond to the "Save" menu command.
"""
if self.fileName != None:
self.saveContents()
def doSaveAs(self, event):
""" Respond to the "Save As" menu command.
"""
if self.fileName == None:
default = ""
else:
default = self.fileName
curDir = os.getcwd()
fileName = wx.FileSelector("Save File As", "Saving",
default_filename=default,
default_extension="psk",
wildcard="*.psk",
flags = wx.SAVE | wx.OVERWRITE_PROMPT)
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()
def doRevert(self, event):
""" Respond to the "Revert" menu command.
"""
if not self.dirty: return
if wx.MessageBox("Discard changes made to this document?", "Confirm",
style = wx.OK | wx.CANCEL | wx.ICON_QUESTION,
parent=self) == wx.CANCEL: return
self.loadContents()
def doExit(self, event):
""" 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()
_app.ExitMainLoop()
def doUndo(self, event):
""" Respond to the "Undo" menu command.
"""
if self.undoInfo == None: return
undoData = self.undoInfo
self._saveUndoInfo() # For undoing the undo...
self.contents = []
for type, data in undoData["contents"]:
obj = DrawingObject(type)
obj.setData(data)
self.contents.append(obj)
self.selection = []
for i in undoData["selection"]:
self.selection.append(self.contents[i])
self.dirty = True
self.drawPanel.Refresh()
self._adjustMenus()
def doSelectAll(self, event):
""" Respond to the "Select All" menu command.
"""
self.selectAll()
def doDuplicate(self, event):
""" Respond to the "Duplicate" menu command.
"""
self._saveUndoInfo()
objs = []
for obj in self.contents:
if obj in self.selection:
newObj = DrawingObject(obj.getType())
newObj.setData(obj.getData())
pos = obj.getPosition()
newObj.setPosition(wx.Point(pos.x + 10, pos.y + 10))
objs.append(newObj)
self.contents = objs + self.contents
self.selectMany(objs)
def doEditText(self, event):
""" Respond to the "Edit Text" menu command.
"""
if len(self.selection) != 1: return
obj = self.selection[0]
if obj.getType() != obj_TEXT: return
editor = EditTextObjectDialog(self, "Edit Text Object")
editor.objectToDialog(obj)
if editor.ShowModal() == wx.ID_CANCEL:
editor.Destroy()
return
self._saveUndoInfo()
editor.dialogToObject(obj)
editor.Destroy()
self.dirty = True
self.drawPanel.Refresh()
self._adjustMenus()
def doDelete(self, event):
""" Respond to the "Delete" menu command.
"""
self._saveUndoInfo()
for obj in self.selection:
self.contents.remove(obj)
del obj
self.deselectAll()
def doChooseSelectTool(self, event=None):
""" Respond to the "Select Tool" menu command.
"""
self._setCurrentTool(self.selectIcon)
self.drawPanel.SetCursor(wx.STANDARD_CURSOR)
self._adjustMenus()
def doChooseLineTool(self, event=None):
""" Respond to the "Line Tool" menu command.
"""
self._setCurrentTool(self.lineIcon)
self.drawPanel.SetCursor(wx.CROSS_CURSOR)
self.deselectAll()
self._adjustMenus()
def doChooseRectTool(self, event=None):
""" Respond to the "Rect Tool" menu command.
"""
self._setCurrentTool(self.rectIcon)
self.drawPanel.SetCursor(wx.CROSS_CURSOR)
self.deselectAll()
self._adjustMenus()
def doChooseEllipseTool(self, event=None):
""" Respond to the "Ellipse Tool" menu command.
"""
self._setCurrentTool(self.ellipseIcon)
self.drawPanel.SetCursor(wx.CROSS_CURSOR)
self.deselectAll()
self._adjustMenus()
def doChooseTextTool(self, event=None):
""" Respond to the "Text Tool" menu command.
"""
self._setCurrentTool(self.textIcon)
self.drawPanel.SetCursor(wx.CROSS_CURSOR)
self.deselectAll()
self._adjustMenus()
def doMoveForward(self, event):
""" Respond to the "Move Forward" menu command.
"""
if len(self.selection) != 1: return
self._saveUndoInfo()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -