forked from ankitects/anki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoolbar.py
170 lines (140 loc) · 4.36 KB
/
toolbar.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# Copyright: Damien Elmes <[email protected]>
# -*- coding: utf-8 -*-
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
from aqt.qt import *
class Toolbar(object):
def __init__(self, mw, web):
self.mw = mw
self.web = web
self.link_handlers = {
"decks": self._deckLinkHandler,
"study": self._studyLinkHandler,
"add": self._addLinkHandler,
"browse": self._browseLinkHandler,
"stats": self._statsLinkHandler,
"sync": self._syncLinkHandler,
}
def onLoaded(self):
self.web.evalWithCallback("$(document.body).height()", self.onHeight)
def onHeight(self, qvar):
height = int(qvar*self.web.zoomFactor())
self.web.setFixedHeight(height)
def draw(self):
self.web.onBridgeCmd = self._linkHandler
self.web.onLoadFinished = self.onLoaded
self.web.stdHtml(self._body % (
# may want a context menu here in the future
' '*20,
self._centerLinks(),
self._rightIcons()),
self._css)
# Available links
######################################################################
def _rightIconsList(self):
return [
["stats", "qrc:/icons/view-statistics.png",
_("Show statistics. Shortcut key: %s") % "Shift+S"],
["sync", "qrc:/icons/view-refresh.png",
_("Synchronize with AnkiWeb. Shortcut key: %s") % "Y"],
]
def _centerLinks(self):
links = [
["decks", _("Decks"), _("Shortcut key: %s") % "D"],
["add", _("Add"), _("Shortcut key: %s") % "A"],
["browse", _("Browse"), _("Shortcut key: %s") % "B"],
]
return self._linkHTML(links)
def _linkHTML(self, links):
buf = ""
for ln, name, title in links:
buf += '''
<a class=hitem title="%s" href=# onclick="pycmd('%s')">%s</a>''' % (
title, ln, name)
buf += " "*3
return buf
def _rightIcons(self):
buf = ""
for ln, icon, title in self._rightIconsList():
buf += '''
<a class=hitem title="%s" href=# onclick='pycmd("%s")'><img width="16px" height="16px" src="%s"></a>''' % (
title, ln, icon)
return buf
# Link handling
######################################################################
def _linkHandler(self, link):
if link in self.link_handlers:
self.link_handlers[link]()
return False
def _deckLinkHandler(self):
self.mw.moveToState("deckBrowser")
def _studyLinkHandler(self):
# if overview already shown, switch to review
if self.mw.state == "overview":
self.mw.col.startTimebox()
self.mw.moveToState("review")
else:
self.mw.onOverview()
def _addLinkHandler(self):
self.mw.onAddCard()
def _browseLinkHandler(self):
self.mw.onBrowse()
def _statsLinkHandler(self):
self.mw.onStats()
def _syncLinkHandler(self):
self.mw.onSync()
# HTML & CSS
######################################################################
_body = """
<center id=outer>
<table id=header width=100%%>
<tr>
<td width=16%% align=left>%s</td>
<td align=center>%s</td>
<td width=15%% valign=middle align=right>%s</td>
</tr></table>
</center>
"""
_css = """
#header {
padding:3px;
font-weight: bold;
border-bottom: 1px solid #aaa;
background: -webkit-gradient(linear, left top, left bottom,
from(#ddd), to(#fff));
}
body {
margin:0; padding:0;
-webkit-user-select: none;
overflow: hidden;
}
* { -webkit-user-drag: none; }
.hitem {
padding-right: 6px;
text-decoration: none;
color: #000;
}
.hitem:hover {
text-decoration: underline;
}
"""
class BottomBar(Toolbar):
_css = Toolbar._css + """
#header {
background: -webkit-gradient(linear, left top, left bottom,
from(#fff), to(#ddd));
border-bottom: 0;
border-top: 1px solid #aaa;
margin-bottom: 6px;
margin-top: 0;
}
"""
_centerBody = """
<center id=outer><table width=100%% id=header><tr><td align=center>
%s</td></tr></table></center>
"""
def draw(self, buf):
self.web.onBridgeCmd = self._linkHandler
self.web.onLoadFinished = self.onLoaded
self.web.stdHtml(
self._centerBody % buf,
self._css)