Refactor plus fixed thumbnail generation crashing stuff

This commit is contained in:
itdominator 2021-11-16 21:41:24 -06:00
parent 00c207a153
commit 3825892405
7 changed files with 205 additions and 203 deletions

View File

@ -22,6 +22,7 @@ class View(Settings, FileHandler, Launcher, Icon, Path):
self.id_length = 10 self.id_length = 10
self.id = "" self.id = ""
self.hide_hidden = self.HIDE_HIDDEN_FILES
self.files = [] self.files = []
self.dirs = [] self.dirs = []
self.vids = [] self.vids = []
@ -59,7 +60,7 @@ class View(Settings, FileHandler, Launcher, Icon, Path):
for f in listdir(path): for f in listdir(path):
file = join(path, f) file = join(path, f)
if self.HIDE_HIDDEN_FILES: if self.hide_hidden:
if f.startswith('.'): if f.startswith('.'):
continue continue

View File

@ -6,7 +6,8 @@ from os.path import isfile
import gi import gi
gi.require_version('Gtk', '3.0') gi.require_version('Gtk', '3.0')
from gi.repository import Gtk # from gi.repository import Gtk
from gi.repository import GdkPixbuf
# Application imports # Application imports
from .mixins import * from .mixins import *
@ -34,17 +35,10 @@ class Icon(DesktopIconMixin, VideoIconMixin):
thumbnl = self.create_scaled_image(full_path, self.VIDEO_ICON_WH) thumbnl = self.create_scaled_image(full_path, self.VIDEO_ICON_WH)
elif full_path.lower().endswith( ('.desktop',) ): # .desktop file parsing elif full_path.lower().endswith( ('.desktop',) ): # .desktop file parsing
thumbnl = self.parse_desktop_files(full_path) thumbnl = self.parse_desktop_files(full_path)
else: # System icons
thumbnl = self.get_system_thumbnail(full_path, self.SYS_ICON_WH[0])
if thumbnl == None: # If no icon whatsoever, return internal default
thumbnl = Gtk.Image.new_from_file(self.DEFAULT_ICON)
return thumbnl return thumbnl
except Exception as e: except Exception as e:
print("Icon generation issue:") return None
print( repr(e) )
return Gtk.Image.new_from_file(self.DEFAULT_ICON)
def create_thumbnail(self, dir, file): def create_thumbnail(self, dir, file):
full_path = dir + "/" + file full_path = dir + "/" + file
@ -56,20 +50,20 @@ class Icon(DesktopIconMixin, VideoIconMixin):
thumbnl = self.create_scaled_image(hash_img_pth, self.VIDEO_ICON_WH) thumbnl = self.create_scaled_image(hash_img_pth, self.VIDEO_ICON_WH)
if thumbnl == None: # If no icon whatsoever, return internal default if thumbnl == None: # If no icon whatsoever, return internal default
thumbnl = Gtk.Image.new_from_file(self.DEFAULT_ICONS + "/video.png") thumbnl = GdkPixbuf.Pixbuf.new_from_file(self.DEFAULT_ICONS + "/video.png")
return thumbnl return thumbnl
except Exception as e: except Exception as e:
print("Thumbnail generation issue:") print("Thumbnail generation issue:")
print( repr(e) ) print( repr(e) )
return Gtk.Image.new_from_file(self.DEFAULT_ICONS + "/video.png") return GdkPixbuf.Pixbuf.new_from_file(self.DEFAULT_ICONS + "/video.png")
def create_scaled_image(self, path, wxh): def create_scaled_image(self, path, wxh):
try: try:
pixbuf = Gtk.Image.new_from_file(path).get_pixbuf() pixbuf = GdkPixbuf.Pixbuf.new_from_file(path)#.get_pixbuf()
scaled_pixbuf = pixbuf.scale_simple(wxh[0], wxh[1], 2) # 2 = BILINEAR and is best by default scaled_pixbuf = pixbuf.scale_simple(wxh[0], wxh[1], 2) # 2 = BILINEAR and is best by default
return Gtk.Image.new_from_pixbuf(scaled_pixbuf) return scaled_pixbuf # Gtk.Image.new_from_pixbuf(scaled_pixbuf)
except Exception as e: except Exception as e:
print("Image Scaling Issue:") print("Image Scaling Issue:")
print( repr(e) ) print( repr(e) )
@ -77,11 +71,11 @@ class Icon(DesktopIconMixin, VideoIconMixin):
def create_from_file(self, path): def create_from_file(self, path):
try: try:
return Gtk.Image.new_from_file(path) return GdkPixbuf.Pixbuf.new_from_file(path)
except Exception as e: except Exception as e:
print("Image from file Issue:") print("Image from file Issue:")
print( repr(e) ) print( repr(e) )
return None return None
def return_generic_icon(self): def return_generic_icon(self):
return Gtk.Image.new_from_file(self.DEFAULT_ICON) return GdkPixbuf.Pixbuf.new_from_file(self.DEFAULT_ICON)

