def paintEvent(self, event): painter = QPainter(self) painter.fillRect(event.rect(), Qt.white) painter.setFont(self.displayFont) redrawRect = event.rect() beginRow = redrawRect.top() // self.squareSize endRow = redrawRect.bottom() // self.squareSize beginColumn = redrawRect.left() // self.squareSize endColumn = redrawRect.right() // self.squareSize painter.setPen(Qt.gray) for row in range(beginRow, endRow + 1): for column in range(beginColumn, endColumn + 1): painter.drawRect(column * self.squareSize, row * self.squareSize, self.squareSize, self.squareSize) fontMetrics = QFontMetrics(self.displayFont) painter.setPen(Qt.black) for row in range(beginRow, endRow + 1): for column in range(beginColumn, endColumn + 1): char = chr(row * COLUMNS + column) painter.setClipRect(column * self.squareSize, row * self.squareSize, self.squareSize, self.squareSize) if char == self.currentChar: painter.fillRect(column * self.squareSize + 1, row * self.squareSize + 1, self.squareSize, self.squareSize, Qt.green) painter.drawText( column * self.squareSize + (self.squareSize / 2) - fontMetrics.width(char) / 2, row * self.squareSize + 4 + fontMetrics.ascent(), char)
def set(self, U=None, vmin=None, vmax=None): # normalize U fm = QFontMetrics(self.font()) self.vmin = vmin if vmin is not None else ( np.min(U) if U is not None else 0.) self.vmax = vmax if vmax is not None else ( np.max(U) if U is not None else 1.) difference = abs(self.vmin - self.vmax) if difference == 0: precision = 3 else: precision = m.log( max(abs(self.vmin), abs(self.vmax)) / difference, 10) + 1 precision = int(min(max(precision, 3), 8)) self.vmin_str = format( ('{:.' + str(precision) + '}').format(self.vmin)) self.vmax_str = format( ('{:.' + str(precision) + '}').format(self.vmax)) self.vmin_width = fm.width(self.vmin_str) self.vmax_width = fm.width(self.vmax_str) self.text_height = fm.height() * 1.5 self.text_ascent = fm.ascent() * 1.5 self.text_descent = fm.descent() * 1.5 self.setMinimumSize( max(self.vmin_width, self.vmax_width) + 20, 300) self.update()
def set(self, U=None, vmin=None, vmax=None): # normalize U fm = QFontMetrics(self.font()) self.vmin = vmin if vmin is not None else (np.min(U) if U is not None else 0.) self.vmax = vmax if vmax is not None else (np.max(U) if U is not None else 1.) precision = m.log(max(abs(self.vmin), abs(self.vmax) / abs(self.vmin - self.vmax)), 10) + 1 precision = int(min(max(precision, 3), 8)) self.vmin_str = format(('{:.' + str(precision) + '}').format(self.vmin)) self.vmax_str = format(('{:.' + str(precision) + '}').format(self.vmax)) self.vmin_width = fm.width(self.vmin_str) self.vmax_width = fm.width(self.vmax_str) self.text_height = fm.height() * 1.5 self.text_ascent = fm.ascent() * 1.5 self.text_descent = fm.descent() * 1.5 self.setMinimumSize(max(self.vmin_width, self.vmax_width) + 20, 300) self.update()
def add_labels_internal(self, gl, render_state, draw_to_canvas, labels): ''' call to add a list of labels ''' text_paint = QPainter() if draw_to_canvas: text_paint.begin(self.bitmap) text_paint.setRenderHints(QPainter.Antialiasing) u = 0 v = 0 line_height = 0 for label in labels: ascent = 0 descent = 0 measured_text_width = 0 height = 0 width = 0 font_size = label.font_size while True: metrics = None if draw_to_canvas: mask = 0x000000FF b = (label.color >> 16) & mask g = (label.color >> 8) & mask r = label.color & mask ######################################################################## LINE CHANGED text_paint.setPen(QColor(0, 0, 0)) #text_paint.setPen(QColor(r, g, b)) # The value 0.75 is hard coded representing phone pixel density text_paint.setFont(QFont('Veranda', font_size * 0.75)) # Paint.ascent is negative, so negate it. metrics = text_paint.fontMetrics() else: # The value 0.75 is hard coded representing phone pixel density metrics = QFontMetrics(QFont('Veranda', font_size * 0.75)) ascent = math.ceil(metrics.ascent()) descent = math.ceil(metrics.descent()) measured_text_width = math.ceil(metrics.boundingRect(label.string).width()) height = int(ascent) + int(descent) width = int(measured_text_width) # If it's wider than the screen, try it again with a font size of 1 # smaller. font_size -= 1 if font_size < 0 or width < render_state.screen_width: break next_u = 0 # Is there room for this string on the current line? if u + width > self.strike_width: # No room, go to the next line: u = 0 next_u = width v += line_height line_height = 0 else: next_u = u + width line_height = max(line_height, height) if (v + line_height > self.strike_height) and draw_to_canvas: raise Exception("out of texture space.") v_base = v + ascent if draw_to_canvas: text_paint.drawText(int(u), int(v_base), label.string) label.set_texture_data(width, height, u, v + height, width, -height, self.texel_width, self.texel_height) u = next_u if draw_to_canvas: text_paint.end() return v + line_height