Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Use user configs to set main window state and geometry #324

Merged
merged 6 commits into from
Sep 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions gwhat/config/gui.py
Original file line number Diff line number Diff line change
@@ -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)
83 changes: 72 additions & 11 deletions gwhat/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -91,7 +93,8 @@ def __init__(self, parent=None):
# Generate the GUI.
self.__initUI__()
splash.finish(self)
self.showMaximized()
self._restore_window_geometry()
self._restore_window_state()

# Load the last opened project.
projectfile = get_path_from_configs('main', 'last_project_filepath')
Expand Down Expand Up @@ -149,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)

Expand Down Expand Up @@ -198,14 +200,73 @@ 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):
"""
Extend Qt method.
"""
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 gwhat.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.
"""
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)

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
the config.
"""
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):
"""
Expand Down
23 changes: 23 additions & 0 deletions gwhat/utils/qthelpers.py
Original file line number Diff line number Diff line change
@@ -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'))