From 4976e2764086970e5d57d7f3806aa929da15223c Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Thu, 2 May 2019 20:44:22 +0800 Subject: [PATCH] [Add]global setting editor --- vnpy/trader/ui/mainwindow.py | 22 +++++++++--- vnpy/trader/ui/widget.py | 70 ++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 4 deletions(-) diff --git a/vnpy/trader/ui/mainwindow.py b/vnpy/trader/ui/mainwindow.py index 25e40878..4eba1f33 100644 --- a/vnpy/trader/ui/mainwindow.py +++ b/vnpy/trader/ui/mainwindow.py @@ -22,6 +22,7 @@ from .widget import ( ContractManager, TradingWidget, AboutDialog, + GlobalDialog ) from ..engine import MainEngine from ..utility import get_icon_path, TRADER_DIR @@ -87,11 +88,9 @@ class MainWindow(QtWidgets.QMainWindow): """""" bar = self.menuBar() - sys_menu = bar.addMenu("系统") - app_menu = bar.addMenu("功能") - help_menu = bar.addMenu("帮助") - # System menu + sys_menu = bar.addMenu("系统") + gateway_names = self.main_engine.get_all_gateway_names() for name in gateway_names: func = partial(self.connect, name) @@ -102,6 +101,8 @@ class MainWindow(QtWidgets.QMainWindow): self.add_menu_action(sys_menu, "退出", "exit.ico", self.close) # App menu + app_menu = bar.addMenu("功能") + all_apps = self.main_engine.get_all_apps() for app in all_apps: ui_module = import_module(app.app_module + ".ui") @@ -113,7 +114,14 @@ class MainWindow(QtWidgets.QMainWindow): 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 = bar.addMenu("帮助") + self.add_menu_action( help_menu, "查询合约", @@ -255,3 +263,9 @@ class MainWindow(QtWidgets.QMainWindow): """ """ webbrowser.open("https://www.vnpy.com/forum/") + + def edit_global_setting(self): + """ + """ + dialog = GlobalDialog() + dialog.exec_() diff --git a/vnpy/trader/ui/widget.py b/vnpy/trader/ui/widget.py index e9accb07..f0b46b99 100644 --- a/vnpy/trader/ui/widget.py +++ b/vnpy/trader/ui/widget.py @@ -5,6 +5,7 @@ Basic widgets for VN Trader. import csv from enum import Enum from typing import Any +from copy import copy from PyQt5 import QtCore, QtGui, QtWidgets @@ -21,6 +22,8 @@ from ..event import ( ) from ..object import OrderRequest, SubscribeRequest from ..utility import load_json, save_json +from ..setting import SETTING_FILENAME, SETTINGS + COLOR_LONG = QtGui.QColor("red") COLOR_SHORT = QtGui.QColor("green") @@ -1002,3 +1005,70 @@ class AboutDialog(QtWidgets.QDialog): vbox = QtWidgets.QVBoxLayout() vbox.addWidget(label) 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()