def data(self, role):
if role in (Qt.EditRole, Qt.StatusTipRole):
return self.formula()
if role == Qt.DisplayRole:
return self.display()
t = str(self.display())
try:
number = int(t)
except ValueError:
number = None
if role == Qt.TextColorRole:
if number is None:
return QColor(Qt.black)
elif number < 0:
return QColor(Qt.red)
return QColor(Qt.blue)
if role == Qt.TextAlignmentRole:
if t and (t[0].isdigit() or t[0] == '-'):
return Qt.AlignRight | Qt.AlignVCenter
return super(SpreadSheetItem, self).data(role)
python类TextAlignmentRole()的实例源码
def data(self, role):
if role in (Qt.EditRole, Qt.StatusTipRole):
return self.formula()
if role == Qt.DisplayRole:
return self.display()
t = str(self.display())
try:
number = int(t)
except ValueError:
number = None
if role == Qt.TextColorRole:
if number is None:
return QColor(Qt.black)
elif number < 0:
return QColor(Qt.red)
return QColor(Qt.blue)
if role == Qt.TextAlignmentRole:
if t and (t[0].isdigit() or t[0] == '-'):
return Qt.AlignRight | Qt.AlignVCenter
return super(SpreadSheetItem, self).data(role)
def data(self, role):
if role in (Qt.EditRole, Qt.StatusTipRole):
return self.formula()
if role == Qt.DisplayRole:
return self.display()
t = str(self.display())
try:
number = int(t)
except ValueError:
number = None
if role == Qt.TextColorRole:
if number is None:
return QColor(Qt.black)
elif number < 0:
return QColor(Qt.red)
return QColor(Qt.blue)
if role == Qt.TextAlignmentRole:
if t and (t[0].isdigit() or t[0] == '-'):
return Qt.AlignRight | Qt.AlignVCenter
return super(SpreadSheetItem, self).data(role)
def data(self, index, role):
"""Return role specific data for the item referred by
index.column()."""
if not index.isValid():
pass
node = self.getNode(index)
column = index.column()
value = node.getItemData(column)
if role == Qt.DisplayRole:
try:
if column == 1:
return '{0:8.3f}'.format(value)
else:
return '{0:8.2f}'.format(value)
except ValueError:
return value
if role == Qt.EditRole:
return str(value)
if role == Qt.CheckStateRole:
if node.parent == self.rootNode and column == 0:
return node.getCheckState()
if role == Qt.TextAlignmentRole:
if column > 0:
return Qt.AlignRight
def test_performance(self):
self.cframe = self.form.compare_frame_controller
self.gframe = self.form.generator_tab_controller
self.form.ui.tabWidget.setCurrentIndex(2)
self.cframe.ui.cbProtoView.setCurrentIndex(0)
self.gframe.ui.cbViewType.setCurrentIndex(0)
proto = self.__build_protocol()
self.cframe.add_protocol(proto)
proto.qt_signals.protocol_updated.emit()
self.assertEqual(self.cframe.protocol_model.row_count, self.NUM_MESSAGES)
self.assertEqual(self.cframe.protocol_model.col_count, self.BITS_PER_MESSAGE)
self.__add_labels()
item = self.gframe.tree_model.rootItem.children[0].children[0]
index = self.gframe.tree_model.createIndex(0, 0, item)
rect = self.gframe.ui.treeProtocols.visualRect(index)
QTest.mousePress(self.gframe.ui.treeProtocols.viewport(), Qt.LeftButton, pos = rect.center())
self.assertEqual(self.gframe.ui.treeProtocols.selectedIndexes()[0], index)
mimedata = self.gframe.tree_model.mimeData(self.gframe.ui.treeProtocols.selectedIndexes())
t = time.time()
self.gframe.table_model.dropMimeData(mimedata, 1, -1, -1, self.gframe.table_model.createIndex(0, 0))
print("{0}: {1} s".format("Time for dropping mimedata", (time.time() - t)))
self.assertEqual(self.gframe.table_model.row_count, self.NUM_MESSAGES)
print("==============================00")
indx = self.gframe.table_model.createIndex(int(self.NUM_MESSAGES / 2), int(self.BITS_PER_MESSAGE / 2))
roles = (Qt.DisplayRole, Qt.BackgroundColorRole, Qt.TextAlignmentRole, Qt.TextColorRole, Qt.FontRole)
time_for_display = 100
for role in roles:
t = time.time()
self.gframe.table_model.data(indx, role = role)
microseconds = (time.time() - t) * 10 ** 6
self.assertLessEqual(microseconds, 2 * time_for_display, msg=self.__role_to_str(role))
if role == Qt.DisplayRole:
time_for_display = microseconds
print("{0}: {1} µs".format(self.__role_to_str(role), microseconds))
def __role_to_str(self, role):
if role == Qt.DisplayRole:
return "Display"
if role == Qt.BackgroundColorRole:
return "BG-Color"
if role == Qt.TextAlignmentRole:
return "Text-Alignment"
if role == Qt.TextColorRole:
return "TextColor"
if role == Qt.ToolTipRole:
return "ToolTip"
if role == Qt.FontRole:
return "Font"
def __role_to_str(self, role):
if role == Qt.DisplayRole:
return "Display"
if role == Qt.BackgroundColorRole:
return "BG-Color"
if role == Qt.TextAlignmentRole:
return "Text-Alignment"
if role == Qt.TextColorRole:
return "TextColor"
if role == Qt.ToolTipRole:
return "ToolTip"
if role == Qt.FontRole:
return "Font"
def headerData(self, section, orientation, role=Qt.DisplayRole):
if role == Qt.DisplayRole and orientation == Qt.Horizontal:
return self.header_labels[section]
elif role == Qt.TextAlignmentRole:
return Qt.AlignLeft
return super().headerData(section, orientation, role)
def data(self, index: QModelIndex, role=Qt.DisplayRole):
i, j = index.row(), index.column()
if role == Qt.DisplayRole:
try:
lbl = self.message_type[i]
except IndexError:
return False
if j == 0:
return lbl.name
elif j == 1:
return self.message.get_label_range(lbl, view=self.proto_view, decode=True)[0] + 1
elif j == 2:
return self.message.get_label_range(lbl, view=self.proto_view, decode=True)[1]
elif j == 3:
return lbl.color_index
elif j == 4:
return lbl.apply_decoding
elif role == Qt.TextAlignmentRole:
return Qt.AlignCenter
elif role == Qt.FontRole and j == 0:
font = QFont()
font.setItalic(self.message_type[i].field_type is None)
return font
else:
return None
def data(self, index, role=Qt.DisplayRole):
'''The data stored under the given role for the item referred
to by the index.
Args:
index (:obj:`QtCore.QModelIndex`): Index
role (:obj:`Qt.ItemDataRole`): Default :obj:`Qt.DisplayRole`
Returns:
data
'''
if role == Qt.DisplayRole:
row = self._data[index.row()]
if (index.column() == 0) and (type(row) != dict):
return row
elif index.column() < self.columnCount():
if type(row) == dict:
if self.header[index.column()] in row:
return row[self.header[index.column()]]
elif self.header[index.column()].lower() in row:
return row[self.header[index.column()].lower()]
return row[index.column()]
return None
elif role == Qt.FontRole:
return QtGui.QFont().setPointSize(30)
elif role == Qt.DecorationRole and index.column() == 0:
return None
elif role == Qt.TextAlignmentRole:
return Qt.AlignLeft;
def data(self, index, role):
if not index.isValid():
return None
if not (0 <= index.row() < self.rowCount()):
return None
elif role == Qt.FontRole:
return QtGui.QFont().setPointSize(30)
elif role == Qt.DecorationRole and index.column() == 0:
return None
elif role == Qt.TextAlignmentRole:
return Qt.AlignLeft;
# Color background
if role == Qt.BackgroundRole:
function = self._data[index.row()]
# Row is selected
if index.row() in self.rows_selected:
return FIRST.color_selected
# Data has been updated since original
if function.has_changed:
return FIRST.color_changed
#
if function.id is not None:
return FIRST.color_unchanged
# Return the default color
return FIRST.color_default
if role == Qt.DisplayRole:
function = self._data[index.row()]
column = index.column()
if 0 == column:
return '0x{0:X}'.format(function.address)
elif 1 == column:
return function.name
elif 2 == column:
return function.prototype
elif 3 == column:
return function.comment
return None
return super(FIRST.Model.Upload, self).data(index, role)