View File

@ -5,8 +5,6 @@ from os.path import isfile
# Gtk imports # Gtk imports
import gi import gi
gi.require_version('Gtk', '3.0') gi.require_version('Gtk', '3.0')
from gi.repository import Gio
from gi.repository import Gtk from gi.repository import Gtk
# Application imports # Application imports
@ -14,26 +12,6 @@ from .xdg.DesktopEntry import DesktopEntry
class DesktopIconMixin: class DesktopIconMixin:
def get_system_thumbnail(self, filename, size):
try:
if os.path.exists(filename):
gioFile = Gio.File.new_for_path(filename)
info = gioFile.query_info('standard::icon' , 0, Gio.Cancellable())
icon = info.get_icon().get_names()[0]
iconTheme = Gtk.IconTheme.get_default()
iconData = iconTheme.lookup_icon(icon , size , 0)
if iconData:
iconPath = iconData.get_filename()
return Gtk.Image.new_from_file(iconPath)
else:
return None
else:
return None
except Exception as e:
print("system icon generation issue:")
print( repr(e) )
return None
def parse_desktop_files(self, full_path): def parse_desktop_files(self, full_path):
try: try:
xdgObj = DesktopEntry(full_path) xdgObj = DesktopEntry(full_path)

View File

@ -40,7 +40,6 @@ class Signals(WindowMixin, PaneMixin):
self.is_pane2_hidden = False self.is_pane2_hidden = False
self.is_pane3_hidden = False self.is_pane3_hidden = False
self.is_pane4_hidden = False self.is_pane4_hidden = False
self.refresh_lock = False
self.window.show() self.window.show()
self.generate_windows(self.state) self.generate_windows(self.state)

View File

