[Add] Mainwindow now supports geometry setting save/load/restore

This commit is contained in:
vn.py 2019-01-12 09:54:56 +08:00
parent 60a640b6d1
commit 6e1d90b327

View File

@ -36,6 +36,9 @@ class MainWindow(QtWidgets.QMainWindow):
self.main_engine = main_engine self.main_engine = main_engine
self.event_engine = event_engine self.event_engine = event_engine
self.path = get_trader_path()
self.window_title = f"VN Trader [{self.path}]"
self.connect_dialogs = {} self.connect_dialogs = {}
self.widgets = {} self.widgets = {}
@ -43,12 +46,10 @@ class MainWindow(QtWidgets.QMainWindow):
def init_ui(self): def init_ui(self):
"""""" """"""
path = get_trader_path() self.setWindowTitle(self.window_title)
title = f"VN Trader [{path}]"
self.setWindowTitle(title)
self.init_dock() self.init_dock()
self.init_menu() self.init_menu()
self.load_window_setting("custom")
def init_dock(self): def init_dock(self):
"""""" """"""
@ -63,6 +64,8 @@ class MainWindow(QtWidgets.QMainWindow):
self.tabifyDockWidget(active_dock, order_dock) self.tabifyDockWidget(active_dock, order_dock)
self.save_window_setting("default")
def init_menu(self): def init_menu(self):
"""""" """"""
bar = self.menuBar() bar = self.menuBar()
@ -93,6 +96,13 @@ class MainWindow(QtWidgets.QMainWindow):
"contract") "contract")
) )
self.add_menu_action(
help_menu,
"还原窗口",
"restore.ico",
self.restore_window_setting
)
self.add_menu_action( self.add_menu_action(
help_menu, help_menu,
"关于", "关于",
@ -161,6 +171,7 @@ class MainWindow(QtWidgets.QMainWindow):
if reply == QtWidgets.QMessageBox.Yes: if reply == QtWidgets.QMessageBox.Yes:
for widget in self.widgets.values(): for widget in self.widgets.values():
widget.close() widget.close()
self.save_window_setting("custom")
self.main_engine.close() self.main_engine.close()
@ -181,3 +192,30 @@ class MainWindow(QtWidgets.QMainWindow):
widget.exec() widget.exec()
else: else:
widget.show() widget.show()
def save_window_setting(self, name: str):
"""
Save current window size and state by trader path and setting name.
"""
settings = QtCore.QSettings(self.window_title, name)
settings.setValue('state', self.saveState())
settings.setValue('geometry', self.saveGeometry())
def load_window_setting(self, name: str):
"""
Load previous window size and state by trader path and setting name.
"""
settings = QtCore.QSettings(self.window_title, name)
state = settings.value('state')
geometry = settings.value('geometry')
if isinstance(state, QtCore.QByteArray):
self.restoreState(state)
self.restoreGeometry(geometry)
def restore_window_setting(self):
"""
Restore window to default setting.
"""
self.load_window_setting("default")
self.showMaximized()