From 92e3d5b22558622cd76a1d9e7e881c4fe8a92ba5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-S=C3=A9bastien=20Gosselin?= Date: Wed, 23 Sep 2020 08:47:43 -0400 Subject: [PATCH 1/6] Add qthelpers functions qbytearray_to_hexstate and hexstate_to_qbytearray --- gwhat/utils/qthelpers.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 gwhat/utils/qthelpers.py diff --git a/gwhat/utils/qthelpers.py b/gwhat/utils/qthelpers.py new file mode 100644 index 000000000..74473e8df --- /dev/null +++ b/gwhat/utils/qthelpers.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +# ----------------------------------------------------------------------------- +# Copyright © 2014-2018 GWHAT Project Contributors +# https://github.com/jnsebgosselin/gwhat +# +# This file is part of GWHAT (Ground-Water Hydrograph Analysis Toolbox). +# Licensed under the terms of the GNU General Public License. +# ----------------------------------------------------------------------------- + +"""Qt utilities""" + +# ---- Third party imports +from qtpy.QtCore import QByteArray + + +def qbytearray_to_hexstate(qba): + """Convert QByteArray object to a str hexstate.""" + return str(bytes(qba.toHex().data()).decode()) + + +def hexstate_to_qbytearray(hexstate): + """Convert a str hexstate to a QByteArray object.""" + return QByteArray().fromHex(str(hexstate).encode('utf-8')) From 66123c348f5b8d17f003733d37cd1dd2cb66abcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-S=C3=A9bastien=20Gosselin?= Date: Wed, 23 Sep 2020 08:50:56 -0400 Subject: [PATCH 2/6] Rework how mainwindow is shown --- gwhat/mainwindow.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/gwhat/mainwindow.py b/gwhat/mainwindow.py index 2e0967e99..a5140645f 100644 --- a/gwhat/mainwindow.py +++ b/gwhat/mainwindow.py @@ -91,7 +91,6 @@ def __init__(self, parent=None): # Generate the GUI. self.__initUI__() splash.finish(self) - self.showMaximized() # Load the last opened project. projectfile = get_path_from_configs('main', 'last_project_filepath') @@ -206,6 +205,12 @@ def closeEvent(self, event): print('Closing GWHAT') event.accept() + def show(self): + """ + Extend Qt method. + """ + super().show() + def except_hook(cls, exception, traceback): """ From 7702716208a9d0720bb3cdba55b4968084acf1ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-S=C3=A9bastien=20Gosselin?= Date: Wed, 23 Sep 2020 08:51:28 -0400 Subject: [PATCH 3/6] Save mainwindow state/geometry in user configs --- gwhat/mainwindow.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/gwhat/mainwindow.py b/gwhat/mainwindow.py index a5140645f..e25a03093 100644 --- a/gwhat/mainwindow.py +++ b/gwhat/mainwindow.py @@ -60,6 +60,8 @@ from gwhat.projet.manager_data import DataManager from gwhat.common import StyleDB from gwhat.utils import icons +from gwhat.utils.qthelpers import ( + qbytearray_to_hexstate, hexstate_to_qbytearray) freeze_support() @@ -197,12 +199,17 @@ def new_project_loaded(self): self.tab_hydrograph.setEnabled(True) self.tab_hydrocalc.setEnabled(True) + # ---- Qt method override/extension def closeEvent(self, event): """Qt method override to close the project before close the app.""" + self._save_window_geometry() + self._save_window_state() + print('Closing projet') self.pmanager.close() - self.tab_hydrocalc.close() + print('Closing GWHAT') + self.tab_hydrocalc.close() event.accept() def show(self): @@ -211,6 +218,23 @@ def show(self): """ super().show() + # ---- Main window settings + def _save_window_geometry(self): + """ + Save the geometry of this mainwindow to the config. + """ + hexstate = qbytearray_to_hexstate(self.saveGeometry()) + CONF.set('main', 'window/geometry', hexstate) + + + def _save_window_state(self): + """ + Save the state of this mainwindow’s toolbars and dockwidgets to + the config. + """ + hexstate = qbytearray_to_hexstate(self.saveState()) + CONF.set('main', 'window/state', hexstate) + def except_hook(cls, exception, traceback): """ From 8812f362086496cd5f613f14209b28b8d08bc713 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-S=C3=A9bastien=20Gosselin?= Date: Wed, 23 Sep 2020 08:51:45 -0400 Subject: [PATCH 4/6] Get mainwindow state/geometry from configs --- gwhat/mainwindow.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/gwhat/mainwindow.py b/gwhat/mainwindow.py index e25a03093..48324bccb 100644 --- a/gwhat/mainwindow.py +++ b/gwhat/mainwindow.py @@ -93,6 +93,8 @@ def __init__(self, parent=None): # Generate the GUI. self.__initUI__() splash.finish(self) + self._restore_window_geometry() + self._restore_window_state() # Load the last opened project. projectfile = get_path_from_configs('main', 'last_project_filepath') @@ -219,6 +221,19 @@ def show(self): super().show() # ---- Main window settings + def _restore_window_geometry(self): + """ + Restore the geometry of this mainwindow from the value saved + in the config. + """ + hexstate = CONF.get('main', 'window/geometry', None) + if hexstate: + hexstate = hexstate_to_qbytearray(hexstate) + self.restoreGeometry(hexstate) + else: + from sardes.config.gui import INIT_MAINWINDOW_SIZE + self.resize(*INIT_MAINWINDOW_SIZE) + def _save_window_geometry(self): """ Save the geometry of this mainwindow to the config. @@ -226,6 +241,16 @@ def _save_window_geometry(self): hexstate = qbytearray_to_hexstate(self.saveGeometry()) CONF.set('main', 'window/geometry', hexstate) + def _restore_window_state(self): + """ + Restore the state of this mainwindow’s toolbars and dockwidgets from + the value saved in the config. + """ + # Then we appply saved configuration if it exists. + hexstate = CONF.get('main', 'window/state', None) + if hexstate: + hexstate = hexstate_to_qbytearray(hexstate) + self.restoreState(hexstate) def _save_window_state(self): """ From e7627215b8cceae3d00e4d3b41d14129830d52ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-S=C3=A9bastien=20Gosselin?= Date: Wed, 23 Sep 2020 09:26:35 -0400 Subject: [PATCH 5/6] Set/Get splitter state --- gwhat/mainwindow.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/gwhat/mainwindow.py b/gwhat/mainwindow.py index 48324bccb..7a57733cb 100644 --- a/gwhat/mainwindow.py +++ b/gwhat/mainwindow.py @@ -152,22 +152,21 @@ def __initUI__(self): self.sync_datamanagers() # Setup the splitter widget. - splitter = QSplitter(Qt.Vertical, parent=self) - splitter.addWidget(self.tab_widget) - splitter.addWidget(self.main_console) + self._splitter = QSplitter(Qt.Vertical, parent=self) + self._splitter.addWidget(self.tab_widget) + self._splitter.addWidget(self.main_console) - splitter.setCollapsible(0, True) - splitter.setStretchFactor(0, 100) - # Forces initially the main_console to its minimal height: - splitter.setSizes([100, 1]) + self._splitter.setCollapsible(0, True) + self._splitter.setStretchFactor(0, 100) + # Force initially the main_console to its minimal height. + self._splitter.setSizes([100, 1]) # Setup the layout of the main widget. main_widget = QWidget() self.setCentralWidget(main_widget) mainGrid = QGridLayout(main_widget) - - mainGrid.addWidget(splitter, 0, 0) + mainGrid.addWidget(self._splitter, 0, 0) mainGrid.addWidget( self.tab_hydrocalc.rechg_eval_widget.progressbar, 3, 0) @@ -252,6 +251,11 @@ def _restore_window_state(self): hexstate = hexstate_to_qbytearray(hexstate) self.restoreState(hexstate) + hexstate = CONF.get('main', 'splitter/state', None) + if hexstate: + hexstate = hexstate_to_qbytearray(hexstate) + self._splitter.restoreState(hexstate) + def _save_window_state(self): """ Save the state of this mainwindow’s toolbars and dockwidgets to @@ -260,6 +264,9 @@ def _save_window_state(self): hexstate = qbytearray_to_hexstate(self.saveState()) CONF.set('main', 'window/state', hexstate) + hexstate = qbytearray_to_hexstate(self._splitter.saveState()) + CONF.set('main', 'splitter/state', hexstate) + def except_hook(cls, exception, traceback): """ From 7f844bf3ffb2ddc159a61971c38435839bbdd359 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-S=C3=A9bastien=20Gosselin?= Date: Wed, 23 Sep 2020 10:13:33 -0400 Subject: [PATCH 6/6] Fix GWHAT mainwindow default size --- gwhat/config/gui.py | 17 +++++++++++++++++ gwhat/mainwindow.py | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 gwhat/config/gui.py diff --git a/gwhat/config/gui.py b/gwhat/config/gui.py new file mode 100644 index 000000000..5348eec76 --- /dev/null +++ b/gwhat/config/gui.py @@ -0,0 +1,17 @@ +# -*- coding: utf-8 -*- +# ----------------------------------------------------------------------------- +# Copyright © 2014-2018 GWHAT Project Contributors +# https://github.com/jnsebgosselin/gwhat +# +# This file is part of GWHAT (Ground-Water Hydrograph Analysis Toolbox). +# Licensed under the terms of the GNU General Public License. +# ----------------------------------------------------------------------------- + + +ICON_COLOR = '#202020' +GREEN = '#00aa00' +RED = '#CC0000' +YELLOW = '#ffa500' +YELLOWLIGHT = '#fcf7b6' + +INIT_MAINWINDOW_SIZE = (1260, 740) diff --git a/gwhat/mainwindow.py b/gwhat/mainwindow.py index 7a57733cb..db8590a48 100644 --- a/gwhat/mainwindow.py +++ b/gwhat/mainwindow.py @@ -230,7 +230,7 @@ def _restore_window_geometry(self): hexstate = hexstate_to_qbytearray(hexstate) self.restoreGeometry(hexstate) else: - from sardes.config.gui import INIT_MAINWINDOW_SIZE + from gwhat.config.gui import INIT_MAINWINDOW_SIZE self.resize(*INIT_MAINWINDOW_SIZE) def _save_window_geometry(self):