@ -1,11 +1,6 @@
# Python imports # Python imports
import copy
from os.path import isdir, isfile
# Lib imports # Lib imports
import gi
from gi.repository import Gdk
# Application imports # Application imports
from . import WidgetMixin from . import WidgetMixin
@ -14,9 +9,6 @@ from . import WidgetMixin
class TabMixin(WidgetMixin): class TabMixin(WidgetMixin):
"""docstring for TabMixin""" """docstring for TabMixin"""
def get_fm_window(self, wid):
return self.window_controller.get_window_by_nickname(f"window_{wid}")
def create_tab(self, wid, path=None): def create_tab(self, wid, path=None):
notebook = self.builder.get_object(f"window_{wid}") notebook = self.builder.get_object(f"window_{wid}")
path_entry = self.builder.get_object(f"path_entry") path_entry = self.builder.get_object(f"path_entry")
@ -50,127 +42,19 @@ class TabMixin(WidgetMixin):
self.window_controller.save_state() self.window_controller.save_state()
self.set_window_title() self.set_window_title()
def set_window_title(self): def on_tab_switch_update(self, notebook, content=None, index=None):
wid, tid = self.window_controller.get_active_data() wid, tid = content.get_children()[0].get_name().split("|")
view = self.get_fm_window(wid).get_view_by_id(tid)
dir = view.get_current_directory()
self.window.set_title(dir)
def grid_icon_single_click(self, widget, eve):
try:
wid, tid = widget.get_name().split("|")
self.window_controller.set_active_data(wid, tid) self.window_controller.set_active_data(wid, tid)
if eve.type == Gdk.EventType.BUTTON_RELEASE and eve.button == 1: # l-click
self.set_path_text(wid, tid) self.set_path_text(wid, tid)
self.set_window_title() self.set_window_title()
if self.single_click_open: # FIXME: need to find a way to pass the model index def get_tab_id_from_widget(self, tab_box):
self.icon_double_left_click(widget) tid = tab_box.get_children()[2]
elif eve.type == Gdk.EventType.BUTTON_RELEASE and eve.button == 3: # r-click return tid.get_text()
pass
# input = self.builder.get_object("filenameInput")
# controls = self.builder.get_object("iconControlsWindow")
# iconsButtonBox = self.builder.get_object("iconsButtonBox")
# menuButtonBox = self.builder.get_object("menuButtonBox")
#
#
# if len(self.selectedFiles) == 1:
# parts = self.selectedFiles[0].split("/")
# input.set_text(parts[len(parts) - 1])
# input.show()
# iconsButtonBox.show()
# menuButtonBox.hide()
# controls.show()
# elif len(self.selectedFiles) > 1:
# input.set_text("")
# input.hide()
# menuButtonBox.hide()
# iconsButtonBox.show()
# controls.show()
# else:
# input.set_text("")
# input.show()
# menuButtonBox.show()
# iconsButtonBox.hide()
# controls.show()
except Exception as e: def get_tab_label_widget_from_widget(self, notebook, widget):
print(repr(e)) return notebook.get_tab_label(widget.get_parent()).get_children()[0]
def grid_icon_double_left_click(self, widget, item):
try:
wid, tid = self.window_controller.get_active_data()
notebook = self.builder.get_object(f"window_{wid}")
path_entry = self.builder.get_object(f"path_entry")
tab_label = self.get_tab_label_widget_from_widget(notebook, widget)
view = self.get_fm_window(wid).get_view_by_id(tid)
model = widget.get_model()
fileName = model[item][1]
dir = view.get_current_directory()
file = dir + "/" + fileName
refresh = True
if isdir(file):
view.set_path(file)
elif isfile(file):
refresh = False
view.open_file_locally(file)
if refresh == True:
self.load_store(view, model)
tab_label.set_label(view.get_end_of_path())
path_entry.set_text(view.get_current_directory())
except Exception as e:
print(repr(e))
def grid_on_drag_set(self, widget, drag_context, data, info, time):
action = widget.get_name()
store = widget.get_model()
treePaths = widget.get_selected_items()
wid, tid = action.split("|")
view = self.get_fm_window(wid).get_view_by_id(tid)
dir = view.get_current_directory()
uris = []
for path in treePaths:
itr = store.get_iter(path)
file = store.get(itr, 1)[0]
fpath = f"file://{dir}/{file}"
uris.append(fpath)
data.set_uris(uris)
event_system.push_gui_event(["refresh_tab", None, action])
def grid_on_drag_motion(self, widget, drag_context, x, y, data):
wid, tid = widget.get_name().split("|")
self.window_controller.set_active_data(wid, tid)
def grid_on_drag_data_received(self, widget, drag_context, x, y, data, info, time):
if info == 80:
wid, tid = self.window_controller.get_active_data()
notebook = self.builder.get_object(f"window_{wid}")
icon_view, tab_label = self.get_icon_view_and_label_from_notebook(notebook, f"{wid}|{tid}")
view = self.get_fm_window(wid).get_view_by_id(tid)
store = icon_view.get_model()
uris = data.get_uris()
dest = view.get_current_directory()
print(f"{wid}|{tid}")
if len(uris) > 0:
print(f"Target Move Path: {dest}")
self.refresh_lock = True
for uri in uris:
print(f"URI: {uri}")
self.move_file(view, uri, dest)
# Reloads new directory
view.load_directory()
self.load_store(view, store)
self.refresh_lock = False
def do_action_from_bar_controls(self, widget, eve=None): def do_action_from_bar_controls(self, widget, eve=None):
action = widget.get_name() action = widget.get_name()

View File

