-
Notifications
You must be signed in to change notification settings - Fork 0
/
Menu_bar_system_tray_icon_and_menu.py
80 lines (67 loc) · 2.89 KB
/
Menu_bar_system_tray_icon_and_menu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# -*- coding: utf-8 -*-
# Author: Nicolò Valigi
# License: GNU GPL, version 3 or later; http://www.gnu.org/copyleft/gpl.html
#
# systray plugin
#
# Adds a simple tray icon menu to create a new card, raise anki, or quit.
# "Add new card" by default opens a new window. If an "add card" window is already
# active (receiving user input) it doesn't do anything. If an "add card"
# windows is already open with data, it raises it. If an empty "add card"
# windows is already open, it closes it and reopens a new one. This works
# around OSX's behaviour of switching Spaces when the same window is opened somewhere
# else.
from aqt.qt import *
from PyQt4.QtGui import *
from PyQt4 import QtCore
import aqt
import os, time
from anki.hooks import addHook
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
def showCardAdder():
(creator, instance) = aqt.dialogs._dialogs["AddCards"]
if instance:
if instance.isActiveWindow():
return
if instance.editor.fieldsAreBlank():
#closing an existing empty editor
instance.close() #it's safe, I've already checked the fields
aqt.dialogs.close("AddCards")
#The second "open" I run raises the add card window even if the main one isn't
aqt.mw.onAddCard()
aqt.dialogs.open("AddCards", aqt.mw)
def click_trap(value):
if value == QSystemTrayIcon.Trigger: #left click!
aqt.mw.app.emit(QtCore.SIGNAL("appMsg"), "raise")
aqt.mw.activateWindow()
aqt.mw.setWindowState( aqt.mw.windowState() & ~Qt.WindowMinimized | Qt.WindowActive)
def createSysTray():
self = aqt.mw
trayIcon = QSystemTrayIcon(self)
ankiLogo = QIcon()
ankiLogo.addPixmap(QPixmap(_fromUtf8(":/icons/anki.png")), QIcon.Normal, QIcon.Off)
trayIcon.setIcon(ankiLogo)
trayMenu = QMenu(self)
trayIcon.setContextMenu(trayMenu)
trayIcon.activated.connect(click_trap)
addNewAction = QAction("Create new card", self)
addNewAction.triggered.connect(showCardAdder)
trayMenu.addAction(addNewAction)
raiseWindowAction = QAction("Raise Anki Window", self)
raiseWindowAction.triggered.connect(lambda: aqt.mw.app.emit(QtCore.SIGNAL("appMsg"), "raise"))
trayMenu.addAction(raiseWindowAction)
trayMenu.addSeparator()
quitAction = QAction("Quit Anki", self)
self.connect(quitAction, QtCore.SIGNAL("triggered()"), self, QtCore.SLOT("close()"))
trayMenu.addAction(quitAction)
trayIcon.setVisible(True)
trayIcon.show()
createSysTray()
#addHook("profileLoaded", createSysTray)
#aqt.editor.Editor.setupFields = wrap (aqt.editor.Editor.setupButtons, addXournalButton, "after")
#def raiseWindow(self):
# aqt.mw.app.emit(QtCore.SIGNAL("appMsg"), "raise")
#addNewAction.triggered.connect(lambda: aqt.dialogs.open("AddCards", self))