1# Copyright (c) 2014-2021 Cedric Bellegarde <cedric.bellegarde@adishatz.org>
2# This program is free software: you can redistribute it and/or modify
3# it under the terms of the GNU General Public License as published by
4# the Free Software Foundation, either version 3 of the License, or
5# (at your option) any later version.
6# This program is distributed in the hope that it will be useful,
7# but WITHOUT ANY WARRANTY; without even the implied warranty of
8# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9# GNU General Public License for more details.
10# You should have received a copy of the GNU General Public License
11# along with this program. If not, see <http://www.gnu.org/licenses/>.
12
13from gi.repository import Gtk, GLib, GObject
14
15from lollypop.utils import emit_signal, on_query_tooltip, get_network_available
16from lollypop.define import App, ArtSize, ArtBehaviour, ViewType
17from lollypop.widgets_banner import BannerWidget
18from lollypop.helper_signals import SignalsHelper, signals_map
19
20
21class InformationBannerWidget(BannerWidget, SignalsHelper):
22    """
23        Banner for Information
24    """
25
26    __gsignals__ = {
27        "search": (GObject.SignalFlags.RUN_FIRST, None, (bool,)),
28    }
29
30    @signals_map
31    def __init__(self, artist_id):
32        """
33            Init information banner
34            @param artist_id as int
35        """
36        BannerWidget.__init__(self, ViewType.OVERLAY)
37        self.__artist_id = artist_id
38        builder = Gtk.Builder()
39        builder.add_from_resource(
40            "/org/gnome/Lollypop/InformationBannerWidget.ui")
41        builder.connect_signals(self)
42        self.__button = builder.get_object("button")
43        self.__badge_artwork = builder.get_object("badge_artwork")
44        self.__title_label = builder.get_object("artist")
45        self.__title_label.connect("query-tooltip", on_query_tooltip)
46        self.__title_label.set_property("has-tooltip", True)
47        self.__widget = builder.get_object("widget")
48        self.__title_label.set_markup(GLib.markup_escape_text(
49            App().artists.get_name(artist_id)))
50#        self.__title_label.get_style_context().add_class("text-x-large")
51        self._overlay.add_overlay(self.__widget)
52        self._overlay.set_overlay_pass_through(self.__widget, True)
53        self.__set_internal_size()
54        if not get_network_available("WIKIPEDIA"):
55            self.__button.set_sensitive(False)
56        return [
57               (App().window.container.widget, "notify::folded",
58                "_on_container_folded"),
59        ]
60
61    def update_for_width(self, width):
62        """
63            Update banner internals for width, call this before showing banner
64            @param width as int
65        """
66        BannerWidget.update_for_width(self, width)
67        self.__set_artwork()
68
69    @property
70    def button(self):
71        """
72            Get toggle button
73            @return Gtk.ToggleButton
74        """
75        return self.__button
76
77#######################
78# PROTECTED           #
79#######################
80    def _handle_width_allocate(self, allocation):
81        """
82            Update artwork
83            @param allocation as Gtk.Allocation
84        """
85        if BannerWidget._handle_width_allocate(self, allocation):
86            self.__set_artwork()
87
88    def _on_container_folded(self, leaflet, folded):
89        """
90            Handle libhandy folded status
91            @param leaflet as Handy.Leaflet
92            @param folded as Gparam
93        """
94        self.__set_internal_size()
95
96    def _on_button_toggled(self, button):
97        """
98            Emit search signal
99            @param button as Gtk.ToggleButton
100        """
101        if get_network_available("WIKIPEDIA"):
102            emit_signal(self, "search", button.get_active())
103
104#######################
105# PRIVATE             #
106#######################
107    def __set_artwork(self):
108        """
109            Set artwork
110        """
111        if App().settings.get_value("artist-artwork") and App().animations:
112            artist = App().artists.get_name(self.__artist_id)
113            App().art_helper.set_artist_artwork(
114                                        artist,
115                                        # +100 to prevent resize lag
116                                        self.width + 100,
117                                        self.height,
118                                        self.get_scale_factor(),
119                                        ArtBehaviour.BLUR_HARD |
120                                        ArtBehaviour.DARKER,
121                                        self._on_artwork)
122        else:
123            self._artwork.get_style_context().add_class("default-banner")
124        if self.width < ArtSize.BANNER * 3:
125            if self.__widget.get_child_at(1, 0) == self.__title_label:
126                self.__widget.remove(self.__title_label)
127                self.__widget.attach(self.__title_label, 2, 2, 3, 1)
128        elif self.__widget.get_child_at(2, 2) == self.__title_label:
129            self.__widget.remove(self.__title_label)
130            self.__widget.attach(self.__title_label, 1, 0, 1, 3)
131
132    def __set_badge_artwork(self, art_size):
133        """
134            Set artist artwork on badge
135            @param art_size as int
136        """
137        if App().settings.get_value("artist-artwork"):
138            artist = App().artists.get_name(self.__artist_id)
139            App().art_helper.set_artist_artwork(
140                                        artist,
141                                        art_size,
142                                        art_size,
143                                        self.get_scale_factor(),
144                                        ArtBehaviour.ROUNDED |
145                                        ArtBehaviour.CROP_SQUARE |
146                                        ArtBehaviour.CACHE,
147                                        self.__on_badge_artist_artwork,
148                                        art_size)
149        else:
150            self.__badge_artwork.hide()
151
152    def __set_internal_size(self):
153        """
154            Set content size based on current width
155        """
156        # Text size
157        title_context = self.__title_label.get_style_context()
158        for c in title_context.list_classes():
159            title_context.remove_class(c)
160        if App().window.folded:
161            art_size = ArtSize.MEDIUM
162            cls = "text-large"
163        else:
164            art_size = ArtSize.BANNER
165            cls = "text-x-large"
166        self.__title_label.get_style_context().add_class(cls)
167        self.__set_badge_artwork(art_size)
168
169    def __on_badge_artist_artwork(self, surface, art_size):
170        """
171            Set artist artwork on badge
172            @param surface as cairo.Surface
173            @param art_size as int
174        """
175        if surface is None:
176            self.__badge_artwork.get_style_context().add_class("circle-icon")
177            self.__badge_artwork.set_size_request(art_size, art_size)
178            self.__badge_artwork.set_from_icon_name(
179                                              "avatar-default-symbolic",
180                                              Gtk.IconSize.DIALOG)
181        else:
182            self.__badge_artwork.get_style_context().remove_class(
183                "circle-icon")
184            self.__badge_artwork.set_from_surface(surface)
185