[Add]global setting editor

This commit is contained in:
vn.py 2019-05-02 20:44:22 +08:00
parent 2da861d9e7
commit 4976e27640
2 changed files with 88 additions and 4 deletions

View File

@ -22,6 +22,7 @@ from .widget import (
ContractManager, ContractManager,
TradingWidget, TradingWidget,
AboutDialog, AboutDialog,
GlobalDialog
) )
from ..engine import MainEngine from ..engine import MainEngine
from ..utility import get_icon_path, TRADER_DIR from ..utility import get_icon_path, TRADER_DIR
@ -87,11 +88,9 @@ class MainWindow(QtWidgets.QMainWindow):
"""""" """"""
bar = self.menuBar() bar = self.menuBar()
sys_menu = bar.addMenu("系统")
app_menu = bar.addMenu("功能")
help_menu = bar.addMenu("帮助")
# System menu # System menu
sys_menu = bar.addMenu("系统")
gateway_names = self.main_engine.get_all_gateway_names() gateway_names = self.main_engine.get_all_gateway_names()
for name in gateway_names: for name in gateway_names:
func = partial(self.connect, name) func = partial(self.connect, name)
@ -102,6 +101,8 @@ class MainWindow(QtWidgets.QMainWindow):
self.add_menu_action(sys_menu, "退出", "exit.ico", self.close) self.add_menu_action(sys_menu, "退出", "exit.ico", self.close)
# App menu # App menu
app_menu = bar.addMenu("功能")
all_apps = self.main_engine.get_all_apps() all_apps = self.main_engine.get_all_apps()
for app in all_apps: for app in all_apps:
ui_module = import_module(app.app_module + ".ui") ui_module = import_module(app.app_module + ".ui")
@ -113,7 +114,14 @@ class MainWindow(QtWidgets.QMainWindow):
app_menu, app.display_name, icon_path, func app_menu, app.display_name, icon_path, func
) )
# Global setting editor
action = QtWidgets.QAction("配置", self)
action.triggered.connect(self.edit_global_setting)
bar.addAction(action)
# Help menu # Help menu
help_menu = bar.addMenu("帮助")
self.add_menu_action( self.add_menu_action(
help_menu, help_menu,
"查询合约", "查询合约",
@ -255,3 +263,9 @@ class MainWindow(QtWidgets.QMainWindow):
""" """
""" """
webbrowser.open("https://www.vnpy.com/forum/") webbrowser.open("https://www.vnpy.com/forum/")
def edit_global_setting(self):
"""
"""
dialog = GlobalDialog()
dialog.exec_()

View File

@ -5,6 +5,7 @@ Basic widgets for VN Trader.
import csv import csv
from enum import Enum from enum import Enum
from typing import Any from typing import Any
from copy import copy
from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5 import QtCore, QtGui, QtWidgets
@ -21,6 +22,8 @@ from ..event import (
) )
from ..object import OrderRequest, SubscribeRequest from ..object import OrderRequest, SubscribeRequest
from ..utility import load_json, save_json from ..utility import load_json, save_json
from ..setting import SETTING_FILENAME, SETTINGS
COLOR_LONG = QtGui.QColor("red") COLOR_LONG = QtGui.QColor("red")
COLOR_SHORT = QtGui.QColor("green") COLOR_SHORT = QtGui.QColor("green")
@ -1002,3 +1005,70 @@ class AboutDialog(QtWidgets.QDialog):
vbox = QtWidgets.QVBoxLayout() vbox = QtWidgets.QVBoxLayout()
vbox.addWidget(label) vbox.addWidget(label)
self.setLayout(vbox) self.setLayout(vbox)
class GlobalDialog(QtWidgets.QDialog):
"""
Start connection of a certain gateway.
"""
def __init__(self):
""""""
super().__init__()
self.widgets = {}
self.init_ui()
def init_ui(self):
""""""
self.setWindowTitle("全局配置")
self.setMinimumWidth(800)
settings = copy(SETTINGS)
settings.update(load_json(SETTING_FILENAME))
# Initialize line edits and form layout based on setting.
form = QtWidgets.QFormLayout()
for field_name, field_value in settings.items():
field_type = type(field_value)
widget = QtWidgets.QLineEdit(str(field_value))
form.addRow(f"{field_name} <{field_type.__name__}>", widget)
self.widgets[field_name] = (widget, field_type)
button = QtWidgets.QPushButton("确定")
button.clicked.connect(self.update_setting)
form.addRow(button)
self.setLayout(form)
def update_setting(self):
"""
Get setting value from line edits and update global setting file.
"""
settings = {}
for field_name, tp in self.widgets.items():
widget, field_type = tp
value_text = widget.text()
if field_type == bool:
if value_text == "True":
field_value = True
else:
field_value = False
else:
field_value = field_type(value_text)
settings[field_name] = field_value
QtWidgets.QMessageBox.information(
self,
"注意",
"全局配置的修改需要重启VN Trader后才会生效",
QtWidgets.QMessageBox.Ok
)
save_json(SETTING_FILENAME, settings)
self.accept()