toolbar_info.py 7.4 KB
Newer Older
Cédric Bellegarde's avatar
Cédric Bellegarde committed
1
# Copyright (c) 2014-2018 Cedric Bellegarde <cedric.bellegarde@adishatz.org>
2 3 4 5 6 7 8 9 10 11 12 13 14
# 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/>.

from gi.repository import Gtk, Gdk, GLib

Cédric Bellegarde's avatar
Cédric Bellegarde committed
15
from lollypop.controllers import InfoController
Cédric Bellegarde's avatar
Cédric Bellegarde committed
16
from lollypop.touch_helper import TouchHelper
17
from lollypop.define import App, Type, ArtSize
18 19


Cédric Bellegarde's avatar
Cédric Bellegarde committed
20
class ToolbarInfo(Gtk.Bin, InfoController):
21 22 23 24 25 26 27 28 29
    """
        Informations toolbar
    """

    def __init__(self):
        """
            Init toolbar
        """
        Gtk.Bin.__init__(self)
Cédric Bellegarde's avatar
Cédric Bellegarde committed
30
        InfoController.__init__(self, ArtSize.SMALL)
31
        builder = Gtk.Builder()
Cédric Bellegarde's avatar
Cédric Bellegarde committed
32
        builder.add_from_resource("/org/gnome/Lollypop/ToolbarInfo.ui")
33
        builder.connect_signals(self)
34
        self.__pop_tunein = None
35
        self.__pop_info = None
36 37
        self.__timeout_id = None
        self.__width = 0
38

Cédric Bellegarde's avatar
Cédric Bellegarde committed
39
        self._infobox = builder.get_object("info")
40 41
        self.add(self._infobox)

42 43 44
        self.__helper = TouchHelper(self._infobox, None, None)
        self.__helper.set_long_func(self.__on_info_long)
        self.__helper.set_short_func(self.__on_info_short)
Cédric Bellegarde's avatar
Cédric Bellegarde committed
45

Cédric Bellegarde's avatar
Cédric Bellegarde committed
46
        self._spinner = builder.get_object("spinner")
47

Cédric Bellegarde's avatar
Cédric Bellegarde committed
48 49 50
        self.__labels = builder.get_object("nowplaying_labels")
        self.__labels.connect("query-tooltip", self.__on_query_tooltip)
        self.__labels.set_property("has-tooltip", True)
51

Cédric Bellegarde's avatar
Cédric Bellegarde committed
52 53 54 55
        self._title_label = builder.get_object("title")
        self._artist_label = builder.get_object("artist")
        self._cover = builder.get_object("cover")
        self._cover.set_property("has-tooltip", True)
56 57
        # Since GTK 3.20, we can set cover full height
        if Gtk.get_minor_version() > 18:
Cédric Bellegarde's avatar
Cédric Bellegarde committed
58
            self._cover.get_style_context().add_class("toolbar-cover-frame")
59
        else:
Cédric Bellegarde's avatar
Cédric Bellegarde committed
60
            self._cover.get_style_context().add_class("small-cover-frame")
61

Cédric Bellegarde's avatar
Cédric Bellegarde committed
62
        self.connect("realize", self.__on_realize)
63 64 65
        App().player.connect("loading-changed", self.__on_loading_changed)
        App().art.connect("album-artwork-changed", self.__update_cover)
        App().art.connect("radio-artwork-changed", self.__update_logo)
66 67 68 69 70 71

    def do_get_preferred_width(self):
        """
            We force preferred width
            @return (int, int)
        """
72
        return (self.__width, self.__width)
73 74 75 76 77 78

    def get_preferred_height(self):
        """
            Return preferred height
            @return (int, int)
        """
79
        return self.__labels.get_preferred_height()
80 81 82 83 84 85

    def set_width(self, width):
        """
            Set widget width
            @param width as int
        """
86
        self.__width = width
Cédric Bellegarde's avatar
Cédric Bellegarde committed
87
        self.set_property("width-request", width)
88

89 90 91 92 93 94 95
#######################
# PROTECTED           #
#######################
    def _on_eventbox_realize(self, eventbox):
        """
            Show hand cursor over
        """
96
        eventbox.get_window().set_cursor(Gdk.Cursor(Gdk.CursorType.HAND2))
97

