Mirage2/src/new-src/core/containers/image_view_scroll.py

110 lines
3.0 KiB
Python

# Python imports
import os
# Lib imports
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Gdk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
# Application imports
from ..widgets.image_view import ImageView
class ImageViewScroll(Gtk.ScrolledWindow):
def __init__(self):
super(ImageViewScroll, self).__init__()
self.fimages = settings.get_images_filter()
self.curent_dir = None
self._setup_styling()
self._setup_signals()
self._subscribe_to_events()
self._load_widgets()
self.show_all()
def _setup_styling(self):
ctx = self.get_style_context()
ctx.add_class("image-view")
self.set_vexpand(True)
self.set_hexpand(True)
def _setup_signals(self):
self._set_up_dnd()
self.connect("size-allocate", self._size_request_change)
def _load_widgets(self):
self.add(ImageView())
def _subscribe_to_events(self):
event_system.subscribe("do_filter_open", self._do_filter_open)
def _set_up_dnd(self):
flags = Gtk.DestDefaults.ALL
URI_TARGET_TYPE = 80
uri_target = Gtk.TargetEntry.new('text/uri-list', Gtk.TargetFlags(0), URI_TARGET_TYPE)
targets = [ uri_target ]
action = Gdk.DragAction.COPY
self.drag_dest_set(flags, targets, action)
self.connect("drag-data-received", self._on_drag_data_received)
def _on_drag_data_received(self, widget, drag_context, _x, _y, data, info, time):
if info == 80:
uris = data.get_uris()
if len(uris) == 0:
uris = data.get_text().split("\n")
self._do_filter_open(uris)
def _do_filter_open(self, uris: [] = []):
if len(uris) == 0:
return
has_loaded_image, path, img_list = self.filter_for_images(uris)
self._handle_open(has_loaded_image, path, img_list)
def filter_for_images(self, files):
path = files[0].replace("file://", "")
img_list = []
listedDir = False
has_loaded_image = False
if not os.path.isdir(path):
event_system.emit("handle_file_from_dnd", (path,))
path = os.path.dirname(path)
has_loaded_image = True
if not self.curent_dir or not self.curent_dir == path:
files = os.listdir(path)
self.curent_dir = path
else:
files = []
for file in files:
if file.endswith(self.fimages):
img_list.append(file)
return has_loaded_image, path, img_list
def _handle_open(self, has_loaded_image, path, img_list):
if len(img_list) == 0:
return
if not has_loaded_image:
img = img_list[0]
target = os.path.join(path, img)
event_system.emit("handle_file_from_dnd", target)
event_system.emit("load_image_list", (path, img_list))
def _size_request_change(self, widget = None, eve = None):
event_system.emit("size_allocate")