-
Notifications
You must be signed in to change notification settings - Fork 19
/
coverart_toolbar.py
253 lines (201 loc) · 8.84 KB
/
coverart_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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# -*- Mode: python; coding: utf-8; tab-width: 4; indent-tabs-mode: nil; -*-
#
# Copyright (C) 2012 - fossfreedom
# Copyright (C) 2012 - Agustin Carrasco
#
# 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 2, 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
from gi.repository import GObject
from gi.repository import Gtk
from gi.repository import RB
from gi.repository import Gio
from coverart_browser_prefs import GSetting
from coverart_browser_prefs import CoverLocale
from coverart_utils import Theme
from coverart_controllers import PlaylistPopupController
from coverart_controllers import GenrePopupController
from coverart_controllers import SortPopupController
from coverart_controllers import ArtistSortPopupController
from coverart_controllers import PropertiesMenuController
from coverart_controllers import DecadePopupController
from coverart_controllers import SortOrderToggleController
from coverart_controllers import ArtistSortOrderToggleController
from coverart_controllers import AlbumSearchEntryController
from coverart_widgets import SearchEntry
from coverart_browser_prefs import webkit_support
import rb
class Toolbar(GObject.Object):
def __init__(self, plugin, mainbox, controllers):
super(Toolbar, self).__init__()
self.plugin = plugin
self.mainbox = mainbox
cl = CoverLocale()
ui_file = rb.find_plugin_file(plugin, self.ui)
# create the toolbar
builder = Gtk.Builder()
builder.set_translation_domain(cl.Locale.LOCALE_DOMAIN)
print (ui_file)
builder.add_from_file(ui_file)
# assign the controllers to the buttons
for button, controller in controllers.items():
if button != 'search':
builder.get_object(button).controller = controller
if not webkit_support():
# button = builder.get_object('flowview_button')
#button.set_visible(False)
separator = builder.get_object('properties_separator')
if separator:
separator.set_visible(False)
# workaround to translate the search entry tooltips
cl.switch_locale(cl.Locale.RB)
search_entry = SearchEntry(has_popup=True)
search_entry.show_all()
cl.switch_locale(cl.Locale.LOCALE_DOMAIN)
# add it to the ui
align = builder.get_object('entry_search_alignment')
align.add(search_entry)
# assign the controller
search_entry.controller = controllers['search']
Theme(self.plugin).connect('theme_changed', self._theme_changed,
controllers)
self.builder = builder.get_object('toolbar')
# now theme the toolbar including child objects such as the button popups
style_context = self.builder.get_style_context()
style_context.add_class(Gtk.STYLE_CLASS_TOOLBAR)
def _theme_changed(self, toolbar, controllers):
for controller in list(controllers.values()):
controller.update_images(True)
class TopToolbar(Toolbar):
ui = 'ui/coverart_topbar.ui'
name = 'main'
def hide(self):
if self.builder.get_visible():
self.builder.hide()
def show(self):
self.mainbox.pack_start(self.builder, False, True, 0)
self.mainbox.reorder_child(self.builder, 0)
self.builder.show()
class LeftToolbar(Toolbar):
ui = 'ui/coverart_leftsidebar.ui'
name = 'left'
def hide(self):
if self.builder.get_visible():
self.builder.hide()
self.plugin.shell.remove_widget(self.builder,
RB.ShellUILocation.SIDEBAR)
def show(self):
self.plugin.shell.add_widget(self.builder,
RB.ShellUILocation.SIDEBAR, expand=False, fill=False)
self.builder.show()
class RightToolbar(Toolbar):
ui = 'ui/coverart_rightsidebar.ui'
name = 'right'
def hide(self):
if self.builder.get_visible():
self.builder.hide()
self.plugin.shell.remove_widget(self.builder,
RB.ShellUILocation.RIGHT_SIDEBAR)
def show(self):
self.plugin.shell.add_widget(self.builder,
RB.ShellUILocation.RIGHT_SIDEBAR, expand=False, fill=False)
self.builder.show()
class ToolbarObject(object):
# properties
PROPERTIES = 'properties_button'
SORT_BY = 'sort_by'
SORT_ORDER = 'sort_order'
SORT_BY_ARTIST = 'sort_by_artist'
SORT_ORDER_ARTIST = 'sort_order_artist'
GENRE = 'genre_button'
PLAYLIST = 'playlist_button'
DECADE = 'decade_button'
SEARCH = 'search'
VIEW = 'view_button'
class ToolbarManager(GObject.Object):
# properties
toolbar_pos = GObject.property(type=str, default=TopToolbar.name)
def __init__(self, plugin, main_box, viewmgr):
super(ToolbarManager, self).__init__()
self.plugin = plugin
# create the buttons controllers
controllers = self._create_controllers(plugin, viewmgr)
# initialize toolbars
self._bars = {}
self._bars[TopToolbar.name] = TopToolbar(plugin, main_box,
controllers)
self._bars[LeftToolbar.name] = LeftToolbar(plugin, main_box,
controllers)
self._bars[RightToolbar.name] = RightToolbar(plugin, main_box,
controllers)
self.last_toolbar_pos = None
# connect signal and properties
self._connect_signals()
self._connect_properties()
self._controllers = controllers
# if the alternative-toolbar is loaded then lets connect to the toolbar-visibility signal
# to control our sources toolbar visibility
if hasattr(self.plugin.shell, 'alternative_toolbar'):
self.plugin.shell.alternative_toolbar.connect('toolbar-visibility', self._visibility)
def _visibility(self, altplugin, value):
if value:
self._bars[self.toolbar_pos].show()
else:
self._bars[self.toolbar_pos].hide()
def set_enabled(self, enabled, toolbar_object=None):
'''
enable or disable the toolbar object.
:param enabled: `bool` value.
:param toolbar_object: `ToolbarObject`
None if enabled is to apply to all objects in the toolbar
'''
if toolbar_object:
self._controllers[toolbar_object].enabled = enabled
else:
for controller in self._controllers:
self._controllers[controller].enabled = enabled
def _connect_signals(self):
self.connect('notify::toolbar-pos', self._on_notify_toolbar_pos)
def _connect_properties(self):
gs = GSetting()
setting = gs.get_setting(gs.Path.PLUGIN)
setting.bind(gs.PluginKey.TOOLBAR_POS, self, 'toolbar_pos',
Gio.SettingsBindFlags.GET)
def _create_controllers(self, plugin, viewmgr):
controllers = {}
album_model = viewmgr.source.album_manager.model
controllers[ToolbarObject.PROPERTIES] = \
PropertiesMenuController(plugin, viewmgr.source)
controllers[ToolbarObject.SORT_BY] = \
SortPopupController(plugin, viewmgr)
controllers[ToolbarObject.SORT_ORDER] = \
SortOrderToggleController(plugin, viewmgr)
controllers[ToolbarObject.SORT_BY_ARTIST] = \
ArtistSortPopupController(plugin, viewmgr)
controllers[ToolbarObject.SORT_ORDER_ARTIST] = \
ArtistSortOrderToggleController(plugin, viewmgr)
controllers[ToolbarObject.GENRE] = \
GenrePopupController(plugin, album_model)
controllers[ToolbarObject.PLAYLIST] = \
PlaylistPopupController(plugin, album_model)
controllers[ToolbarObject.DECADE] = \
DecadePopupController(plugin, album_model)
controllers[ToolbarObject.SEARCH] = \
AlbumSearchEntryController(album_model)
controllers[ToolbarObject.VIEW] = viewmgr.controller
return controllers
def _on_notify_toolbar_pos(self, *args):
if self.last_toolbar_pos:
self._bars[self.last_toolbar_pos].hide()
self._bars[self.toolbar_pos].show()
self.last_toolbar_pos = self.toolbar_pos