Mirage2/src/core/widgets/image_view_mixin.py

131 lines
4.4 KiB
Python

# Python imports
# Lib imports
import gi
gi.require_version('GdkPixbuf', '2.0')
from gi.repository import GdkPixbuf
# Application imports
class ImageViewMixin:
def _get_active_image_path(self):
if self.pixbuff and self.pixbuff.path:
return self.pixbuff.path
return None
def _zoom_out(self):
if self.work_pixbuff and self.pixbuff:
# TODO: Setup scale factor setting to pull from settings...
stepx = self.work_pixbuff.get_width() * 0.05
stepy = self.work_pixbuff.get_height() * 0.05
w = self.work_pixbuff.get_width() - stepx
h = self.work_pixbuff.get_height() - stepy
self.work_pixbuff = self.pixbuff.scale_simple(w, h, 2) # 2 = BILINEAR and is best by default
self.set_from_pixbuf(self.work_pixbuff)
def _rotate_left(self):
if self.work_pixbuff and self.pixbuff:
self.work_pixbuff = self.work_pixbuff.rotate_simple(GdkPixbuf.PixbufRotation.COUNTERCLOCKWISE)
self.pixbuff = self.pixbuff.rotate_simple(GdkPixbuf.PixbufRotation.COUNTERCLOCKWISE)
self.set_from_pixbuf(self.work_pixbuff)
def _vertical_flip(self):
if self.work_pixbuff and self.pixbuff:
self.work_pixbuff = self.work_pixbuff.flip(True)
self.pixbuff = self.pixbuff.flip(True)
self.set_from_pixbuf(self.work_pixbuff)
def _scale_1_two_1(self):
self.fit_to_win = False
if self.work_pixbuff and self.pixbuff:
self.work_pixbuff = self.pixbuff
self.set_from_pixbuf(self.work_pixbuff)
def _fit_to_container(self, pixbuff = None):
self.fit_to_win = True
if (self.work_pixbuff and self.pixbuff) or pixbuff:
parent_aloc = self.get_parent().get_parent().get_allocation()
pw = parent_aloc.width
ph = parent_aloc.height
iw = None
ih = None
w = 0
h = 0
if pixbuff:
iw = pixbuff.get_width()
ih = pixbuff.get_height()
else:
iw = self.pixbuff.get_width()
ih = self.pixbuff.get_height()
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
if not pixbuff in (None, ""):
return pixbuff.scale_simple(w, h, 2)
self.work_pixbuff = self.pixbuff.scale_simple(w, h, 2) # 2 = BILINEAR and is best by default
self.set_from_pixbuf(self.work_pixbuff)
def _horizontal_flip(self):
if self.work_pixbuff and self.pixbuff:
self.work_pixbuff = self.work_pixbuff.flip(False)
self.pixbuff = self.pixbuff.flip(False)
self.set_from_pixbuf(self.work_pixbuff)
def _rotate_right(self):
if self.work_pixbuff and self.pixbuff:
self.work_pixbuff = self.work_pixbuff.rotate_simple(GdkPixbuf.PixbufRotation.CLOCKWISE)
self.pixbuff = self.pixbuff.rotate_simple(GdkPixbuf.PixbufRotation.CLOCKWISE)
self.set_from_pixbuf(self.work_pixbuff)
def _zoom_in(self):
if self.work_pixbuff and self.pixbuff:
# TODO: Setup scale factor setting to pull from settings...
stepx = self.work_pixbuff.get_width() * 0.05
stepy = self.work_pixbuff.get_height() * 0.05
w = self.work_pixbuff.get_width() + stepx
h = self.work_pixbuff.get_height() + stepy
self.work_pixbuff = self.pixbuff.scale_simple(w, h, 2) # 2 = BILINEAR and is best by default
self.set_from_pixbuf(self.work_pixbuff)
def _size_allocate(self):
if self.fit_to_win:
self._fit_to_container()
@daemon_threaded
def _play_animation(self):
import time
from gi.repository import GLib
self.playing_animation = True
# NOTE: Divide by 1000 b/c time.sleep takes seconds but delay is in milliseconds
delay = self.animation.get_delay_time() / 1000
while self.playing_animation:
self.animation.advance()
pixbuff = self.animation.get_pixbuf()
pixbuff = pixbuff if not self.fit_to_win else self._fit_to_container(pixbuff)
GLib.idle_add(self.set_from_pixbuf, *(pixbuff,))
time.sleep(delay)
def _stop_animation(self):
self.playing_animation = False