📄 docview.py
字号:
to wxPython and is not in wxWindows.
"""
self._writeable = writeable
class View(wx.EvtHandler):
"""
The view class can be used to model the viewing and editing component of
an application's file-based data. It is part of the document/view
framework supported by wxWindows, and cooperates with the wxDocument,
wxDocTemplate and wxDocManager classes.
"""
def __init__(self):
"""
Constructor. Define your own default constructor to initialize
application-specific data.
"""
wx.EvtHandler.__init__(self)
self._viewDocument = None
self._viewFrame = None
def Destroy(self):
"""
Destructor. Removes itself from the document's list of views.
"""
if self._viewDocument:
self._viewDocument.RemoveView(self)
wx.EvtHandler.Destroy(self)
def ProcessEvent(self, event):
"""
Processes an event, searching event tables and calling zero or more
suitable event handler function(s). Note that the ProcessEvent
method is called from the wxPython docview framework directly since
wxPython does not have a virtual ProcessEvent function.
"""
if not self.GetDocument() or not self.GetDocument().ProcessEvent(event):
return False
else:
return True
def ProcessUpdateUIEvent(self, event):
"""
Processes a UI event, searching event tables and calling zero or more
suitable event handler function(s). Note that the ProcessEvent
method is called from the wxPython docview framework directly since
wxPython does not have a virtual ProcessEvent function.
"""
return False
def OnActivateView(self, activate, activeView, deactiveView):
"""
Called when a view is activated by means of wxView::Activate. The
default implementation does nothing.
"""
pass
def OnClosingDocument(self):
"""
Override this to clean up the view when the document is being closed.
The default implementation does nothing.
"""
pass
def OnDraw(self, dc):
"""
Override this to draw the view for the printing framework. The
default implementation does nothing.
"""
pass
def OnPrint(self, dc, info):
"""
Override this to print the view for the printing framework. The
default implementation calls View.OnDraw.
"""
self.OnDraw(dc)
def OnUpdate(self, sender, hint):
"""
Called when the view should be updated. sender is a pointer to the
view that sent the update request, or NULL if no single view requested
the update (for instance, when the document is opened). hint is as yet
unused but may in future contain application-specific information for
making updating more efficient.
"""
if hint:
if hint[0] == "modify": # if dirty flag changed, update the view's displayed title
frame = self.GetFrame()
if frame and hasattr(frame, "OnTitleIsModified"):
frame.OnTitleIsModified()
return True
return False
def OnChangeFilename(self):
"""
Called when the filename has changed. The default implementation
constructs a suitable title and sets the title of the view frame (if
any).
"""
if self.GetFrame():
appName = wx.GetApp().GetAppName()
if not self.GetDocument():
if appName:
title = appName
else:
return
else:
if appName and isinstance(self.GetFrame(), DocChildFrame): # Only need app name in title for SDI
title = appName + _(" - ")
else:
title = ''
self.GetFrame().SetTitle(title + self.GetDocument().GetPrintableName())
def GetDocument(self):
"""
Returns the document associated with the view.
"""
return self._viewDocument
def SetDocument(self, doc):
"""
Associates the given document with the view. Normally called by the
framework.
"""
self._viewDocument = doc
if doc:
doc.AddView(self)
def GetViewName(self):
"""
Gets the name associated with the view (passed to the wxDocTemplate
constructor). Not currently used by the framework.
"""
return self._viewTypeName
def SetViewName(self, name):
"""
Sets the view type name. Should only be called by the framework.
"""
self._viewTypeName = name
def Close(self, deleteWindow=True):
"""
Closes the view by calling OnClose. If deleteWindow is true, this
function should delete the window associated with the view.
"""
if self.OnClose(deleteWindow = deleteWindow):
return True
else:
return False
def Activate(self, activate=True):
"""
Call this from your view frame's OnActivate member to tell the
framework which view is currently active. If your windowing system
doesn't call OnActivate, you may need to call this function from
OnMenuCommand or any place where you know the view must be active, and
the framework will need to get the current view.
The prepackaged view frame wxDocChildFrame calls wxView.Activate from
its OnActivate member and from its OnMenuCommand member.
"""
if self.GetDocument() and self.GetDocumentManager():
self.OnActivateView(activate, self, self.GetDocumentManager().GetCurrentView())
self.GetDocumentManager().ActivateView(self, activate)
def OnClose(self, deleteWindow=True):
"""
Implements closing behaviour. The default implementation calls
wxDocument.Close to close the associated document. Does not delete the
view. The application may wish to do some cleaning up operations in
this function, if a call to wxDocument::Close succeeded. For example,
if your application's all share the same window, you need to
disassociate the window from the view and perhaps clear the window. If
deleteWindow is true, delete the frame associated with the view.
"""
if self.GetDocument():
return self.GetDocument().Close()
else:
return True
def OnCreate(self, doc, flags):
"""
wxDocManager or wxDocument creates a wxView via a wxDocTemplate. Just
after the wxDocTemplate creates the wxView, it calls wxView::OnCreate.
In its OnCreate member function, the wxView can create a
wxDocChildFrame or a derived class. This wxDocChildFrame provides user
interface elements to view and/or edit the contents of the wxDocument.
By default, simply returns true. If the function returns false, the
view will be deleted.
"""
return True
def OnCreatePrintout(self):
"""
Returns a wxPrintout object for the purposes of printing. It should
create a new object every time it is called; the framework will delete
objects it creates.
By default, this function returns an instance of wxDocPrintout, which
prints and previews one page by calling wxView.OnDraw.
Override to return an instance of a class other than wxDocPrintout.
"""
return DocPrintout(self, self.GetDocument().GetPrintableName())
def GetFrame(self):
"""
Gets the frame associated with the view (if any). Note that this
"frame" is not a wxFrame at all in the generic MDI implementation
which uses the notebook pages instead of the frames and this is why
this method returns a wxWindow and not a wxFrame.
"""
return self._viewFrame
def SetFrame(self, frame):
"""
Sets the frame associated with this view. The application should call
this if possible, to tell the view about the frame. See GetFrame for
the explanation about the mismatch between the "Frame" in the method
name and the type of its parameter.
"""
self._viewFrame = frame
def GetDocumentManager(self):
"""
Returns the document manager instance associated with this view.
"""
if self._viewDocument:
return self.GetDocument().GetDocumentManager()
else:
return None
class DocTemplate(wx.Object):
"""
The wxDocTemplate class is used to model the relationship between a
document class and a view class.
"""
def __init__(self, manager, description, filter, dir, ext, docTypeName, viewTypeName, docType, viewType, flags=DEFAULT_TEMPLATE_FLAGS, icon=None):
"""
Constructor. Create instances dynamically near the start of your
application after creating a wxDocManager instance, and before doing
any document or view operations.
manager is the document manager object which manages this template.
description is a short description of what the template is for. This
string will be displayed in the file filter list of Windows file
selectors.
filter is an appropriate file filter such as \*.txt.
dir is the default directory to use for file selectors.
ext is the default file extension (such as txt).
docTypeName is a name that should be unique for a given type of
document, used for gathering a list of views relevant to a
particular document.
viewTypeName is a name that should be unique for a given view.
docClass is a Python class. If this is not supplied, you will need to
derive a new wxDocTemplate class and override the CreateDocument
member to return a new document instance on demand.
viewClass is a Python class. If this is not supplied, you will need to
derive a new wxDocTemplate class and override the CreateView member to
return a new view instance on demand.
flags is a bit list of the following:
wx.TEMPLATE_VISIBLE The template may be displayed to the user in
dialogs.
wx.TEMPLATE_INVISIBLE The template may not be displayed to the user in
dialogs.
wx.DEFAULT_TEMPLATE_FLAGS Defined as wxTEMPLATE_VISIBLE.
"""
self._docManager = manager
self._description = description
self._fileFilter = filter
self._directory = dir
self._defaultExt = ext
self._docTypeName = docTypeName
self._viewTypeName = viewTypeName
self._docType = docType
self._viewType = viewType
self._flags = flags
self._icon = icon
self._docManager.AssociateTemplate(self)
def GetDefaultExtension(self):
"""
Returns the default file extension for the document data, as passed to
the document template constructor.
"""
return self._defaultExt
def SetDefaultExtension(self, defaultExt):
"""
Sets the default file extension.
"""
self._defaultExt = defaultExt
def GetDescription(self):
"""
Returns the text description of this template, as passed to the
document template constructor.
"""
return self._description
def SetDescription(self, description):
"""
Sets the template description.
"""
self._description = description
def GetDirectory(self):
"""
Returns the default directory, as passed to the document template
constructor.
"""
return self._directory
def SetDirectory(self, dir):
"""
Sets the default directory.
"""
self._directory = dir
def GetDocumentManager(self):
"""
Returns the document manager instance for which this template was
created.
"""
return self._docManager
def SetDocumentManager(self, manager):
"""
Sets the document manager instance for which this template was
created. Should not be called by the application.
"""
self._docManager = manager
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -