/usr/lib/python2.7/dist-packages/ginga/qtw/CanvasRenderQt.py is in python-ginga 2.6.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | #
# CanvasRenderQt.py -- for rendering into a ImageViewQt widget
#
# This is open-source software licensed under a BSD license.
# Please see the file LICENSE.txt for details.
#
from ginga.qtw.QtHelp import QtGui, QtCore, QFont, QPainter, QPen, \
QPolygonF, QPolygon, QColor, QPainterPath
from ginga import colors
from ginga.util.six.moves import map, zip
# force registration of all canvas types
import ginga.canvas.types.all
class RenderContext(object):
def __init__(self, viewer):
self.viewer = viewer
# TODO: encapsulate this drawable
self.cr = QPainter(self.viewer.pixmap)
def __get_color(self, color, alpha):
clr = QColor()
if isinstance(color, tuple):
clr.setRgbF(color[0], color[1], color[2], alpha)
else:
r, g, b = colors.lookup_color(color)
clr.setRgbF(r, g, b, alpha)
return clr
def set_line_from_shape(self, shape):
pen = QPen()
pen.setWidthF(getattr(shape, 'linewidth', 1.0))
if hasattr(shape, 'linestyle'):
if shape.linestyle == 'dash':
pen.setDashPattern([ 3.0, 4.0, 6.0, 4.0])
pen.setDashOffset(5.0)
alpha = getattr(shape, 'alpha', 1.0)
color = self.__get_color(shape.color, alpha)
pen.setColor(color)
self.cr.setPen(pen)
def set_fill_from_shape(self, shape):
fill = getattr(shape, 'fill', False)
if fill:
if hasattr(shape, 'fillcolor') and shape.fillcolor:
color = shape.fillcolor
else:
color = shape.color
if color is None:
self.cr.setBrush(QtCore.Qt.NoBrush)
else:
alpha = getattr(shape, 'alpha', None)
fillalpha = getattr(shape, 'fillalpha', alpha)
color = self.__get_color(color, fillalpha)
self.cr.setBrush(color)
else:
self.cr.setBrush(QtCore.Qt.NoBrush)
def set_font_from_shape(self, shape):
if hasattr(shape, 'font'):
if hasattr(shape, 'fontsize') and shape.fontsize is not None:
fontsize = shape.fontsize
else:
fontsize = shape.scale_font(self.viewer)
self.cr.setFont(QFont(shape.font, pointSize=fontsize))
def initialize_from_shape(self, shape, line=True, fill=True, font=True):
if line:
self.set_line_from_shape(shape)
if fill:
self.set_fill_from_shape(shape)
if font:
self.set_font_from_shape(shape)
def set_line(self, color, alpha=1.0, linewidth=1, style='solid'):
clr = self.__get_color(color, alpha)
pen = self.cr.pen()
pen.setColor(clr)
pen.setWidthF(float(linewidth))
if style == 'dash':
pen.setDashPattern([ 3.0, 4.0, 6.0, 4.0])
pen.setDashOffset(5.0)
self.cr.setPen(pen)
def set_fill(self, color, alpha=1.0):
if color is None:
self.cr.setBrush(QtCore.Qt.NoBrush)
else:
color = self.__get_color(color, alpha)
self.cr.setBrush(color)
def set_font(self, fontname, fontsize, color='black', alpha=1.0):
self.set_line(color, alpha=alpha)
self.cr.setFont(QFont(fontname, pointSize=fontsize))
def text_extents(self, text):
fm = self.cr.fontMetrics()
width = fm.width(text)
height = fm.height()
return width, height
##### DRAWING OPERATIONS #####
def draw_text(self, cx, cy, text, rot_deg=0.0):
self.cr.save()
self.cr.translate(cx, cy)
self.cr.rotate(-rot_deg)
self.cr.drawText(0, 0, text)
self.cr.restore()
def draw_polygon(self, cpoints):
qpoints = list(map(lambda p: QtCore.QPoint(p[0], p[1]),
(cpoints + (cpoints[0],))))
qpoly = QPolygon(qpoints)
self.cr.drawPolygon(qpoly)
def draw_circle(self, cx, cy, cradius):
# this is necessary to work around a bug in Qt--radius of 0
# causes a crash
cradius = max(cradius, 0.000001)
pt = QtCore.QPointF(cx, cy)
self.cr.drawEllipse(pt, float(cradius), float(cradius))
def draw_bezier_curve(self, cp):
path = QPainterPath()
path.moveTo(cp[0][0], cp[0][1])
path.cubicTo(cp[1][0], cp[1][1], cp[2][0], cp[2][1], cp[3][0], cp[3][1])
self.cr.drawPath(path)
def draw_ellipse_bezier(self, cp):
# draw 4 bezier curves to make the ellipse
path = QPainterPath()
path.moveTo(cp[0][0], cp[0][1])
path.cubicTo(cp[1][0], cp[1][1], cp[2][0], cp[2][1], cp[3][0], cp[3][1])
path.cubicTo(cp[4][0], cp[4][1], cp[5][0], cp[5][1], cp[6][0], cp[6][1])
path.cubicTo(cp[7][0], cp[7][1], cp[8][0], cp[8][1], cp[9][0], cp[9][1])
path.cubicTo(cp[10][0], cp[10][1], cp[11][0], cp[11][1], cp[12][0], cp[12][1])
self.cr.drawPath(path)
def draw_line(self, cx1, cy1, cx2, cy2):
self.cr.pen().setCapStyle(QtCore.Qt.RoundCap)
self.cr.drawLine(cx1, cy1, cx2, cy2)
def draw_path(self, cpoints):
self.cr.pen().setCapStyle(QtCore.Qt.RoundCap)
for i in range(len(cpoints) - 1):
cx1, cy1 = cpoints[i]
cx2, cy2 = cpoints[i+1]
self.cr.drawLine(cx1, cy1, cx2, cy2)
class CanvasRenderer(object):
def __init__(self, viewer):
self.viewer = viewer
def setup_cr(self, shape):
cr = RenderContext(self.viewer)
cr.initialize_from_shape(shape, font=False)
return cr
def get_dimensions(self, shape):
cr = self.setup_cr(shape)
cr.set_font_from_shape(shape)
return cr.text_extents(shape.text)
#END
|