104 lines
2.8 KiB
Python
104 lines
2.8 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._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()
|
|
|
|
def _load_widgets(self):
|
|
viewport = Gtk.Viewport()
|
|
viewport.add(ImageView())
|
|
self.add(viewport)
|
|
|
|
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: [] = []):
|
|
path, img_list = self.filter_for_images(uris)
|
|
self._handle_open(path, img_list)
|
|
|
|
def filter_for_images(self, files):
|
|
path = files[0].replace("file://", "")
|
|
img_list = []
|
|
listedDir = False
|
|
|
|
if len(files) == 1:
|
|
if os.path.isdir(path):
|
|
listedDir = True
|
|
files = os.listdir(path)
|
|
|
|
if not os.path.isdir(path):
|
|
path = os.path.dirname(path)
|
|
|
|
for file in files:
|
|
if file.endswith(self.fimages):
|
|
if listedDir:
|
|
img_list.append(file)
|
|
else:
|
|
img_list.append(file.replace("file://", "").replace(f"{path}/", ""))
|
|
|
|
return path, img_list
|
|
|
|
def _handle_open(self,path, img_list):
|
|
if len(img_list) == 0:
|
|
return
|
|
|
|
if len(img_list) == 1:
|
|
img = img_list[0]
|
|
target = os.path.join(path, img)
|
|
event_system.emit("handle_file_from_dnd", target)
|
|
return
|
|
|
|
event_system.emit("load_image_list", (path, img_list))
|