container_views.py 14 KB
Newer Older
1
# Copyright (c) 2014-2020 Cedric Bellegarde <cedric.bellegarde@adishatz.org>
2 3 4 5 6 7 8 9 10 11 12
# 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 3 of the License, 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, see <http://www.gnu.org/licenses/>.

Cédric Bellegarde's avatar
Cédric Bellegarde committed
13 14
from gi.repository import Gtk

Cédric Bellegarde's avatar
Cédric Bellegarde committed
15
from lollypop.define import App, Type, ViewType, MARGIN_SMALL, StorageType
16
from lollypop.utils import emit_signal, get_default_storage_type
17 18 19 20 21 22 23 24 25 26 27 28 29


class ViewsContainer:
    """
        Views management for main view
    """

    def __init__(self):
        """
            Init container
        """
        pass

30 31 32 33 34 35
    def show_widget(self, widget):
        """
            Show widget
            @param widget as Gtk.Widget
        """
        from lollypop.view import View
Cédric Bellegarde's avatar
Cédric Bellegarde committed
36
        view = View(StorageType.ALL, ViewType.DEFAULT)
37 38 39 40 41 42
        view.show()
        view.add(widget)
        widget.set_vexpand(True)
        self._stack.add(view)
        self._stack.set_visible_child(view)

43
    def show_menu(self, widget):
Cédric Bellegarde's avatar
Cédric Bellegarde committed
44 45 46 47
        """
            Show menu widget
            @param widget as Gtk.Widget
        """
48
        def on_hidden(widget, hide, view):
49 50 51 52 53 54 55
            if hide:
                self._stack.set_transition_type(
                    Gtk.StackTransitionType.SLIDE_UP)
                self.go_back()
                self._stack.set_transition_type(
                    Gtk.StackTransitionType.CROSSFADE)
                App().enable_special_shortcuts(True)
56 57
                if App().lookup_action("reload").get_state():
                    self.reload_view()
Cédric Bellegarde's avatar
Cédric Bellegarde committed
58
            if self.can_go_back:
Cédric Bellegarde's avatar
Cédric Bellegarde committed
59
                emit_signal(self, "can-go-back-changed", True)
Cédric Bellegarde's avatar
Cédric Bellegarde committed
60

Cédric Bellegarde's avatar
Cédric Bellegarde committed
61 62
        from lollypop.view_menu import MenuView
        view = MenuView(widget)
Cédric Bellegarde's avatar
Cédric Bellegarde committed
63
        view.show()
64
        widget.connect("hidden", on_hidden, view)
Cédric Bellegarde's avatar
Cédric Bellegarde committed
65 66 67 68
        self._stack.add(view)
        self._stack.set_transition_type(Gtk.StackTransitionType.SLIDE_DOWN)
        self._stack.set_visible_child(view)
        self._stack.set_transition_type(Gtk.StackTransitionType.CROSSFADE)
Cédric Bellegarde's avatar
Cédric Bellegarde committed
69
        emit_signal(self, "can-go-back-changed", False)
70
        App().enable_special_shortcuts(False)
Cédric Bellegarde's avatar
Cédric Bellegarde committed
71

72
    def show_view(self, item_ids, data=None, storage_type=None):
73 74
        """
            Show view for item id
75
            @param item_ids as [int]
76
            @param data as object
77
            @param storage_type as StorageType
78
        """
79
        view = None
80 81
        if storage_type is None:
            storage_type = get_default_storage_type()
82
        hide_selection_list = True
83 84 85 86
        if item_ids:
            if item_ids[0] in [Type.POPULARS,
                               Type.LOVED,
                               Type.RECENTS,
87
                               Type.LITTLE,
88
                               Type.RANDOMS]:
89
                view = self._get_view_albums(item_ids, [], storage_type)
90 91
            elif item_ids[0] == Type.WEB:
                view = self._get_view_albums(item_ids, [], StorageType.SAVED)
Cédric Bellegarde's avatar
Cédric Bellegarde committed
92
            elif item_ids[0] == Type.SUGGESTIONS:
93
                view = self._get_view_suggestions(storage_type)
