2023-04-23 21:18:35 -05:00
|
|
|
# Python imports
|
|
|
|
|
|
|
|
|
|
# Lib imports
|
|
|
|
|
import gi
|
|
|
|
|
gi.require_version('Gtk', '3.0')
|
2023-04-23 22:07:50 -05:00
|
|
|
gi.require_version('GdkPixbuf', '2.0')
|
2023-04-23 21:18:35 -05:00
|
|
|
from gi.repository import Gtk
|
2023-04-23 23:35:23 -05:00
|
|
|
from gi.repository import GLib
|
2023-04-23 22:07:50 -05:00
|
|
|
from gi.repository import GdkPixbuf
|
|
|
|
|
|
2023-04-23 21:18:35 -05:00
|
|
|
|
|
|
|
|
# Application imports
|
2023-04-27 20:01:53 -05:00
|
|
|
from .image_view import ImageView, PImage
|
2023-04-23 21:18:35 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Image(Gtk.EventBox):
|
2026-05-22 16:01:31 -05:00
|
|
|
def __init__(self, path: str):
|
2023-04-23 21:18:35 -05:00
|
|
|
super(Image, self).__init__()
|
|
|
|
|
|
2026-05-22 18:27:16 -05:00
|
|
|
self._thumbnail_with = settings_manager.settings.config.thumbnail_with
|
|
|
|
|
self._thumbnail_height = settings_manager.settings.config.thumbnail_height
|
2023-04-24 00:12:56 -05:00
|
|
|
self.is_loaded = False
|
|
|
|
|
self.image = None
|
|
|
|
|
self.path = path
|
2023-04-23 21:18:35 -05:00
|
|
|
|
|
|
|
|
self._setup_styling()
|
|
|
|
|
self._setup_signals()
|
|
|
|
|
self._load_widgets()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _setup_styling(self):
|
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
def _setup_signals(self):
|
|
|
|
|
self.connect("button-release-event", self.set_image_to_view)
|
|
|
|
|
|
|
|
|
|
def _subscribe_to_events(self):
|
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
def _load_widgets(self):
|
|
|
|
|
self.image = Gtk.Image()
|
|
|
|
|
self.image.show()
|
|
|
|
|
self.add(self.image)
|
|
|
|
|
|
|
|
|
|
def set_image_to_view(self, widget = None, eve = None):
|
|
|
|
|
if eve.button == 1:
|
2026-05-22 18:27:16 -05:00
|
|
|
event_system.emit("handle-file-from-dnd", (self.path, ))
|
2023-04-23 22:07:50 -05:00
|
|
|
|
|
|
|
|
def load_pixbuf(self):
|
|
|
|
|
self.set_from_pixbuf( self.get_pixbuf_data(self.path, \
|
|
|
|
|
self._thumbnail_with, \
|
|
|
|
|
self._thumbnail_height) )
|
2023-04-24 00:12:56 -05:00
|
|
|
self.is_loaded = True
|
2023-04-23 22:07:50 -05:00
|
|
|
|
2023-04-24 22:31:05 -05:00
|
|
|
def set_from_pixbuf(self, pixbuf):
|
|
|
|
|
self.image.set_from_pixbuf(pixbuf)
|
|
|
|
|
|
2026-05-22 18:27:16 -05:00
|
|
|
def get_pixbuf_data(self, path: str, w: int = 126, h: int = 126):
|
2023-04-23 22:07:50 -05:00
|
|
|
path = self.path if not path else path
|
|
|
|
|
pixbuf = None
|
|
|
|
|
|
|
|
|
|
if PImage and path.endswith(".webp"):
|
2023-04-28 19:40:16 -05:00
|
|
|
pixbuf = ImageView.image2pixbuf(path)
|
2023-04-23 22:07:50 -05:00
|
|
|
|
2023-04-27 17:57:23 -05:00
|
|
|
if path.endswith(".gif"):
|
|
|
|
|
pixbuf = GdkPixbuf.PixbufAnimation.new_from_file(path).get_static_image()
|
|
|
|
|
|
2026-05-22 16:01:31 -05:00
|
|
|
if pixbuf:
|
|
|
|
|
return pixbuf.scale_simple(w, h, 2) # 2 = BILINEAR and is best by default
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
return GdkPixbuf.Pixbuf.new_from_file_at_scale(path, w, h, True)
|
|
|
|
|
except Exception:
|
|
|
|
|
return Gtk.Image.new_from_resource(path) \
|
|
|
|
|
.get_pixbuf() \
|
|
|
|
|
.scale_simple(w, h, 2) # 2 = BILINEAR and is best by default
|
2023-04-23 22:07:50 -05:00
|
|
|
|