High quality png export of bar figures. Save file dialog added for bar figures.

This commit is contained in:
Anne de Jong 2018-10-03 20:14:33 +02:00 committed by J.A. de Jong - ASCEE
parent 6b4e5fba9b
commit 38d1cfe435

View File

@ -36,11 +36,13 @@ DEFAULT_COLORS = [ASCEEColors.blue, ASCEEColors.green, Qt.red, Qt.cyan,
Qt.darkYellow, Qt.darkYellow,
Qt.darkMagenta] Qt.darkMagenta]
def graphicsTextItem(label): def graphicsTextItem(label):
item = QGraphicsTextItem(label) item = QGraphicsTextItem(label)
item.setFont(Branding.figureFont()) item.setFont(Branding.figureFont())
return item return item
class BarScene(QGraphicsScene): class BarScene(QGraphicsScene):
""" """
Graphhics Scene for plotting bars Graphhics Scene for plotting bars
@ -70,7 +72,7 @@ class BarScene(QGraphicsScene):
legendpos: position of legend w.r.t. default position, in pixels legendpos: position of legend w.r.t. default position, in pixels
""" """
super().__init__(parent=parent) super().__init__(parent=parent)
self.setSceneRect(QRect(0,0,*size)) self.setSceneRect(QRect(0, 0, *size))
# self.setBackgroundBrush(ASCEEColors.bgBrush(0, size[0])) # self.setBackgroundBrush(ASCEEColors.bgBrush(0, size[0]))
self.ylim = ylim self.ylim = ylim
@ -115,7 +117,7 @@ class BarScene(QGraphicsScene):
range_ = ylim[1]-ylim[0] range_ = ylim[1]-ylim[0]
ytickval = i/(nyticks-1)*range_ + ylim[0] ytickval = i/(nyticks-1)*range_ + ylim[0]
yticklabel = f'{ytickval:.0f}' yticklabel = f'{ytickval:.0f}'
txt =graphicsTextItem(yticklabel) txt = graphicsTextItem(yticklabel)
txtwidth = txt.boundingRect().width() txtwidth = txt.boundingRect().width()
txtmaxwidth = max(txtmaxwidth, txtwidth) txtmaxwidth = max(txtmaxwidth, txtwidth)
txt.setPos(leftoffset-10-txtwidth, txt.setPos(leftoffset-10-txtwidth,
@ -177,7 +179,7 @@ class BarScene(QGraphicsScene):
if legend is not None: if legend is not None:
maxlegtxtwidth = 0 maxlegtxtwidth = 0
legposx, legposy = (0,0) if legendpos is None else legendpos legposx, legposy = (0, 0) if legendpos is None else legendpos
legpos = (xsize-rightoffset-300+legposx, legpos = (xsize-rightoffset-300+legposx,
ysize-topoffset-30+legposy) ysize-topoffset-30+legposy)
@ -198,7 +200,7 @@ class BarScene(QGraphicsScene):
# The position of the legend, in screen coordinates # The position of the legend, in screen coordinates
pos = (legpos[0], legpos[1] - i*dyleg) pos = (legpos[0], legpos[1] - i*dyleg)
color = self.colors[i%len(self.colors)] color = self.colors[i % len(self.colors)]
legrect = self.createRect(*pos, Lxlegrect, Lylegrect) legrect = self.createRect(*pos, Lxlegrect, Lylegrect)
@ -231,18 +233,22 @@ class BarScene(QGraphicsScene):
Returns: Returns:
True on success True on success
""" """
image = QImage(*self.size, size = self.size
pixelsx = max(1200, size[0])
pixelsy = int(pixelsx*size[1]/size[0])
imagesize = (pixelsx, pixelsy)
image = QImage(pixelsx,
pixelsy,
QImage.Format_ARGB32_Premultiplied) QImage.Format_ARGB32_Premultiplied)
painter = QPainter(image) painter = QPainter(image)
# painter.begin() painter.setRenderHint(QPainter.Antialiasing)
# painter.setRenderHint(QPainter.Antialiasing)
painter.setBrush(Qt.white) painter.setBrush(Qt.white)
painter.setPen(Qt.white) painter.setPen(Qt.white)
painter.drawRect(QRect(0, 0, *self.size)) painter.drawRect(QRect(0, 0, *imagesize))
targetrect = QRectF(0, 0, *self.size) targetrect = QRectF(0, 0, *imagesize)
sourcerect = QRectF(0, 0, *self.size) sourcerect = QRectF(0, 0, *size)
self.render(painter, targetrect, sourcerect) self.render(painter, targetrect, sourcerect)
painter.end() painter.end()
@ -358,7 +364,7 @@ class BarScene(QGraphicsScene):
yvals = np.clip(yvals, 0, 1) yvals = np.clip(yvals, 0, 1)
for g in range(G): for g in range(G):
color = self.colors[g%len(self.colors)] color = self.colors[g % len(self.colors)]
for n in range(N): for n in range(N):
bar = self.bgs[g][n] bar = self.bgs[g][n]
bar.setRect(self.getBarRect(n, g, yvals[n, g])) bar.setRect(self.getBarRect(n, g, yvals[n, g]))