예제 #1
0
 def __init__(self, data, encoding, x0, y0, x1, y1, xsize, ysize):
     p = QPixmap()
     p.loadFromData(data, encoding, Qt.ImageConversionFlag.AutoColor)
     w, h = p.width(), p.height()
     p = p.copy(x0, y0, min(w, x1-x0), min(h, y1-y0))
     if p.width() != xsize or p.height() != ysize:
         p = p.scaled(xsize, ysize, Qt.AspectRatioMode.IgnoreAspectRatio, Qt.TransformationMode.SmoothTransformation)
     QGraphicsPixmapItem.__init__(self, p)
     self.height, self.width = ysize, xsize
     self.setTransformationMode(Qt.TransformationMode.SmoothTransformation)
     self.setShapeMode(QGraphicsPixmapItem.ShapeMode.BoundingRectShape)
예제 #2
0
파일: text.py 프로젝트: Farb/calibre
 def __init__(self, data, encoding, x0, y0, x1, y1, xsize, ysize):
     p = QPixmap()
     p.loadFromData(data, encoding, Qt.AutoColor)
     w, h = p.width(), p.height()
     p = p.copy(x0, y0, min(w, x1-x0), min(h, y1-y0))
     if p.width() != xsize or p.height() != ysize:
         p = p.scaled(xsize, ysize, Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
     QGraphicsPixmapItem.__init__(self, p)
     self.height, self.width = ysize, xsize
     self.setTransformationMode(Qt.SmoothTransformation)
     self.setShapeMode(QGraphicsPixmapItem.BoundingRectShape)
예제 #3
0
    def _setup_ui(self):
        self.setRenderHint(QPainter.Antialiasing)
        self.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)
        self.setResizeAnchor(QGraphicsView.AnchorUnderMouse)
        self.setFrameShape(QFrame.NoFrame)
        self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)

        self._loading_map = self._generate_loading_map()

        self._scene = QGraphicsScene(self)
        self._scene.setBackgroundBrush(QBrush(self.palette().dark().color()))

        self._image = QGraphicsPixmapItem()
        self._scene.addItem(self._image)

        # hud
        hud_color = self.palette().highlight().color().lighter(150)
        self._overlay = self._scene.addEllipse(QRectF(), QPen(hud_color, 4),
                                               QBrush(Qt.NoBrush))
        self._crosshair = self._scene.addEllipse(QRectF(), QPen(hud_color, 2),
                                                 QBrush(Qt.NoBrush))

        rect = QRectF(0, 0, self._w, self._h)
        cx = rect.center().x()
        cy = rect.center().y()
        r = min(rect.width(), rect.height()) * 0.7
        self._overlay.setRect(QRectF(cx - r / 2, cy - r / 2, r, r))
        rc = min(rect.width(), rect.height()) * 0.05
        self._crosshair.setRect(QRectF(cx - rc / 2, cy - rc / 2, rc, rc))

        self._overlay.setVisible(False)
        self._crosshair.setVisible(False)

        # scene
        self.setScene(self._scene)

        self._info = CameraInspectorInfo()
        self.setLayout(self._info)
예제 #4
0
 def drawHp(self):
     self.health = []
     hpcount = self.game.player.hp
     for i in range(0,hpcount):
         heart = QGraphicsPixmapItem(self.heart)
         heart.setY(660)
         heart.setX(20+i*50)
         self.scene.addItem(heart)
         self.health.append(heart)
예제 #5
0
 def drawProjectiles(self):
     for projectile in self.game.projectiles:
         if projectile.shape == None:
             if projectile.ptype == 1:
                 shape = QGraphicsPixmapItem(self.arrow)
             elif projectile.ptype == 2:
                 shape = QGraphicsEllipseItem(0,0,5,5)
                 shape.setBrush(QColor(0,0,0))
             shape.setX(projectile.x)
             shape.setY(projectile.y)
             self.scene.addItem(shape)
             projectile.shape = shape
         projectile.shape.setX(projectile.x)
         projectile.shape.setY(projectile.y)
         if projectile.ptype == 1:
             projectile.shape.setRotation(projectile.rotation)
         if projectile.shape.collidesWithItem(projectile.enemy.shape):
             projectile.enemy.getHit(projectile.damage)
             self.scene.removeItem(projectile.shape)
             self.game.projectiles.remove(projectile)
         self.update()
