File size: 24,459 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
// SPDX-License-Identifier: LGPL-2.1-or-later

/***************************************************************************

 *   Copyright (c) 2023 David Friedli <david[at]friedli-be.ch>             *

 *                                                                         *

 *   This file is part of FreeCAD.                                         *

 *                                                                         *

 *   FreeCAD is free software: you can redistribute it and/or modify it    *

 *   under the terms of the GNU Lesser General Public License as           *

 *   published by the Free Software Foundation, either version 2.1 of the  *

 *   License, or (at your option) any later version.                       *

 *                                                                         *

 *   FreeCAD 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      *

 *   Lesser General Public License for more details.                       *

 *                                                                         *

 *   You should have received a copy of the GNU Lesser General Public      *

 *   License along with FreeCAD. If not, see                               *

 *   <https://www.gnu.org/licenses/>.                                      *

 *                                                                         *

 **************************************************************************/

#include "Gui/Application.h"
#include "Gui/MDIView.h"

#include <Inventor/actions/SoGetMatrixAction.h>
#include <Inventor/nodes/SoAnnotation.h>
#include <Inventor/nodes/SoBaseColor.h>
#include <Inventor/nodes/SoCoordinate3.h>
#include <Inventor/nodes/SoCamera.h>
#include <Inventor/nodes/SoDrawStyle.h>
#include <Inventor/nodes/SoIndexedLineSet.h>
#include <Inventor/nodes/SoMarkerSet.h>
#include <Inventor/nodes/SoPickStyle.h>
#include <Inventor/draggers/SoTranslate2Dragger.h>
#include <Inventor/engines/SoComposeMatrix.h>
#include <Inventor/engines/SoTransformVec3f.h>
#include <Inventor/engines/SoConcatenate.h>
#include <Inventor/SbViewportRegion.h>


#include <App/DocumentObject.h>
#include <Base/Console.h>
#include <Gui/BitmapFactory.h>
#include <Gui/Document.h>
#include <Gui/ViewParams.h>
#include <Gui/Inventor/MarkerBitmaps.h>
#include <Gui/View3DInventor.h>
#include <Gui/View3DInventorViewer.h>

#include <Mod/Measure/App/Preferences.h>
#include "ViewProviderMeasureBase.h"

using namespace MeasureGui;
using namespace Measure;


PROPERTY_SOURCE(MeasureGui::ViewProviderMeasureGroup, Gui::ViewProviderDocumentObjectGroup)

ViewProviderMeasureGroup::ViewProviderMeasureGroup()
{}

ViewProviderMeasureGroup::~ViewProviderMeasureGroup() = default;

QIcon ViewProviderMeasureGroup::getIcon() const

{
    return Gui::BitmapFactory().pixmap("Measurement-Group.svg");
}


// NOLINTBEGIN
PROPERTY_SOURCE(MeasureGui::ViewProviderMeasureBase, Gui::ViewProviderDocumentObject)
// NOLINTEND

