def __init__(self, columns: int, rows: int, **kwargs: 'Any') -> None: super().__init__(device_type='console-text-display', **kwargs) self.__text_widget = text = QTextEdit() self.__col = 0 self.__row = 0 self.__total_cols = columns self.__total_rows = rows self.__text = ' ' * (self.__total_cols * self.__total_rows) self.__show_cursor = False text.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) text.setFocusPolicy(Qt.NoFocus) text.setReadOnly(True) text.setStyleSheet(''' color: white; background-color: black; border-color: black; border-width: 1px; border-style: solid; ''') font = QFont('Monospace') font.setStyleHint(QFont.TypeWriter) text.setFont(font) tdoc = text.document() fmetrics = QFontMetricsF(tdoc.defaultFont()) margins = text.contentsMargins() height = fmetrics.lineSpacing()*rows + \ 2*(tdoc.documentMargin() + text.frameWidth()) + \ margins.top() + margins.bottom() width = fmetrics.width('A')*columns + \ 2*(tdoc.documentMargin() + text.frameWidth()) + \ margins.left() + margins.right() text.setFixedHeight(height) text.setFixedWidth(width)
def onClickGenerate(self): doc = self.teText.document() font = QFont() font.setPointSizeF(self.sbFontSize.value()) doc.setDefaultFont(font) m = QFontMetricsF(font) nbLines = doc.lineCount() width = max([m.width(ll) for ll in doc.toPlainText().split("\n")]) pixmap = QPixmap(width*1.2, m.lineSpacing()*nbLines*1.2) pixmap.fill(Qt.white) painter = QPainter(pixmap) painter.setRenderHint(QPainter.Antialiasing, True) doc.drawContents(painter) painter.end() self.laImg.setPixmap(pixmap) self.imgToBurn = QImage(pixmap.toImage())