Beispiel #1
0
 def pixmapFromSvg(self, pmapSize=None, withBorders=None):
     """returns a pixmap with default size as given in SVG and optional borders/shadows"""
     if withBorders is None:
         withBorders = Preferences.showShadows
     if withBorders:
         wantSize = self.tileset.tileSize.toSize()
     else:
         wantSize = self.tileset.faceSize.toSize()
     if not pmapSize:
         pmapSize = wantSize
     result = QPixmap(pmapSize)
     result.fill(Qt.transparent)
     painter = QPainter(result)
     if not painter.isActive():
         logException('painter is not active. Wanted size: %s' % str(pmapSize))
     try:
         xScale = float(pmapSize.width()) / wantSize.width()
         yScale = float(pmapSize.height()) / wantSize.height()
     except ZeroDivisionError:
         xScale = 1
         yScale = 1
     if not withBorders:
         painter.scale(*self.tileset.tileFaceRelation())
         painter.translate(-self.facePos())
     renderer = self.tileset.renderer()
     renderer.render(painter, self.elementId())
     painter.resetTransform()
     self._drawDarkness(painter)
     if self.showFace():
         faceSize = self.tileset.faceSize.toSize()
         faceSize = QSize(faceSize.width() * xScale, faceSize.height() * yScale)
         painter.translate(self.facePos())
         renderer.render(painter, self.tileset.svgName[self.tile.element.lower()],
                 QRectF(QPointF(), QSizeF(faceSize)))
     return result
Beispiel #2
0
 def paintEvent(self, e):
   w, h = self.width() / Player.N, self.height() / Player.N
   painter = QPainter(self)
   for j in range(0, Player.N):
     for i in range(0, Player.N):
       self.panel.render(painter, QRectF(i * w, j * h, w, h))
   if self.visible:
     for i in range(0, self.ship.__len__()):
       ship = self.ship[i]
       x, y, vertical = self.player.getShip(i)
       if not i == self.moving:
         dx, dy = 0, 0
       else:
         dx, dy = self.dx, self.dy
       if not vertical:
         ship.render(painter, QRectF(x * w + dx, y * h + dy, w * Player.LENGTH[i], h))
       else:
         painter.setTransform(QTransform(0, 1, 1, 0, 0, 0))
         ship.render(painter, QRectF(y * h + dy, x * w + dx, h * Player.LENGTH[i], w))
         painter.resetTransform()
Beispiel #3
0
 def pixmapFromSvg(self, pmapSize=None, withBorders=None):
     """returns a pixmap with default size as given in SVG and optional borders/shadows"""
     if withBorders is None:
         withBorders = Preferences.showShadows
     if withBorders:
         wantSize = self.tileset.tileSize.toSize()
     else:
         wantSize = self.tileset.faceSize.toSize()
     if not pmapSize:
         pmapSize = wantSize
     result = QPixmap(pmapSize)
     result.fill(Qt.transparent)
     painter = QPainter(result)
     if not painter.isActive():
         logException('painter is not active. Wanted size: %s' %
                      str(pmapSize))
     try:
         xScale = float(pmapSize.width()) / wantSize.width()
         yScale = float(pmapSize.height()) / wantSize.height()
     except ZeroDivisionError:
         xScale = 1
         yScale = 1
     if not withBorders:
         painter.scale(*self.tileset.tileFaceRelation())
         painter.translate(-self.facePos())
     renderer = self.tileset.renderer()
     renderer.render(painter, self.elementId())
     painter.resetTransform()
     self._drawDarkness(painter)
     if self.showFace():
         faceSize = self.tileset.faceSize.toSize()
         faceSize = QSize(faceSize.width() * xScale,
                          faceSize.height() * yScale)
         painter.translate(self.facePos())
         renderer.render(painter,
                         self.tileset.svgName[self.tile.element.lower()],
                         QRectF(QPointF(), QSizeF(faceSize)))
     return result