ViewProviderMeasureBase::ViewProviderMeasureBase()
{
    static const char* agroup = "Appearance";
    // NOLINTBEGIN
    ADD_PROPERTY_TYPE(
        TextColor,
        (Preferences::defaultTextColor()),
        agroup,
        App::Prop_None,
        "Color for the measurement text"
    );
    ADD_PROPERTY_TYPE(
        TextBackgroundColor,
        (Preferences::defaultTextBackgroundColor()),
        agroup,
        App::Prop_None,
        "Color for the measurement text background"
    );
    ADD_PROPERTY_TYPE(
        LineColor,
        (Preferences::defaultLineColor()),
        agroup,
        App::Prop_None,
        "Color for the measurement lines"
    );
    ADD_PROPERTY_TYPE(
        FontSize,
        (Preferences::defaultFontSize()),
        agroup,
        App::Prop_None,
        "Size of measurement text"
    );
    // NOLINTEND

    pGlobalSeparator = new SoSeparator();
    pGlobalSeparator->ref();

    // Connect visibility of delta measurements to the ModeSwitch
    auto visibilitySwitch = new SoSwitch();
    getRoot()->insertChild(visibilitySwitch, 0);
    visibilitySwitch->addChild(pGlobalSeparator);
    visibilitySwitch->whichChild.connectFrom(&pcModeSwitch->whichChild);

    // setupAnnoSceneGraph() - sets up the annotation scene graph
    pLabel = new Gui::SoFrameLabel();
    pLabel->ref();
    pColor = new SoBaseColor();
    pColor->ref();
    pLabelTranslation = new SoTransform();
    pLabelTranslation->ref();

    auto ps = getSoPickStyle();

    // Dragger
    SoSeparator* dragSeparator = new SoSeparator();
    pDragger = new SoTranslate2Dragger();
    pDragger->ref();
    pDraggerOrientation = new SoTransform();
    pDraggerOrientation->ref();
    dragSeparator->addChild(pDraggerOrientation);
    dragSeparator->addChild(pDragger);

    // Transform drag location by dragger local orientation and connect to labelTranslation
    auto matrixEngine = new SoComposeMatrix();
    matrixEngine->rotation.connectFrom(&pDraggerOrientation->rotation);
    auto transformEngine = new SoTransformVec3f();
    transformEngine->vector.connectFrom(&pDragger->translation);
    transformEngine->matrix.connectFrom(&matrixEngine->matrix);
    pLabelTranslation->translation.connectFrom(&transformEngine->point);

    pTextSeparator = new SoSeparator();
    pTextSeparator->ref();
    pTextSeparator->addChild(dragSeparator);
    pTextSeparator->addChild(pLabelTranslation);
    pTextSeparator->addChild(pLabel);

    // Empty line separator which can be populated by inherited class
    pLineSeparator = new SoSeparator();
    pLineSeparator->ref();
    pLineSeparator->addChild(ps);
    pLineSeparator->addChild(getSoLineStylePrimary());
    pLineSeparator->addChild(pColor);

    // Secondary line separator
    pLineSeparatorSecondary = new SoSeparator();
    pLineSeparatorSecondary->ref();
    pLineSeparatorSecondary->addChild(ps);
    pLineSeparatorSecondary->addChild(getSoLineStyleSecondary());
    pLineSeparatorSecondary->addChild(pColor);

    pRootSeparator = new SoAnnotation();
    pRootSeparator->ref();
    pRootSeparator->addChild(pLineSeparator);
    pRootSeparator->addChild(pLineSeparatorSecondary);
    pRootSeparator->addChild(pTextSeparator);
    addDisplayMaskMode(pRootSeparator, "Base");

    pRootSeparator->touch();
    pTextSeparator->touch();
    pLineSeparator->touch();

    // Register dragger callback
    auto dragger = pDragger;

    dragger->addValueChangedCallback(draggerChangedCallback, this);


    // Use the label node as the transform handle
    SoSearchAction sa;
    sa.setInterest(SoSearchAction::FIRST);
    sa.setSearchingAll(true);
    sa.setNode(pLabel);
    sa.apply(pcRoot);
    SoPath* labelPath = sa.getPath();
    assert(labelPath);
    dragger->setPartAsPath("translator", labelPath);

    // Hide the dragger feedback during translation
    dragger->setPart("translatorActive", NULL);
    dragger->setPart("xAxisFeedback", NULL);
    dragger->setPart("yAxisFeedback", NULL);
    // end setupSceneGraph

    // these touches cause onChanged to run which then updates pLabel and pColor with the initial
    // values
    TextColor.touch();
    TextBackgroundColor.touch();
    FontSize.touch();
    LineColor.touch();
    fieldFontSize.setValue(FontSize.getValue());
}

ViewProviderMeasureBase::~ViewProviderMeasureBase()
{
    _mVisibilityChangedConnection.disconnect();
    pGlobalSeparator->unref();
    pLabel->unref();
    pColor->unref();
    pDragger->unref();
    pDraggerOrientation->unref();
    pLabelTranslation->unref();
    pTextSeparator->unref();
    pLineSeparator->unref();
    pRootSeparator->unref();
}

std::vector<std::string> ViewProviderMeasureBase::getDisplayModes() const

{
    // add modes
    std::vector<std::string> StrList;
    StrList.emplace_back("Base");
    return StrList;
}

void ViewProviderMeasureBase::setDisplayMode(const char* ModeName)