예제 #6
0
class CameraInspectorCore(QGraphicsView):
    _w = setting.camera_resolution[0]
    _h = setting.camera_resolution[1]

    def __init__(self):
        super().__init__()
        self._zoom = 0
        self._scene = None
        self._image = None
        self._last_rect = None
        self._info = None
        self._require_fit_map = False
        self._disconnect_map = None
        self._loading_map = None

        self._setup_ui()

    def _generate_loading_map(self):
        text = 'Loading...'
        pixmap = QPixmap(self._w, self._h)
        pixmap.fill(self.palette().dark().color())
        painter = QPainter(pixmap)
        painter.setRenderHint(QPainter.TextAntialiasing)
        font = QFont()
        font.setPixelSize(60)
        painter.setFont(font)
        painter.setPen(self.palette().text().color())
        text_size = painter.fontMetrics().size(0, text)
        painter.drawText((self._w - text_size.width()) / 2,
                         (self._h - text_size.height()) / 2, text_size.width(),
                         text_size.height(), Qt.AlignCenter, text)
        painter.end()
        return pixmap

    def _setup_ui(self):
        self.setRenderHint(QPainter.Antialiasing)
        self.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)
        self.setResizeAnchor(QGraphicsView.AnchorUnderMouse)
        self.setFrameShape(QFrame.NoFrame)
        self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)

        self._loading_map = self._generate_loading_map()

        self._scene = QGraphicsScene(self)
        self._scene.setBackgroundBrush(QBrush(self.palette().dark().color()))

        self._image = QGraphicsPixmapItem()
        self._scene.addItem(self._image)

        # hud
        hud_color = self.palette().highlight().color().lighter(150)
        self._overlay = self._scene.addEllipse(QRectF(), QPen(hud_color, 4),
                                               QBrush(Qt.NoBrush))
        self._crosshair = self._scene.addEllipse(QRectF(), QPen(hud_color, 2),
                                                 QBrush(Qt.NoBrush))

        rect = QRectF(0, 0, self._w, self._h)
        cx = rect.center().x()
        cy = rect.center().y()
        r = min(rect.width(), rect.height()) * 0.7
        self._overlay.setRect(QRectF(cx - r / 2, cy - r / 2, r, r))
        rc = min(rect.width(), rect.height()) * 0.05
        self._crosshair.setRect(QRectF(cx - rc / 2, cy - rc / 2, rc, rc))

        self._overlay.setVisible(False)
        self._crosshair.setVisible(False)

        # scene
        self.setScene(self._scene)

        self._info = CameraInspectorInfo()
        self.setLayout(self._info)

    def toggle_overlay(self, toggle):
        self._overlay.setVisible(toggle)
        self._crosshair.setVisible(toggle)

    def change_camera(self, serial):
        self._info.update_info(serial=serial)
        self.set_map(self._loading_map)
        self._require_fit_map = True

    def set_map(self, pixmap):
        if pixmap and pixmap.width() == self._w:
            self._image.setPixmap(pixmap)
            if self._require_fit_map:
                self._fit_map()
                self._require_fit_map = False
        else:
            self._image.setPixmap(self._loading_map)
            if self._zoom != 0:
                self._fit_map()

    def _fit_map(self):
        self.setDragMode(QGraphicsView.NoDrag)
        rect = QRectF(self._image.pixmap().rect())
        self.setSceneRect(rect)

        # 取得目前 scale,歸回 scale 1
        m = self.transform().mapRect(QRectF(0, 0, 1, 1))
        self.scale(1 / m.width(), 1 / m.height())

        # 縮放成適合圖像大小
        scenerect = self.transform().mapRect(rect)
        factor = min(self.width() / scenerect.width(),
                     self.height() / scenerect.height())
        self.scale(factor, factor)

        self._zoom = 0

        self._info.update_info(zoom=self.transform().m11())

    def wheelEvent(self, event):
        if event.angleDelta().y() > 0:
            factor = 1.25
            self._zoom += 1
        else:
            factor = 0.8
            self._zoom -= 1

        if self._zoom > 0:
            self.scale(factor, factor)
            self.setDragMode(QGraphicsView.ScrollHandDrag)
        elif self._zoom == 0:
            self._fit_map()
        else:
            self._zoom = 0

        self._info.update_info(zoom=self.transform().m11())

    def resizeEvent(self, event):
        if not self.isVisible():
            return

        if self._zoom == 0:
            if not self._image.pixmap().isNull():
                self._fit_map()
        elif self._last_rect is not None:
            la = self._last_rect.center()
            c = self.rect().center()
            self.translate(c.x() - la.x(), c.y() - la.y())

        self._last_rect = self.rect()