Added preview and menu logic

This commit is contained in:
itdominator 2023-04-22 10:55:06 -05:00
parent 212f387ec4
commit 454a8b0cab
8 changed files with 222 additions and 11 deletions

View File

@ -9,7 +9,7 @@ from gi.repository import Gtk
from ..widgets.radio_buttons import RadioButtons
from ..widgets.delay_amount import DelayAmount
from ..widgets.preview_image import PreviewPane
from ..widgets.menu_popover import MenuPopover
class LeftBox(Gtk.Box):
@ -30,6 +30,11 @@ class LeftBox(Gtk.Box):
...
def _load_widgets(self):
menu = MenuPopover()
delay_amount = DelayAmount()
menu.set_relative_to(delay_amount)
menu.set_position(Gtk.PositionType.BOTTOM)
self.add(RadioButtons())
self.add(DelayAmount())
self.add(delay_amount)
self.add(PreviewPane())

View File

@ -31,6 +31,7 @@ class DelayAmount(Gtk.Box):
...
def _subscribe_to_events(self):
event_system.subscribe("set_grab_delay", self.set_grab_delay)
event_system.subscribe("grab_delay", self.grab_delay)
def _load_widgets(self):
@ -47,6 +48,11 @@ class DelayAmount(Gtk.Box):
self.add(label)
self.add(spinner)
def set_grab_delay(self, wait = 0.0):
delay_amount = self.get_children()[1]
delay_amount.set_value(wait)
def grab_delay(self, wait = None):
delay_amount = self.get_children()[1]
if not wait:

View File

