5108 lines · 155.9 KB · Python
RawDownload
		x = event.pos().x()		if x < 0:			return no_data		low0, pos0, time_from = self.XPixelToData(x)		low1, pos1, time_to = self.XPixelToData(x + 1)		hregions = set()		hregion_times = []		if not low1:			for i in xrange(pos0, pos1 + 1):				hregion = self.data.points[i].hregion				hregions.add(hregion)				if i == pos0:					time = time_from				else:					time = self.data.XToData(self.data.points[i].x)				hregion_times.append((hregion, time))		return (time_from, time_to, hregions, hregion_times) 	def hoverMoveEvent(self, event):		time_from, time_to, hregions, hregion_times = self.EventToData(event)		if time_from is not None:			self.event_handler.PointEvent(self.data.cpu, time_from, time_to, hregions) 	def hoverLeaveEvent(self, event):		self.event_handler.NoPointEvent() 	def mousePressEvent(self, event):		if event.button() != Qt.RightButton:			super(SwitchGraphDataGraphicsItem, self).mousePressEvent(event)			return		time_from, time_to, hregions, hregion_times = self.EventToData(event)		if hregion_times:			self.event_handler.RightClickEvent(self.data.cpu, hregion_times, event.screenPos()) # X-axis graphics item class XAxisGraphicsItem(QGraphicsItem): 	def __init__(self, width, parent=None):		super(XAxisGraphicsItem, self).__init__(parent) 		self.width = width		self.max_mark_sz = 4		self.height = self.max_mark_sz + 1 	def boundingRect(self):		return QRectF(0, 0, self.width, self.height) 	def Step(self):		attrs = self.parentItem().attrs		subrange = attrs.subrange.x		t = subrange.hi - subrange.lo		s = (3.0 * t) / self.width		n = 1.0		while s > n:			n = n * 10.0		return n 	def PaintMarks(self, painter, at_y, lo, hi, step, i):		attrs = self.parentItem().attrs		x = lo		while x <= hi:			xp = attrs.XToPixel(x)			if i % 10:				if i % 5:					sz = 1				else:					sz = 2			else:				sz = self.max_mark_sz				i = 0			painter.drawLine(xp, at_y, xp, at_y + sz)			x += step			i += 1 	def paint(self, painter, option, widget):		# Using QPainter::drawLine(int x1, int y1, int x2, int y2) so x2 = width -1		painter.drawLine(0, 0, self.width - 1, 0)		n = self.Step()		attrs = self.parentItem().attrs		subrange = attrs.subrange.x		if subrange.lo:			x_offset = n - (subrange.lo % n)		else:			x_offset = 0.0		x = subrange.lo + x_offset		i = (x / n) % 10		self.PaintMarks(painter, 0, x, subrange.hi, n, i) 	def ScaleDimensions(self):		n = self.Step()		attrs = self.parentItem().attrs		lo = attrs.subrange.x.lo		hi = (n * 10.0) + lo		width = attrs.XToPixel(hi)		if width > 500:			width = 0		return (n, lo, hi, width) 	def PaintScale(self, painter, at_x, at_y):		n, lo, hi, width = self.ScaleDimensions()		if not width:			return		painter.drawLine(at_x, at_y, at_x + width, at_y)		self.PaintMarks(painter, at_y, lo, hi, n, 0) 	def ScaleWidth(self):		n, lo, hi, width = self.ScaleDimensions()		return width 	def ScaleHeight(self):		return self.height 	def ScaleUnit(self):		return self.Step() * 10 # Scale graphics item base class class ScaleGraphicsItem(QGraphicsItem): 	def __init__(self, axis, parent=None):		super(ScaleGraphicsItem, self).__init__(parent)		self.axis = axis 	def boundingRect(self):		scale_width = self.axis.ScaleWidth()		if not scale_width:			return QRectF()		return QRectF(0, 0, self.axis.ScaleWidth() + 100, self.axis.ScaleHeight()) 	def paint(self, painter, option, widget):		scale_width = self.axis.ScaleWidth()		if not scale_width:			return		self.axis.PaintScale(painter, 0, 5)		x = scale_width + 4		painter.drawText(QPointF(x, 10), self.Text()) 	def Unit(self):		return self.axis.ScaleUnit() 	def Text(self):		return "" # Switch graph scale graphics item class SwitchScaleGraphicsItem(ScaleGraphicsItem): 	def __init__(self, axis, parent=None):		super(SwitchScaleGraphicsItem, self).__init__(axis, parent) 	def Text(self):		unit = self.Unit()		if unit >= 1000000000:			unit = int(unit / 1000000000)			us = "s"		elif unit >= 1000000:			unit = int(unit / 1000000)			us = "ms"		elif unit >= 1000:			unit = int(unit / 1000)			us = "us"		else:			unit = int(unit)			us = "ns"		return " = " + str(unit) + " " + us # Switch graph graphics item contains graph title, scale, x/y-axis, and the graphed data class SwitchGraphGraphicsItem(QGraphicsItem): 	def __init__(self, collection, data, attrs, event_handler, first, parent=None):		super(SwitchGraphGraphicsItem, self).__init__(parent)		self.collection = collection		self.data = data		self.attrs = attrs		self.event_handler = event_handler 		margin = 20		title_width = 50 		self.title_graphics = QGraphicsSimpleTextItem(data.title, self) 		self.title_graphics.setPos(margin, margin)		graph_width = attrs.XToPixel(attrs.subrange.x.hi) + 1		graph_height = attrs.YToPixel(attrs.subrange.y.hi) + 1 		self.graph_origin_x = margin + title_width + margin		self.graph_origin_y = graph_height + margin 		x_axis_size = 1		y_axis_size = 1		self.yline = QGraphicsLineItem(0, 0, 0, graph_height, self) 		self.x_axis = XAxisGraphicsItem(graph_width, self)		self.x_axis.setPos(self.graph_origin_x, self.graph_origin_y + 1) 		if first:			self.scale_item = SwitchScaleGraphicsItem(self.x_axis, self)			self.scale_item.setPos(self.graph_origin_x, self.graph_origin_y + 10) 		self.yline.setPos(self.graph_origin_x - y_axis_size, self.graph_origin_y - graph_height) 		self.axis_point = QGraphicsLineItem(0, 0, 0, 0, self)		self.axis_point.setPos(self.graph_origin_x - 1, self.graph_origin_y +1) 		self.width = self.graph_origin_x + graph_width + margin		self.height = self.graph_origin_y + margin 		self.graph = SwitchGraphDataGraphicsItem(data, graph_width, graph_height, attrs, event_handler, self)		self.graph.setPos(self.graph_origin_x, self.graph_origin_y - graph_height) 		if parent and 'EnableRubberBand' in dir(parent):			parent.EnableRubberBand(self.graph_origin_x, self.graph_origin_x + graph_width - 1, self) 	def boundingRect(self):		return QRectF(0, 0, self.width, self.height) 	def paint(self, painter, option, widget):		pass 	def RBXToPixel(self, x):		return self.attrs.PixelToX(x - self.graph_origin_x) 	def RBXRangeToPixel(self, x0, x1):		return (self.RBXToPixel(x0), self.RBXToPixel(x1 + 1)) 	def RBPixelToTime(self, x):		if x < self.data.points[0].x:			return self.data.XToData(0)		return self.data.XToData(x) 	def RBEventTimes(self, x0, x1):		x0, x1 = self.RBXRangeToPixel(x0, x1)		time_from = self.RBPixelToTime(x0)		time_to = self.RBPixelToTime(x1)		return (time_from, time_to) 	def RBEvent(self, x0, x1):		time_from, time_to = self.RBEventTimes(x0, x1)		self.event_handler.RangeEvent(time_from, time_to) 	def RBMoveEvent(self, x0, x1):		if x1 < x0:			x0, x1 = x1, x0		self.RBEvent(x0, x1) 	def RBReleaseEvent(self, x0, x1, selection_state):		if x1 < x0:			x0, x1 = x1, x0		x0, x1 = self.RBXRangeToPixel(x0, x1)		self.event_handler.SelectEvent(x0, x1, selection_state) # Graphics item to draw a vertical bracket (used to highlight "forward" sub-range) class VerticalBracketGraphicsItem(QGraphicsItem): 	def __init__(self, parent=None):		super(VerticalBracketGraphicsItem, self).__init__(parent) 		self.width = 0		self.height = 0		self.hide() 	def SetSize(self, width, height):		self.width = width + 1		self.height = height + 1 	def boundingRect(self):		return QRectF(0, 0, self.width, self.height) 	def paint(self, painter, option, widget):		colour = QColor(255, 255, 0, 32)		painter.fillRect(0, 0, self.width, self.height, colour)		x1 = self.width - 1		y1 = self.height - 1		painter.drawLine(0, 0, x1, 0)		painter.drawLine(0, 0, 0, 3)		painter.drawLine(x1, 0, x1, 3)		painter.drawLine(0, y1, x1, y1)		painter.drawLine(0, y1, 0, y1 - 3)		painter.drawLine(x1, y1, x1, y1 - 3) # Graphics item to contain graphs arranged vertically class VertcalGraphSetGraphicsItem(QGraphicsItem): 	def __init__(self, collection, attrs, event_handler, child_class, parent=None):		super(VertcalGraphSetGraphicsItem, self).__init__(parent) 		self.collection = collection 		self.top = 10 		self.width = 0		self.height = self.top 		self.rubber_band = None		self.rb_enabled = False 		first = True		for data in collection.data:			child = child_class(collection, data, attrs, event_handler, first, self)			child.setPos(0, self.height + 1)			rect = child.boundingRect()			if rect.right() > self.width:				self.width = rect.right()			self.height = self.height + rect.bottom() + 1			first = False 		self.bracket = VerticalBracketGraphicsItem(self) 	def EnableRubberBand(self, xlo, xhi, rb_event_handler):		if self.rb_enabled:			return		self.rb_enabled = True		self.rb_in_view = False		self.setAcceptedMouseButtons(Qt.LeftButton)		self.rb_xlo = xlo		self.rb_xhi = xhi		self.rb_event_handler = rb_event_handler		self.mousePressEvent = self.MousePressEvent		self.mouseMoveEvent = self.MouseMoveEvent		self.mouseReleaseEvent = self.MouseReleaseEvent 	def boundingRect(self):		return QRectF(0, 0, self.width, self.height) 	def paint(self, painter, option, widget):		pass 	def RubberBandParent(self):		scene = self.scene()		view = scene.views()[0]		viewport = view.viewport()		return viewport 	def RubberBandSetGeometry(self, rect):		scene_rectf = self.mapRectToScene(QRectF(rect))		scene = self.scene()		view = scene.views()[0]		poly = view.mapFromScene(scene_rectf)		self.rubber_band.setGeometry(poly.boundingRect()) 	def SetSelection(self, selection_state):		if self.rubber_band:			if selection_state:				self.RubberBandSetGeometry(selection_state)				self.rubber_band.show()			else:				self.rubber_band.hide() 	def SetBracket(self, rect):		if rect:			x, y, width, height = rect.x(), rect.y(), rect.width(), rect.height()			self.bracket.setPos(x, y)			self.bracket.SetSize(width, height)			self.bracket.show()		else:			self.bracket.hide() 	def RubberBandX(self, event):		x = event.pos().toPoint().x()		if x < self.rb_xlo:			x = self.rb_xlo		elif x > self.rb_xhi:			x = self.rb_xhi		else:			self.rb_in_view = True		return x 	def RubberBandRect(self, x):		if self.rb_origin.x() <= x:			width = x - self.rb_origin.x()			rect = QRect(self.rb_origin, QSize(width, self.height))		else:			width = self.rb_origin.x() - x			top_left = QPoint(self.rb_origin.x() - width, self.rb_origin.y())			rect = QRect(top_left, QSize(width, self.height))		return rect 	def MousePressEvent(self, event):		self.rb_in_view = False		x = self.RubberBandX(event)		self.rb_origin = QPoint(x, self.top)		if self.rubber_band is None:			self.rubber_band = QRubberBand(QRubberBand.Rectangle, self.RubberBandParent())		self.RubberBandSetGeometry(QRect(self.rb_origin, QSize(0, self.height)))		if self.rb_in_view:			self.rubber_band.show()			self.rb_event_handler.RBMoveEvent(x, x)		else:			self.rubber_band.hide() 	def MouseMoveEvent(self, event):		x = self.RubberBandX(event)		rect = self.RubberBandRect(x)		self.RubberBandSetGeometry(rect)		if self.rb_in_view:			self.rubber_band.show()			self.rb_event_handler.RBMoveEvent(self.rb_origin.x(), x) 	def MouseReleaseEvent(self, event):		x = self.RubberBandX(event)		if self.rb_in_view:			selection_state = self.RubberBandRect(x)		else:			selection_state = None		self.rb_event_handler.RBReleaseEvent(self.rb_origin.x(), x, selection_state) # Switch graph legend data model class SwitchGraphLegendModel(QAbstractTableModel): 	def __init__(self, collection, region_attributes, parent=None):		super(SwitchGraphLegendModel, self).__init__(parent) 		self.region_attributes = region_attributes 		self.child_items = sorted(collection.hregions.values(), key=GraphDataRegionOrdinal)		self.child_count = len(self.child_items) 		self.highlight_set = set() 		self.column_headers = ("pid", "tid", "comm") 	def rowCount(self, parent):		return self.child_count 	def headerData(self, section, orientation, role):		if role != Qt.DisplayRole:			return None		if orientation != Qt.Horizontal:			return None		return self.columnHeader(section) 	def index(self, row, column, parent):		return self.createIndex(row, column, self.child_items[row]) 	def columnCount(self, parent=None):		return len(self.column_headers) 	def columnHeader(self, column):		return self.column_headers[column] 	def data(self, index, role):		if role == Qt.BackgroundRole:			child = self.child_items[index.row()]			if child in self.highlight_set:				return self.region_attributes[child.key].colour			return None		if role == Qt.ForegroundRole:			child = self.child_items[index.row()]			if child in self.highlight_set:				return QColor(255, 255, 255)			return self.region_attributes[child.key].colour		if role != Qt.DisplayRole:			return None		hregion = self.child_items[index.row()]		col = index.column()		if col == 0:			return hregion.pid		if col == 1:			return hregion.tid		if col == 2:			return hregion.comm		return None 	def SetHighlight(self, row, set_highlight):		child = self.child_items[row]		top_left = self.createIndex(row, 0, child)		bottom_right = self.createIndex(row, len(self.column_headers) - 1, child)		self.dataChanged.emit(top_left, bottom_right) 	def Highlight(self, highlight_set):		for row in xrange(self.child_count):			child = self.child_items[row]			if child in self.highlight_set:				if child not in highlight_set:					self.SetHighlight(row, False)			elif child in highlight_set:				self.SetHighlight(row, True)		self.highlight_set = highlight_set # Switch graph legend is a table class SwitchGraphLegend(QWidget): 	def __init__(self, collection, region_attributes, parent=None):		super(SwitchGraphLegend, self).__init__(parent) 		self.data_model = SwitchGraphLegendModel(collection, region_attributes) 		self.model = QSortFilterProxyModel()		self.model.setSourceModel(self.data_model) 		self.view = QTableView()		self.view.setModel(self.model)		self.view.setEditTriggers(QAbstractItemView.NoEditTriggers)		self.view.verticalHeader().setVisible(False)

Lines 1501–2000 of 5108. Previous lines