From 6adba374f85a8ad2c3907370600f734cf072da68 Mon Sep 17 00:00:00 2001 From: gergelyszaz Date: Wed, 20 Sep 2023 21:49:07 +0200 Subject: [PATCH] Create new styles with indexed filenames --- ai_diffusion/style.py | 12 +++++++++--- ai_diffusion/ui/settings.py | 12 +++++------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/ai_diffusion/style.py b/ai_diffusion/style.py index e7517d71c..75ee1be15 100644 --- a/ai_diffusion/style.py +++ b/ai_diffusion/style.py @@ -1,6 +1,5 @@ from enum import Enum import json -import os from pathlib import Path from PyQt5.QtCore import QObject, pyqtSignal @@ -195,7 +194,14 @@ def __init__(self, folder: Path = default_folder): def default(self): return self[0] - def create(self, name: str) -> Style: + def create(self, name: str = "style") -> Style: + if Path(self.folder / f"{name}.json").exists(): + i = 1 + basename = name + while Path(self.folder / f"{basename}_{i}.json").exists(): + i += 1 + name = f"{basename}_{i}" + new_style = Style(self.folder / f"{name}.json") new_style.name = name self._list.append(new_style) @@ -205,7 +211,7 @@ def create(self, name: str) -> Style: def delete(self, style: Style): self._list.remove(style) - os.remove(style.filepath) + style.filepath.unlink() self.changed.emit() def reload(self): diff --git a/ai_diffusion/ui/settings.py b/ai_diffusion/ui/settings.py index 9c8d08cae..4f269be8e 100644 --- a/ai_diffusion/ui/settings.py +++ b/ai_diffusion/ui/settings.py @@ -5,7 +5,6 @@ QVBoxLayout, QHBoxLayout, QDialog, - QInputDialog, QPushButton, QCheckBox, QFrame, @@ -628,14 +627,13 @@ def current_style(self, style: Style): def update_model_lists(self): self._read() - + def _create_style(self): - filename, ok = QInputDialog.getText(self, "New style", "") - if ok and filename: - self.current_style = Styles.list().create(filename) - self._change_style() + # make sure the new style is in the combobox before setting it as the current style + new_style = Styles.list().create() self._update_style_list() - + self.current_style = new_style + def _delete_style(self): Styles.list().delete(self.current_style) self._update_style_list()