Pytop/src/Pytop/widgets/Grid.py

210 lines
7.0 KiB
Python
Raw Normal View History

2020-05-09 05:02:08 +00:00
# Python imports
import os, threading, time
from os.path import isdir, isfile, join
from os import listdir
# Gtk imports
2019-06-09 21:03:38 +00:00
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Gdk', '3.0')
from gi.repository import Gtk as gtk
from gi.repository import Gdk as gdk
2019-06-17 01:47:15 +00:00
from gi.repository import GLib as glib
2019-06-15 02:48:32 +00:00
from gi.repository import GdkPixbuf
2019-06-09 21:03:38 +00:00
# Application imports
from .Icon import Icon
2019-06-23 00:21:18 +00:00
from utils.FileHandler import FileHandler
2019-06-09 21:03:38 +00:00
2019-06-15 02:48:32 +00:00
def threaded(fn):
def wrapper(*args, **kwargs):
2019-10-15 07:10:46 +00:00
threading.Thread(target=fn, args=args, kwargs=kwargs).start()
2019-06-15 02:48:32 +00:00
return wrapper
2019-06-09 21:03:38 +00:00
2019-07-07 23:18:51 +00:00
2019-06-15 02:48:32 +00:00
class Grid:
def __init__(self, grid, settings):
2019-07-07 23:18:51 +00:00
self.grid = grid
self.settings = settings
self.fileHandler = FileHandler(self.settings)
self.store = gtk.ListStore(GdkPixbuf.Pixbuf, str)
self.usrHome = settings.returnUserHome()
self.hideHiddenFiles = settings.isHideHiddenFiles()
self.builder = settings.returnBuilder()
self.ColumnSize = settings.returnColumnSize()
self.vidsFilter = settings.returnVidsFilter()
self.imagesFilter = settings.returnImagesFilter()
self.iconFactory = Icon(settings)
self.selectedFiles = []
self.currentPath = ""
2019-06-16 09:28:58 +00:00
self.grid.set_model(self.store)
self.grid.set_pixbuf_column(0)
self.grid.set_text_column(1)
self.grid.connect("item-activated", self.iconDblLeftClick)
self.grid.connect("button_release_event", self.iconSingleClick, (self.grid,))
2019-06-09 21:03:38 +00:00
def setNewDirectory(self, path):
2020-05-09 05:02:08 +00:00
self.store.clear()
2019-06-15 02:48:32 +00:00
self.currentPath = path
dirPaths = ['.', '..']
2019-06-19 00:17:29 +00:00
vids = []
images = []
desktop = []
files = []
2019-06-09 21:03:38 +00:00
2019-06-15 02:48:32 +00:00
for f in listdir(path):
file = join(path, f)
2019-07-07 23:18:51 +00:00
if self.hideHiddenFiles:
2019-06-09 21:03:38 +00:00
if f.startswith('.'):
continue
2019-07-07 23:18:51 +00:00
2019-06-09 21:03:38 +00:00
if isfile(file):
2019-07-07 23:18:51 +00:00
lowerName = file.lower()
if lowerName.endswith(self.vidsFilter):
2019-06-19 00:17:29 +00:00
vids.append(f)
2019-07-07 23:18:51 +00:00
elif lowerName.endswith(self.imagesFilter):
2019-06-19 00:17:29 +00:00
images.append(f)
2019-07-07 23:18:51 +00:00
elif lowerName.endswith((".desktop",)):
2019-06-19 00:17:29 +00:00
desktop.append(f)
else:
files.append(f)
2019-06-09 21:03:38 +00:00
else:
dirPaths.append(f)
2019-06-09 21:03:38 +00:00
dirPaths.sort()
2019-06-19 00:17:29 +00:00
vids.sort()
images.sort()
desktop.sort()
2019-06-15 02:48:32 +00:00
files.sort()
2019-06-21 04:07:30 +00:00
2019-07-07 23:18:51 +00:00
files = dirPaths + vids + images + desktop + files
self.generateGridIcons(path, files)
2019-10-15 07:10:46 +00:00
self.fillVideoIcons(path, vids, len(dirPaths))
2019-06-21 04:07:30 +00:00
2019-06-15 02:48:32 +00:00
@threaded
2019-07-07 23:18:51 +00:00
def generateGridIcons(self, dirPath, files):
2019-06-15 02:48:32 +00:00
for file in files:
image = self.iconFactory.createIcon(dirPath, file).get_pixbuf()
glib.idle_add(self.addToGrid, (image, file,))
2019-09-29 00:14:32 +00:00
2020-02-10 01:53:03 +00:00
@threaded
def fillVideoIcons(self, dirPath, files, start):
model = self.grid.get_model()
# Wait till we have a proper index...
while len(self.store) < (start + 1):
time.sleep(.650)
i = start
for file in files:
2020-02-10 01:53:03 +00:00
self.updateGrid(model, dirPath, file, i)
i += 1
2020-02-10 01:53:03 +00:00
@threaded
def updateGrid(self, model, dirPath, file, i):
try:
image = self.iconFactory.createThumbnail(dirPath, file).get_pixbuf()
iter = model.get_iter_from_string(str(i))
glib.idle_add(self.replaceInGrid, (iter, image,))
except Exception as e:
2020-05-09 05:02:08 +00:00
# Errors seem to happen when fillVideoIcons index wait check is to low
print("widgets/Grid.py sinking errors on updateGrid method...")
2020-02-10 01:53:03 +00:00
def addToGrid(self, dataSet):
self.store.append([dataSet[0], dataSet[1]])
def replaceInGrid(self, dataSet):
# Iter, row column, new pixbuf...
self.store.set_value(dataSet[0], 0 , dataSet[1])
2019-07-05 19:34:15 +00:00
def iconDblLeftClick(self, widget, item):
try:
model = widget.get_model()
fileName = model[item][1]
dir = self.currentPath
file = dir + "/" + fileName
2019-06-09 21:03:38 +00:00
2019-06-15 02:48:32 +00:00
if fileName == ".":
self.setNewDirectory(dir)
2019-06-15 02:48:32 +00:00
elif fileName == "..":
parentDir = os.path.abspath(os.path.join(dir, os.pardir))
self.currentPath = parentDir
self.setNewDirectory(parentDir)
self.settings.saveSettings(parentDir)
2019-06-15 02:48:32 +00:00
elif isdir(file):
self.currentPath = file
self.setNewDirectory(self.currentPath)
self.settings.saveSettings(self.currentPath)
2019-06-15 02:48:32 +00:00
elif isfile(file):
2019-07-05 19:34:15 +00:00
self.fileHandler.openFile(file)
2019-06-15 02:48:32 +00:00
except Exception as e:
print(e)
2019-07-07 00:08:25 +00:00
def iconSingleClick(self, widget, eve, rclicked_icon):
2019-06-15 02:48:32 +00:00
try:
2019-07-07 00:08:25 +00:00
if eve.type == gdk.EventType.BUTTON_RELEASE and eve.button == 1:
self.selectedFiles.clear()
2019-06-30 03:50:29 +00:00
items = widget.get_selected_items()
model = widget.get_model()
2019-07-07 00:08:25 +00:00
dir = self.currentPath
2019-06-30 03:50:29 +00:00
2019-07-07 00:08:25 +00:00
for item in items:
fileName = model[item][1]
if fileName != "." and fileName != "..":
file = dir + "/" + fileName
self.selectedFiles.append(file) # Used for return to caller
elif eve.type == gdk.EventType.BUTTON_RELEASE and eve.button == 3:
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:
2019-06-30 03:50:29 +00:00
input.set_text("")
input.hide()
2019-07-07 00:08:25 +00:00
menuButtonBox.hide()
iconsButtonBox.show()
controls.show()
else:
input.set_text("")
input.show()
menuButtonBox.show()
iconsButtonBox.hide()
2019-06-30 03:50:29 +00:00
controls.show()
except Exception as e:
print(e)
2019-07-05 19:34:15 +00:00
def returnSelectedFiles(self):
2019-07-07 02:18:32 +00:00
# NOTE: Just returning selectedFiles looks like it returns a "pointer"
# to the children. This means we lose the list if any left click occures
2019-07-07 02:18:32 +00:00
# in this class.
files = []
for file in self.selectedFiles:
files.append(file)
return files
2019-07-05 19:34:15 +00:00
def returnCurrentPath(self):
currentPath = self.currentPath
return currentPath