void tst_QStaticText::plainTextVsRichText()
{
    QPixmap imagePlainText(1000, 1000);
    imagePlainText.fill(Qt::white);
    {
        QPainter p(&imagePlainText);

        QStaticText staticText;
        staticText.setText("FOObar");
        staticText.setTextFormat(Qt::PlainText);

        p.drawStaticText(10, 10, staticText);
    }

    QPixmap imageRichText(1000, 1000);
    imageRichText.fill(Qt::white);
    {
        QPainter p(&imageRichText);

        QStaticText staticText;
        staticText.setText("<html><body>FOObar</body></html>");
        staticText.setTextFormat(Qt::RichText);

        p.drawStaticText(10, 10, staticText);
    }

#if defined(DEBUG_SAVE_IMAGE)
    imagePlainText.save("plainTextVsRichText_imagePlainText.png");
    imageRichText.save("plainTextVsRichText_imageRichText.png");
#endif

    QCOMPARE(imagePlainText, imageRichText);
}
void tst_QStaticText::richTextOverridesPen()
{
    QFont font = QApplication::font();
    font.setStyleStrategy(QFont::NoAntialias);

    QFontMetricsF fm(font);
    QPixmap image(qCeil(fm.width("XXXXX")), qCeil(fm.height()));
    image.fill(Qt::white);
    {
        QPainter p(&image);
        p.setFont(font);
        p.setPen(Qt::green);

        QStaticText staticText;
        staticText.setText("<html><body><font color=\"#ff0000\">XXXXX</font></body></html>");
        staticText.setTextFormat(Qt::RichText);
        p.drawStaticText(0, 0, staticText);
    }

    QImage img = image.toImage();
    for (int x=0; x<img.width(); ++x) {
        for (int y=0; y<img.height(); ++y) {
            QRgb pixel = img.pixel(x, y);
            QVERIFY(pixel == QColor(Qt::white).rgba()
                    || pixel == QColor(Qt::red).rgba());
        }
    }
}
void tst_QStaticText::setFont()
{
    QFont font = QApplication::font();
    font.setBold(true);
    font.setPointSize(28);

    QPixmap imageDrawText(1000, 1000);
    imageDrawText.fill(Qt::white);
    {
        QPainter p(&imageDrawText);
        p.drawText(QRectF(0, 0, 1000, 1000), 0, "Lorem ipsum dolor sit amet, consectetur adipiscing elit.");

        p.setFont(font);
        p.drawText(QRectF(11, 120, 1000, 1000), 0, "Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
    }

    QPixmap imageDrawStaticText(1000, 1000);
    imageDrawStaticText.fill(Qt::white);
    {
        QPainter p(&imageDrawStaticText);

        QStaticText text;
        text.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
        text.setTextFormat(Qt::PlainText);

        p.drawStaticText(0, 0, text);

        p.setFont(font);
        p.drawStaticText(11, 120, text);
    }

#if defined(DEBUG_SAVE_IMAGE)
    imageDrawText.save("setFont_imageDrawText.png");
    imageDrawStaticText.save("setFont_imageDrawStaticText.png");
#endif

    QCOMPARE(imageDrawStaticText, imageDrawText);
}
Beispiel #4
0
void text_display::paintEvent(QPaintEvent *event)
{
	int offset = get_offset();
	int end_offset = get_rows() * get_columns() + offset;
	QPainter painter(this);
	QColor text = palette().color(QPalette::WindowText);
	painter.setPen(text);
	painter.setFont(editor_font::get_font());
	painter.setClipping(true);
	
	const bookmark_map *bookmarks = buffer->get_bookmark_map();
	if(bookmarks){
		for(const auto &bookmark : *bookmarks){
			selection bookmark_selection = selection::create_selection(
							buffer->snes_to_pc(bookmark.address), bookmark.size);
			//non-sequential just skip
			if(bookmark_selection.get_end_byte() < offset || 
			   bookmark_selection.get_start_byte() > end_offset){
				continue;
			}
			paint_selection(painter, bookmark_selection, bookmark.color);
		}
	}
	
	if(editor->is_comparing()){
		auto diffs = editor->get_diff();
		for(auto &diff : *diffs){
			if(diff.get_end_byte() < offset){
				continue;
			}else if(diff.get_start_byte() > end_offset){ //sequential, break early
				break;
			}
			paint_selection(painter, diff, diff_color);
		}
	}
	
	selection selection_area = get_selection();
	
	selection_color.setAlpha(170);
	if(selection_area.is_active()){
		paint_selection(painter, selection_area, selection_color);
	}
	
	painter.setClipRegion(event->region());
	
	if(!selection_area.is_active()){
		painter.setClipping(false);
		QPoint cursor_position = nibble_to_screen(get_cursor_nibble());
		if(cursor_state && focusPolicy() != Qt::NoFocus){
			painter.fillRect(cursor_position.x(), cursor_position.y(), 
			                 cursor_width, editor_font::get_height(), text);
		}
		QRect active_line(0, cursor_position.y(), 
		                  get_line_characters() * editor_font::get_width(), editor_font::get_height());
		painter.fillRect(active_line, selection_color);
	}

	for(int i = offset, row = 0; i < end_offset; i += get_columns(), row++){
		int real_row = i / get_columns();
		if(!row_cache.contains(real_row)){
			int line_end = i + get_columns();	
			if(line_end > buffer->size()){
				line_end = buffer->size();
			}
			QString line;
			QTextStream string_stream(&line);
			get_line(i, line_end, string_stream);
			QStaticText *text = new QStaticText(line);
			text->setTextFormat(Qt::PlainText);
			row_cache.insert(real_row, text);
		}
		if(row * editor_font::get_height() >= event->rect().y()){
			painter.drawStaticText(0, row * editor_font::get_height(), *row_cache.object(real_row));
		}
	}
}