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 -*- # -*- coding: utf-8 -*-
# Copyright (C) 2011 Alex Yatskov # Copyright (C) 2011 Alex Yatskov
@ -16,26 +17,32 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
from PyQt4 import QtGui, QtCore from PyQt4 import QtGui, QtCore
from yomichan import anki_host
from yomichan.lang import japanese from yomichan.lang import japanese
from yomichan.util import buildResPath
from yomichan.preference_data import Preferences from yomichan.preference_data import Preferences
from yomichan.reader import MainWindowReader from yomichan.reader import MainWindowReader
class YomichanPlugin: class Yomichan:
def __init__(self): def __init__(self):
self.languages = {'Japanese': japanese.initLanguage()} self.languages = {'Japanese': japanese.initLanguage()}
self.preferences = Preferences() self.preferences = Preferences()
self.preferences.load() self.preferences.load()
class YomichanPlugin(Yomichan):
def __init__(self):
Yomichan.__init__(self)
self.toolIconVisible = False self.toolIconVisible = False
self.window = None self.window = None
self.anki = anki_host.Anki() self.anki = anki_host.Anki()
self.parent = self.anki.window() self.parent = self.anki.window()
self.separator = QtGui.QAction(self.parent) self.separator = QtGui.QAction(self.parent)
self.separator.setSeparator(True) 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.action.setIconVisibleInMenu(True)
self.parent.connect(self.action, QtCore.SIGNAL('triggered()'), self.onShowRequest) self.parent.connect(self.action, QtCore.SIGNAL('triggered()'), self.onShowRequest)
@ -50,9 +57,9 @@ class YomichanPlugin:
else: else:
self.window = MainWindowReader( self.window = MainWindowReader(
self.parent, self.parent,
self.preferences,
self.languages, self.languages,
None, None,
self.preferences,
self.anki, self.anki,
self.onWindowClose, self.onWindowClose,
self.onWindowUpdate self.onWindowUpdate
@ -102,4 +109,23 @@ class YomichanPlugin:
self.toolIconVisible = True 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 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) QtGui.QMainWindow.__init__(self, parent)
uic.loadUi(buildResPath('ui/reader.ui'), self) uic.loadUi(buildResPath('ui/reader.ui'), self)
@ -47,12 +47,7 @@ class MainWindowReader(QtGui.QMainWindow):
self.textContent.mousePressEvent = self.onContentMousePress self.textContent.mousePressEvent = self.onContentMousePress
self.dockAnki.setEnabled(bool(anki)) self.dockAnki.setEnabled(bool(anki))
if preferences: self.preferences = preferences
self.preferences = preferences
else:
self.preferences = Preferences()
self.preferences.load()
self.updateFinder = UpdateFinder() self.updateFinder = UpdateFinder()
self.state = self.State() self.state = self.State()
self.languages = languages self.languages = languages
@ -162,8 +157,7 @@ class MainWindowReader(QtGui.QMainWindow):
filename = QtGui.QFileDialog.getOpenFileName( filename = QtGui.QFileDialog.getOpenFileName(
parent=self, parent=self,
caption='Select a file to open', caption='Select a file to open',
filter='Archive files (*.bz2 *.gz *.tar *.tgz);;Text files (*.txt);;All files (*.*)', filter='Text files (*.txt);;Archive files (*.bz2 *.gz *.tar *.tgz);;All files (*.*)'
selectedFilter='Text files (*.txt)'
) )
if not filename.isNull(): if not filename.isNull():
self.openFile(filename) self.openFile(filename)
@ -353,6 +347,7 @@ class MainWindowReader(QtGui.QMainWindow):
def openFileByExtension(self, filename): def openFileByExtension(self, filename):
self.clearArchiveFiles() self.clearArchiveFiles()
if tarfile.is_tarfile(filename): if tarfile.is_tarfile(filename):
# opening an empty tar file raises ReadError # opening an empty tar file raises ReadError
with tarfile.open(filename, 'r:*') as tp: with tarfile.open(filename, 'r:*') as tp:
@ -381,6 +376,7 @@ class MainWindowReader(QtGui.QMainWindow):
self.state.archiveIndex = None self.state.archiveIndex = None
with open(filename, 'rb') as fp: with open(filename, 'rb') as fp:
content = fp.read() content = fp.read()
return content return content
@ -560,7 +556,7 @@ class MainWindowReader(QtGui.QMainWindow):
return return
for filename in filenames: 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.addSeparator()
self.menuOpenRecent.addAction('Clear file history', self.clearRecentFiles) self.menuOpenRecent.addAction('Clear file history', self.clearRecentFiles)