{
    if (strcmp(ModeName, "Base") == 0) {
        setDisplayMaskMode("Base");
    }
    ViewProviderDocumentObject::setDisplayMode(ModeName);
}


void ViewProviderMeasureBase::finishRestoring()

{
    if (Visibility.getValue() && isSubjectVisible()) {
        show();
    }
    ViewProviderDocumentObject::finishRestoring();
}


void ViewProviderMeasureBase::onChanged(const App::Property* prop)

{
    if (prop == &TextColor) {
        const Base::Color& color = TextColor.getValue();
        pLabel->textColor.setValue(color.r, color.g, color.b);
        updateIcon();
    }
    else if (prop == &TextBackgroundColor) {
        const Base::Color& color = TextBackgroundColor.getValue();
        pLabel->backgroundColor.setValue(color.r, color.g, color.b);
    }
    else if (prop == &LineColor) {
        const Base::Color& color = LineColor.getValue();
        pColor->rgb.setValue(color.r, color.g, color.b);
    }
    else if (prop == &FontSize) {
        pLabel->size = FontSize.getValue();
        fieldFontSize.setValue(FontSize.getValue());
    }

    ViewProviderDocumentObject::onChanged(prop);
}

void ViewProviderMeasureBase::draggerChangedCallback(void* data, SoDragger*)

{
    auto me = static_cast<ViewProviderMeasureBase*>(data);
    me->onLabelMoved();
}

void ViewProviderMeasureBase::setLabelValue(const Base::Quantity& value)

{
    pLabel->string.setValue(value.getUserString().c_str());
}

void ViewProviderMeasureBase::setLabelValue(const QString& value)

{
    auto lines = value.split(QStringLiteral("\n"));

    int i = 0;
    for (auto& it : lines) {
        pLabel->string.set1Value(i, it.toUtf8().constData());
        i++;
    }
}

void ViewProviderMeasureBase::setLabelTranslation(const SbVec3f& position)

{
    // Set the dragger translation to keep it in sync with pLabelTranslation
    pDragger->translation.setValue(position);
}


SoPickStyle* ViewProviderMeasureBase::getSoPickStyle()

{
    auto ps = new SoPickStyle();
    ps->style = SoPickStyle::UNPICKABLE;
    return ps;
}

SoDrawStyle* ViewProviderMeasureBase::getSoLineStylePrimary()

{
    auto style = new SoDrawStyle();
    style->lineWidth = 2.0f;
    return style;
}

SoDrawStyle* ViewProviderMeasureBase::getSoLineStyleSecondary()

{
    auto style = new SoDrawStyle();
    style->lineWidth = 1.0f;
    return style;
}

SoSeparator* ViewProviderMeasureBase::getSoSeparatorText()

{
    return pTextSeparator;
}


void ViewProviderMeasureBase::positionAnno(const Measure::MeasureBase* measureObject)

{
    (void)measureObject;
}


void ViewProviderMeasureBase::updateIcon()

{
    // This assumes the icons main color is black

    Gui::ColorMap colorMap {
        {0x000000, TextColor.getValue().getPackedRGB() >> 8},
    };
    pLabel->setIcon(Gui::BitmapFactory().pixmapFromSvg(sPixmap, QSize(20, 20), colorMap));
}


void ViewProviderMeasureBase::attach(App::DocumentObject* pcObj)

{
    ViewProviderDocumentObject::attach(pcObj);
    updateIcon();
}


//! handle changes to the feature's properties
void ViewProviderMeasureBase::updateData(const App::Property* prop)

{
    bool doUpdate = false;

    auto obj = getMeasureObject();
    if (!obj) {
        return;
    }

    if (strcmp(prop->getName(), "Label") == 0) {
        doUpdate = true;
    }

    // Check if one of the input properties has been changed
    auto inputProps = obj->getInputProps();
    if (std::ranges::find(inputProps, std::string(prop->getName())) != inputProps.end()) {
        doUpdate = true;

        // Add connections to be notified when the measured objects are changed
        connectToSubject(obj->getSubject());
    }

    // Check if the result prop has been changed
    auto resultProp = obj->getResultProp();
    if (resultProp && prop == resultProp) {
        doUpdate = true;
    }

    if (doUpdate) {
        redrawAnnotation();

        // Update label
        std::string userLabel(obj->Label.getValue());
        std::string name = userLabel.substr(0, userLabel.find(":"));
        obj->Label.setValue((name + ": ") + obj->getResultString().toStdString());
    }

    ViewProviderDocumentObject::updateData(prop);
}