@ -16,7 +16,10 @@ class ImagesList(TreeMixin, Gtk.ScrolledWindow):
def __init__(self):
super(ImagesList, self).__init__()
self._tree_view = None
self._store = None
self._dir_watcher = None
self._watch_dir = None
self._set_file_watcher()
self._setup_styling()
@ -40,9 +43,16 @@ class ImagesList(TreeMixin, Gtk.ScrolledWindow):
...
def _load_widgets(self):
grid, self._store = self._create_treeview_widget("Images")
scroll, self._store = self._create_treeview_widget("Images")
self._tree_view = scroll.get_children()[0]
self._tree_view.connect("button-press-event", self._handle_clicks)
self._tree_view.connect("row-activated", self._set_active_image)
self._tree_view.set_property("activate-on-single-click", True)
self.referesh_directory_list()
self.add(grid)
self.add(scroll)
def _set_file_watcher(self):
if settings.is_debug():
@ -50,10 +60,10 @@ class ImagesList(TreeMixin, Gtk.ScrolledWindow):
return
images_dir = settings.get_screenshots_dir()
dir_watcher = Gio.File.new_for_path(f"{images_dir}") \
.monitor_directory(Gio.FileMonitorFlags.WATCH_MOVES, Gio.Cancellable())
self._watch_dir = Gio.File.new_for_uri(f"file://{images_dir}")
self._dir_watcher = self._watch_dir.monitor_directory(Gio.FileMonitorFlags.WATCH_MOVES, Gio.Cancellable())
dir_watcher.connect("changed", self._dir_watch_updates)
self._dir_watcher.connect("changed", self._dir_watch_updates, ())
def _dir_watch_updates(self, file_monitor, file, other_file = None, eve_type = None, data = None):
if eve_type in [Gio.FileMonitorEvent.CREATED, Gio.FileMonitorEvent.DELETED,
@ -63,6 +73,29 @@ class ImagesList(TreeMixin, Gtk.ScrolledWindow):
self._store.clear()
self.referesh_directory_list()
def _set_active_image(self, tree_view = None, path = None, column = None):
file = self.get_selected()
event_system.emit("set_image_to_view", (file,))
event_system.emit("set_revert_data", (file,))
def _handle_clicks(self, widget = None, eve = None):
if eve.button == 1 and eve.type == 5:
file = self.get_selected()
event_system.emit("set_revert_data", (file,))
event_system.emit("open_file")
return
if eve.button == 1 and eve.type == 4:
self._set_active_image()
return
if eve.button == 3:
file = self.get_selected()
event_system.emit("set_revert_data", (file,))
event_system.emit("show_menu", (file,))
return
@threaded
def referesh_directory_list(self):
images = settings.get_directory_list()
@ -72,3 +105,10 @@ class ImagesList(TreeMixin, Gtk.ScrolledWindow):
def add_to_store(self, image):
self._store.append(image)
def get_selected(self):
model, treeiter = self._tree_view.get_selection().get_selected()
if treeiter != None:
return model[treeiter][0]
return None

View File

@ -0,0 +1,117 @@
# Python imports
import os
import subprocess
# Lib imports
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
# Application imports
class MenuPopover(Gtk.Popover):
def __init__(self):
super(MenuPopover, self).__init__()
self._revert_name = None
self._rename_entry = None
self._setup_styling()
self._setup_signals()
self._subscribe_to_events()
self._load_widgets()
def _setup_styling(self):
self.set_size_request(360, -1)
def _setup_signals(self):
# self.connect("grab-focus", self.set_revert_data)
...
def _subscribe_to_events(self):
event_system.subscribe("set_revert_data", self.set_revert_data)
event_system.subscribe("show_menu", self.show_menu)
event_system.subscribe("open_file", self.open_file)
def _load_widgets(self):
box = Gtk.Box()
box2 = Gtk.Box()
self._rename_entry = Gtk.Entry()
revert_button = Gtk.Button(label = "Revert")
rename_button = Gtk.Button(label = "Rename")
open_button = Gtk.Button(label = "Open")
delete_button = Gtk.Button(label = "Delete")
revert_button.set_image( Gtk.Image.new_from_icon_name("gtk-undo", 16) )
rename_button.set_image( Gtk.Image.new_from_icon_name("gtk-edit", 16) )
open_button.set_image( Gtk.Image.new_from_icon_name("gtk-open", 16) )
delete_button.set_image( Gtk.Image.new_from_icon_name("gtk-delete", 16) )
revert_button.set_always_show_image(True)
rename_button.set_always_show_image(True)
open_button.set_always_show_image(True)
delete_button.set_always_show_image(True)
box.set_orientation(Gtk.Orientation.VERTICAL)
box2.set_orientation(Gtk.Orientation.HORIZONTAL)
self._rename_entry.set_hexpand(True)
box2.add(self._rename_entry)
box2.add(revert_button)
box.add(box2)
box.add(rename_button)
box.add(open_button)
box.add(delete_button)
revert_button.connect("clicked", self.revert_name)
rename_button.connect("clicked", self.rename_file)
open_button.connect("clicked", self.open_file)
delete_button.connect("clicked", self.delete_file)
box.show_all()
self.add(box)
def set_revert_data(self, name):
if not name in ("", None):
self._revert_name = name
self._rename_entry.set_text(name)
def show_menu(self, name):
if not name in ("", None):
self.set_revert_data(name)
self.popup()
def revert_name(self, widget = None, data = None):
self._rename_entry.set_text(self._revert_name)
def rename_file(self, widget, data=None):
dir = settings.get_screenshots_dir()
new_name = self._rename_entry.get_text().strip()
old_file_path = os.path.join(dir, self._revert_name)
new_file_path = os.path.join(dir, new_name)
try:
if os.path.isfile(old_file_path) and not new_name in ("", None):
os.rename(old_file_path, new_file_path)
self._revert_name = new_name
except Exception as e:
logger.info(e)
def open_file(self, widget = None, data = None):
dir = settings.get_screenshots_dir()
file = os.path.join(dir, self._revert_name)
subprocess.Popen(['xdg-open', file], stdout = subprocess.PIPE)
def delete_file(self, widget, data=None):
try:
dir = settings.get_screenshots_dir()
file = os.path.join(dir, self._revert_name)
if os.path.isfile(file):
os.remove(file)
self.popdown()
except Exception as e:
logger.info(e)

View File

@ -34,6 +34,7 @@ class MonitorList(TreeMixin, Gtk.Box):
def _subscribe_to_events(self):
event_system.subscribe("get_selected_monitor", self.get_selected_monitor)
event_system.subscribe("set_monitor_sensitive", self.set_monitor_sensitive)
def _load_widgets(self):
grid, self._store = self._create_treeview_widget("Monitors")
@ -41,6 +42,7 @@ class MonitorList(TreeMixin, Gtk.Box):
self._load_monitor_store()
self.set_monitor_sensitive()
grid.set_hexpand(True)
self.add(grid)
@ -53,9 +55,14 @@ class MonitorList(TreeMixin, Gtk.Box):
self._store.append([mon])
i += 1
self._monitors_view.set_cursor(0)
def get_selected_monitor(self):
iter = self._monitors_view.get_selection().get_selected()[1]
path = self._store.get_path(iter)
# Slot 0 is ref monitor. Need to add 1 to get proper slot
return self.MONITORS[int(str(path)) + 1]
def set_monitor_sensitive(self, isSensitive = False):
self._monitors_view.set_sensitive(isSensitive)

View File

@ -1,9 +1,11 @@
# Python imports
import os
# Lib imports
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gio
# Application imports
@ -17,6 +19,7 @@ class PreviewPane(Gtk.AspectFrame):
self._setup_styling()
self._setup_signals()
self._subscribe_to_events()
self._load_widgets()
self.show_all()
@ -28,6 +31,20 @@ class PreviewPane(Gtk.AspectFrame):
def _setup_signals(self):
...
def _subscribe_to_events(self):
event_system.subscribe("set_image_to_view", self.set_image_to_view)
def _load_widgets(self):
self._preview_image = Gtk.Image()
self.add(self._preview_image)
def set_image_to_view(self, image_file):
if not image_file:
return
images_dir = settings.get_screenshots_dir()
path = os.path.join(images_dir, image_file)
pixbuf = Gtk.Image.new_from_file(path).get_pixbuf()
scaledPixBuf = pixbuf.scale_simple(480, 320, 2) # 2 = BILINEAR and is best by default
self._preview_image.set_from_pixbuf(scaledPixBuf)

View File

@ -51,7 +51,26 @@ class RadioButtons(Gtk.Box):
else:
last_child = child
child.connect("released", self._set_data_state)
def _get_active_type(self):
group = self.get_children()[0].get_group()
active_radio = [r for r in group if r.get_active()]
return active_radio[0]
def _set_data_state(self, widget = None, eve = None):
label = widget.get_label()
isSensitive = False
wait = 0.0
if label == "Entire Screen":
...
if label == "Active Window":
wait = 4.0
if label == "Select Region":
...
if label == "Select Monitor":
isSensitive = True
event_system.emit("set_grab_delay", (wait,))
event_system.emit("set_monitor_sensitive", (isSensitive,))

View File

@ -2,10 +2,10 @@
background-color: rgba(0, 0, 0, 0.0);
/* border: 2px solid rgba(136, 204, 39, 1); */
/* Dark Bergundy */
border: 2px solid rgba(116, 0, 0, 0.64);
border: 2px solid rgba(116, 0, 0, 1);
}
.expand-button {
background-color: rgba(0, 0, 0, 0.0);
border: 2px solid rgba(56, 56, 56, 0.46);
border: 2px solid rgba(56, 56, 56, 1);
}