Use binded events to add ActionRow

This commit is contained in:
Hugo Posnic 2023-03-28 13:41:22 +02:00
parent 8cd94f1a91
commit 3f4009d265
4 changed files with 59 additions and 50 deletions

View File

@ -20,7 +20,8 @@ from gi.repository import Gtk, Gio, GObject
from shutil import copy2
from pathlib import Path
from .tools import message_dialog, get_file_type
from .resultitem import ResultItem
from .tools import message_dialog, get_file_type, sizeof_fmt
SETTINGS_SCHEMA = 'com.github.huluti.Curtail'
@ -44,10 +45,8 @@ class Compressor():
self.full_name = self.file_data.name
self.size = self.file_data.stat().st_size
self.new_size = 0
self.row = None
def run_command(self, command):
def run_command(self, command, result_item):
try:
subprocess.call(command,
stdout=subprocess.PIPE,
@ -55,7 +54,7 @@ class Compressor():
stdin=subprocess.PIPE,
shell=True,
timeout=20)
self.command_finished()
self.command_finished(result_item)
except Exception as err:
message_dialog(self.win, _("An error has occured"), str(err))
@ -63,8 +62,11 @@ class Compressor():
def compress_image(self):
file_type = get_file_type(self.filename)
if file_type:
self.row = self.win.create_result_row(self.full_name, self.filename,
self.new_filename, self.size)
result_item = ResultItem(self.full_name, self.filename,
self.new_filename, sizeof_fmt(self.size), 0)
self.win.results_model.insert(0, result_item)
self.win.listbox.invalidate_headers()
lossy = self._settings.get_boolean('lossy')
metadata = self._settings.get_boolean('metadata')
file_attributes = self._settings.get_boolean('file-attributes')
@ -75,14 +77,12 @@ class Compressor():
command = self.build_jpg_command(lossy, metadata, file_attributes)
elif file_type == 'webp':
command = self.build_webp_command(lossy, metadata)
self.run_command(command) # compress image
self.run_command(command, result_item) # compress image
def command_finished(self):
# TODO: Check if new size is equal or higher than the old one
self.new_size = self.new_file_data.stat().st_size
savings = round(100 - (self.new_size * 100 / self.size), 2)
self.win.update_result_row(self.row, self.size, self.new_size, savings)
def command_finished(self, result_item):
new_size = self.new_file_data.stat().st_size
result_item.size = result_item.size + ' -> ' + sizeof_fmt(new_size)
result_item.savings = str(round(100 - (new_size * 100 / self.size), 2)) + '%'
def build_png_command(self, lossy, metadata, file_attributes):
pngquant = 'pngquant --quality=0-{} -f "{}" --output "{}"'
@ -168,5 +168,3 @@ class Compressor():
return command
def feed(self, stdout, condition):
return True

View File

@ -31,6 +31,7 @@ curtail_sources = [
'window.py',
'compressor.py',
'preferences.py',
'resultitem.py',
'tools.py'
]

22
src/resultitem.py Normal file
View File

@ -0,0 +1,22 @@
# resultitem.py
from gi.repository import GObject
class ResultItem(GObject.Object):
name = GObject.Property(type=str)
filename = GObject.Property(type=str)
new_filename = GObject.Property(type=str)
size = GObject.Property(type=str)
savings = GObject.Property(type=str)
def __init__(self, name, filename, new_filename, size, savings):
super().__init__()
self.name = name
self.filename = filename
self.new_filename = new_filename
self.size = size
self.savings = savings
def __repr__(self):
return str(self.name)

View File

@ -16,14 +16,15 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import subprocess
from gi.repository import Gtk, Gdk, Gio, GLib, Adw
from gi.repository import Gtk, Gdk, Gio, GLib, Adw, GObject
from urllib.parse import unquote
from pathlib import Path
from .resultitem import ResultItem
from .preferences import CurtailPrefsWindow
from .compressor import Compressor
from .tools import message_dialog, add_filechooser_filters, \
sizeof_fmt, get_file_type, create_image_from_file
from .tools import message_dialog, add_filechooser_filters, get_file_type, \
create_image_from_file
UI_PATH = '/com/github/huluti/Curtail/ui/'
SETTINGS_SCHEMA = 'com.github.huluti.Curtail'
@ -52,7 +53,7 @@ class CurtailWindow(Gtk.ApplicationWindow):
filechooser_button = Gtk.Template.Child()
toggle_lossy = Gtk.Template.Child()
apply_to_queue = False
results_model = Gio.ListStore.new(ResultItem)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@ -82,6 +83,7 @@ class CurtailWindow(Gtk.ApplicationWindow):
self.toggle_lossy.connect('notify::active', self.on_lossy_changed)
# Results
self.listbox.bind_model(self.results_model, self.create_result_row)
self.adjustment = self.scrolled_window.get_vadjustment()
def create_simple_action(self, action_name, callback, shortcut=None):
@ -107,47 +109,33 @@ class CurtailWindow(Gtk.ApplicationWindow):
self.resultbox.set_visible(False)
self.homebox.set_visible(True)
self.clear_button_headerbar.set_visible(False)
self.sync_ui()
def clear_results(self, *args):
self.show_results(False)
while child := self.resultbox.get_first_child():
self.resultbox.remove(child)
def go_end_scrolledwindow(self):
self.adjustment.set_value(self.adjustment.get_upper())
def sync_ui(self):
main_context = GLib.MainContext.default()
while main_context.pending():
main_context.iteration(False)
def clear_results(self, *args):
self.show_results(False)
self.results_model.remove_all()
def create_result_row(self, name, filename, new_filename, size):
def create_result_row(self, result_item):
row = Adw.ActionRow()
row.set_title(name)
row.set_tooltip_text(new_filename)
subtitle = sizeof_fmt(size)
row.set_subtitle(subtitle)
row.set_title(result_item.name)
row.set_tooltip_text(result_item.new_filename)
row.set_subtitle(result_item.size)
image = create_image_from_file(filename, 60, 60)
image = create_image_from_file(result_item.filename, 60, 60)
row.add_prefix(image)
self.listbox.insert(row, -1) # insert at end
self.sync_ui()
return row
def update_result_row(self, row, size, new_size, savings):
subtitle = sizeof_fmt(size) + ' -> ' + sizeof_fmt(new_size)
row.set_subtitle(subtitle)
savings_widget = Gtk.Label(label=str(savings) + '%')
color = 'success' if savings > 0 else 'error'
savings_widget.add_css_class(color)
savings_widget = Gtk.Label()
savings_widget.add_css_class('success')
row.add_suffix(savings_widget)
self.sync_ui()
result_item.bind_property('savings', savings_widget, 'label',
GObject.BindingFlags.DEFAULT)
result_item.bind_property('size', row, 'subtitle',
GObject.BindingFlags.DEFAULT)
return row
def set_saving_subtitle(self):
label = ''