00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef VCG_WRAP_QT_GLLABEL_H
00024 #define VCG_WRAP_QT_GLLABEL_H
00025
00026 #include <QMessageBox>
00027 #include <wrap/qt/col_qt_convert.h>
00028 #include <wrap/qt/device_to_logical.h>
00029 #include <wrap/qt/checkGLError.h>
00030 #include <QPainter>
00031 namespace vcg
00032 {
00033
00034
00035 class glLabel
00036 {
00037
00038 public:
00039 class Mode
00040 {
00041 public:
00042 Mode()
00043 {
00044 init();
00045 }
00046 Mode(Color4b _color)
00047 {
00048 init();
00049 color=_color;
00050 }
00051
00052 void init()
00053 {
00054 color=vcg::Color4b(vcg::Color4b::White);
00055 angle=0;
00056 rightAlign = false;
00057 qFont.setStyleStrategy(QFont::NoAntialias);
00058 qFont.setFamily("Helvetica");
00059 qFont.setPixelSize(12);
00060 }
00061
00062 Mode(QFont &_qFont, vcg::Color4b _color, float _angle,bool _rightAlign)
00063 {
00064 qFont=_qFont;
00065 color=_color;
00066 angle=_angle;
00067 rightAlign=_rightAlign;
00068 }
00069
00070 float angle;
00071 bool rightAlign;
00072 vcg::Color4b color;
00073 QFont qFont;
00074 };
00075
00076 private:
00077 static void enter2D(QPainter *painter)
00078 {
00079 glPushAttrib(GL_ENABLE_BIT|GL_VIEWPORT_BIT);
00080 glDisable(GL_DEPTH_TEST);
00081 glMatrixMode(GL_PROJECTION);
00082 glPushMatrix();
00083 glMatrixMode(GL_MODELVIEW);
00084 glPushMatrix();
00085 painter->endNativePainting();
00086 painter->save();
00087 }
00088
00089 static void exit2D(QPainter *painter)
00090 {
00091
00092 painter->restore();
00093 painter->beginNativePainting();
00094 glMatrixMode(GL_PROJECTION);
00095 glPopMatrix();
00096 glMatrixMode(GL_MODELVIEW);
00097 glPopMatrix();
00098 glPopAttrib();
00099 }
00100
00101 public:
00102 enum LabelPosition { TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT} ;
00103
00104 static void render2D(QPainter *painter, const LabelPosition pos, const QString &text, const Mode &m=Mode())
00105 {
00106 render2D(painter,pos,0,text,m);
00107 }
00108 static void render2D(QPainter *painter, const LabelPosition pos, int linePos, const QString &text, const Mode &m=Mode())
00109 {
00110 Mode lm = m;
00111 if(pos == TOP_RIGHT || pos == BOTTOM_RIGHT)
00112 lm.rightAlign=true;
00113
00114 GLint view[4];
00115 glGetIntegerv(GL_VIEWPORT, view);
00116 QFontMetrics qfm(m.qFont);
00117 float delta = qfm.ascent()/2;
00118 switch(pos)
00119 {
00120 case TOP_LEFT : render2D(painter,vcg::Point2f(delta, view[3]-3*delta - delta*3*linePos),text,lm); break;
00121 case TOP_RIGHT : render2D(painter,vcg::Point2f(view[2]-delta, view[3]-3*delta - delta*3*linePos),text,lm); break;
00122 case BOTTOM_LEFT : render2D(painter,vcg::Point2f(delta, 3*delta + delta*3*linePos),text,lm); break;
00123 case BOTTOM_RIGHT : render2D(painter,vcg::Point2f(view[2]-delta, 3*delta + delta*3*linePos),text,lm); break;
00124 }
00125 }
00126
00127
00128
00129
00130 static void render2D(QPainter *painter, const vcg::Point2f &p, const QString &text, const Mode &m)
00131 {
00132 GLint view[4];
00133 glGetIntegerv(GL_VIEWPORT, view);
00134 QFontMetrics qfm(m.qFont);
00135 QRect textBox = qfm.boundingRect(text);
00136 glLabel::enter2D(painter);
00137 painter->setRenderHint(QPainter::TextAntialiasing);
00138 painter->setPen(vcg::ColorConverter::ToQColor(m.color));
00139 painter->setFont(m.qFont);
00140
00141 painter->translate(QPointF(p[0],view[3]-p[1]));
00142 painter->rotate(m.angle);
00143 QPoint base(0,qfm.ascent()/2);
00144 if(m.rightAlign)
00145 base.setX(-textBox.width() -qfm.maxWidth());
00146 painter->drawText(base,text);
00147 glLabel::exit2D(painter);
00148 glViewport(view[0],view[1],view[2],view[3]);
00149 }
00150
00151 static void render(QPainter *painter, const vcg::Point3f &p, const QString &text)
00152 {
00153 Mode m;
00154 render(painter,p,text,m);
00155 }
00156
00157 static void render(QPainter *painter, const vcg::Point3f &p, const QString &text, const Mode &m)
00158 {
00159 GLdouble model[16];
00160 GLdouble proj[16];
00161 GLint view[4];
00162
00163 glGetDoublev(GL_MODELVIEW_MATRIX, model);
00164 glGetDoublev(GL_PROJECTION_MATRIX, proj);
00165 glGetIntegerv(GL_VIEWPORT, view);
00166 GLdouble winx,winy,winz;
00167
00168 gluProject(p[0],p[1],p[2],model,proj,view,&winx,&winy,&winz);
00169
00170 QFontMetrics qfm(m.qFont);
00171 QRect textBox = qfm.boundingRect(text);
00172 glLabel::enter2D(painter);
00173
00174 painter->setRenderHint(QPainter::TextAntialiasing);
00175 painter->setPen(vcg::ColorConverter::ToQColor(m.color));
00176 painter->setFont(m.qFont);
00177 painter->translate(QPointF(QTDeviceToLogical(painter,winx),
00178 QTDeviceToLogical(painter,view[3]-winy)));
00179 painter->rotate(m.angle);
00180 QPoint base(0,qfm.ascent()/2);
00181 if(m.rightAlign)
00182 base.setX(-textBox.width() -qfm.maxWidth());
00183 painter->drawText(base,text);
00184
00185 glLabel::exit2D(painter);
00186 }
00187
00188
00189
00190 static void render(QPainter *painter, const vcg::Point3d &p, const QString &text)
00191 { render(painter,Point3f::Construct(p),text); }
00192 static void render(QPainter *painter, const vcg::Point3d &p, const QString &text, const Mode &m)
00193 { render(painter,Point3f::Construct(p),text,m); }
00194
00195 };
00196 }
00197
00198 #endif