@ -1,5 +1,5 @@
# Python imports # Python imports
import threading, subprocess import os, threading, subprocess
# Lib imports # Lib imports
import gi import gi
@ -9,6 +9,7 @@ gi.require_version('Gdk', '3.0')
from gi.repository import Gtk from gi.repository import Gtk
from gi.repository import Gdk from gi.repository import Gdk
from gi.repository import GLib from gi.repository import GLib
from gi.repository import Gio
from gi.repository import GdkPixbuf from gi.repository import GdkPixbuf
# Application imports # Application imports
@ -27,30 +28,55 @@ class WidgetMixin:
store.clear() store.clear()
dir = view.get_current_directory() dir = view.get_current_directory()
files = view.get_files() files = view.get_files()
icon = GdkPixbuf.Pixbuf.new_from_file(view.DEFAULT_ICON)
for i, file in enumerate(files): for i, file in enumerate(files):
generic_icon = Gtk.Image.new_from_file(view.DEFAULT_ICON).get_pixbuf() store.append([icon, file[0]])
store.append([generic_icon, file[0]])
self.create_icon(i, view, store, dir, file[0]) self.create_icon(i, view, store, dir, file[0])
# Not likely called often here but could be useful # NOTE: Not likely called often from here but it could be useful
if save_state: if save_state:
self.window_controller.save_state() self.window_controller.save_state()
@threaded @threaded
def create_icon(self, i, view, store, dir, file): def create_icon(self, i, view, store, dir, file):
icon = None icon = view.create_icon(dir, file)
try: fpath = dir + "/" + file
icon = view.create_icon(dir, file).get_pixbuf() GLib.idle_add(self.update_store, (i, store, icon, view, fpath,))
except Exception as e:
return
GLib.idle_add(self.update_store, (i, store, icon,))
def update_store(self, item): def update_store(self, item):
i, store, icon = item i, store, icon, view, fpath = item
itr = store.get_iter(i) itr = store.get_iter(i)
if not icon:
icon = self.get_system_thumbnail(fpath, view.SYS_ICON_WH[0])
if not icon:
icon = GdkPixbuf.Pixbuf.new_from_file(view.DEFAULT_ICON)
store.set_value(itr, 0, icon) store.set_value(itr, 0, icon)
def get_system_thumbnail(self, filename, size):
try:
if os.path.exists(filename):
gioFile = Gio.File.new_for_path(filename)
info = gioFile.query_info('standard::icon' , 0, Gio.Cancellable())
icon = info.get_icon().get_names()[0]
iconTheme = Gtk.IconTheme.get_default()
iconData = iconTheme.lookup_icon(icon , size , 0)
if iconData:
iconPath = iconData.get_filename()
return GdkPixbuf.Pixbuf.new_from_file(iconPath)
else:
return None
else:
return None
except Exception as e:
print("System icon generation issue:")
print( repr(e) )
return None
def create_tab_widget(self, view): def create_tab_widget(self, view):
@ -148,26 +174,6 @@ class WidgetMixin:
return scroll, store return scroll, store
def on_tab_switch_update(self, notebook, content=None, index=None):
wid, tid = content.get_children()[0].get_name().split("|")
self.window_controller.set_active_data(wid, tid)
self.set_path_text(wid, tid)
self.set_window_title()
def set_path_text(self, wid, tid):
path_entry = self.builder.get_object("path_entry")
view = self.get_fm_window(wid).get_view_by_id(tid)
path_entry.set_text(view.get_current_directory())
def get_tab_id_from_widget(self, tab_box):
tid = tab_box.get_children()[2]
return tid.get_text()
def get_tab_label_widget_from_widget(self, notebook, widget):
return notebook.get_tab_label(widget.get_parent()).get_children()[0]
def get_icon_view_and_label_from_notebook(self, notebook, _name): def get_icon_view_and_label_from_notebook(self, notebook, _name):
icon_view = None icon_view = None
tab_label = None tab_label = None

View File

