def paintEvent(self, evt): painter = QPainter(self) if self.color is not None: painter.setPen(QPen(self.color)) painter.rotate(self.angle) transform = painter.transform().inverted()[0] rct = transform.mapRect(self.rect()) painter.drawText(rct, self.alignment(), self.text())
def get(self): """Override AbstractDataSetWidget method""" value = self.item.get() if value is not None: color = QColor(value) self.picture = QPicture() painter = QPainter() painter.begin(self.picture) painter.fillRect(QRect(0, 0, 60, 20), QBrush(color)) painter.end() self.group.setPicture(self.picture)
def print_(self, device): if not self.win: self.build_window() W = device.width() H = device.height() from numpy import array coords = array(list(self.axes.keys())) imin = coords[:, 0].min() imax = coords[:, 0].max() jmin = coords[:, 1].min() jmax = coords[:, 1].max() w = W / (jmax - jmin + 1) h = H / (imax - imin + 1) paint = QPainter(device) for (i, j), ax in list(self.axes.items()): oy = (i - imin) * h ox = (j - jmin) * w ax.widget.print_(paint, QRect(ox, oy, w, h))
def get_font_array(sz, chars=DEFAULT_CHARS): from guidata.qt.QtGui import QFont, QPainter, QColor font = QFont() font.setFixedPitch(True) font.setPixelSize(sz) font.setStyleStrategy(QFont.NoAntialias) dummy = QImage(10, 10, QImage.Format_ARGB32) pnt = QPainter(dummy) pnt.setFont(font) metric = pnt.fontMetrics() rct = metric.boundingRect(chars) pnt.end() h = rct.height() w = rct.width() img = QImage(w, h, QImage.Format_ARGB32) paint = QPainter() paint.begin(img) paint.setFont(font) paint.setBrush(QColor(255, 255, 255)) paint.setPen(QColor(255, 255, 255)) paint.drawRect(0, 0, w + 1, h + 1) paint.setPen(QColor(0, 0, 0)) paint.setBrush(QColor(0, 0, 0)) paint.drawText(0, paint.fontMetrics().ascent(), chars) paint.end() try: try: data = img.bits().asstring(img.numBytes()) except AttributeError: # PyQt5 data = img.bits().asstring(img.byteCount()) except SystemError: # Python 3 return npy = np.frombuffer(data, np.uint8) npy.shape = img.height(), img.bytesPerLine() / 4, 4 return npy[:, :, 0]
def get_font_array(sz, chars=DEFAULT_CHARS): from guidata.qt.QtGui import QFont, QPainter, QColor font = QFont() font.setFixedPitch(True) font.setPixelSize(sz) font.setStyleStrategy(QFont.NoAntialias) dummy = QImage(10, 10, QImage.Format_ARGB32) pnt = QPainter(dummy) pnt.setFont(font) metric = pnt.fontMetrics() rct = metric.boundingRect(chars) pnt.end() h = rct.height() w = rct.width() img = QImage(w, h, QImage.Format_ARGB32) paint = QPainter() paint.begin(img) paint.setFont(font) paint.setBrush( QColor(255, 255, 255) ) paint.setPen( QColor(255, 255, 255) ) paint.drawRect(0, 0, w+1, h+1) paint.setPen( QColor(0, 0, 0)) paint.setBrush( QColor(0, 0, 0) ) paint.drawText(0, paint.fontMetrics().ascent(), chars) paint.end() try: try: data = img.bits().asstring(img.numBytes()) except AttributeError: # PyQt5 data = img.bits().asstring(img.byteCount()) except SystemError: # Python 3 return npy = np.frombuffer(data, np.uint8) npy.shape = img.height(), img.bytesPerLine()/4, 4 return npy[:,:, 0]