Skip to content

Commit

Permalink
allowing the user to save json file
Browse files Browse the repository at this point in the history
  • Loading branch information
abujazar committed Sep 5, 2024
1 parent f7cd2f4 commit ac946dd
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 11 deletions.
71 changes: 70 additions & 1 deletion gui/femmt_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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):
Expand Down
34 changes: 24 additions & 10 deletions gui/femmt_gui.ui
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<property name="geometry">
<rect>
<x>0</x>
<y>-164</y>
<y>0</y>
<width>1508</width>
<height>1280</height>
</rect>
Expand All @@ -61,7 +61,7 @@
</size>
</property>
<property name="currentIndex">
<number>2</number>
<number>3</number>
</property>
<widget class="QWidget" name="md_QWidget">
<attribute name="title">
Expand Down Expand Up @@ -2022,6 +2022,13 @@
<string>Simulation Text Output</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_17">
<item>
<widget class="QPushButton" name="md_pushButton_simulation_result">
<property name="text">
<string>Save electrical simulation result (JSON)</string>
</property>
</widget>
</item>
<item>
<widget class="QScrollArea" name="scrollArea_11">
<property name="widgetResizable">
Expand Down Expand Up @@ -2775,6 +2782,13 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="md_pushButton_thermal_simulation_result">
<property name="text">
<string>Save thermal simulation result (JSON)</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_21">
<property name="orientation">
Expand Down Expand Up @@ -4470,8 +4484,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>694</width>
<height>301</height>
<width>1440</width>
<height>1172</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_44">
Expand Down Expand Up @@ -4707,8 +4721,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>635</width>
<height>518</height>
<width>1398</width>
<height>755</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_13">
Expand Down Expand Up @@ -4759,8 +4773,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>547</width>
<height>221</height>
<width>1440</width>
<height>1172</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_48">
Expand Down Expand Up @@ -4879,8 +4893,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>468</width>
<height>518</height>
<width>1378</width>
<height>1020</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_10">
Expand Down

0 comments on commit ac946dd

Please sign in to comment.