WIP new structure
This commit is contained in:
@@ -1,3 +0,0 @@
|
||||
"""
|
||||
Core module
|
||||
"""
|
||||
@@ -1,177 +0,0 @@
|
||||
# Python imports
|
||||
import html, time
|
||||
|
||||
import pyscreenshot as capture
|
||||
|
||||
# Lib imports
|
||||
import gi, cairo
|
||||
gi.require_version('Gdk', '3.0')
|
||||
|
||||
from gi.repository import Gdk
|
||||
from gi.repository import GLib
|
||||
|
||||
# Application imports
|
||||
|
||||
|
||||
|
||||
|
||||
class MouseButtons:
|
||||
LEFT_BUTTON = 1
|
||||
RIGHT_BUTTON = 3
|
||||
|
||||
|
||||
class DrawingArea:
|
||||
def __init__(self):
|
||||
self.builder = settings.get_builder()
|
||||
self.main_window = self.builder.get_object('Main_Window')
|
||||
self.region_window = self.builder.get_object('Region_Window')
|
||||
self.region_menu = self.builder.get_object('regionMenu')
|
||||
self.message_label = self.builder.get_object('messageLabel')
|
||||
MONITOR = settings.get_monitor_data()
|
||||
|
||||
settings.set_window_data(self.region_window)
|
||||
self.region_window.set_default_size(MONITOR[0].width, MONITOR[0].height)
|
||||
self.region_window.set_size_request(MONITOR[0].width, MONITOR[0].height)
|
||||
self.region_window.set_keep_above(True)
|
||||
|
||||
self.DRAW_AREA = self.builder.get_object("selectionArea")
|
||||
self.DRAW_AREA.add_events(Gdk.EventMask.BUTTON_PRESS_MASK)
|
||||
self.DRAW_AREA.add_events(Gdk.EventMask.BUTTON_RELEASE_MASK)
|
||||
self.DRAW_AREA.add_events(Gdk.EventMask.BUTTON1_MOTION_MASK)
|
||||
self.DRAW_AREA.connect("button-press-event", self.on_button_press)
|
||||
self.DRAW_AREA.connect("button-release-event", self.on_button_release)
|
||||
self.DRAW_AREA.connect("motion-notify-event", self.on_mouse_move)
|
||||
self.DRAW_AREA.connect("draw", self.on_draw)
|
||||
|
||||
area = settings.get_monitor_data()[0]
|
||||
self.SCREENSHOTS_DIR = settings.get_screenshots_dir()
|
||||
self.WIN_REC = [area.x, area.y, area.width, area.height]
|
||||
self.coords = [[0.0, 0.0], [0.0, 0.0]] # point1 and point2
|
||||
self.BORDER_COLOR = [255, 0, 0, 0.84]
|
||||
self.TRANS_COLOR = [0, 0, 0, 0.0]
|
||||
self.BG_COLOR = [0, 0, 0, 0.4]
|
||||
self.success = "#88cc27"
|
||||
self.warning = "#ffa800"
|
||||
self.error = "#ff0000"
|
||||
self.rec = None
|
||||
self.cr = None
|
||||
|
||||
self.region_window.set_default_size(area.width, area.height)
|
||||
self.region_window.set_size_request(area.width, area.height)
|
||||
self.region_window.move(area.x, area.y)
|
||||
self.region_window.set_resizable(False)
|
||||
self.region_window.set_keep_above(True)
|
||||
|
||||
|
||||
def on_button_press(self, w, e):
|
||||
self.message_label.set_markup("")
|
||||
if e.type == Gdk.EventType.BUTTON_PRESS and e.button == MouseButtons.LEFT_BUTTON:
|
||||
self.coords[0] = [e.x, e.y]
|
||||
self.region_menu.hide()
|
||||
|
||||
# This will reset draw area initially. No further use
|
||||
if self.cr:
|
||||
self.draw(self.cr, self.WIN_REC, self.BG_COLOR)
|
||||
self.DRAW_AREA.queue_draw()
|
||||
if e.type == Gdk.EventType.BUTTON_PRESS and e.button == MouseButtons.RIGHT_BUTTON:
|
||||
self.region_menu.show()
|
||||
|
||||
# Update second set of coords.
|
||||
def on_mouse_move(self, w, e):
|
||||
self.coords[1] = [e.x, e.y]
|
||||
self.DRAW_AREA.queue_draw()
|
||||
|
||||
def on_button_release(self, w, e):
|
||||
if e.type == Gdk.EventType.BUTTON_RELEASE and e.button == MouseButtons.LEFT_BUTTON:
|
||||
self.region_menu.show()
|
||||
|
||||
@daemon_threaded
|
||||
def grab_region(self, widget):
|
||||
GLib.idle_add(self.main_window.hide)
|
||||
GLib.idle_add(self.region_menu.hide)
|
||||
|
||||
while self.main_window.is_visible():
|
||||
...
|
||||
while self.region_menu.is_visible():
|
||||
...
|
||||
|
||||
time.sleep(0.05)
|
||||
self.grab_region_idle()
|
||||
|
||||
def go_to_main_window(self, widget):
|
||||
self.go_to_main_window_idle()
|
||||
|
||||
def grab_region_idle(self):
|
||||
self.do_bounding_box_grab(self.rec[0], self.rec[1], self.rec[2], self.rec[3])
|
||||
GLib.idle_add(utils.referesh_directory_list)
|
||||
GLib.idle_add(self.main_window.show)
|
||||
GLib.idle_add(self.region_menu.show)
|
||||
|
||||
def go_to_main_window_idle(self):
|
||||
self.region_window.hide()
|
||||
self.region_menu.hide()
|
||||
self.main_window.show()
|
||||
|
||||
|
||||
def on_draw(self, wid, cr):
|
||||
if not self.cr: self.cr = cr
|
||||
# Reset the screen with transparent view
|
||||
self.draw(cr, self.WIN_REC, self.BG_COLOR)
|
||||
|
||||
point1 = self.coords[0]
|
||||
point2 = self.coords[1]
|
||||
x1 = point1[0]
|
||||
y1 = point1[1]
|
||||
x2 = point2[0]
|
||||
y2 = point2[1]
|
||||
w = x2 - x1
|
||||
h = y2 - y1
|
||||
|
||||
# Rectangle information for region and screen grab
|
||||
self.rec = [int(x1), int(y1), int(x2), int(y2)]
|
||||
# Draw the new selection region
|
||||
self.selection_draw(cr, [x1, y1, w, h], self.BORDER_COLOR, self.TRANS_COLOR)
|
||||
|
||||
|
||||
def draw(self, cr, x1y1wh, rgba):
|
||||
cr.set_source_rgba(rgba[0], rgba[1], rgba[2], rgba[3])
|
||||
cr.rectangle(x1y1wh[0], x1y1wh[1], x1y1wh[2], x1y1wh[3])
|
||||
cr.set_operator(1);
|
||||
cr.fill()
|
||||
|
||||
def selection_draw(self, cr, x1y1wh, brdrcol, transclr):
|
||||
# Clear the region
|
||||
cr.set_source_rgba(transclr[0], transclr[1], transclr[2], transclr[3])
|
||||
cr.rectangle(x1y1wh[0], x1y1wh[1], x1y1wh[2], x1y1wh[3])
|
||||
cr.set_operator(0);
|
||||
cr.fill()
|
||||
|
||||
# Draw a border
|
||||
cr.set_source_rgba(brdrcol[0], brdrcol[1], brdrcol[2], brdrcol[3])
|
||||
cr.rectangle(x1y1wh[0] - 2, x1y1wh[1] - 2, x1y1wh[2] + 4, x1y1wh[3] + 4)
|
||||
cr.set_operator(1);
|
||||
cr.stroke()
|
||||
|
||||
# Actual region grab
|
||||
def do_bounding_box_grab(self, x1, y1, x2, y2):
|
||||
try:
|
||||
temp = 0;
|
||||
if x2 < x1:
|
||||
temp = x1
|
||||
x1 = x2
|
||||
x2 = temp
|
||||
|
||||
if y2 < y1:
|
||||
temp = y1
|
||||
y1 = y2
|
||||
y2 = temp
|
||||
|
||||
|
||||
utils.do_bounding_box_grab(x1, y1, x2, y2)
|
||||
markup = "<span foreground='" + self.success + "'>Grabbed region successfully...</span>"
|
||||
GLib.idle_add(self.message_label.set_markup, markup)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
markup = "<span foreground='" + self.warning + "' >Oops...</span>" + \
|
||||
"\n<span foreground='" + self.error + "'>" + html.escape( str(e) ) + "</span>"
|
||||
GLib.idle_add(self.message_label.set_markup, markup)
|
||||
@@ -1,49 +0,0 @@
|
||||
# Python imports
|
||||
import os, subprocess
|
||||
|
||||
# lib imports
|
||||
|
||||
|
||||
# Application imports
|
||||
|
||||
|
||||
class MainMenuPopup:
|
||||
def __init__(self):
|
||||
self.builder = settings.get_builder()
|
||||
self.file_name_entry = self.builder.get_object("fileNameEntry")
|
||||
self.SCREENSHOTS_DIR = settings.get_screenshots_dir()
|
||||
self.backup_name = None
|
||||
|
||||
|
||||
def rename_file(self, widget, data=None):
|
||||
new_name = self.file_name_entry.get_text().strip()
|
||||
old_file_path = f"{self.SCREENSHOTS_DIR}/{self.backup_name}"
|
||||
new_file_path = f"{self.SCREENSHOTS_DIR}/{new_name}"
|
||||
try:
|
||||
if os.path.isfile(old_file_path) and new_name:
|
||||
os.rename(old_file_path, new_file_path)
|
||||
self.backup_name = new_name
|
||||
utils.referesh_directory_list()
|
||||
except Exception as e:
|
||||
print(repr(e))
|
||||
|
||||
def open_file(self, widget, data=None):
|
||||
file = f"{self.SCREENSHOTS_DIR}/{self.backup_name}"
|
||||
subprocess.Popen(['xdg-open', file], stdout=subprocess.PIPE)
|
||||
|
||||
def delete_file(self, widget, data=None):
|
||||
try:
|
||||
file = f"{self.SCREENSHOTS_DIR}/{self.backup_name}"
|
||||
if os.path.isfile(file):
|
||||
os.remove(file)
|
||||
self.builder.get_object("mainMenu").popdown()
|
||||
utils.referesh_directory_list()
|
||||
except Exception as e:
|
||||
print(repr(e))
|
||||
|
||||
|
||||
def reset_name(self, widget, data=None):
|
||||
self.file_name_entry.set_text(self.backup_name)
|
||||
|
||||
def set_backup_var(self, widget):
|
||||
self.backup_name = self.file_name_entry.get_text()
|
||||
@@ -1,160 +0,0 @@
|
||||
# Python imports
|
||||
import os, signal, time
|
||||
import pyscreenshot as capture
|
||||
|
||||
# Lib imports
|
||||
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
|
||||
from gi.repository import GLib
|
||||
|
||||
# Application imports
|
||||
|
||||
|
||||
|
||||
class MouseButtons:
|
||||
LEFT_BUTTON = 1
|
||||
RIGHT_BUTTON = 3
|
||||
|
||||
|
||||
class MainWindow:
|
||||
def __init__(self):
|
||||
self.builder = settings.get_builder()
|
||||
self.main_window = self.builder.get_object('Main_Window')
|
||||
self.region_window = self.builder.get_object('Region_Window')
|
||||
self.monitors_view = self.builder.get_object("monitorsView")
|
||||
self.monitor_store = self.builder.get_object("monitorStore")
|
||||
self.MONITORS = settings.get_monitor_data()
|
||||
|
||||
# Not adding the reference monitor
|
||||
i = 0
|
||||
for monitor in self.MONITORS:
|
||||
if i > 0:
|
||||
mon = str(monitor.width) + "x" + str(monitor.height) + "+" + str(monitor.x) + "+" + str(monitor.y)
|
||||
self.monitor_store.append([mon])
|
||||
i += 1
|
||||
|
||||
self.monitors_view.set_cursor(1)
|
||||
|
||||
self.SCREENSHOTS_DIR = settings.get_screenshots_dir()
|
||||
utils.referesh_directory_list()
|
||||
|
||||
GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, signal.SIGINT, Gtk.main_quit)
|
||||
|
||||
|
||||
def take_screenshot(self, widget):
|
||||
active_radio = self.get_active_radio()
|
||||
active = active_radio.get_children()[0].get_text()
|
||||
|
||||
if "Entire screen" in active:
|
||||
self.grab_entire_screen()
|
||||
if "Active window" in active:
|
||||
self.get_active_window()
|
||||
if "Select a region" in active:
|
||||
self.region_window.show_all()
|
||||
if "Select a monitor" in active:
|
||||
self.snapshot_monitor()
|
||||
|
||||
|
||||
def grab_entire_screen(self):
|
||||
self.main_window.hide()
|
||||
self.get_entire_screen()
|
||||
|
||||
def get_entire_screen(self):
|
||||
# childprocess=False needed to not crash program
|
||||
im = capture.grab(childprocess=False)
|
||||
im.save(utils.generate_screenshot_name())
|
||||
self.main_window.show()
|
||||
utils.referesh_directory_list()
|
||||
|
||||
def get_active_window(self):
|
||||
utils.sleep()
|
||||
|
||||
screen = Gdk.get_default_root_window().get_screen()
|
||||
w = screen.get_active_window()
|
||||
pb = Gdk.pixbuf_get_from_window(w, *w.get_geometry())
|
||||
pb.savev(utils.generate_screenshot_name(), "png", (), ())
|
||||
utils.referesh_directory_list()
|
||||
|
||||
|
||||
|
||||
@daemon_threaded
|
||||
def snapshot_monitor(self):
|
||||
GLib.idle_add(self.main_window.hide)
|
||||
|
||||
while self.main_window.is_visible():
|
||||
...
|
||||
|
||||
time.sleep(0.05)
|
||||
GLib.idle_add(self.do_snapshot_monitor)
|
||||
|
||||
|
||||
def do_snapshot_monitor(self):
|
||||
iterator = self.monitors_view.get_selection().get_selected()[1]
|
||||
path = self.monitor_store.get_path(iterator)
|
||||
# Slot 0 is ref monitor. Need to add 1 to get proper slot
|
||||
monitor = self.MONITORS[int(str(path)) + 1]
|
||||
|
||||
utils.sleep()
|
||||
|
||||
x2 = monitor.x + monitor.width
|
||||
y2 = monitor.y + monitor.height
|
||||
utils.do_bounding_box_grab(monitor.x, monitor.y, x2, y2)
|
||||
utils.referesh_directory_list()
|
||||
self.main_window.show()
|
||||
|
||||
def toggle_radio_bttn(self, widget):
|
||||
delay_amount = self.builder.get_object('delayAmount')
|
||||
snapshot_bttn = self.builder.get_object('snapshotBttn')
|
||||
active = self.get_active_radio().get_children()[0].get_text()
|
||||
|
||||
self.region_window.hide()
|
||||
self.monitors_view.set_sensitive(False)
|
||||
delay_amount.set_sensitive(True)
|
||||
snapshot_bttn.set_sensitive(True)
|
||||
delay_amount.set_value(0)
|
||||
|
||||
if "Active window" in active:
|
||||
delay_amount.set_value(4)
|
||||
if "Select a region" in active:
|
||||
delay_amount.set_sensitive(False)
|
||||
if "Select a monitor" in active:
|
||||
self.monitors_view.set_sensitive(True)
|
||||
|
||||
def set_image(self, user_data):
|
||||
# We need the refresh state for the files list b/c GtkTreeSelection
|
||||
# is calling this method b/c caling this too quickly causes issues...
|
||||
if utils.get_refreshing_state() == False:
|
||||
selected = user_data.get_selected()[1]
|
||||
if selected:
|
||||
fileNameEntry = self.builder.get_object("fileNameEntry")
|
||||
imageView = self.builder.get_object("imageView")
|
||||
file = self.builder.get_object("fileStore").get_value(selected, 0)
|
||||
fullPath = f"{self.SCREENSHOTS_DIR}/{file}"
|
||||
|
||||
try:
|
||||
if os.path.isfile(fullPath):
|
||||
fileNameEntry.set_text(file)
|
||||
pixbuf = Gtk.Image.new_from_file(fullPath).get_pixbuf()
|
||||
scaledPixBuf = pixbuf.scale_simple(480, 320, 2) # 2 = BILINEAR and is best by default
|
||||
imageView.set_from_pixbuf(scaledPixBuf)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
def get_active_radio(self):
|
||||
master_radio = self.builder.get_object('entireScrnToggle')
|
||||
active_radio = next((
|
||||
radio for radio in master_radio.get_group()
|
||||
if radio.get_active()
|
||||
))
|
||||
return active_radio
|
||||
|
||||
def show_main_menu(self, w, e):
|
||||
if e.type == Gdk.EventType.BUTTON_PRESS and e.button == MouseButtons.RIGHT_BUTTON:
|
||||
self.builder.get_object("mainMenu").popup()
|
||||
|
||||
def close(self, widget):
|
||||
Gtk.main_quit()
|
||||
Reference in New Issue
Block a user