94 95 96 97 98 99 100 101
            elif item_ids[0] == Type.SEARCH:
                view = self.get_view_search(data)
            elif item_ids[0] == Type.INFO:
                view = self._get_view_info()
            elif item_ids[0] == Type.DEVICE_ALBUMS:
                view = self._get_view_device_albums(data)
            elif item_ids[0] == Type.DEVICE_PLAYLISTS:
                view = self._get_view_device_playlists(data)
102 103
            elif item_ids[0] == Type.LYRICS:
                view = self._get_view_lyrics()
104 105
            elif item_ids[0] == Type.GENRES:
                if data is None:
106
                    view = self._get_view_genres(storage_type)
107
                else:
108
                    view = self._get_view_albums([data], [], storage_type)
109
            elif item_ids[0] == Type.ALBUM:
110
                hide_selection_list = False
111
                view = self._get_view_album(data, storage_type)
112 113
            elif item_ids[0] == Type.YEARS:
                if data is None:
114
                    view = self._get_view_albums_decades(storage_type)
115
                else:
116
                    view = self._get_view_albums_years(data, storage_type)
117
            elif item_ids[0] == Type.PLAYLISTS:
118
                view = self._get_view_playlists(data)
119 120 121 122 123 124 125 126 127 128 129
            elif item_ids[0] == Type.EQUALIZER:
                from lollypop.view_equalizer import EqualizerView
                view = EqualizerView()
            elif item_ids[0] in [Type.SETTINGS,
                                 Type.SETTINGS_APPEARANCE,
                                 Type.SETTINGS_BEHAVIOUR,
                                 Type.SETTINGS_COLLECTIONS,
                                 Type.SETTINGS_WEB,
                                 Type.SETTINGS_DEVICES]:
                view = self._get_view_settings(item_ids[0])
            elif item_ids[0] == Type.ALL:
130
                view = self._get_view_albums(item_ids, [], storage_type)
131
            elif item_ids[0] == Type.COMPILATIONS:
132
                view = self._get_view_albums(item_ids, [], storage_type)
133
            elif item_ids[0] == Type.ARTISTS:
134
                view = self._get_view_artists([], data, storage_type)
135
        self._sidebar.select_ids(item_ids, False)
136 137 138
        if hide_selection_list:
            self._hide_right_list()
            self.left_list.hide()
139
        if view is not None:
140
            self.set_focused_view(view)
141 142
            view.show()
            self._stack.add(view)
143
            self._stack.set_visible_child(view)
144

Cédric Bellegarde's avatar
Cédric Bellegarde committed
145
    def get_view_current(self):
146 147 148 149
        """
            Get view for current playlist
            @return View
        """
150
        from lollypop.view_current_albums import CurrentAlbumsView
151
        # Search view in children
152 153
        for (_view, _class, args, sidebar_id,
             selection_ids, position) in self._stack.history.items:
Cédric Bellegarde's avatar
Cédric Bellegarde committed
154 155 156
            if _class == CurrentAlbumsView and _view is not None:
                self._stack.history.remove(_view)
                return _view
Cédric Bellegarde's avatar
Cédric Bellegarde committed
157
        view = CurrentAlbumsView(ViewType.DND)
158
        view.populate()
159 160 161
        view.show()
        return view

Cédric Bellegarde's avatar
Cédric Bellegarde committed
162
    def get_view_search(self, search=""):
163 164
        """
            Get view for search
165
            @param search as str
166 167 168
            @return SearchView
        """
        from lollypop.view_search import SearchView
169
        view = None
170 171
        # Search view in current view
        if self.view is not None and isinstance(self.view, SearchView):
172
            view = self.view
173
        # Search view in children
174 175 176 177 178 179 180 181
        else:
            for (_view, _class, args, sidebar_id,
                 selection_ids, position) in self._stack.history.items:
                if _class == SearchView and _view is not None:
                    self._stack.history.remove(_view)
                    view = _view
                    break
        if view is None:
Cédric Bellegarde's avatar
Cédric Bellegarde committed
182 183
            view_type = ViewType.SCROLLED
            view = SearchView("", view_type)
184 185 186 187 188
            view.show()
        if search:
            view.set_search(search)
        else:
            view.grab_focus()
189 190
        return view

191 192 193
##############
# PROTECTED  #
##############
194
    def _get_view_playlists(self, playlist_id=None):
195 196
        """
            Get playlists view for playlists
197
            @param playlist_id as int
198 199
            @return View
        """
