def addArrow(self, startPoint=QPointF(), endPoint=QPointF(), pen=QPen()):
        alpha = 5 * pi / 6
        arrowLength = 10

        theta = atan2((endPoint.y() - startPoint.y()),
                      (endPoint.x() - startPoint.x()))

        gamma1 = theta + alpha
        gamma2 = theta - alpha
        arrowPoint_1 = QPointF(endPoint.x() + arrowLength * cos(gamma1),
                               endPoint.y() + arrowLength * sin(gamma1))
        arrowPoint_2 = QPointF(endPoint.x() + arrowLength * cos(gamma2),
                               endPoint.y() + arrowLength * sin(gamma2))
        line_0 = QLineF(startPoint, endPoint)
        line_1 = QLineF(endPoint, arrowPoint_1)
        line_2 = QLineF(endPoint, arrowPoint_2)

        line_item_0 = QGraphicsLineItem(line_0)
        line_item_0.setPen(pen)
        line_item_1 = QGraphicsLineItem(line_1)
        line_item_1.setPen(pen)
        line_item_2 = QGraphicsLineItem(line_2)
        line_item_2.setPen(pen)

        arrowItems = [line_item_0, line_item_1, line_item_2]

        self.addItem(line_item_0)
        self.addItem(line_item_1)
        self.addItem(line_item_2)

        return arrowItems
 def add_edge(self, roi_num_1, roi_num_2):
     pixel_coords_1 = self.items_dict[
         'r' + str(roi_num_1).zfill(2)]['pixel_coords']
     pixel_coords_2 = self.items_dict[
         'r' + str(roi_num_2).zfill(2)]['pixel_coords']
     self.line_dict[(str(roi_num_1) + '-' +
                     str(roi_num_2))] = QGraphicsLineItem(
                         QLineF(pixel_coords_2, pixel_coords_1))
     self.addItem(self.line_dict[(str(roi_num_1) + '-' + str(roi_num_2))])
Beispiel #3
0
    def _handle_add_clicked(self):
        name = self.name_edit.text()
        if len(name) != 0:
            # remove and re-draw it
            if name in self.places_dict.keys():
                self.places_dict.pop(name)
                for item in self._sceneItems[name].keys():
                    self._scene.removeItem(self._sceneItems[name][item])
            try:
                # storing the values in the dict
                x = self.x_spin.value()
                y = self.y_spin.value()
                theta = self.theta_spin.value()
                q = quaternion_from_euler(0.0, 0.0, theta)
                self.places_dict[str(name)] = [
                    x, y, 0.0,
                    float(q[0]),
                    float(q[1]),
                    float(q[2]),
                    float(q[3])
                ]

                # drawing the items
                self._sceneItems[name] = {"text": QGraphicsTextItem()}
                self._sceneItems[name]["text"].setDefaultTextColor(
                    QColor(0, 0, 255))
                self._sceneItems[name]["text"].setFont(QFont("Times", 10))
                self._sceneItems[name]["text"].setPlainText(name)
                scene_pos = self._gridToScene(x, y, q)
                x_c = scene_pos[0] - self._sceneItems[name][
                    "text"].boundingRect().width() / 2.0
                self._sceneItems[name]["text"].setPos(x_c, scene_pos[1])
                self._scene.addItem(self._sceneItems[name]["text"])
                self._sceneItems[name]["rec"] = self._scene.addRect(
                    QRectF((scene_pos[0] - 2), (scene_pos[1] - 2), 4, 4))
                line = QLineF(
                    scene_pos[0], scene_pos[1],
                    (scene_pos[0] - self._mag * cos(radians(scene_pos[3]))),
                    (scene_pos[1] + self._mag * sin(radians(scene_pos[3]))))
                self._sceneItems[name]["line"] = self._scene.addLine(
                    line, pen=QPen(Qt.red, 2))

            except ValueError:
                QMessageBox.critical(self, "Error!",
                                     "You must insert a valid value.")
        else:
            QMessageBox.critical(
                self, "Error!",
                "You have to insert a name and a valid position.")
Beispiel #4
0
 def _theta_changed(self, value):
     name = self.name_edit.text()
     if (len(name) != 0) and (name in self.places_dict.keys()):
         self._scene.removeItem(self._sceneItems[name]["line"])
         grid_pos = self.places_dict[name]
         scene_pos = self._gridToScene(grid_pos[0], grid_pos[1],
                                       numpy.asarray(grid_pos[3:7]))
         line = QLineF(scene_pos[0], scene_pos[1],
                       (scene_pos[0] - self._mag * cos(radians(value))),
                       (scene_pos[1] + self._mag * sin(radians(value))))
         self._sceneItems[name]["line"] = self._scene.addLine(line,
                                                              pen=QPen(
                                                                  Qt.red,
                                                                  2))
         q = quaternion_from_euler(0.0, 0.0, radians(value))
         self.places_dict[name][3] = float(q[0])
         self.places_dict[name][4] = float(q[1])
         self.places_dict[name][5] = float(q[2])
         self.places_dict[name][6] = float(q[3])
