Add possibility to remove portrait image
This commit is contained in:
parent
28ca1932e9
commit
985ded70a8
|
@ -17,10 +17,14 @@ import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import ctbrec.Config;
|
import ctbrec.Config;
|
||||||
import ctbrec.Model;
|
import ctbrec.Model;
|
||||||
|
import ctbrec.StringUtil;
|
||||||
import ctbrec.ui.controls.Dialogs;
|
import ctbrec.ui.controls.Dialogs;
|
||||||
import ctbrec.ui.controls.FileSelectionBox;
|
import ctbrec.ui.controls.FileSelectionBox;
|
||||||
|
import javafx.geometry.Insets;
|
||||||
import javafx.scene.Cursor;
|
import javafx.scene.Cursor;
|
||||||
import javafx.scene.Node;
|
import javafx.scene.Node;
|
||||||
|
import javafx.scene.control.Label;
|
||||||
|
import javafx.scene.layout.GridPane;
|
||||||
|
|
||||||
public class SetPortraitAction {
|
public class SetPortraitAction {
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(SetPortraitAction.class);
|
private static final Logger LOG = LoggerFactory.getLogger(SetPortraitAction.class);
|
||||||
|
@ -40,27 +44,52 @@ public class SetPortraitAction {
|
||||||
source.setCursor(Cursor.WAIT);
|
source.setCursor(Cursor.WAIT);
|
||||||
String portraitId = Config.getInstance().getSettings().modelPortraits.getOrDefault(model.getUrl(), UUID.randomUUID().toString());
|
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();
|
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) {
|
if (!accepted) {
|
||||||
|
source.setCursor(Cursor.DEFAULT);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String selectedFile = portraitSelectionBox.fileProperty().getValue();
|
String selectedFile = portraitSelectionBox.fileProperty().getValue();
|
||||||
|
|
||||||
LOG.debug("User selected {}", selectedFile);
|
if (StringUtil.isBlank(selectedFile)) {
|
||||||
boolean success = processImageFile(portraitId, selectedFile);
|
removePortrait(portraitId);
|
||||||
if (success) {
|
} else {
|
||||||
Config.getInstance().getSettings().modelPortraits.put(model.getUrl(), portraitId);
|
LOG.debug("User selected {}", selectedFile);
|
||||||
try {
|
boolean success = processImageFile(portraitId, selectedFile);
|
||||||
Config.getInstance().save();
|
if (success) {
|
||||||
runCallback();
|
Config.getInstance().getSettings().modelPortraits.put(model.getUrl(), portraitId);
|
||||||
} catch (IOException e) {
|
try {
|
||||||
Dialogs.showError("Set Portrait", "Couldn't change portrait image: ", e);
|
Config.getInstance().save();
|
||||||
|
runCallback();
|
||||||
|
} catch (IOException e) {
|
||||||
|
Dialogs.showError("Set Portrait", "Couldn't change portrait image: ", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
source.setCursor(Cursor.DEFAULT);
|
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() {
|
private void runCallback() {
|
||||||
if (callback != null) {
|
if (callback != null) {
|
||||||
try {
|
try {
|
||||||
|
@ -98,11 +127,16 @@ public class SetPortraitAction {
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean copyToCacheAsJpg(String portraitId, BufferedImage portrait) throws IOException {
|
private boolean copyToCacheAsJpg(String portraitId, BufferedImage portrait) throws IOException {
|
||||||
File configDir = Config.getInstance().getConfigDir();
|
File output = getPortraitFile(portraitId);
|
||||||
File portraitDir = new File(configDir, "portraits");
|
Files.createDirectories(output.getParentFile().toPath());
|
||||||
Files.createDirectories(portraitDir.toPath());
|
|
||||||
File output = new File(portraitDir, portraitId + '.' + FORMAT);
|
|
||||||
LOG.debug("Writing scaled portrait to {}", output);
|
LOG.debug("Writing scaled portrait to {}", output);
|
||||||
return ImageIO.write(portrait, FORMAT, 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue