Debian Codegraph @

src/Gui/SoTextLabel.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2009 Werner Mayer <wmayer[at]users.sourceforge.net> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23 #include <FCConfig.h>
24
25 #ifdef FC_OS_WIN32
26 # include <windows.h>
27 #endif
28 #ifdef FC_OS_MACOSX
29 # include <OpenGL/gl.h>
30 #else
31 # include <GL/gl.h>
32 #endif
33 #include <QFontMetrics>
34 #include <QPainter>
35 #include <QPen>
36 #include <Inventor/actions/SoGLRenderAction.h>
37 #include <Inventor/bundles/SoMaterialBundle.h>
38 #include <Inventor/elements/SoLazyElement.h>
39 #include <Inventor/misc/SoState.h>
40
41 #include <Inventor/C/basic.h>
42 #include <Inventor/draggers/SoTranslate2Dragger.h>
43 #include <Inventor/elements/SoCullElement.h>
44 #include <Inventor/elements/SoFontNameElement.h>
45 #include <Inventor/elements/SoGLTextureEnabledElement.h>
46 #include <Inventor/elements/SoModelMatrixElement.h>
47 #include <Inventor/elements/SoProjectionMatrixElement.h>
48 #include <Inventor/elements/SoViewingMatrixElement.h>
49 #include <Inventor/elements/SoViewportRegionElement.h>
50 #include <Inventor/elements/SoViewVolumeElement.h>
51 #include <Inventor/elements/SoMultiTextureEnabledElement.h>
52
53 #include "SoTextLabel.h"
54 #include "SoFCInteractiveElement.h"
55 #include "Tools.h"
56
57
58 using namespace Gui;
59
60
61 SO_NODE_SOURCE(SoTextLabel)
62
63 void SoTextLabel::initClass()
64 {
65 SO_NODE_INIT_CLASS(SoTextLabel, SoText2, "Text2");
66 }
67
68 SoTextLabel::SoTextLabel()
69 {
70 SO_NODE_CONSTRUCTOR(SoTextLabel);
71 SO_NODE_ADD_FIELD(backgroundColor, (SbVec3f(1.0f, 1.0f, 1.0f)));
72 SO_NODE_ADD_FIELD(background, (true));
73 SO_NODE_ADD_FIELD(frameSize, (10.0f));
74 }
75
76 /**
77 * Renders the label.
78 */
79 void SoTextLabel::GLRender(SoGLRenderAction* action)
80 {
81 if (!this->shouldGLRender(action)) {
82 return;
83 }
84
85 // only draw text without background
86 if (!this->background.getValue()) {
87 inherited::GLRender(action);
88 return;
89 }
90
91 SoState* state = action->getState();
92
93 state->push();
94 SoLazyElement::setLightModel(state, SoLazyElement::BASE_COLOR);
95
96 SbBox3f box;
97 SbVec3f center;
98 this->computeBBox(action, box, center);
99
100 if (!SoCullElement::cullTest(state, box, true)) {
101 SoMaterialBundle mb(action);
102 mb.sendFirst();
103 const SbMatrix& mat = SoModelMatrixElement::get(state);
104 const SbMatrix& projmatrix
105 = (mat * SoViewingMatrixElement::get(state) * SoProjectionMatrixElement::get(state));
106 const SbViewportRegion& vp = SoViewportRegionElement::get(state);
107 SbVec2s vpsize = vp.getViewportSizePixels();
108
109 // font stuff
110 SbName fontname = SoFontNameElement::get(state);
111 int lines = this->string.getNum();
112
113 // get left bottom corner of the label
114 SbVec3f nilpoint(0.0f, 0.0f, 0.0f);
115 projmatrix.multVecMatrix(nilpoint, nilpoint);
116 nilpoint[0] = (nilpoint[0] + 1.0f) * 0.5f * vpsize[0];
117 nilpoint[1] = (nilpoint[1] + 1.0f) * 0.5f * vpsize[1];
118
119 // Unfortunately, the size of the label is stored in the pimpl class of
120 // SoText2 which cannot be accessed directly. However, there is a trick
121 // to get the required information: set model, viewing and projection
122 // matrix to the identity matrix and also view volume to some default
123 // values. SoText2::computeBBox() then calls SoText2P::getQuad which
124 // returns the sizes in form of the bounding box. These values can be
125 // reverse-engineered to get width and height.
126 state->push();
127 SoModelMatrixElement::set(state, this, SbMatrix::identity());
128 SoViewingMatrixElement::set(state, this, SbMatrix::identity());
129 SoProjectionMatrixElement::set(state, this, SbMatrix::identity());
130 SbViewVolume vv;
131 vv.ortho(-1, 1, -1, 1, -1, 1);
132 SoViewVolumeElement::set(state, this, vv);
133
134 SbBox3f box;
135 SbVec3f center;
136 this->computeBBox(action, box, center);
137 state->pop();
138
139 float xmin, ymin, zmin, xmax, ymax, zmax;
140 box.getBounds(xmin, ymin, zmin, xmax, ymax, zmax);
141 SbVec3f v0(xmin, ymax, zmax);
142 SbVec3f v1(xmax, ymax, zmax);
143 SbVec3f v2(xmax, ymin, zmax);
144 SbVec3f v3(xmin, ymin, zmax);
145 vv.projectToScreen(v0, v0);
146 vv.projectToScreen(v1, v1);
147 vv.projectToScreen(v2, v2);
148 vv.projectToScreen(v3, v3);
149
150 float width, height;
151 width = (v1[0] - v0[0]) * vpsize[0];
152 height = (v1[1] - v3[1]) * vpsize[1];
153 switch (this->justification.getValue()) {
154 case SoText2::RIGHT:
155 nilpoint[0] -= width;
156 break;
157 case SoText2::CENTER:
158 nilpoint[0] -= 0.5f * width;
159 break;
160 default:
161 break;
162 }
163
164 if (lines > 1) {
165 nilpoint[1] -= (float(lines - 1) / (float)lines * height);
166 }
167
168 SbVec3f toppoint = nilpoint;
169 toppoint[0] += width;
170 toppoint[1] += height;
171
172 // Set new state.
173 glMatrixMode(GL_MODELVIEW);
174 glPushMatrix();
175 glLoadIdentity();
176 glMatrixMode(GL_PROJECTION);
177 glPushMatrix();
178 glLoadIdentity();
179 glOrtho(0, vpsize[0], 0, vpsize[1], -1.0f, 1.0f);
180 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
181
182 state->push();
183
184 // disable textures for all units
185 SoGLTextureEnabledElement::set(state, this, false);
186 SoMultiTextureEnabledElement::set(state, this, false);
187
188 glPushAttrib(GL_ENABLE_BIT | GL_PIXEL_MODE_BIT | GL_COLOR_BUFFER_BIT);
189 glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
190
191 // color and frame size
192 SbColor color = this->backgroundColor.getValue();
193 float fs = this->frameSize.getValue();
194
195 // draw background
196 glColor3f(color[0], color[1], color[2]);
197 glBegin(GL_QUADS);
198 glVertex3f(nilpoint[0] - fs, nilpoint[1] - fs, 0.0f);
199 glVertex3f(toppoint[0] + fs, nilpoint[1] - fs, 0.0f);
200 glVertex3f(toppoint[0] + fs, toppoint[1] + fs, 0.0f);
201 glVertex3f(nilpoint[0] - fs, toppoint[1] + fs, 0.0f);
202 glEnd();
203
204 // pop old state
205 glPopClientAttrib();
206 glPopAttrib();
207 state->pop();
208
209 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
210 // Pop old GL matrix state.
211 glMatrixMode(GL_PROJECTION);
212 glPopMatrix();
213 glMatrixMode(GL_MODELVIEW);
214 glPopMatrix();
215 }
216
217 state->pop();
218
219 inherited::GLRender(action);
220 }
221
222 // ------------------------------------------------------
223
224 SO_NODE_SOURCE(SoColorBarLabel)
225
226 void SoColorBarLabel::initClass()
227 {
228 SO_NODE_INIT_CLASS(SoColorBarLabel, SoText2, "Text2");
229 }
230
231 SoColorBarLabel::SoColorBarLabel()
232 {
233 SO_NODE_CONSTRUCTOR(SoColorBarLabel);
234 }
235
236 void SoColorBarLabel::computeBBox(SoAction* action, SbBox3f& box, SbVec3f& center)
237 {
238 inherited::computeBBox(action, box, center);
239 if (!box.hasVolume()) {
240 SbViewVolume vv = SoViewVolumeElement::get(action->getState());
241 // workaround for https://github.com/coin3d/coin/issues/417:
242 // extend by 2 percent
243 vv.scaleWidth(1.02f);
244 SoViewVolumeElement::set(action->getState(), this, vv);
245 inherited::computeBBox(action, box, center);
246 }
247 }
248
249 // ------------------------------------------------------
250
251 SO_NODE_SOURCE(SoStringLabel)
252
253 void SoStringLabel::initClass()
254 {
255 SO_NODE_INIT_CLASS(SoStringLabel, SoNode, "Node");
256 }
257
258 SoStringLabel::SoStringLabel()
259 {
260 SO_NODE_CONSTRUCTOR(SoStringLabel);
261 SO_NODE_ADD_FIELD(string, (""));
262 SO_NODE_ADD_FIELD(textColor, (SbVec3f(1.0f, 1.0f, 1.0f)));
263 SO_NODE_ADD_FIELD(name, ("Helvetica"));
264 SO_NODE_ADD_FIELD(size, (12));
265 }
266
267 /**
268 * Renders the open edges only.
269 */
270 void SoStringLabel::GLRender(SoGLRenderAction* action)
271 {
272 QOpenGLWidget* window;
273 SoState* state = action->getState();
274 state->push();
275 SoLazyElement::setLightModel(state, SoLazyElement::BASE_COLOR);
276 SoGLWidgetElement::get(state, window);
277 if (!window) {
278 state->pop();
279 return;
280 }
281
282 // Enter in 2D screen mode
283 glMatrixMode(GL_PROJECTION);
284 glPushMatrix();
285 glLoadIdentity();
286 glOrtho(-1, 1, -1, 1, -1, 1);
287 glMatrixMode(GL_MODELVIEW);
288 glPushMatrix();
289 glLoadIdentity();
290 glPushAttrib(GL_ENABLE_BIT);
291 glDisable(GL_DEPTH_TEST);
292 glDisable(GL_LIGHTING);
293 glDisable(GL_TEXTURE_2D);
294 glEnable(GL_BLEND);
295
296 QFont font;
297 font.setStyleStrategy(QFont::NoAntialias);
298 font.setFamily(QLatin1String(this->name.getValue()));
299 font.setPixelSize(this->size.getValue());
300
301 glBlendFunc(GL_ONE, GL_SRC_ALPHA);
302
303 // text color
304 SbColor color = this->textColor.getValue();
305 glColor4f(color[0], color[1], color[2], 1);
306 const SbMatrix& mat = SoModelMatrixElement::get(state);
307 const SbMatrix& projmatrix
308 = (mat * SoViewingMatrixElement::get(state) * SoProjectionMatrixElement::get(state));
309 SbVec3f nil(0.0f, 0.0f, 0.0f);
310 projmatrix.multVecMatrix(nil, nil);
311 QStringList list;
312 for (int i = 0; i < this->string.getNum(); i++) {
313 list << QLatin1String(this->string[i].getString());
314 }
315
316 // Leave 2D screen mode
317 glPopAttrib();
318 glPopMatrix();
319 glMatrixMode(GL_PROJECTION);
320 glPopMatrix();
321 glMatrixMode(GL_MODELVIEW);
322
323 state->pop();
324 }
325
326 // ------------------------------------------------------
327
328 SO_NODE_SOURCE(SoFrameLabel)
329
330 void SoFrameLabel::initClass()
331 {
332 SO_NODE_INIT_CLASS(SoFrameLabel, SoImage, "Image");
333 }
334
335 SoFrameLabel::SoFrameLabel()
336 {
337 SO_NODE_CONSTRUCTOR(SoFrameLabel);
338 SO_NODE_ADD_FIELD(string, (""));
339 SO_NODE_ADD_FIELD(textColor, (SbVec3f(1.0f, 1.0f, 1.0f)));
340 SO_NODE_ADD_FIELD(backgroundColor, (SbVec3f(0.0f, 0.333f, 1.0f)));
341 SO_NODE_ADD_FIELD(justification, (LEFT));
342 SO_NODE_ADD_FIELD(name, ("Helvetica"));
343 SO_NODE_ADD_FIELD(size, (12));
344 SO_NODE_ADD_FIELD(frame, (true));
345 SO_NODE_ADD_FIELD(border, (true));
346 SO_NODE_ADD_FIELD(backgroundUseBaseColor, (false));
347 SO_NODE_ADD_FIELD(textUseBaseColor, (false));
348 // SO_NODE_ADD_FIELD(image, (SbVec2s(0,0), 0, NULL));
349 }
350
351 void SoFrameLabel::setIcon(const QPixmap& pixMap)
352 {
353 iconPixmap = pixMap;
354 drawImage();
355 }
356
357 void SoFrameLabel::notify(SoNotList* list)
358 {
359 SoField* f = list->getLastField();
360 if (f == &this->string || f == &this->textColor || f == &this->backgroundColor
361 || f == &this->justification || f == &this->name || f == &this->size || f == &this->frame
362 || f == &this->border) {
363 drawImage();
364 }
365
366 inherited::notify(list);
367 }
368
369 void SoFrameLabel::drawImage()
370 {
371 const SbString* s = string.getValues(0);
372 int num = string.getNum();
373 if (num == 0) {
374 this->image = SoSFImage();
375 return;
376 }
377
378 QFont font(QString::fromLatin1(QByteArray(name.getValue())), size.getValue());
379 QFontMetrics fm(font);
380 int w = 0;
381 int h = fm.height() * num;
382 const SbColor& b = backgroundColor.getValue();
383 QColor backgroundBrush;
384 backgroundBrush.setRgbF(b[0], b[1], b[2]);
385 const SbColor& t = textColor.getValue();
386 QColor front;
387 front.setRgbF(t[0], t[1], t[2]);
388 const QPen borderPen(QColor(0, 0, 127), 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
389
390 QStringList lines;
391 for (int i = 0; i < num; i++) {
392 QString line = QString::fromUtf8(s[i].getString());
393 w = std::max<int>(w, QtTools::horizontalAdvance(fm, line));
394 lines << line;
395 }
396
397 int padding = 5;
398
399 bool drawIcon = false;
400 QImage iconImg;
401 int widthIcon = 0;
402 int heightIcon = 0;
403 if (!iconPixmap.isNull()) {
404 drawIcon = true;
405 iconImg = iconPixmap.toImage();
406 widthIcon = iconImg.width() + 2 * padding;
407 heightIcon = iconImg.height() + 2 * padding;
408 }
409
410 int widthText = w + 2 * padding;
411 int heightText = h + 2 * padding;
412 int widthTotal = widthText + widthIcon;
413 int heightTotal = heightText > heightIcon ? heightText : heightIcon;
414 int paddingTextV = (heightTotal - h) / 2;
415 int paddingIconV = (heightTotal - iconImg.height()) / 2;
416
417 QImage image(widthTotal, heightTotal, QImage::Format_ARGB32_Premultiplied);
418 image.fill(0x00000000);
419 QPainter painter(&image);
420 painter.setRenderHint(QPainter::Antialiasing);
421
422 SbBool drawFrame = frame.getValue();
423 SbBool drawBorder = border.getValue();
424 if (drawFrame || drawBorder) {
425 painter.setPen(drawBorder ? borderPen : QPen(Qt::transparent));
426 painter.setBrush(QBrush(drawFrame ? backgroundBrush : Qt::transparent));
427
428 QRectF rectangle(0.0, 0.0, widthTotal, heightTotal);
429 painter.drawRoundedRect(rectangle, 5, 5);
430 }
431
432 if (drawIcon) {
433 painter.drawImage(QPoint(padding, paddingIconV), iconImg);
434 }
435
436 painter.setPen(front);
437
438 Qt::Alignment align = Qt::AlignVCenter;
439 if (justification.getValue() == 0) {
440 align = Qt::AlignVCenter | Qt::AlignLeft;
441 }
442 else if (justification.getValue() == 1) {
443 align = Qt::AlignVCenter | Qt::AlignRight;
444 }
445 else {
446 align = Qt::AlignVCenter | Qt::AlignHCenter;
447 }
448 QString text = lines.join(QLatin1String("\n"));
449 painter.setFont(font);
450 painter.drawText(widthIcon + padding, paddingTextV, w, h, align, text);
451 painter.end();
452
453 SoSFImage sfimage;
454 Gui::BitmapFactory().convert(image, sfimage);
455 this->image = sfimage;
456 }
457
458 /**
459 * Renders the open edges only.
460 */
461 void SoFrameLabel::GLRender(SoGLRenderAction* action)
462 {
463
464 if (backgroundUseBaseColor.getValue()) {
465 SoState* state = action->getState();
466 const SbColor& diffuse = SoLazyElement::getDiffuse(state, 0);
467
468 if (diffuse != this->backgroundColor.getValue()) {
469 this->backgroundColor.setValue(diffuse);
470 }
471 }
472
473 if (textUseBaseColor.getValue()) {
474 SoState* state = action->getState();
475 const SbColor& diffuse = SoLazyElement::getDiffuse(state, 0);
476
477 if (diffuse != this->textColor.getValue()) {
478 this->textColor.setValue(diffuse);
479 }
480 }
481
482 inherited::GLRender(action);
483 }
484
485 // ------------------------------------------------------
486
487 SO_NODE_SOURCE(TranslateManip)
488
489 void TranslateManip::initClass()
490 {
491 SO_NODE_INIT_CLASS(TranslateManip, SoTransformManip, "TransformManip");
492 }
493
494 TranslateManip::TranslateManip()
495 {
496 SO_NODE_CONSTRUCTOR(TranslateManip);
497
498 auto myDrag = new SoTranslate2Dragger;
499 setDragger(myDrag);
500 }
501
502 TranslateManip::~TranslateManip() = default;