wallpaper_flowbox_item.py 3.02 KB
Newer Older
1
from gettext import gettext as _
2
from gi.repository import Gtk, Gdk, Gio, GdkPixbuf
3
import os
4
from . import threading_helper as ThreadingHelper
5
from PIL import Image
6
7
from hashlib import sha256
from .confManager import ConfManager
8
from pathlib import Path
9
10
11

class WallpaperBox(Gtk.FlowBoxChild):

12
13
    def __init__(self, wp_path, **kwargs):
        super().__init__(**kwargs)
14
        self.confman = ConfManager()
15
16
17
18

        self.set_halign(Gtk.Align.CENTER)
        self.set_valign(Gtk.Align.CENTER)

19
        self.wallpaper_path = Path(wp_path)
20
21
22
23
        self.cache_path = '{0}/{1}.png'.format(
            self.confman.thumbs_cache_path,
            sha256(
                f'HydraPaperThumb{self.wallpaper_path}'.encode()
24
25
            ).hexdigest()
        )
26
        self.is_fav = False
27
        self.container_box = Gtk.Overlay()
28
29
        self.container_box.set_halign(Gtk.Align.CENTER)
        self.container_box.set_valign(Gtk.Align.CENTER)
30
        self.wp_image = Gtk.Image.new_from_icon_name('image-x-generic', Gtk.IconSize.DIALOG)
31
32
33
34
35
36
37
        self.heart_icon = Gtk.Image.new_from_icon_name('emblem-favorite', Gtk.IconSize.DIALOG)
        self.heart_icon.set_no_show_all(True)
        self.heart_icon.set_halign(Gtk.Align.END)
        self.heart_icon.set_valign(Gtk.Align.END)
        self.heart_icon.set_margin_bottom(6)
        self.heart_icon.set_margin_right(6)
        self.container_box.add(self.wp_image)
38
39
40
        self.container_box.set_margin_left(12)
        self.container_box.set_margin_right(12)
        self.container_box.wallpaper_path = wp_path
41
42
43
44

        self.container_box.add_overlay(self.heart_icon)
        self.heart_icon.hide()

45
        self.add(self.container_box)
46
        self.set_wallpaper_thumb()
47
        self.set_fav(self.wallpaper_path in self.confman.conf['favorites'])
48
49

    def set_wallpaper_thumb(self):
50
51
52
53
54
55
        if not os.path.isfile(self.cache_path):
            mkthumb_thread = ThreadingHelper.do_async(
                self.make_wallpaper_thumb,
                (self.wallpaper_path,)
            )
            ThreadingHelper.wait_for_thread(mkthumb_thread)
56
        self.wp_image.set_from_file(self.cache_path)
57
58
        self.wp_image.show()

59
    def set_fav(self, fav: bool):
60
61
62
        self.is_fav = fav
        if self.is_fav:
            self.heart_icon.show()
63
64
            if not self.wallpaper_path in self.confman.conf['favorites']:
                self.confman.conf['favorites'].append(self.wallpaper_path)
65
66
        else:
            self.heart_icon.hide()
67
68
69
70
            if self.wallpaper_path in self.confman.conf['favorites']:
                self.confman.conf['favorites'].pop(
                    self.confman.conf['favorites'].index(self.wallpaper_path)
                )
71
        self.confman.save_conf()
72

73
    def make_wallpaper_thumb(self, wp_path):
74
75
76
        try:
            thumb = Image.open(self.wallpaper_path)
            thumb.thumbnail((250, 250), Image.ANTIALIAS)
77
            thumb.save(self.cache_path, 'PNG')
78
        except IOError:
79
            print(_('ERROR: cannot create thumbnail for file'), self.wallpaper_path)
80
        return self.cache_path