/
QGIViewDimension.cpp
1559 lines (1329 loc) · 58.7 KB
/
QGIViewDimension.cpp
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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* Copyright (c) 2013 Luke Parry <l.parry@warwick.ac.uk> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library 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 Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
#include <BRep_Builder.hxx>
#include <TopoDS_Compound.hxx>
# include <TopoDS_Shape.hxx>
# include <TopoDS_Edge.hxx>
# include <TopoDS.hxx>
# include <BRepAdaptor_Curve.hxx>
# include <Precision.hxx>
# include <QGraphicsScene>
# include <QGraphicsSceneMouseEvent>
# include <QPainter>
# include <QPaintDevice>
# include <QSvgGenerator>
# include <math.h>
#endif
#include <App/Application.h>
#include <App/Material.h>
#include <Base/Console.h>
#include <Base/Exception.h>
#include <Base/Parameter.h>
#include <Base/UnitsApi.h>
#include <Gui/Command.h>
#include <Mod/Part/App/PartFeature.h>
#include <Mod/TechDraw/App/DrawViewDimension.h>
#include <Mod/TechDraw/App/DrawViewPart.h>
#include <Mod/TechDraw/App/DrawUtil.h>
#include <Mod/TechDraw/App/Geometry.h>
#include "Rez.h"
#include "ZVALUE.h"
#include "QGIArrow.h"
#include "QGIDimLines.h"
#include "QGIViewDimension.h"
#include "ViewProviderDimension.h"
#include "DrawGuiUtil.h"
//TODO: hide the Qt coord system (+y down).
using namespace TechDraw;
using namespace TechDrawGui;
enum SnapMode{
NoSnap,
VerticalSnap,
HorizontalSnap
};
QGIDatumLabel::QGIDatumLabel()
{
posX = 0;
posY = 0;
setCacheMode(QGraphicsItem::NoCache);
setFlag(ItemSendsGeometryChanges, true);
setFlag(ItemIsMovable, true);
setFlag(ItemIsSelectable, true);
setAcceptHoverEvents(true);
m_dimText = new QGCustomText();
m_dimText->setParentItem(this);
m_tolText = new QGCustomText();
m_tolText->setParentItem(this);
m_ctrl = false;
}
QVariant QGIDatumLabel::itemChange(GraphicsItemChange change, const QVariant &value)
{
if (change == ItemSelectedHasChanged && scene()) {
if(isSelected()) {
Q_EMIT selected(true);
setPrettySel();
} else {
Q_EMIT selected(false);
setPrettyNormal();
}
update();
} else if(change == ItemPositionHasChanged && scene()) {
setLabelCenter();
Q_EMIT dragging(m_ctrl);
}
return QGraphicsItem::itemChange(change, value);
}
void QGIDatumLabel::mousePressEvent(QGraphicsSceneMouseEvent * event)
{
if(event->modifiers() & Qt::ControlModifier) {
m_ctrl = true;
}
if(scene() && this == scene()->mouseGrabberItem()) {
Q_EMIT dragFinished();
}
QGraphicsItem::mousePressEvent(event);
}
void QGIDatumLabel::mouseMoveEvent(QGraphicsSceneMouseEvent * event)
{
QGraphicsItem::mouseMoveEvent(event);
}
void QGIDatumLabel::mouseReleaseEvent(QGraphicsSceneMouseEvent * event)
{
m_ctrl = false;
if(scene() && this == scene()->mouseGrabberItem()) {
Q_EMIT dragFinished();
}
QGraphicsItem::mouseReleaseEvent(event);
}
void QGIDatumLabel::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
Q_EMIT hover(true);
if (!isSelected()) {
setPrettyPre();
}
QGraphicsItem::hoverEnterEvent(event);
}
void QGIDatumLabel::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
QGIView *view = dynamic_cast<QGIView *> (parentItem());
assert(view != 0);
Q_UNUSED(view);
Q_EMIT hover(false);
if (!isSelected()) {
setPrettyNormal();
}
QGraphicsItem::hoverLeaveEvent(event);
}
QRectF QGIDatumLabel::boundingRect() const
{
return childrenBoundingRect();
}
void QGIDatumLabel::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(widget);
Q_UNUSED(painter);
QStyleOptionGraphicsItem myOption(*option);
myOption.state &= ~QStyle::State_Selected;
// painter->drawRect(boundingRect()); //good for debugging
}
void QGIDatumLabel::setPosFromCenter(const double &xCenter, const double &yCenter)
{
//set label's Qt position(top,left) given boundingRect center point
setPos(xCenter - m_dimText->boundingRect().width() / 2., yCenter - m_dimText->boundingRect().height() / 2.);
//set tolerance position
QRectF labelBox = m_dimText->boundingRect();
double right = labelBox.right();
double top = labelBox.top();
m_tolText->setPos(right,top);
}
void QGIDatumLabel::setLabelCenter()
{
//save label's bRect center (posX,posY) given Qt position (top,left)
posX = x() + m_dimText->boundingRect().width() / 2.;
posY = y() + m_dimText->boundingRect().height() / 2.;
}
void QGIDatumLabel::setFont(QFont f)
{
m_dimText->setFont(f);
QFont tFont(f);
double fontSize = f.pointSizeF();
double tolAdj = getTolAdjust();
tFont.setPointSizeF(fontSize * tolAdj);
m_tolText->setFont(tFont);
}
void QGIDatumLabel::setDimString(QString t)
{
prepareGeometryChange();
m_dimText->setPlainText(t);
}
void QGIDatumLabel::setDimString(QString t, qreal maxWidth)
{
prepareGeometryChange();
m_dimText->setPlainText(t);
m_dimText->setTextWidth(maxWidth);
}
void QGIDatumLabel::setTolString()
{
prepareGeometryChange();
QGIViewDimension* qgivd = dynamic_cast<QGIViewDimension*>(parentItem());
if( qgivd == nullptr ) {
return; //tarfu
}
const auto dim( dynamic_cast<TechDraw::DrawViewDimension *>(qgivd->getViewObject()) );
if( dim == nullptr ) {
return;
} else if (!dim->hasTolerance()) {
return;
}
double overTol = dim->OverTolerance.getValue();
double underTol = dim->UnderTolerance.getValue();
int precision = getPrecision();
QString overFormat = QString::number(overTol,'f', precision);
QString underFormat = QString::number(underTol,'f',precision);
QString html = QString::fromUtf8("<div>%1 <br/>%2 </div>");
html = html.arg(overFormat).arg(underFormat);
m_tolText->setHtml(html);
return;
}
int QGIDatumLabel::getPrecision(void)
{
int precision;
bool global = false;
Base::Reference<ParameterGrp> hGrp = App::GetApplication().GetUserParameter()
.GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Mod/TechDraw/Dimensions");
global = hGrp->GetBool("UseGlobalDecimals", true);
if (global) {
precision = Base::UnitsApi::getDecimals();
} else {
precision = hGrp->GetInt("AltDecimals", 2);
}
return precision;
}
double QGIDatumLabel::getTolAdjust(void)
{
double adjust;
Base::Reference<ParameterGrp> hGrp = App::GetApplication().GetUserParameter()
.GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Mod/TechDraw/Dimensions");
adjust = hGrp->GetFloat("TolSizeAdjust", 0.50);
return adjust;
}
void QGIDatumLabel::setPrettySel(void)
{
m_dimText->setPrettySel();
m_tolText->setPrettySel();
}
void QGIDatumLabel::setPrettyPre(void)
{
m_dimText->setPrettyPre();
m_tolText->setPrettyPre();
}
void QGIDatumLabel::setPrettyNormal(void)
{
m_dimText->setPrettyNormal();
m_tolText->setPrettyNormal();
}
void QGIDatumLabel::setColor(QColor c)
{
m_colNormal = c;
m_dimText->setColor(m_colNormal);
m_tolText->setColor(m_colNormal);
}
//**************************************************************
QGIViewDimension::QGIViewDimension() :
hasHover(false),
m_lineWidth(0.0),
m_obtuse(false)
{
setHandlesChildEvents(false);
setFlag(QGraphicsItem::ItemIsMovable, false);
setCacheMode(QGraphicsItem::NoCache);
datumLabel = new QGIDatumLabel();
addToGroup(datumLabel);
datumLabel->setColor(getNormalColor());
datumLabel->setPrettyNormal();
dimLines = new QGIDimLines();
addToGroup(dimLines);
aHead1 = new QGIArrow();
addToGroup(aHead1);
aHead2 = new QGIArrow();
addToGroup(aHead2);
datumLabel->setZValue(ZVALUE::DIMENSION);
dimLines->setZValue(ZVALUE::DIMENSION);
aHead1->setZValue(ZVALUE::DIMENSION);
aHead2->setZValue(ZVALUE::DIMENSION);
//centerMark = new QGICMark();
//addToGroup(centerMark);
// connecting the needed slots and signals
QObject::connect(
datumLabel, SIGNAL(dragging(bool)),
this , SLOT (datumLabelDragged(bool)));
QObject::connect(
datumLabel, SIGNAL(dragFinished()),
this , SLOT (datumLabelDragFinished()));
QObject::connect(
datumLabel, SIGNAL(selected(bool)),
this , SLOT (select(bool)));
QObject::connect(
datumLabel, SIGNAL(hover(bool)),
this , SLOT (hover(bool)));
dimLines->setStyle(Qt::SolidLine);
// toggleBorder(false);
setZValue(ZVALUE::DIMENSION); //note: this won't paint dimensions over another View if it stacks
//above this Dimension's parent view. need Layers?
}
void QGIViewDimension::setViewPartFeature(TechDraw::DrawViewDimension *obj)
{
if(obj == 0)
return;
setViewFeature(static_cast<TechDraw::DrawView *>(obj));
// Set the QGIGroup Properties based on the DrawView
float x = Rez::guiX(obj->X.getValue());
float y = Rez::guiX(-obj->Y.getValue());
datumLabel->setPosFromCenter(x, y);
updateDim();
draw();
}
void QGIViewDimension::select(bool state)
{
setSelected(state);
draw();
}
void QGIViewDimension::hover(bool state)
{
hasHover = state;
draw();
}
void QGIViewDimension::updateView(bool update)
{
Q_UNUSED(update);
auto dim( dynamic_cast<TechDraw::DrawViewDimension*>(getViewObject()) );
if( dim == nullptr )
return;
auto vp = static_cast<ViewProviderDimension*>(getViewProvider(getViewObject()));
if ( vp == nullptr ) {
return;
}
if (update||
dim->X.isTouched() ||
dim->Y.isTouched()) {
float x = Rez::guiX(dim->X.getValue());
float y = Rez::guiX(dim->Y.getValue());
datumLabel->setPosFromCenter(x,-y);
updateDim();
}
else if(vp->Fontsize.isTouched() ||
vp->Font.isTouched()) {
updateDim();
} else if (vp->LineWidth.isTouched()) { //never happens!!
m_lineWidth = vp->LineWidth.getValue();
updateDim();
} else {
updateDim();
}
draw();
}
void QGIViewDimension::updateDim(bool obtuse)
{
(void) obtuse;
const auto dim( dynamic_cast<TechDraw::DrawViewDimension *>(getViewObject()) );
if( dim == nullptr ) {
return;
}
auto vp = static_cast<ViewProviderDimension*>(getViewProvider(getViewObject()));
if ( vp == nullptr ) {
return;
}
QString labelText = QString::fromUtf8(dim->getFormatedValue(m_obtuse).c_str());
QFont font = datumLabel->getFont();
font.setFamily(QString::fromUtf8(vp->Font.getValue()));
font.setPointSizeF(calculateFontPointSizeF(this, vp->Fontsize.getValue()));
datumLabel->setFont(font);
prepareGeometryChange();
datumLabel->setDimString(labelText);
datumLabel->setTolString();
datumLabel->setPosFromCenter(datumLabel->X(),datumLabel->Y());
}
//this is for formatting and finding centers, not display
QString QGIViewDimension::getLabelText(void)
{
QString result;
QString first = datumLabel->getDimText()->toPlainText();
QString second = datumLabel->getTolText()->toPlainText();
result = first + second;
return result;
}
void QGIViewDimension::datumLabelDragged(bool ctrl)
{
Q_UNUSED(ctrl);
draw();
}
void QGIViewDimension::datumLabelDragFinished()
{
auto dim( dynamic_cast<TechDraw::DrawViewDimension *>(getViewObject()) );
if( dim == nullptr ) {
return;
}
double x = Rez::appX(datumLabel->X()),
y = Rez::appX(datumLabel->Y());
Gui::Command::openCommand("Drag Dimension");
Gui::Command::doCommand(Gui::Command::Doc,"App.ActiveDocument.%s.X = %f", dim->getNameInDocument(), x);
Gui::Command::doCommand(Gui::Command::Doc,"App.ActiveDocument.%s.Y = %f", dim->getNameInDocument(), -y);
Gui::Command::commitCommand();
}
void QGIViewDimension::draw()
{
if (!isVisible()) { //should this be controlled by parent ViewPart?
return;
}
datumLabel->show();
show();
TechDraw::DrawViewDimension *dim = dynamic_cast<TechDraw::DrawViewDimension *>(getViewObject());
if((!dim) || //nothing to draw, don't try
(!dim->isDerivedFrom(TechDraw::DrawViewDimension::getClassTypeId())) ||
(!dim->has2DReferences()) ) {
datumLabel->hide();
hide();
return;
}
const TechDraw::DrawViewPart *refObj = dim->getViewPart();
if(!refObj->hasGeometry()) { //nothing to draw yet (restoring)
datumLabel->hide();
hide();
return;
}
auto vp = static_cast<ViewProviderDimension*>(getViewProvider(getViewObject()));
if ( vp == nullptr ) {
return;
}
m_colNormal = getNormalColor();
datumLabel->setColor(m_colNormal);
m_lineWidth = Rez::guiX(vp->LineWidth.getValue());
float margin = Rez::guiX(5.f);
QString labelText = getLabelText();
Base::Vector3d lblCenter(datumLabel->X(), datumLabel->Y(), 0); //already Qt gui coords
const char *dimType = dim->Type.getValueAsString();
datumLabel->show();
show();
if (strcmp(dimType, "Distance") == 0 ||
strcmp(dimType, "DistanceX") == 0 ||
strcmp(dimType, "DistanceY") == 0) {
Base::Vector3d stdUp(0.0,1.0,0.0);
Base::Vector3d stdLeft(-1.0,0.0,0.0);
pointPair pts = dim->getLinearPoints();
Base::Vector3d startDist, endDist, midDist; //start/end/mid points of distance line
startDist = Rez::guiX(pts.first);
endDist = Rez::guiX(pts.second);
if (startDist.y < endDist.y) { //always measure bottom to top
Base::Vector3d temp = startDist;
startDist = endDist;
endDist = temp;
}
Base::Vector3d vecDist = (endDist - startDist);
// +/- aligned method
// dimension text legible from bottom or right
// text outside arrows (not between) iso convention (asme convention)
// text to left of vertical dims
// text above horizontal dims
Base::Vector3d dirDist, normDist; //direction/normal vectors of distance line
Base::Vector3d dirExt;
Base::Vector3d dirDim, normDim;
Base::Vector3d dirIso;
dirDist = vecDist;
dirDist.Normalize();
normDist = Base::Vector3d (-dirDist.y,dirDist.x, 0); //normal to distance direction
//toward dimension line??? how to tell?
if (strcmp(dimType, "Distance") == 0 ) {
//distance and dimension lines parallel
dirDim = dirDist;
normDim = normDist;
} else if (strcmp(dimType, "DistanceX") == 0 ) {
//distance and dimension lines not (necessarily) parallel
dirDim = Base::Vector3d ( ((endDist.x - startDist.x >= FLT_EPSILON) ? 1 : -1) , 0, 0);
normDim = Base::Vector3d (-dirDim.y,dirDim.x, 0);
} else if (strcmp(dimType, "DistanceY") == 0 ) {
//distance and dimension lines not (necessarily) parallel
dirDim = Base::Vector3d (0, 1, 0);
normDim = Base::Vector3d (-1, 0, 0);
}
//for ortho drawing extension lines are para to normDim, perp to dirDist
dirExt = normDim; //dirExt is para or anti-parallel to real extension direction
dirIso = normDim;
if (refObj->isIso()) {
//is this dimension an iso dimension? ie points +/-isoX,+/-isoY,+/-isoZ
dirIso = findIsoDir(dirDist);
dirExt = findIsoExt(dirIso);
}
dirExt.Normalize();
// Get magnitude of angle between dimension line and horizontal
// to determine rotation of dimension text ("aligned" convention. alt is "unidirectional")
//note qt y axis is reversed! and angles are CW!
double textRotAngle = atan2(-dirDim.y,dirDim.x);
if (textRotAngle < 0.0) {
textRotAngle = 2 * M_PI + textRotAngle; //map to +ve textRotAngle
}
//orient text right side up
double angleFiddle = M_PI / 10.0; // 18 => 10*, 12 => 15*, 10 => 18*, ...
if ((textRotAngle > M_PI_2 + angleFiddle) && // > 100CW
(textRotAngle <= M_PI)) { // < 180CW -> Q2
textRotAngle += M_PI; // flip CW
} else if ((textRotAngle > M_PI) && // > 180CW
(textRotAngle <= 1.5*M_PI - angleFiddle)) { // < 260CW -> Q3
textRotAngle -= M_PI; // flip CCW
}
Base::Vector3d textNorm = normDim;
if (strcmp(dimType, "DistanceX") == 0 ) {
textNorm = stdUp; //always above
} else if (strcmp(dimType, "DistanceY") == 0 ) {
textNorm = stdLeft; //left of dimLine
} else if (std::abs(dirDist.x) < FLT_EPSILON) { //this is horizontal dim line
textNorm = stdLeft; //left of dimLine
} else if (std::abs(dirDist.y) < FLT_EPSILON) { //this is vertical dim line
textNorm = stdLeft; //left of dimLine
}
// +/- pos of startDist vs endDist for vert/horiz Dims
// distStartDelta sb zero for normal dims
float distStartDelta = vecDist.Dot(normDim); // component of distance vector in dim line direction
Base::Vector3d startDistAdj = startDist + normDim * distStartDelta;
double textOffset = getDefaultTextVerticalOffset();
QPointF qFigure = boundingRect().center();
Base::Vector3d figureCenter(qFigure.x(),qFigure.y(),0.0);
QRectF mappedRect = mapRectFromItem(datumLabel, datumLabel->boundingRect());
lblCenter = Base::Vector3d(mappedRect.center().x(), mappedRect.center().y(), 0.0);
//find actual extension direction vector
Base::Vector3d startIntercept = DrawUtil::Intersect2d(startDist, dirExt,
lblCenter,dirDim);
Base::Vector3d dirExtActual = (startIntercept - startDist);
dirExtActual.Normalize();
midDist = (startDistAdj + endDist) / 2.0; //middle point of distance line
//fauxCenter is where the getDimText() would be if it was on the dimLine
Base::Vector3d fauxCenter;
if (strcmp(dimType, "Distance") == 0 ) { //oblique line
textRotAngle = -textRotAngle; // flip text 180*
fauxCenter = lblCenter + textOffset * dirExtActual;
double slope;
if (DrawUtil::fpCompare(dirDist.x, 0.0)) {
slope = std::numeric_limits<float>::max(); //vertical line
} else {
slope = fabs(dirDist.y / dirDist.x);
}
double deg = atan(slope) * 180.0 / M_PI;
if (deg > (90.0 - (angleFiddle * 180.0/M_PI))) { //mostly vertical +/- 10*
//dim text reads bottom to top on left side of dim line
if (lblCenter.x > fauxCenter.x) { //label is to right of dimline
fauxCenter = lblCenter - textOffset*dirExtActual; //move dim line closer to figure
}
} else { //mostly horizontal
//dim text reads left to right above dim line
if (lblCenter.y > fauxCenter.y) { //label is below dimline
fauxCenter = lblCenter - textOffset*dirExtActual; //move dim line closer to figure
}
}
} else if (strcmp(dimType, "DistanceX") == 0 ) {
fauxCenter = lblCenter + textOffset * textNorm;
} else if (strcmp(dimType, "DistanceY") == 0 ) {
textRotAngle = - textRotAngle;
fauxCenter = lblCenter - textOffset * textNorm;
}
//intersection of extension lines and dimension line
startIntercept = DrawUtil::Intersect2d(startDist, dirExt,
fauxCenter,dirDim);
Base::Vector3d endIntercept = DrawUtil::Intersect2d(endDist, dirExt,
fauxCenter,dirDim);
dirExtActual = (startIntercept - startDist);
dirExtActual.Normalize();
margin = Rez::guiX(2.f);
float scaler = 1.;
Base::Vector3d extStartEnd = startIntercept + dirExtActual * (margin * scaler);
Base::Vector3d extEndEnd = endIntercept + dirExtActual * (margin * scaler);
//case 1: inner placement: text between extensions & fits. arros point out from inside (default)
//case 2: inner placement2: text too big to fit. arrows point in from outside
//case 3: outer placement: text is outside extensions. arrows point in, 1 arrow tracks getDimText()
Base::Vector3d dim1Tip = startIntercept;
Base::Vector3d dim1Tail = fauxCenter;
Base::Vector3d dim2Tip = endIntercept;
Base::Vector3d dim2Tail = fauxCenter;
Base::Vector3d a1Dir = -dirDim;
Base::Vector3d a2Dir = dirDim;
if (strcmp(dimType, "DistanceY") == 0 ) {
a1Dir = Base::Vector3d(0,1,0);
a2Dir = Base::Vector3d(0,-1,0);
}
double dimSpan = (extEndEnd - extStartEnd).Length(); //distance between extension lines
double fauxToDim1 = (fauxCenter - dim1Tip).Length(); //label to arrow #1
double fauxToDim2 = (fauxCenter - dim2Tip).Length(); //label to end #2
double tailLength = Rez::guiX(10.f) * scaler;
//case2 - innerPlacement && text > span
double lblWidth = datumLabel->boundingRect().width();
if ((DrawUtil::isBetween(fauxCenter, dim1Tip, dim2Tip)) &&
(lblWidth > dimSpan) ) {
if (strcmp(dimType, "DistanceY") == 0 ) {
a1Dir = Base::Vector3d(0,-1,0);
a2Dir = Base::Vector3d(0,1,0);
dim1Tail = dim1Tip;
dim2Tail = dim2Tip;
} else {
dim1Tail = dim1Tip - tailLength * dirDim;
dim2Tail = dim2Tip + tailLength * dirDim;
a1Dir = dirDim;
a2Dir = -dirDim;
}
}
if (!DrawUtil::isBetween(fauxCenter, dim1Tip, dim2Tip)) {
//case3 - outerPlacement
if (strcmp(dimType, "DistanceY") == 0 ) {
a1Dir = Base::Vector3d(0,-1,0);
a2Dir = Base::Vector3d(0,1,0);
} else {
a1Dir = dirDim;
a2Dir = -dirDim;
}
if (fauxToDim1 < fauxToDim2) {
dim1Tail = fauxCenter;
dim2Tail = dim2Tip - tailLength*a2Dir;
} else {
dim1Tail = dim1Tip - tailLength*a1Dir;
dim2Tail = fauxCenter;
}
}
// Extension lines
QPainterPath path;
Base::Vector3d gap = startDist + dirExtActual * (margin * scaler); //space ext line a bt
path.moveTo(gap.x, gap.y);
path.lineTo(extStartEnd.x, extStartEnd.y);
gap = endDist + dirExtActual * (margin * scaler);
path.moveTo(gap.x, gap.y);
path.lineTo(extEndEnd.x, extEndEnd.y);
//Dimension lines (arrow shafts)
//TODO: line tip goes just a bit too far. overlaps the arrowhead's point.
path.moveTo(dim1Tip.x, dim1Tip.y);
path.lineTo(dim1Tail.x, dim1Tail.y);
path.moveTo(dim2Tip.x, dim2Tip.y);
path.lineTo(dim2Tail.x, dim2Tail.y);
dimLines->setPath(path);
// Note Bounding Box size is not the same width or height as text (only used for finding center)
double bbX = datumLabel->boundingRect().width();
double bbY = datumLabel->boundingRect().height();
datumLabel->setTransformOriginPoint(bbX / 2, bbY /2);
double angleOption = 0.0; //put lblText angle adjustments here
datumLabel->setRotation((textRotAngle * 180 / M_PI) + angleOption);
if (strcmp(dimType, "DistanceY") == 0 ) {
datumLabel->setRotation(-90.0 + angleOption);
}
aHead1->setDirMode(true);
aHead2->setDirMode(true);
if (vp->FlipArrowheads.getValue()) {
aHead1->setDirection(a1Dir * -1.0);
aHead2->setDirection(a2Dir * -1.0);
} else {
aHead1->setDirection(a1Dir);
aHead2->setDirection(a2Dir);
}
aHead1->setStyle(QGIArrow::getPrefArrowStyle());
aHead1->setSize(QGIArrow::getPrefArrowSize());
aHead1->draw();
aHead2->setStyle(QGIArrow::getPrefArrowStyle());
aHead2->setSize(QGIArrow::getPrefArrowSize());
aHead2->draw();
aHead1->setPos(dim1Tip.x, dim1Tip.y);
aHead2->setPos(dim2Tip.x, dim2Tip.y);
aHead1->setDirMode(false);
aHead2->setDirMode(false);
} else if(strcmp(dimType, "Diameter") == 0) {
// terminology: Dimension Text, Dimension Line(s), Extension Lines, Arrowheads
Base::Vector3d arrow1Tip, arrow2Tip;
Base::Vector3d arrow1Tail, arrow2Tail;
Base::Vector3d arrow1Dir,arrow2Dir;
double radius;
Base::Vector3d pointOnCurve,curveCenter;
Base::Vector3d startExt1,endExt1,startExt2,endExt2;
arcPoints pts = dim->getArcPoints();
bool isArc = pts.isArc;
radius = Rez::guiX(pts.radius);
curveCenter = Rez::guiX(pts.center);
pointOnCurve = Rez::guiX(pts.onCurve.first);
Base::Vector3d startDist,endDist,dirDim;
startDist = Rez::guiX(pts.onCurve.first);
endDist = Rez::guiX(pts.onCurve.second);
dirDim = endDist - startDist;
if (fabs(dirDim.Length()) < Precision::Confusion()) {
dirDim = Base::Vector3d(1.0,0.0,0.0);
}
dirDim.Normalize();
Base::Vector3d fauxCenter = lblCenter;
//default arrow endpoints
arrow1Tip = curveCenter - dirDim * radius;
arrow2Tip = curveCenter + dirDim * radius;
arrow1Tail = curveCenter;
arrow2Tail = curveCenter;
double gapMargin = Rez::guiX(4.f);
margin = Rez::guiX(2.f);
float scaler = 1.;
double tip = (margin * scaler);
double gap = (gapMargin * scaler); //sb % of radius?
//offset of dimLine from getDimText()
double horizOffset = getDefaultTextHorizontalOffset(lblCenter.x > curveCenter.x);
double vertOffset = getDefaultTextVerticalOffset();
bool outerPlacement = false;
if ((lblCenter-curveCenter).Length() > radius) { //label is outside circle
outerPlacement = true;
}
// // Note Bounding Box size is not the same width or height as text (only used for finding center)
float bbX = datumLabel->boundingRect().width();
float bbY = datumLabel->boundingRect().height();
datumLabel->setTransformOriginPoint(bbX / 2, bbY /2);
int posMode = NoSnap;
bool isLeader = false;
QPainterPath path;
if(outerPlacement) {
Base::Vector3d v = (lblCenter - curveCenter);
double angle = atan2(v.y, v.x);
double tolerance = 15.0; //deg
tolerance *= M_PI / 180;
if( (angle > -tolerance && angle < tolerance) || //angle = 0 or 180 (+/- 15)
(angle > (M_PI - tolerance) || angle < (-M_PI + tolerance)) ) { //dim line is Horizontal
posMode = HorizontalSnap;
} else if( (angle < ( M_PI / 2. + tolerance) && angle > ( M_PI / 2. - tolerance)) || //angle = 90 or 270 (+/- 15)
(angle < (-M_PI / 2. + tolerance) && angle > (-M_PI / 2. - tolerance)) ) { //dim line is Vertical
posMode = VerticalSnap;
}
// fauxCenter = Base::Vector3d(lblCenter.x,lblCenter.y + vertOffset,lblCenter.z);
if(posMode == VerticalSnap) {
QRectF mappedRect = mapRectFromItem(datumLabel, datumLabel->boundingRect());
lblCenter = Base::Vector3d(mappedRect.center().x(), mappedRect.center().y(), 0.0);
if (lblCenter.y > curveCenter.y) { //
fauxCenter = Base::Vector3d(lblCenter.x,lblCenter.y + vertOffset,lblCenter.z);
} else {
fauxCenter = Base::Vector3d(lblCenter.x,lblCenter.y + vertOffset,lblCenter.z);
tip = -tip;
gap = -gap;
}
arrow1Tip.x = curveCenter.x - radius; //to left, on circle cl
arrow1Tip.y = fauxCenter.y;
arrow1Tail.x = curveCenter.x;
arrow1Tail.y = arrow1Tip.y;
arrow1Dir = (arrow1Tip - arrow1Tail).Normalize();
path.moveTo(arrow1Tail.x, arrow1Tail.y);
path.lineTo(arrow1Tip.x, arrow1Tip.y);
arrow2Tip.x = curveCenter.x + radius;
arrow2Tip.y = fauxCenter.y;
arrow2Tail.x = curveCenter.x;
arrow2Tail.y = arrow2Tip.y;
arrow2Dir = (arrow2Tip - arrow2Tail).Normalize();
path.moveTo(arrow2Tail.x, arrow2Tail.y);
path.lineTo(arrow2Tip.x, arrow2Tip.y);
startExt1.x = curveCenter.x - radius;
startExt1.y = curveCenter.y + gap;
endExt1.x = startExt1.x;
endExt1.y = fauxCenter.y + tip;
startExt2.x = curveCenter.x + radius;
startExt2.y = curveCenter.y + gap;
endExt2.x = startExt2.x;
endExt2.y = fauxCenter.y + tip;
path.moveTo(startExt1.x, startExt1.y);
path.lineTo(endExt1.x, endExt1.y);
path.moveTo(startExt2.x, startExt2.y);
path.lineTo(endExt2.x, endExt2.y);
datumLabel->setRotation(0.0);
} else if(posMode == HorizontalSnap) {
QRectF mappedRect = mapRectFromItem(datumLabel, datumLabel->boundingRect());
lblCenter = Base::Vector3d(mappedRect.center().x(), mappedRect.center().y(), 0.0);
if (lblCenter.x > curveCenter.x) { //label right
// fauxCenter = Base::Vector3d(lblCenter.x - horizOffset,lblCenter.y,lblCenter.z); //unidirection convention
fauxCenter = Base::Vector3d(lblCenter.x + vertOffset,lblCenter.y,lblCenter.z); //aligned convention
} else { //label left
tip = -tip;
gap = -gap;
// fauxCenter = Base::Vector3d(lblCenter.x + horizOffset,lblCenter.y,lblCenter.z);
fauxCenter = Base::Vector3d(lblCenter.x + vertOffset,lblCenter.y,lblCenter.z);
}
arrow1Tip.x = fauxCenter.x;
arrow1Tip.y = curveCenter.y - radius;
arrow1Tail.x = arrow1Tip.x;
arrow1Tail.y = curveCenter.y;
arrow1Dir = (arrow1Tip - arrow1Tail).Normalize();
path.moveTo(arrow1Tail.x, arrow1Tail.y);
path.lineTo(arrow1Tip.x, arrow1Tip.y);
arrow2Tip.x = fauxCenter.x;
arrow2Tip.y = curveCenter.y + radius;
arrow2Tail.x = arrow2Tip.x;
arrow2Tail.y = curveCenter.y;
arrow2Dir = (arrow2Tip - arrow2Tail).Normalize();
path.moveTo(arrow2Tail.x, arrow2Tail.y);
path.lineTo(arrow2Tip.x, arrow2Tip.y);
startExt1.x = curveCenter.x + gap;
startExt1.y = curveCenter.y - radius;
endExt1.x = fauxCenter.x + tip;
endExt1.y = startExt1.y;
startExt2.x = curveCenter.x + gap;
startExt2.y = curveCenter.y + radius;
endExt2.x = fauxCenter.x + tip;
endExt2.y = startExt2.y;
path.moveTo(startExt1.x, startExt1.y);
path.lineTo(endExt1.x, endExt1.y);
path.moveTo(startExt2.x, startExt2.y);
path.lineTo(endExt2.x, endExt2.y);
datumLabel->setRotation(-90.0); //aligned convention
// datumLabel->setRotation(0.0); //unidirectional convention
} else { //outer placement, NoSnap
QRectF mappedRect = mapRectFromItem(datumLabel, datumLabel->boundingRect());
lblCenter = Base::Vector3d(mappedRect.center().x(), mappedRect.center().y(), 0.0);
isLeader = true;
arrow1Tail = lblCenter;
arrow1Tail.x += horizOffset;
Base::Vector3d kinkPoint = arrow1Tail;
kinkPoint.x += (lblCenter.x < curveCenter.x) ? margin : - margin;
arrow1Tip = curveCenter + (kinkPoint - curveCenter).Normalize() * radius;
path.moveTo(arrow1Tail.x, arrow1Tail.y);
path.lineTo(kinkPoint.x, kinkPoint.y);
path.lineTo(arrow1Tip.x, arrow1Tip.y);
arrow1Dir = (arrow1Tip - kinkPoint).Normalize();
datumLabel->setRotation(0.);
}
} else { //NOT outerplacement ie dimLines are inside circle
double vertOffset = getDefaultTextVerticalOffset();
Base::Vector3d dirLabel = lblCenter - curveCenter;
if (dirLabel.Length() < Precision::Confusion()) {
dirLabel = Base::Vector3d(-1.0, 0.0, 0.0);
}
double labelAngle = 0.0;
if (vertOffset < dirLabel.Length()) {
double lineShiftAngle = asin(vertOffset/dirLabel.Length());
labelAngle = atan2(dirLabel.y, dirLabel.x);
if (labelAngle > 0.25*M_PI) {
labelAngle -= M_PI + lineShiftAngle;
}
else if (labelAngle < -0.75*M_PI) {
labelAngle += M_PI - lineShiftAngle;
}
else {
labelAngle += lineShiftAngle;
}
}
// Set the angle of the dimension text
datumLabel->setRotation(labelAngle*180/M_PI);
dirDim = Base::Vector3d(cos(labelAngle), sin(labelAngle), 0.0);
arrow1Tip = curveCenter - dirDim * radius;
arrow1Tail = curveCenter;
arrow1Dir = (arrow1Tip - arrow1Tail).Normalize();
arrow2Tip = curveCenter + dirDim * radius;
arrow2Tail = curveCenter;
arrow2Dir = (arrow2Tip - arrow2Tail).Normalize();
path.moveTo(arrow1Tail.x, arrow1Tail.y);
path.lineTo(arrow1Tip.x, arrow1Tip.y);
path.moveTo(arrow2Tail.x, arrow2Tail.y);
path.lineTo(arrow2Tip.x, arrow2Tip.y);
}