Commit 8760cd36 authored by Pierre-Antoine Rouby's avatar Pierre-Antoine Rouby
Browse files

Pamhyr: Implement window duplication deny mechanism with hash (#7).

Showing with 186 additions and 122 deletions
+186 -122
......@@ -68,6 +68,9 @@ class CheckListWindow(PamhyrWindow):
parent=parent
)
# Add solver to hash computation data
self._hash_data.append(self._solver)
self._checker_list = (
self._study.checkers() +
self._solver.checkers()
......
......@@ -83,6 +83,9 @@ class FrictionsWindow(PamhyrWindow):
parent=parent
)
# Add reach to hash computation data
self._hash_data.append(self._reach)
self.setup_table()
self.setup_graph()
self.setup_connections()
......
......@@ -73,6 +73,9 @@ class GeometryWindow(PamhyrWindow):
parent=parent
)
# Add reach to hash computation data
self._hash_data.append(self._reach)
self._tablemodel = None
self._profile_window = []
......
......@@ -87,6 +87,9 @@ class InitialConditionsWindow(PamhyrWindow):
parent=parent
)
# Add reach to hash computation data
self._hash_data.append(self._reach)
self._ics = study.river.initial_conditions.get(self._reach)
self.setup_table()
......
......@@ -480,6 +480,30 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
# SUBWINDOW #
#############
def _sub_window_exists(self, cls,
data=None):
"""Check if window already exists
Check if window already exists, used to deni window open
duplication
Args:
cls: Window class, must inerit to PamhyrWindow or
PamhyrDialog
data: Data used for hash computation of cls
Returns:
The window if hash already exists on sub window dictionary,
otherelse None
"""
hash = cls._hash(data)
if self.sub_win_exists(hash):
win = self.get_sub_win(hash)
win.activateWindow()
return True
else:
return False
def open_configure(self):
"""Open configure window
......@@ -488,6 +512,12 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
Returns:
Nothing
"""
if self._sub_window_exists(
ConfigureWindow,
data=[None, self.conf]
):
return
self.config = ConfigureWindow(config=self.conf, parent=self)
self.config.show()
......@@ -499,6 +529,12 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
Returns:
Nothing
"""
if self._sub_window_exists(
AboutWindow,
data=[None, None]
):
return
self.about = AboutWindow(parent=self)
self.about.show()
......@@ -527,6 +563,12 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
Nothing
"""
if self._study is None:
if self._sub_window_exists(
NewStudyWindow,
data=[None, None]
):
return
self.new_study = NewStudyWindow(parent=self)
self.new_study.show()
......@@ -537,6 +579,12 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
Nothing
"""
if self._study is not None:
if self._sub_window_exists(
NewStudyWindow,
data=[self._study, None]
):
return
self.new_study = NewStudyWindow(study=self._study, parent=self)
self.new_study.show()
......@@ -547,11 +595,14 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
Nothing
"""
if self._study is not None:
if not self.sub_win_exists("River network"):
self.network = NetworkWindow(study=self._study, parent=self)
self.network.show()
else:
self.network.activateWindow()
if self._sub_window_exists(
NetworkWindow,
data=[self._study, None]
):
return
self.network = NetworkWindow(study=self._study, parent=self)
self.network.show()
def open_geometry(self):
"""Open geometry window
......@@ -560,115 +611,117 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
Nothing
"""
if (self._study is not None and self._study.river.has_current_reach()):
geometry = self.sub_win_filter_first(
"Geometry",
contain=[self._study.river.current_reach().name]
)
reach = self._study.river.current_reach().reach
if geometry is None:
geometry = GeometryWindow(
study=self._study, config=self.conf, parent=self)
geometry.show()
else:
geometry.activateWindow()
if self._sub_window_exists(
GeometryWindow,
data=[self._study, self.conf, reach]
):
return
geometry = GeometryWindow(
study=self._study,
config=self.conf,
reach=reach,
parent=self
)
geometry.show()
else:
self.msg_select_reach()
def open_boundary_cond(self):
bound = self.sub_win_filter_first(
"Boundary conditions",
contain=[]
)
if self._sub_window_exists(
BoundaryConditionWindow,
data=[self._study, None]
):
return
if bound is None:
bound = BoundaryConditionWindow(study=self._study, parent=self)
bound.show()
else:
bound.activateWindow()
bound = BoundaryConditionWindow(study=self._study, parent=self)
bound.show()
def open_lateral_contrib(self):
lateral = self.sub_win_filter_first(
"Lateral contribution",
contain=[]
)
if self._sub_window_exists(
LateralContributionWindow,
data=[self._study, None]
):
return
if lateral is None:
lateral = LateralContributionWindow(study=self._study, parent=self)
lateral.show()
else:
lateral.activateWindow()
lateral = LateralContributionWindow(study=self._study, parent=self)
lateral.show()
def open_stricklers(self):
strick = self.sub_win_filter_first(
"Stricklers",
contain=[]
)
if self._sub_window_exists(
StricklersWindow,
data=[self._study, self.conf]
):
return
if strick is None:
strick = StricklersWindow(
study=self._study,
config=self.conf,
parent=self
)
strick.show()
else:
strick.activateWindow()
strick = StricklersWindow(
study=self._study,
config=self.conf,
parent=self
)
strick.show()
def open_frictions(self):
if (self._study is not None and
self._study.river.has_current_reach()):
if self._study is not None:
if self._study.river.has_current_reach():
reach = self._study.river.current_reach()
frictions = self.sub_win_filter_first(
"Frictions",
contain=[self._study.river.current_reach().name]
)
if self._sub_window_exists(
FrictionsWindow,
data=[self._study, None, reach]
):
return
if frictions is None:
frictions = FrictionsWindow(
study=self._study,
parent=self
)
frictions.show()
else:
frictions.activateWindow()
else:
self.msg_select_reach()
self.msg_select_reach()
def open_initial_conditions(self):
if self._study.river.has_current_reach():
initial = self.sub_win_filter_first(
"Initial condition",
contain=[self._study.river.current_reach().name]
)
if initial is None:
initial = InitialConditionsWindow(
study=self._study,
config=self.conf,
parent=self
)
initial.show()
else:
initial.activateWindow()
else:
self.msg_select_reach()
reach = self._study.river.current_reach()
def open_solver_parameters(self):
params = self.sub_win_filter_first(
"Solver parameters",
contain=[]
)
if self._sub_window_exists(
InitialConditionsWindow,
data=[self._study, self.conf, reach]
):
return
if params is None:
params = SolverParametersWindow(
initial = InitialConditionsWindow(
study=self._study,
config=self.conf,
reach=reach,
parent=self
)
params.show()
initial.show()
else:
params.activateWindow()
self.msg_select_reach()
def open_solver_parameters(self):
if self._sub_window_exists(
SolverParametersWindow,
data=[self._study, None]
):
return
params = SolverParametersWindow(
study=self._study,
parent=self
)
params.show()
def open_sediment_layers(self):
if self._sub_window_exists(
SedimentLayersWindow,
data=[self._study, None]
):
return
sl = SedimentLayersWindow(
study=self._study,
parent=self
......@@ -676,8 +729,17 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
sl.show()
def open_reach_sediment_layers(self):
reach = self._study.river.current_reach().reach
if self._sub_window_exists(
ReachSedimentLayersWindow,
data=[self._study, None, reach]
):
return
sl = ReachSedimentLayersWindow(
study=self._study,
reach=reach,
parent=self
)
sl.show()
......@@ -693,6 +755,17 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
)
if run.exec():
solver = run.solver
if self._sub_window_exists(
CheckListWindow,
data=[
self._study,
self.conf,
solver
]
):
return
check = CheckListWindow(
study=self._study,
config=self.conf,
......
......@@ -46,9 +46,12 @@ class ReachSedimentLayersWindow(PamhyrWindow):
_pamhyr_ui = "ReachSedimentLayers"
_pamhyr_name = "Reach sediment layers"
def __init__(self, study=None, config=None, parent=None):
def __init__(self, study=None, config=None, reach=None, parent=None):
self._sediment_layers = study.river.sediment_layers
self._reach = study.river.current_reach().reach
if reach is None:
self._reach = study.river.current_reach().reach
else:
self._reach = reach
name = (
self._pamhyr_name + " - " +
......@@ -64,6 +67,9 @@ class ReachSedimentLayersWindow(PamhyrWindow):
parent=parent
)
# Add reach to hash computation data
self._hash_data.append(self._reach)
self.setup_table()
self.setup_plot()
self.setup_connections()
......
......@@ -483,7 +483,7 @@ class ASubMainWindow(QMainWindow, ASubWindowFeatures, WindowToolKit):
def closeEvent(self, event):
if self.parent is not None:
self.parent.sub_win_del(self.name)
self.parent.sub_win_del(self.hash())
def find(self, qtype, name):
"""Find an ui component
......@@ -520,7 +520,7 @@ class ASubWindow(QDialog, ASubWindowFeatures, WindowToolKit):
def closeEvent(self, event):
if self.parent is not None:
self.parent.sub_win_del(self.name)
self.parent.sub_win_del(self.hash())
def find(self, qtype, name):
"""Find an ui component
......
......@@ -42,59 +42,33 @@ class ListedSubWindow(object):
f"Open window: {name}: {self.sub_win_cnt}: {win.hash()}")
except Exception:
logger.info(f"Open window: {name}: {self.sub_win_cnt}: X")
logger.warning(f"Sub window without hash method !")
def sub_win_del(self, name):
def sub_win_del(self, h):
self.sub_win_list = list(
filter(
lambda x: x[0] != name,
lambda x: x[1].hash() != h,
self.sub_win_list
)
)
self.sub_win_cnt = len(self.sub_win_list)
logger.info(f"Close window: {name}: {self.sub_win_cnt}")
logger.info(f"Close window: {h}: {self.sub_win_cnt}")
def _sub_win_exists(self, name):
def _sub_win_exists(self, h):
return reduce(
lambda acc, n: (acc or (n[0] == name)),
lambda acc, el: (acc or (h == (el[1].hash()))),
self.sub_win_list,
False
)
def _sub_win_exists_with_contain(self, name, contain):
return reduce(
lambda acc, n: (
acc or
(
(n[0] == name) and
reduce(
lambda acc, c: acc and (c in n[1]._title),
contain,
True
)
)
),
self.sub_win_list,
False
)
def sub_win_exists(self, name, contain=[]):
if contain == []:
return self._sub_win_exists(name)
else:
return self._sub_win_exists_with_contain(name, contain)
def sub_win_exists(self, h):
return self._sub_win_exists(h)
def sub_win_filter_first(self, name, contain):
def get_sub_win(self, h):
try:
return next(
filter(
lambda n: (
(name in n[0]) and
reduce(
lambda acc, c: acc and (c in n[1]._title),
contain,
True
)
),
lambda el: (h == el[1].hash()),
self.sub_win_list,
)
)[1]
......
......@@ -122,7 +122,6 @@ class PamhyrWindowTools(object):
hash_str += repr(el)
h = hash(hash_str)
logger.debug(f"Compute hash = {h} for window {cls._pamhyr_name}")
return h
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment