Slight Refactoring

This commit is contained in:
itdominator 2023-04-23 22:07:50 -05:00
parent 19ae2974c3
commit a8f423e8b8
3 changed files with 51 additions and 22 deletions

View File

@ -65,6 +65,9 @@ class ImageViewScroll(Gtk.ScrolledWindow):
def _do_filter_open(self, uris: [] = []): def _do_filter_open(self, uris: [] = []):
if len(uris) == 0:
return
path, img_list = self.filter_for_images(uris) path, img_list = self.filter_for_images(uris)
self._handle_open(path, img_list) self._handle_open(path, img_list)

View File

@ -3,7 +3,14 @@
# Lib imports # Lib imports
import gi import gi
gi.require_version('Gtk', '3.0') gi.require_version('Gtk', '3.0')
gi.require_version('GdkPixbuf', '2.0')
from gi.repository import Gtk from gi.repository import Gtk
from gi.repository import GdkPixbuf
try:
from PIL import Image as PImage
except Exception as e:
PImage = None
# Application imports # Application imports
@ -13,6 +20,8 @@ class Image(Gtk.EventBox):
def __init__(self, path = None): def __init__(self, path = None):
super(Image, self).__init__() super(Image, self).__init__()
self._thumbnail_with = settings.get_thumbnail_with()
self._thumbnail_height = settings.get_thumbnail_height()
self.image = None self.image = None
self.path = path self.path = path
@ -41,3 +50,34 @@ class Image(Gtk.EventBox):
def set_image_to_view(self, widget = None, eve = None): def set_image_to_view(self, widget = None, eve = None):
if eve.button == 1: if eve.button == 1:
event_system.emit("handle_file_from_dnd", (self.path, )) event_system.emit("handle_file_from_dnd", (self.path, ))
def load_pixbuf(self):
self.set_from_pixbuf( self.get_pixbuf_data(self.path, \
self._thumbnail_with, \
self._thumbnail_height) )
def get_pixbuf_data(self, path, w = 126, h = 126):
path = self.path if not path else path
pixbuf = None
if PImage and path.endswith(".webp"):
return self.image2pixbuf(path, w, h)
try:
pixbuf = Gtk.Image.new_from_file(path).get_pixbuf()
except Exception:
pixbuf = Gtk.Image.new_from_resource(path).get_pixbuf()
return pixbuf.scale_simple(w, h, 2) # 2 = BILINEAR and is best by default
def image2pixbuf(self, path, _w, _h):
"""Convert Pillow image to GdkPixbuf"""
im = PImage.open(path)
data = im.tobytes()
data = GLib.Bytes.new(data)
w, h = im.size
pixbuf = GdkPixbuf.Pixbuf.new_from_bytes(data, GdkPixbuf.Colorspace.RGB,
False, 8, w, h, w * 3)
return pixbuf.scale_simple(_w, _h, 2) # 2 = BILINEAR and is best by default

View File

@ -16,8 +16,6 @@ class ImageList(Gtk.Box):
self.path = None self.path = None
self.img_list = None self.img_list = None
self._thumbnail_with = settings.get_thumbnail_with()
self._thumbnail_height = settings.get_thumbnail_height()
self._setup_styling() self._setup_styling()
self._setup_signals() self._setup_signals()
@ -64,29 +62,17 @@ class ImageList(Gtk.Box):
for file in paths: for file in paths:
self.add( Image(file) ) self.add( Image(file) )
self.pre_fill_gtkimgs(paths) self.pre_fill_gtkimgs()
def pre_fill_gtkimgs(self, paths: [] = []):
children = self.get_children()
for i, path in enumerate(paths):
self.load_pixbuf_to_target(path, children[i])
self.show_range() self.show_range()
def pre_fill_gtkimgs(self):
children = self.get_children()
for child in children:
self.load_pixbuf_to_target(child)
@daemon_threaded @daemon_threaded
def load_pixbuf_to_target(self, path, child): def load_pixbuf_to_target(self, child):
pixbuf = self.get_pixbuf_data(path, self._thumbnail_with, self._thumbnail_height) child.load_pixbuf()
child.set_from_pixbuf(pixbuf)
def get_pixbuf_data(self, path, w = 126, h = 126):
pixbuf = None
try:
pixbuf = Gtk.Image.new_from_file(path).get_pixbuf()
except Exception:
pixbuf = Gtk.Image.new_from_resource(path).get_pixbuf()
return pixbuf.scale_simple(w, h, 2) # 2 = BILINEAR and is best by default
def show_range(self, i = 0, j = 9): def show_range(self, i = 0, j = 9):
children = self.get_children() children = self.get_children()