File: gl_label.h

package info (click to toggle)
meshlab 2020.09%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 45,132 kB
  • sloc: cpp: 400,238; ansic: 31,952; javascript: 1,578; sh: 387; yacc: 238; lex: 139; python: 86; makefile: 30
file content (198 lines) | stat: -rwxr-xr-x 6,745 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/****************************************************************************
* MeshLab                                                           o o     *
* An extendible mesh processor                                    o     o   *
*                                                                _   O  _   *
* Copyright(C) 2005, 2009                                          \/)\/    *
* Visual Computing Lab                                            /\/|      *
* ISTI - Italian National Research Council                           |      *
*                                                                    \      *
* All rights reserved.                                                      *
*                                                                           *
* This program is free software; you can redistribute it and/or modify      *
* it under the terms of the GNU General Public License as published by      *
* the Free Software Foundation; either version 2 of the License, or         *
* (at your option) any later version.                                       *
*                                                                           *
* This program is distributed in the hope that it will be useful,           *
* but WITHOUT ANY WARRANTY; without even the implied warranty of            *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
* for more details.                                                         *
*                                                                           *
****************************************************************************/
#ifndef VCG_WRAP_QT_GLLABEL_H
#define VCG_WRAP_QT_GLLABEL_H

#include <QMessageBox>
#include <wrap/qt/col_qt_convert.h>
#include <wrap/qt/device_to_logical.h>
#include <wrap/qt/checkGLError.h>
#include <QPainter>
namespace vcg
{
/*
 */
  class glLabel
  {

    public:
    class Mode
    {
    public:
      Mode()
      {
        init();
      }
      Mode(Color4b _color)
      {
        init();
        color=_color;
      }

      void init()
      {
        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|GL_VIEWPORT_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();
    }

  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);
      glViewport(view[0],view[1],view[2],view[3]);
    }

    static void render(QPainter *painter, const vcg::Point3f &p, const QString &text)
    {
      Mode m;
      render(painter,p,text,m);
    }

    static void render(QPainter *painter, const vcg::Point3f &p, const QString &text, const Mode &m)
    {
      GLdouble model[16];
      GLdouble proj[16];
      GLint view[4];

      glGetDoublev(GL_MODELVIEW_MATRIX, model);
      glGetDoublev(GL_PROJECTION_MATRIX, proj);
      glGetIntegerv(GL_VIEWPORT, view);
      GLdouble winx,winy,winz;

      gluProject(p[0],p[1],p[2],model,proj,view,&winx,&winy,&winz);

      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(QTDeviceToLogical(painter,winx),
                                 QTDeviceToLogical(painter,view[3]-winy)));
      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::Point3d &p, const QString &text)
    { render(painter,Point3f::Construct(p),text); }
    static void render(QPainter *painter, const vcg::Point3d &p, const QString &text, const Mode &m)
    { render(painter,Point3f::Construct(p),text,m); }

  };
} // end namespace

#endif