def paintEvent(self, ev): color = self.palette().color(QPalette.Highlight) painter = QPainter(self) # Filled rectangle. painter.setClipRect(self.rect()) color.setAlpha(50) painter.fillRect(self.rect().adjusted(2,2,-2,-2), color) # Thin rectangle outside. color.setAlpha(150) painter.setPen(color) painter.drawRect(self.rect().adjusted(0,0,-1,-1)) # Pseudo-handles at the corners and sides color.setAlpha(100) pen = QPen(color) pen.setWidth(8) painter.setPen(pen) painter.setBackgroundMode(Qt.OpaqueMode) # Clip at 4 corners region = QRegion(QRect(0,0,20,20)) region += QRect(self.rect().width()-20, 0, 20, 20) region += QRect(self.rect().width()-20, self.rect().height()-20, 20, 20) region += QRect(0, self.rect().height()-20, 20, 20) # Clip middles region += QRect(0, self.rect().height()/2-10, self.rect().width(), 20) region += QRect(self.rect().width()/2-10, 0, 20, self.rect().height()) # Draw thicker rectangles, clipped at corners and sides. painter.setClipRegion(region) painter.drawRect(self.rect())
def export_scene(canvas, width, height, dpi, exportFileName): """ This function exports the scene to a file. """ # need this to make sure we take away focus from # any currently selected legend items canvas.clearFocus() with HiddenStitchManager(canvas): # NOTE: We seem to need the 1px buffer region to avoid # the image being cut off margin = 10 theScene = visible_bounding_rect(canvas.items()) theScene.adjust(-margin, -margin, margin, margin) # check if user requested an svg file svg = True if QFileInfo(exportFileName).completeSuffix() == "svg" \ else False if svg: generator = QSvgGenerator() generator.setFileName(exportFileName) generator.setSize(QSize(width, height)) generator.setViewBox(QRect(0, 0, width, height)) generator.setTitle("sconcho generated SVG image") generator.setDescription("this svg image was exported from " "a sconcho project") generator.setResolution(dpi) else: generator = QImage(width+2*margin, height+2*margin, QImage.Format_ARGB32_Premultiplied) generator.fill(1) inchesToMeter = 39.3700787 generator.setDotsPerMeterX(dpi*inchesToMeter) generator.setDotsPerMeterY(dpi*inchesToMeter) painter = QPainter(generator) painter.setRenderHints(QPainter.SmoothPixmapTransform | QPainter.HighQualityAntialiasing | QPainter.TextAntialiasing ) painter.setBackgroundMode(Qt.TransparentMode ) canvas.render(painter, QRectF(), theScene ) painter.end() if not svg: generator.save(exportFileName)
def export_scene(canvas, width, height, dpi, exportFileName): """ This function exports the scene to a file. """ # need this to make sure we take away focus from # any currently selected legend items canvas.clearFocus() with HiddenStitchManager(canvas): # NOTE: We seem to need the 1px buffer region to avoid # the image being cut off margin = 10 theScene = visible_bounding_rect(canvas.items()) theScene.adjust(-margin, -margin, margin, margin) # check if user requested an svg file svg = True if QFileInfo(exportFileName).completeSuffix() == "svg" \ else False if svg: generator = QSvgGenerator() generator.setFileName(exportFileName) generator.setSize(QSize(width, height)) generator.setViewBox(QRect(0, 0, width, height)) generator.setTitle("sconcho generated SVG image") generator.setDescription("this svg image was exported from " "a sconcho project") generator.setResolution(dpi) else: generator = QImage(width + 2 * margin, height + 2 * margin, QImage.Format_ARGB32_Premultiplied) generator.fill(1) inchesToMeter = 39.3700787 generator.setDotsPerMeterX(dpi * inchesToMeter) generator.setDotsPerMeterY(dpi * inchesToMeter) painter = QPainter(generator) painter.setRenderHints(QPainter.SmoothPixmapTransform | QPainter.HighQualityAntialiasing | QPainter.TextAntialiasing) painter.setBackgroundMode(Qt.TransparentMode) canvas.render(painter, QRectF(), theScene) painter.end() if not svg: generator.save(exportFileName)
def render(self, url): """The real worker. Loads the page (_load_page) and awaits the end of the given 'delay'. While it is waiting outstanding QApplication events are processed. After the given delay, the Window or Widget (depends on the value of 'grabWholeWindow' is drawn into a QPixmap and postprocessed (_post_process_image). """ self._load_page(url, self.width, self.height, self.timeout) # Wait for end of timer. In this time, process # other outstanding Qt events. if self.wait > 0: if self.logger: self.logger.debug("Waiting %d seconds " % self.wait) waitToTime = time.time() + self.wait while time.time() < waitToTime and QApplication.hasPendingEvents(): QApplication.processEvents() if self.renderTransparentBackground: # Another possible drawing solution image = QImage(self._page.viewportSize(), QImage.Format_ARGB32) image.fill(QColor(255, 0, 0, 0).rgba()) # http://ariya.blogspot.com/2009/04/transparent-qwebview-and-qwebpage.html palette = self._view.palette() palette.setBrush(QPalette.Base, Qt.transparent) self._page.setPalette(palette) self._view.setAttribute(Qt.WA_OpaquePaintEvent, False) painter = QPainter(image) painter.setBackgroundMode(Qt.TransparentMode) self._page.mainFrame().render(painter) painter.end() else: if self.grabWholeWindow: # Note that this does not fully ensure that the # window still has the focus when the screen is # grabbed. This might result in a race condition. self._view.activateWindow() image = QPixmap.grabWindow(self._window.winId()) else: image = QPixmap.grabWidget(self._window) return self._post_process_image(image)
def paintEvent(self, ev): color = self.palette().color(QPalette.Highlight) painter = QPainter(self) # Filled rectangle. painter.setClipRect(self.rect()) color.setAlpha(50) painter.fillRect(self.rect().adjusted(2, 2, -2, -2), color) # Thin rectangle outside. color.setAlpha(150) painter.setPen(color) painter.drawRect(self.rect().adjusted(0, 0, -1, -1)) # Pseudo-handles at the corners and sides color.setAlpha(100) pen = QPen(color) pen.setWidth(8) painter.setPen(pen) painter.setBackgroundMode(Qt.OpaqueMode) # Clip at 4 corners region = QRegion(QRect(0, 0, 20, 20)) region += QRect(self.rect().width() - 20, 0, 20, 20) region += QRect(self.rect().width() - 20, self.rect().height() - 20, 20, 20) region += QRect(0, self.rect().height() - 20, 20, 20) # Clip middles region += QRect(0, self.rect().height() / 2 - 10, self.rect().width(), 20) region += QRect(self.rect().width() / 2 - 10, 0, 20, self.rect().height()) # Draw thicker rectangles, clipped at corners and sides. painter.setClipRegion(region) painter.drawRect(self.rect())
def paintEvent(self, pe): p = QPainter(self) p.setRenderHint(QPainter.Antialiasing) # p.setBrush(QColor(0xf0, 0xf0, 0xf0)) # color of the border # p.drawRect(-1, -1, 800, 800) pen = QPen() pen.setWidth(1) pen.setColor(QColor(0x58, 0x58, 0x58)) # color of the borders of the keys p.setPen(pen) p.setBrush(QColor(0x58, 0x58, 0x58)) # color of the keys p.setBackgroundMode(Qt.TransparentMode) rx = 3 space = self.space w = self.usable_width kw = self.key_w def drawRow(row, sx, sy, last_end=False): x = sx y = sy keys = row rw = w - sx i = 0 for k in keys: rect = QRectF(x, y, kw, kw) if i == len(keys) - 1 and last_end: rect.setWidth(rw) p.drawRoundedRect(rect, rx, rx) rect.adjust(5, 1, 0, 0) p.setPen(QColor(0xff, 0xff, 0xff)) p.setFont(self.lowerFont) p.drawText(rect, Qt.AlignLeft | Qt.AlignBottom, self.regular_text(k)) p.setPen(QColor(0x9e, 0xde, 0x00)) p.setFont(self.upperFont) p.drawText(rect, Qt.AlignLeft | Qt.AlignTop, self.shift_text(k)) rw = rw - space - kw x = x + space + kw i = i + 1 p.setPen(pen) return (x, rw) x = 6 y = 6 keys = self.kb["keys"] ext_return = self.kb["extended_return"] first_key_w = 0 rows = 4 remaining_x = [0, 0, 0, 0] remaining_widths = [0, 0, 0, 0] for i in range(0, rows): if first_key_w > 0: first_key_w = first_key_w * 1.375 if self.kb == self.kb_105 and i == 3: first_key_w = kw * 1.275 rect = QRectF(6, y, first_key_w, kw) p.drawRoundedRect(rect, rx, rx) x = 6 + first_key_w + space else: first_key_w = kw x, rw = drawRow(keys[i], x, y, i == 1 and not ext_return) remaining_x[i] = x remaining_widths[i] = rw if i != 1 and i != 2: rect = QRectF(x, y, rw, kw) p.drawRoundedRect(rect, rx, rx) x = .5 y = y + space + kw if ext_return: rx = rx * 2 x1 = remaining_x[1] y1 = 6 + kw * 1 + space * 1 w1 = remaining_widths[1] x2 = remaining_x[2] y2 = 6 + kw * 2 + space * 2 # this is some serious crap... but it has to be so # maybe one day keyboards won't look like this... # one can only hope pp = QPainterPath() pp.moveTo(x1, y1 + rx) pp.arcTo(x1, y1, rx, rx, 180, -90) pp.lineTo(x1 + w1 - rx, y1) pp.arcTo(x1 + w1 - rx, y1, rx, rx, 90, -90) pp.lineTo(x1 + w1, y2 + kw - rx) pp.arcTo(x1 + w1 - rx, y2 + kw - rx, rx, rx, 0, -90) pp.lineTo(x2 + rx, y2 + kw) pp.arcTo(x2, y2 + kw - rx, rx, rx, -90, -90) pp.lineTo(x2, y1 + kw) pp.lineTo(x1 + rx, y1 + kw) pp.arcTo(x1, y1 + kw - rx, rx, rx, -90, -90) pp.closeSubpath() p.drawPath(pp) else: x = remaining_x[2] y = .5 + kw * 2 + space * 2 rect = QRectF(x, y, remaining_widths[2], kw) p.drawRoundedRect(rect, rx, rx) QWidget.paintEvent(self, pe)
def paintEvent(self,e): painter = QPainter(self) painter.fillRect(0, 0, self.width * self.fixed_font_width + 2, self.height * self.fixed_font_height, Qt.black) painter.setPen(Qt.gray) painter.setRenderHint(QPainter.TextAntialiasing) painter.setFont(self.fixed_font) painter.setBackgroundMode(Qt.OpaqueMode) # Print main window l = self.height while (l > 0): c = 1 while (c <= self.width): y = self.fixed_font_metrics.ascent() + (l - 1) * self.fixed_font_height x = 1 + ((c - 1) * self.fixed_font_width) #print "**",l,"**",c if self.buf[(((self.height - l) * self.width) + c - 1) * 4] == 0: painter.setPen(self.ztoq_color(self.cur_fg)) else: painter.setPen(self.ztoq_color(self.buf[(((self.height - l) * self.width) + c - 1) * 4])) if self.buf[((((self.height - l) * self.width) + c - 1) * 4) + 1] == 0: painter.setBackground(QBrush(self.ztoq_color(self.cur_bg))) else: painter.setBackground(QBrush(self.ztoq_color(self.buf[((((self.height - l) * self.width) + c - 1) * 4) + 1]))) # Set appropriate font style if self.buf[((((self.height - l) * self.width) + c - 1) * 4) + 2] == 0: f = painter.font() f.setBold(False) f.setItalic(False) painter.setFont(f) if self.buf[((((self.height - l) * self.width) + c - 1) * 4) + 2] & 1: # Reverse video painter.setPen(self.ztoq_color(self.buf[((((self.height - l) * self.width) + c - 1) * 4) + 1])) painter.setBackground(QBrush(self.ztoq_color(self.buf[(((self.height - l) * self.width) + c - 1) * 4]))) if self.buf[((((self.height - l) * self.width) + c - 1) * 4) + 2] & 2: # Bold f = painter.font() f.setBold(True) painter.setFont(f) if self.buf[((((self.height - l) * self.width) + c - 1) * 4) + 2] & 4: # Italic f = painter.font() f.setItalic(True) painter.setFont(f) if self.buf[((((self.height - l) * self.width) + c - 1) * 4) + 3] <> 0: painter.drawText(x,y,self.buf[((((self.height - l) * self.width) + c - 1) * 4) + 3]) c += 1 l -= 1 c = 1 # Print upper window if self.upper_buf <> []: l = 1 while (l <= self.upper_buf_height): c = 1 while (c <= self.width): y = self.fixed_font_metrics.ascent() + (l - 1) * self.fixed_font_height x = 1 + ((c - 1) * self.fixed_font_width) #print "**",l,"**",c if self.upper_buf[((((l - 1) * self.width) + c - 1) * 4) + 3] <> 0: painter.setPen(self.ztoq_color(self.upper_buf[(((l - 1) * self.width) + c - 1) * 4])) painter.setBackground(QBrush(self.ztoq_color(self.upper_buf[((((l - 1) * self.width) + c - 1) * 4) + 1]))) # Set appropriate font style if self.upper_buf[((((l - 1) * self.width) + c - 1) * 4) + 2] == 0: f = painter.font() f.setBold(False) f.setItalic(False) painter.setFont(f) if self.upper_buf[((((l - 1) * self.width) + c - 1) * 4) + 2] & 1: # Reverse video painter.setPen(self.ztoq_color(self.upper_buf[((((l - 1) * self.width) + c - 1) * 4) + 1])) painter.setBackground(QBrush(self.ztoq_color(self.upper_buf[(((l - 1) * self.width) + c - 1) * 4]))) if self.upper_buf[((((l - 1) * self.width) + c - 1) * 4) + 2] & 2: # Bold f = painter.font() f.setBold(True) painter.setFont(f) if self.upper_buf[((((l - 1) * self.width) + c - 1) * 4) + 2] & 4: # Italic f = painter.font() f.setItalic(True) painter.setFont(f) painter.drawText(x,y,self.upper_buf[((((l - 1) * self.width) + c - 1) * 4) + 3]) c += 1 l += 1 # Print cursor if visible if self._cursor_visible: self.display_cursor()
def paintEvent(self, pe): p = QPainter(self) p.setRenderHint(QPainter.Antialiasing) p.setBrush(QColor(0xd6, 0xd6, 0xd6)) p.drawRect(-1, -1, 740, 740) pen = QPen() pen.setWidth(1) pen.setColor(QColor(0x58, 0x58, 0x58)) p.setPen(pen) p.setBrush(QColor(0x58, 0x58, 0x58)) p.setBackgroundMode(Qt.TransparentMode) rx = 3 space = self.space w = self.usable_width kw = self.key_w def drawRow(row, sx, sy, last_end=False): x = sx y = sy keys = row rw = w - sx i = 0 for k in keys: rect = QRectF(x, y, kw, kw) if i == len(keys) - 1 and last_end: rect.setWidth(rw) p.drawRoundedRect(rect, rx, rx) rect.adjust(5, 1, 0, 0) p.setPen(QColor(0xff, 0xff, 0xff)) p.setFont(self.lowerFont) p.drawText(rect, Qt.AlignLeft | Qt.AlignBottom, self.regular_text(k)) p.setPen(QColor(0x9e, 0xde, 0x00)) p.setFont(self.upperFont) p.drawText(rect, Qt.AlignLeft | Qt.AlignTop, self.shift_text(k)) rw = rw - space - kw x = x + space + kw i = i + 1 p.setPen(pen) return (x, rw) x = 6 y = 6 keys = self.kb["keys"] ext_return = self.kb["extended_return"] first_key_w = 0 rows = 4 remaining_x = [0, 0, 0, 0] remaining_y = [0, 0, 0, 0] remaining_widths = [0, 0, 0, 0] for i in range(0, rows): if first_key_w > 0: first_key_w = first_key_w * 1.375 if self.kb == self.kb_105 and i == 3: first_key_w = kw * 1.275 rect = QRectF(6, y, first_key_w, kw) p.drawRoundedRect(rect, rx, rx) x = 6 + first_key_w + space else: first_key_w = kw x, rw = drawRow(keys[i], x, y, i == 1 and not ext_return) remaining_x[i] = x # Had to save y as well to properly allign the right shift key remaining_y[i] = y remaining_widths[i] = rw if i != 1 and i != 2: rect = QRectF(x, y, rw, kw) p.drawRoundedRect(rect, rx, rx) x = .5 y = y + space + kw if ext_return: rx = rx * 2 x1 = remaining_x[1] y1 = 6 + kw * 1 + space * 1 w1 = remaining_widths[1] x2 = remaining_x[2] y2 = 6 + kw * 2 + space * 2 # this is some serious crap... but it has to be so # maybe one day keyboards won't look like this... # one can only hope pp = QPainterPath() pp.moveTo(x1, y1 + rx) pp.arcTo(x1, y1, rx, rx, 180, -90) pp.lineTo(x1 + w1 - rx, y1) pp.arcTo(x1 + w1 - rx, y1, rx, rx, 90, -90) pp.lineTo(x1 + w1, y2 + kw - rx) pp.arcTo(x1 + w1 - rx, y2 + kw - rx, rx, rx, 0, -90) pp.lineTo(x2 + rx, y2 + kw) pp.arcTo(x2, y2 + kw - rx, rx, rx, -90, -90) pp.lineTo(x2, y1 + kw) pp.lineTo(x1 + rx, y1 + kw) pp.arcTo(x1, y1 + kw - rx, rx, rx, -90, -90) pp.closeSubpath() p.drawPath(pp) else: x = remaining_x[2] y = remaining_y[2] rect = QRectF(x, y, remaining_widths[2], kw) p.drawRoundedRect(rect, rx, rx) QWidget.paintEvent(self, pe)
class Printer(BasePrinter): FONT_NAME = 'Courrier' def __init__(self, *args, **kwargs): BasePrinter.__init__(self, *args, **kwargs) self.page_number = 0 self.printer = QPrinter(QPrinter.HighResolution) ## todo: remove-me self.printer.setOutputFileName("ufwi_log-page.pdf") self.printer.setOutputFormat(QPrinter.NativeFormat) #self.printer.setOrientation(QPrinter.Landscape) self.printer.setPaperSize(QPrinter.A4) self.printer.setResolution(100) def _init_painter(self): self.painter = QPainter(self.printer) self.painter.setRenderHint(QPainter.Antialiasing) self.painter.setBackgroundMode(Qt.OpaqueMode) self.painter.setBrush(Qt.white) self.painter.setFont(QFont(self.FONT_NAME, 9)) fillrect = QRect(0, 0, self.printer.pageRect().width(), self.printer.pageRect().height()) self.painter.fillRect(fillrect, self.painter.brush()) self.page_number = 1 self._draw_header() self._draw_footer() self.drawCentralTitle(self.title) if self.logo: self.painter.drawImage(QPoint(self.width() / 2 - self.logo.width() / 2, self.height() / 2 + QFontMetrics(self.painter.font()).height() + self.height() / 4 - self.logo.height() / 2), self.logo) def _draw_header(self): if not self.header_enabled: return h = -self.headerHeight() self.drawText(QDateTime.currentDateTime().toString('yyyy-MM-dd hh:mm'), h, Qt.AlignLeft) h += self.drawText(self.interval_label, h, Qt.AlignRight) self.drawText(self.title, h, Qt.AlignLeft) self.drawText(self.enterprise, h, Qt.AlignRight) self.painter.drawLine(0, self.headerHeight() - 1, self.printer.pageRect().width(), self.headerHeight() - 1) def _draw_footer(self): if not self.footer_enabled: return self.painter.drawLine(10, self.printer.pageRect().height() - self.footerHeight(), self.printer.pageRect().width() - 10, self.printer.pageRect().height() - self.footerHeight()) h = self.height() + 1 #self.drawText('topleft', h, Qt.AlignLeft) h += self.drawText(u'© 2008-2010 EdenWall', h, Qt.AlignRight, size=9) self.drawText(unicode(self.page_number), h, Qt.AlignHCenter) #self.drawText('bottomright', h, Qt.AlignRight) def headerHeight(self): if self.header_enabled: return 40 else: return 0 def footerHeight(self): if self.footer_enabled: return 40 else: return 0 def height(self): return self.printer.pageRect().height() - self.headerHeight() - self.footerHeight() def width(self): return self.printer.pageRect().width() def showDialog(self, parent): dialog = QPrintDialog(self.printer, parent) if dialog.exec_() != QPrintDialog.Accepted: return False self._init_painter() return True def end(self): self.painter.end() def newPage(self): self.printer.newPage() self.page_number += 1 self._draw_header() self._draw_footer() def drawCentralTitle(self, text): self.painter.setBackgroundMode(Qt.TransparentMode) f = QFont(self.FONT_NAME, 22) f.setWeight(QFont.Bold) self.painter.setFont(f) height = QFontMetrics(self.painter.font()).height() self.painter.drawText(QRect(0, self.printer.pageRect().height() * 0.5, self.printer.pageRect().width(), height), Qt.AlignHCenter | Qt.AlignVCenter, text) self.painter.setFont(QFont(self.FONT_NAME, 10)) def drawHCenteredTitle(self, text, vpos): self.painter.setBackgroundMode(Qt.TransparentMode) self.painter.setFont(QFont(self.FONT_NAME, 18)) height = QFontMetrics(self.painter.font()).height() self.painter.drawText(QRect(0, self.headerHeight() + vpos, self.printer.pageRect().width(), height), Qt.AlignHCenter | Qt.AlignVCenter, text) self.painter.setFont(QFont(self.FONT_NAME, 10)) return height def drawText(self, text, vpos, halign=Qt.AlignLeft, size=10, width=None): self.painter.setBackgroundMode(Qt.TransparentMode) self.painter.setFont(QFont(self.FONT_NAME, size)) height = QFontMetrics(self.painter.font()).height() if width is None: width = self.printer.pageRect().width() self.painter.drawText(QRect(0, self.headerHeight() + vpos, width, height), halign | Qt.AlignVCenter, text) return height def drawFragment(self, frag_widget, vpos, len_frags): # Display it on half the page scale = 1 single_frag = False if frag_widget.pos == len_frags - 1 and frag_widget.pos % 2 == 0: scale = 2 single_frag = True y = self.headerHeight() + vpos width = self.printer.pageRect().width() * 0.8 * scale height = self.printer.pageRect().height() * 0.5 * 0.8 x = self.printer.pageRect().width() * 0.1 if single_frag: if frag_widget.view.__class__.__name__ != "TreeFragmentView": x = -(width - self.printer.pageRect().width()) / 2. else: width = width / 2. rect = QRect(x, y, width, height) # Temporary remove stylesheet to avoid to print background. stylesheet_save = frag_widget.styleSheet() frag_widget.setStyleSheet('') frag_widget.getView().printMe(self.painter, rect) frag_widget.setStyleSheet(stylesheet_save)