Mirage2/src/controller/Controller.py

141 lines
4.9 KiB
Python

# Python imports
import os
import threading, subprocess, time
# Gtk imports
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GLib, GdkPixbuf
# Application imports
from .mixins import *
from . import Controller_Data
def threaded(fn):
def wrapper(*args, **kwargs):
threading.Thread(target=fn, args=args, kwargs=kwargs).start()
return wrapper
class Controller(TreeViewUpdateMixin, Controller_Data):
def __init__(self, _settings, args, unknownargs):
self.setup_controller_data(_settings)
self.window.show_all()
self.handle_args(args, unknownargs)
def tear_down(self, widget=None, eve=None):
event_system.send_ipc_message("close server")
time.sleep(event_sleep_time)
Gtk.main_quit()
@threaded
def gui_event_observer(self):
while True:
time.sleep(event_sleep_time)
event = event_system.consume_gui_event()
if event:
try:
type, target, data = event
if not type:
method = getattr(self.__class__, target)
GLib.idle_add(method, *(self, *data,))
else:
method = getattr(self.__class__, "hadle_gui_event_and_call_back")
GLib.idle_add(method, *(self, type, target, data))
except Exception as e:
print(repr(e))
def hadle_gui_event_and_call_back(self, type, target, parameters):
method = getattr(self.__class__, target)
data = method(*(self, *parameters))
event_system.push_module_event([type, None, (data,)])
def handle_args(self, args=None, unknownargs=None):
if args.dir and os.path.isdir(args.dir):
self.load_store(self.view, self.thumbnail_store, arg)
if args.file and os.path.isfile(args.file):
path = "/" + '/'.join(rgs.file.split("/")[:-1])
self.load_store(self.view, self.thumbnail_store, path)
image = Gtk.Image.new_from_pixbuf(self._get_pixbuf(args.file))
self._load_image(image)
if unknownargs:
for arg in unknownargs:
if os.path.isdir(arg):
self.load_store(self.view, self.thumbnail_store, arg)
elif os.path.isfile(arg):
path = "/" + '/'.join(arg.split("/")[:-1])
self.load_store(self.view, self.thumbnail_store, path)
image = Gtk.Image.new_from_pixbuf(self._get_pixbuf(arg))
self._load_image(image)
def _on_drag_data_received(self, widget, drag_context, x, y, data, info, time):
if info == 80:
uri = data.get_uris()[0].split("file://")[1]
if os.path.isfile(uri) and uri.endswith(self.images_filter):
try:
image = Gtk.Image.new_from_pixbuf(self._get_pixbuf(uri))
except Exception as e:
image = Gtk.Image.new_from_pixbuf(self._get_pixbuf(self.blank_image))
self._load_image(image)
elif os.path.isdir(uri):
self.load_store(self.view, self.thumbnail_store, uri)
def load_image_from_treeview(self, widget):
store, iter = widget.get_selection().get_selected()
uri = store.get_value(iter, 1)
image = Gtk.Image.new_from_pixbuf(self._get_pixbuf(uri))
self._load_image(image)
def _get_pixbuf(self, uri):
self.current_path_label.set_label(uri)
self.current_img = uri
geom_rec = self.image_area.get_parent().get_parent().get_allocated_size()[0]
width = geom_rec.width - 15
height = geom_rec.height - 15
self.image_area.set_size_request(width, height)
return GdkPixbuf.Pixbuf.new_from_file_at_scale(uri, width, height, True)
def _load_image(self, img):
self.clear_children(self.image_area)
self.image_area.add(img)
self.image_area.show_all()
@threaded
def scale_image_from_parent_resize(self, widget, allocation):
if not self.image_update_lock:
GLib.idle_add(self._on_scale_image_from_parent_resize, ())
def _on_scale_image_from_parent_resize(self, eve):
if self.current_img:
self.image_update_lock = True
image = Gtk.Image.new_from_pixbuf(self._get_pixbuf(self.current_img))
self._load_image(image)
self.image_update_lock = False
def get_clipboard_data(self):
proc = subprocess.Popen(['xclip','-selection', 'clipboard', '-o'], stdout=subprocess.PIPE)
retcode = proc.wait()
data = proc.stdout.read()
return data.decode("utf-8").strip()
def set_clipboard_data(self, data):
proc = subprocess.Popen(['xclip','-selection','clipboard'], stdin=subprocess.PIPE)
proc.stdin.write(data)
proc.stdin.close()
retcode = proc.wait()