forked from zhanglongqi/pymodslave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModSlaveMBDataItemDelegate.py
70 lines (59 loc) · 2.42 KB
/
ModSlaveMBDataItemDelegate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#-------------------------------------------------------------------------------
# Name: ModSlaveMBDataItemDelegate
# Purpose:
#
# Author: elbar
#
# Created: 17/12/2012
# Copyright: (c) elbar 2012
# Licence: <your licence>
#-------------------------------------------------------------------------------
#!/usr/bin/env python
from PyQt4 import QtGui,QtCore
import Utils
#-------------------------------------------------------------------------------
class ModSlaveMBDataItemDelegate(QtGui.QStyledItemDelegate):
""" Modbus data model items delegate """
def __init__(self, discrete=False, data_type=0):#data type > 0 : decimal, 1 : hex
super(ModSlaveMBDataItemDelegate,self).__init__()
self._discrete = discrete
self._data_type = data_type
def paint(self, painter, option, index):
QtGui.QStyledItemDelegate.paint(self, painter, option, index)
def createEditor(self, parent, option, index):
# print("Create editor")
editor = QtGui.QLineEdit(parent)
if (self._discrete):
editor.setInputMask("b")
elif (self._data_type == 0):
editor.setInputMask("00000")
elif (self._data_type == 1):
editor.setInputMask("hhhh")
else:
editor.setInputMask("00000")
return editor
def setEditorData(self, editor, index):
# print("Set editor data")
value = (index.model()).data(index, QtCore.Qt.EditRole).toString()
editor.setText(value)
def setModelData(self, editor, model,index):
# print("Set model data")
value = str(editor.text())
if (self._data_type == 0): # decimal
_value = int(value, 10)
elif(self._data_type == 1): #hex
_value = int(value, 16)
if (not self._discrete): # check values
if (_value > 65535):
Utils.errorMessageBox("Value is greater than 65535.")
return
model.setData(index, value, QtCore.Qt.EditRole);
# emit SIGNAL for updating modbus data
self.emit(QtCore.SIGNAL("update_data"))
def updateEditorGeometry(self, editor, option, index):
# print("Update editor geometry")
editor.setGeometry(option.rect)
def set_data_type(self, data_type):
# print("Data Type = {0}".format(data_type))
self._data_type = data_type
#-------------------------------------------------------------------------------