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] 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)