98 99 100
#######################
# PRIVATE             #
#######################
101
    def __update_cover(self, art, album_id):
102 103 104 105 106
        """
            Update cover for album_id
            @param art as Art
            @param album id as int
        """
107 108 109
        if App().player.current_track.album.id == album_id:
            surface = App().art.get_album_artwork(
                                       App().player.current_track.album,
110
                                       self._artsize,
111
                                       self._cover.get_scale_factor())
112 113
            self._cover.set_from_surface(surface)

114
    def __update_logo(self, art, name):
115 116 117 118 119
        """
            Update logo for name
            @param art as Art
            @param name as str
        """
120 121
        if App().player.current_track.album_artist == name:
            pixbuf = App().art.get_radio_artwork(name, self._artsize)
122 123
            self._cover.set_from_surface(pixbuf)

Cédric Bellegarde's avatar
Cédric Bellegarde committed
124
    def __on_info_long(self, args):
125 126
        """
            Show current track menu
Cédric Bellegarde's avatar
Cédric Bellegarde committed
127
            @param args as []
128
        """
129
        if App().player.current_track.id >= 0:
130
            from lollypop.pop_menu import PlaylistsMenu
131
            from lollypop.pop_menu import TrackMenuPopover
132
            popover = TrackMenuPopover(
133 134
                        App().player.current_track,
                        PlaylistsMenu(App().player.current_track))
135 136 137
            popover.set_relative_to(self._infobox)
            popover.show()

Cédric Bellegarde's avatar
Cédric Bellegarde committed
138 139 140 141 142
    def __on_info_short(self, args):
        """
            Show track information popover
            @param args as []
        """
143
        if App().player.current_track.id == Type.EXTERNALS:
Cédric Bellegarde's avatar
Cédric Bellegarde committed
144 145 146 147 148
            from lollypop.pop_externals import ExternalsPopover
            expopover = ExternalsPopover()
            expopover.set_relative_to(self._infobox)
            expopover.populate()
            expopover.show()
149
        elif App().player.current_track.id is not None:
150 151
            if self.__pop_info is None:
                from lollypop.pop_info import InfoPopover
152
                self.__pop_info = InfoPopover([])
153
                self.__pop_info.set_relative_to(self._infobox)
154
            if App().player.current_track.id == Type.RADIOS:
Cédric Bellegarde's avatar
Cédric Bellegarde committed
155 156 157 158
                view_type = Type.RADIOS
            else:
                view_type = Type.ALBUMS
            self.__pop_info.set_view_type(view_type)
159
            self.__pop_info.show()
Cédric Bellegarde's avatar
Cédric Bellegarde committed
160

161
    def __on_loading_changed(self, player, show):
162 163 164
        """
            Show spinner based on loading status
            @param player as player
165 166 167 168 169 170 171 172 173 174 175 176
            @param show as bool
        """
        if show:
            self._title_label.hide()
            self._artist_label.hide()
            self._cover.hide()
            self._spinner.show()
            self._spinner.start()
            self._infobox.show()
        else:
            self._spinner.hide()
            self._spinner.stop()
177

178
    def __on_realize(self, toolbar):
179
        """
180
            Calculate art size
181
            @param toolbar as ToolbarInfos
182
        """
183 184
        style = self.get_style_context()
        padding = style.get_padding(style.get_state())
185
        self._artsize = self.get_allocated_height()\
186 187 188
            - padding.top - padding.bottom
        # Since GTK 3.20, we can set cover full height
        if Gtk.get_minor_version() < 20:
189
            self._artsize -= 2
190

191
    def __on_query_tooltip(self, widget, x, y, keyboard, tooltip):
192 193 194 195 196 197 198 199 200 201 202
        """
            Show tooltip if needed
            @param widget as Gtk.Widget
            @param x as int
            @param y as int
            @param keyboard as bool
            @param tooltip as Gtk.Tooltip
        """
        layout_title = self._title_label.get_layout()
        layout_artist = self._artist_label.get_layout()
        if layout_title.is_ellipsized() or layout_artist.is_ellipsized():
203 204
            artist = GLib.markup_escape_text(self._artist_label.get_text())
            title = GLib.markup_escape_text(self._title_label.get_text())
205 206 207 208
            tooltip.set_markup("<b>%s</b> - %s" % (artist, title))
        else:
            return False
        return True