def CreateTopImage( width, title, payload_description, text ): text_extent_qt_image = HG.client_controller.bitmap_manager.GetQtImage( 20, 20, 24 ) painter = QG.QPainter( text_extent_qt_image ) text_font = QW.QApplication.font() basic_font_size = text_font.pointSize() payload_description_font = QW.QApplication.font() payload_description_font.setPointSize( int( basic_font_size * 1.4 ) ) title_font = QW.QApplication.font() title_font.setPointSize( int( basic_font_size * 2.0 ) ) texts_to_draw = [] current_y = 6 for ( t, f ) in ( ( title, title_font ), ( payload_description, payload_description_font ), ( text, text_font ) ): painter.setFont( f ) wrapped_texts = WrapText( painter, t, width - 20 ) line_height = painter.fontMetrics().height() wrapped_texts_with_ys = [] if len( wrapped_texts ) > 0: current_y += 10 for wrapped_text in wrapped_texts: wrapped_texts_with_ys.append( ( wrapped_text, current_y ) ) current_y += line_height + 4 texts_to_draw.append( ( wrapped_texts_with_ys, f ) ) current_y += 6 top_height = current_y del painter del text_extent_qt_image # top_qt_image = HG.client_controller.bitmap_manager.GetQtImage( width, top_height, 24 ) painter = QG.QPainter( top_qt_image ) painter.setBackground( QG.QBrush( QC.Qt.white ) ) painter.eraseRect( painter.viewport() ) # painter.drawPixmap( width-16-5, 5, CC.global_pixmaps().file_repository ) # for ( wrapped_texts_with_ys, f ) in texts_to_draw: painter.setFont( f ) for ( wrapped_text, y ) in wrapped_texts_with_ys: ( text_size, wrapped_text ) = ClientGUIFunctions.GetTextSizeFromPainter( painter, wrapped_text ) ClientGUIFunctions.DrawText( painter, ( width - text_size.width() ) // 2, y, wrapped_text ) del painter data_bytearray = top_qt_image.bits() if QP.qtpy.PYSIDE2: data_bytes = bytes( data_bytearray ) elif QP.qtpy.PYQT5: data_bytes = data_bytearray.asstring( top_height * width * 3 ) top_image_rgb = numpy.fromstring( data_bytes, dtype = 'uint8' ).reshape( ( top_height, width, 3 ) ) top_image = cv2.cvtColor( top_image_rgb, cv2.COLOR_RGB2GRAY ) top_height_header = struct.pack( '!H', top_height ) byte0 = top_height_header[0:1] byte1 = top_height_header[1:2] top_image[0][0] = ord( byte0 ) top_image[0][1] = ord( byte1 ) return top_image
def TextExceedsWidth( painter, text, width ): ( text_size, text ) = ClientGUIFunctions.GetTextSizeFromPainter( painter, text ) return text_size.width() > width