mirror of https://gitlab.com/pamhyr/pamhyr2
Config: Save the last study open and keep close status.
parent
02dafd4694
commit
9ecd9a8f2a
|
|
@ -78,13 +78,14 @@ class WindowToolKit(object):
|
|||
|
||||
return header, values
|
||||
|
||||
def file_dialog(self, select_file=True, callback=lambda x: None):
|
||||
def file_dialog(self, select_file=True, callback=lambda x: None, directory=None):
|
||||
"""Open a new file dialog and send result to callback function
|
||||
|
||||
Args:
|
||||
select_file: Select a file if True, else select a dir
|
||||
callback: The callback function with one arguments, files
|
||||
selection list
|
||||
directory: Defaut directory
|
||||
|
||||
Returns:
|
||||
The returns of callback
|
||||
|
|
@ -97,6 +98,8 @@ class WindowToolKit(object):
|
|||
mode = QFileDialog.FileMode.Directory
|
||||
|
||||
dialog.setFileMode(mode)
|
||||
if directory is not None:
|
||||
dialog.setDirectory(directory)
|
||||
|
||||
if dialog.exec_():
|
||||
file_names = dialog.selectedFiles()
|
||||
|
|
|
|||
|
|
@ -119,6 +119,9 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
|
|||
self.trans = QTranslator(self)
|
||||
#self.ui.retranslateUi()
|
||||
|
||||
if self.conf.last_study != "" and not self.conf.close_correctly:
|
||||
self.dialog_reopen_study()
|
||||
|
||||
def set_title(self):
|
||||
if self.model is not None:
|
||||
self.setWindowTitle(f"PAMHYR - {self.model.name}")
|
||||
|
|
@ -202,16 +205,24 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
|
|||
if self.model is not None and not self.model.is_saved:
|
||||
self._close_question = True
|
||||
if self.dialog_close():
|
||||
# PAMHYR is close correctly (no crash)
|
||||
self.conf.set_close_correctly()
|
||||
|
||||
super(ApplicationWindow, self).close()
|
||||
else:
|
||||
self._close_question = False
|
||||
else:
|
||||
# PAMHYR is close correctly (no crash)
|
||||
self.conf.set_close_correctly()
|
||||
|
||||
super(ApplicationWindow, self).close()
|
||||
|
||||
def closeEvent(self, event):
|
||||
if not self._close_question:
|
||||
if self.model is not None and not self.model.is_saved:
|
||||
if self.dialog_close(cancel = False):
|
||||
# PAMHYR is close correctly (no crash)
|
||||
self.conf.set_close_correctly()
|
||||
super(ApplicationWindow, self).closeEvent(event)
|
||||
else:
|
||||
super(ApplicationWindow, self).closeEvent(event)
|
||||
|
|
@ -264,11 +275,13 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
|
|||
def set_model(self, model):
|
||||
self.model = model
|
||||
self.update_enable_action()
|
||||
self.conf.set_last_study(self.model.filename)
|
||||
self.set_title()
|
||||
|
||||
def close_model(self):
|
||||
self.model = None
|
||||
self.update_enable_action()
|
||||
self.conf.set_close_correctly()
|
||||
self.set_title()
|
||||
|
||||
def update_enable_action(self):
|
||||
|
|
@ -295,6 +308,19 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
|
|||
# FEATURES #
|
||||
############
|
||||
|
||||
def open_study(self, filename):
|
||||
"""Open a study
|
||||
|
||||
Args:
|
||||
filename: The study path
|
||||
|
||||
Returns:
|
||||
Nothing
|
||||
"""
|
||||
self.set_model(Study.open(filename))
|
||||
logger.info(f"Open Study - {self.model.name}")
|
||||
self.set_title()
|
||||
|
||||
def save_study(self):
|
||||
"""Save current study
|
||||
|
||||
|
|
@ -351,6 +377,27 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
|
|||
"Geometry edition need a reach selected "
|
||||
"into river network window to work on it")
|
||||
|
||||
def dialog_reopen_study(self):
|
||||
dlg = QMessageBox(self)
|
||||
|
||||
dlg.setWindowTitle("Last open study")
|
||||
dlg.setText("Do you want to open again the last open study?")
|
||||
opt = QMessageBox.Cancel | QMessageBox.Ok #| QMessageBox.Open
|
||||
|
||||
dlg.setStandardButtons(opt)
|
||||
dlg.setIcon(QMessageBox.Question)
|
||||
|
||||
res = dlg.exec()
|
||||
|
||||
if res == QMessageBox.Ok:
|
||||
self.open_study(self.conf.last_study)
|
||||
return True
|
||||
elif res == QMessageBox.Open:
|
||||
self.open_model()
|
||||
return True
|
||||
elif res == QMessageBox.Cancel:
|
||||
return False
|
||||
|
||||
def dialog_close(self, cancel = True):
|
||||
dlg = QMessageBox(self)
|
||||
|
||||
|
|
@ -411,12 +458,11 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit):
|
|||
dialog.setDefaultSuffix(".pamhyr")
|
||||
#dialog.setFilter(dialog.filter() | QtCore.QDir.Hidden)
|
||||
dialog.setNameFilters(['PamHyr (*.pamhyr)'])
|
||||
dialog.setDirectory(os.path.dirname(self.conf.last_study))
|
||||
|
||||
if dialog.exec_():
|
||||
file_name = dialog.selectedFiles()
|
||||
self.set_model(Study.open(file_name[0]))
|
||||
logger.info(f"Open Study - {self.model.name}")
|
||||
self.set_title()
|
||||
self.open_study(file_name[0])
|
||||
|
||||
def open_new_study(self):
|
||||
"""Open dialog to set new study
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ logger = logging.getLogger()
|
|||
|
||||
class Config(SQL):
|
||||
def __init__(self):
|
||||
self._version = '0.0.2'
|
||||
self._version = '0.0.3'
|
||||
self.filename = Config.filename()
|
||||
self.set_default_value()
|
||||
|
||||
|
|
@ -105,6 +105,10 @@ class Config(SQL):
|
|||
''
|
||||
)
|
||||
""")
|
||||
if int(release) < 3:
|
||||
self.execute(f"INSERT INTO data VALUES ('last_study', '')")
|
||||
self.execute(f"INSERT INTO data VALUES ('close_correctly', 'True')")
|
||||
|
||||
self.commit()
|
||||
|
||||
def _load_solver(self):
|
||||
|
|
@ -176,6 +180,12 @@ class Config(SQL):
|
|||
v = self.execute("SELECT value FROM data WHERE key='lang'")
|
||||
self.lang = v[0]
|
||||
|
||||
# Last study
|
||||
v = self.execute("SELECT value FROM data WHERE key='last_study'")
|
||||
self.last_study = v[0]
|
||||
v = self.execute("SELECT value FROM data WHERE key='close_correctly'")
|
||||
self.close_correctly = v[0] == "True"
|
||||
|
||||
# Debug
|
||||
v = self.execute("SELECT value FROM data WHERE key='debug'")
|
||||
self.debug = v[0] == "True"
|
||||
|
|
@ -228,6 +238,8 @@ class Config(SQL):
|
|||
"backup_max": self.backup_max,
|
||||
"editor": self.editor,
|
||||
"lang": self.lang,
|
||||
"last_study": self.last_study,
|
||||
"close_correctly": self.close_correctly,
|
||||
"debug": self.debug,
|
||||
}
|
||||
|
||||
|
|
@ -279,9 +291,25 @@ class Config(SQL):
|
|||
# Stricklers
|
||||
self.stricklers = StricklersList()
|
||||
|
||||
# Last study
|
||||
self.last_study = ""
|
||||
self.close_correctly = False
|
||||
|
||||
# Debug
|
||||
self.debug = False
|
||||
|
||||
def set_close_correctly(self):
|
||||
self.close_correctly = True
|
||||
self.execute(f"UPDATE data SET value='True' WHERE key='close_correctly'")
|
||||
self.commit()
|
||||
|
||||
def set_last_study(self, filename):
|
||||
self.last_study = filename
|
||||
self.close_correctly = False
|
||||
self.execute(f"UPDATE data SET value='{self._sql_format(self.last_study)}' WHERE key='last_study'")
|
||||
self.execute(f"UPDATE data SET value='{self.close_correctly}' WHERE key='close_correctly'")
|
||||
self.commit()
|
||||
|
||||
@classmethod
|
||||
def filename(cls):
|
||||
file = ""
|
||||
|
|
|
|||
Loading…
Reference in New Issue