Skip to content

Commit

Permalink
Create new styles with indexed filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
gergelyszaz authored and Acly committed Sep 22, 2023
1 parent b286249 commit 6adba37
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
12 changes: 9 additions & 3 deletions ai_diffusion/style.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from enum import Enum
import json
import os
from pathlib import Path
from PyQt5.QtCore import QObject, pyqtSignal

Expand Down Expand Up @@ -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)
Expand All @@ -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):
Expand Down
12 changes: 5 additions & 7 deletions ai_diffusion/ui/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
QVBoxLayout,
QHBoxLayout,
QDialog,
QInputDialog,
QPushButton,
QCheckBox,
QFrame,
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 6adba37

Please sign in to comment.