generated from itdominator/Python-With-Gtk-Template
176 lines
4.5 KiB
Python
176 lines
4.5 KiB
Python
# Python imports
|
|
|
|
# Lib imports
|
|
import gi
|
|
gi.require_version('Gtk', '3.0')
|
|
from gi.repository import Gtk
|
|
from gi.repository import Gio
|
|
|
|
# Application imports
|
|
from utils.widget_save_load_controller import WidgetSaveLoadController
|
|
|
|
|
|
|
|
class FileWidgetException(Exception):
|
|
...
|
|
|
|
|
|
class FileChooser(Gtk.Dialog):
|
|
"""docstring for FileChooser."""
|
|
|
|
def __init__(self):
|
|
super(FileChooser, self).__init__()
|
|
|
|
self._file_chooser_widget = None
|
|
|
|
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):
|
|
...
|
|
|
|
def _load_widgets(self):
|
|
hbox = self.get_content_area()
|
|
self._file_chooser_widget = Gtk.FileChooserWidget()
|
|
self._file_chooser_widget.set_select_multiple(True)
|
|
|
|
hbox.add(self._file_chooser_widget)
|
|
self.add_button("Cancel", 1)
|
|
self.add_button("Select", 0)
|
|
|
|
def get_file_chooser_widget(self):
|
|
return self._file_chooser_widget
|
|
|
|
|
|
class FileWidget(WidgetSaveLoadController, Gtk.Box):
|
|
def __init__(self):
|
|
super(FileWidget, self).__init__()
|
|
|
|
self._file_path = None
|
|
self._file_name = "No File Selected..."
|
|
self.label = None
|
|
|
|
self._setup_styling()
|
|
self._setup_signals()
|
|
self._subscribe_to_events()
|
|
self._load_widgets()
|
|
self.set_file_path()
|
|
|
|
event_system.emit("register_to_query_controller", (self, self.get_query_data))
|
|
self.show_all()
|
|
|
|
|
|
def new(self):
|
|
widget = FileWidget()
|
|
return widget
|
|
|
|
def _setup_styling(self):
|
|
self.set_orientation(1)
|
|
|
|
def _setup_signals(self):
|
|
self.connect("key-release-event", self._key_released)
|
|
|
|
def _subscribe_to_events(self):
|
|
...
|
|
|
|
def _load_widgets(self):
|
|
box = Gtk.Box()
|
|
eve_box = Gtk.EventBox()
|
|
image = Gtk.Image(stock = Gtk.STOCK_FILE)
|
|
self.label = Gtk.Label(self._file_name)
|
|
|
|
box.set_orientation(1)
|
|
eve_box.connect('button-press-event', self._clicked)
|
|
|
|
box.add(image)
|
|
box.add(self.label)
|
|
eve_box.add(box)
|
|
self.add(eve_box)
|
|
eve_box.show_all()
|
|
|
|
|
|
def _key_released(self, widget = None, eve = None):
|
|
if eve.type == 9:
|
|
# if enter or spacebar:
|
|
# pass
|
|
...
|
|
|
|
def _clicked(self, widget = None, eve = None):
|
|
if eve.button == 1 and eve.type == 5: # NOTE: Left dbl click
|
|
event_system.emit("open_files", ( [self.get_file_path()], ) )
|
|
return
|
|
|
|
if eve.button == 3 and eve.type == 4: # NOTE: Right click
|
|
try:
|
|
self.set_current_file()
|
|
self.get_parent().save_needed = True
|
|
except FileWidgetException as e:
|
|
logger.debug(e)
|
|
|
|
return
|
|
|
|
def get_query_data(self):
|
|
return self.get_file_name()
|
|
|
|
def get_saveable_data(self):
|
|
self.save_collection["data"] = self.get_file_path()
|
|
self.save_collection["widget_type"] = str(type(self).__name__)
|
|
return self.save_collection
|
|
|
|
def set_saveable_data(self, data):
|
|
self.save_collection = data
|
|
self.load_saveable_data()
|
|
|
|
def load_saveable_data(self):
|
|
self.set_file_path( self.save_collection["data"] )
|
|
|
|
|
|
def set_file_path(self, path = settings.get_home_path()):
|
|
try:
|
|
self._file_path = Gio.File.new_for_uri(path)
|
|
if not self._file_path.get_path():
|
|
raise FileWidgetException("Not URI based path...")
|
|
except FileWidgetException as e:
|
|
self._file_path = Gio.File.new_for_path(path)
|
|
|
|
self._file_name = self.get_file_name()
|
|
self.label.set_label(self._file_name)
|
|
|
|
def get_file(self):
|
|
return self._file_path
|
|
|
|
def get_file_path(self):
|
|
path = self._file_path.get_path()
|
|
if not path:
|
|
path = self._file_path.get_uri()
|
|
|
|
return path
|
|
|
|
def get_file_name(self):
|
|
info = self._file_path.query_info("standard::*", 0, cancellable = None)
|
|
return info.get_display_name()
|
|
|
|
def set_current_file(self):
|
|
dlg = FileChooser()
|
|
response = dlg.run()
|
|
if response == 0:
|
|
widget = dlg.get_file_chooser_widget()
|
|
uris = widget.get_uris()
|
|
if len(uris) == 1:
|
|
uri = uris[0]
|
|
|
|
self.set_file_path(uri)
|
|
|
|
dlg.destroy()
|