📄 docview.py
字号:
def GetFileFilter(self):
"""
Returns the file filter, as passed to the document template
constructor.
"""
return self._fileFilter
def SetFileFilter(self, filter):
"""
Sets the file filter.
"""
self._fileFilter = filter
def GetFlags(self):
"""
Returns the flags, as passed to the document template constructor.
(see the constructor description for more details).
"""
return self._flags
def SetFlags(self, flags):
"""
Sets the internal document template flags (see the constructor
description for more details).
"""
self._flags = flags
def GetIcon(self):
"""
Returns the icon, as passed to the document template
constructor. This method has been added to wxPython and is
not in wxWindows.
"""
return self._icon
def SetIcon(self, flags):
"""
Sets the icon. This method has been added to wxPython and is not
in wxWindows.
"""
self._icon = icon
def GetDocumentType(self):
"""
Returns the Python document class, as passed to the document template
constructor.
"""
return self._docType
def GetViewType(self):
"""
Returns the Python view class, as passed to the document template
constructor.
"""
return self._viewType
def IsVisible(self):
"""
Returns true if the document template can be shown in user dialogs,
false otherwise.
"""
return (self._flags & TEMPLATE_VISIBLE) == TEMPLATE_VISIBLE
def IsNewable(self):
"""
Returns true if the document template can be shown in "New" dialogs,
false otherwise.
This method has been added to wxPython and is not in wxWindows.
"""
return (self._flags & TEMPLATE_NO_CREATE) != TEMPLATE_NO_CREATE
def GetDocumentName(self):
"""
Returns the document type name, as passed to the document template
constructor.
"""
return self._docTypeName
def GetViewName(self):
"""
Returns the view type name, as passed to the document template
constructor.
"""
return self._viewTypeName
def CreateDocument(self, path, flags):
"""
Creates a new instance of the associated document class. If you have
not supplied a class to the template constructor, you will need to
override this function to return an appropriate document instance.
"""
doc = self._docType()
doc.SetFilename(path)
doc.SetDocumentTemplate(self)
self.GetDocumentManager().AddDocument(doc)
doc.SetCommandProcessor(doc.OnCreateCommandProcessor())
if doc.OnCreate(path, flags):
return doc
else:
if doc in self.GetDocumentManager().GetDocuments():
doc.DeleteAllViews()
return None
def CreateView(self, doc, flags):
"""
Creates a new instance of the associated document view. If you have
not supplied a class to the template constructor, you will need to
override this function to return an appropriate view instance.
"""
view = self._viewType()
view.SetDocument(doc)
if view.OnCreate(doc, flags):
return view
else:
view.Destroy()
return None
def FileMatchesTemplate(self, path):
"""
Returns True if the path's extension matches one of this template's
file filter extensions.
"""
ext = FindExtension(path)
if not ext: return False
extList = self.GetFileFilter().replace('*','').split(';')
return ext in extList
class DocManager(wx.EvtHandler):
"""
The wxDocManager class is part of the document/view framework supported by
wxWindows, and cooperates with the wxView, wxDocument and wxDocTemplate
classes.
"""
def __init__(self, flags=DEFAULT_DOCMAN_FLAGS, initialize=True):
"""
Constructor. Create a document manager instance dynamically near the
start of your application before doing any document or view operations.
flags is used in the Python version to indicate whether the document
manager is in DOC_SDI or DOC_MDI mode.
If initialize is true, the Initialize function will be called to
create a default history list object. If you derive from wxDocManager,
you may wish to call the base constructor with false, and then call
Initialize in your own constructor, to allow your own Initialize or
OnCreateFileHistory functions to be called.
"""
wx.EvtHandler.__init__(self)
self._defaultDocumentNameCounter = 1
self._flags = flags
self._currentView = None
self._lastActiveView = None
self._maxDocsOpen = 10000
self._fileHistory = None
self._templates = []
self._docs = []
self._lastDirectory = ""
if initialize:
self.Initialize()
wx.EVT_MENU(self, wx.ID_OPEN, self.OnFileOpen)
wx.EVT_MENU(self, wx.ID_CLOSE, self.OnFileClose)
wx.EVT_MENU(self, wx.ID_CLOSE_ALL, self.OnFileCloseAll)
wx.EVT_MENU(self, wx.ID_REVERT, self.OnFileRevert)
wx.EVT_MENU(self, wx.ID_NEW, self.OnFileNew)
wx.EVT_MENU(self, wx.ID_SAVE, self.OnFileSave)
wx.EVT_MENU(self, wx.ID_SAVEAS, self.OnFileSaveAs)
wx.EVT_MENU(self, wx.ID_UNDO, self.OnUndo)
wx.EVT_MENU(self, wx.ID_REDO, self.OnRedo)
wx.EVT_MENU(self, wx.ID_PRINT, self.OnPrint)
wx.EVT_MENU(self, wx.ID_PRINT_SETUP, self.OnPrintSetup)
wx.EVT_MENU(self, wx.ID_PREVIEW, self.OnPreview)
wx.EVT_UPDATE_UI(self, wx.ID_OPEN, self.OnUpdateFileOpen)
wx.EVT_UPDATE_UI(self, wx.ID_CLOSE, self.OnUpdateFileClose)
wx.EVT_UPDATE_UI(self, wx.ID_CLOSE_ALL, self.OnUpdateFileCloseAll)
wx.EVT_UPDATE_UI(self, wx.ID_REVERT, self.OnUpdateFileRevert)
wx.EVT_UPDATE_UI(self, wx.ID_NEW, self.OnUpdateFileNew)
wx.EVT_UPDATE_UI(self, wx.ID_SAVE, self.OnUpdateFileSave)
wx.EVT_UPDATE_UI(self, wx.ID_SAVEAS, self.OnUpdateFileSaveAs)
wx.EVT_UPDATE_UI(self, wx.ID_UNDO, self.OnUpdateUndo)
wx.EVT_UPDATE_UI(self, wx.ID_REDO, self.OnUpdateRedo)
wx.EVT_UPDATE_UI(self, wx.ID_PRINT, self.OnUpdatePrint)
wx.EVT_UPDATE_UI(self, wx.ID_PRINT_SETUP, self.OnUpdatePrintSetup)
wx.EVT_UPDATE_UI(self, wx.ID_PREVIEW, self.OnUpdatePreview)
def Destroy(self):
"""
Destructor.
"""
self.Clear()
wx.EvtHandler.Destroy(self)
def GetFlags(self):
"""
Returns the document manager's flags. This method has been
added to wxPython and is not in wxWindows.
"""
return self._flags
def CloseDocument(self, doc, force=True):
"""
Closes the specified document.
"""
if doc.Close() or force:
doc.DeleteAllViews()
if doc in self._docs:
doc.Destroy()
return True
return False
def CloseDocuments(self, force=True):
"""
Closes all currently opened documents.
"""
for document in self._docs[::-1]: # Close in lifo (reverse) order. We clone the list to make sure we go through all docs even as they are deleted
if not self.CloseDocument(document, force):
return False
document.DeleteAllViews() # Implicitly delete the document when the last view is removed
return True
def Clear(self, force=True):
"""
Closes all currently opened document by callling CloseDocuments and
clears the document manager's templates.
"""
if not self.CloseDocuments(force):
return False
self._templates = []
return True
def Initialize(self):
"""
Initializes data; currently just calls OnCreateFileHistory. Some data
cannot always be initialized in the constructor because the programmer
must be given the opportunity to override functionality. In fact
Initialize is called from the wxDocManager constructor, but this can
be vetoed by passing false to the second argument, allowing the
derived class's constructor to call Initialize, possibly calling a
different OnCreateFileHistory from the default.
The bottom line: if you're not deriving from Initialize, forget it and
construct wxDocManager with no arguments.
"""
self.OnCreateFileHistory()
return True
def OnCreateFileHistory(self):
"""
A hook to allow a derived class to create a different type of file
history. Called from Initialize.
"""
self._fileHistory = wx.FileHistory()
def OnFileClose(self, event):
"""
Closes and deletes the currently active document.
"""
doc = self.GetCurrentDocument()
if doc:
doc.DeleteAllViews()
if doc in self._docs:
self._docs.remove(doc)
def OnFileCloseAll(self, event):
"""
Closes and deletes all the currently opened documents.
"""
return self.CloseDocuments(force = False)
def OnFileNew(self, event):
"""
Creates a new document and reads in the selected file.
"""
self.CreateDocument('', DOC_NEW)
def OnFileOpen(self, event):
"""
Creates a new document and reads in the selected file.
"""
if not self.CreateDocument('', DEFAULT_DOCMAN_FLAGS):
self.OnOpenFileFailure()
def OnFileRevert(self, event):
"""
Reverts the current document by calling wxDocument.Save for the current
document.
"""
doc = self.GetCurrentDocument()
if not doc:
return
doc.Revert()
def OnFileSave(self, event):
"""
Saves the current document by calling wxDocument.Save for the current
document.
"""
doc = self.GetCurrentDocument()
if not doc:
return
doc.Save()
def OnFileSaveAs(self, event):
"""
Calls wxDocument.SaveAs for the current document.
"""
doc = self.GetCurrentDocument()
if not doc:
return
doc.SaveAs()
def OnPrint(self, event):
"""
Prints the current document by calling its View's OnCreatePrintout
method.
"""
view = self.GetCurrentView()
if not view:
return
printout = view.OnCreatePrintout()
if printout:
if not hasattr(self, "printData"):
self.printData = wx.PrintData()
self.printData.SetPaperId(wx.PAPER_LETTER)
self.printData.SetPrintMode(wx.PRINT_MODE_PRINTER)
pdd = wx.PrintDialogData(self.printData)
printer = wx.Printer(pdd)
printer.Print(view.GetFrame(), printout)
def OnPrintSetup(self, event):
"""
Presents the print setup dialog.
"""
view = self.GetCurrentView()
if view:
parentWin = view.GetFrame()
else:
parentWin = wx.GetApp().GetTopWindow()
if not hasattr(self, "printData"):
self.printData = wx.PrintData()
self.printData.SetPaperId(wx.PAPER_LETTER)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -