/
rs_graphicview.cpp
1953 lines (1597 loc) · 44.7 KB
/
rs_graphicview.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
/****************************************************************************
**
** This file is part of the LibreCAD project, a 2D CAD program
**
** Copyright (C) 2015 A. Stebich (librecad@mail.lordofbikes.de)
** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
**
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file gpl-2.0.txt included in the
** packaging of this file.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**
** This copyright notice MUST APPEAR in all copies of the script!
**
**********************************************************************/
#include<climits>
#include<cmath>
#include <QApplication>
#include <QAction>
#include <QMouseEvent>
#include <QtAlgorithms>
#include "rs_graphicview.h"
#include "rs_color.h"
#include "rs_debug.h"
#include "rs_dialogfactory.h"
#include "rs_eventhandler.h"
#include "rs_graphic.h"
#include "rs_grid.h"
#include "rs_line.h"
#include "rs_linetypepattern.h"
#include "rs_math.h"
#include "rs_painter.h"
#include "rs_snapper.h"
#include "rs_settings.h"
#include "rs_units.h"
#ifdef EMU_C99
#include "emu_c99.h"
#endif
struct RS_GraphicView::ColorData {
/** background color (any color) */
RS_Color background;
/** foreground color (black or white) */
RS_Color foreground;
/** grid color */
RS_Color gridColor = Qt::gray;
/** meta grid color */
RS_Color metaGridColor;
/** selected color */
RS_Color selectedColor;
/** highlighted color */
RS_Color highlightedColor;
/** Start handle color */
RS_Color startHandleColor;
/** Intermediate (not start/end vertex) handle color */
RS_Color handleColor;
/** End handle color */
RS_Color endHandleColor;
/** Relative-zero marker color */
RS_Color relativeZeroColor;
/* Relative-zero hidden state */
bool hideRelativeZero = false;
};
/**
* Constructor.
*/
RS_GraphicView::RS_GraphicView(QWidget* parent, Qt::WindowFlags f)
:QWidget(parent, f)
,eventHandler{new RS_EventHandler{this}}
, m_colorData{std::make_unique<ColorData>()}
,grid{std::make_unique<RS_Grid>(this)}
,defaultSnapMode{std::make_unique<RS_SnapMode>()}
,drawingMode(RS2::ModeFull)
,savedViews(16)
,previousViewTime{std::make_unique<QDateTime>(QDateTime::currentDateTime())}
{
RS_SETTINGS->beginGroup("Colors");
setBackground(QColor(RS_SETTINGS->readEntry("/background", RS_Settings::background)));
setGridColor(QColor(RS_SETTINGS->readEntry("/grid", RS_Settings::grid)));
setMetaGridColor(QColor(RS_SETTINGS->readEntry("/meta_grid", RS_Settings::meta_grid)));
setSelectedColor(QColor(RS_SETTINGS->readEntry("/select", RS_Settings::select)));
setHighlightedColor(QColor(RS_SETTINGS->readEntry("/highlight", RS_Settings::highlight)));
setStartHandleColor(QColor(RS_SETTINGS->readEntry("/start_handle", RS_Settings::start_handle)));
setHandleColor(QColor(RS_SETTINGS->readEntry("/handle", RS_Settings::handle)));
setEndHandleColor(QColor(RS_SETTINGS->readEntry("/end_handle", RS_Settings::end_handle)));
setRelativeZeroColor(QColor(RS_SETTINGS->readEntry("/relativeZeroColor", RS_Settings::relativeZeroColor)));
RS_SETTINGS->endGroup();
RS_SETTINGS->beginGroup("/Appearance");
RS_SETTINGS->writeEntry("/hideRelativeZero", RS_SETTINGS->readNumEntry("/hideRelativeZero", 0));
RS_SETTINGS->endGroup();
}
RS_GraphicView::~RS_GraphicView()
{
qDeleteAll(overlayEntities);
}
/**
* Must be called by any derived class in the destructor.
*/
void RS_GraphicView::cleanUp() {
m_bIsCleanUp=true;
}
/**
* Sets the pointer to the graphic which contains the entities
* which are visualized by this widget.
*/
void RS_GraphicView::setContainer(RS_EntityContainer* container) {
this->container = container;
//adjustOffsetControls();
}
/**
* Sets the zoom factor in X for this visualization of the graphic.
*/
void RS_GraphicView::setFactorX(double f) {
if (!zoomFrozen) {
factor.x = std::abs(f);
}
}
/**
* Sets the zoom factor in Y for this visualization of the graphic.
*/
void RS_GraphicView::setFactorY(double f) {
if (!zoomFrozen) {
factor.y = std::abs(f);
}
}
void RS_GraphicView::setOffset(int ox, int oy) {
// DEBUG_HEADER
// RS_DEBUG->print(/*RS_Debug::D_WARNING, */"set offset from (%d, %d) to (%d, %d)", getOffsetX(), getOffsetY(), ox, oy);
setOffsetX(ox);
setOffsetY(oy);
}
/**
* @return true if the grid is switched on.
*/
bool RS_GraphicView::isGridOn() const{
if (container) {
RS_Graphic* graphic = container->getGraphic();
if (graphic != nullptr) {
return graphic->isGridOn();
}
}
return true;
}
/**
* @return true if the grid is isometric
*
*@Author: Dongxu Li
*/
bool RS_GraphicView::isGridIsometric() const{
return grid->isIsometric();
}
void RS_GraphicView::setCrosshairType(RS2::CrosshairType chType){
grid->setCrosshairType(chType);
}
RS2::CrosshairType RS_GraphicView::getCrosshairType() const{
return grid->getCrosshairType();
}
/**
* Centers the drawing in x-direction.
*/
void RS_GraphicView::centerOffsetX() {
if (container && !zoomFrozen) {
offsetX = (int)(((getWidth()-borderLeft-borderRight)
- (container->getSize().x*factor.x))/2.0
- (container->getMin().x*factor.x)) + borderLeft;
}
}
/**
* Centers the drawing in y-direction.
*/
void RS_GraphicView::centerOffsetY() {
if (container && !zoomFrozen) {
offsetY = (int)((getHeight()-borderTop-borderBottom
- (container->getSize().y*factor.y))/2.0
- (container->getMin().y*factor.y)) + borderBottom;
}
}
/**
* Centers the given coordinate in the view in x-direction.
*/
void RS_GraphicView::centerX(double v) {
if (!zoomFrozen) {
offsetX = (int)((v*factor.x)
- (double)(getWidth()-borderLeft-borderRight)/2.0);
}
}
/**
* Centers the given coordinate in the view in y-direction.
*/
void RS_GraphicView::centerY(double v) {
if (!zoomFrozen) {
offsetY = (int)((v*factor.y)
- (double)(getHeight()-borderTop-borderBottom)/2.0);
}
}
/**
* @return Current action or nullptr.
*/
RS_ActionInterface* RS_GraphicView::getDefaultAction() {
if (eventHandler) {
return eventHandler->getDefaultAction();
} else {
return nullptr;
}
}
/**
* Sets the default action of the event handler.
*/
void RS_GraphicView::setDefaultAction(RS_ActionInterface* action) {
if (eventHandler) {
eventHandler->setDefaultAction(action);
}
}
/**
* @return Current action or nullptr.
*/
RS_ActionInterface* RS_GraphicView::getCurrentAction() {
if (eventHandler) {
return eventHandler->getCurrentAction();
} else {
return nullptr;
}
}
/**
* Sets the current action of the event handler.
*/
void RS_GraphicView::setCurrentAction(RS_ActionInterface* action) {
if (eventHandler) {
eventHandler->setCurrentAction(action);
}
}
/**
* Kills all running selection actions. Called when a selection action
* is launched to reduce confusion.
*/
void RS_GraphicView::killSelectActions() {
if (eventHandler) {
eventHandler->killSelectActions();
}
}
/**
* Kills all running actions.
*/
void RS_GraphicView::killAllActions() {
if (eventHandler) {
eventHandler->killAllActions();
}
}
/**
* Go back in menu or current action.
*/
void RS_GraphicView::back()
{
if (eventHandler && eventHandler->hasAction())
{
eventHandler->back();
}
}
/**
* Go forward with the current action.
*/
void RS_GraphicView::enter() {
if (eventHandler && eventHandler->hasAction()) {
eventHandler->enter();
}
}
/**
* Called by the actual GUI class which implements a command line.
*/
void RS_GraphicView::commandEvent(RS_CommandEvent* e) {
if (eventHandler) {
eventHandler->commandEvent(e);
}
}
/**
* Enables coordinate input in the command line.
*/
void RS_GraphicView::enableCoordinateInput() {
if (eventHandler) {
eventHandler->enableCoordinateInput();
}
}
/**
* Disables coordinate input in the command line.
*/
void RS_GraphicView::disableCoordinateInput() {
if (eventHandler) {
eventHandler->disableCoordinateInput();
}
}
/**
* zooms in by factor f
*/
void RS_GraphicView::zoomIn(double f, const RS_Vector& center) {
if (f<1.0e-6) {
RS_DEBUG->print(RS_Debug::D_WARNING,
"RS_GraphicView::zoomIn: invalid factor");
return;
}
RS_Vector c = center;
if (!c.valid) {
//find mouse position
c= getMousePosition();
}
zoomWindow(
toGraph(0, 0)
.scale(c, RS_Vector(1.0/f,1.0/f)),
toGraph(getWidth(), getHeight())
.scale(c, RS_Vector(1.0/f,1.0/f)));
//adjustOffsetControls();
//adjustZoomControls();
// updateGrid();
redraw();
}
/**
* zooms in by factor f in x
*/
void RS_GraphicView::zoomInX(double f) {
factor.x*=f;
offsetX=(int)((offsetX-getWidth()/2)*f)+getWidth()/2;
adjustOffsetControls();
adjustZoomControls();
// updateGrid();
redraw();
}
/**
* zooms in by factor f in y
*/
void RS_GraphicView::zoomInY(double f) {
factor.y*=f;
offsetY=(int)((offsetY-getHeight()/2)*f)+getHeight()/2;
adjustOffsetControls();
adjustZoomControls();
// updateGrid();
redraw();
}
/**
* zooms out by factor f
*/
void RS_GraphicView::zoomOut(double f, const RS_Vector& center) {
if (f<1.0e-6) {
RS_DEBUG->print(RS_Debug::D_WARNING,
"RS_GraphicView::zoomOut: invalid factor");
return;
}
zoomIn(1/f, center);
}
/**
* zooms out by factor f in x
*/
void RS_GraphicView::zoomOutX(double f) {
if (f<1.0e-6) {
RS_DEBUG->print(RS_Debug::D_WARNING,
"RS_GraphicView::zoomOutX: invalid factor");
return;
}
factor.x/=f;
offsetX=(int)(offsetX/f);
adjustOffsetControls();
adjustZoomControls();
// updateGrid();
redraw();
}
/**
* zooms out by factor f y
*/
void RS_GraphicView::zoomOutY(double f) {
if (f<1.0e-6) {
RS_DEBUG->print(RS_Debug::D_WARNING,
"RS_GraphicView::zoomOutY: invalid factor");
return;
}
factor.y/=f;
offsetY=(int)(offsetY/f);
adjustOffsetControls();
adjustZoomControls();
// updateGrid();
redraw();
}
/**
* performs autozoom
*
* @param axis include axis in zoom
* @param keepAspectRatio true: keep aspect ratio 1:1
* false: factors in x and y are stretched to the max
*/
#include <iostream>
void RS_GraphicView::zoomAuto(bool axis, bool keepAspectRatio) {
RS_DEBUG->print("RS_GraphicView::zoomAuto");
if (container) {
container->calculateBorders();
double sx, sy;
if (axis) {
auto const dV = container->getMax() - container->getMin();
sx = std::max(dV.x, 0.);
sy = std::max(dV.y, 0.);
} else {
sx = container->getSize().x;
sy = container->getSize().y;
}
// std::cout<<" RS_GraphicView::zoomAuto("<<sx<<","<<sy<<")"<<std::endl;
// std::cout<<" RS_GraphicView::zoomAuto("<<axis<<","<<keepAspectRatio<<")"<<std::endl;
double fx=1., fy=1.;
unsigned short fFlags=0;
if (sx>RS_TOLERANCE) {
fx = (getWidth()-borderLeft-borderRight) / sx;
} else {
fFlags += 1; //invalid x factor
}
if (sy>RS_TOLERANCE) {
fy = (getHeight()-borderTop-borderBottom) / sy;
} else {
fFlags += 2; //invalid y factor
}
// std::cout<<"0: fx= "<<fx<<"\tfy="<<fy<<std::endl;
RS_DEBUG->print("f: %f/%f", fx, fy);
switch(fFlags){
case 1:
fx=fy;
break;
case 2:
fy=fx;
break;
case 3:
return; //do not do anything, invalid factors
default:
if (keepAspectRatio) {
fx = fy = std::min(fx, fy);
}
// break;
}
// std::cout<<"1: fx= "<<fx<<"\tfy="<<fy<<std::endl;
RS_DEBUG->print("f: %f/%f", fx, fy);
//exclude invalid factors
fFlags=0;
if (fx<RS_TOLERANCE||fx>RS_MAXDOUBLE) {
fx=1.0;
fFlags += 1;
}
if (fy<RS_TOLERANCE||fy>RS_MAXDOUBLE) {
fy=1.0;
fFlags += 2;
}
if(fFlags == 3 ) return;
saveView();
// std::cout<<"2: fx= "<<fx<<"\tfy="<<fy<<std::endl;
setFactorX(fx);
setFactorY(fy);
RS_DEBUG->print("f: %f/%f", fx, fy);
// RS_DEBUG->print("adjustZoomControls");
adjustZoomControls();
// RS_DEBUG->print("centerOffsetX");
centerOffsetX();
// RS_DEBUG->print("centerOffsetY");
centerOffsetY();
// RS_DEBUG->print("adjustOffsetControls");
adjustOffsetControls();
// RS_DEBUG->print("updateGrid");
// updateGrid();
redraw();
}
RS_DEBUG->print("RS_GraphicView::zoomAuto OK");
}
/**
* Shows previous view.
*/
void RS_GraphicView::zoomPrevious() {
RS_DEBUG->print("RS_GraphicView::zoomPrevious");
if (container) {
restoreView();
}
}
/**
* Saves the current view as previous view to which we can
* switch back later with @see restoreView().
*/
void RS_GraphicView::saveView() {
if(getGraphic()) getGraphic()->setModified(true);
QDateTime noUpdateWindow=QDateTime::currentDateTime().addMSecs(-500);
//do not update view within 500 milliseconds
if(*previousViewTime > noUpdateWindow) return;
*previousViewTime = QDateTime::currentDateTime();
savedViews[savedViewIndex]=std::make_tuple(offsetX,offsetY,factor);
savedViewIndex = (savedViewIndex+1)%savedViews.size();
if(savedViewCount<savedViews.size()) savedViewCount++;
if(savedViewCount==1){
emit previous_zoom_state(true);
}
}
/**
* Restores the view previously saved with
* @see saveView().
*/
void RS_GraphicView::restoreView() {
if(savedViewCount == 0) return;
savedViewCount --;
if(savedViewCount==0){
emit previous_zoom_state(false);
}
savedViewIndex = (savedViewIndex + savedViews.size() - 1)%savedViews.size();
offsetX = std::get<0>(savedViews[savedViewIndex]);
offsetY = std::get<1>(savedViews[savedViewIndex]);
factor = std::get<2>(savedViews[savedViewIndex]);
adjustOffsetControls();
adjustZoomControls();
// updateGrid();
redraw();
}
/* *
* Function name:
* Description: Performs autozoom in Y axis only.
* Author(s): ..., Claude Sylvain
* Created: ?
* Last modified: 23 July 2011
*
* Parameters: bool axis:
* Axis in zoom.
*
* Returns: void
* */
void RS_GraphicView::zoomAutoY(bool axis) {
if (container) {
double visibleHeight = 0.0;
double minY = RS_MAXDOUBLE;
double maxY = RS_MINDOUBLE;
bool noChange = false;
for(auto e: *container){
if (e->rtti()==RS2::EntityLine) {
RS_Line* l = (RS_Line*)e;
double x1, x2;
x1 = toGuiX(l->getStartpoint().x);
x2 = toGuiX(l->getEndpoint().x);
if ( ((x1 > 0.0) && (x1 < (double) getWidth())) ||
((x2 > 0.0) && (x2 < (double) getWidth())))
{
minY = std::min(minY, l->getStartpoint().y);
minY = std::min(minY, l->getEndpoint().y);
maxY = std::max(maxY, l->getStartpoint().y);
maxY = std::max(maxY, l->getEndpoint().y);
}
}
}
if (axis) {
visibleHeight = std::max(maxY, 0.0) - std::min(minY, 0.0);
} else {
visibleHeight = maxY-minY;
}
if (visibleHeight<1.0) {
noChange = true;
}
double fy = 1.0;
if (visibleHeight>1.0e-6) {
fy = (getHeight()-borderTop-borderBottom)
/ visibleHeight;
if (factor.y<0.000001) {
noChange = true;
}
}
if (noChange==false) {
setFactorY(fy);
//centerOffsetY();
offsetY = (int)((getHeight()-borderTop-borderBottom
- (visibleHeight*factor.y))/2.0
- (minY*factor.y)) + borderBottom;
adjustOffsetControls();
adjustZoomControls();
// updateGrid();
}
RS_DEBUG->print("Auto zoom y ok");
}
}
/**
* Zooms the area given by v1 and v2.
*
* @param keepAspectRatio true: keeps the aspect ratio 1:1
* false: zooms exactly the selected range to the
* current graphic view
*/
void RS_GraphicView::zoomWindow(RS_Vector v1, RS_Vector v2,
bool keepAspectRatio) {
double zoomX=480.0; // Zoom for X-Axis
double zoomY=640.0; // Zoom for Y-Axis (Set smaller one)
int zoomBorder = 0;
// Switch left/right and top/bottom is necessary:
if(v1.x>v2.x) {
std::swap(v1.x,v2.x);
}
if(v1.y>v2.y) {
std::swap(v1.y,v2.y);
}
// Get zoom in X and zoom in Y:
if(v2.x-v1.x>1.0e-6) {
zoomX = getWidth() / (v2.x-v1.x);
}
if(v2.y-v1.y>1.0e-6) {
zoomY = getHeight() / (v2.y-v1.y);
}
// Take smaller zoom:
if (keepAspectRatio) {
if(zoomX<zoomY) {
if(getWidth()!=0) {
zoomX = zoomY = ((double)(getWidth()-2*zoomBorder)) /
(double)getWidth()*zoomX;
}
} else {
if(getHeight()!=0) {
zoomX = zoomY = ((double)(getHeight()-2*zoomBorder)) /
(double)getHeight()*zoomY;
}
}
}
zoomX=std::abs(zoomX);
zoomY=std::abs(zoomY);
// Borders in pixel after zoom
int pixLeft =(int)(v1.x*zoomX);
int pixTop =(int)(v2.y*zoomY);
int pixRight =(int)(v2.x*zoomX);
int pixBottom=(int)(v1.y*zoomY);
if( pixLeft == INT_MIN || pixLeft== INT_MAX ||
pixRight == INT_MIN || pixRight== INT_MAX ||
pixTop == INT_MIN || pixTop== INT_MAX ||
pixBottom == INT_MIN || pixBottom== INT_MAX ) {
RS_DIALOGFACTORY->commandMessage("Requested zooming factor out of range. Zooming not changed");
return;
}
saveView();
// Set new offset for zero point:
offsetX = - pixLeft + (getWidth() -pixRight +pixLeft)/2;
offsetY = - pixTop + (getHeight() -pixBottom +pixTop)/2;
factor.x = zoomX;
factor.y = zoomY;
adjustOffsetControls();
adjustZoomControls();
// updateGrid();
redraw();
}
/**
* Centers the point v1.
*/
void RS_GraphicView::zoomPan(int dx, int dy) {
//offsetX+=(int)toGuiDX(v1.x);
//offsetY+=(int)toGuiDY(v1.y);
offsetX += dx;
offsetY -= dy;
adjustOffsetControls();
//adjustZoomControls();
// updateGrid();
redraw();
}
/**
* Scrolls in the given direction.
*/
void RS_GraphicView::zoomScroll(RS2::Direction direction) {
switch (direction) {
case RS2::Up:
offsetY-=50;
break;
case RS2::Down:
offsetY+=50;
break;
case RS2::Right:
offsetX+=50;
break;
case RS2::Left:
offsetX-=50;
break;
}
adjustOffsetControls();
adjustZoomControls();
// updateGrid();
redraw();
}
/**
* Zooms to page extends.
*/
void RS_GraphicView::zoomPage() {
RS_DEBUG->print("RS_GraphicView::zoomPage");
if (!container) {
return;
}
RS_Graphic* graphic = container->getGraphic();
if (!graphic) {
return;
}
RS_Vector s = graphic->getPrintAreaSize()/graphic->getPaperScale();
double fx, fy;
if (s.x>RS_TOLERANCE) {
fx = (getWidth()-borderLeft-borderRight) / s.x;
} else {
fx = 1.0;
}
if (s.y>RS_TOLERANCE) {
fy = (getHeight()-borderTop-borderBottom) / s.y;
} else {
fy = 1.0;
}
RS_DEBUG->print("f: %f/%f", fx, fy);
fx = fy = std::min(fx, fy);
RS_DEBUG->print("f: %f/%f", fx, fy);
if (fx<RS_TOLERANCE) {
fx=fy=1.0;
}
setFactorX(fx);
setFactorY(fy);
RS_DEBUG->print("f: %f/%f", fx, fy);
centerOffsetX();
centerOffsetY();
adjustOffsetControls();
adjustZoomControls();
// updateGrid();
redraw();
}
/**
* Draws the entities within the given range.
*/
void RS_GraphicView::drawWindow_DEPRECATED(RS_Vector v1, RS_Vector v2) {
RS_DEBUG->print("RS_GraphicView::drawWindow() begin");
if (container) {
for(auto se: *container){
if (se->isInWindow(v1, v2)) {
drawEntity(nullptr, se);
}
}
}
RS_DEBUG->print("RS_GraphicView::drawWindow() end");
}
/**
* Draws the entities.
* This function can only be called from within the paint event
*
*/
void RS_GraphicView::drawLayer1(RS_Painter *painter) {
// drawing paper border:
if (isPrintPreview()) {
drawPaper(painter);
}
// drawing meta grid:
if (!isPrintPreview()) {
//increase grid point size on for DPI>96
auto dpiX = int(qApp->screens().front()->logicalDotsPerInch());
const bool isHiDpi = dpiX > 96;
// DEBUG_HEADER
// RS_DEBUG->print(RS_Debug::D_ERROR, "dpiX=%d\n",dpiX);
const RS_Pen penSaved=painter->getPen();
if(isHiDpi) {
RS_Pen pen=penSaved;
pen.setWidth(RS2::Width01);
painter->setPen(pen);
}
//only drawGrid updates the grid layout (updatePointArray())
drawMetaGrid(painter);
//draw grid after metaGrid to avoid overwriting grid points by metaGrid lines
//bug# 3430258
drawGrid(painter);
if(isDraftMode())
drawDraftSign(painter);
if(isHiDpi)
painter->setPen(penSaved);
}
}
/* *
* Function name:
* Description: Do the drawing, step 2/3.
* Author(s): ..., Claude Sylvain
* Created: ?
* Last modified: 23 July 2011
*
* Parameters: RS_Painter *painter:
* ...
*
* Returns: void
* */
void RS_GraphicView::drawLayer2(RS_Painter *painter)
{
drawEntity(painter, container); // Draw all entities.
// If not in print preview, draw the absolute zero reference.
// ----------------------------------------------------------
if (!isPrintPreview())
drawAbsoluteZero(painter);
}
void RS_GraphicView::drawLayer3(RS_Painter *painter) {
// drawing zero points:
if (!isPrintPreview()) {
drawRelativeZero(painter);
drawOverlay(painter);
}
}
/* *
* Function name:
*
* Description: - Sets the pen of the painter object to the suitable pen
* for the given entity.
*
* Author(s): ..., Claude Sylvain
* Created: ?
* Last modified: 17 November 2011
*
* Parameters: RS_Painter *painter:
* ...
*
* RS_Entity *e:
* ...
*
* Returns: void
*/
void RS_GraphicView::setPenForEntity(RS_Painter *painter,RS_Entity *e, double& patternOffset)
{
if (draftMode) {