// TODO: should this be pure virtual?
void ViewProviderMeasureBase::redrawAnnotation()

{
    // Base::Console().message("VPMB::redrawAnnotation()\n");
}

//! connect to the subject to receive visibility updates
void ViewProviderMeasureBase::connectToSubject(App::DocumentObject* subject)

{
    if (!subject) {
        return;
    }

    // disconnect any existing connection
    if (_mVisibilityChangedConnection.connected()) {
        _mVisibilityChangedConnection.disconnect();
    }

    // NOLINTBEGIN
    auto bndVisibility = std::bind(
        &ViewProviderMeasureBase::onSubjectVisibilityChanged,
        this,
        std::placeholders::_1,
        std::placeholders::_2
    );
    // NOLINTEND
    _mVisibilityChangedConnection = subject->signalChanged.connect(bndVisibility);
}

//! connect to the subject to receive visibility updates
void ViewProviderMeasureBase::connectToSubject(std::vector<App::DocumentObject*> subject)

{
    if (subject.empty()) {
        return;
    }

    // TODO: should we connect to all the subject objects when there is >1?
    auto proxy = subject.front();
    connectToSubject(proxy);
}


//! retrieve the feature
Measure::MeasureBase* ViewProviderMeasureBase::getMeasureObject()

{
    // Note: Cast to MeasurePropertyBase once we use it to provide the needed values e.g.
    // basePosition textPosition etc.
    auto feature = dynamic_cast<Measure::MeasureBase*>(pcObject);
    if (!feature) {
        throw Base::RuntimeError("Feature not found for ViewProviderMeasureBase");
    }
    return feature;
}


//! calculate a good direction from the elements being measured to the annotation text based on the
//! layout of the elements and relationship with the cardinal axes and the view direction.
//! elementDirection is expected to be a normalized vector. an example of an elementDirection would
//! be the vector from the start of a line to the end.
Base::Vector3d ViewProviderMeasureBase::getTextDirection(Base::Vector3d elementDirection, double tolerance)

{
    // TODO: this can fail if the active view is not a 3d view (spreadsheet, techdraw page) and
    // something causes a measure to try to update we need to search through the mdi views for a 3d
    // view and take the direction from it (or decide that if the active view is not 3d, assume we
    // are looking from the front).
    Base::Vector3d viewDirection;
    Base::Vector3d upDirection;

    Gui::View3DInventor* view = nullptr;
    try {
        view = dynamic_cast<Gui::View3DInventor*>(this->getActiveView());
    }
    catch (const Base::RuntimeError&) {
        Base::Console().log("ViewProviderMeasureBase::getTextDirection: Could not get active view\n");
    }

    if (view) {
        Gui::View3DInventorViewer* viewer = view->getViewer();
        viewDirection = toVector3d(viewer->getViewDirection()).Normalize();
        upDirection = toVector3d(viewer->getUpDirection()).Normalize();
        // Measure doesn't work with this kind of active view.  Might be dependency graph, might be
        // TechDraw, or ????
        // throw Base::RuntimeError("Measure doesn't work with this kind of active view.");
    }
    else {
        viewDirection = Base::Vector3d(0.0, 1.0, 0.0);
        upDirection = Base::Vector3d(0.0, 0.0, 1.0);
    }

    Base::Vector3d textDirection = elementDirection.Cross(viewDirection);
    if (textDirection.Length() < tolerance) {
        // either elementDirection and viewDirection are parallel or one of them is null.
        textDirection = elementDirection.Cross(upDirection);
    }

    return textDirection.Normalize();
}


//! true if the subject of this measurement is visible.  For Measures that have multiple object
//! subject, all of the subjects must be visible.
bool ViewProviderMeasureBase::isSubjectVisible()

