Пример #1
0
class ImageViewer(QWidget):
    def __init__(self, image_path):
        super().__init__()

        self.scene = QGraphicsScene()
        self.view = QGraphicsView(self.scene)
        layout = QVBoxLayout()
        layout.addWidget(self.view)
        self.setLayout(layout)

        self.load_image(image_path)

    def load_image(self, image_path):
        pixmap = QPixmap(image_path)
        self.scene.addPixmap(pixmap)
        self.view.fitInView(QRectF(0, 0, pixmap.width(), pixmap.height()), Qt.KeepAspectRatio)
        self.scene.update()
Пример #2
0
class ImageViewer(QWidget):
    def __init__(self, parent):
        super().__init__()
        self.parent = parent
        self.scene = QGraphicsScene()
        self.view = QGraphicsView(self.scene)
        layout = QVBoxLayout()
        layout.addWidget(self.view)

        self.img_width = None
        self.img_height = None

        self.setLayout(layout)

    def load_image(self, image_arr):
        self.clear()

        image_path = QImage(image_arr, image_arr.shape[1], image_arr.shape[0], QImage.Format_RGB888)

        pixmap = QPixmap(image_path)
        self.scene.addPixmap(pixmap)

        self.img_width = pixmap.width()
        self.img_height = pixmap.height()

        self.scale_self()

        self.scene.update()

    def scale_self(self):
        if self.img_width is not None:
            self.view.fitInView(QRectF(0, 0, self.img_width, self.img_height), Qt.KeepAspectRatio)
            self.view.scale(self.img_width / self.scene.width(), self.img_height / self.scene.height())

    def clear(self):
        self.scene.clear()
Пример #3
0
class SpriteView(QWidget):
    sprite_edited = Signal()

    def __init__(self, parent):
        super().__init__(parent)
        self._scene = QGraphicsScene(self)
        self._graphics_view = QGraphicsView(self._scene, self)

        layout = QVBoxLayout(self)
        layout.addWidget(self._graphics_view)

        self._pixmap_item = SpriteGraphicsItem(self)
        self._scene.addItem(self._pixmap_item)

        self._image = QImage(SPRITE_SIZE, SPRITE_SIZE, QImage.Format_ARGB32)
        self.sprite = [[0] * SPRITE_SIZE] * SPRITE_SIZE

        button_layout = QHBoxLayout(self)
        layout.addLayout(button_layout)
        self._color_button_group = QButtonGroup(self)
        for color in COLORS:
            button = QPushButton(self)
            button.setCheckable(True)
            button.setText(f"{color}")
            self._color_button_group.addButton(button, color)
            button_layout.addWidget(button)

        self._selected_color = 0
        self._color_button_group.button(self._selected_color).setChecked(True)
        QObject.connect(self._color_button_group, SIGNAL("buttonClicked(int)"),
                        self._color_selected)

        self._update_scaling()

    @property
    def sprite(self):
        return self._sprite

    @sprite.setter
    def sprite(self, sprite):
        if len(sprite) != SPRITE_SIZE:
            raise SpriteFormatException("Invalid Row Count")
        for row in sprite:
            if len(row) != SPRITE_SIZE:
                raise SpriteFormatException("Invalid Column Count")
            for value in row:
                if value not in COLORS:
                    raise SpriteFormatException(f"Invalid Color Value {value}")
        self._sprite = sprite
        self._update_image()

    def _update_image(self):
        for row in range(SPRITE_SIZE):
            for column in range(SPRITE_SIZE):
                self._image.setPixel(column, row,
                                     COLORS[self._sprite[row][column]])
        pixmap = QPixmap.fromImage(self._image)
        self._pixmap_item.setPixmap(pixmap)

    def resizeEvent(self, *args, **kwargs):
        super().resizeEvent(*args, **kwargs)
        self._update_scaling()

    def showEvent(self, *args, **kwargs):
        super().showEvent(*args, **kwargs)
        self._update_scaling()

    def _update_scaling(self):
        self._graphics_view.fitInView(self._pixmap_item, Qt.KeepAspectRatio)

    def _color_selected(self, id):
        self._selected_color = id

    def paint_pixel(self, x, y):
        if self._sprite[y][x] == self._selected_color:
            return
        self._sprite[y][x] = self._selected_color
        self._update_image()
        self.sprite_edited.emit()