200
        view_type = ViewType.PLAYLISTS | ViewType.SCROLLED
201 202 203 204
        if playlist_id is None:
            from lollypop.view_playlists_manager import PlaylistsManagerView
            view = PlaylistsManagerView(view_type)
        else:
205
            from lollypop.view_playlists import PlaylistsView
206
            view_type |= ViewType.DND
207
            view = PlaylistsView(playlist_id, view_type)
208
        view.populate()
209 210
        return view

211 212 213 214 215
    def _get_view_device_playlists(self, index):
        """
            Show playlists for device at index
            @param index as int
        """
216
        view_type = ViewType.SCROLLED
217 218 219
        from lollypop.view_playlists_manager import PlaylistsManagerDeviceView
        view = PlaylistsManagerDeviceView(index, view_type)
        view.populate()
220 221
        return view

222 223 224 225 226
    def _get_view_lyrics(self):
        """
            Show lyrics for track
            @pram track as Track
        """
Cédric Bellegarde's avatar
Cédric Bellegarde committed
227
        view_type = ViewType.SCROLLED
228
        from lollypop.view_lyrics import LyricsView
Cédric Bellegarde's avatar
Cédric Bellegarde committed
229
        view = LyricsView(view_type)
230 231 232 233
        view.populate(App().player.current_track)
        view.show()
        return view

234
    def _get_view_artists_rounded(self, storage_type):
235 236
        """
            Get rounded artists view
237 238
            @param storage_type as StorageType
            @return RoundedArtistsViewWithBanner
239
        """
Cédric Bellegarde's avatar
Cédric Bellegarde committed
240
        view_type = ViewType.SCROLLED
241
        from lollypop.view_artists_rounded import RoundedArtistsViewWithBanner
Cédric Bellegarde's avatar
Cédric Bellegarde committed
242
        view = RoundedArtistsViewWithBanner(storage_type, view_type)
243 244 245
        self._stack.add(view)
        view.populate()
        view.show()
246 247
        return view

248
    def _get_view_artists(self, genre_ids, artist_ids, storage_type):
249 250
        """
            Get artists view for genres/artists
251
            @param genre_ids as [int]
252
            @param artist_ids as [int]
253 254
            @param storage_type as StorageType
            @return ArtistViewBox/ArtistViewList
255
        """
Cédric Bellegarde's avatar
Cédric Bellegarde committed
256
        view_type = ViewType.SCROLLED
257 258
        if App().settings.get_value("show-artist-tracks"):
            from lollypop.view_artist_list import ArtistViewList
Cédric Bellegarde's avatar
Cédric Bellegarde committed
259 260
            view = ArtistViewList(genre_ids, artist_ids,
                                  storage_type, view_type)
261 262
        else:
            from lollypop.view_artist_box import ArtistViewBox
Cédric Bellegarde's avatar
Cédric Bellegarde committed
263 264
            view = ArtistViewBox(genre_ids, artist_ids,
                                 storage_type, view_type)
265
        view.populate()
266
        view.show()
267 268
        return view

269
    def _get_view_suggestions(self, storage_type):
270 271
        """
            Get home view
272 273
            @param storage_type as StorageType
            @return SuggestionsView
274
        """
Cédric Bellegarde's avatar
Cédric Bellegarde committed
275
        view_type = ViewType.SCROLLED
Cédric Bellegarde's avatar
Cédric Bellegarde committed
276
        from lollypop.view_suggestions import SuggestionsView
Cédric Bellegarde's avatar
Cédric Bellegarde committed
277
        view = SuggestionsView(storage_type, view_type)
278 279 280 281
        view.populate()
        view.show()
        return view

282
    def _get_view_albums_decades(self, storage_type):
283 284
        """
            Get album view for decades
285 286
            @param storage_type as StorageType
            @return DecadesBoxView
287
        """
Cédric Bellegarde's avatar
Cédric Bellegarde committed
288
        view_type = ViewType.SCROLLED
289
        from lollypop.view_decades_box import DecadesBoxView
Cédric Bellegarde's avatar
Cédric Bellegarde committed
290
        view = DecadesBoxView(storage_type, view_type)
291 292
        view.populate()
        view.show()
293 294
        return view

295
    def _get_view_album(self, album, storage_type):