Beispiel #5
0
    def _handle_load_clicked(self):
        filename = QFileDialog.getOpenFileName(
            self, self.tr('Load from File'), '.',
            self.tr('YAML files {.yaml} (*.yaml)'))
        if filename[0] != '':
            with open(filename[0], 'r') as infile:
                try:
                    self.places_dict = yaml.load(infile)
                    for k in self._sceneItems.keys():
                        for item in self._sceneItems[k].keys():
                            self._scene.removeItem(self._sceneItems[k][item])
                    for name in self.places_dict.keys():
                        #
                        q = numpy.asarray(self.places_dict[name][3:7])
                        scene_pos = self._gridToScene(
                            self.places_dict[name][0],
                            self.places_dict[name][1], q)

                        # drawing the items
                        self._sceneItems[name] = {"text": QGraphicsTextItem()}
                        self._sceneItems[name]["text"].setDefaultTextColor(
                            QColor(0, 0, 255))
                        self._sceneItems[name]["text"].setFont(
                            QFont("Times", 10))
                        self._sceneItems[name]["text"].setPlainText(name)
                        x_c = scene_pos[0] - self._sceneItems[name][
                            "text"].boundingRect().width() / 2.0
                        self._sceneItems[name]["text"].setPos(
                            x_c, scene_pos[1])
                        self._scene.addItem(self._sceneItems[name]["text"])
                        self._sceneItems[name]["rec"] = self._scene.addRect(
                            QRectF((scene_pos[0] - 2), (scene_pos[1] - 2), 4,
                                   4))
                        line = QLineF(scene_pos[0], scene_pos[1],
                                      (scene_pos[0] -
                                       self._mag * cos(radians(scene_pos[3]))),
                                      (scene_pos[1] +
                                       self._mag * sin(radians(scene_pos[3]))))
                        self._sceneItems[name]["line"] = self._scene.addLine(
                            line, pen=QPen(Qt.red, 2))
                except yaml.scanner.ScannerError:
                    QMessageBox.critical(self, "Error!", "Invalid YAML file.")
Beispiel #6
0
 def _y_changed(self, value):
     name = self.name_edit.text()
     if (len(name) != 0) and (name in self.places_dict.keys()):
         self._scene.removeItem(self._sceneItems[name]["line"])
         self._scene.removeItem(self._sceneItems[name]["rec"])
         grid_pos = self.places_dict[name]
         scene_pos = self._gridToScene(grid_pos[0], value,
                                       numpy.asarray(grid_pos[3:7]))
         x_c = scene_pos[0] - self._sceneItems[name]["text"].boundingRect(
         ).width() / 2.0
         self._sceneItems[name]["text"].setPos(x_c, scene_pos[1])
         self._sceneItems[name]["rec"] = self._scene.addRect(
             QRectF((scene_pos[0] - 2), (scene_pos[1] - 2), 4, 4))
         line = QLineF(
             scene_pos[0], scene_pos[1],
             (scene_pos[0] - self._mag * cos(radians(scene_pos[3]))),
             (scene_pos[1] + self._mag * sin(radians(scene_pos[3]))))
         self._sceneItems[name]["line"] = self._scene.addLine(line,
                                                              pen=QPen(
                                                                  Qt.red,
                                                                  2))
         self.places_dict[name][1] = value
Beispiel #7
0
    def paintEvent(self, event):
        painter = QPainter(self)
        #painter.begin(self)

        # puts the arrow in the middle
        painter.translate(self.width() / 2, self.height() / 2)
        painter.rotate(self.angleRobo + self.angleVel)

        line = QLineF(0, 0, self.width() / 2 - 3, 0)

        headSize = min(self.width() / 20, 4)
        points = QPolygonF()
        points.append(QPointF(self.width() / 2 - headSize * 2, headSize))
        points.append(QPointF(self.width() / 2 - headSize * 2, -headSize))
        points.append(QPointF(self.width() / 2 - 3, 0))

        pen = QPen(self.color, 2)
        painter.setPen(pen)
        brush = QBrush(self.color)
        painter.setBrush(brush)

        painter.drawLine(line)
        painter.drawConvexPolygon(points)