@ -1,8 +1,148 @@
# Python imports
import copy
from os.path import isdir, isfile
# Lib imports
from . import TabMixin from . import TabMixin
import gi
from gi.repository import Gdk
# Application imports
from . import WidgetMixin
class WindowMixin(TabMixin): class WindowMixin(TabMixin):
"""docstring for WindowMixin""" """docstring for WindowMixin"""
def get_fm_window(self, wid):
return self.window_controller.get_window_by_nickname(f"window_{wid}")
def set_window_title(self):
wid, tid = self.window_controller.get_active_data()
view = self.get_fm_window(wid).get_view_by_id(tid)
dir = view.get_current_directory()
self.window.set_title(dir)
def set_path_text(self, wid, tid):
path_entry = self.builder.get_object("path_entry")
view = self.get_fm_window(wid).get_view_by_id(tid)
path_entry.set_text(view.get_current_directory())
def grid_icon_single_click(self, widget, eve):
try:
wid, tid = widget.get_name().split("|")
self.window_controller.set_active_data(wid, tid)
if eve.type == Gdk.EventType.BUTTON_RELEASE and eve.button == 1: # l-click
self.set_path_text(wid, tid)
self.set_window_title()
if self.single_click_open: # FIXME: need to find a way to pass the model index
self.grid_icon_double_left_click(widget)
elif eve.type == Gdk.EventType.BUTTON_RELEASE and eve.button == 3: # r-click
pass
# input = self.builder.get_object("filenameInput")
# controls = self.builder.get_object("iconControlsWindow")
# iconsButtonBox = self.builder.get_object("iconsButtonBox")
# menuButtonBox = self.builder.get_object("menuButtonBox")
#
#
# if len(self.selectedFiles) == 1:
# parts = self.selectedFiles[0].split("/")
# input.set_text(parts[len(parts) - 1])
# input.show()
# iconsButtonBox.show()
# menuButtonBox.hide()
# controls.show()
# elif len(self.selectedFiles) > 1:
# input.set_text("")
# input.hide()
# menuButtonBox.hide()
# iconsButtonBox.show()
# controls.show()
# else:
# input.set_text("")
# input.show()
# menuButtonBox.show()
# iconsButtonBox.hide()
# controls.show()
except Exception as e:
print(repr(e))
def grid_icon_double_left_click(self, widget, item):
try:
wid, tid = self.window_controller.get_active_data()
notebook = self.builder.get_object(f"window_{wid}")
path_entry = self.builder.get_object(f"path_entry")
tab_label = self.get_tab_label_widget_from_widget(notebook, widget)
view = self.get_fm_window(wid).get_view_by_id(tid)
model = widget.get_model()
fileName = model[item][1]
dir = view.get_current_directory()
file = dir + "/" + fileName
refresh = True
if isdir(file):
view.set_path(file)
elif isfile(file):
refresh = False
view.open_file_locally(file)
if refresh == True:
self.load_store(view, model)
tab_label.set_label(view.get_end_of_path())
path_entry.set_text(view.get_current_directory())
except Exception as e:
print(repr(e))
def grid_on_drag_set(self, widget, drag_context, data, info, time):
action = widget.get_name()
store = widget.get_model()
treePaths = widget.get_selected_items()
wid, tid = action.split("|")
view = self.get_fm_window(wid).get_view_by_id(tid)
dir = view.get_current_directory()
uris = []
for path in treePaths:
itr = store.get_iter(path)
file = store.get(itr, 1)[0]
fpath = f"file://{dir}/{file}"
uris.append(fpath)
data.set_uris(uris)
event_system.push_gui_event(["refresh_tab", None, action])
def grid_on_drag_motion(self, widget, drag_context, x, y, data):
wid, tid = widget.get_name().split("|")
self.window_controller.set_active_data(wid, tid)
def grid_on_drag_data_received(self, widget, drag_context, x, y, data, info, time):
if info == 80:
wid, tid = self.window_controller.get_active_data()
notebook = self.builder.get_object(f"window_{wid}")
icon_view, tab_label = self.get_icon_view_and_label_from_notebook(notebook, f"{wid}|{tid}")
view = self.get_fm_window(wid).get_view_by_id(tid)
store = icon_view.get_model()
uris = data.get_uris()
dest = view.get_current_directory()
if len(uris) > 0:
print(f"Target Move Path: {dest}")
for uri in uris:
print(f"URI: {uri}")
self.move_file(view, uri, dest)
# Reloads new directory
view.load_directory()
self.load_store(view, store)
def create_new_view_notebook(self, widget=None, wid=None, path=None): def create_new_view_notebook(self, widget=None, wid=None, path=None):
self.create_tab(wid, path) self.create_tab(wid, path)