📄 docview.py
字号:
#----------------------------------------------------------------------------
# Name: docview.py
# Purpose: Port of the wxWindows docview classes
#
# Author: Peter Yared
#
# Created: 5/15/03
# CVS-ID: $Id: docview.py,v 1.16 2006/06/11 00:13:01 RD Exp $
# Copyright: (c) 2003-2006 ActiveGrid, Inc. (Port of wxWindows classes by Julian Smart et al)
# License: wxWindows license
#----------------------------------------------------------------------------
import os
import os.path
import shutil
import wx
import sys
_ = wx.GetTranslation
#----------------------------------------------------------------------
# docview globals
#----------------------------------------------------------------------
DOC_SDI = 1
DOC_MDI = 2
DOC_NEW = 4
DOC_SILENT = 8
DOC_OPEN_ONCE = 16
DOC_NO_VIEW = 32
DEFAULT_DOCMAN_FLAGS = DOC_SDI & DOC_OPEN_ONCE
TEMPLATE_VISIBLE = 1
TEMPLATE_INVISIBLE = 2
TEMPLATE_NO_CREATE = (4 | TEMPLATE_VISIBLE)
DEFAULT_TEMPLATE_FLAGS = TEMPLATE_VISIBLE
MAX_FILE_HISTORY = 9
#----------------------------------------------------------------------
# Convenience functions from wxWindows used in docview
#----------------------------------------------------------------------
def FileNameFromPath(path):
"""
Returns the filename for a full path.
"""
return os.path.split(path)[1]
def FindExtension(path):
"""
Returns the extension of a filename for a full path.
"""
return os.path.splitext(path)[1].lower()
def FileExists(path):
"""
Returns True if the path exists.
"""
return os.path.isfile(path)
def PathOnly(path):
"""
Returns the path of a full path without the filename.
"""
return os.path.split(path)[0]
#----------------------------------------------------------------------
# Document/View Classes
#----------------------------------------------------------------------
class Document(wx.EvtHandler):
"""
The document class can be used to model an application's file-based data. It
is part of the document/view framework supported by wxWindows, and cooperates
with the wxView, wxDocTemplate and wxDocManager classes.
Note this wxPython version also keeps track of the modification date of the
document and if it changes on disk outside of the application, we will warn the
user before saving to avoid clobbering the file.
"""
def __init__(self, parent=None):
"""
Constructor. Define your own default constructor to initialize
application-specific data.
"""
wx.EvtHandler.__init__(self)
self._documentParent = parent
self._documentTemplate = None
self._commandProcessor = None
self._savedYet = False
self._writeable = True
self._documentTitle = None
self._documentFile = None
self._documentTypeName = None
self._documentModified = False
self._documentModificationDate = None
self._documentViews = []
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.
"""
return False
def GetFilename(self):
"""
Gets the filename associated with this document, or "" if none is
associated.
"""
return self._documentFile
def GetTitle(self):
"""
Gets the title for this document. The document title is used for an
associated frame (if any), and is usually constructed by the framework
from the filename.
"""
return self._documentTitle
def SetTitle(self, title):
"""
Sets the title for this document. The document title is used for an
associated frame (if any), and is usually constructed by the framework
from the filename.
"""
self._documentTitle = title
def GetDocumentName(self):
"""
The document type name given to the wxDocTemplate constructor,
copied to this document when the document is created. If several
document templates are created that use the same document type, this
variable is used in wxDocManager::CreateView to collate a list of
alternative view types that can be used on this kind of document.
"""
return self._documentTypeName
def SetDocumentName(self, name):
"""
Sets he document type name given to the wxDocTemplate constructor,
copied to this document when the document is created. If several
document templates are created that use the same document type, this
variable is used in wxDocManager::CreateView to collate a list of
alternative view types that can be used on this kind of document. Do
not change the value of this variable.
"""
self._documentTypeName = name
def GetDocumentSaved(self):
"""
Returns True if the document has been saved. This method has been
added to wxPython and is not in wxWindows.
"""
return self._savedYet
def SetDocumentSaved(self, saved=True):
"""
Sets whether the document has been saved. This method has been
added to wxPython and is not in wxWindows.
"""
self._savedYet = saved
def GetCommandProcessor(self):
"""
Returns the command processor associated with this document.
"""
return self._commandProcessor
def SetCommandProcessor(self, processor):
"""
Sets the command processor to be used for this document. The document
will then be responsible for its deletion. Normally you should not
call this; override OnCreateCommandProcessor instead.
"""
self._commandProcessor = processor
def IsModified(self):
"""
Returns true if the document has been modified since the last save,
false otherwise. You may need to override this if your document view
maintains its own record of being modified (for example if using
wxTextWindow to view and edit the document).
"""
return self._documentModified
def Modify(self, modify):
"""
Call with true to mark the document as modified since the last save,
false otherwise. You may need to override this if your document view
maintains its own record of being modified (for example if using
xTextWindow to view and edit the document).
This method has been extended to notify its views that the dirty flag has changed.
"""
self._documentModified = modify
self.UpdateAllViews(hint=("modify", self, self._documentModified))
def SetDocumentModificationDate(self):
"""
Saves the file's last modification date.
This is used to check if the file has been modified outside of the application.
This method has been added to wxPython and is not in wxWindows.
"""
self._documentModificationDate = os.path.getmtime(self.GetFilename())
def GetDocumentModificationDate(self):
"""
Returns the file's modification date when it was loaded from disk.
This is used to check if the file has been modified outside of the application.
This method has been added to wxPython and is not in wxWindows.
"""
return self._documentModificationDate
def IsDocumentModificationDateCorrect(self):
"""
Returns False if the file has been modified outside of the application.
This method has been added to wxPython and is not in wxWindows.
"""
if not os.path.exists(self.GetFilename()): # document must be in memory only and can't be out of date
return True
return self._documentModificationDate == os.path.getmtime(self.GetFilename())
def GetViews(self):
"""
Returns the list whose elements are the views on the document.
"""
return self._documentViews
def GetDocumentTemplate(self):
"""
Returns the template that created the document.
"""
return self._documentTemplate
def SetDocumentTemplate(self, template):
"""
Sets the template that created the document. Should only be called by
the framework.
"""
self._documentTemplate = template
def DeleteContents(self):
"""
Deletes the contents of the document. Override this method as
necessary.
"""
return True
def Destroy(self):
"""
Destructor. Removes itself from the document manager.
"""
self.DeleteContents()
self._documentModificationDate = None
if self.GetDocumentManager():
self.GetDocumentManager().RemoveDocument(self)
wx.EvtHandler.Destroy(self)
def Close(self):
"""
Closes the document, by calling OnSaveModified and then (if this true)
OnCloseDocument. This does not normally delete the document object:
use DeleteAllViews to do this implicitly.
"""
if self.OnSaveModified():
if self.OnCloseDocument():
return True
else:
return False
else:
return False
def OnCloseDocument(self):
"""
The default implementation calls DeleteContents (an empty
implementation) sets the modified flag to false. Override this to
supply additional behaviour when the document is closed with Close.
"""
self.NotifyClosing()
self.DeleteContents()
self.Modify(False)
return True
def DeleteAllViews(self):
"""
Calls wxView.Close and deletes each view. Deleting the final view will
implicitly delete the document itself, because the wxView destructor
calls RemoveView. This in turns calls wxDocument::OnChangedViewList,
whose default implemention is to save and delete the document if no
views exist.
"""
manager = self.GetDocumentManager()
for view in self._documentViews:
if not view.Close():
return False
if self in manager.GetDocuments():
self.Destroy()
return True
def GetFirstView(self):
"""
A convenience function to get the first view for a document, because
in many cases a document will only have a single view.
"""
if len(self._documentViews) == 0:
return None
return self._documentViews[0]
def GetDocumentManager(self):
"""
Returns the associated document manager.
"""
if self._documentTemplate:
return self._documentTemplate.GetDocumentManager()
return None
def OnNewDocument(self):
"""
The default implementation calls OnSaveModified and DeleteContents,
makes a default title for the document, and notifies the views that
the filename (in fact, the title) has changed.
"""
if not self.OnSaveModified() or not self.OnCloseDocument():
return False
self.DeleteContents()
self.Modify(False)
self.SetDocumentSaved(False)
name = self.GetDocumentManager().MakeDefaultName()
self.SetTitle(name)
self.SetFilename(name, notifyViews = True)
def Save(self):
"""
Saves the document by calling OnSaveDocument if there is an associated
filename, or SaveAs if there is no filename.
"""
if not self.IsModified(): # and self._savedYet: This was here, but if it is not modified who cares if it hasn't been saved yet?
return True
""" check for file modification outside of application """
if not self.IsDocumentModificationDateCorrect():
msgTitle = wx.GetApp().GetAppName()
if not msgTitle:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -