Strongly restructured the glLabel utility class for writing on opengl/qt painter context. Added methods for on screen printing on the viewport corners with almost automatic line managment.

This commit is contained in:
Paolo Cignoni 2010-10-31 13:06:03 +00:00
parent 80a8c29144
commit 3f48658c57
1 changed files with 116 additions and 37 deletions

View File

@ -29,23 +29,120 @@
#include <QPainter> #include <QPainter>
namespace vcg namespace vcg
{ {
/*
*/
class glLabel class glLabel
{ {
public: public:
class Mode
{
public:
Mode()
{
color=vcg::Color4b(vcg::Color4b::White);
angle=0;
rightAlign = false;
qFont.setStyleStrategy(QFont::NoAntialias);
qFont.setFamily("Helvetica");
qFont.setPixelSize(12);
}
Mode(QFont &_qFont, vcg::Color4b _color, float _angle,bool _rightAlign)
{
qFont=_qFont;
color=_color;
angle=_angle;
rightAlign=_rightAlign;
}
float angle;
bool rightAlign;
vcg::Color4b color;
QFont qFont;
};
private:
static void enter2D(QPainter *painter)
{
glPushAttrib(GL_ENABLE_BIT);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
painter->endNativePainting();
painter->save();
}
static void exit2D(QPainter *painter)
{
checkGLError::qDebug("glLabel");
painter->restore();
painter->beginNativePainting();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glPopAttrib();
checkGLError::qDebug("glLabel");
}
public:
enum LabelPosition { TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT} ;
static void render2D(QPainter *painter, const LabelPosition pos, const QString &text, const Mode &m=Mode())
{
render2D(painter,pos,0,text,m);
}
static void render2D(QPainter *painter, const LabelPosition pos, int linePos, const QString &text, const Mode &m=Mode())
{
Mode lm = m;
if(pos == TOP_RIGHT || pos == BOTTOM_RIGHT)
lm.rightAlign=true;
GLint view[4];
glGetIntegerv(GL_VIEWPORT, view);
QFontMetrics qfm(m.qFont);
float delta = qfm.ascent()/2;
switch(pos)
{
case TOP_LEFT : render2D(painter,vcg::Point2f(delta, view[3]-3*delta - delta*3*linePos),text,lm); break;
case TOP_RIGHT : render2D(painter,vcg::Point2f(view[2]-delta, view[3]-3*delta - delta*3*linePos),text,lm); break;
case BOTTOM_LEFT : render2D(painter,vcg::Point2f(delta, 3*delta + delta*3*linePos),text,lm); break;
case BOTTOM_RIGHT : render2D(painter,vcg::Point2f(view[2]-delta, 3*delta + delta*3*linePos),text,lm); break;
}
}
/*
*/
static void render2D(QPainter *painter, const vcg::Point2f &p, const QString &text, const Mode &m)
{
GLint view[4];
glGetIntegerv(GL_VIEWPORT, view);
QFontMetrics qfm(m.qFont);
QRect textBox = qfm.boundingRect(text);
glLabel::enter2D(painter);
painter->setRenderHint(QPainter::TextAntialiasing);
painter->setPen(vcg::ColorConverter::ToQColor(m.color));
painter->setFont(m.qFont);
painter->translate(QPointF(p[0],view[3]-p[1]));
painter->rotate(m.angle);
QPoint base(0,qfm.ascent()/2);
if(m.rightAlign)
base.setX(-textBox.width() -qfm.maxWidth());
painter->drawText(base,text);
glLabel::exit2D(painter);
}
static void render(QPainter *painter, const vcg::Point3f &p, const QString &text) static void render(QPainter *painter, const vcg::Point3f &p, const QString &text)
{ {
QFont qFont; Mode m;
qFont.setStyleStrategy(QFont::NoAntialias); render(painter,p,text,m);
qFont.setFamily("Helvetica");
qFont.setPixelSize(12);
vcg::Color4b color(vcg::Color4b::White);
render(painter,p,text,qFont,color);
} }
static void render(QPainter *painter, const vcg::Point3f &p, const QString &text, QFont qFont, vcg::Color4b color, float angle =0,bool rightAlign=false) static void render(QPainter *painter, const vcg::Point3f &p, const QString &text, Mode &m)
{ {
GLdouble model[16]; GLdouble model[16];
GLdouble proj[16]; GLdouble proj[16];
@ -58,47 +155,29 @@ namespace vcg
gluProject(p[0],p[1],p[2],model,proj,view,&winx,&winy,&winz); gluProject(p[0],p[1],p[2],model,proj,view,&winx,&winy,&winz);
glPushAttrib(GL_ENABLE_BIT); QFontMetrics qfm(m.qFont);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
QFontMetrics qfm(qFont);
QRect textBox = qfm.boundingRect(text); QRect textBox = qfm.boundingRect(text);
painter->endNativePainting(); glLabel::enter2D(painter);
painter->save();
painter->setRenderHint(QPainter::TextAntialiasing); painter->setRenderHint(QPainter::TextAntialiasing);
painter->setPen(vcg::ColorConverter::ToQColor(color)); painter->setPen(vcg::ColorConverter::ToQColor(m.color));
painter->setFont(qFont); painter->setFont(m.qFont);
//qDebug("Printing at %f %f (%f %f %f) '%s'",winx,winy,p[0],p[1],p[2],qPrintable(text));
painter->translate(QPointF(winx,view[3]-winy)); painter->translate(QPointF(winx,view[3]-winy));
painter->rotate(angle); painter->rotate(m.angle);
//painter->drawText(QPointF(0,0),text);
//painter->drawText(QRect(),Qt::AlignLeft+Qt::AlignVCenter,text);
//painter->drawText(QRect(view[0],view[1],view[2],view[3]),Qt::AlignLeft+Qt::AlignVCenter,text);
//QPoint base(0,textBox.height()/2);
QPoint base(0,qfm.ascent()/2); QPoint base(0,qfm.ascent()/2);
if(rightAlign) if(m.rightAlign)
base.setX(-textBox.width() -qfm.maxWidth()); base.setX(-textBox.width() -qfm.maxWidth());
painter->drawText(base,text); painter->drawText(base,text);
//painter->drawText(QPointF(winx,view[3]-winy),text);
painter->restore(); glLabel::exit2D(painter);
painter->beginNativePainting();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glPopAttrib();
checkGLError::qDebug("myRenderText");
} }
static void render(QPainter *painter, const vcg::Point3d &p, const QString &text) static void render(QPainter *painter, const vcg::Point3d &p, const QString &text)
{ render(painter,Point3f::Construct(p),text); } { render(painter,Point3f::Construct(p),text); }
static void render(QPainter *painter, const vcg::Point3d &p, const QString &text, QFont qFont, vcg::Color4b color) static void render(QPainter *painter, const vcg::Point3d &p, const QString &text, Mode &m)
{ render(painter,Point3f::Construct(p),text,qFont,color); } { render(painter,Point3f::Construct(p),text,m); }
}; };
} // end namespace } // end namespace