{
    Gui::Document* guiDoc = nullptr;
    try {
        guiDoc = this->getDocument();
    }
    catch (const Base::RuntimeError&) {
        Base::Console().log("ViewProviderMeasureBase::isSubjectVisible: Could not get document\n");
        return false;
    }

    // we need these things to proceed
    if (!getMeasureObject() || !guiDoc) {
        return false;
    }

    // Show the measurement if it doesn't track any subjects
    if (getMeasureObject()->getSubject().empty()) {
        return true;
    }

    for (auto& obj : getMeasureObject()->getSubject()) {
        Gui::ViewProvider* vp = guiDoc->getViewProvider(obj);
        if (!vp || !vp->isVisible()) {
            return false;
        }
    }

    // all of the subject objects are visible
    return true;
}


//! gets called when the subject object issues a signalChanged (ie a property change).  We are only
//! interested in the subject's Visibility property
void ViewProviderMeasureBase::onSubjectVisibilityChanged(

    const App::DocumentObject& docObj,

    const App::Property& prop

)

{
    if (docObj.isRemoving()) {
        return;
    }


    std::string propName = prop.getName();
    if (propName == "Visibility") {
        if (!docObj.Visibility.getValue()) {
            // show ourselves only if subject is visible
            setVisible(false);
        }
        else {
            // here, we don't know if we should be visible or not, so we have to check the whole
            // subject
            setVisible(isSubjectVisible() && Visibility.getValue());
        }
    }
}


float ViewProviderMeasureBase::getViewScale()

{
    float scale = 1.0;

    Gui::View3DInventor* view = dynamic_cast<Gui::View3DInventor*>(this->getActiveView());
    if (!view) {
        Base::Console().log("ViewProviderMeasureBase::getViewScale: Could not get active view\n");
        return scale;
    }
    Gui::View3DInventorViewer* viewer = view->getViewer();

    SoCamera* const camera = viewer->getSoRenderManager()->getCamera();
    if (!camera) {
        return false;
    }

    SbViewVolume volume(camera->getViewVolume());
    SbVec3f center(volume.getSightPoint(camera->focalDistance.getValue()));
    scale = volume.getWorldToScreenScale(center, 1.0);
    return scale;
}


// NOLINTBEGIN
PROPERTY_SOURCE(MeasureGui::ViewProviderMeasure, MeasureGui::ViewProviderMeasureBase)
// NOLINTEND

//! the general purpose view provider.  handles area, length, etc - any measure without a
//! specialized VP
ViewProviderMeasure::ViewProviderMeasure()
{
    sPixmap = "umf-measurement";


    // setupSceneGraph for leader?
    const size_t lineCount(3);

    // indexes used to create the edges
    // this makes a line from verts[0] to verts[1]
    static const int32_t lines[lineCount] = {0, 1, -1};

    pCoords = new SoCoordinate3();
    pCoords->ref();

    // Combine coordinates from baseTranslation and labelTranslation
    auto engineCat = new SoConcatenate(SoMFVec3f::getClassTypeId());
    auto origin = new SoSFVec3f();
    origin->setValue(0, 0, 0);
    engineCat->input[0]->connectFrom(origin);
    engineCat->input[1]->connectFrom(&pLabelTranslation->translation);
    pCoords->point.setNum(engineCat->output->getNumConnections());
    pCoords->point.connectFrom(engineCat->output);

    pLines = new SoIndexedLineSet();
    pLines->ref();
    pLines->coordIndex.setNum(lineCount);
    pLines->coordIndex.setValues(0, lineCount, lines);

    auto lineSep = pLineSeparator;
    lineSep->addChild(pCoords);
    lineSep->addChild(pLines);
    auto points = new SoMarkerSet();
    points->markerIndex = Gui::Inventor::MarkerBitmaps::getMarkerIndex(
        "CROSS",
        Gui::ViewParams::instance()->getMarkerSize()
    );
    points->numPoints = 1;
    lineSep->addChild(points);

    // Connect dragger local orientation to view orientation
    Gui::View3DInventor* view = nullptr;
    try {
        view = dynamic_cast<Gui::View3DInventor*>(this->getActiveView());
    }
    catch (const Base::RuntimeError&) {
        Base::Console().log("ViewProviderMeasure::ViewProviderMeasure: Could not get active view\n");
    }

    if (view) {
        Gui::View3DInventorViewer* viewer = view->getViewer();
        auto renderManager = viewer->getSoRenderManager();
        auto cam = renderManager->getCamera();
        pDraggerOrientation->rotation.connectFrom(&cam->orientation);
    }
}

