void NetHackQtMapWindow::paintEvent()

in win/Qt4/qt4map.cpp [778:913]


void NetHackQtMapWindow::paintEvent(QPaintEvent* event)
{
    QRect area=event->rect();
    QRect garea;
    garea.setCoords(
	std::max(0,area.left()/qt_settings->glyphs().width()),
	std::max(0,area.top()/qt_settings->glyphs().height()),
	std::min(COLNO-1,area.right()/qt_settings->glyphs().width()),
	std::min(ROWNO-1,area.bottom()/qt_settings->glyphs().height())
    );

    QPainter painter;

    painter.begin(this);

    if (is_rogue_level(&u.uz) || iflags.wc_ascii_map) {
	// You enter a VERY primitive world!

	painter.setClipRect( event->rect() ); // (normally we don't clip)
	painter.fillRect( event->rect(), Qt::black );

	if ( !rogue_font ) {
	    // Find font...
	    int pts = 5;
	    QString fontfamily = iflags.wc_font_map
		? iflags.wc_font_map : "Courier";
	    bool bold = false;
	    if ( fontfamily.right(5).toLower() == "-bold" ) {
		fontfamily.truncate(fontfamily.length()-5);
		bold = true;
	    }
	    while ( pts < 32 ) {
		QFont f(fontfamily, pts, bold ? QFont::Bold : QFont::Normal);
		painter.setFont(QFont(fontfamily, pts));
		QFontMetrics fm = painter.fontMetrics();
		if ( fm.width("M") > qt_settings->glyphs().width() )
		    break;
		if ( fm.height() > qt_settings->glyphs().height() )
		    break;
		pts++;
	    }
	    rogue_font = new QFont(fontfamily,pts-1);
	}
	painter.setFont(*rogue_font);

	for (int j=garea.top(); j<=garea.bottom(); j++) {
	    for (int i=garea.left(); i<=garea.right(); i++) {
		unsigned short g=Glyph(i,j);
		int color;
		char32_t ch;
		unsigned special;

		painter.setPen( Qt::green );
		/* map glyph to character and color */
    		mapglyph(g, &ch, &color, &special, i, j, 0);
#ifdef TEXTCOLOR
		painter.setPen( nhcolor_to_pen(color) );
#endif
		painter.drawText(
		    i*qt_settings->glyphs().width(),
		    j*qt_settings->glyphs().height(),
		    qt_settings->glyphs().width(),
		    qt_settings->glyphs().height(),
		    Qt::AlignCenter,
		    QString(QChar(ch)).left(1)
		);
#ifdef TEXTCOLOR
		if (((special & MG_PET) != 0) && ::iflags.hilite_pet) {
                    painter.drawPixmap(QPoint(i*qt_settings->glyphs().width(), j*qt_settings->glyphs().height()), pet_annotation);
                } else if (((special & MG_OBJPILE) != 0) && ::iflags.hilite_pile) {
                    painter.drawPixmap(QPoint(i*qt_settings->glyphs().width(), j*qt_settings->glyphs().height()), pile_annotation);
                }
#endif
	    }
	}

	painter.setFont(font());
    } else {
	for (int j=garea.top(); j<=garea.bottom(); j++) {
	    for (int i=garea.left(); i<=garea.right(); i++) {
		unsigned short g=Glyph(i,j);
		int color;
		int ch;
		unsigned special;
		mapglyph(g, &ch, &color, &special, i, j, 0);
		qt_settings->glyphs().drawCell(painter, g, i, j);
#ifdef TEXTCOLOR
		if (((special & MG_PET) != 0) && ::iflags.hilite_pet) {
                    painter.drawPixmap(QPoint(i*qt_settings->glyphs().width(), j*qt_settings->glyphs().height()), pet_annotation);
                } else if (((special & MG_OBJPILE) != 0) && ::iflags.hilite_pile) {
                    painter.drawPixmap(QPoint(i*qt_settings->glyphs().width(), j*qt_settings->glyphs().height()), pile_annotation);
                }
#endif
	    }
	}
    }

    if (garea.contains(cursor)) {
	if (Is_rogue_level(&u.uz)) {
#ifdef TEXTCOLOR
	    painter.setPen( Qt::white );
#else
	    painter.setPen( Qt::green ); // REALLY primitive
#endif
	} else
	{
	    int hp100;
	    if (u.mtimedone) {
		hp100=u.mhmax ? u.mh*100/u.mhmax : 100;
	    } else {
		hp100=u.uhpmax ? u.uhp*100/u.uhpmax : 100;
	    }

	    if (hp100 > 75) painter.setPen(Qt::white);
	    else if (hp100 > 50) painter.setPen(Qt::yellow);
	    else if (hp100 > 25) painter.setPen(QColor(0xff,0xbf,0x00)); // orange
	    else if (hp100 > 10) painter.setPen(Qt::red);
	    else painter.setPen(Qt::magenta);
	}

	painter.drawRect(
	    cursor.x()*qt_settings->glyphs().width(),cursor.y()*qt_settings->glyphs().height(),
	    qt_settings->glyphs().width()-1,qt_settings->glyphs().height()-1);
    }

    if (area.intersects(messages_rect)) {
	painter.setPen(Qt::black);
	painter.drawText(viewport.contentsX()+1,viewport.contentsY()+1,
	    viewport.width(),0, Qt::TextWordWrap|Qt::AlignTop|Qt::AlignLeft|Qt::TextDontClip, messages);
	painter.setPen(Qt::white);
	painter.drawText(viewport.contentsX(),viewport.contentsY(),
	    viewport.width(),0, Qt::TextWordWrap|Qt::AlignTop|Qt::AlignLeft|Qt::TextDontClip, messages);
    }

    painter.end();
}