1

Add: Open files in archive from menu

This commit is contained in:
Pauline Gomér 2011-11-05 19:51:42 +01:00
parent d440b58f10
commit 6095ad7ded
2 changed files with 56 additions and 7 deletions

View File

@ -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())
@ -504,6 +524,30 @@ class MainWindowReader(QtGui.QMainWindow, Ui_MainWindowReader):
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):
self.preferences.clearRecentFiles()
self.updateRecentFiles()

View File

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