ViewProviderMeasure::~ViewProviderMeasure()
{
    pCoords->unref();
    pLines->unref();
}

void ViewProviderMeasure::positionAnno(const Measure::MeasureBase* measureObject)

{
    (void)measureObject;

    // Initialize the text position
    Base::Vector3d textPos = getTextPosition();
    auto srcVec = SbVec3f(textPos.x, textPos.y, textPos.z);

    // Translate the position by the local dragger matrix (pDraggerOrientation)
    Gui::View3DInventor* view = nullptr;
    try {
        view = dynamic_cast<Gui::View3DInventor*>(this->getActiveView());
    }
    catch (const Base::RuntimeError&) {
        Base::Console().log("ViewProviderMeasure::positionAnno: Could not get active view\n");
    }

    if (!view) {
        return;
    }

    Gui::View3DInventorViewer* viewer = view->getViewer();
    auto gma = SoGetMatrixAction(viewer->getSoRenderManager()->getViewportRegion());
    gma.apply(pDraggerOrientation);
    auto mat = gma.getMatrix();
    SbVec3f destVec(0, 0, 0);
    mat.multVecMatrix(srcVec, destVec);

    setLabelTranslation(destVec);
    updateView();
}

void ViewProviderMeasure::onChanged(const App::Property* prop)

{
    if (pcObject == nullptr) {
        return;
    }

    ViewProviderMeasureBase::onChanged(prop);
}


//! repaint the annotation
void ViewProviderMeasure::redrawAnnotation()

{
    // point on element
    Base::Vector3d basePos = getBasePosition();
    pcTransform->translation.setValue(SbVec3f(basePos.x, basePos.y, basePos.z));

    setLabelValue(getMeasureObject()->getResultString());

    ViewProviderMeasureBase::redrawAnnotation();
    ViewProviderDocumentObject::updateView();
}


Base::Vector3d ViewProviderMeasure::getBasePosition()

{
    auto measureObject = getMeasureObject();
    Base::Placement placement = measureObject->getPlacement();
    return placement.getPosition();
}

Base::Vector3d ViewProviderMeasure::getTextPosition()

{
    // Return the initial position relative to the base position
    auto basePoint = getBasePosition();

    Gui::View3DInventor* view = dynamic_cast<Gui::View3DInventor*>(this->getActiveView());
    if (!view) {
        Base::Console().log("ViewProviderMeasureBase::getTextPosition: Could not get active view\n");
        return Base::Vector3d();
    }

    Gui::View3DInventorViewer* viewer = view->getViewer();

    // Convert to screenspace, offset and convert back to world space
    SbVec2s screenPos = viewer->getPointOnViewport(SbVec3f(basePoint.x, basePoint.y, basePoint.z));
    SbVec3f vec = viewer->getPointOnFocalPlane(screenPos + SbVec2s(30.0, 30.0));
    Base::Vector3d textPos(vec[0], vec[1], vec[2]);

    return textPos - basePoint;
}

//! called by the system when it is time to display this measure
void ViewProviderMeasureBase::show()

{
    if (isSubjectVisible()) {
        // only show the annotation if the subject is visible.
        // this avoids disconnected annotations floating in space.
        ViewProviderDocumentObject::show();
    }
}


PROPERTY_SOURCE(MeasureGui::ViewProviderMeasureArea, MeasureGui::ViewProviderMeasure)
PROPERTY_SOURCE(MeasureGui::ViewProviderMeasureLength, MeasureGui::ViewProviderMeasure)
PROPERTY_SOURCE(MeasureGui::ViewProviderMeasurePosition, MeasureGui::ViewProviderMeasure)
PROPERTY_SOURCE(MeasureGui::ViewProviderMeasureRadius, MeasureGui::ViewProviderMeasure)
PROPERTY_SOURCE(MeasureGui::ViewProviderMeasureDiameter, MeasureGui::ViewProviderMeasure)
PROPERTY_SOURCE(MeasureGui::ViewProviderMeasureCOM, MeasureGui::ViewProviderMeasure)