/
qgssnappingwidget.cpp
857 lines (762 loc) · 33.7 KB
/
qgssnappingwidget.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
/***************************************************************************
qgssnappingwidget.cpp
begin : August 2016
copyright : (C) 2016 Denis Rouzaud
email : denis.rouzaud@gmail.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <QAction>
#include <QComboBox>
#include <QFont>
#include <QHBoxLayout>
#include <QHeaderView>
#include <QLabel>
#include <QMenu>
#include <QToolBar>
#include <QToolButton>
#include <QWidgetAction>
#include <QCheckBox>
#include "qgisapp.h"
#include "qgsapplication.h"
#include "qgsdoublespinbox.h"
#include "qgsfloatingwidget.h"
#include "qgslayertreegroup.h"
#include "qgslayertree.h"
#include "qgslayertreeview.h"
#include "qgsmapcanvas.h"
#include "qgsmaplayer.h"
#include "qgsproject.h"
#include "qgssnappingconfig.h"
#include "qgssnappinglayertreemodel.h"
#include "qgssnappingwidget.h"
#include "qgsunittypes.h"
#include "qgssettings.h"
#include "qgsscalewidget.h"
#ifdef ENABLE_MODELTEST
#include "modeltest.h"
#endif
class SnapTypeMenu: public QMenu
{
public:
SnapTypeMenu( const QString &title, QWidget *parent = nullptr )
: QMenu( title, parent ) {}
void mouseReleaseEvent( QMouseEvent *e )
{
QAction *action = activeAction();
if ( action )
action->trigger();
else
QMenu::mouseReleaseEvent( e );
}
};
QgsSnappingWidget::QgsSnappingWidget( QgsProject *project, QgsMapCanvas *canvas, QWidget *parent )
: QWidget( parent )
, mProject( project )
, mConfig( project )
, mCanvas( canvas )
{
// detect the type of display
QToolBar *tb = qobject_cast<QToolBar *>( parent );
if ( tb )
{
mDisplayMode = ToolBar;
setObjectName( QStringLiteral( "SnappingOptionToolBar" ) );
}
else
{
mDisplayMode = Widget;
setObjectName( QStringLiteral( "SnappingOptionDialog" ) );
}
// Advanced config layer tree view
mAdvancedConfigWidget = new QWidget( this );
QVBoxLayout *advancedLayout = new QVBoxLayout();
if ( mDisplayMode == Widget )
advancedLayout->setContentsMargins( 0, 0, 0, 0 );
// tree view
mLayerTreeView = new QTreeView();
QgsSnappingLayerTreeModel *model = new QgsSnappingLayerTreeModel( mProject, mCanvas, this );
model->setLayerTreeModel( new QgsLayerTreeModel( mProject->layerTreeRoot(), model ) );
#ifdef ENABLE_MODELTEST
new ModelTest( model, this );
new ModelTest( model->layerTreeModel(), this );
#endif
// connections
connect( model, &QgsSnappingLayerTreeModel::rowsInserted, this, &QgsSnappingWidget::onSnappingTreeLayersChanged );
connect( model, &QgsSnappingLayerTreeModel::modelReset, this, &QgsSnappingWidget::onSnappingTreeLayersChanged );
connect( model, &QgsSnappingLayerTreeModel::rowsRemoved, this, &QgsSnappingWidget::onSnappingTreeLayersChanged );
connect( mProject, &QObject::destroyed, this, [ = ] {mLayerTreeView->setModel( nullptr );} );
// model->setFlags( 0 );
mLayerTreeView->setModel( model );
mLayerTreeView->resizeColumnToContents( 0 );
mLayerTreeView->header()->show();
mLayerTreeView->setSelectionMode( QAbstractItemView::NoSelection );
// item delegates
mLayerTreeView->setEditTriggers( QAbstractItemView::AllEditTriggers );
mLayerTreeView->setItemDelegate( new QgsSnappingLayerDelegate( mCanvas, this ) );
mLayerTreeView->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding );
mLayerTreeView->setMinimumWidth( 500 );
mLayerTreeView->resizeColumnToContents( 0 );
// filter line edit
QHBoxLayout *filterLayout = new QHBoxLayout();
filterLayout->setContentsMargins( 0, 0, 0, 0 );
filterLayout->addStretch();
QgsFilterLineEdit *filterLineEdit = new QgsFilterLineEdit();
filterLineEdit->setShowClearButton( true );
filterLineEdit->setShowSearchIcon( true );
filterLineEdit->setPlaceholderText( tr( "Filter layers…" ) );
connect( filterLineEdit, &QgsFilterLineEdit::textChanged, model, &QgsSnappingLayerTreeModel::setFilterText );
filterLayout->addStretch();
filterLayout->addWidget( filterLineEdit );
advancedLayout->addWidget( mLayerTreeView );
advancedLayout->addLayout( filterLayout );
mAdvancedConfigWidget->setLayout( advancedLayout );
// enable button
mEnabledAction = new QAction( tr( "Toggle Snapping" ), this );
mEnabledAction->setCheckable( true );
mEnabledAction->setIcon( QIcon( QgsApplication::getThemeIcon( "/mIconSnapping.svg" ) ) );
mEnabledAction->setToolTip( tr( "Enable Snapping (S)" ) );
mEnabledAction->setShortcut( tr( "S", "Keyboard shortcut: toggle snapping" ) );
mEnabledAction->setObjectName( QStringLiteral( "EnableSnappingAction" ) );
connect( mEnabledAction, &QAction::toggled, this, &QgsSnappingWidget::enableSnapping );
// avoid intersection mode button
mAvoidIntersectionsModeButton = new QToolButton();
mAvoidIntersectionsModeButton->setToolTip( tr( "When avoid overlap is enabled, digitized features will be clipped to not overlapped existing ones." ) );
mAvoidIntersectionsModeButton->setPopupMode( QToolButton::InstantPopup );
QMenu *avoidIntersectionsModeMenu = new QMenu( tr( "Set Avoid Overlap Mode" ), this );
mAllowIntersectionsAction = new QAction( QIcon( QgsApplication::getThemeIcon( "/mActionAllowIntersections.svg" ) ), tr( "Allow Overlap" ), avoidIntersectionsModeMenu );
mAvoidIntersectionsCurrentLayerAction = new QAction( QIcon( QgsApplication::getThemeIcon( "/mActionAvoidIntersectionsCurrentLayer.svg" ) ), tr( "Avoid Overlap on Active Layer" ), avoidIntersectionsModeMenu );
mAvoidIntersectionsLayersAction = new QAction( QIcon( QgsApplication::getThemeIcon( "/mActionAvoidIntersectionsLayers.svg" ) ), tr( "Follow Advanced Configuration" ), avoidIntersectionsModeMenu );
avoidIntersectionsModeMenu->addAction( mAllowIntersectionsAction );
avoidIntersectionsModeMenu->addAction( mAvoidIntersectionsCurrentLayerAction );
avoidIntersectionsModeMenu->addAction( mAvoidIntersectionsLayersAction );
mAvoidIntersectionsModeButton->setMenu( avoidIntersectionsModeMenu );
mAvoidIntersectionsModeButton->setObjectName( QStringLiteral( "AvoidIntersectionsModeButton" ) );
if ( mDisplayMode == Widget )
{
mAvoidIntersectionsModeButton->setToolButtonStyle( Qt::ToolButtonTextBesideIcon );
}
connect( mAvoidIntersectionsModeButton, &QToolButton::triggered, this, &QgsSnappingWidget::avoidIntersectionsModeButtonTriggered );
// mode button
mModeButton = new QToolButton();
mModeButton->setToolTip( tr( "Snapping Mode" ) );
mModeButton->setPopupMode( QToolButton::InstantPopup );
QMenu *modeMenu = new QMenu( tr( "Set Snapping Mode" ), this );
mAllLayersAction = new QAction( QIcon( QgsApplication::getThemeIcon( "/mIconSnappingAllLayers.svg" ) ), tr( "All Layers" ), modeMenu );
mActiveLayerAction = new QAction( QIcon( QgsApplication::getThemeIcon( "/mIconSnappingActiveLayer.svg" ) ), tr( "Active Layer" ), modeMenu );
mAdvancedModeAction = new QAction( QIcon( QgsApplication::getThemeIcon( "/mIconSnappingAdvanced.svg" ) ), tr( "Advanced Configuration" ), modeMenu );
modeMenu->addAction( mAllLayersAction );
modeMenu->addAction( mActiveLayerAction );
modeMenu->addAction( mAdvancedModeAction );
if ( mDisplayMode == ToolBar )
{
modeMenu->addSeparator();
QAction *openDialogAction = new QAction( tr( "Open Snapping Options…" ), modeMenu );
connect( openDialogAction, &QAction::triggered, QgisApp::instance(), &QgisApp::snappingOptions );
modeMenu->addAction( openDialogAction );
}
mModeButton->setMenu( modeMenu );
mModeButton->setObjectName( QStringLiteral( "SnappingModeButton" ) );
if ( mDisplayMode == Widget )
{
mModeButton->setToolButtonStyle( Qt::ToolButtonTextBesideIcon );
}
connect( mModeButton, &QToolButton::triggered, this, &QgsSnappingWidget::modeButtonTriggered );
// type button
mTypeButton = new QToolButton();
mTypeButton->setToolTip( tr( "Snapping Type" ) );
mTypeButton->setPopupMode( QToolButton::InstantPopup );
SnapTypeMenu *typeMenu = new SnapTypeMenu( tr( "Set Snapping Mode" ), this );
mVertexAction = new QAction( QIcon( QgsApplication::getThemeIcon( "/mIconSnappingVertex.svg" ) ), tr( "Vertex" ), typeMenu );
mSegmentAction = new QAction( QIcon( QgsApplication::getThemeIcon( "/mIconSnappingSegment.svg" ) ), tr( "Segment" ), typeMenu );
mAreaAction = new QAction( QIcon( QgsApplication::getThemeIcon( "/mIconSnappingArea.svg" ) ), tr( "Area" ), typeMenu );
mCentroidAction = new QAction( QIcon( QgsApplication::getThemeIcon( "/mIconSnappingCentroid.svg" ) ), tr( "Centroid" ), typeMenu );
mMiddleAction = new QAction( QIcon( QgsApplication::getThemeIcon( "/mIconSnappingMiddle.svg" ) ), tr( "Middle of Segments" ), typeMenu );
mVertexAction->setCheckable( true );
mSegmentAction->setCheckable( true );
mAreaAction->setCheckable( true );
mCentroidAction->setCheckable( true );
mMiddleAction->setCheckable( true );
typeMenu->addAction( mVertexAction );
typeMenu->addAction( mSegmentAction );
typeMenu->addAction( mAreaAction );
typeMenu->addAction( mCentroidAction );
typeMenu->addAction( mMiddleAction );
mTypeButton->setMenu( typeMenu );
mTypeButton->setObjectName( QStringLiteral( "SnappingTypeButton" ) );
if ( mDisplayMode == Widget )
{
mTypeButton->setToolButtonStyle( Qt::ToolButtonTextBesideIcon );
}
connect( mTypeButton, &QToolButton::triggered, this, &QgsSnappingWidget::typeButtonTriggered );
// tolerance
mToleranceSpinBox = new QDoubleSpinBox();
mToleranceSpinBox->setDecimals( 5 );
mToleranceSpinBox->setMaximum( 99999999.990000 );
mToleranceSpinBox->setToolTip( tr( "Snapping Tolerance in Defined Units" ) );
mToleranceSpinBox->setObjectName( QStringLiteral( "SnappingToleranceSpinBox" ) );
connect( mToleranceSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsSnappingWidget::changeTolerance );
// units
mUnitsComboBox = new QComboBox();
mUnitsComboBox->addItem( tr( "px" ), QgsTolerance::Pixels );
// Get canvas units
const QString mapCanvasDistanceUnits { QgsUnitTypes::toString( mCanvas->mapSettings().mapUnits() ) };
mUnitsComboBox->addItem( mapCanvasDistanceUnits, QgsTolerance::ProjectUnits );
mUnitsComboBox->setToolTip( tr( "Snapping Unit Type: Pixels (px) or Project/Map Units (%1)" ).arg( mapCanvasDistanceUnits ) );
mUnitsComboBox->setObjectName( QStringLiteral( "SnappingUnitComboBox" ) );
connect( mUnitsComboBox, qgis::overload<int>::of( &QComboBox::currentIndexChanged ),
this, &QgsSnappingWidget::changeUnit );
connect( mCanvas, &QgsMapCanvas::destinationCrsChanged, this, [ = ]
{
// Update map units from canvas
const QString mapCanvasDistanceUnits { QgsUnitTypes::toString( mCanvas->mapSettings().mapUnits() ) };
mUnitsComboBox->setItemText( 1, mapCanvasDistanceUnits );
mUnitsComboBox->setToolTip( tr( "Snapping Unit Type: Pixels (px) or Map Units (%1)" ).arg( mapCanvasDistanceUnits ) );
model->resetLayerTreeModel();
} );
// topological editing button
mTopologicalEditingAction = new QAction( tr( "Topological Editing" ), this );
mTopologicalEditingAction->setCheckable( true );
mTopologicalEditingAction->setIcon( QIcon( QgsApplication::getThemeIcon( "/mIconTopologicalEditing.svg" ) ) );
mTopologicalEditingAction->setToolTip( tr( "Enable Topological Editing" ) );
mTopologicalEditingAction->setObjectName( QStringLiteral( "TopologicalEditingAction" ) );
connect( mTopologicalEditingAction, &QAction::toggled, this, &QgsSnappingWidget::enableTopologicalEditing );
// snapping on intersection button
mIntersectionSnappingAction = new QAction( tr( "Snapping on Intersection" ), this );
mIntersectionSnappingAction->setCheckable( true );
mIntersectionSnappingAction->setIcon( QIcon( QgsApplication::getThemeIcon( "/mIconSnappingIntersection.svg" ) ) );
mIntersectionSnappingAction->setToolTip( tr( "Enable Snapping on Intersection" ) );
mIntersectionSnappingAction->setObjectName( QStringLiteral( "IntersectionSnappingAction" ) );
connect( mIntersectionSnappingAction, &QAction::toggled, this, &QgsSnappingWidget::enableIntersectionSnapping );
// snapping on intersection button
mEnableTracingAction = new QAction( tr( "Enable Tracing" ), this );
mEnableTracingAction->setCheckable( true );
mEnableTracingAction->setIcon( QIcon( QgsApplication::getThemeIcon( "/mActionTracing.svg" ) ) );
mEnableTracingAction->setToolTip( tr( "Enable Tracing (T)" ) );
mEnableTracingAction->setShortcut( tr( "T", "Keyboard shortcut: Enable tracing" ) );
mEnableTracingAction->setObjectName( QStringLiteral( "EnableTracingAction" ) );
// tracing offset
mTracingOffsetSpinBox = new QgsDoubleSpinBox;
mTracingOffsetSpinBox->setRange( -1000000, 1000000 );
mTracingOffsetSpinBox->setDecimals( 6 );
mTracingOffsetSpinBox->setClearValue( 0 );
mTracingOffsetSpinBox->setClearValueMode( QgsDoubleSpinBox::ClearValueMode::CustomValue );
// Note: set to false to "fix" a crash
// See https://github.com/qgis/QGIS/pull/8266 for some more context
mTracingOffsetSpinBox->setShowClearButton( false );
QMenu *tracingMenu = new QMenu( this );
QWidgetAction *widgetAction = new QWidgetAction( tracingMenu );
QVBoxLayout *tracingWidgetLayout = new QVBoxLayout;
tracingWidgetLayout->addWidget( new QLabel( "Offset" ) );
tracingWidgetLayout->addWidget( mTracingOffsetSpinBox );
QWidget *tracingWidget = new QWidget;
tracingWidget->setLayout( tracingWidgetLayout );
widgetAction->setDefaultWidget( tracingWidget );
tracingMenu->addAction( widgetAction );
mEnableTracingAction->setMenu( tracingMenu );
// self-snapping button
mSelfSnappingAction = new QAction( tr( "Self-snapping" ), this );
mSelfSnappingAction->setCheckable( true );
mSelfSnappingAction->setIcon( QIcon( QgsApplication::getThemeIcon( "/mIconSnappingSelf.svg" ) ) );
mSelfSnappingAction->setToolTip( tr( "If self snapping is enabled, snapping will also take the current state of the digitized feature into consideration." ) );
mSelfSnappingAction->setObjectName( QStringLiteral( "SelfSnappingAction" ) );
connect( mSelfSnappingAction, &QAction::toggled, this, &QgsSnappingWidget::enableSelfSnapping );
// layout
if ( mDisplayMode == ToolBar )
{
// hiding widget in a toolbar is not possible, actions are required
tb->addAction( mEnabledAction );
mModeAction = tb->addWidget( mModeButton );
// edit advanced config button
QToolButton *advConfigButton = new QToolButton( this );
advConfigButton->setPopupMode( QToolButton::InstantPopup );
QMenu *advConfigMenu = new QMenu( this );
QWidgetAction *advConfigWidgetAction = new QWidgetAction( advConfigMenu );
advConfigWidgetAction->setDefaultWidget( mAdvancedConfigWidget );
advConfigMenu->addAction( advConfigWidgetAction );
advConfigButton->setIcon( QIcon( QgsApplication::getThemeIcon( "/mActionShowAllLayers.svg" ) ) );
advConfigButton->setToolTip( tr( "Edit advanced configuration" ) );
advConfigButton->setObjectName( QStringLiteral( "EditAdvancedConfigurationButton" ) );
advConfigButton->setMenu( advConfigMenu );
mEditAdvancedConfigAction = tb->addWidget( advConfigButton );
// other buttons / actions
mTypeAction = tb->addWidget( mTypeButton );
mToleranceAction = tb->addWidget( mToleranceSpinBox );
mUnitAction = tb->addWidget( mUnitsComboBox );
tb->addAction( mTopologicalEditingAction );
mAvoidIntersectionsModeAction = tb->addWidget( mAvoidIntersectionsModeButton );
tb->addAction( mIntersectionSnappingAction );
tb->addAction( mEnableTracingAction );
tb->addAction( mSelfSnappingAction );
}
else
{
mMinScaleWidget = new QgsScaleWidget();
mMinScaleWidget->setToolTip( tr( "Minimum scale from which snapping is enabled (i.e. most \"zoomed out\" scale)" ) );
mMinScaleWidget->setObjectName( QStringLiteral( "SnappingMinScaleSpinBox" ) );
connect( mMinScaleWidget, &QgsScaleWidget::scaleChanged, this, &QgsSnappingWidget::changeMinScale );
mMaxScaleWidget = new QgsScaleWidget();
mMaxScaleWidget->setToolTip( tr( "Maximum scale up to which snapping is enabled (i.e. most \"zoomed in\" scale)" ) );
mMaxScaleWidget->setObjectName( QStringLiteral( "SnappingMaxScaleSpinBox" ) );
connect( mMaxScaleWidget, &QgsScaleWidget::scaleChanged, this, &QgsSnappingWidget::changeMaxScale );
mSnappingScaleModeButton = new QToolButton();
mSnappingScaleModeButton->setToolTip( tr( "Snapping scale mode" ) );
mSnappingScaleModeButton->setPopupMode( QToolButton::InstantPopup );
QMenu *scaleModeMenu = new QMenu( tr( "Set snapping scale mode" ), this );
mDefaultSnappingScaleAct = new QAction( QIcon( QgsApplication::getThemeIcon( "/mIconSnappingOnScale.svg" ) ), tr( "Disabled" ), scaleModeMenu );
mDefaultSnappingScaleAct->setToolTip( tr( "Scale dependency disabled" ) );
mGlobalSnappingScaleAct = new QAction( QIcon( QgsApplication::getThemeIcon( "/mIconSnappingOnScale.svg" ) ), tr( "Global" ), scaleModeMenu );
mGlobalSnappingScaleAct->setToolTip( tr( "Scale dependency global" ) );
mPerLayerSnappingScaleAct = new QAction( QIcon( QgsApplication::getThemeIcon( "/mIconSnappingOnScale.svg" ) ), tr( "Per layer" ), scaleModeMenu );
mPerLayerSnappingScaleAct->setToolTip( tr( "Scale dependency per layer" ) );
scaleModeMenu->addAction( mDefaultSnappingScaleAct );
scaleModeMenu->addAction( mGlobalSnappingScaleAct );
scaleModeMenu->addAction( mPerLayerSnappingScaleAct );
mSnappingScaleModeButton->setMenu( scaleModeMenu );
mSnappingScaleModeButton->setObjectName( QStringLiteral( "SnappingScaleModeButton" ) );
mSnappingScaleModeButton->setToolButtonStyle( Qt::ToolButtonTextBesideIcon );
connect( mSnappingScaleModeButton, &QToolButton::triggered, this, &QgsSnappingWidget::snappingScaleModeTriggered );
// mode = widget
QHBoxLayout *layout = new QHBoxLayout();
QToolButton *enabledButton = new QToolButton();
enabledButton->addAction( mEnabledAction );
enabledButton->setDefaultAction( mEnabledAction );
layout->addWidget( enabledButton );
layout->addWidget( mModeButton );
layout->addWidget( mTypeButton );
layout->addWidget( mToleranceSpinBox );
layout->addWidget( mUnitsComboBox );
mSnappingScaleModeButton->setDefaultAction( mDefaultSnappingScaleAct );
layout->addWidget( mSnappingScaleModeButton );
layout->addWidget( mMinScaleWidget );
layout->addWidget( mMaxScaleWidget );
QToolButton *topoButton = new QToolButton();
topoButton->addAction( mTopologicalEditingAction );
topoButton->setDefaultAction( mTopologicalEditingAction );
topoButton->setToolButtonStyle( Qt::ToolButtonTextBesideIcon );
layout->addWidget( topoButton );
layout->addWidget( mAvoidIntersectionsModeButton );
QToolButton *interButton = new QToolButton();
interButton->addAction( mIntersectionSnappingAction );
interButton->setDefaultAction( mIntersectionSnappingAction );
interButton->setToolButtonStyle( Qt::ToolButtonTextBesideIcon );
layout->addWidget( interButton );
QToolButton *selfsnapButton = new QToolButton();
selfsnapButton->addAction( mSelfSnappingAction );
selfsnapButton->setDefaultAction( mSelfSnappingAction );
selfsnapButton->setToolButtonStyle( Qt::ToolButtonTextBesideIcon );
layout->addWidget( selfsnapButton );
layout->setContentsMargins( 0, 0, 0, 0 );
layout->setAlignment( Qt::AlignRight );
layout->setSpacing( mDisplayMode == Widget ? 3 : 0 );
QGridLayout *topLayout = new QGridLayout();
topLayout->addLayout( layout, 0, 0, Qt::AlignLeft | Qt::AlignTop );
topLayout->addWidget( mAdvancedConfigWidget, 1, 0 );
setLayout( topLayout );
}
// connect settings changed and map units changed to properly update the widget
connect( project, &QgsProject::snappingConfigChanged, this, &QgsSnappingWidget::projectSnapSettingsChanged );
connect( project, &QgsProject::topologicalEditingChanged, this, &QgsSnappingWidget::projectTopologicalEditingChanged );
connect( project, &QgsProject::avoidIntersectionsModeChanged, this, &QgsSnappingWidget::projectAvoidIntersectionModeChanged );
connect( mCanvas, &QgsMapCanvas::destinationCrsChanged, this, &QgsSnappingWidget::updateToleranceDecimals );
// Slightly modify the config so the settings changed code doesn't early exit
mConfig = project->snappingConfig();
mConfig.setEnabled( !mConfig.enabled() );
projectSnapSettingsChanged();
// modeChanged determines if widget are visible or not based on mode
modeChanged();
updateToleranceDecimals();
bool defaultSnapEnabled = QgsSettings().value( QStringLiteral( "/qgis/digitizing/default_snap_enabled" ), false ).toBool();
enableSnapping( defaultSnapEnabled );
restoreGeometry( QgsSettings().value( QStringLiteral( "/Windows/SnappingWidget/geometry" ) ).toByteArray() );
}
QgsSnappingWidget::~QgsSnappingWidget()
{
if ( mDisplayMode == Widget )
{
QgsSettings().setValue( QStringLiteral( "/Windows/SnappingWidget/geometry" ), saveGeometry() );
}
}
void QgsSnappingWidget::projectSnapSettingsChanged()
{
QgsSnappingConfig config = mProject->snappingConfig();
if ( mConfig == config )
return;
mConfig = config;
mEnabledAction->setChecked( config.enabled() );
if ( config.mode() == QgsSnappingConfig::AllLayers && mModeButton->defaultAction() != mAllLayersAction )
{
mModeButton->setDefaultAction( mAllLayersAction );
modeChanged();
updateToleranceDecimals();
}
if ( config.mode() == QgsSnappingConfig::ActiveLayer && mModeButton->defaultAction() != mActiveLayerAction )
{
mModeButton->setDefaultAction( mActiveLayerAction );
modeChanged();
updateToleranceDecimals();
}
if ( config.mode() == QgsSnappingConfig::AdvancedConfiguration && mModeButton->defaultAction() != mAdvancedModeAction )
{
mModeButton->setDefaultAction( mAdvancedModeAction );
modeChanged();
updateToleranceDecimals();
}
// Clear
mVertexAction->setChecked( false );
mSegmentAction->setChecked( false );
mAreaAction->setChecked( false );
mCentroidAction->setChecked( false );
mMiddleAction->setChecked( false );
if ( config.typeFlag() & QgsSnappingConfig::VertexFlag )
{
mTypeButton->setDefaultAction( mVertexAction );
mVertexAction->setChecked( true );
}
if ( config.typeFlag() & QgsSnappingConfig::SegmentFlag )
{
mTypeButton->setDefaultAction( mSegmentAction );
mSegmentAction->setChecked( true );
}
if ( config.typeFlag() & QgsSnappingConfig::AreaFlag )
{
mTypeButton->setDefaultAction( mAreaAction );
mAreaAction->setChecked( true );
}
if ( config.typeFlag() & QgsSnappingConfig::CentroidFlag )
{
mTypeButton->setDefaultAction( mCentroidAction );
mCentroidAction->setChecked( true );
}
if ( config.typeFlag() & QgsSnappingConfig::MiddleOfSegmentFlag )
{
mTypeButton->setDefaultAction( mMiddleAction );
mMiddleAction->setChecked( true );
}
if ( static_cast<QgsTolerance::UnitType>( mUnitsComboBox->currentData().toInt() ) != config.units() )
{
mUnitsComboBox->setCurrentIndex( mUnitsComboBox->findData( config.units() ) );
}
if ( mToleranceSpinBox->value() != config.tolerance() )
{
mToleranceSpinBox->setValue( config.tolerance() );
}
if ( mMinScaleWidget && mMinScaleWidget->scale() != config.minimumScale() )
{
mMinScaleWidget->setScale( config.minimumScale() );
}
if ( mMaxScaleWidget && mMaxScaleWidget->scale() != config.maximumScale() )
{
mMaxScaleWidget->setScale( config.maximumScale() );
}
if ( mSnappingScaleModeButton && config.scaleDependencyMode() == QgsSnappingConfig::Disabled )
{
mSnappingScaleModeButton->setDefaultAction( mDefaultSnappingScaleAct );
}
else if ( mSnappingScaleModeButton && config.scaleDependencyMode() == QgsSnappingConfig::Global )
{
mSnappingScaleModeButton->setDefaultAction( mGlobalSnappingScaleAct );
}
else if ( mSnappingScaleModeButton && config.scaleDependencyMode() == QgsSnappingConfig::PerLayer )
{
mSnappingScaleModeButton->setDefaultAction( mPerLayerSnappingScaleAct );
}
if ( config.intersectionSnapping() != mIntersectionSnappingAction->isChecked() )
{
mIntersectionSnappingAction->setChecked( config.intersectionSnapping() );
}
if ( config.selfSnapping() != mSelfSnappingAction->isChecked() )
{
mSelfSnappingAction->setChecked( config.selfSnapping() );
}
toggleSnappingWidgets( config.enabled() );
}
void QgsSnappingWidget::projectAvoidIntersectionModeChanged()
{
switch ( mProject->avoidIntersectionsMode() )
{
case QgsProject::AvoidIntersectionsMode::AllowIntersections:
mAvoidIntersectionsModeButton->setDefaultAction( mAllowIntersectionsAction );
mAllowIntersectionsAction->setChecked( true );
mAvoidIntersectionsCurrentLayerAction->setChecked( false );
mAvoidIntersectionsLayersAction->setChecked( false );
break;
case QgsProject::AvoidIntersectionsMode::AvoidIntersectionsCurrentLayer:
mAvoidIntersectionsModeButton->setDefaultAction( mAvoidIntersectionsCurrentLayerAction );
mAllowIntersectionsAction->setChecked( false );
mAvoidIntersectionsCurrentLayerAction->setChecked( true );
mAvoidIntersectionsLayersAction->setChecked( false );
break;
case QgsProject::AvoidIntersectionsMode::AvoidIntersectionsLayers:
mAvoidIntersectionsModeButton->setDefaultAction( mAvoidIntersectionsLayersAction );
mAllowIntersectionsAction->setChecked( false );
mAvoidIntersectionsCurrentLayerAction->setChecked( false );
mAvoidIntersectionsLayersAction->setChecked( true );
break;
}
}
void QgsSnappingWidget::projectTopologicalEditingChanged()
{
if ( mProject->topologicalEditing() != mTopologicalEditingAction->isChecked() )
{
mTopologicalEditingAction->setChecked( mProject->topologicalEditing() );
}
}
void QgsSnappingWidget::enableSnapping( bool checked )
{
toggleSnappingWidgets( checked );
mConfig.setEnabled( checked );
mProject->setSnappingConfig( mConfig );
}
void QgsSnappingWidget::toggleSnappingWidgets( bool enabled )
{
mModeButton->setEnabled( enabled );
mTypeButton->setEnabled( enabled );
mToleranceSpinBox->setEnabled( enabled );
if ( mSnappingScaleModeButton )
mSnappingScaleModeButton->setEnabled( enabled );
if ( mMinScaleWidget )
mMinScaleWidget->setEnabled( enabled && mConfig.scaleDependencyMode() == QgsSnappingConfig::Global );
if ( mMaxScaleWidget )
mMaxScaleWidget->setEnabled( enabled && mConfig.scaleDependencyMode() == QgsSnappingConfig::Global );
mUnitsComboBox->setEnabled( enabled );
if ( mEditAdvancedConfigAction )
{
mEditAdvancedConfigAction->setEnabled( enabled );
}
if ( mAdvancedConfigWidget )
{
mAdvancedConfigWidget->setEnabled( enabled );
}
mIntersectionSnappingAction->setEnabled( enabled );
mSelfSnappingAction->setEnabled( enabled );
mEnableTracingAction->setEnabled( enabled );
}
void QgsSnappingWidget::changeTolerance( double tolerance )
{
mConfig.setTolerance( tolerance );
mProject->setSnappingConfig( mConfig );
}
void QgsSnappingWidget::changeMinScale( double minScale )
{
mConfig.setMinimumScale( minScale );
mProject->setSnappingConfig( mConfig );
}
void QgsSnappingWidget::changeMaxScale( double maxScale )
{
mConfig.setMaximumScale( maxScale );
mProject->setSnappingConfig( mConfig );
}
void QgsSnappingWidget::changeUnit( int idx )
{
QgsTolerance::UnitType unit = static_cast<QgsTolerance::UnitType>( mUnitsComboBox->itemData( idx ).toInt() );
mConfig.setUnits( unit );
mProject->setSnappingConfig( mConfig );
updateToleranceDecimals();
}
void QgsSnappingWidget::enableTopologicalEditing( bool enabled )
{
mProject->setTopologicalEditing( enabled );
}
void QgsSnappingWidget::enableIntersectionSnapping( bool enabled )
{
mConfig.setIntersectionSnapping( enabled );
mProject->setSnappingConfig( mConfig );
}
void QgsSnappingWidget::enableSelfSnapping( bool enabled )
{
mConfig.setSelfSnapping( enabled );
mProject->setSnappingConfig( mConfig );
}
void QgsSnappingWidget::onSnappingTreeLayersChanged()
{
mLayerTreeView->expandAll();
mLayerTreeView->resizeColumnToContents( 0 );
}
void QgsSnappingWidget::avoidIntersectionsModeButtonTriggered( QAction *action )
{
if ( action != mAllowIntersectionsAction &&
action != mAvoidIntersectionsCurrentLayerAction &&
action != mAvoidIntersectionsLayersAction )
{
return;
}
if ( action != mAvoidIntersectionsModeButton->defaultAction() )
{
mAvoidIntersectionsModeButton->setDefaultAction( action );
if ( action == mAllowIntersectionsAction )
{
mProject->setAvoidIntersectionsMode( QgsProject::AvoidIntersectionsMode::AllowIntersections );
}
else if ( action == mAvoidIntersectionsCurrentLayerAction )
{
mProject->setAvoidIntersectionsMode( QgsProject::AvoidIntersectionsMode::AvoidIntersectionsCurrentLayer );
}
else if ( action == mAvoidIntersectionsLayersAction )
{
mProject->setAvoidIntersectionsMode( QgsProject::AvoidIntersectionsMode::AvoidIntersectionsLayers );
}
}
}
void QgsSnappingWidget::modeButtonTriggered( QAction *action )
{
if ( action != mAllLayersAction &&
action != mActiveLayerAction &&
action != mAdvancedModeAction )
{
return;
}
if ( action != mModeButton->defaultAction() )
{
mModeButton->setDefaultAction( action );
if ( action == mAllLayersAction )
{
mConfig.setMode( QgsSnappingConfig::AllLayers );
}
else if ( action == mActiveLayerAction )
{
mConfig.setMode( QgsSnappingConfig::ActiveLayer );
}
else if ( action == mAdvancedModeAction )
{
mConfig.setMode( QgsSnappingConfig::AdvancedConfiguration );
}
mProject->setSnappingConfig( mConfig );
updateToleranceDecimals();
modeChanged();
}
}
void QgsSnappingWidget::typeButtonTriggered( QAction *action )
{
unsigned int type = static_cast<int>( mConfig.typeFlag() );
mTypeButton->setDefaultAction( action );
if ( action == mVertexAction )
{
type ^= static_cast<int>( QgsSnappingConfig::VertexFlag );
}
else if ( action == mSegmentAction )
{
type ^= static_cast<int>( QgsSnappingConfig::SegmentFlag );
}
else if ( action == mAreaAction )
{
type ^= static_cast<int>( QgsSnappingConfig::AreaFlag );
}
else if ( action == mCentroidAction )
{
type ^= static_cast<int>( QgsSnappingConfig::CentroidFlag );
}
else if ( action == mMiddleAction )
{
type ^= static_cast<int>( QgsSnappingConfig::MiddleOfSegmentFlag );
}
mConfig.setTypeFlag( static_cast<QgsSnappingConfig::SnappingTypeFlag>( type ) );
mProject->setSnappingConfig( mConfig );
}
void QgsSnappingWidget::snappingScaleModeTriggered( QAction *action )
{
mSnappingScaleModeButton->setDefaultAction( action );
QgsSnappingConfig::ScaleDependencyMode mode = mConfig.scaleDependencyMode();
if ( action == mDefaultSnappingScaleAct )
{
mode = QgsSnappingConfig::Disabled;
}
else if ( action == mGlobalSnappingScaleAct )
{
mode = QgsSnappingConfig::Global;
}
else if ( action == mPerLayerSnappingScaleAct )
{
mode = QgsSnappingConfig::PerLayer;
}
mMinScaleWidget->setEnabled( mode == QgsSnappingConfig::Global );
mMaxScaleWidget->setEnabled( mode == QgsSnappingConfig::Global );
mConfig.setScaleDependencyMode( mode );
mProject->setSnappingConfig( mConfig );
mLayerTreeView->reset();
}
void QgsSnappingWidget::updateToleranceDecimals()
{
if ( mConfig.units() == QgsTolerance::Pixels )
{
mToleranceSpinBox->setDecimals( 0 );
}
else
{
QgsUnitTypes::DistanceUnit mapUnit = mCanvas->mapUnits();
QgsUnitTypes::DistanceUnitType type = QgsUnitTypes::unitType( mapUnit );
if ( type == QgsUnitTypes::Standard )
{
mToleranceSpinBox->setDecimals( 2 );
}
else
{
mToleranceSpinBox->setDecimals( 5 );
}
}
}
void QgsSnappingWidget::modeChanged()
{
bool advanced = mConfig.mode() == QgsSnappingConfig::AdvancedConfiguration;
if ( mDisplayMode == ToolBar )
{
mTypeAction->setVisible( !advanced );
mToleranceAction->setVisible( !advanced );
mUnitAction->setVisible( !advanced );
mEditAdvancedConfigAction->setVisible( advanced );
}
else
{
mTypeButton->setVisible( !advanced );
mToleranceSpinBox->setVisible( !advanced );
mUnitsComboBox->setVisible( !advanced );
if ( mDisplayMode == Widget && mAdvancedConfigWidget )
{
mAdvancedConfigWidget->setVisible( advanced );
}
if ( mSnappingScaleModeButton )
mSnappingScaleModeButton->setVisible( advanced );
if ( mMinScaleWidget )
mMinScaleWidget->setVisible( advanced );
if ( mMaxScaleWidget )
mMaxScaleWidget->setVisible( advanced );
}
}
QgsSnappingConfig QgsSnappingWidget::config() const
{
return mConfig;
}
void QgsSnappingWidget::setConfig( const QgsSnappingConfig &config )
{
if ( mConfig == config )
return;
mConfig = config;
}
void QgsSnappingWidget::cleanGroup( QgsLayerTreeNode *node )
{
QgsLayerTreeGroup *group = QgsLayerTree::isGroup( node ) ? QgsLayerTree::toGroup( node ) : nullptr;
if ( !group )
return;
QList<QgsLayerTreeNode *> toRemove;
const auto constChildren = node->children();
for ( QgsLayerTreeNode *child : constChildren )
{
if ( QgsLayerTree::isLayer( child ) && ( !QgsLayerTree::toLayer( child )->layer() || QgsLayerTree::toLayer( child )->layer()->type() != QgsMapLayerType::VectorLayer ) )
{
toRemove << child;
continue;
}
cleanGroup( child );
if ( QgsLayerTree::isGroup( child ) && child->children().isEmpty() )
toRemove << child;
}
const auto constToRemove = toRemove;
for ( QgsLayerTreeNode *child : constToRemove )
group->removeChildNode( child );
}