def drawPoints(self): img = qrcode.make('TestCode 001',image_factory=qrcode.image.svg.SvgImage ) type(img) gitem = QGraphicsScene() self.graphicsView.setScene(gitem) #gitem.addLine(0, 0, 2, 2) qbrush = QBrush() qbrush.setStyle(Qt.SolidPattern) qbrush.setColor(Qt.black) gitem.addRect(0, 0, 2, 2, brush=qbrush ) self.graphicsView.show()
class Plotter(QObject): def __init__(self, dimensions, main_title, x_title, y_title): QObject.__init__(self) self.dimensions = dimensions self.scene = QGraphicsScene() self.main_title = TextItem(main_title, self.scene, self.dimensions, main_title_transform) self.x_title = TextItem(x_title, self.scene, self.dimensions, x_title_transform) self.y_title = TextItem(y_title, self.scene, self.dimensions, y_title_transform) if config.debug.plotter.show_margin_boxes: self.debug_margin_boxes = [] dimensions.changed.connect(self.show_margin_boxes) self.show_margin_boxes() def show_margin_boxes(self): for rect in self.debug_margin_boxes: self.scene.removeItem(rect) r = [] r.append(self.scene.addRect(0,0, self.dimensions.left_margin, self.dimensions.height, red, red_t)) r.append(self.scene.addRect(0,0, self.dimensions.width, self.dimensions.top_margin, green, green_t)) r.append(self.scene.addRect(0, self.dimensions.top_margin + self.dimensions.grid_height, self.dimensions.width, self.dimensions.bottom_margin, blue, blue_t)) r.append(self.scene.addRect(self.dimensions.width, self.dimensions.height, -self.dimensions.right_margin, -self.dimensions.height, cyan, cyan_t)) halfway_height = self.dimensions.top_margin + self.dimensions.grid_height/2 r.append(self.scene.addLine(0, halfway_height, self.dimensions.width, halfway_height, QPen(black, 0))) halfway_width = self.dimensions.left_margin + self.dimensions.grid_width/2 r.append(self.scene.addLine(halfway_width, 0, halfway_width, self.dimensions.height, QPen(black, 0))) self.debug_margin_boxes = r
def Paint(self): scene = QGraphicsScene() hor = self.ui.horizontal.currentIndex() vert = self.ui.vertical.currentIndex() v = {"a": [], "d": [], "c": [], "r": [], "f": []} h = {"a": [], "d": [], "c": [], "r": [], "f": []} for alg in self.algs: projects = [] if hor == 0: projects = sorted(self.stats[alg].values(),key=lambda x: 1.0 / 3.0 * (x["computersload"] + x["storesload"] + x["ramload"])) elif hor == 1: projects = sorted(self.stats[alg].values(),key=lambda x: 0.5*(x["computersload"]+x["ramload"])) elif hor == 2: projects = sorted(self.stats[alg].values(),key=lambda x: x["computersload"]) elif hor == 3: projects = sorted(self.stats[alg].values(),key=lambda x: x["ramload"]) elif hor == 4: projects = sorted(self.stats[alg].values(),key=lambda x: x["storesload"]) if projects == []: return for proj in projects: if vert == 0: v[alg].append(proj["assigned"]) elif vert == 1: v[alg].append(proj["ratio"]) elif vert == 2: v[alg].append(proj["replicas"]) if hor == 0: h[alg].append(1.0 / 3.0 * (proj["computersload"] + proj["storesload"] + proj["ramload"])) elif hor == 1: h[alg].append(0.5 * (proj["computersload"] + proj["ramload"])) elif hor == 2: h[alg].append(proj["computersload"]) elif hor == 3: h[alg].append(proj["ramload"]) elif hor == 4: h[alg].append(proj["storesload"]) maxnum = max([max(v[i]) for i in self.algs] + [1]) scene.addLine(5, 5, 5, 213, QPen(self.settings["axis"])) scene.addLine(2, 210, 210, 210, QPen(self.settings["axis"])) for i in range(10): scene.addLine(5 + (i + 1) * 20, 209, 5 + (i + 1) * 20, 211, QPen(self.settings["axis"])) scene.addLine(4, 210 - (i + 1) * 20, 6, 210 - (i + 1) * 20, QPen(self.settings["axis"])) font = QFont() font.setPointSize(6) t1 = scene.addText(str(0.1*(i + 1)), font) t1.setPos((i + 1) * 20, 212) if int(0.1*maxnum*(i + 1)) != 0: t2 = scene.addText(str(int(0.1*maxnum*(i + 1))), font) t2.setPos(-10, 200 - (i + 1) * 20) legendy = 10 for alg in self.algs: x0 = h[alg][0] y0 = v[alg][0] color = QColor(random.randint(1, 255), random.randint(1, 255), random.randint(1, 255)) brush = QBrush(color) color = QPen(color) for x,y in zip(h[alg],v[alg]): scene.addLine(5 + x0 * 200, 210 - float(y0)/maxnum * 200, 5 + x * 200, 210 - float(y)/maxnum * 200, color) scene.addLine(5 + x * 200 - 2, 210 - float(y)/maxnum * 200 - 2 , 5 + x * 200 + 2, 210 - float(y)/maxnum * 200 + 2, color) scene.addLine(5 + x * 200 + 2, 210 - float(y)/maxnum * 200 - 2, 5 + x * 200 - 2, 210 - float(y)/maxnum * 200 + 2, color) x0 = x y0 = y scene.addRect(220, legendy, 5, 5, color, brush) t = scene.addText(algnames[alg], font) t.setPos(230, legendy-5) legendy += 10 self.ui.graph.setScene(scene)
class PaintArea(QGraphicsView): def __init__(self, parent): QGraphicsView.__init__(self) self.init(parent) self.initShape() self.initCall = 2 self.minimumWidth = 550 self.setEnabled(False) def init(self, parent): self.timeline = parent self.pixmap = None self.image = None def initShape(self): self.scene = QGraphicsScene() self.setScene(self.scene) self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.setAlignment(Qt.AlignLeft) self.clicStart = None self.selectionRect = None def resizeEvent(self, sizEvent): if self.selectionRect and self.scene: self.scene.removeItem(self.selectionRect) self.selectionRect = None self.clicStart = None self.width = sizEvent.size().width() self.height = sizEvent.size().height() if self.initCall > 0: self.timeline.options.setMaximumWidth(self.timeline.options.minimumSizeHint().width()) self.initCall -= 1 if self.width < self.minimumWidth: self.timeline.setStateInfo('Unable to draw - Not enought width') else: self.timeline.workerThread.render() def mousePressEvent(self, mouseEvent): self.clicStart = mouseEvent.pos() if self.clicStart.x() < self.timeline.draw.yLeftMargin - 1: self.clicStart.setX(self.timeline.draw.yLeftMargin - 1) if self.clicStart.x() > self.timeline.ploter.width - self.timeline.m + 1: self.clicStart.setX(self.timeline.ploter.width - self.timeline.m + 1) if self.selectionRect: for item in self.scene.items(): if str(type(item)) == "<class 'PyQt4.QtGui.QGraphicsRectItem'>": self.scene.removeItem(item) self.selectionRect = None self.timeline.options.zoomButton.setEnabled(False) self.timeline.options.exportButton.setEnabled(False) self.timeline.options.selectedNodes.setText('Nothing selected') if self.timeline.selDateMin: self.timeline.options.selStartTime.setText('From ' + str(self.timeline.fromUSec(self.timeline.baseDateMin).strftime('%d.%m.%Y %H:%M:%S'))) self.timeline.options.selEndTime.setText('To ' + str(self.timeline.fromUSec(self.timeline.baseDateMax).strftime('%d.%m.%Y %H:%M:%S'))) else: self.timeline.options.selStartTime.setText('No selection start time') self.timeline.options.selEndTime.setText('No selection end time') self.timeline.options.selectedNodes.setText('Nothing selected') def mouseMoveEvent(self, dragMoveEvent): if self.clicStart: if self.clicStart.x() < dragMoveEvent.x(): x = self.clicStart.x() w = dragMoveEvent.x() - self.clicStart.x() else: x = dragMoveEvent.x() w = self.clicStart.x() - dragMoveEvent.x() # Limit rectangle to selectable area if x < self.timeline.draw.yLeftMargin - 1: x = self.timeline.draw.yLeftMargin - 1 w = self.clicStart.x() - x if x > self.timeline.ploter.width - self.timeline.m + 1: x = self.timeline.ploter.width - self.timeline.m + 1 w = 0 if x + w > self.timeline.ploter.width - self.timeline.m + 1: w = ((self.timeline.ploter.width - self.timeline.m + 1) - x) y = (self.timeline.m / 3) - 1 h = self.height - self.timeline.m - self.timeline.m / 3 + 2 if self.selectionRect and self.scene: self.scene.removeItem(self.selectionRect) self.selectionRect = self.scene.addRect(QRectF(x, y, w, h), QPen(Qt.DashDotDotLine)) def mouseReleaseEvent(self, mouseEvent): if self.selectionRect: if self.clicStart.x() > mouseEvent.x(): x1 = mouseEvent.x() x2 = self.clicStart.x() else: x1 = self.clicStart.x() x2 = mouseEvent.x() self.timeline.nodesInRange(x1, x2) start = self.timeline.draw.findXTime(x1) if start: self.timeline.options.selStartTime.setText('From ' + str(start.strftime('%d.%m.%Y %H:%M:%S'))) end = self.timeline.draw.findXTime(x2) if end: self.timeline.options.selEndTime.setText('To ' + str(end.strftime('%d.%m.%Y %H:%M:%S')))
# We must override these or else the default implementation prevents # the mouseMoveEvent() override from working. def mousePressEvent(self, event): pass def mouseReleaseEvent(self, event): pass if __name__ == "__main__": app = QApplication([]) rect = QRectF(0, 0, 150, 150) scene = QGraphicsScene() scene.addRect(rect) view = QGraphicsView(scene) view.setSceneRect(rect) w = QWidget() w.setLayout(QVBoxLayout()) w.layout().addWidget(view) w.layout().addWidget(QSizeGrip(view)) # w.layout().addSpacerItem( QSpacerItem(0, 0, QSizePolicy.Expanding) ) w.show() w.raise_() box = DraggableBox(None, scene=scene) box.setPos(10, 20) #box.setRotation(45)
class Canvas(QGraphicsView): def __init__(self, window, resultDict, imagePath): QGraphicsView.__init__(self) self.window = window self.pen = QPen(QColor("red")) self.pen.setWidth(0.5) self.canvasScene = QGraphicsScene() self.setScene(self.canvasScene) self.resultDict = resultDict self.imagePath = imagePath self.setBackgroundBrush(QBrush(Qt.black, Qt.SolidPattern)) def drawImage(self, imageFile): """Draw an image on the canvas""" image = QPixmap(imageFile) self.canvasScene.addPixmap(image) return image def drawFeaturePoint(self, pointList): """Draw a feature point on the canvas""" radius = 0.5 width, height = 2, 2 x1, y1, x2, y2 = pointList #Draw ellipse and bounding rect. Is a hacked version! self.canvasScene.addEllipse(x1 - radius + 5, y1 - radius + 3, 2 * radius, 2 * radius, self.pen) self.canvasScene.addEllipse(x2 - radius + self.imageWidth + 10, y2 - radius + 3, 2 * radius, 2 * radius, self.pen) self.canvasScene.addRect(x1 - width / 2. + 5, y1 - height / 2. + 3, width, height, self.pen) self.canvasScene.addRect(x2 - width / 2. + self.imageWidth + 10, y2 - height / 2. + 3, width, height, self.pen) def drawFeatureImages(self, imageFile): """Draw two consecutive images on the screen""" #Load image files path, file_ = os.path.split(imageFile) image1 = QPixmap(os.path.join(path, 'first_' + file_)) image2 = QPixmap(os.path.join(path, 'second_' + file_)) self.imageWidth = image1.width() #Add pixmaps image1Map = self.canvasScene.addPixmap(image1) image2Map = self.canvasScene.addPixmap(image2) #Shift pixmaps to the right position image1Map.setOffset(QPointF(5, 3)) image2Map.setOffset(QPointF(10 + image1.width(), 3)) def drawPolygon(self, Polygon): """Draw a polygon on the canvas""" polygon = QPolygonF() for point in Polygon: polygon.append(QPointF(point[0], point[1])) self.canvasScene.addPolygon(polygon, self.pen) def getWorkerId(self): return self.resultDict.values()[self.index][0][0] def getAssignmentId(self): return self.resultDict.keys()[self.index] def nextImage(self): """Load next image""" self.index += 1 self.canvasScene.clear() if self.index > len(self.resultDict) - 1 or len(self.resultDict) <= 0: self.canvasScene.addText("No annotations to review") self.window.reviewFlag = False self.window.updateTable() else: #Draw Image and Polygon assignmentId = self.resultDict.keys()[self.index] result = self.resultDict[assignmentId] image = result[0][1] pointList = result[0][2] if self.window.segmentation_mode: pointList = [round(float(point), 3) for point in pointList] pointList = zip(*[iter(pointList)] * 2) self.drawImage(os.path.join(self.imagePath, image)) self.drawPolygon(pointList) else: pointList = [round(float(point), 3) for point in pointList] pointList = zip(*[iter(pointList)] * 4) self.drawFeatureImages(os.path.join(self.imagePath, image)) for point in pointList: self.drawFeaturePoint(point) #update scene self.window.setWindowTitle("MTurk Review Tool ({0}/{1}) Rejected: {2} Approved: {3}".format(self.index + 1, len(self.resultDict), len(self.window.rejected), len(self.window.approved))) self.canvasScene.setSceneRect(self.canvasScene.itemsBoundingRect()) self.fitInView(0, 0, self.canvasScene.width(), self.canvasScene.height(), 1) self.canvasScene.update(0, 0, self.canvasScene.width(), self.canvasScene.height())
def Paint(self): scene = QGraphicsScene() hor = self.ui.horizontal.currentIndex() vert = self.ui.vertical.currentIndex() v = {"a": [], "d": [], "c": [], "r": [], "f": []} h = {"a": [], "d": [], "c": [], "r": [], "f": []} for alg in self.algs: projects = [] if hor == 0: projects = sorted(self.stats[alg].values(),key=lambda x: 1.0 / 3.0 * (x["computersload"] + x["storesload"] + x["ramload"])) elif hor == 1: projects = sorted(self.stats[alg].values(),key=lambda x: 0.5*(x["computersload"]+x["ramload"])) elif hor == 2: projects = sorted(self.stats[alg].values(),key=lambda x: x["computersload"]) elif hor == 3: projects = sorted(self.stats[alg].values(),key=lambda x: x["ramload"]) elif hor == 4: projects = sorted(self.stats[alg].values(),key=lambda x: x["storesload"]) if projects == []: return for proj in projects: if vert == 0: v[alg].append(proj["assigned"]) elif vert == 1: v[alg].append(proj["ratio"]) elif vert == 2: v[alg].append(proj["replicas"]) if hor == 0: h[alg].append(1.0 / 3.0 * (proj["computersload"] + proj["storesload"] + proj["ramload"])) elif hor == 1: h[alg].append(0.5 * (proj["computersload"] + proj["ramload"])) elif hor == 2: h[alg].append(proj["computersload"]) elif hor == 3: h[alg].append(proj["ramload"]) elif hor == 4: h[alg].append(proj["storesload"]) maxnum = max([max(v[i]) for i in self.algs] + [1]) scene.addLine(5, 5, 5, 213, QPen(self.settings["axis"])) scene.addLine(2, 210, 210, 210, QPen(self.settings["axis"])) for i in range(10): scene.addLine(5 + (i + 1) * 20, 209, 5 + (i + 1) * 20, 211, QPen(self.settings["axis"])) scene.addLine(4, 210 - (i + 1) * 20, 6, 210 - (i + 1) * 20, QPen(self.settings["axis"])) font = QFont() font.setPointSize(6) t1 = scene.addText(str(0.1*(i + 1)), font) t1.setPos((i + 1) * 20, 212) if int(0.1*maxnum*(i + 1)) != 0: t2 = scene.addText(str(int(0.1*maxnum*(i + 1))), font) t2.setPos(-10, 200 - (i + 1) * 20) legendy = 10 for alg in self.algs: x0 = h[alg][0] y0 = v[alg][0] color = QColor(random.randint(1, 255), random.randint(1, 255), random.randint(1, 255)) brush = QBrush(color) color = QPen(color) for x,y in zip(h[alg],v[alg]): scene.addLine(5 + x0 * 200, 210 - float(y0)/maxnum * 200, 5 + x * 200, 210 - float(y)/maxnum * 200, color) scene.addLine(5 + x * 200 - 2, 210 - float(y)/maxnum * 200 - 2 , 5 + x * 200 + 2, 210 - float(y)/maxnum * 200 + 2, color) scene.addLine(5 + x * 200 + 2, 210 - float(y)/maxnum * 200 - 2, 5 + x * 200 - 2, 210 - float(y)/maxnum * 200 + 2, color) x0 = x y0 = y scene.addRect(220, legendy, 5, 5, color, brush) t = scene.addText(algnames[alg], font) t.setPos(230, legendy-5) legendy += 10 self.ui.graph.setScene(scene)
class PaintArea(QGraphicsView): def __init__(self, parent): QGraphicsView.__init__(self) self.init(parent) self.initShape() self.initCall = 2 self.minimumWidth = 550 self.setEnabled(False) def init(self, parent): self.timeline = parent self.pixmap = None self.image = None def initShape(self): self.scene = QGraphicsScene() self.setScene(self.scene) self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.setAlignment(Qt.AlignLeft) self.clicStart = None self.selectionRect = None def resizeEvent(self, sizEvent): if self.selectionRect and self.scene: self.scene.removeItem(self.selectionRect) self.selectionRect = None self.clicStart = None self.width = sizEvent.size().width() self.height = sizEvent.size().height() if self.initCall > 0: self.timeline.options.setMaximumWidth( self.timeline.options.minimumSizeHint().width()) self.initCall -= 1 if self.width < self.minimumWidth: self.timeline.setStateInfo('Unable to draw - Not enought width') else: self.timeline.workerThread.render() def mousePressEvent(self, mouseEvent): self.clicStart = mouseEvent.pos() if self.clicStart.x() < self.timeline.draw.yLeftMargin - 1: self.clicStart.setX(self.timeline.draw.yLeftMargin - 1) if self.clicStart.x( ) > self.timeline.ploter.width - self.timeline.m + 1: self.clicStart.setX(self.timeline.ploter.width - self.timeline.m + 1) if self.selectionRect: for item in self.scene.items(): if str(type( item)) == "<class 'PyQt4.QtGui.QGraphicsRectItem'>": self.scene.removeItem(item) self.selectionRect = None self.timeline.options.zoomButton.setEnabled(False) self.timeline.options.exportButton.setEnabled(False) self.timeline.options.selectedNodes.setText('Nothing selected') if self.timeline.selDateMin: self.timeline.options.selStartTime.setText('From ' + str( self.timeline.fromUSec(self.timeline.baseDateMin).strftime( '%d.%m.%Y %H:%M:%S'))) self.timeline.options.selEndTime.setText('To ' + str( self.timeline.fromUSec(self.timeline.baseDateMax).strftime( '%d.%m.%Y %H:%M:%S'))) else: self.timeline.options.selStartTime.setText( 'No selection start time') self.timeline.options.selEndTime.setText( 'No selection end time') self.timeline.options.selectedNodes.setText('Nothing selected') def mouseMoveEvent(self, dragMoveEvent): if self.clicStart: if self.clicStart.x() < dragMoveEvent.x(): x = self.clicStart.x() w = dragMoveEvent.x() - self.clicStart.x() else: x = dragMoveEvent.x() w = self.clicStart.x() - dragMoveEvent.x() # Limit rectangle to selectable area if x < self.timeline.draw.yLeftMargin - 1: x = self.timeline.draw.yLeftMargin - 1 w = self.clicStart.x() - x if x > self.timeline.ploter.width - self.timeline.m + 1: x = self.timeline.ploter.width - self.timeline.m + 1 w = 0 if x + w > self.timeline.ploter.width - self.timeline.m + 1: w = ((self.timeline.ploter.width - self.timeline.m + 1) - x) y = (self.timeline.m / 3) - 1 h = self.height - self.timeline.m - self.timeline.m / 3 + 2 if self.selectionRect and self.scene: self.scene.removeItem(self.selectionRect) self.selectionRect = self.scene.addRect(QRectF(x, y, w, h), QPen(Qt.DashDotDotLine)) def mouseReleaseEvent(self, mouseEvent): if self.selectionRect: if self.clicStart.x() > mouseEvent.x(): x1 = mouseEvent.x() x2 = self.clicStart.x() else: x1 = self.clicStart.x() x2 = mouseEvent.x() self.timeline.nodesInRange(x1, x2) start = self.timeline.draw.findXTime(x1) if start: self.timeline.options.selStartTime.setText( 'From ' + str(start.strftime('%d.%m.%Y %H:%M:%S'))) end = self.timeline.draw.findXTime(x2) if end: self.timeline.options.selEndTime.setText( 'To ' + str(end.strftime('%d.%m.%Y %H:%M:%S')))