1

changing launcher

This commit is contained in:
Alex Yatskov 2011-11-19 09:19:17 -08:00
parent 4ace6e419f
commit ddaa195cfe
3 changed files with 57 additions and 67 deletions

38
yomichan_plugin.py → yomichan.py Normal file → Executable file
View File

@ -1,3 +1,4 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# Copyright (C) 2011 Alex Yatskov
@ -16,26 +17,32 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
from PyQt4 import QtGui, QtCore
from yomichan import anki_host
from yomichan.lang import japanese
from yomichan.util import buildResPath
from yomichan.preference_data import Preferences
from yomichan.reader import MainWindowReader
class YomichanPlugin:
class Yomichan:
def __init__(self):
self.languages = {'Japanese': japanese.initLanguage()}
self.preferences = Preferences()
self.preferences.load()
class YomichanPlugin(Yomichan):
def __init__(self):
Yomichan.__init__(self)
self.toolIconVisible = False
self.window = None
self.anki = anki_host.Anki()
self.parent = self.anki.window()
self.separator = QtGui.QAction(self.parent)
self.separator.setSeparator(True)
self.action = QtGui.QAction(QtGui.QIcon(':/logos/logos/logo32x32.png'), '&Yomichan...', self.parent)
self.action = QtGui.QAction(QtGui.QIcon(buildResPath('img/logo32x32.png')), '&Yomichan...', self.parent)
self.action.setIconVisibleInMenu(True)
self.parent.connect(self.action, QtCore.SIGNAL('triggered()'), self.onShowRequest)
@ -50,9 +57,9 @@ class YomichanPlugin:
else:
self.window = MainWindowReader(
self.parent,
self.preferences,
self.languages,
None,
self.preferences,
self.anki,
self.onWindowClose,
self.onWindowUpdate
@ -102,4 +109,23 @@ class YomichanPlugin:
self.toolIconVisible = True
plugin = YomichanPlugin()
class YomichanStandalone(Yomichan):
def __init__(self):
Yomichan.__init__(self)
self.application = QtGui.QApplication(sys.argv)
self.window = MainWindowReader(
None,
self.preferences,
self.languages,
filename=sys.argv[1] if len(sys.argv) >= 2 else None
)
self.window.show()
self.application.exec_()
if __name__ == '__main__':
instance = YomichanStandalone()
else:
from yomichan import anki_host
instance = YomichanPlugin()

View File

@ -1,32 +0,0 @@
#!/usr/bin/env python2
# Copyright (C) 2011 Alex Yatskov
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
from PyQt4 import QtGui
from yomichan.lang import japanese
from yomichan.reader import MainWindowReader
filename = sys.argv[1] if len(sys.argv) >= 2 else None
languages = {'Japanese': japanese.initLanguage()}
application = QtGui.QApplication(sys.argv)
window = MainWindowReader(languages=languages, filename=filename)
window.show()
application.exec_()

View File

@ -39,7 +39,7 @@ class MainWindowReader(QtGui.QMainWindow):
self.archiveIndex = None
def __init__(self, parent=None, languages=None, filename=None, preferences=None, anki=None, closed=None, updated=None):
def __init__(self, parent, preferences, languages, filename=None, anki=None, closed=None, updated=None):
QtGui.QMainWindow.__init__(self, parent)
uic.loadUi(buildResPath('ui/reader.ui'), self)
@ -47,12 +47,7 @@ class MainWindowReader(QtGui.QMainWindow):
self.textContent.mousePressEvent = self.onContentMousePress
self.dockAnki.setEnabled(bool(anki))
if preferences:
self.preferences = preferences
else:
self.preferences = Preferences()
self.preferences.load()
self.preferences = preferences
self.updateFinder = UpdateFinder()
self.state = self.State()
self.languages = languages
@ -162,8 +157,7 @@ class MainWindowReader(QtGui.QMainWindow):
filename = QtGui.QFileDialog.getOpenFileName(
parent=self,
caption='Select a file to open',
filter='Archive files (*.bz2 *.gz *.tar *.tgz);;Text files (*.txt);;All files (*.*)',
selectedFilter='Text files (*.txt)'
filter='Text files (*.txt);;Archive files (*.bz2 *.gz *.tar *.tgz);;All files (*.*)'
)
if not filename.isNull():
self.openFile(filename)
@ -350,17 +344,18 @@ class MainWindowReader(QtGui.QMainWindow):
self.setStatus(u'Loaded file {0}'.format(filename))
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:
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:
@ -381,32 +376,33 @@ class MainWindowReader(QtGui.QMainWindow):
self.state.archiveIndex = None
with open(filename, 'rb') as fp:
content = fp.read()
return content
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:',
self,
'Yomichan',
'Select file to open:',
self.formatQStringList(names),
current = 0,
editable=False)
(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))
@ -523,13 +519,13 @@ class MainWindowReader(QtGui.QMainWindow):
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.menuOpenArchive.setEnabled(True)
for name in self.formatQStringList(names):
@ -539,8 +535,8 @@ class MainWindowReader(QtGui.QMainWindow):
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
@ -560,7 +556,7 @@ class MainWindowReader(QtGui.QMainWindow):
return
for filename in filenames:
self.menuOpenRecent.addAction(filename, (lambda fn=filename: self.openFile(fn)))
self.menuOpenRecent.addAction(filename, lambda fn=filename: self.openFile(fn))
self.menuOpenRecent.addSeparator()
self.menuOpenRecent.addAction('Clear file history', self.clearRecentFiles)