Skip to content

Commit 4f353cc

Browse files
authored
Merge pull request #99 from kkiesling/separate_model_view
Separate model view from view settings for more reliable reload
2 parents 5691d44 + 9ae7a76 commit 4f353cc

2 files changed

Lines changed: 280 additions & 162 deletions

File tree

openmc_plotter/main_window.py

Lines changed: 16 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import os
44
import pickle
55
from threading import Thread
6-
import hashlib
76

87
from PySide2 import QtCore, QtGui
98
from PySide2.QtGui import QKeyEvent
@@ -21,7 +20,7 @@
2120
except ImportError:
2221
_HAVE_VTK = False
2322

24-
from .plotmodel import PlotModel, DomainTableModel
23+
from .plotmodel import PlotModel, DomainTableModel, hash_file
2524
from .plotgui import PlotImage, ColorDialog
2625
from .docks import DomainDock, TallyDock
2726
from .overlays import ShortcutsOverlay
@@ -41,17 +40,6 @@ def _openmcReload(threads=None):
4140
openmc.lib.init(args)
4241
openmc.lib.settings.verbosity = 1
4342

44-
def hash_file(filename):
45-
# return the md5 hash of a file
46-
h = hashlib.md5()
47-
with open(filename,'rb') as file:
48-
chunk = 0
49-
while chunk != b'':
50-
# read 32768 bytes at a time
51-
chunk = file.read(32768)
52-
h.update(chunk)
53-
return h.hexdigest()
54-
5543
class MainWindow(QMainWindow):
5644
def __init__(self,
5745
font=QtGui.QFontMetrics(QtGui.QFont()),
@@ -463,10 +451,8 @@ def loadModel(self, reload=False, use_settings_pkl=True):
463451
if reload:
464452
self.resetModels()
465453
else:
466-
# create new plot model
467-
self.model = PlotModel()
468-
if use_settings_pkl:
469-
self.restoreModelSettings()
454+
self.model = PlotModel(use_settings_pkl)
455+
470456
# update plot and model settings
471457
self.updateRelativeBases()
472458

@@ -1074,51 +1060,6 @@ def restoreWindowSettings(self):
10741060

10751061
self.colorDialog.setVisible(is_visible)
10761062

1077-
def restoreModelSettings(self):
1078-
if os.path.isfile("plot_settings.pkl"):
1079-
1080-
with open('plot_settings.pkl', 'rb') as file:
1081-
model = pickle.load(file)
1082-
1083-
# check if loaded cell/mat ids hash match the pkl file:
1084-
current_mat_xml_hash = hash_file('materials.xml')
1085-
current_geom_xml_hash = hash_file('geometry.xml')
1086-
if (current_mat_xml_hash != model.mat_xml_hash) or \
1087-
(current_geom_xml_hash != model.geom_xml_hash):
1088-
# hashes do not match so ignore plot_settings.pkl file
1089-
msg_box = QMessageBox()
1090-
msg = "WARNING: Model has changed since storing plot " +\
1091-
"settings. Ignoring previous plot settings."
1092-
msg_box.setText(msg)
1093-
msg_box.setIcon(QMessageBox.Warning)
1094-
msg_box.setStandardButtons(QMessageBox.Ok)
1095-
msg_box.exec_()
1096-
return
1097-
1098-
# do not replace model if the version is out of date
1099-
if model.version != self.model.version:
1100-
print("WARNING: previous plot settings are for a different "
1101-
"version of the GUI. They will be ignored.")
1102-
wrn_msg = "Existing version: {}, Current GUI version: {}"
1103-
print(wrn_msg.format(model.version, self.model.version))
1104-
return
1105-
1106-
try:
1107-
self.model.statepoint = model.statepoint
1108-
except OSError:
1109-
msg_box = QMessageBox()
1110-
msg = "Could not open statepoint file: \n\n {} \n"
1111-
msg_box.setText(msg.format(self.model.statepoint.filename))
1112-
msg_box.setIcon(QMessageBox.Warning)
1113-
msg_box.setStandardButtons(QMessageBox.Ok)
1114-
msg_box.exec_()
1115-
self.model.statepoint = None
1116-
1117-
self.model.currentView = model.currentView
1118-
self.model.activeView = copy.deepcopy(model.currentView)
1119-
self.model.previousViews = model.previousViews
1120-
self.model.subsequentViews = model.subsequentViews
1121-
11221063
def resetModels(self):
11231064
self.cellsModel = DomainTableModel(self.model.activeView.cells)
11241065
self.materialsModel = DomainTableModel(self.model.activeView.materials)
@@ -1209,20 +1150,22 @@ def closeEvent(self, event):
12091150
self.saveSettings()
12101151

12111152
def saveSettings(self):
1212-
1213-
if len(self.model.previousViews) > 10:
1214-
self.model.previousViews = self.model.previousViews[-10:]
1215-
if len(self.model.subsequentViews) > 10:
1216-
self.model.subsequentViews = self.model.subsequentViews[-10:]
1153+
if self.model.statepoint:
1154+
self.model.statepoint.close()
12171155

12181156
# get hashes for geometry.xml and material.xml at close
1219-
self.model.mat_xml_hash = hash_file('materials.xml')
1220-
self.model.geom_xml_hash = hash_file('geometry.xml')
1221-
1157+
mat_xml_hash = hash_file('materials.xml')
1158+
geom_xml_hash = hash_file('geometry.xml')
1159+
1160+
pickle_data = {
1161+
'version': self.model.version,
1162+
'currentView': self.model.currentView,
1163+
'statepoint': self.model.statepoint,
1164+
'mat_xml_hash': mat_xml_hash,
1165+
'geom_xml_hash': geom_xml_hash
1166+
}
12221167
with open('plot_settings.pkl', 'wb') as file:
1223-
if self.model.statepoint:
1224-
self.model.statepoint.close()
1225-
pickle.dump(self.model, file)
1168+
pickle.dump(pickle_data, file)
12261169

12271170
def exportTallyData(self):
12281171
# show export tool dialog

0 commit comments

Comments
 (0)