def draw(self): spots = {'substrate': self.pattern.calculate_spots(), 'superstructures': [dom.calculate_spots() for dom in self.pattern.domains]} try: self.scene().removeItem(self.spots['substrate']) except: pass for group in ['spots', 'indices', 'vectors']: for domainGroup in eval("self.{}['superstructures']".format(group)): try: self.scene().removeItem(domainGroup) # maybe forcibly delete domainGroup here # rather than relying on Python's garbage collection except: continue # create new groups for group in ['spots', 'indices', 'vectors']: obj = eval('self.{}'.format(group)) obj['substrate'] = QtGui.QGraphicsItemGroup(self, self.scene()) obj['superstructures'] = [QtGui.QGraphicsItemGroup(self, self.scene()) for item in spots['superstructrues']] QtGui.QPainterPath for spot in spots['substrate']: self.indices['substrate'].addToGroup(self.draw_label(spot))
def createItemsGroup(self): """Container that treats a group of items as a single item One item is the contour itself Other items are chord, camber, point markers, etc. """ self.contour_group = QtGui.QGraphicsItemGroup(parent=self.contour_item, scene=self.scene) self.markers = QtGui.QGraphicsItemGroup(parent=self.contour_item, scene=self.scene)
def strudel(self, dist): attr = self.attributes[self.attributes_select[0]][0] attr = self.dataset.domain[attr] ss = np.sum(dist) box = QtGui.QGraphicsItemGroup() if ss < 1e-6: QtGui.QGraphicsRectItem(0, -10, 1, 10, box) cum = 0 for i, v in enumerate(dist): if v < 1e-6: continue if self.stretched: v /= ss v *= self.scale_x rect = QtGui.QGraphicsRectItem(cum + 1, -6, v - 2, 12, box) rect.setBrush(QtGui.QBrush(QtGui.QColor(*attr.colors[i]))) rect.setPen(QtGui.QPen(QtCore.Qt.NoPen)) if self.stretched: tooltip = "{}: {:.2f}%".format(attr.values[i], 100 * dist[i] / sum(dist)) else: tooltip = "{}: {}".format(attr.values[i], int(dist[i])) rect.setToolTip(tooltip) cum += v return box
def searchNullWords(self): self.nullWords = QtGui.QGraphicsItemGroup(scene=self.scene) log.info('search %s mapping for null words' % (self.mapping_name)) found = 0 tmpnull = [] start = self.mapping.start searcher = searcher.NullSearcher(self.mapping) for vaddr in searcher: offset = vaddr - start tmpnull.append(offset / searcher.WORDSIZE) # filter and regroup null word by bunches nb = 0 should = 0 for k, g in itertools.groupby(enumerate(tmpnull), lambda i_x: i_x[0] - i_x[1]): # still not getting lambdas.... rang = [v * 4 for v in map(operator.itemgetter(1), g)] # add an structure of len(range)*4 bytes self.nullWords.addToGroup( widgets.Structure(rang[0], Dummy(4 * len(rang), value=0), scene=self.scene, color=QtCore.Qt.gray)) log.debug("Adding a zero struct of %d len" % (len(rang))) nb += 1 should += len(rang) log.debug('Created %d qrect instead of %d words' % (nb, should)) self.scene.addItem(self.nullWords) self.nullWords.hide() self.nullWords.setEnabled(False) self.nullWords.setZValue(1) # zValue has to be > 0 # still not letting me trough the boundingRect self.nullWords.setToolTip('Null value words') return
def recreateFromData(self): # Remove all subitems. self.removeFromGroup(self.child) self.scene().removeItem(self.child) self.child = QtGui.QGraphicsItemGroup() self.child.setParentItem(self) # Do nothing if no data are loaded. if len(self.data.fitdata.values) == 0: return timeModifier = self.width / float(self.data.timeSpan) valueModifier = self.height / float(self.data.valueSpan) lastTime = None lastValue = None for t in range( 0, self.data.fitTimePointer[1] - self.data.fitTimePointer[0] + 1): time = (self.data.time[self.data.fitTimePointer[0] + t] - self.data.minTime) * timeModifier fit = self.height - (self.data.fitdata.values[t] - self.data.minValue) * valueModifier if lastTime is not None: line = QtGui.QGraphicsLineItem( QtCore.QLineF(lastTime, lastFit, time, fit)) line.setPen(self.pen) line.setParentItem(self.child) lastTime = time lastFit = fit
def drawMesh(self, airfoil): self.toggleSplinePoints() # delete old mesh if existing if hasattr(airfoil, 'mesh'): self.parent.scene.removeItem(airfoil.mesh) airfoil.mesh = QtGui.QGraphicsItemGroup(parent=airfoil.contour_item, scene=self.parent.scene) for block in self.tunnel.blocks: for lines in [block.getULines(), block.getVLines()]: for line in lines: # instantiate a graphics item contour = gc.GraphicsCollection() # make it polygon type and populate its points points = [QtCore.QPointF(x, y) for x, y in line] contour.Polyline(QtGui.QPolygonF(points), '') # set its properties contour.pen.setColor(QtGui.QColor(0, 0, 0, 255)) contour.pen.setWidth(0.8) contour.pen.setCosmetic(True) contour.brush.setStyle(QtCore.Qt.NoBrush) # add contour as a GraphicsItem to the scene # these are the objects which are drawn in the GraphicsView meshline = PGraphicsItem.GraphicsItem( contour, self.parent.scene) airfoil.mesh.addToGroup(meshline) airfoil.contour_group.addToGroup(airfoil.mesh)
def arrow(self, sx, sy, ex, ey, arrow_size=5, color=None, penwidth=None): """Mimicks canvas api. See openexp._canvas.canvas.""" a = math.atan2(ey - sy, ex - sx) sx1 = ex + arrow_size * math.cos(a + math.radians(135)) sy1 = ey + arrow_size * math.sin(a + math.radians(135)) sx2 = ex + arrow_size * math.cos(a + math.radians(225)) sy2 = ey + arrow_size * math.sin(a + math.radians(225)) group = QtGui.QGraphicsItemGroup() i = self.line(sx, sy, ex, ey, color=color, penwidth=penwidth, add=False) group.addToGroup(i) i = self.line(sx1, sy1, ex, ey, color=color, penwidth=penwidth, add=False) group.addToGroup(i) i = self.line(sx2, sy2, ex, ey, color=color, penwidth=penwidth, add=False) group.addToGroup(i) self.addItem(group) return group
def box_group(self, stat, height=20): def line(x0, y0, x1, y1, *args): return QtGui.QGraphicsLineItem(x0 * scale_x, y0, x1 * scale_x, y1, *args) scale_x = self.scale_x box = QtGui.QGraphicsItemGroup() whisker1 = line(stat.a_min, -1.5, stat.a_min, 1.5, box) whisker2 = line(stat.a_max, -1.5, stat.a_max, 1.5, box) vert_line = line(stat.a_min, 0, stat.a_max, 0, box) mean_line = line(stat.mean, -height / 3, stat.mean, height / 3, box) for it in (whisker1, whisker2, mean_line): it.setPen(self._pen_paramet) vert_line.setPen(self._pen_dotted) var_line = line(stat.mean - stat.dev, 0, stat.mean + stat.dev, 0, box) var_line.setPen(self._pen_paramet) mbox = QtGui.QGraphicsRectItem(stat.q25 * scale_x, -height / 2, (stat.q75 - stat.q25) * scale_x, height, box) mbox.setBrush(self._box_brush) mbox.setPen(QtGui.QPen(QtCore.Qt.NoPen)) mbox.setZValue(-200) median_line = line(stat.median, -height / 2, stat.median, height / 2, box) median_line.setPen(self._pen_median) median_line.setZValue(-150) return box
def searchNullWords(self): self.nullWords = QtGui.QGraphicsItemGroup(scene=self.scene) log.info('search %s mapping for null words' % (self.mapping_name)) found = 0 tmpnull = [] start = self.mapping.start searcher = signature.NullSearcher(self.mapping) for vaddr in searcher: offset = vaddr - start tmpnull.append(offset / searcher.WORDSIZE) # filter and regroup null word by bunches nb = 0 should = 0 for k, g in itertools.groupby(enumerate(tmpnull), lambda (i, x): i - x): rang = [v * 4 for v in map(operator.itemgetter(1), g) ] # still not getting lambdas.... # add an structure of len(range)*4 bytes self.nullWords.addToGroup( widgets.Structure(rang[0], Dummy(4 * len(rang), value=0), scene=self.scene, color=QtCore.Qt.gray)) log.debug("Adding a zero struct of %d len" % (len(rang))) nb += 1 should += len(rang)
def add_crossing(self, lane_id, lane_type, points): row = Row("lane-id=%s, lane-type=%s \"(crossing)\"" % (lane_id, lane_type)) for point in points: row += self.add_point(point) # Uncomment the next 2 lines to make "Victoria-Street/34450/4550" looks nice. #if len(points) > 2: # self.arrange_by_x(points) group = QtGui.QGraphicsItemGroup() for i in range(len(points) - 1): p1 = points[i] p2 = points[i + 1] line = self.scene.addLine(p1.x, p1.y, p2.x, p2.y, blue) group.addToGroup(line) self.scene.addItem(group) row.data = group self.selector.crossings.append(group) if self.selector.are_crossings_hidden(): group.hide() return row
def update(self): # Remove the old axis. self.removeFromGroup(self.child) self.scene().removeItem(self.child) self.child = QtGui.QGraphicsItemGroup() self.child.setParentItem(self) line = QtGui.QGraphicsLineItem(QtCore.QLineF(0, 0, self.width, 0)) line.setParentItem(self.child) # Calculate and draw tics. timeSpan = float(self.maxTime - self.minTime) bigTicSpan = float(1e10) while math.fmod(timeSpan, bigTicSpan) == timeSpan: bigTicSpan /= 10 if timeSpan / bigTicSpan < 2: bigTicSpan /= 10 ticSpan = bigTicSpan / 10 ticTime = self.minTime - math.fmod(self.minTime, ticSpan) if ticTime < self.minTime: ticTime += ticSpan bigTicTime = self.minTime + bigTicSpan - math.fmod( self.minTime, bigTicSpan) count = 10 - int((bigTicTime - self.minTime) / ticSpan) time = ticTime maxValuesHeight = 0 while time < self.maxTime: ticlen = 10 ticx = ((time - self.minTime) * self.width) / timeSpan if count % 10 == 0: # Normalize time diff = math.fmod(time, ticSpan) time = time - diff if diff > ticSpan / 2.0: time = time + ticSpan elif -diff > ticSpan / 2.0: time = time - ticSpan text = QtGui.QGraphicsTextItem(str(time)) text.setFont(variables.timeAxisValuesFont.value()) text.setPos(ticx - text.boundingRect().width() / 2, 13) maxValuesHeight = max(maxValuesHeight, text.boundingRect().height()) text.setParentItem(self.child) ticlen = 15 tic = QtGui.QGraphicsLineItem(QtCore.QLineF(ticx, 0, ticx, ticlen)) tic.setParentItem(self.child) time += ticSpan count += 1 # Sets and displays axis label. if variables.timeAxisCaptionEnabled.value(): text = QtGui.QGraphicsTextItem(variables.timeAxisCaption.value()) text.setFont(variables.timeAxisCaptionFont.value()) text.setPos(self.width - text.boundingRect().width(), maxValuesHeight) text.setParentItem(self.child)
def __init__(self, data, parent=None): super(Fit, self).__init__(parent) self.data = data self.pen = QtGui.QPen() self.pen.setWidth(3) self.pen.setColor(QtGui.QColor("#882222")) self.child = QtGui.QGraphicsItemGroup() self.child.setParentItem(self)
def __init__(self, graphicsScene, color, withDetails=False, parent=None): QtGui.QWidget.__init__(self, parent) # model self.graphicsScene = graphicsScene self.gitemgroup = QtGui.QGraphicsItemGroup(scene=self.graphicsScene) self.gitemgroup.setZValue(20) # zValue has to be > 0 self.color = color self.showDetailledView = withDetails self._init()
def show_structures_allocated(self, context): self.allocated_structures = QtGui.QGraphicsItemGroup(scene=self.scene) def offset(vaddr): # FIXME return vaddr - context.heap.start for s in context.listStructures(): #offset, value, color = QtCore.Qt.green, scene=None, parent=None): self.allocated_structures.addToGroup(widgets.Structure(offset(s._vaddr), s, scene = self.scene, color = QtCore.Qt.blue) ) return
def create_plus_sign(self, pt, color=red, width=100): horizontal = self.scene.addLine(pt.x - width, pt.y, pt.x + width, pt.y, color) vertical = self.scene.addLine(pt.x, pt.y - width, pt.x, pt.y + width, color) group = QtGui.QGraphicsItemGroup() group.addToGroup(horizontal) group.addToGroup(vertical) self.scene.addItem(group) return group
def searchValue(self, value): ''' value is what type ? ''' resultGroup = QtGui.QGraphicsItemGroup() #self.graphicsView.GetScene().createItemGroup(items) log.debug('parsing %s mapping for value %s'%(self.mapping_name, value)) found = 0 for res in self.mapping.search(value): found +=1 resultGroup.addToGroup(widgets.Word(offset,value,scene = self.scene, color = QtCore.Qt.yellow) ) resultGroup.show() self.scene.addItem(resultGroup) return resultGroup
def __setup(self): self.__clear() font = self.font() group = QtGui.QGraphicsItemGroup(self) for text in self.__items: t = QtGui.QGraphicsSimpleTextItem(text, group) t.setData(0, text) t.setFont(font) t.setToolTip(text) self.__textitems.append(t)
def label_group(self, stat, attr, mean_lab): def centered_text(val, pos): t = QtGui.QGraphicsSimpleTextItem( "%.*f" % (attr.number_of_decimals + 1, val), labels) t.setFont(self._label_font) bbox = t.boundingRect() t.setPos(pos - bbox.width() / 2, 22) return t def line(x, down=1): QtGui.QGraphicsLineItem(x, 12 * down, x, 20 * down, labels) def move_label(label, frm, to): label.setX(to) to += t_box.width() / 2 path = QtGui.QPainterPath() path.lineTo(0, 4) path.lineTo(to - frm, 4) path.lineTo(to - frm, 8) p = QtGui.QGraphicsPathItem(path) p.setPos(frm, 12) labels.addToGroup(p) labels = QtGui.QGraphicsItemGroup() labels.addToGroup(mean_lab) m = stat.mean * self.scale_x mean_lab.setPos(m, -22) line(m, -1) msc = stat.median * self.scale_x med_t = centered_text(stat.median, msc) med_box_width2 = med_t.boundingRect().width() line(msc) x = stat.q25 * self.scale_x t = centered_text(stat.q25, x) t_box = t.boundingRect() med_left = msc - med_box_width2 if x + t_box.width() / 2 >= med_left - 5: move_label(t, x, med_left - t_box.width() - 5) else: line(x) x = stat.q75 * self.scale_x t = centered_text(stat.q75, x) t_box = t.boundingRect() med_right = msc + med_box_width2 if x - t_box.width() / 2 <= med_right + 5: move_label(t, x, med_right + 5) else: line(x) return labels
def _init_ui(self): scene = QtGui.QGraphicsScene() self.setScene(scene) group = QtGui.QGraphicsItemGroup() scene.addItem(group) text = scene.addText("Test") group.addToGroup(text) br = text.boundingRect() rect = scene.addRect(br) group.addToGroup(rect) group.setFlag(QtGui.QGraphicsItem.ItemIsMovable) self.setInteractive(True)
def __init__(self, id, length, width, graphics_scene): self.id = id item1 = QtGui.QGraphicsRectItem(-length / 2, -width / 2, length - 100, width) item2 = QtGui.QGraphicsRectItem(length / 2 - 100, -width / 2, 100, width) item2.setBrush(QtCore.Qt.black) item = QtGui.QGraphicsItemGroup() item.addToGroup(item1) item.addToGroup(item2) graphics_scene.addItem(item) item.hide() self.graphics_item = item
def searchPointers(self): self.pointers = QtGui.QGraphicsItemGroup(scene=self.scene) log.info('search %s mapping for pointer'%(self.mapping_name)) found = 0 start = self.mapping.start searcher = pointerfinder.PointerSearcher(self.mapping) for vaddr in searcher: word = self.mapping.readWord(vaddr) #searcher should return [(offset, value)] offset = vaddr - start self.pointers.addToGroup(widgets.Word(offset, word, scene = self.scene, color = QtCore.Qt.red) ) # fill the scene self.scene.addItem(self.pointers) self.pointers.hide() self.pointers.setZValue(10) # zValue has to be > 0 #self.pointers.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, False) return
def fixdot(self, x=None, y=None, color=None, style=u'default'): """Mimicks canvas api. See openexp._canvas.canvas.""" color = self._color(color) x = self._x(x) y = self._y(y) group = QtGui.QGraphicsItemGroup() h = 2 known = True if style == u'default': style = u'open-medium' if u'large' in style: s = 16 elif u'medium' in style: s = 8 elif u'small' in style: s = 4 else: known = False s = 8 if u'open' in style: i = self.ellipse(x-s, y-s, 2*s, 2*s, fill=True, color=color, add=False) group.addToGroup(i) i = self.ellipse(x-h, y-h, 2*h, 2*h, fill=True, color=self.background_color, add=False) group.addToGroup(i) elif u'filled' in style: i = self.ellipse(x-s, y-s, 2*s, 2*s, fill=True, color=color, add=True) group.addToGroup(i) elif u'cross' in style: i = self.line(x, y-s, x, y+s, color=color, add=False, penwidth=1) group.addToGroup(i) i = self.line(x-s, y, x+s, y, color=color, add=False, penwidth=1) group.addToGroup(i) else: i = self.ellipse(x-s, y-s, 2*s, 2*s, fill=True, color=color, add=True) group.addToGroup(i) known = False self.addItem(group) if not known: self.notify(_(u'Fixdot style "%s" is unknown or variably defined') \ % style) return group
def __init__(self, parent): super(PhotoViewer, self).__init__(parent) self._zoom = 0 self._empty = True self._scene = QtGui.QGraphicsScene(self) self._photo = QtGui.QGraphicsPixmapItem() self._scene.addItem(self._photo) self.setScene(self._scene) self.setTransformationAnchor(QtGui.QGraphicsView.AnchorUnderMouse) self.setResizeAnchor(QtGui.QGraphicsView.AnchorUnderMouse) self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded) self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded) self.setBackgroundBrush(QtGui.QBrush(QtGui.QColor(30, 30, 30))) self.setFrameShape(QtGui.QFrame.NoFrame) self.highlight = QtGui.QGraphicsRectItem() self._scene.addItem(self.highlight) self.annotation_items = QtGui.QGraphicsItemGroup(scene=self._scene)
def add_kerb_line(self, points): if len(points) > 2: self.arrange_by_x(points) group = QtGui.QGraphicsItemGroup() for i in range(len(points) - 1): p1 = points[i] p2 = points[i + 1] line = self.scene.addLine(p1.x, p1.y, p2.x, p2.y, magenta) group.addToGroup(line) self.scene.addItem(group) self.selector.kerb_lines.append(group) if self.selector.are_kerb_lines_hidden(): group.hide() return group
def strudel(self, dist): ss = np.sum(dist) box = QtGui.QGraphicsItemGroup() if ss < 1e-6: QtGui.QGraphicsRectItem(0, -10, 1, 10, box) cum = 0 get_color = self.discPalette.getRGB for i, v in enumerate(dist): if v < 1e-6: continue if self.stretched: v /= ss v *= self.scale_x rect = QtGui.QGraphicsRectItem(cum + 1, -6, v - 2, 12, box) rect.setBrush(QtGui.QBrush(QtGui.QColor(*get_color(i)))) rect.setPen(QtGui.QPen(QtCore.Qt.NoPen)) cum += v return box
def addMarkersSpline(self): """Create marker for polygon contour""" self.markersSpline = QtGui.QGraphicsItemGroup(parent=self.contour_item, scene=self.scene) for x, y in zip(*self.spline_data[0]): # put airfoil contour points as graphicsitem points = gc.GraphicsCollection() points.pen.setColor(QtGui.QColor(60, 60, 80, 255)) points.brush.setColor(QtGui.QColor(180, 180, 50, 230)) points.pen.setCosmetic(True) # no pen thickness change when zoomed points.Circle(x, y, 0.003, marker=True) marker = PGraphicsItem.GraphicsItem(points, self.scene) self.markersSpline.addToGroup(marker) self.contour_group.addToGroup(self.markersSpline)
def recreateFromData(self): # Remove all subitems. self.removeFromGroup(self.child) self.scene().removeItem(self.child) self.child = QtGui.QGraphicsItemGroup() self.child.setParentItem(self) timeModifier = self.width / float(self.data.timeSpan) valueModifier = self.height / float(self.data.valueSpan) lastTime = None lastValue = None for t in range(0, len(self.data.time)): time = (self.data.time[t] - self.data.minTime) * timeModifier value = self.height - (self.data.values[t] - self.data.minValue) * valueModifier if lastTime is not None: line = QtGui.QGraphicsLineItem(QtCore.QLineF(lastTime, lastValue, time, value)) line.setParentItem(self.child) lastTime = time lastValue = value
def updateSelectionArea(self): mask = self.selectionMask() brush = self._stylebrush[mask.astype(int)] self._item.setBrush(brush) if self._selitem is not None: self.removeItem(self._selitem) self._selitem = None if self.__selectiondelegate is not None: self._selitem = QtGui.QGraphicsItemGroup() self._selitem.dataBounds = \ lambda axis, frac=1.0, orthoRange=None: None for p1, p2 in self.__selectiondelegate.selection: r = QRectF(p1, p2).normalized() ritem = QtGui.QGraphicsRectItem(r, self._selitem) ritem.setBrush(QtGui.QBrush(Qt.NoBrush)) ritem.setPen(QtGui.QPen(Qt.red, 0)) self.addItem(self._selitem)
def add_annotations(self, annotations, pattern): self._scene.removeItem(self.annotation_items) self._scene.update() self.annotation_items = QtGui.QGraphicsItemGroup(scene=self._scene) for stain in pattern.stains: text = "" if annotations['outline']: self.add_outline(stain) if annotations['ellipse']: self.add_ellipse(stain) if annotations['id']: text += str(stain.id) if annotations['directionality']: text += " " + str(stain.direction()) if annotations['center']: self.add_center(stain) if annotations['gamma']: text += " " + str(stain.orientaton()[1]) if annotations['direction_line']: self.add_direction_line(stain) self.add_text(stain, text)
def strudel(self, dist): attr_ind = self.attributes_select[0] attr = self.ddataset.domain[attr_ind] ss = np.sum(dist) box = QtGui.QGraphicsItemGroup() if ss < 1e-6: QtGui.QGraphicsRectItem(0, -10, 1, 10, box) cum = 0 get_color = self.discPalette.getRGB for i, v in enumerate(dist): if v < 1e-6: continue if self.stretched: v /= ss v *= self.scale_x rect = QtGui.QGraphicsRectItem(cum + 1, -6, v - 2, 12, box) rect.setBrush(QtGui.QBrush(QtGui.QColor(*get_color(i)))) rect.setPen(QtGui.QPen(QtCore.Qt.NoPen)) rect.setToolTip(attr.values[i]) cum += v return box