Debian Codegraph @

src/Gui/ViewProviderAnnotation.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2008 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
24 #include <QMenu>
25 #include <QFont>
26 #include <QFontMetrics>
27 #include <QImage>
28 #include <QPainter>
29 #include <Inventor/SbLine.h>
30 #include <Inventor/SbPlane.h>
31 #include <Inventor/actions/SoSearchAction.h>
32 #include <Inventor/events/SoEvent.h>
33 #include <Inventor/nodes/SoAnnotation.h>
34 #include <Inventor/nodes/SoAsciiText.h>
35 #include <Inventor/nodes/SoBaseColor.h>
36 #include <Inventor/nodes/SoCoordinate3.h>
37 #include <Inventor/nodes/SoDrawStyle.h>
38 #include <Inventor/nodes/SoFont.h>
39 #include <Inventor/nodes/SoImage.h>
40 #include <Inventor/nodes/SoLineSet.h>
41 #include <Inventor/nodes/SoPickStyle.h>
42 #include <Inventor/nodes/SoPointSet.h>
43 #include <Inventor/nodes/SoRotationXYZ.h>
44 #include <Inventor/nodes/SoText2.h>
45 #include <Inventor/nodes/SoTranslation.h>
46
47 #include <Inventor/draggers/SoTranslate2Dragger.h>
48
49 #include <App/Annotation.h>
50 #include <App/Document.h>
51 #include <App/PropertyStandard.h>
52 #include <Base/Parameter.h>
53
54 #include "ViewProviderAnnotation.h"
55 #include "Application.h"
56 #include "BitmapFactory.h"
57 #include "Document.h"
58 #include "SoFCSelection.h"
59 #include "Tools.h"
60 #include "Utilities.h"
61 #include "ViewParams.h"
62 #include "Window.h"
63
64
65 using namespace Gui;
66
67 const char* ViewProviderAnnotation::JustificationEnums[] = {"Left", "Right", "Center", nullptr};
68 const char* ViewProviderAnnotation::RotationAxisEnums[] = {"X", "Y", "Z", nullptr};
69
70 PROPERTY_SOURCE(Gui::ViewProviderAnnotation, Gui::ViewProviderDocumentObject)
71
72
73 ViewProviderAnnotation::ViewProviderAnnotation()
74 {
75 ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath(
76 "User parameter:BaseApp/Preferences/View"
77 );
78 unsigned long col = hGrp->GetUnsigned("AnnotationTextColor", 4294967295UL); // light grey
79 float r, g, b;
80 r = ((col >> 24) & 0xff) / 255.0;
81 g = ((col >> 16) & 0xff) / 255.0;
82 b = ((col >> 8) & 0xff) / 255.0;
83 ADD_PROPERTY(TextColor, (r, g, b));
84 ADD_PROPERTY(Justification, ((long)0));
85 Justification.setEnums(JustificationEnums);
86 ADD_PROPERTY(FontSize, (12));
87 ADD_PROPERTY(FontName, ("Arial"));
88 ADD_PROPERTY(LineSpacing, (1.0));
89 ADD_PROPERTY(Rotation, (0));
90 ADD_PROPERTY(RotationAxis, ((long)2));
91 RotationAxis.setEnums(RotationAxisEnums);
92
93 pFont = new SoFont();
94 pFont->ref();
95 pLabel = new SoText2();
96 pLabel->ref();
97 pLabel3d = new SoAsciiText();
98 pLabel3d->ref();
99 pColor = new SoBaseColor();
100 pColor->ref();
101 pTranslation = new SoTranslation();
102 pTranslation->ref();
103 pRotationXYZ = new SoRotationXYZ();
104 pRotationXYZ->ref();
105
106 RotationAxis.touch();
107 TextColor.touch();
108 FontSize.touch();
109 FontName.touch();
110
111 sPixmap = "Tree_Annotation";
112 }
113
114 ViewProviderAnnotation::~ViewProviderAnnotation()
115 {
116 pFont->unref();
117 pLabel->unref();
118 pLabel3d->unref();
119 pColor->unref();
120 pTranslation->unref();
121 pRotationXYZ->unref();
122 }
123
124 void ViewProviderAnnotation::onChanged(const App::Property* prop)
125 {
126 if (prop == &TextColor) {
127 const Base::Color& c = TextColor.getValue();
128 pColor->rgb.setValue(c.r, c.g, c.b);
129 }
130 else if (prop == &Justification) {
131 if (Justification.getValue() == 0) {
132 pLabel->justification = SoText2::LEFT;
133 pLabel3d->justification = SoAsciiText::LEFT;
134 }
135 else if (Justification.getValue() == 1) {
136 pLabel->justification = SoText2::RIGHT;
137 pLabel3d->justification = SoAsciiText::RIGHT;
138 }
139 else if (Justification.getValue() == 2) {
140 pLabel->justification = SoText2::CENTER;
141 pLabel3d->justification = SoAsciiText::CENTER;
142 }
143 }
144 else if (prop == &FontSize) {
145 pFont->size = FontSize.getValue();
146 }
147 else if (prop == &FontName) {
148 pFont->name = FontName.getValue();
149 }
150 else if (prop == &LineSpacing) {
151 pLabel->spacing = LineSpacing.getValue();
152 pLabel3d->spacing = LineSpacing.getValue();
153 }
154 else if (prop == &RotationAxis) {
155 if (RotationAxis.getValue() == 0) {
156 pRotationXYZ->axis = SoRotationXYZ::X;
157 }
158 else if (RotationAxis.getValue() == 1) {
159 pRotationXYZ->axis = SoRotationXYZ::Y;
160 }
161 else if (RotationAxis.getValue() == 2) {
162 pRotationXYZ->axis = SoRotationXYZ::Z;
163 }
164 }
165 else if (prop == &Rotation) {
166 pRotationXYZ->angle = (Rotation.getValue() / 360) * (2 * std::numbers::pi);
167 }
168 else {
169 ViewProviderDocumentObject::onChanged(prop);
170 }
171 }
172
173 std::vector<std::string> ViewProviderAnnotation::getDisplayModes() const
174 {
175 // add modes
176 std::vector<std::string> StrList;
177 StrList.emplace_back("Screen");
178 StrList.emplace_back("World");
179 return StrList;
180 }
181
182 void ViewProviderAnnotation::setDisplayMode(const char* ModeName)
183 {
184 if (strcmp(ModeName, "Screen") == 0) {
185 setDisplayMaskMode("Screen");
186 }
187 else if (strcmp(ModeName, "World") == 0) {
188 setDisplayMaskMode("World");
189 }
190
191 ViewProviderDocumentObject::setDisplayMode(ModeName);
192 }
193
194 void ViewProviderAnnotation::attach(App::DocumentObject* f)
195 {
196 ViewProviderDocumentObject::attach(f);
197
198 auto anno = new SoAnnotation();
199 auto anno3d = new SoAnnotation();
200
201 auto textsep = new SoFCSelection();
202
203 // set selection/highlight colors
204 float transparency;
205 ParameterGrp::handle hGrp = Gui::WindowParameter::getDefaultParameter()->GetGroup("View");
206 SbColor highlightColor = textsep->colorHighlight.getValue();
207 auto highlight = (unsigned long)(highlightColor.getPackedValue());
208 highlight = hGrp->GetUnsigned("HighlightColor", highlight);
209 highlightColor.setPackedValue((uint32_t)highlight, transparency);
210 textsep->colorHighlight.setValue(highlightColor);
211 // Do the same with the selection color
212 SbColor selectionColor = textsep->colorSelection.getValue();
213 auto selection = (unsigned long)(selectionColor.getPackedValue());
214 selection = hGrp->GetUnsigned("SelectionColor", selection);
215 selectionColor.setPackedValue((uint32_t)selection, transparency);
216 textsep->colorSelection.setValue(selectionColor);
217
218 textsep->objectName = pcObject->getNameInDocument();
219 textsep->documentName = pcObject->getDocument()->getName();
220 textsep->subElementName = "Main";
221 textsep->addChild(pTranslation);
222 textsep->addChild(pRotationXYZ);
223 textsep->addChild(pColor);
224 textsep->addChild(pFont); // causes problems
225 textsep->addChild(pLabel);
226
227 auto textsep3d = new SoFCSelection();
228
229 // set sel/highlight color here too
230 textsep3d->colorHighlight.setValue(highlightColor);
231 textsep3d->colorSelection.setValue(selectionColor);
232
233 textsep3d->objectName = pcObject->getNameInDocument();
234 textsep3d->documentName = pcObject->getDocument()->getName();
235 textsep3d->subElementName = "Main";
236 textsep3d->addChild(pTranslation);
237 textsep3d->addChild(pRotationXYZ);
238 textsep3d->addChild(pColor);
239 textsep3d->addChild(pFont);
240 textsep3d->addChild(pLabel3d);
241
242 anno->addChild(textsep);
243 anno3d->addChild(textsep3d);
244
245 addDisplayMaskMode(anno, "Screen");
246 addDisplayMaskMode(anno3d, "World");
247 }
248
249 void ViewProviderAnnotation::updateData(const App::Property* prop)
250 {
251 if (prop->is<App::PropertyStringList>() && strcmp(prop->getName(), "LabelText") == 0) {
252 const std::vector<std::string> lines
253 = static_cast<const App::PropertyStringList*>(prop)->getValues();
254 int index = 0;
255 pLabel->string.setNum((int)lines.size());
256 pLabel3d->string.setNum((int)lines.size());
257 for (const auto& line : lines) {
258 const char* cs = line.c_str();
259 if (line.empty()) {
260 cs = " "; // empty lines make coin crash, we use a space instead
261 }
262 pLabel->string.set1Value(index, SbString(cs));
263 pLabel3d->string.set1Value(index, SbString(cs));
264 index++;
265 }
266 }
267 else if (prop->is<App::PropertyVector>() && strcmp(prop->getName(), "Position") == 0) {
268 Base::Vector3d v = static_cast<const App::PropertyVector*>(prop)->getValue();
269 pTranslation->translation.setValue(v.x, v.y, v.z);
270 }
271
272 ViewProviderDocumentObject::updateData(prop);
273 }
274
275 // ----------------------------------------------------------------------------
276
277 const char* ViewProviderAnnotationLabel::JustificationEnums[] = {"Left", "Right", "Center", nullptr};
278
279 PROPERTY_SOURCE(Gui::ViewProviderAnnotationLabel, Gui::ViewProviderDocumentObject)
280
281
282 ViewProviderAnnotationLabel::ViewProviderAnnotationLabel()
283 {
284 ADD_PROPERTY(TextColor, (1.0f, 1.0f, 1.0f));
285 ADD_PROPERTY(BackgroundColor, (0.0f, 0.333f, 1.0f));
286 ADD_PROPERTY(Justification, ((long)0));
287 Justification.setEnums(JustificationEnums);
288 QFont fn;
289 ADD_PROPERTY(FontSize, (fn.pointSize()));
290 ADD_PROPERTY(FontName, ((const char*)fn.family().toLatin1()));
291 ADD_PROPERTY(Frame, (true));
292
293 pColor = new SoBaseColor();
294 pColor->ref();
295 pBaseTranslation = new SoTranslation();
296 pBaseTranslation->ref();
297 pTextTranslation = new TranslateManip();
298 pTextTranslation->ref();
299 pCoords = new SoCoordinate3();
300 pCoords->ref();
301 pImage = new SoImage();
302 pImage->ref();
303
304 BackgroundColor.touch();
305
306 sPixmap = "Tree_Annotation";
307 }
308
309 ViewProviderAnnotationLabel::~ViewProviderAnnotationLabel()
310 {
311 pColor->unref();
312 pBaseTranslation->unref();
313 pTextTranslation->unref();
314 pCoords->unref();
315 pImage->unref();
316 }
317
318 void ViewProviderAnnotationLabel::onChanged(const App::Property* prop)
319 {
320 if (prop == &BackgroundColor) {
321 const Base::Color& c = BackgroundColor.getValue();
322 pColor->rgb.setValue(c.r, c.g, c.b);
323 }
324 if (prop == &TextColor || prop == &BackgroundColor || prop == &Justification
325 || prop == &FontSize || prop == &FontName || prop == &Frame) {
326 if (getObject()) {
327 App::Property* label = getObject()->getPropertyByName("LabelText");
328 if (label && label->is<App::PropertyStringList>()) {
329 drawImage(static_cast<App::PropertyStringList*>(label)->getValues());
330 }
331 }
332 }
333 else {
334 ViewProviderDocumentObject::onChanged(prop);
335 }
336 }
337
338 std::vector<std::string> ViewProviderAnnotationLabel::getDisplayModes() const
339 {
340 // add modes
341 std::vector<std::string> StrList;
342 StrList.emplace_back("Line");
343 StrList.emplace_back("Object");
344 return StrList;
345 }
346
347 void ViewProviderAnnotationLabel::setDisplayMode(const char* ModeName)
348 {
349 if (strcmp(ModeName, "Line") == 0) {
350 setDisplayMaskMode("Line");
351 }
352 else if (strcmp(ModeName, "Object") == 0) {
353 setDisplayMaskMode("Object");
354 }
355
356 ViewProviderDocumentObject::setDisplayMode(ModeName);
357 }
358
359 void ViewProviderAnnotationLabel::attach(App::DocumentObject* f)
360 {
361 ViewProviderDocumentObject::attach(f);
362
363 // plain image
364 SoSeparator* textsep = new SoAnnotation();
365 auto pickStyleObj = new SoPickStyle();
366 pickStyleObj->style = SoPickStyle::SHAPE_ON_TOP;
367 textsep->addChild(pickStyleObj);
368 textsep->addChild(pBaseTranslation);
369 textsep->addChild(pImage);
370
371 // image with line
372 SoSeparator* linesep = new SoAnnotation();
373 auto pickStyleLine = new SoPickStyle();
374 pickStyleLine->style = SoPickStyle::SHAPE_ON_TOP;
375 linesep->addChild(pickStyleLine);
376 linesep->addChild(pBaseTranslation);
377 linesep->addChild(pColor);
378 linesep->addChild(pCoords);
379 linesep->addChild(new SoLineSet());
380 auto ds = new SoDrawStyle();
381 ds->pointSize.setValue(3.0f);
382 linesep->addChild(ds);
383 linesep->addChild(new SoPointSet());
384 linesep->addChild(pTextTranslation);
385 linesep->addChild(pImage);
386
387 addDisplayMaskMode(linesep, "Line");
388 addDisplayMaskMode(textsep, "Object");
389
390 // Use the image node as the transform handle
391 SoSearchAction sa;
392 sa.setInterest(SoSearchAction::FIRST);
393 sa.setSearchingAll(true);
394 sa.setNode(this->pImage);
395 sa.apply(pcRoot);
396 SoPath* imagePath = sa.getPath();
397 if (imagePath) {
398 SoDragger* dragger = pTextTranslation->getDragger();
399 dragger->addStartCallback(dragStartCallback, this);
400 dragger->addFinishCallback(dragFinishCallback, this);
401 dragger->addMotionCallback(dragMotionCallback, this);
402
403 dragger->setPartAsPath("translator", imagePath);
404
405 // Hide the dragger feedback during translation
406 dragger->setPart("translatorActive", NULL);
407 dragger->setPart("xAxisFeedback", NULL);
408 dragger->setPart("yAxisFeedback", NULL);
409 }
410 }
411
412 void ViewProviderAnnotationLabel::updateData(const App::Property* prop)
413 {
414 if (prop->is<App::PropertyStringList>() && strcmp(prop->getName(), "LabelText") == 0) {
415 drawImage(static_cast<const App::PropertyStringList*>(prop)->getValues());
416 }
417 else if (prop->is<App::PropertyVector>() && strcmp(prop->getName(), "BasePosition") == 0) {
418 Base::Vector3d v = static_cast<const App::PropertyVector*>(prop)->getValue();
419 pBaseTranslation->translation.setValue(v.x, v.y, v.z);
420 }
421 else if (prop->is<App::PropertyVector>() && strcmp(prop->getName(), "TextPosition") == 0) {
422 Base::Vector3d v = static_cast<const App::PropertyVector*>(prop)->getValue();
423 pCoords->point.set1Value(1, SbVec3f(v.x, v.y, v.z));
424 pTextTranslation->translation.setValue(v.x, v.y, v.z);
425 }
426
427 ViewProviderDocumentObject::updateData(prop);
428 }
429
430
431 void ViewProviderAnnotationLabel::dragStartCallback(void*, SoDragger*)
432 {
433 // This is called when a manipulator is about to manipulating
434 Gui::Application::Instance->activeDocument()->openCommand(
435 QT_TRANSLATE_NOOP("Command", "Transform")
436 );
437 }
438
439 void ViewProviderAnnotationLabel::dragFinishCallback(void*, SoDragger*)
440 {
441 // This is called when a manipulator has done manipulating
442 Gui::Application::Instance->activeDocument()->commitCommand();
443 }
444
445 void ViewProviderAnnotationLabel::dragMotionCallback(void* data, SoDragger* drag)
446 {
447 auto that = static_cast<ViewProviderAnnotationLabel*>(data);
448 if (auto* obj = that->getObject<App::AnnotationLabel>()) {
449 Base::Vector3d basepos = obj->BasePosition.getValue();
450 Base::Vector3d textpos = obj->TextPosition.getValue();
451
452 auto globalText = Base::convertTo<SbVec3f>(basepos + textpos);
453 SbVec3f pnt = drag->getWorldStartingPoint();
454 // difference between the label's origin and the picked point
455 SbVec3f move = pnt - globalText;
456
457 SbViewVolume vv = drag->getViewVolume();
458 SbVec3f normal = vv.getProjectionDirection();
459
460 SbPlane plane(normal, pnt);
461
462 const SoEvent* ev = drag->getEvent();
463 const SbViewportRegion& vpr = drag->getViewportRegion();
464
465 SbLine line;
466 vv.projectPointToLine(ev->getNormalizedPosition(vpr), line);
467
468 SbVec3f intersect;
469 plane.intersect(line, intersect);
470 drag->setStartingPoint(intersect);
471
472 auto text = Base::convertTo<Base::Vector3d>(intersect - move);
473 text = text - basepos;
474 obj->TextPosition.setValue(text);
475 }
476 }
477
478 void ViewProviderAnnotationLabel::drawImage(const std::vector<std::string>& s)
479 {
480 if (s.empty()) {
481 pImage->image = SoSFImage();
482 this->hide();
483 return;
484 }
485
486 QFont font(QString::fromLatin1(this->FontName.getValue()), (int)this->FontSize.getValue());
487 QFontMetrics fm(font);
488 int w = 0;
489 int h = fm.height() * s.size();
490 const Base::Color& b = this->BackgroundColor.getValue();
491 QColor brush;
492 brush.setRgbF(b.r, b.g, b.b);
493 const Base::Color& t = this->TextColor.getValue();
494 QColor front;
495 front.setRgbF(t.r, t.g, t.b);
496
497 QStringList lines;
498 for (const auto& it : s) {
499 QString line = QString::fromUtf8(it.c_str());
500 w = std::max<int>(w, QtTools::horizontalAdvance(fm, line));
501 lines << line;
502 }
503
504 QImage image(w + 10, h + 10, QImage::Format_ARGB32_Premultiplied);
505 image.fill(0x00000000);
506 QPainter painter(&image);
507 painter.setRenderHint(QPainter::Antialiasing);
508
509 bool drawFrame = this->Frame.getValue();
510 if (drawFrame) {
511 painter.setPen(QPen(QColor(0, 0, 127), 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
512 painter.setBrush(QBrush(brush, Qt::SolidPattern));
513 QRectF rectangle(0.0, 0.0, w + 10, h + 10);
514 painter.drawRoundedRect(rectangle, 5, 5);
515 }
516
517 painter.setPen(front);
518
519 Qt::Alignment align = Qt::AlignVCenter;
520 if (Justification.getValue() == 0) {
521 align = Qt::AlignVCenter | Qt::AlignLeft;
522 }
523 else if (Justification.getValue() == 1) {
524 align = Qt::AlignVCenter | Qt::AlignRight;
525 }
526 else {
527 align = Qt::AlignVCenter | Qt::AlignHCenter;
528 }
529 QString text = lines.join(QLatin1String("\n"));
530 painter.setFont(font);
531 painter.drawText(5, 5, w, h, align, text);
532 painter.end();
533
534 SoSFImage sfimage;
535 Gui::BitmapFactory().convert(image, sfimage);
536 pImage->image = sfimage;
537 }