📄 pysketch.py
字号:
obj = self.selection[0]
index = self.contents.index(obj)
if index == 0: return
del self.contents[index]
self.contents.insert(index-1, obj)
self.drawPanel.Refresh()
self._adjustMenus()
def doMoveToFront(self, event):
""" Respond to the "Move to Front" menu command.
"""
if len(self.selection) != 1: return
self._saveUndoInfo()
obj = self.selection[0]
self.contents.remove(obj)
self.contents.insert(0, obj)
self.drawPanel.Refresh()
self._adjustMenus()
def doMoveBackward(self, event):
""" Respond to the "Move Backward" menu command.
"""
if len(self.selection) != 1: return
self._saveUndoInfo()
obj = self.selection[0]
index = self.contents.index(obj)
if index == len(self.contents) - 1: return
del self.contents[index]
self.contents.insert(index+1, obj)
self.drawPanel.Refresh()
self._adjustMenus()
def doMoveToBack(self, event):
""" Respond to the "Move to Back" menu command.
"""
if len(self.selection) != 1: return
self._saveUndoInfo()
obj = self.selection[0]
self.contents.remove(obj)
self.contents.append(obj)
self.drawPanel.Refresh()
self._adjustMenus()
def doShowAbout(self, event):
""" Respond to the "About pySketch" menu command.
"""
dialog = wx.Dialog(self, -1, "About pySketch") # ,
#style=wx.DIALOG_MODAL | wx.STAY_ON_TOP)
dialog.SetBackgroundColour(wx.WHITE)
panel = wx.Panel(dialog, -1)
panel.SetBackgroundColour(wx.WHITE)
panelSizer = wx.BoxSizer(wx.VERTICAL)
boldFont = wx.Font(panel.GetFont().GetPointSize(),
panel.GetFont().GetFamily(),
wx.NORMAL, wx.BOLD)
logo = wx.StaticBitmap(panel, -1, wx.Bitmap("images/logo.bmp",
wx.BITMAP_TYPE_BMP))
lab1 = wx.StaticText(panel, -1, "pySketch")
lab1.SetFont(wx.Font(36, boldFont.GetFamily(), wx.ITALIC, wx.BOLD))
lab1.SetSize(lab1.GetBestSize())
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)
lab2 = wx.StaticText(panel, -1, "A simple object-oriented drawing " + \
"program.")
lab2.SetFont(boldFont)
lab2.SetSize(lab2.GetBestSize())
lab3 = wx.StaticText(panel, -1, "pySketch is completely free " + \
"software; please")
lab3.SetFont(boldFont)
lab3.SetSize(lab3.GetBestSize())
lab4 = wx.StaticText(panel, -1, "feel free to adapt or use this " + \
"in any way you like.")
lab4.SetFont(boldFont)
lab4.SetSize(lab4.GetBestSize())
lab5 = wx.StaticText(panel, -1, "Author: Erik Westra " + \
"(ewestra@wave.co.nz)")
lab5.SetFont(boldFont)
lab5.SetSize(lab5.GetBestSize())
btnOK = wx.Button(panel, wx.ID_OK, "OK")
panelSizer.Add(imageSizer, 0, wx.ALIGN_CENTRE)
panelSizer.Add((10, 10)) # Spacer.
panelSizer.Add(lab2, 0, wx.ALIGN_CENTRE)
panelSizer.Add((10, 10)) # Spacer.
panelSizer.Add(lab3, 0, wx.ALIGN_CENTRE)
panelSizer.Add(lab4, 0, wx.ALIGN_CENTRE)
panelSizer.Add((10, 10)) # Spacer.
panelSizer.Add(lab5, 0, wx.ALIGN_CENTRE)
panelSizer.Add((10, 10)) # Spacer.
panelSizer.Add(btnOK, 0, wx.ALL | wx.ALIGN_CENTRE, 5)
panel.SetAutoLayout(True)
panel.SetSizer(panelSizer)
panelSizer.Fit(panel)
topSizer = wx.BoxSizer(wx.HORIZONTAL)
topSizer.Add(panel, 0, wx.ALL, 10)
dialog.SetAutoLayout(True)
dialog.SetSizer(topSizer)
topSizer.Fit(dialog)
dialog.Centre()
btn = dialog.ShowModal()
dialog.Destroy()
# =============================
# == Object Creation Methods ==
# =============================
def createLine(self, x1, y1, x2, y2):
""" 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)
obj = DrawingObject(obj_LINE, position=wx.Point(topLeftX, topLeftY),
size=wx.Size(botRightX-topLeftX,
botRightY-topLeftY),
penColour=self.penColour,
fillColour=self.fillColour,
lineSize=self.lineSize,
startPt = wx.Point(x1 - topLeftX, y1 - topLeftY),
endPt = wx.Point(x2 - topLeftX, y2 - topLeftY))
self.contents.insert(0, obj)
self.dirty = True
self.doChooseSelectTool()
self.select(obj)
def createRect(self, x, y, width, height):
""" Create a new rectangle object at the given position and size.
"""
self._saveUndoInfo()
obj = DrawingObject(obj_RECT, position=wx.Point(x, y),
size=wx.Size(width, height),
penColour=self.penColour,
fillColour=self.fillColour,
lineSize=self.lineSize)
self.contents.insert(0, obj)
self.dirty = True
self.doChooseSelectTool()
self.select(obj)
def createEllipse(self, x, y, width, height):
""" Create a new ellipse object at the given position and size.
"""
self._saveUndoInfo()
obj = DrawingObject(obj_ELLIPSE, position=wx.Point(x, y),
size=wx.Size(width, height),
penColour=self.penColour,
fillColour=self.fillColour,
lineSize=self.lineSize)
self.contents.insert(0, obj)
self.dirty = True
self.doChooseSelectTool()
self.select(obj)
def createText(self, x, y, width, height):
""" Create a new text object at the given position and size.
"""
editor = EditTextObjectDialog(self, "Create Text Object")
if editor.ShowModal() == wx.ID_CANCEL:
editor.Destroy()
return
self._saveUndoInfo()
obj = DrawingObject(obj_TEXT, position=wx.Point(x, y),
size=wx.Size(width, height))
editor.dialogToObject(obj)
editor.Destroy()
self.contents.insert(0, obj)
self.dirty = True
self.doChooseSelectTool()
self.select(obj)
# =======================
# == Selection Methods ==
# =======================
def selectAll(self):
""" Select every DrawingObject in our document.
"""
self.selection = []
for obj in self.contents:
self.selection.append(obj)
self.drawPanel.Refresh()
self._adjustMenus()
def deselectAll(self):
""" Deselect every DrawingObject in our document.
"""
self.selection = []
self.drawPanel.Refresh()
self._adjustMenus()
def select(self, obj):
""" Select the given DrawingObject within our document.
"""
self.selection = [obj]
self.drawPanel.Refresh()
self._adjustMenus()
def selectMany(self, objs):
""" Select the given list of DrawingObjects.
"""
self.selection = objs
self.drawPanel.Refresh()
self._adjustMenus()
def selectByRectangle(self, x, y, width, height):
""" 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()
# ======================
# == File I/O Methods ==
# ======================
def loadContents(self):
""" Load the contents of our document into memory.
"""
f = open(self.fileName, "rb")
objData = cPickle.load(f)
f.close()
for type, data in objData:
obj = DrawingObject(type)
obj.setData(data)
self.contents.append(obj)
self.dirty = False
self.selection = []
self.undoInfo = None
self.drawPanel.Refresh()
self._adjustMenus()
def saveContents(self):
""" Save the contents of our document to disk.
"""
objData = []
for obj in self.contents:
objData.append([obj.getType(), obj.getData()])
f = open(self.fileName, "wb")
cPickle.dump(objData, f)
f.close()
self.dirty = False
def askIfUserWantsToSave(self, action):
""" 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.
response = wx.MessageBox("Save changes before " + action + "?",
"Confirm", wx.YES_NO | wx.CANCEL, self)
if response == wx.YES:
if self.fileName == None:
fileName = wx.FileSelector("Save File As", "Saving",
default_extension="psk",
wildcard="*.psk",
flags = wx.SAVE | wx.OVERWRITE_PROMPT)
if fileName == "": return False # User cancelled.
self.fileName = fileName
self.saveContents()
return True
elif response == wx.NO:
return True # User doesn't want changes saved.
elif response == wx.CANCEL:
return False # User cancelled.
# =====================
# == Private Methods ==
# =====================
def _adjustMenus(self):
""" 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.
self.fileMenu.Enable(wx.ID_SAVE, canSave)
self.fileMenu.Enable(wx.ID_REVERT, canRevert)
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)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -