From ac946dd6349328c62b8bb722868e5d5c3506b925 Mon Sep 17 00:00:00 2001 From: abujazar Date: Thu, 5 Sep 2024 11:24:54 +0200 Subject: [PATCH] allowing the user to save json file --- gui/femmt_gui.py | 71 +++++++++++++++++++++++++++++++++++++++++++++++- gui/femmt_gui.ui | 34 ++++++++++++++++------- 2 files changed, 94 insertions(+), 11 deletions(-) diff --git a/gui/femmt_gui.py b/gui/femmt_gui.py index 6debe55f..541bcfd8 100644 --- a/gui/femmt_gui.py +++ b/gui/femmt_gui.py @@ -5,7 +5,7 @@ from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg, NavigationToolbar2QT from mpl_toolkits.axes_grid1 import make_axes_locatable from matplotlib import cm -from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QMessageBox +from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QMessageBox, QFileDialog, QInputDialog from PyQt5 import QtCore, uic, QtWidgets from PyQt5.QtGui import QPixmap, QDoubleValidator, QIntValidator import femmt as fmt @@ -14,6 +14,7 @@ from typing import List import PIL import webbrowser +import shutil # new import for threads from PyQt5.QtCore import QObject, pyqtSignal, pyqtSlot, QThread, QCoreApplication, QMutex @@ -257,6 +258,10 @@ def __init__(self, parent=None): self.action_documentation.triggered.connect(self.webbrowser_documentation) self.action_report_bug.triggered.connect(self.webbrowser_bugreport) + # ## Save electro-magnetic and thermal simulation results + self.md_pushButton_simulation_result.clicked.connect(self.save_results_to_directory) + self.md_pushButton_thermal_simulation_result.clicked.connect(self.save_results_to_directory) + "******* Manual Design *********" "Signals in Definition Tab" @@ -736,6 +741,70 @@ def webbrowser_documentation(self): """Open the web browser to the FEMMT documentation.""" webbrowser.open('https://upb-lea.github.io/FEM_Magnetics_Toolbox/') + def save_results_to_directory(self): + """Open a dialog for the user to select a directory and saves the results from the default GUI working directory to the selected directory.""" + options = QFileDialog.Options() + selected_directory = QFileDialog.getExistingDirectory(self, "Select Directory to Save Results", options=options) + + if selected_directory: + # Define the source directory (where the results are stored by default) + source_directory = os.path.join(self.default_gui_working_directory, "results") + + # Check if the source directory exists + if not os.path.exists(source_directory): + QMessageBox.warning(self, "No Results Found", "The results directory does not exist. There are no results to save.") + return + + # Check if the source directory contains any .json files + json_files = [f for f in os.listdir(source_directory) if f.endswith('.json')] + + if not json_files: + QMessageBox.warning(self, "No Results Found", "There are no .json result files in the current working directory.") + return + + # Copy files + try: + self.copy_results_to_directory(source_directory, selected_directory) + self.statusBar().showMessage(f"Results saved to: {selected_directory}", 5000) + except Exception as e: + self.statusBar().showMessage(f"Error saving results: {str(e)}", 5000) + + def copy_results_to_directory(self, source_directory, target_directory): + """ + Copy JSON files from the source directory to the target directory. + + :param source_directory: The path to the directory where the results are stored. + :type source_directory: str + :param target_directory: The path to the directory where the results should be copied to. + :type target_directory: str + """ + # Create the target directory if it doesn't exist + if not os.path.exists(target_directory): + os.makedirs(target_directory) + + # Copy only .json files from the source to the target + for item in os.listdir(source_directory): + if item.endswith('.json'): # Check if the file is a .json file + source_item = os.path.join(source_directory, item) + target_item = os.path.join(target_directory, item) + + # Check if the file already exists in the target directory + if os.path.exists(target_item): + # Prompt user to rename the file + new_name, ok = QInputDialog.getText(self, "File Exists", + f"The file '{item}' already exists. Please enter a new name:") + if ok and new_name: + # Ensure the new name ends with .json + if not new_name.endswith('.json'): + new_name += '.json' + target_item = os.path.join(target_directory, new_name) + else: + # If the user cancels the dialog or does not provide a new name, skip copying this file + continue + + # Copy the JSON file + shutil.copy2(source_item, target_item) + # **************************** Automated design tab ************************************************************ # def plot_volume_loss(self, data_matrix, matplotlib_widget: MatplotlibWidget): diff --git a/gui/femmt_gui.ui b/gui/femmt_gui.ui index a6cac1cd..89fc4664 100644 --- a/gui/femmt_gui.ui +++ b/gui/femmt_gui.ui @@ -36,7 +36,7 @@ 0 - -164 + 0 1508 1280 @@ -61,7 +61,7 @@ - 2 + 3 @@ -2022,6 +2022,13 @@ Simulation Text Output + + + + Save electrical simulation result (JSON) + + + @@ -2775,6 +2782,13 @@ + + + + Save thermal simulation result (JSON) + + + @@ -4470,8 +4484,8 @@ 0 0 - 694 - 301 + 1440 + 1172 @@ -4707,8 +4721,8 @@ 0 0 - 635 - 518 + 1398 + 755 @@ -4759,8 +4773,8 @@ 0 0 - 547 - 221 + 1440 + 1172 @@ -4879,8 +4893,8 @@ 0 0 - 468 - 518 + 1378 + 1020