Пример #4
0
class QGameOfLife(QWidget):

    Games = {
        "Game of Life": (GameOfLife, {
            'fill_rate': 0.50
        }),
        "Bacteria": (GrayScottDiffusion, {
            'coeffs': (0.16, 0.08, 0.035, 0.065)
        }),
        "Coral": (GrayScottDiffusion, {
            'coeffs': (0.16, 0.08, 0.062, 0.062)
        }),
        "Fingerprint": (GrayScottDiffusion, {
            'coeffs': (0.19, 0.05, 0.060, 0.062)
        }),
        "Spirals": (GrayScottDiffusion, {
            'coeffs': (0.10, 0.10, 0.018, 0.050)
        }),
        "Unstable": (GrayScottDiffusion, {
            'coeffs': (0.16, 0.08, 0.020, 0.055)
        }),
        "Worms": (GrayScottDiffusion, {
            'coeffs': (0.16, 0.08, 0.050, 0.065)
        }),
        "Zebrafish": (GrayScottDiffusion, {
            'coeffs': (0.16, 0.08, 0.035, 0.060)
        }),
    }

    def __init__(self, size=(400, 400)):
        super(QGameOfLife, self).__init__()
        self.size = size
        self.game = None
        self.initUI()
        self.show()

    def initUI(self):
        self.setWindowTitle(self.tr("Game of Life"))
        self.setLayout(QVBoxLayout())
        self.layout().setSpacing(0)
        self.layout().setContentsMargins(0, 0, 0, 0)

        self.comboBox = QComboBox()
        self.comboBox.addItems([*QGameOfLife.Games.keys()])
        self.comboBox.currentTextChanged.connect(self.select)
        self.layout().addWidget(self.comboBox)

        self.scene = QGraphicsScene()
        self.view = QGraphicsView(self.scene)
        self.view.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.view.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.view.setSizePolicy(
            QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred))
        self.view.setFrameShape(QFrame.NoFrame)
        self.layout().addWidget(self.view)

        self.item = None
        self.timer = QTimer()
        self.timer.setInterval(10)
        self.timer.timeout.connect(self.tick)
        initialGame = random.choice([*QGameOfLife.Games.keys()])
        self.select(initialGame)
        self.view.fitInView(self.item, Qt.KeepAspectRatioByExpanding)
        self.comboBox.setCurrentText(initialGame)

    def select(self, name: str):
        self.timer.stop()
        Game, args = QGameOfLife.Games[name]
        self.game = Game(self.size, **args)
        self.tick()
        self.timer.start()

    def tick(self):
        self.game.tick()
        bitmap = self.game.visualize()
        image = QImage(bitmap.data, bitmap.shape[1], bitmap.shape[0],
                       QImage.Format_Grayscale8)
        self.scene.removeItem(self.item)
        pixmap = QPixmap.fromImage(image)
        self.item = self.scene.addPixmap(pixmap)

    def resizeEvent(self, event: QResizeEvent):
        self.view.fitInView(self.item, Qt.KeepAspectRatioByExpanding)

    def sizeHint(self) -> QSize:
        return QSize(self.size[0], self.size[1])
