forked from dickloraine/EmbedComicMetadata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.py
125 lines (101 loc) · 4.69 KB
/
ui.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
#!/usr/bin/env python2
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
from __future__ import (unicode_literals, division, absolute_import,
print_function)
__license__ = 'GPL v3'
__copyright__ = '2015, dloraine'
__docformat__ = 'restructuredtext en'
try:
from PyQt5.Qt import QMenu, QIcon, QPixmap
except ImportError:
from PyQt4.Qt import QMenu, QIcon, QPixmap
from functools import partial
from calibre.gui2.actions import InterfaceAction
from calibre.gui2 import error_dialog
from calibre_plugins.EmbedComicMetadata.config import prefs
from calibre_plugins.EmbedComicMetadata.languages.lang import _L
from calibre_plugins.EmbedComicMetadata.ini import (
get_configuration, CONFIG_NAME, CONFIG_DESCRIPTION, CONFIG_TRIGGER_FUNC,
CONFIG_TRIGGER_ARG, CONFIG_MENU)
config = get_configuration()
class EmbedComicMetadata(InterfaceAction):
name = 'Embed Comic Metadata'
# Declare the main action associated with this plugin
if prefs["main_import"]:
action_spec = (_L['Import Comic Metadata'], None,
_L['Imports the metadata from the comic to calibre'], None)
else:
action_spec = (_L['Embed Comic Metadata'], None,
_L['Embeds calibres metadata into the comic'], None)
def genesis(self):
# menu
self.menu = QMenu(self.gui)
# Get the icon for this interface action
icon = self.get_icon('images/embed_comic_metadata.png')
# The qaction is automatically created from the action_spec defined
# above
self.qaction.setMenu(self.menu)
self.qaction.setIcon(icon)
self.qaction.triggered.connect(self.main_menu_triggered)
# build menu
self.menu.clear()
self.build_menu()
self.toggle_menu_items()
def build_menu(self):
for item in config[CONFIG_MENU]["UI_Action_Items"]:
if item[CONFIG_NAME] == "seperator":
self.menu.addSeparator()
continue
elif item[CONFIG_TRIGGER_ARG]:
triggerfunc = partial(item[CONFIG_TRIGGER_FUNC], self, item[CONFIG_TRIGGER_ARG])
else:
triggerfunc = partial(item[CONFIG_TRIGGER_FUNC], self)
self.menu_action(item[CONFIG_NAME], item[CONFIG_DESCRIPTION], triggerfunc)
# add configuration entry
self.menu_action("configure", _L["Configure"],
partial(self.interface_action_base_plugin.do_user_config, (self.gui)))
def toggle_menu_items(self):
for item in config[CONFIG_MENU]["Items"]:
action = getattr(self, item[CONFIG_NAME])
action.setVisible(prefs[item[CONFIG_NAME]])
def main_menu_triggered(self):
from calibre_plugins.EmbedComicMetadata.main import embed_into_comic, import_to_calibre
i = prefs["main_import"]
# Check the preferences for what should be done
if (i and prefs['read_cbi'] and prefs['read_cix']) or (
not i and prefs['cbi_embed'] and prefs['cix_embed']):
action = "both"
elif (i and prefs['read_cbi']) or (not i and prefs['cbi_embed']):
action = "cbi"
elif (i and prefs['read_cix']) or (not i and prefs['cix_embed']):
action = "cix"
else:
return error_dialog(self.gui, _L['Cannot update metadata'],
_L['No embed format selected'], show=True)
if i:
import_to_calibre(self, action)
else:
embed_into_comic(self, action)
def apply_settings(self):
# In an actual non trivial plugin, you would probably need to
# do something based on the settings in prefs
prefs
def menu_action(self, name, title, triggerfunc):
action = self.create_menu_action(self.menu, name, title, icon=None,
shortcut=None, description=None,
triggered=triggerfunc, shortcut_name=None)
setattr(self, name, action)
def get_icon(self, icon_name):
import os
from calibre.utils.config import config_dir
# Check to see whether the icon exists as a Calibre resource
# This will enable skinning if the user stores icons within a folder like:
# ...\AppData\Roaming\calibre\resources\images\Plugin Name\
icon_path = os.path.join(config_dir, 'resources', 'images', self.name,
icon_name.replace('images/', ''))
if os.path.exists(icon_path):
pixmap = QPixmap()
pixmap.load(icon_path)
return QIcon(pixmap)
# As we did not find an icon elsewhere, look within our zip resources
return get_icons(icon_name)