-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
sort.py
86 lines (70 loc) · 2.8 KB
/
sort.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
#!/usr/bin/env python
# vim:fileencoding=utf-8
from __future__ import (unicode_literals, division, absolute_import,
print_function)
__license__ = 'GPL v3'
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
from PyQt4.Qt import QToolButton, QAction, pyqtSignal, QIcon
from calibre.gui2.actions import InterfaceAction
from calibre.utils.icu import sort_key
class SortAction(QAction):
sort_requested = pyqtSignal(object, object)
def __init__(self, text, key, ascending, parent):
QAction.__init__(self, text, parent)
self.key, self.ascending = key, ascending
self.triggered.connect(self)
def __call__(self):
self.sort_requested.emit(self.key, self.ascending)
class SortByAction(InterfaceAction):
name = 'Sort By'
action_spec = (_('Sort By'), 'arrow-up.png', _('Sort the list of books'), None)
action_type = 'current'
popup_type = QToolButton.InstantPopup
action_add_menu = True
dont_add_to = frozenset([
'toolbar', 'toolbar-device', 'context-menu-device', 'toolbar-child',
'menubar', 'menubar-device', 'context-menu-cover-browser'])
def genesis(self):
self.sorted_icon = QIcon(I('ok.png'))
def location_selected(self, loc):
self.qaction.setEnabled(loc == 'library')
def update_menu(self):
menu = self.qaction.menu()
for action in menu.actions():
action.sort_requested.disconnect()
menu.clear()
lv = self.gui.library_view
m = lv.model()
db = m.db
try:
sort_col, order = m.sorted_on
except TypeError:
sort_col, order = 'date', True
fm = db.field_metadata
def get_name(k):
ans = fm[k]['name']
if k == 'cover':
ans = _('Has cover')
return ans
name_map = {get_name(k):k for k in fm.sortable_field_keys() if fm[k]['name']}
self._sactions = []
for name in sorted(name_map, key=sort_key):
key = name_map[name]
if key in {'sort', 'series_sort', 'formats', 'path'}:
continue
if key == 'ondevice' and self.gui.device_connected is None:
continue
ascending = None
if key == sort_col:
name = _('%s [reverse current sort]') % name
ascending = not order
sac = SortAction(name, key, ascending, menu)
if key == sort_col:
sac.setIcon(self.sorted_icon)
sac.sort_requested.connect(self.sort_requested)
menu.addAction(sac)
def sort_requested(self, key, ascending):
if ascending is None:
self.gui.library_view.intelligent_sort(key, True)
else:
self.gui.library_view.sort_by_named_field(key, ascending)