优化复选框的显示

This commit is contained in:
ares89@wind 2017-02-09 19:31:14 +08:00
parent 1e2799933f
commit 2605103b9f
3 changed files with 99 additions and 16 deletions

View File

@ -3,6 +3,7 @@
''' '''
行情记录模块相关的GUI控制组件 行情记录模块相关的GUI控制组件
''' '''
import json
import re import re
import sys import sys
@ -11,7 +12,7 @@ from PyQt4.QtGui import QTreeView
from dataRecorder.drEngine import DrEngine from dataRecorder.drEngine import DrEngine
from eventEngine import * from eventEngine import *
from uiBasicWidget import QtGui, QtCore from uiBasicWidget import QtGui, QtCore
from util.UiUtil import CheckBoxDelegate from util.UiUtil import CheckBoxDelegate, GateWayListItemEditorCreator
reload(sys) reload(sys)
sys.setdefaultencoding("utf8") sys.setdefaultencoding("utf8")
@ -112,7 +113,7 @@ class TreeModel(QtCore.QAbstractItemModel):
index.column() in [1, 2] or index.column() == 3 and item.parentItem != self.rootItem): index.column() in [1, 2] or index.column() == 3 and item.parentItem != self.rootItem):
return QtCore.Qt.Checked if item.data(index.column()) == True else QtCore.Qt.Unchecked return QtCore.Qt.Checked if item.data(index.column()) == True else QtCore.Qt.Unchecked
if role != QtCore.Qt.DisplayRole or index.column() == 3 and item.parentItem == self.rootItem: if role != QtCore.Qt.DisplayRole or index.column() in [3, 4] and item.parentItem == self.rootItem:
return None return None
return item.data(index.column()) return item.data(index.column())
@ -121,9 +122,12 @@ class TreeModel(QtCore.QAbstractItemModel):
if not index.isValid(): if not index.isValid():
return QtCore.Qt.NoItemFlags return QtCore.Qt.NoItemFlags
if index.column() in [0, 4, 5]: if index.column() in [0, 4]:
return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
if index.column() in [5]:
return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEditable
item = index.internalPointer() item = index.internalPointer()
if index.column() == 3 and item.parentItem == self.rootItem: if index.column() == 3 and item.parentItem == self.rootItem:
return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
@ -210,9 +214,9 @@ class DrEditWidget(QtGui.QWidget):
vline = QtGui.QHBoxLayout() vline = QtGui.QHBoxLayout()
vline.setSpacing(2) vline.setSpacing(2)
btnTickAll = QtGui.QPushButton(u"全部记录tick", self) btnTickAll = QtGui.QPushButton(u"全部记录Tick", self)
btnBarAll = QtGui.QPushButton(u'全部记录bar', self) btnBarAll = QtGui.QPushButton(u'全部记录Bar', self)
btnSaveAll = QtGui.QPushButton(u'保存设置', self) btnSaveAll = QtGui.QPushButton(u'保存设置(重启后生效)', self)
btnTickAll.clicked.connect(self.selectAllTick) btnTickAll.clicked.connect(self.selectAllTick)
btnBarAll.clicked.connect(self.selectAllBar) btnBarAll.clicked.connect(self.selectAllBar)
btnSaveAll.clicked.connect(self.saveSetting) btnSaveAll.clicked.connect(self.saveSetting)
@ -230,6 +234,10 @@ class DrEditWidget(QtGui.QWidget):
self.qTreeView.setItemDelegateForColumn(2, CheckBoxDelegate(self)) self.qTreeView.setItemDelegateForColumn(2, CheckBoxDelegate(self))
self.qTreeView.setItemDelegateForColumn(3, CheckBoxDelegate(self)) self.qTreeView.setItemDelegateForColumn(3, CheckBoxDelegate(self))
factory = QtGui.QItemEditorFactory()
factory.registerEditor(QtCore.QVariant.Line, GateWayListItemEditorCreator())
QtGui.QItemEditorFactory.setDefaultFactory(factory)
vbox.addWidget(self.qTreeView) vbox.addWidget(self.qTreeView)
self.setLayout(vbox) self.setLayout(vbox)
@ -244,19 +252,48 @@ class DrEditWidget(QtGui.QWidget):
def loadData(self): def loadData(self):
child = [] child = []
tick = {}
bar = {}
active = []
with open(self.mainEngine.drEngine.settingFileName) as f:
drSetting = json.load(f)
if 'tick' in drSetting:
l = drSetting['tick']
for setting in l:
tick[setting[0]] = setting[1]
if 'bar' in drSetting:
l = drSetting['bar']
for setting in l:
bar[setting[0]] = setting[1]
if 'active' in drSetting:
d = drSetting['active']
for activeSymbol, symbol in d.items():
active.append(symbol)
contractDict = {} contractDict = {}
contracts = self.mainEngine.getAllContracts() contracts = self.mainEngine.getAllContracts()
for contract in contracts: for contract in contracts:
contractName = self.getContractChineseName(contract.name) contractName = self.getContractChineseName(contract.name)
gateWayName = contract.gatewayName
hasTick = tick.has_key(contract.symbol)
hasBar = bar.has_key(contract.symbol)
hasActive = contract.symbol in active
if contractDict.has_key(contractName): if contractDict.has_key(contractName):
parentItem = contractDict[contractName] parentItem = contractDict[contractName]
item = TreeItem([contract.symbol, False, False, False, contract.exchange, "CTP"], parentItem) item = TreeItem([contract.symbol, hasTick, hasBar, hasActive, contract.exchange, gateWayName],
parentItem)
parentItem.appendChild(item) parentItem.appendChild(item)
else: else:
item = TreeItem([contractName, False, False, False, contract.exchange, "CTP"], self.model.rootItem) item = TreeItem([contractName, False, False, False, contract.exchange, gateWayName],
self.model.rootItem)
contractDict[contractName] = item contractDict[contractName] = item
child.append(item) child.append(item)
subItem = TreeItem([contract.symbol, False, False, False, contract.exchange, "CTP"], item) subItem = TreeItem([contract.symbol, hasTick, hasBar, hasActive, contract.exchange, gateWayName], item)
item.appendChild(subItem) item.appendChild(subItem)
# yumi = TreeItem([u"玉米", False, False, False, "SH", "CTP"], self.model.rootItem) # yumi = TreeItem([u"玉米", False, False, False, "SH", "CTP"], self.model.rootItem)

