2023-04-23 21:47:33 +00:00
|
|
|
# Python imports
|
|
|
|
|
|
|
|
# Lib imports
|
|
|
|
import gi
|
|
|
|
gi.require_version('Gtk', '3.0')
|
|
|
|
gi.require_version('Gdk', '3.0')
|
2023-04-27 22:57:23 +00:00
|
|
|
gi.require_version('GdkPixbuf', '2.0')
|
2023-04-23 21:47:33 +00:00
|
|
|
from gi.repository import Gtk
|
|
|
|
from gi.repository import Gdk
|
2023-04-25 03:31:05 +00:00
|
|
|
from gi.repository import GdkPixbuf
|
2023-04-23 21:47:33 +00:00
|
|
|
|
|
|
|
# Application imports
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ImageView(Gtk.Image):
|
|
|
|
def __init__(self):
|
|
|
|
super(ImageView, self).__init__()
|
|
|
|
|
2023-04-25 21:12:30 +00:00
|
|
|
self.pixbuf = None
|
|
|
|
self.work_pixbuf = None
|
|
|
|
self.animation = None
|
2023-04-27 03:00:13 +00:00
|
|
|
self.fit_to_win = True
|
2023-04-23 21:47:33 +00:00
|
|
|
|
|
|
|
self._setup_styling()
|
|
|
|
self._setup_signals()
|
|
|
|
self._subscribe_to_events()
|
|
|
|
self._load_widgets()
|
|
|
|
|
|
|
|
self.show_all()
|
|
|
|
|
|
|
|
|
|
|
|
def _setup_styling(self):
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
|
|
def _setup_signals(self):
|
|
|
|
...
|
|
|
|
|
|
|
|
def _subscribe_to_events(self):
|
2023-04-25 21:12:30 +00:00
|
|
|
event_system.subscribe("zoom_out", self._zoom_out)
|
2023-04-23 21:47:33 +00:00
|
|
|
event_system.subscribe("handle_file_from_dnd", self._handle_file_from_dnd)
|
2023-04-25 03:31:05 +00:00
|
|
|
event_system.subscribe("vertical_flip", self._vertical_flip)
|
|
|
|
event_system.subscribe("rotate_left", self._rotate_left)
|
|
|
|
event_system.subscribe("scale_1_two_1", self._scale_1_two_1)
|
|
|
|
event_system.subscribe("fit_to_container", self._fit_to_container)
|
|
|
|
event_system.subscribe("horizontal_flip", self._horizontal_flip)
|
|
|
|
event_system.subscribe("rotate_right", self._rotate_right)
|
2023-04-25 21:12:30 +00:00
|
|
|
event_system.subscribe("zoom_in", self._zoom_in)
|
2023-04-23 21:47:33 +00:00
|
|
|
|
2023-04-27 00:26:19 +00:00
|
|
|
event_system.subscribe("size_allocate", self._size_allocate)
|
|
|
|
|
2023-04-23 21:47:33 +00:00
|
|
|
def _load_widgets(self):
|
|
|
|
...
|
|
|
|
|
|
|
|
def _handle_file_from_dnd(self, path = None):
|
|
|
|
logger.debug(f"Loading image from: {path}")
|
|
|
|
self.load_path(path)
|
|
|
|
|
|
|
|
if self.animation:
|
|
|
|
logger.debug("Start animation stub...")
|
|
|
|
|
|
|
|
def load_path(self, path):
|
2023-04-25 21:12:30 +00:00
|
|
|
self.pixbuf = None
|
|
|
|
self.work_pixbuf = None
|
|
|
|
self.animation = None
|
2023-04-23 21:47:33 +00:00
|
|
|
|
|
|
|
if path.endswith(".gif"):
|
2023-04-27 22:57:23 +00:00
|
|
|
self.work_pixbuf = GdkPixbuf.PixbufAnimation.new_from_file(path).get_static_image()
|
2023-04-23 21:47:33 +00:00
|
|
|
try:
|
|
|
|
self.animation = Gtk.Image.new_from_file(path).get_animation()
|
|
|
|
except Exception:
|
|
|
|
self.animation = Gtk.Image.new_from_resource(path).get_animation()
|
|
|
|
|
|
|
|
|
2023-04-27 22:57:23 +00:00
|
|
|
if not self.work_pixbuf:
|
|
|
|
try:
|
|
|
|
self.work_pixbuf = Gtk.Image.new_from_file(path).get_pixbuf()
|
|
|
|
except Exception:
|
|
|
|
self.work_pixbuf = Gtk.Image.new_from_resource(path).get_pixbuf()
|
2023-04-25 21:12:30 +00:00
|
|
|
|
|
|
|
self.pixbuf = self.work_pixbuf
|
|
|
|
self.set_from_pixbuf(self.work_pixbuf)
|
|
|
|
|
|
|
|
if self.fit_to_win:
|
|
|
|
self._fit_to_container()
|
2023-04-23 21:47:33 +00:00
|
|
|
|
2023-04-24 02:18:35 +00:00
|
|
|
event_system.emit("update_path_label", (path,))
|
2023-04-25 03:31:05 +00:00
|
|
|
|
2023-04-25 21:12:30 +00:00
|
|
|
|
|
|
|
def _zoom_out(self):
|
2023-04-27 22:57:23 +00:00
|
|
|
if self.work_pixbuf and self.pixbuf:
|
2023-04-25 21:12:30 +00:00
|
|
|
# TODO: Setup scale factor setting to pull from settings...
|
|
|
|
stepx = self.work_pixbuf.get_width() * 0.05
|
|
|
|
stepy = self.work_pixbuf.get_height() * 0.05
|
|
|
|
|
|
|
|
w = self.work_pixbuf.get_width() - stepx
|
|
|
|
h = self.work_pixbuf.get_height() - stepy
|
|
|
|
|
|
|
|
self.work_pixbuf = self.pixbuf.scale_simple(w, h, 2) # 2 = BILINEAR and is best by default
|
|
|
|
self.set_from_pixbuf(self.work_pixbuf)
|
|
|
|
|
2023-04-25 03:31:05 +00:00
|
|
|
def _rotate_left(self):
|
2023-04-27 22:57:23 +00:00
|
|
|
if self.work_pixbuf and self.pixbuf:
|
2023-04-25 21:12:30 +00:00
|
|
|
self.work_pixbuf = self.work_pixbuf.rotate_simple(GdkPixbuf.PixbufRotation.COUNTERCLOCKWISE)
|
|
|
|
self.pixbuf = self.pixbuf.rotate_simple(GdkPixbuf.PixbufRotation.COUNTERCLOCKWISE)
|
|
|
|
self.set_from_pixbuf(self.work_pixbuf)
|
2023-04-25 03:31:05 +00:00
|
|
|
|
|
|
|
def _vertical_flip(self):
|
2023-04-27 22:57:23 +00:00
|
|
|
if self.work_pixbuf and self.pixbuf:
|
2023-04-27 00:26:19 +00:00
|
|
|
self.work_pixbuf = self.work_pixbuf.flip(True)
|
|
|
|
self.pixbuf = self.pixbuf.flip(True)
|
2023-04-25 21:12:30 +00:00
|
|
|
self.set_from_pixbuf(self.work_pixbuf)
|
2023-04-25 03:31:05 +00:00
|
|
|
|
|
|
|
def _scale_1_two_1(self):
|
2023-04-25 21:12:30 +00:00
|
|
|
self.fit_to_win = False
|
2023-04-27 22:57:23 +00:00
|
|
|
if self.work_pixbuf and self.pixbuf:
|
2023-04-25 21:12:30 +00:00
|
|
|
self.work_pixbuf = self.pixbuf
|
|
|
|
self.set_from_pixbuf(self.work_pixbuf)
|
2023-04-25 03:31:05 +00:00
|
|
|
|
|
|
|
def _fit_to_container(self):
|
2023-04-25 21:12:30 +00:00
|
|
|
self.fit_to_win = True
|
2023-04-27 22:57:23 +00:00
|
|
|
if self.work_pixbuf and self.pixbuf:
|
2023-04-25 21:12:30 +00:00
|
|
|
parent_aloc = self.get_parent().get_parent().get_allocation()
|
|
|
|
pw = parent_aloc.width
|
|
|
|
ph = parent_aloc.height
|
|
|
|
iw = self.pixbuf.get_width()
|
|
|
|
ih = self.pixbuf.get_height()
|
|
|
|
w = 0
|
|
|
|
h = 0
|
|
|
|
|
|
|
|
if iw == 0 or ih == 0:
|
|
|
|
return
|
|
|
|
|
|
|
|
w = pw;
|
|
|
|
h = ((ih * w) / iw + 0.5)
|
|
|
|
|
|
|
|
if h > ph:
|
|
|
|
h = ph
|
|
|
|
w = (iw * h) / ih + 0.5
|
|
|
|
|
|
|
|
|
|
|
|
self.work_pixbuf = self.pixbuf.scale_simple(w, h, 2) # 2 = BILINEAR and is best by default
|
|
|
|
self.set_from_pixbuf(self.work_pixbuf)
|
2023-04-25 03:31:05 +00:00
|
|
|
|
|
|
|
def _horizontal_flip(self):
|
2023-04-27 22:57:23 +00:00
|
|
|
if self.work_pixbuf and self.pixbuf:
|
2023-04-27 00:26:19 +00:00
|
|
|
self.work_pixbuf = self.work_pixbuf.flip(False)
|
|
|
|
self.pixbuf = self.pixbuf.flip(False)
|
2023-04-25 21:12:30 +00:00
|
|
|
self.set_from_pixbuf(self.work_pixbuf)
|
2023-04-25 03:31:05 +00:00
|
|
|
|
|
|
|
def _rotate_right(self):
|
2023-04-27 22:57:23 +00:00
|
|
|
if self.work_pixbuf and self.pixbuf:
|
2023-04-25 21:12:30 +00:00
|
|
|
self.work_pixbuf = self.work_pixbuf.rotate_simple(GdkPixbuf.PixbufRotation.CLOCKWISE)
|
|
|
|
self.pixbuf = self.pixbuf.rotate_simple(GdkPixbuf.PixbufRotation.CLOCKWISE)
|
|
|
|
self.set_from_pixbuf(self.work_pixbuf)
|
|
|
|
|
|
|
|
def _zoom_in(self):
|
2023-04-27 22:57:23 +00:00
|
|
|
if self.work_pixbuf and self.pixbuf:
|
2023-04-25 21:12:30 +00:00
|
|
|
# TODO: Setup scale factor setting to pull from settings...
|
|
|
|
stepx = self.work_pixbuf.get_width() * 0.05
|
|
|
|
stepy = self.work_pixbuf.get_height() * 0.05
|
|
|
|
|
|
|
|
w = self.work_pixbuf.get_width() + stepx
|
|
|
|
h = self.work_pixbuf.get_height() + stepy
|
|
|
|
|
|
|
|
self.work_pixbuf = self.pixbuf.scale_simple(w, h, 2) # 2 = BILINEAR and is best by default
|
|
|
|
self.set_from_pixbuf(self.work_pixbuf)
|
2023-04-27 00:26:19 +00:00
|
|
|
|
|
|
|
def _size_allocate(self):
|
|
|
|
if self.fit_to_win:
|
|
|
|
self._fit_to_container()
|