1
yomichan-anki/yomichan.py

132 lines
3.7 KiB
Python
Raw Normal View History

2011-11-19 17:19:17 +00:00
#!/usr/bin/env python2
2011-08-28 18:01:32 +00:00
# -*- coding: utf-8 -*-
2011-10-27 15:22:26 +00:00
2011-08-28 18:01:32 +00:00
# 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/>.
2011-11-19 17:19:17 +00:00
import sys
2011-08-28 18:01:32 +00:00
from PyQt4 import QtGui, QtCore
2011-11-05 16:52:44 +00:00
from yomichan.lang import japanese
2011-11-19 17:19:17 +00:00
from yomichan.util import buildResPath
from yomichan.preference_data import Preferences
from yomichan.reader import MainWindowReader
2011-08-28 18:01:32 +00:00
2011-11-19 17:19:17 +00:00
class Yomichan:
2011-08-28 18:01:32 +00:00
def __init__(self):
self.languages = {'Japanese': japanese.initLanguage()}
self.preferences = Preferences()
self.preferences.load()
2011-11-19 17:19:17 +00:00
class YomichanPlugin(Yomichan):
def __init__(self):
Yomichan.__init__(self)
2011-08-28 18:01:32 +00:00
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)
2011-11-19 17:19:17 +00:00
self.action = QtGui.QAction(QtGui.QIcon(buildResPath('img/logo32x32.png')), '&Yomichan...', self.parent)
2011-08-28 18:01:32 +00:00
self.action.setIconVisibleInMenu(True)
2011-10-08 16:32:51 +00:00
self.parent.connect(self.action, QtCore.SIGNAL('triggered()'), self.onShowRequest)
2011-08-28 18:01:32 +00:00
2011-10-08 16:32:51 +00:00
self.anki.addHook('loadDeck', self.onDeckLoad)
self.anki.addHook('deckClosed', self.onDeckClose)
2011-08-28 18:01:32 +00:00
2011-10-08 16:32:51 +00:00
def onShowRequest(self):
2011-08-28 18:01:32 +00:00
if self.window:
self.window.setVisible(True)
self.window.activateWindow()
else:
self.window = MainWindowReader(
self.parent,
2011-11-19 17:19:17 +00:00
self.preferences,
2011-08-28 18:01:32 +00:00
self.languages,
None,
self.anki,
2011-10-08 16:32:51 +00:00
self.onWindowClose,
self.onWindowUpdate
2011-08-28 18:01:32 +00:00
)
self.window.show()
2011-10-08 16:32:51 +00:00
def onWindowClose(self):
2011-08-28 18:01:32 +00:00
self.window = None
2011-10-08 16:32:51 +00:00
def onWindowUpdate(self):
2011-08-28 18:01:32 +00:00
if self.preferences.ankiShowIcon:
2011-10-08 16:32:51 +00:00
self.showToolIcon()
2011-08-28 18:01:32 +00:00
else:
2011-10-08 16:32:51 +00:00
self.hideToolIcon()
2011-08-28 18:01:32 +00:00
2011-10-08 16:32:51 +00:00
def onDeckLoad(self):
2011-08-28 18:01:32 +00:00
self.anki.toolsMenu().addAction(self.separator)
self.anki.toolsMenu().addAction(self.action)
if self.preferences.ankiShowIcon:
2011-10-08 16:32:51 +00:00
self.showToolIcon()
2011-08-28 18:01:32 +00:00
2011-10-08 16:32:51 +00:00
def onDeckClose(self):
2011-08-28 18:01:32 +00:00
self.anki.toolsMenu().removeAction(self.action)
self.anki.toolsMenu().removeAction(self.separator)
2011-10-08 16:32:51 +00:00
self.hideToolIcon()
2011-08-28 18:01:32 +00:00
if self.window:
self.window.close()
self.window = None
2011-10-08 16:32:51 +00:00
def hideToolIcon(self):
2011-08-28 18:01:32 +00:00
if self.toolIconVisible:
self.anki.toolBar().removeAction(self.action)
self.toolIconVisible = False
2011-10-08 16:32:51 +00:00
def showToolIcon(self):
2011-08-28 18:01:32 +00:00
if not self.toolIconVisible:
self.anki.toolBar().addAction(self.action)
self.toolIconVisible = True
2011-11-19 17:19:17 +00:00
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()