From cc3eea535f893b7764c5be249e0ecbee495f013c Mon Sep 17 00:00:00 2001 From: Bastian Kleineidam Date: Fri, 5 Nov 2010 01:11:28 +0100 Subject: [PATCH] Save parent URL source in local file. --- doc/changelog.txt | 2 +- linkcheck/gui/editor.py | 73 ++++++++++++++++++++++---- linkcheck/gui/linkchecker_ui_editor.py | 30 ++++++++++- linkcheck/gui/ui/editor.ui | 47 ++++++++++++++++- 4 files changed, 139 insertions(+), 13 deletions(-) diff --git a/doc/changelog.txt b/doc/changelog.txt index c57222c4..3b5cc489 100644 --- a/doc/changelog.txt +++ b/doc/changelog.txt @@ -16,7 +16,7 @@ Changes: redirects any more. Features: - +- gui: Add command to save the parent URL source in a local file. 5.4 "How to train your dragon" (released 26.10.2010) diff --git a/linkcheck/gui/editor.py b/linkcheck/gui/editor.py index 7df45543..0a7e8351 100644 --- a/linkcheck/gui/editor.py +++ b/linkcheck/gui/editor.py @@ -15,7 +15,7 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -from PyQt4 import Qsci, QtGui +from PyQt4 import Qsci, QtGui, QtCore from .linkchecker_ui_editor import Ui_EditorDialog @@ -23,7 +23,7 @@ ContentTypeLexers = { "application/x-shellscript": Qsci.QsciLexerBash, "application/x-sh": Qsci.QsciLexerBash, "application/x-msdos-program": Qsci.QsciLexerBatch, - "": Qsci.QsciLexerCMake, + #"": Qsci.QsciLexerCMake, "text/x-c++src": Qsci.QsciLexerCPP, "text/css": Qsci.QsciLexerCSS, #"": Qsci.QsciLexerCSharp, @@ -41,8 +41,8 @@ ContentTypeLexers = { #"": Qsci.QsciLexerPOV, "text/x-pascal": Qsci.QsciLexerPascal, "text/x-perl": Qsci.QsciLexerPerl, - "": Qsci.QsciLexerPostScript, - "": Qsci.QsciLexerProperties, + "application/postscript": Qsci.QsciLexerPostScript, + "text/plain+ini": Qsci.QsciLexerProperties, "text/x-python": Qsci.QsciLexerPython, "application/x-ruby": Qsci.QsciLexerRuby, #"": Qsci.QsciLexerSQL, @@ -94,18 +94,19 @@ class Editor (Qsci.QsciScintilla): # folding margin colors (foreground,background) self.setFoldMarginColors(QtGui.QColor("#f5f5dc"),QtGui.QColor("#aaaaaa")) - # No editing (yet) in this window, since the source is taken - # from an online URL. - # XXX allow editing and saving to a local file. - self.setReadOnly(True) class EditorWindow (QtGui.QDialog, Ui_EditorDialog): def __init__ (self, parent=None): super(EditorWindow, self).__init__(parent) self.setupUi(self) - self.editor = Editor(parent=self) - self.verticalLayout.addWidget(self.editor) + # filename used for saving + self.filename = None + # the Scintilla editor widget + self.editor = Editor(parent=self.frame) + layout = QtGui.QVBoxLayout(self.frame) + layout.setMargin(0) + layout.addWidget(self.editor) # for debugging #self.setText(("1234567890"*8) + "\n\nx\n\n") #lexer = Qsci.QsciLexerHTML() @@ -128,3 +129,55 @@ class EditorWindow (QtGui.QDialog, Ui_EditorDialog): def setText (self, text, line=1, col=1): self.editor.setText(text) self.editor.setCursorPosition(line-1, col-1) + self.editor.setModified(False) + + @QtCore.pyqtSignature("") + def on_actionSave_triggered (self): + """Save editor contents.""" + if self.editor.isModified() or not self.filename: + self.save() + + def save (self): + if not self.filename: + res = QtGui.QFileDialog.getSaveFileName(self, "Save File As") + if not res: + return + self.filename = res + self.setWindowTitle(self.filename) + fh = None + try: + try: + fh = QtCore.QFile(self.filename) + if not fh.open(QtCore.QIODevice.WriteOnly): + raise IOError(fh.errorString()) + stream = QtCore.QTextStream(fh) + stream.setCodec("UTF-8") + stream << self.editor.text() + self.editor.setModified(False) + except (IOError, OSError), e: + err = QtGui.QMessageBox(self) + err.setText(str(e)) + err.exec_() + finally: + if fh is not None: + fh.close() + + def load (self, filename): + self.filename = filename + self.setWindowTitle(self.filename) + fh = None + try: + try: + fh = QtCore.QFile(self.filename) + if not fh.open(QtCore.QIODevice.ReadOnly): + raise IOError(fh.errorString()) + stream = QtCore.QTextStream(fh) + stream.setCodec("UTF-8") + self.setText(stream.readAll()) + except (IOError, OSError), e: + err = QtGui.QMessageBox(self) + err.setText(str(e)) + err.exec_() + finally: + if fh is not None: + fh.close() diff --git a/linkcheck/gui/linkchecker_ui_editor.py b/linkcheck/gui/linkchecker_ui_editor.py index be3213d2..0e115aee 100644 --- a/linkcheck/gui/linkchecker_ui_editor.py +++ b/linkcheck/gui/linkchecker_ui_editor.py @@ -2,7 +2,7 @@ # Form implementation generated from reading ui file 'ui/editor.ui' # -# Created: Sun Oct 3 09:27:19 2010 +# Created: Fri Nov 5 00:55:45 2010 # by: PyQt4 UI code generator 4.7.3 # # WARNING! All changes made in this file will be lost! @@ -14,12 +14,40 @@ class Ui_EditorDialog(object): EditorDialog.setObjectName("EditorDialog") EditorDialog.setWindowModality(QtCore.Qt.ApplicationModal) EditorDialog.resize(640, 600) + sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Preferred) + sizePolicy.setHorizontalStretch(0) + sizePolicy.setVerticalStretch(0) + sizePolicy.setHeightForWidth(EditorDialog.sizePolicy().hasHeightForWidth()) + EditorDialog.setSizePolicy(sizePolicy) self.verticalLayout = QtGui.QVBoxLayout(EditorDialog) + self.verticalLayout.setMargin(0) self.verticalLayout.setObjectName("verticalLayout") + self.menubar = QtGui.QMenuBar(EditorDialog) + self.menubar.setObjectName("menubar") + self.menuFile = QtGui.QMenu(self.menubar) + self.menuFile.setObjectName("menuFile") + self.verticalLayout.addWidget(self.menubar) + self.frame = QtGui.QFrame(EditorDialog) + sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding) + sizePolicy.setHorizontalStretch(0) + sizePolicy.setVerticalStretch(0) + sizePolicy.setHeightForWidth(self.frame.sizePolicy().hasHeightForWidth()) + self.frame.setSizePolicy(sizePolicy) + self.frame.setFrameShape(QtGui.QFrame.NoFrame) + self.frame.setFrameShadow(QtGui.QFrame.Plain) + self.frame.setLineWidth(0) + self.frame.setObjectName("frame") + self.verticalLayout.addWidget(self.frame) + self.actionSave = QtGui.QAction(EditorDialog) + self.actionSave.setObjectName("actionSave") + self.menuFile.addAction(self.actionSave) + self.menubar.addAction(self.menuFile.menuAction()) self.retranslateUi(EditorDialog) QtCore.QMetaObject.connectSlotsByName(EditorDialog) def retranslateUi(self, EditorDialog): EditorDialog.setWindowTitle(_("LinkChecker source view")) + self.menuFile.setTitle(_("File")) + self.actionSave.setText(_("Save")) diff --git a/linkcheck/gui/ui/editor.ui b/linkcheck/gui/ui/editor.ui index ba122018..f8c53e13 100644 --- a/linkcheck/gui/ui/editor.ui +++ b/linkcheck/gui/ui/editor.ui @@ -13,10 +13,55 @@ 600 + + + 0 + 0 + + LinkChecker source view - + + + 0 + + + + + + File + + + + + + + + + + + 0 + 0 + + + + QFrame::NoFrame + + + QFrame::Plain + + + 0 + + + + + + + Save + +