From 00cfb32ab33f50a035728406716223134761ca2b Mon Sep 17 00:00:00 2001 From: 0xMRTT <0xMRTT@proton.me> Date: Fri, 26 May 2023 17:42:14 +0200 Subject: [PATCH] feat: add custom filename --- data/ui/window.blp | 62 +++++++++++++++++++++++++--------------------- src/main.py | 33 ++++++++++++++++++------ src/window.py | 19 +++++++------- 3 files changed, 69 insertions(+), 45 deletions(-) diff --git a/data/ui/window.blp b/data/ui/window.blp index a1b610f..0cf31b3 100644 --- a/data/ui/window.blp +++ b/data/ui/window.blp @@ -90,37 +90,37 @@ template $ImaginerWindow : Adw.ApplicationWindow { } } - Adw.ActionRow image_size { - title: _("Image Size"); + // Adw.ActionRow image_size { + // title: _("Image Size"); - Gtk.Box { - orientation: horizontal; - spacing: 4; - valign: center; + // Gtk.Box { + // orientation: horizontal; + // spacing: 4; + // valign: center; - Gtk.SpinButton image_width_spinbutton { - tooltip-text: _("Image Width"); - adjustment: Gtk.Adjustment image_width_adjustment { - lower: 1; - upper: 8192; - step-increment: 1; - page-increment: 5; - }; - value-changed => on_image_width_changed(); - } + // Gtk.SpinButton image_width_spinbutton { + // tooltip-text: _("Image Width"); + // adjustment: Gtk.Adjustment image_width_adjustment { + // lower: 1; + // upper: 8192; + // step-increment: 1; + // page-increment: 5; + // }; + // value-changed => on_image_width_changed(); + // } - Gtk.SpinButton image_height_spinbutton { - tooltip-text: _("Image Height"); - adjustment: Gtk.Adjustment image_height_adjustment { - lower: 1; - upper: 8192; - step-increment: 1; - page-increment: 5; - }; - value-changed => on_image_height_changed(); - } - } - } + // Gtk.SpinButton image_height_spinbutton { + // tooltip-text: _("Image Height"); + // adjustment: Gtk.Adjustment image_height_adjustment { + // lower: 1; + // upper: 8192; + // step-increment: 1; + // page-increment: 5; + // }; + // value-changed => on_image_height_changed(); + // } + // } + // } } Adw.PreferencesGroup { @@ -128,6 +128,12 @@ template $ImaginerWindow : Adw.ApplicationWindow { margin-start: 12; margin-end: 12; + Adw.EntryRow filename_prompt { + title: _("Filename"); + + text: _("bavarder-%d-%b-%Y"); + } + Adw.ActionRow { title: _("Save Location"); activatable-widget: button_output; diff --git a/src/main.py b/src/main.py index 643253d..90629e7 100644 --- a/src/main.py +++ b/src/main.py @@ -40,6 +40,7 @@ from tempfile import NamedTemporaryFile import unicodedata from time import gmtime, strftime from os.path import basename, splitext +from random import randint from .provider import PROVIDERS import platform @@ -310,16 +311,28 @@ Providers: {self.enabled_providers} def on_ask_action(self, widget, _): """Callback for the app.ask action.""" + self.win.banner.set_revealed(False) self.prompt = self.win.prompt.get_text() self.negative_prompt = self.win.negative_prompt.get_text() + self.filename = self.win.filename_prompt.get_text() try: - self.path = self.file_path + self.path = self.file_path + "/" except AttributeError: self.path = "imaginer" else: - self.path = f"{self.path}/imaginer-{self.slugify(self.prompt)}-{strftime('%d-%b-%Y-%H-%M-%S', gmtime())}" + self.path += strftime(self.filename, gmtime()) + self.path.replace(" ", "_") + + try: + print(self.path) + if self.previous_path == self.path: + self.path += f"_{randint(0, 1000)}" + except AttributeError: # no previous path + pass + finally: + self.previous_path = self.path if self.prompt == "" or self.prompt is None: # empty prompt return @@ -344,13 +357,17 @@ Providers: {self.enabled_providers} if image: self.win.banner.set_revealed(False) - image.save(path) - self.win.image.set_file(Gio.File.new_for_path(path)) - self.win.image.set_visible(True) - print("Image saved") + try: + image.save(path) + except OSError as e: + self.win.banner.set_title(str(e)) + self.win.banner.set_revealed(True) + finally: + path = self.providers[self.provider].path("imaginer") + self.win.image.set_file(Gio.File.new_for_path(path)) + self.win.image.set_visible(True) else: - print("No image returned") - + self.win.banner.set_title(_("No image found")) self.t = KillableThread(target=thread_run) self.t.start() diff --git a/src/window.py b/src/window.py index 64fd36c..ea43827 100644 --- a/src/window.py +++ b/src/window.py @@ -36,8 +36,9 @@ class ImaginerWindow(Adw.ApplicationWindow): negative_prompt = Gtk.Template.Child() menu = Gtk.Template.Child() label_output = Gtk.Template.Child() - image_width_spinbutton = Gtk.Template.Child() - image_height_spinbutton = Gtk.Template.Child() + filename_prompt = Gtk.Template.Child() + #image_width_spinbutton = Gtk.Template.Child() + #image_height_spinbutton = Gtk.Template.Child() def __init__(self, **kwargs): super().__init__(**kwargs) @@ -62,11 +63,11 @@ class ImaginerWindow(Adw.ApplicationWindow): "is-fullscreen", self, "fullscreened", Gio.SettingsBindFlags.DEFAULT ) - @Gtk.Template.Callback() - def on_image_width_changed(self, widget): - self.app.width = self.image_width_spinbutton.get_value_as_int() - + # @Gtk.Template.Callback() + # def on_image_width_changed(self, widget): + # self.app.width = self.image_width_spinbutton.get_value_as_int() + # - @Gtk.Template.Callback() - def on_image_height_changed(self, widget): - self.app.height = self.image_height_spinbutton.get_value_as_int() + # @Gtk.Template.Callback() + # def on_image_height_changed(self, widget): + # self.app.height = self.image_height_spinbutton.get_value_as_int()