From 6095ad7ded2f0785cbc3acc07ad3ffb0051f306e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pauline=20Gom=C3=A9r?= Date: Sat, 5 Nov 2011 19:51:42 +0100 Subject: [PATCH 1/2] Add: Open files in archive from menu --- core/reader.py | 58 +++++++++++++++++++++++++++++++++++++++++------ core/reader_ui.py | 5 ++++ 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/core/reader.py b/core/reader.py index 600dbcf..d01fbca 100644 --- a/core/reader.py +++ b/core/reader.py @@ -36,6 +36,7 @@ class MainWindowReader(QtGui.QMainWindow, Ui_MainWindowReader): self.searchPosition = 0 self.searchText = unicode() self.scanPosition = 0 + self.archiveIndex = None def __init__(self, parent=None, languages=None, filename=None, preferences=None, anki=None, closed=None, updated=None): @@ -355,6 +356,10 @@ class MainWindowReader(QtGui.QMainWindow, Ui_MainWindowReader): # opening an empty tar file raises ReadError with tarfile.open(filename, 'r:*') as tp: files = [f for f in tp.getnames() if tp.getmember(f).isfile()] + names = [f.decode('utf-8') for f in files] + + self.updateArchiveFiles(filename, names) + if len(files) == 0: content = unicode() elif len(files) == 1: @@ -363,33 +368,48 @@ class MainWindowReader(QtGui.QMainWindow, Ui_MainWindowReader): fp.close() else: # Using index because of encoding difficulties - (index, ok) = self.selectItem([f.decode('utf-8') for f in files]) + (index, ok) = self.selectFileName(names) if ok: - fp = tp.extractfile(files[index - 1]) + fp = tp.extractfile(files[index]) content = fp.read() fp.close() + self.state.archiveIndex = index else: content = unicode() else: + self.state.archiveIndex = None with open(filename, 'rb') as fp: content = fp.read() return content - def selectItem(self, list): - items = [self.formatQString(i, x) for i, x in enumerate(list)] + + def selectFileName(self, names): + if self.state.archiveIndex is not None: + return (self.state.archiveIndex, True) + (item, ok) = QtGui.QInputDialog.getItem( self, 'Yomichan', 'Select file to open:', - items, + self.formatQStringList(names), current = 0, editable=False) - (index, success) = item.split('.').first().toInt() - return (index, ok and success) + (index, success) = self.getItemIndex(item) + return (index - 1, ok and success) + + + def getItemIndex(self, item): + return item.split('.').first().toInt() + + + def formatQStringList(self, list): + return [self.formatQString(i, x) for i, x in enumerate(list)] + def formatQString(self, index, item): return QtCore.QString(str(index + 1) + '. ').append(QtCore.QString(item)) + def closeFile(self): self.setWindowTitle('Yomichan') self.textContent.setPlainText(unicode()) @@ -502,6 +522,30 @@ class MainWindowReader(QtGui.QMainWindow, Ui_MainWindowReader): cursor.setPosition(samplePosStart, QtGui.QTextCursor.MoveAnchor) cursor.setPosition(samplePosStart + lengthSelect, QtGui.QTextCursor.KeepAnchor) self.textContent.setTextCursor(cursor) + + + def clearArchiveFiles(self): + self.menuOpenArchive.clear() + self.menuOpenArchive.setEnabled(False) + + + def updateArchiveFiles(self, filename, names): + self.clearArchiveFiles() + + self.menuOpenArchive.setEnabled(True) + for name in self.formatQStringList(names): + (index, ok) = self.getItemIndex(name) + if ok: + index = index - 1 + self.menuOpenArchive.addAction(name, (lambda fn=filename, idx=index: self.openFileInArchive(fn, idx))) + else: + self.menuOpenArchive.addAction(name, (lambda fn=filename: self.openFile(fn))) + + + def openFileInArchive(self, filename, index): + self.state.scanPosition = 0 + self.state.archiveIndex = index + self.openFile(filename) def clearRecentFiles(self): diff --git a/core/reader_ui.py b/core/reader_ui.py index b717b1d..dd9dcee 100644 --- a/core/reader_ui.py +++ b/core/reader_ui.py @@ -39,6 +39,10 @@ class Ui_MainWindowReader(object): self.menuFile = QtGui.QMenu(self.menubar) self.menuFile.setTitle(QtGui.QApplication.translate("MainWindowReader", "&File", None, QtGui.QApplication.UnicodeUTF8)) self.menuFile.setObjectName(_fromUtf8("menuFile")) + self.menuOpenArchive = QtGui.QMenu(self.menuFile) + self.menuOpenArchive.setTitle(QtGui.QApplication.translate("MainWindowReader", "Open from &archive", None, QtGui.QApplication.UnicodeUTF8)) + self.menuOpenArchive.setObjectName(_fromUtf8("menuOpenArchive")) + self.menuOpenArchive.setEnabled(False) self.menuOpenRecent = QtGui.QMenu(self.menuFile) self.menuOpenRecent.setTitle(QtGui.QApplication.translate("MainWindowReader", "Open &recent", None, QtGui.QApplication.UnicodeUTF8)) self.menuOpenRecent.setObjectName(_fromUtf8("menuOpenRecent")) @@ -234,6 +238,7 @@ class Ui_MainWindowReader(object): self.actionFeedback.setText(QtGui.QApplication.translate("MainWindowReader", "&Feedback...", None, QtGui.QApplication.UnicodeUTF8)) self.actionFeedback.setObjectName(_fromUtf8("actionFeedback")) self.menuFile.addAction(self.actionOpen) + self.menuFile.addAction(self.menuOpenArchive.menuAction()) self.menuFile.addAction(self.menuOpenRecent.menuAction()) self.menuFile.addSeparator() self.menuFile.addAction(self.actionQuit) From 1ac03a2e3bcb4397b03ab7b2dd0bf99c3d61cc8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pauline=20Gom=C3=A9r?= Date: Mon, 14 Nov 2011 20:06:45 +0100 Subject: [PATCH 2/2] Fix: Reverse-engineered GUI changes. --- core/reader.py | 3 +-- core/reader_ui.py | 4 ++-- dev/reader.ui | 9 +++++++++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/core/reader.py b/core/reader.py index d01fbca..92a31a7 100644 --- a/core/reader.py +++ b/core/reader.py @@ -352,6 +352,7 @@ class MainWindowReader(QtGui.QMainWindow, Ui_MainWindowReader): self.setWindowTitle(u'Yomichan - {0} ({1})'.format(os.path.split(filename)[1], encoding)) def openFileByExtension(self, filename): + self.clearArchiveFiles() if tarfile.is_tarfile(filename): # opening an empty tar file raises ReadError with tarfile.open(filename, 'r:*') as tp: @@ -530,8 +531,6 @@ class MainWindowReader(QtGui.QMainWindow, Ui_MainWindowReader): def updateArchiveFiles(self, filename, names): - self.clearArchiveFiles() - self.menuOpenArchive.setEnabled(True) for name in self.formatQStringList(names): (index, ok) = self.getItemIndex(name) diff --git a/core/reader_ui.py b/core/reader_ui.py index dd9dcee..b0ca3fb 100644 --- a/core/reader_ui.py +++ b/core/reader_ui.py @@ -2,7 +2,7 @@ # Form implementation generated from reading ui file 'dev/reader.ui' # -# Created: Fri Oct 7 08:55:15 2011 +# Created: Mon Nov 14 19:35:11 2011 # by: PyQt4 UI code generator 4.8.5 # # WARNING! All changes made in this file will be lost! @@ -40,9 +40,9 @@ class Ui_MainWindowReader(object): self.menuFile.setTitle(QtGui.QApplication.translate("MainWindowReader", "&File", None, QtGui.QApplication.UnicodeUTF8)) self.menuFile.setObjectName(_fromUtf8("menuFile")) self.menuOpenArchive = QtGui.QMenu(self.menuFile) + self.menuOpenArchive.setEnabled(False) self.menuOpenArchive.setTitle(QtGui.QApplication.translate("MainWindowReader", "Open from &archive", None, QtGui.QApplication.UnicodeUTF8)) self.menuOpenArchive.setObjectName(_fromUtf8("menuOpenArchive")) - self.menuOpenArchive.setEnabled(False) self.menuOpenRecent = QtGui.QMenu(self.menuFile) self.menuOpenRecent.setTitle(QtGui.QApplication.translate("MainWindowReader", "Open &recent", None, QtGui.QApplication.UnicodeUTF8)) self.menuOpenRecent.setObjectName(_fromUtf8("menuOpenRecent")) diff --git a/dev/reader.ui b/dev/reader.ui index 4316ca3..350ef92 100644 --- a/dev/reader.ui +++ b/dev/reader.ui @@ -47,12 +47,21 @@ &File + + + false + + + Open from &archive + + Open &recent +