296 297 298
        """
            Show album
            @param album as Album
299 300
            @param storage_type as StorageType
            @return AlbumView
301
        """
302
        view_type = ViewType.TWO_COLUMNS | ViewType.SCROLLED |\
Cédric Bellegarde's avatar
Cédric Bellegarde committed
303
            ViewType.OVERLAY | ViewType.ALBUM
Cédric Bellegarde's avatar
Cédric Bellegarde committed
304
        from lollypop.view_album import AlbumView
305
        view = AlbumView(album, storage_type, view_type)
306
        view.populate()
307 308
        return view

309
    def _get_view_genres(self, storage_type):
310 311
        """
            Get view for genres
312 313
            @param storage_type as StorageType
            @return GenresBoxView
314
        """
315
        view_type = ViewType.SCROLLED
Cédric Bellegarde's avatar
Cédric Bellegarde committed
316
        from lollypop.view_genres_box import GenresBoxView
317
        view = GenresBoxView(storage_type, view_type)
318 319
        view.populate()
        view.show()
320 321
        return view

322
    def _get_view_albums_years(self, years, storage_type):
323 324 325
        """
            Get album view for years
            @param years as [int]
326
            @param storage_type as StorageType
Cédric Bellegarde's avatar
Cédric Bellegarde committed
327
            @return AlbumsForYearsBoxView
328
        """
329
        view_type = ViewType.SCROLLED
Cédric Bellegarde's avatar
Cédric Bellegarde committed
330 331 332
        from lollypop.view_albums_box import AlbumsForYearsBoxView
        view = AlbumsForYearsBoxView([Type.YEARS], years,
                                     storage_type, view_type)
333
        view.populate()
334 335
        return view

336
    def _get_view_albums(self, genre_ids, artist_ids, storage_type):
337 338
        """
            Get albums view for genres/artists
339
            @param genre_ids as [int]
340 341
            @param artist_ids as [int]
            @param storage_type as StorageType
Cédric Bellegarde's avatar
Cédric Bellegarde committed
342
            @return AlbumsForGenresBoxView
343
        """
344
        view_type = ViewType.SCROLLED
Cédric Bellegarde's avatar
Cédric Bellegarde committed
345 346 347
        from lollypop.view_albums_box import AlbumsForGenresBoxView
        view = AlbumsForGenresBoxView(genre_ids, artist_ids,
                                      storage_type, view_type)
348
        view.populate()
349 350
        return view

351 352 353 354
    def _get_view_device_albums(self, index):
        """
            Show albums for device at index
            @param index as int
355
            @return AlbumsDeviceBoxView
356
        """
357
        view_type = ViewType.SCROLLED
Cédric Bellegarde's avatar
Cédric Bellegarde committed
358
        from lollypop.view_albums_box import AlbumsDeviceBoxView
359 360
        view = AlbumsDeviceBoxView(index, view_type)
        view.populate()
361 362
        return view

363 364 365 366 367
    def _get_view_info(self):
        """
            Get view for information
            @return InformationView
        """
Cédric Bellegarde's avatar
Cédric Bellegarde committed
368
        view_type = ViewType.SCROLLED
369
        from lollypop.view_information import InformationView
Cédric Bellegarde's avatar
Cédric Bellegarde committed
370
        view = InformationView(view_type, True)
371
        view.populate()
Cédric Bellegarde's avatar
Cédric Bellegarde committed
372 373
        view.set_margin_top(MARGIN_SMALL)
        view.set_margin_start(MARGIN_SMALL)
374 375
        return view

Cédric Bellegarde's avatar
Cédric Bellegarde committed
376 377 378 379
    def _get_view_settings(self, item_id):
        """
            Show settings views
            @param item_id as int
380
            @return SettingsView/SettingsChildView
Cédric Bellegarde's avatar
Cédric Bellegarde committed
381 382 383
        """
        if item_id == Type.SETTINGS:
            from lollypop.view_settings import SettingsView
384
            view = SettingsView(ViewType.SCROLLED)
Cédric Bellegarde's avatar
Cédric Bellegarde committed
385 386
        else:
            from lollypop.view_settings import SettingsChildView
387
            view = SettingsChildView(item_id, ViewType.SCROLLED)
Cédric Bellegarde's avatar
Cédric Bellegarde committed
388
        return view