View File

@ -6,9 +6,6 @@
import json import json
from PyQt4.QtCore import QObject
from dataRecorder.drEngine import DrEngine
from dataRecorder.uiDrEdit import DrEditWidget from dataRecorder.uiDrEdit import DrEditWidget
from eventEngine import * from eventEngine import *
from uiBasicWidget import QtGui, QtCore from uiBasicWidget import QtGui, QtCore
@ -178,6 +175,7 @@ class DrEngineManager(QtGui.QWidget):
self.mDrEditWidget.show() self.mDrEditWidget.show()
def restart(self): def restart(self):
self.drEngine.stop() pass
self.updateSetting() # self.drEngine.stop()
self.mainEngine.drEngine = DrEngine(self.mainEngine, self.mainEngine.eventEngine) # self.updateSetting()
# self.mainEngine.drEngine = DrEngine(self.mainEngine, self.mainEngine.eventEngine)

View File

@ -1,5 +1,6 @@
from PyQt4 import QtCore from PyQt4 import QtCore
from PyQt4.QtGui import QStyle, QStyledItemDelegate, QStyleOptionButton, QApplication, QItemDelegate from PyQt4.QtGui import QStyle, QStyledItemDelegate, QStyleOptionButton, QApplication, QItemDelegate, QComboBox, \
QItemEditorCreatorBase
class CheckBoxDelegate(QStyledItemDelegate): class CheckBoxDelegate(QStyledItemDelegate):
@ -94,3 +95,50 @@ class CheckBoxDelegate(QStyledItemDelegate):
option.rect.height() / 2 - option.rect.height() / 2 -
check_box_rect.height() / 2) check_box_rect.height() / 2)
return QtCore.QRect(check_box_point, check_box_rect.size()) return QtCore.QRect(check_box_point, check_box_rect.size())
class ComboDelegate(QItemDelegate):
def __init__(self, parent, dataList=[]):
QItemDelegate.__init__(self, parent)
self.dataList = dataList
def createEditor(self, parent, option, index):
combo = QComboBox(parent)
return combo
def setEditorData(self, editor, index):
text = index.data().toString()
index = editor.findText(text)
editor.setCurrentIndex(index)
def setModelData(self, editor, model, index):
model.setData(index, editor.itemText(editor.currentIndex()))
def updateEditorGeometry(self, editor, option, index):
print option, option.rect
editor.setGeometry(option.rect)
class GateWayListEditor(QComboBox):
def __init__(self, widget=None, dataList=[]):
super(GateWayListEditor, self).__init__(widget)
self.dataList = dataList
self.populateList()
def getGateWay(self):
gateWay = self.itemData(self.currentIndex(), QtCore.Qt.DisplayRole)
return gateWay
def setGateWay(self, gateWay):
self.setCurrentIndex(self.findData(gateWay, QtCore.Qt.DisplayRole))
def populateList(self):
for i, gateWayName in enumerate(self.dataList):
self.insertItem(i, gateWayName)
self.setItemData(i, gateWayName, QtCore.Qt.DisplayRole)
class GateWayListItemEditorCreator(QItemEditorCreatorBase):
def createWidget(self, parent):
return GateWayListEditor(parent, ["CTP", "LTS", "XTP", "FEMAS", "XSPEED", "QDP", "KSOTP", "KSGOLD", "SGIT"])