modify MovieEditor

This commit is contained in:
nisihara1 2017-05-17 00:24:12 +09:00
parent c7c76144e1
commit f9b7336865
3 changed files with 283 additions and 194 deletions

View File

@ -0,0 +1,34 @@
/*
* Copyright (C) 2017 Satomichi Nishihara
*
* This file is distributed under the terms of the
* GNU General Public License. See the file `LICENSE'
* in the root directory of the present distribution,
* or http://www.gnu.org/copyleft/gpl.txt .
*/
package burai.app.project.editor.result.movie;
import burai.app.QEFXMainController;
import burai.project.Project;
public class MovieMaker {
private QEFXMainController mainController;
private Project project;
protected MovieMaker(QEFXMainController mainController, Project project) {
if (mainController == null) {
throw new IllegalArgumentException("mainController is null.");
}
if (project == null) {
throw new IllegalArgumentException("project is null.");
}
this.mainController = mainController;
this.project = project;
}
}

View File

@ -0,0 +1,220 @@
/*
* Copyright (C) 2017 Satomichi Nishihara
*
* This file is distributed under the terms of the
* GNU General Public License. See the file `LICENSE'
* in the root directory of the present distribution,
* or http://www.gnu.org/copyleft/gpl.txt .
*/
package burai.app.project.editor.result.movie;
import java.io.File;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import burai.app.QEFXMain;
import burai.app.QEFXMainController;
import burai.atoms.model.Atom;
import burai.atoms.model.Cell;
import burai.atoms.model.exception.ZeroVolumCellException;
import burai.com.consts.Constants;
import burai.com.env.Environments;
import burai.com.math.Matrix3D;
import burai.project.Project;
import burai.project.property.ProjectGeometry;
public class ProjectExporter {
private QEFXMainController mainController;
private Project project;
private ProjectGeometry geometry;
protected ProjectExporter(QEFXMainController mainController, Project project, ProjectGeometry geometry) {
if (mainController == null) {
throw new IllegalArgumentException("mainController is null.");
}
if (project == null) {
throw new IllegalArgumentException("project is null.");
}
if (geometry == null) {
throw new IllegalArgumentException("geometry is null.");
}
this.mainController = mainController;
this.project = project;
this.geometry = geometry;
}
protected void exportProject() {
Project project = this.saveNewProject();
if (project == null) {
return;
}
boolean status = this.editProject(project);
if (!status) {
System.err.println("cannot edit project.");
return;
}
if (this.mainController != null) {
this.mainController.showProject(project);
}
}
private Project saveNewProject() {
File directory = null;
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("New project");
String projPath = this.project == null ? null : this.project.getDirectoryPath();
projPath = projPath == null ? null : projPath.trim();
File projDir = null;
if (projPath != null && !(projPath.isEmpty())) {
projDir = new File(projPath);
}
File initDir = projDir == null ? null : projDir.getParentFile();
String initPath = initDir == null ? null : initDir.getPath();
if (initDir == null || initPath == null || initPath.trim().isEmpty()) {
initPath = Environments.getProjectsPath();
initDir = new File(initPath);
}
if (initDir != null) {
try {
if (initDir.isDirectory()) {
fileChooser.setInitialDirectory(initDir);
}
} catch (Exception e) {
e.printStackTrace();
}
}
Stage stage = this.mainController == null ? null : this.mainController.getStage();
if (stage != null) {
directory = fileChooser.showSaveDialog(stage);
}
if (directory == null) {
return null;
}
try {
if (directory.exists()) {
Alert alert = new Alert(AlertType.ERROR);
QEFXMain.initializeDialogOwner(alert);
alert.setHeaderText(directory.getName() + " already exists.");
alert.showAndWait();
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
Project project = null;
if (this.project != null) {
project = this.project.cloneProject(directory);
}
if (project == null) {
Alert alert = new Alert(AlertType.ERROR);
QEFXMain.initializeDialogOwner(alert);
alert.setHeaderText("Cannot create project: " + directory.getPath());
alert.showAndWait();
return null;
}
return project;
}
private boolean editProject(Project project) {
Cell cell = project == null ? null : project.getCell();
if (cell == null) {
return false;
}
if (this.geometry == null) {
return false;
}
double[][] lattice = this.geometry.getCell();
lattice = Matrix3D.mult(Constants.BOHR_RADIUS_ANGS, lattice);
if (lattice == null || lattice.length < 3) {
return false;
}
if (lattice[0] == null || lattice[0].length < 3) {
return false;
}
if (lattice[1] == null || lattice[1].length < 3) {
return false;
}
if (lattice[2] == null || lattice[2].length < 3) {
return false;
}
try {
cell.moveLattice(lattice);
} catch (ZeroVolumCellException e) {
e.printStackTrace();
return false;
}
int natom = this.geometry.numAtoms();
int natom_ = cell.numAtoms(true);
Atom[] refAtoms = null;
if (natom == natom_) {
refAtoms = cell.listAtoms(true);
}
if (refAtoms != null && refAtoms.length >= natom) {
for (int i = 0; i < natom; i++) {
String name = this.geometry.getName(i);
if (name == null || name.trim().isEmpty()) {
continue;
}
double x = this.geometry.getX(i) * Constants.BOHR_RADIUS_ANGS;
double y = this.geometry.getY(i) * Constants.BOHR_RADIUS_ANGS;
double z = this.geometry.getZ(i) * Constants.BOHR_RADIUS_ANGS;
Atom atom = refAtoms[i];
if (atom == null) {
cell.addAtom(new Atom(name, x, y, z));
} else {
atom.setName(name);
atom.moveTo(x, y, z);
}
}
} else {
cell.removeAllAtoms();
for (int i = 0; i < natom; i++) {
String name = this.geometry.getName(i);
if (name == null || name.trim().isEmpty()) {
continue;
}
double x = this.geometry.getX(i) * Constants.BOHR_RADIUS_ANGS;
double y = this.geometry.getY(i) * Constants.BOHR_RADIUS_ANGS;
double z = this.geometry.getZ(i) * Constants.BOHR_RADIUS_ANGS;
cell.addAtom(new Atom(name, x, y, z));
}
}
project.saveQEInputs();
return true;
}
}

View File

@ -9,8 +9,6 @@
package burai.app.project.editor.result.movie;
import java.io.File;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
@ -18,17 +16,11 @@ import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import burai.app.QEFXMain;
import burai.app.project.QEFXProjectController;
import burai.app.project.editor.result.QEFXResultEditorController;
import burai.app.project.viewer.result.movie.QEFXMovieViewerController;
import burai.atoms.model.Atom;
import burai.atoms.model.Cell;
import burai.atoms.model.exception.ZeroVolumCellException;
import burai.com.consts.Constants;
import burai.com.env.Environments;
import burai.com.graphic.svg.SVGLibrary;
import burai.com.graphic.svg.SVGLibrary.SVGData;
import burai.com.math.Matrix3D;
@ -45,6 +37,9 @@ public class QEFXMovieEditorController extends QEFXResultEditorController<QEFXMo
@FXML
private Button movieButton;
@FXML
private Button exportButton;
@FXML
private TextField numberField;
@ -56,9 +51,6 @@ public class QEFXMovieEditorController extends QEFXResultEditorController<QEFXMo
@FXML
private Label timeLabel;
@FXML
private Button exportButton;
@FXML
private TextArea atomArea;
@ -79,8 +71,8 @@ public class QEFXMovieEditorController extends QEFXResultEditorController<QEFXMo
@Override
protected void setupFXComponents() {
this.setupMovieButton();
this.setupNumberField();
this.setupExportButton();
this.setupNumberField();
this.setupAtomArea();
}
@ -93,8 +85,31 @@ public class QEFXMovieEditorController extends QEFXResultEditorController<QEFXMo
this.movieButton.setGraphic(SVGLibrary.getGraphic(SVGData.MOVIE, GRAPHIC_SIZE, null, GRAPHIC_CLASS));
this.movieButton.setOnAction(event -> {
// TODO
});
if (this.mainController != null && this.project != null) {
MovieMaker movieMaker = new MovieMaker(this.mainController, this.project);
}
});
}
private void setupExportButton() {
if (this.exportButton == null) {
return;
}
this.exportButton.getStyleClass().add(GRAPHIC_CLASS);
this.exportButton.setGraphic(SVGLibrary.getGraphic(SVGData.EXPORT, GRAPHIC_SIZE, null, GRAPHIC_CLASS));
this.exportButton.setOnAction(event -> {
ProjectGeometry geometry = null;
if (this.viewerController != null) {
geometry = this.viewerController.getGeometry();
}
if (this.mainController != null && this.project != null && geometry != null) {
ProjectExporter exporter = new ProjectExporter(this.mainController, this.project, geometry);
exporter.exportProject();
}
});
}
private void setupNumberField() {
@ -143,186 +158,6 @@ public class QEFXMovieEditorController extends QEFXResultEditorController<QEFXMo
});
}
private void setupExportButton() {
if (this.exportButton == null) {
return;
}
this.exportButton.getStyleClass().add(GRAPHIC_CLASS);
this.exportButton.setGraphic(SVGLibrary.getGraphic(SVGData.EXPORT, GRAPHIC_SIZE, null, GRAPHIC_CLASS));
this.exportButton.setOnAction(event -> {
Project project = this.saveNewProject();
if (project == null) {
return;
}
boolean status = this.editProject(project);
if (!status) {
System.err.println("cannot edit project.");
return;
}
if (this.mainController != null) {
this.mainController.showProject(project);
}
});
}
public Project saveNewProject() {
File directory = null;
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("New project");
String projPath = this.project == null ? null : this.project.getDirectoryPath();
projPath = projPath == null ? null : projPath.trim();
File projDir = null;
if (projPath != null && !(projPath.isEmpty())) {
projDir = new File(projPath);
}
File initDir = projDir == null ? null : projDir.getParentFile();
String initPath = initDir == null ? null : initDir.getPath();
if (initDir == null || initPath == null || initPath.trim().isEmpty()) {
initPath = Environments.getProjectsPath();
initDir = new File(initPath);
}
if (initDir != null) {
try {
if (initDir.isDirectory()) {
fileChooser.setInitialDirectory(initDir);
}
} catch (Exception e) {
e.printStackTrace();
}
}
Stage stage = this.getStage();
if (stage != null) {
directory = fileChooser.showSaveDialog(stage);
}
if (directory == null) {
return null;
}
try {
if (directory.exists()) {
Alert alert = new Alert(AlertType.ERROR);
QEFXMain.initializeDialogOwner(alert);
alert.setHeaderText(directory.getName() + " already exists.");
alert.showAndWait();
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
Project project = null;
if (this.project != null) {
project = this.project.cloneProject(directory);
}
if (project == null) {
Alert alert = new Alert(AlertType.ERROR);
QEFXMain.initializeDialogOwner(alert);
alert.setHeaderText("Cannot create project: " + directory.getPath());
alert.showAndWait();
return null;
}
return project;
}
private boolean editProject(Project project) {
Cell cell = project == null ? null : project.getCell();
if (cell == null) {
return false;
}
ProjectGeometry geometry = null;
if (this.viewerController != null) {
geometry = this.viewerController.getGeometry();
}
if (geometry == null) {
return false;
}
double[][] lattice = geometry.getCell();
lattice = Matrix3D.mult(Constants.BOHR_RADIUS_ANGS, lattice);
if (lattice == null || lattice.length < 3) {
return false;
}
if (lattice[0] == null || lattice[0].length < 3) {
return false;
}
if (lattice[1] == null || lattice[1].length < 3) {
return false;
}
if (lattice[2] == null || lattice[2].length < 3) {
return false;
}
try {
cell.moveLattice(lattice);
} catch (ZeroVolumCellException e) {
e.printStackTrace();
return false;
}
int natom = geometry.numAtoms();
int natom_ = cell.numAtoms(true);
Atom[] refAtoms = null;
if (natom == natom_) {
refAtoms = cell.listAtoms(true);
}
if (refAtoms != null && refAtoms.length >= natom) {
for (int i = 0; i < natom; i++) {
String name = geometry.getName(i);
if (name == null || name.trim().isEmpty()) {
continue;
}
double x = geometry.getX(i) * Constants.BOHR_RADIUS_ANGS;
double y = geometry.getY(i) * Constants.BOHR_RADIUS_ANGS;
double z = geometry.getZ(i) * Constants.BOHR_RADIUS_ANGS;
Atom atom = refAtoms[i];
if (atom == null) {
cell.addAtom(new Atom(name, x, y, z));
} else {
atom.setName(name);
atom.moveTo(x, y, z);
}
}
} else {
cell.removeAllAtoms();
for (int i = 0; i < natom; i++) {
String name = geometry.getName(i);
if (name == null || name.trim().isEmpty()) {
continue;
}
double x = geometry.getX(i) * Constants.BOHR_RADIUS_ANGS;
double y = geometry.getY(i) * Constants.BOHR_RADIUS_ANGS;
double z = geometry.getZ(i) * Constants.BOHR_RADIUS_ANGS;
cell.addAtom(new Atom(name, x, y, z));
}
}
project.saveQEInputs();
return true;
}
private void setupAtomArea() {
if (this.atomArea == null) {
return;