Пример #5
0
class Escena(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        self.resize(QDesktopWidget().availableGeometry(self).size() * 0.6)
        self.scene = QGraphicsScene()
        self.scene.setSceneRect(-3000, -4000, 6000, 8000)
        self.view = QGraphicsView(self.scene)
        self.view.resize(self.size())
        self.view.fitInView(self.scene.sceneRect(), Qt.KeepAspectRatio)
        self.view.setParent(self)
        self.view.setTransformationAnchor(QGraphicsView.NoAnchor)
        self.view.setResizeAnchor(QGraphicsView.NoAnchor)
        self.view.scale(3, -3)
        self.lines = []
        self.show()

    def wheelEvent(self, event):
        zoomInFactor = 1.15
        zoomOutFactor = 1 / zoomInFactor
        # Zoom
        if event.delta() > 0:
            zoomFactor = zoomInFactor
        else:
            zoomFactor = zoomOutFactor
        self.view.scale(zoomFactor, zoomFactor)

    def resizeEvent(self, event):
        self.view.resize(self.size())

    def draw(self, comps, cluster):
        colors = QColor.colorNames()

        # for co in sample:
        #     print(co)
        #     x, _, z, _ = co['world']
        #     self.scene.addEllipse(x-50,z-50,100,100, pen=QPen(QColor('orange'), 5))

        # x,_,z,_ = sample[-1]['world']
        # self.scene.addRect(x,z,60,60, pen=QPen(QColor('red'), 100))
        # x,_,z,_ = sample[0]['world']
        # self.scene.addRect(x,z,60,60, pen=QPen(QColor('green'), 100))

        # for co in comps:
        #     if sample[co[-1]]['timestamp']-sample[co[0]]['timestamp'] > 200 and len(co)> 4:
        #         color = colors[random.randint(0, len(colors)-1)]
        #         for c in co:
        #             x, _, z, _ = sample[c]['world']
        #             self.scene.addEllipse(x-15,z-15,30,30, pen=QPen(QColor(color), 100), brush=QBrush(color=QColor(color)))

        if cluster[-1]['timestamp'] - cluster[0]['timestamp'] > 200 and len(
                cluster) > 4:
            color = colors[random.randint(0, len(colors) - 1)]
            for sample in cluster:
                x, _, z, _ = sample['world']
                self.scene.addEllipse(x - 15,
                                      z - 15,
                                      30,
                                      30,
                                      pen=QPen(QColor(color), 100),
                                      brush=QBrush(color=QColor(color)))

    def drawTrack(self, clusters):
        colors = QColor.colorNames()
        # for line in self.lines:
        #     self.scene.removeItem(line)
        self.scene.clear()
        for cluster in clusters:
            color = colors[random.randint(0, len(colors) - 1)]
            for t in cluster:
                self.scene.addLine(t[0][0],
                                   t[0][1],
                                   t[1][0],
                                   t[1][1],
                                   pen=QPen(QColor(color), 60))
Пример #6
0
class QGameOfLife(QWidget):

    Games = {
        "Game of Life": (GameOfLife, {'fill_rate': 0.50}),
        "Bacteria": (GrayScottDiffusion, {'coeffs': (0.16, 0.08, 0.035, 0.065)}),
        "Coral": (GrayScottDiffusion, {'coeffs': (0.16, 0.08, 0.062, 0.062)}),
        "Fingerprint": (GrayScottDiffusion, {'coeffs': (0.19, 0.05, 0.060, 0.062)}),
        "Spirals": (GrayScottDiffusion, {'coeffs': (0.10, 0.10, 0.018, 0.050)}),
        "Unstable": (GrayScottDiffusion, {'coeffs': (0.16, 0.08, 0.020, 0.055)}),
        "Worms": (GrayScottDiffusion, {'coeffs': (0.16, 0.08, 0.050, 0.065)}),
        "Zebrafish": (GrayScottDiffusion, {'coeffs': (0.16, 0.08, 0.035, 0.060)}),
    }

    def __init__(self, size=(400, 400)):
        super(QGameOfLife, self).__init__()
        self.size = size
        self.game = None
        self.initUI()
        self.show()

    def initUI(self):
        self.setWindowTitle(self.tr("Game of Life"))
        self.setLayout(QVBoxLayout())
        self.layout().setSpacing(0)
        self.layout().setContentsMargins(0, 0, 0, 0)

        self.comboBox = QComboBox()
        self.comboBox.addItems([*QGameOfLife.Games.keys()])
        self.comboBox.currentTextChanged.connect(self.select)
        self.layout().addWidget(self.comboBox)

        self.scene = QGraphicsScene()
        self.view = QGraphicsView(self.scene)
        self.view.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.view.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.view.setSizePolicy(QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred))
        self.view.setFrameShape(QFrame.NoFrame)
        self.layout().addWidget(self.view)

        self.item = None
        self.timer = QTimer()
        self.timer.setInterval(10)
        self.timer.timeout.connect(self.tick)
        initialGame = random.choice([*QGameOfLife.Games.keys()])
        self.select(initialGame)
        self.view.fitInView(self.item, Qt.KeepAspectRatioByExpanding)
        self.comboBox.setCurrentText(initialGame)

    def select(self, name: str):
        self.timer.stop()
        Game, args = QGameOfLife.Games[name]
        self.game = Game(self.size, **args)
        self.tick()
        self.timer.start()

    def tick(self):
        self.game.tick()
        bitmap = self.game.visualize()
        image = QImage(bitmap.data, bitmap.shape[1], bitmap.shape[0], QImage.Format_Grayscale8)
        self.scene.removeItem(self.item)
        pixmap = QPixmap.fromImage(image)
        self.item = self.scene.addPixmap(pixmap)

    def resizeEvent(self, event: QResizeEvent):
        self.view.fitInView(self.item, Qt.KeepAspectRatioByExpanding)

    def sizeHint(self) -> QSize:
        return QSize(self.size[0], self.size[1])