Add possibility to remove portrait image

This commit is contained in:
0xb00bface 2021-08-15 14:29:38 +02:00
parent 28ca1932e9
commit 985ded70a8
1 changed files with 48 additions and 14 deletions

View File

@ -17,10 +17,14 @@ import org.slf4j.LoggerFactory;
import ctbrec.Config;
import ctbrec.Model;
import ctbrec.StringUtil;
import ctbrec.ui.controls.Dialogs;
import ctbrec.ui.controls.FileSelectionBox;
import javafx.geometry.Insets;
import javafx.scene.Cursor;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
public class SetPortraitAction {
private static final Logger LOG = LoggerFactory.getLogger(SetPortraitAction.class);
@ -40,27 +44,52 @@ public class SetPortraitAction {
source.setCursor(Cursor.WAIT);
String portraitId = Config.getInstance().getSettings().modelPortraits.getOrDefault(model.getUrl(), UUID.randomUUID().toString());
GridPane pane = new GridPane();
Label l = new Label("Select a portrait image. Leave empty to remove a portrait again.");
pane.add(l, 0, 0);
FileSelectionBox portraitSelectionBox = new FileSelectionBox();
boolean accepted = Dialogs.showCustomInput(source.getScene(), "Select a portrait image", portraitSelectionBox);
pane.add(portraitSelectionBox, 0, 1);
GridPane.setMargin(l, new Insets(5));
GridPane.setMargin(portraitSelectionBox, new Insets(5));
boolean accepted = Dialogs.showCustomInput(source.getScene(), "Select a portrait image", pane);
if (!accepted) {
source.setCursor(Cursor.DEFAULT);
return;
}
String selectedFile = portraitSelectionBox.fileProperty().getValue();
LOG.debug("User selected {}", selectedFile);
boolean success = processImageFile(portraitId, selectedFile);
if (success) {
Config.getInstance().getSettings().modelPortraits.put(model.getUrl(), portraitId);
try {
Config.getInstance().save();
runCallback();
} catch (IOException e) {
Dialogs.showError("Set Portrait", "Couldn't change portrait image: ", e);
if (StringUtil.isBlank(selectedFile)) {
removePortrait(portraitId);
} else {
LOG.debug("User selected {}", selectedFile);
boolean success = processImageFile(portraitId, selectedFile);
if (success) {
Config.getInstance().getSettings().modelPortraits.put(model.getUrl(), portraitId);
try {
Config.getInstance().save();
runCallback();
} catch (IOException e) {
Dialogs.showError("Set Portrait", "Couldn't change portrait image: ", e);
}
}
}
source.setCursor(Cursor.DEFAULT);
}
private void removePortrait(String portraitId) {
File portraitFile = getPortraitFile(portraitId);
try {
if (portraitFile.exists()) {
Files.delete(portraitFile.toPath());
}
Config.getInstance().getSettings().modelPortraits.remove(model.getUrl());
Config.getInstance().save();
runCallback();
} catch (IOException e) {
Dialogs.showError("Remove Portrait", "Couldn't remove portrait image: ", e);
}
}
private void runCallback() {
if (callback != null) {
try {
@ -98,11 +127,16 @@ public class SetPortraitAction {
}
private boolean copyToCacheAsJpg(String portraitId, BufferedImage portrait) throws IOException {
File configDir = Config.getInstance().getConfigDir();
File portraitDir = new File(configDir, "portraits");
Files.createDirectories(portraitDir.toPath());
File output = new File(portraitDir, portraitId + '.' + FORMAT);
File output = getPortraitFile(portraitId);
Files.createDirectories(output.getParentFile().toPath());
LOG.debug("Writing scaled portrait to {}", output);
return ImageIO.write(portrait, FORMAT, output);
}
private File getPortraitFile(String portraitId) {
File configDir = Config.getInstance().getConfigDir();
File portraitDir = new File(configDir, "portraits");
File output = new File(portraitDir, portraitId + '.' + FORMAT);
return output;
}
}