class QtRenderer(Renderer):
    """An implementation of :class:`~renderer.Renderer` for PyQt4.
       
       This renderer will draw on any `QPaintDevice`
    """
    def __init__(self, paint_device):
        """Creates a new renderer based on a QPaintDevice pd"""
        self._grid_pen = QPen(QColor(0x808080))
        self._grid_pen.setStyle(Qt.DashLine)
        self._painter = None
        Renderer.__init__(self, paint_device)

    def set_canvas(self, canvas):
        """Tell the renderer to draw on canvas
        The type of canvas is implementation-dependent"""
        if self._painter is not None:
            self._painter.restore()
            self._painter.restore()
            self._painter.end()
        
        self._paintdevice = canvas
        self._painter = QPainter(canvas)
        self._painter.setRenderHint(QPainter.Antialiasing)

        # invert the y axis
        self._painter.scale(1,-1)
        self._painter.translate(0,-canvas.height())

        Renderer.set_canvas(self,canvas)

    def _get_canvas_size(self,pd):
        """Get the canvas size tuple (width,height)"""
        return (pd.width(), pd.height())

    def push_state(self):
        """Store the current state on the stack.
        Current state includes default pose, pen and brush"""
        ### FIXME store things
        self._painter.save()

    def pop_state(self):
        """Restore the last saved state from the stack
        The state includes default pose, pen and brush"""
        ### FIXME store things
        self._painter.restore()

    def _calculate_bounds(self):
        transform = self._painter.worldTransform().inverted()[0]
        xs,ys = zip(
                    transform.map(0.0,0.0),
                    transform.map(0.0,float(self.size[1])),
                    transform.map(float(self.size[0]),float(self.size[1])),
                    transform.map(float(self.size[0]),0.0)
                    )

        self._bounds = (min(xs), min(ys), max(xs), max(ys))

    def _draw_grid(self):
        self.reset_pose()
        self._painter.setPen(self._grid_pen)
        xmin, ymin, xmax, ymax = self._bounds

        # Determine min/max x & y line indices:
        x_ticks = (int(xmin//self._grid_spacing), int(xmax//self._grid_spacing + 1))
        y_ticks = (int(ymin//self._grid_spacing), int(ymax//self._grid_spacing + 1))

        self._painter.drawLines(
            [QLineF(xmin, i * self._grid_spacing,
                    xmax, i * self._grid_spacing)
                for i in range(*y_ticks)])
        self._painter.drawLines(
            [QLineF(i * self._grid_spacing, ymin,
                    i * self._grid_spacing, ymax)
                for i in range(*x_ticks)])

    def scale(self, factor):
        """Scale drawing operations by factor
        To be implemented in subclasses."""
        self._painter.scale(factor,factor)

    def rotate(self, angle):
        """Rotate canvas by angle (in radians)
        To be implemented in subclasses."""
        self._painter.rotate(degrees(angle))

    def translate(self, dx, dy):
        """Translate canvas by dx, dy
        To be implemented in subclasses."""
        self._painter.translate(dx,dy)

    def clear_screen(self):
        """Erases the current screen with a white brush"""
        self._painter.save()
        self._painter.resetTransform()
        self.set_pen(0xFFFFFF)
        self.set_brush(0xFFFFFF)
        self.draw_rectangle(0,0,self.size[0],self.size[1])
        self._painter.restore()
        Renderer.clear_screen(self)

    @staticmethod
    def __qcolor(color):
        """Returns qcolor for a given ARGB color"""
        c = QColor(color)
        if color > 0xFFFFFF:
            c.setAlpha((color >> 24) & 0xFF)
        return c

    def set_pen(self,color=0, thickness=0):
        """Sets the line color and thickness.
        Color is interpreted as 0xAARRGGBB."""
        if color is None:
            self._painter.setPen(Qt.NoPen)
        else:
            self._painter.setPen(QPen(self.__qcolor(color),thickness))

    def set_brush(self,color):
        """Sets the fill color.
        Color is interpreted as 0xAARRGGBB."""
        if color is None:
            self._painter.setBrush(Qt.NoBrush)
        else:
            self._painter.setBrush(self.__qcolor(color))

    def draw_polygon(self,points):
        """Draws a polygon.
        Expects a list of points as a list of tuples or as a numpy array."""
        self._painter.drawPolygon(QPolygonF([QPointF(*point[:2]) for point in points]))

    def draw_ellipse(self, cx, cy, ra, rb = None):
        """Draws an ellipse."""
        if rb is None:
            rb = ra
        self._painter.drawEllipse(QRectF(cx-ra,cy-ra,2*ra,2*rb))

    def draw_rectangle(self, x, y, w, h):
        """Draws a rectangle."""
        self._painter.drawRect(QRectF(x,y,w,h))

    def draw_text(self, text, x, y, bgcolor = 0):
        """Draws a text string at the defined position."""
        pass

    def draw_line(self, x1, y1, x2, y2):
        """Draws a line using the current pen from (x1,y1) to (x2,y2)"""
        self._painter.drawLine(QLineF(x1,y1,x2,y2))
Beispiel #5
0
class QtRenderer(Renderer):
    """An implementation of :class:`~renderer.Renderer` for PyQt4.
       
       This renderer will draw on any `QPaintDevice`
    """
    def __init__(self, paint_device):
        """Creates a new renderer based on a QPaintDevice pd"""
        self._grid_pen = QPen(QColor(0x808080))
        self._grid_pen.setStyle(Qt.DashLine)
        self._painter = None
        Renderer.__init__(self, paint_device)

    def set_canvas(self, canvas):
        """Tell the renderer to draw on canvas
        The type of canvas is implementation-dependent"""
        if self._painter is not None:
            self._painter.restore()
            self._painter.restore()
            self._painter.end()

        self._paintdevice = canvas
        self._painter = QPainter(canvas)
        self._painter.setRenderHint(QPainter.Antialiasing)

        # invert the y axis
        self._painter.scale(1, -1)
        self._painter.translate(0, -canvas.height())

        Renderer.set_canvas(self, canvas)

    def _get_canvas_size(self, pd):
        """Get the canvas size tuple (width,height)"""
        return (pd.width(), pd.height())

    def push_state(self):
        """Store the current state on the stack.
        Current state includes default pose, pen and brush"""
        ### FIXME store things
        self._painter.save()

    def pop_state(self):
        """Restore the last saved state from the stack
        The state includes default pose, pen and brush"""
        ### FIXME store things
        self._painter.restore()

    def _calculate_bounds(self):
        transform = self._painter.worldTransform().inverted()[0]
        xs, ys = zip(transform.map(0.0, 0.0),
                     transform.map(0.0, float(self.size[1])),
                     transform.map(float(self.size[0]), float(self.size[1])),
                     transform.map(float(self.size[0]), 0.0))

        self._bounds = (min(xs), min(ys), max(xs), max(ys))

    def _draw_grid(self):
        self.reset_pose()
        self._painter.setPen(self._grid_pen)
        xmin, ymin, xmax, ymax = self._bounds

        # Determine min/max x & y line indices:
        x_ticks = (int(xmin // self._grid_spacing),
                   int(xmax // self._grid_spacing + 1))
        y_ticks = (int(ymin // self._grid_spacing),
                   int(ymax // self._grid_spacing + 1))

        self._painter.drawLines([
            QLineF(xmin, i * self._grid_spacing, xmax, i * self._grid_spacing)
            for i in range(*y_ticks)
        ])
        self._painter.drawLines([
            QLineF(i * self._grid_spacing, ymin, i * self._grid_spacing, ymax)
            for i in range(*x_ticks)
        ])

    def scale(self, factor):
        """Scale drawing operations by factor
        To be implemented in subclasses."""
        self._painter.scale(factor, factor)

    def rotate(self, angle):
        """Rotate canvas by angle (in radians)
        To be implemented in subclasses."""
        self._painter.rotate(degrees(angle))

    def translate(self, dx, dy):
        """Translate canvas by dx, dy
        To be implemented in subclasses."""
        self._painter.translate(dx, dy)

    def clear_screen(self):
        """Erases the current screen with a white brush"""
        self._painter.save()
        self._painter.resetTransform()
        self.set_pen(0xFFFFFF)
        self.set_brush(0xFFFFFF)
        self.draw_rectangle(0, 0, self.size[0], self.size[1])
        self._painter.restore()
        Renderer.clear_screen(self)

    @staticmethod
    def __qcolor(color):
        """Returns qcolor for a given ARGB color"""
        c = QColor(color)
        if color > 0xFFFFFF:
            c.setAlpha((color >> 24) & 0xFF)
        return c

    def set_pen(self, color):
        """Sets the line color.
        Color is interpreted as 0xAARRGGBB."""
        if color is None:
            self._painter.setPen(Qt.NoPen)
        else:
            self._painter.setPen(self.__qcolor(color))

    def set_brush(self, color):
        """Sets the fill color.
        Color is interpreted as 0xAARRGGBB."""
        if color is None:
            self._painter.setBrush(Qt.NoBrush)
        else:
            self._painter.setBrush(self.__qcolor(color))

    def draw_polygon(self, points):
        """Draws a polygon.
        Expects a list of points as a list of tuples or as a numpy array."""
        self._painter.drawPolygon(
            QPolygonF([QPointF(*point[:2]) for point in points]))

    def draw_ellipse(self, cx, cy, ra, rb=None):
        """Draws an ellipse."""
        if rb is None:
            rb = ra
        self._painter.drawEllipse(QRectF(cx - ra, cy - ra, 2 * ra, 2 * rb))

    def draw_rectangle(self, x, y, w, h):
        """Draws a rectangle."""
        self._painter.drawRect(QRectF(x, y, w, h))

    def draw_text(self, text, x, y, bgcolor=0):
        """Draws a text string at the defined position."""
        pass

    def draw_line(self, x1, y1, x2, y2):
        """Draws a line using the current pen from (x1,y1) to (x2,y2)"""
        self._painter.drawLine(QLineF(x1, y1, x2, y2))
Beispiel #6
0
class QtRenderer(Renderer):
    def __init__(self, pd):
        """Creates a new renderer based on a QPaintDevice pd
        """
        self._defpose = Pose() # The pose in the bottom-left corner
        self._zoom = 1 # The zooming factor
        self._zoom_c = False # Whether the scaling is done from center
        Renderer.__init__(self, (pd.width(), pd.height()), pd)

    def set_canvas(self, canvas):
        """Tell the renderer to draw on canvas
        
        The type of canvas is implementation-dependent
        """
        self._paintdevice = canvas
        self._painter = QPainter(canvas)
        self._painter.setRenderHint(QPainter.Antialiasing)
        # invert the y axis
        self._painter.scale(1,-1)
        self._painter.translate(0,-canvas.height())
        
        self.set_pen(None)
        # push the default state
        self._painter.save()
        self._painter.save()
        
    def set_zoom(self, zoom_level):
        self._zoom = float(zoom_level)
        self.__update_default_state()
        
    def __update_default_state(self):
        self._painter.restore() # Reset state
        self._painter.restore() # Set zoom to 1     
        self._painter.save() # Re-save the zoom-1
        if self._zoom_c:
            self._painter.translate(self.size[0]/2,self.size[1]/2)
        self._painter.scale(self._zoom,self._zoom)
        self._painter.rotate(degrees(-self._defpose.theta))
        self._painter.translate(-self._defpose.x, -self._defpose.y)
        self._painter.save() # Save the zoomed state
        self.clear_screen()

    def __set_scr_pose(self,pose):
        self._defpose = pose
        self.__update_default_state()
        self.clear_screen()

    def set_screen_pose(self, pose):
        self._zoom_c = False
        self.__set_scr_pose(pose)

    def set_screen_center_pose(self, pose):
        self._zoom_c = True
        self.__set_scr_pose(pose)
   
    def clear_screen(self):
        self._painter.save()
        self._painter.resetTransform()
        self.set_pen(0xFFFFFF)
        self.set_brush(0xFFFFFF)
        self.draw_rectangle(0,0,self.size[0],self.size[1])        
        self._painter.restore()
   
    def __delete__(self):
        self._painter.restore()
        self._painter.restore()
   
    def reset_pose(self):
        """Resets the renderer to world coordinates
        """
        self._painter.restore()
        self._painter.save()
        
    def add_pose(self,pose):
        """Add a pose transformation to the current transformation
        """
        self._painter.translate(pose.x,pose.y)
        self._painter.rotate(degrees(pose.theta))

    @staticmethod
    def __qcolor(color):
        """Returns qcolor for a given ARGB color
        """
        c = QColor(color)
        if color > 0xFFFFFF:
            c.setAlpha((color >> 24) & 0xFF)
        return c

    def set_pen(self,color):
        """Sets the line color.
        
        Color is interpreted as 0xAARRGGBB.
        """
        if color is None:
            self._painter.setPen(Qt.NoPen)
        else:
            self._painter.setPen(self.__qcolor(color))

    def set_brush(self,color):
        """Sets the fill color.
        
        Color is interpreted as 0xAARRGGBB.
        """
        if color is None:
            self._painter.setBrush(Qt.NoBrush)
        else:
            self._painter.setBrush(self.__qcolor(color))
        
    def draw_polygon(self,points):
        """Draws a polygon.
        
        Expects a list of points as a list of tuples or as a numpy array.
        """
        self._painter.drawPolygon(QPolygonF([QPointF(*point[:2]) for point in points]))
        
    def draw_ellipse(self, x, y, w, h):
        """Draws an ellipse.
        """
        self._painter.drawEllipse(x,y,w,h)

    def draw_rectangle(self, x, y, w, h):
        """Draws a rectangle.
        """
        self._painter.drawRect(x,y,w,h)
        
    def draw_text(self, text, x, y, bgcolor = 0):
        """Draws a text string at the defined position.
        """
        pass
    
    def draw_line(self, x1, y1, x2, y2):
        """Draws a line using the current pen from (x1,y1) to (x2,y2)
        """
        pass