changing launcher
This commit is contained in:
parent
4ace6e419f
commit
ddaa195cfe
38
yomichan_plugin.py → yomichan.py
Normal file → Executable file
38
yomichan_plugin.py → yomichan.py
Normal file → Executable 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()
|
32
yomichan.pyw
32
yomichan.pyw
@ -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_()
|
|
@ -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)
|
||||||
@ -350,17 +344,18 @@ class MainWindowReader(QtGui.QMainWindow):
|
|||||||
|
|
||||||
self.setStatus(u'Loaded file {0}'.format(filename))
|
self.setStatus(u'Loaded file {0}'.format(filename))
|
||||||
self.setWindowTitle(u'Yomichan - {0} ({1})'.format(os.path.split(filename)[1], encoding))
|
self.setWindowTitle(u'Yomichan - {0} ({1})'.format(os.path.split(filename)[1], encoding))
|
||||||
|
|
||||||
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:
|
||||||
files = [f for f in tp.getnames() if tp.getmember(f).isfile()]
|
files = [f for f in tp.getnames() if tp.getmember(f).isfile()]
|
||||||
names = [f.decode('utf-8') for f in files]
|
names = [f.decode('utf-8') for f in files]
|
||||||
|
|
||||||
self.updateArchiveFiles(filename, names)
|
self.updateArchiveFiles(filename, names)
|
||||||
|
|
||||||
if len(files) == 0:
|
if len(files) == 0:
|
||||||
content = unicode()
|
content = unicode()
|
||||||
elif len(files) == 1:
|
elif len(files) == 1:
|
||||||
@ -381,32 +376,33 @@ 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
|
||||||
|
|
||||||
|
|
||||||
def selectFileName(self, names):
|
def selectFileName(self, names):
|
||||||
if self.state.archiveIndex is not None:
|
if self.state.archiveIndex is not None:
|
||||||
return (self.state.archiveIndex, True)
|
return (self.state.archiveIndex, True)
|
||||||
|
|
||||||
(item, ok) = QtGui.QInputDialog.getItem(
|
(item, ok) = QtGui.QInputDialog.getItem(
|
||||||
self,
|
self,
|
||||||
'Yomichan',
|
'Yomichan',
|
||||||
'Select file to open:',
|
'Select file to open:',
|
||||||
self.formatQStringList(names),
|
self.formatQStringList(names),
|
||||||
current = 0,
|
current = 0,
|
||||||
editable=False)
|
editable=False)
|
||||||
(index, success) = self.getItemIndex(item)
|
(index, success) = self.getItemIndex(item)
|
||||||
return (index - 1, ok and success)
|
return (index - 1, ok and success)
|
||||||
|
|
||||||
|
|
||||||
def getItemIndex(self, item):
|
def getItemIndex(self, item):
|
||||||
return item.split('.').first().toInt()
|
return item.split('.').first().toInt()
|
||||||
|
|
||||||
|
|
||||||
def formatQStringList(self, list):
|
def formatQStringList(self, list):
|
||||||
return [self.formatQString(i, x) for i, x in enumerate(list)]
|
return [self.formatQString(i, x) for i, x in enumerate(list)]
|
||||||
|
|
||||||
|
|
||||||
def formatQString(self, index, item):
|
def formatQString(self, index, item):
|
||||||
return QtCore.QString(str(index + 1) + '. ').append(QtCore.QString(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, QtGui.QTextCursor.MoveAnchor)
|
||||||
cursor.setPosition(samplePosStart + lengthSelect, QtGui.QTextCursor.KeepAnchor)
|
cursor.setPosition(samplePosStart + lengthSelect, QtGui.QTextCursor.KeepAnchor)
|
||||||
self.textContent.setTextCursor(cursor)
|
self.textContent.setTextCursor(cursor)
|
||||||
|
|
||||||
|
|
||||||
def clearArchiveFiles(self):
|
def clearArchiveFiles(self):
|
||||||
self.menuOpenArchive.clear()
|
self.menuOpenArchive.clear()
|
||||||
self.menuOpenArchive.setEnabled(False)
|
self.menuOpenArchive.setEnabled(False)
|
||||||
|
|
||||||
|
|
||||||
def updateArchiveFiles(self, filename, names):
|
def updateArchiveFiles(self, filename, names):
|
||||||
self.menuOpenArchive.setEnabled(True)
|
self.menuOpenArchive.setEnabled(True)
|
||||||
for name in self.formatQStringList(names):
|
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)))
|
self.menuOpenArchive.addAction(name, (lambda fn=filename, idx=index: self.openFileInArchive(fn, idx)))
|
||||||
else:
|
else:
|
||||||
self.menuOpenArchive.addAction(name, (lambda fn=filename: self.openFile(fn)))
|
self.menuOpenArchive.addAction(name, (lambda fn=filename: self.openFile(fn)))
|
||||||
|
|
||||||
|
|
||||||
def openFileInArchive(self, filename, index):
|
def openFileInArchive(self, filename, index):
|
||||||
self.state.scanPosition = 0
|
self.state.scanPosition = 0
|
||||||
self.state.archiveIndex = index
|
self.state.archiveIndex = index
|
||||||
@ -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)
|
||||||
|
Loading…
Reference in New Issue
Block a user