Skip to content

Commit 6917860

Browse files
authoredAug 31, 2018
Merge pull request #7735 from 3nids/snap3
add layer filter in snapping configuration
2 parents c3fd631 + 04e7c7a commit 6917860

File tree

4 files changed

+53
-17
lines changed

4 files changed

+53
-17
lines changed
 

‎src/app/qgssnappinglayertreemodel.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ QWidget *QgsSnappingLayerDelegate::createEditor( QWidget *parent, const QStyleOp
5353
QVariant val = index.model()->data( index.model()->sibling( index.row(), QgsSnappingLayerTreeModel::UnitsColumn, index ), Qt::UserRole );
5454
if ( val.isValid() )
5555
{
56-
QgsTolerance::UnitType units = ( QgsTolerance::UnitType )val.toInt();
56+
QgsTolerance::UnitType units = static_cast<QgsTolerance::UnitType>( val.toInt() );
5757
if ( units == QgsTolerance::Pixels )
5858
{
5959
w->setDecimals( 0 );
@@ -90,7 +90,7 @@ void QgsSnappingLayerDelegate::setEditorData( QWidget *editor, const QModelIndex
9090

9191
if ( index.column() == QgsSnappingLayerTreeModel::TypeColumn )
9292
{
93-
QgsSnappingConfig::SnappingType type = ( QgsSnappingConfig::SnappingType )val.toInt();
93+
QgsSnappingConfig::SnappingType type = static_cast<QgsSnappingConfig::SnappingType>( val.toInt() );
9494
QComboBox *cb = qobject_cast<QComboBox *>( editor );
9595
if ( cb )
9696
{
@@ -107,7 +107,7 @@ void QgsSnappingLayerDelegate::setEditorData( QWidget *editor, const QModelIndex
107107
}
108108
else if ( index.column() == QgsSnappingLayerTreeModel::UnitsColumn )
109109
{
110-
QgsTolerance::UnitType units = ( QgsTolerance::UnitType )val.toInt();
110+
QgsTolerance::UnitType units = static_cast<QgsTolerance::UnitType>( val.toInt() );
111111
QComboBox *w = qobject_cast<QComboBox *>( editor );
112112
if ( w )
113113
{
@@ -229,6 +229,15 @@ QgsVectorLayer *QgsSnappingLayerTreeModel::vectorLayer( const QModelIndex &idx )
229229
return qobject_cast<QgsVectorLayer *>( QgsLayerTree::toLayer( node )->layer() );
230230
}
231231

232+
void QgsSnappingLayerTreeModel::setFilterText( const QString &filterText )
233+
{
234+
if ( filterText == mFilterText )
235+
return;
236+
237+
mFilterText = filterText;
238+
invalidateFilter();
239+
}
240+
232241
void QgsSnappingLayerTreeModel::onSnappingSettingsChanged()
233242
{
234243
const QHash<QgsVectorLayer *, QgsSnappingConfig::IndividualLayerSettings> oldSettings = mIndividualLayerSettings;
@@ -317,7 +326,7 @@ bool QgsSnappingLayerTreeModel::nodeShown( QgsLayerTreeNode *node ) const
317326
else
318327
{
319328
QgsVectorLayer *layer = qobject_cast<QgsVectorLayer *>( QgsLayerTree::toLayer( node )->layer() );
320-
return layer && layer->isSpatial();
329+
return layer && layer->isSpatial() && ( mFilterText.isEmpty() || layer->name().contains( mFilterText, Qt::CaseInsensitive ) );
321330
}
322331
}
323332

@@ -441,8 +450,6 @@ QVariant QgsSnappingLayerTreeModel::data( const QModelIndex &idx, int role ) con
441450
return tr( "vertex and segment" );
442451
case QgsSnappingConfig::Segment:
443452
return tr( "segment" );
444-
default:
445-
return tr( "N/A" );
446453
}
447454
}
448455

@@ -537,12 +544,13 @@ bool QgsSnappingLayerTreeModel::setData( const QModelIndex &index, const QVarian
537544
else if ( value.toInt() == Qt::Unchecked )
538545
ls.setEnabled( false );
539546
else
540-
Q_ASSERT( "expected checked or unchecked" );
547+
Q_ASSERT( false ); // expected checked or unchecked
541548

542549
QgsSnappingConfig config = mProject->snappingConfig();
543550
config.setIndividualLayerSettings( vl, ls );
544551
mProject->setSnappingConfig( config );
545552
}
553+
emit dataChanged( index, index );
546554
return true;
547555
}
548556

@@ -561,10 +569,11 @@ bool QgsSnappingLayerTreeModel::setData( const QModelIndex &index, const QVarian
561569
if ( !ls.valid() )
562570
return false;
563571

564-
ls.setType( ( QgsSnappingConfig::SnappingType )value.toInt() );
572+
ls.setType( static_cast<QgsSnappingConfig::SnappingType>( value.toInt() ) );
565573
QgsSnappingConfig config = mProject->snappingConfig();
566574
config.setIndividualLayerSettings( vl, ls );
567575
mProject->setSnappingConfig( config );
576+
emit dataChanged( index, index );
568577
return true;
569578
}
570579
}
@@ -585,6 +594,7 @@ bool QgsSnappingLayerTreeModel::setData( const QModelIndex &index, const QVarian
585594
QgsSnappingConfig config = mProject->snappingConfig();
586595
config.setIndividualLayerSettings( vl, ls );
587596
mProject->setSnappingConfig( config );
597+
emit dataChanged( index, index );
588598
return true;
589599
}
590600
}
@@ -601,10 +611,11 @@ bool QgsSnappingLayerTreeModel::setData( const QModelIndex &index, const QVarian
601611
if ( !ls.valid() )
602612
return false;
603613

604-
ls.setUnits( ( QgsTolerance::UnitType )value.toInt() );
614+
ls.setUnits( static_cast<QgsTolerance::UnitType>( value.toInt() ) );
605615
QgsSnappingConfig config = mProject->snappingConfig();
606616
config.setIndividualLayerSettings( vl, ls );
607617
mProject->setSnappingConfig( config );
618+
emit dataChanged( index, index );
608619
return true;
609620
}
610621
}
@@ -625,6 +636,7 @@ bool QgsSnappingLayerTreeModel::setData( const QModelIndex &index, const QVarian
625636
avoidIntersectionsList.removeAll( vl );
626637

627638
mProject->setAvoidIntersectionsLayers( avoidIntersectionsList );
639+
emit dataChanged( index, index );
628640
return true;
629641
}
630642
}

‎src/app/qgssnappinglayertreemodel.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ class APP_EXPORT QgsSnappingLayerTreeModel : public QSortFilterProxyModel
7676

7777
QgsVectorLayer *vectorLayer( const QModelIndex &idx ) const;
7878

79+
public slots:
80+
void setFilterText( const QString &filterText = QString() );
81+
7982
protected:
8083
bool filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const override;
8184

@@ -86,6 +89,7 @@ class APP_EXPORT QgsSnappingLayerTreeModel : public QSortFilterProxyModel
8689
bool nodeShown( QgsLayerTreeNode *node ) const;
8790

8891
QgsProject *mProject = nullptr;
92+
QString mFilterText;
8993
QHash<QgsVectorLayer *, QgsSnappingConfig::IndividualLayerSettings> mIndividualLayerSettings;
9094
QgsLayerTreeModel *mLayerTreeModel = nullptr;
9195

‎src/app/qgssnappingwidget.cpp

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ QgsSnappingWidget::QgsSnappingWidget( QgsProject *project, QgsMapCanvas *canvas,
6262
}
6363

6464
// Advanced config layer tree view
65+
mAdvancedConfigWidget = new QWidget( this );
66+
QVBoxLayout *advancedLayout = new QVBoxLayout();
67+
if ( mDisplayMode == Widget )
68+
advancedLayout->setContentsMargins( 0, 0, 0, 0 );
69+
// tree view
6570
mLayerTreeView = new QTreeView();
6671
QgsSnappingLayerTreeModel *model = new QgsSnappingLayerTreeModel( mProject, this );
6772
model->setLayerTreeModel( new QgsLayerTreeModel( mProject->layerTreeRoot(), model ) );
@@ -82,6 +87,20 @@ QgsSnappingWidget::QgsSnappingWidget( QgsProject *project, QgsMapCanvas *canvas,
8287
mLayerTreeView->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding );
8388
mLayerTreeView->setMinimumWidth( 500 );
8489
mLayerTreeView->resizeColumnToContents( 0 );
90+
// filter line edit
91+
QHBoxLayout *filterLayout = new QHBoxLayout();
92+
filterLayout->setContentsMargins( 0, 0, 0, 0 );
93+
filterLayout->addStretch();
94+
QgsFilterLineEdit *filterLineEdit = new QgsFilterLineEdit();
95+
filterLineEdit->setShowClearButton( true );
96+
filterLineEdit->setShowSearchIcon( true );
97+
filterLineEdit->setPlaceholderText( tr( "Filter layers…" ) );
98+
connect( filterLineEdit, &QgsFilterLineEdit::textChanged, model, &QgsSnappingLayerTreeModel::setFilterText );
99+
filterLayout->addStretch();
100+
filterLayout->addWidget( filterLineEdit );
101+
advancedLayout->addWidget( mLayerTreeView );
102+
advancedLayout->addLayout( filterLayout );
103+
mAdvancedConfigWidget->setLayout( advancedLayout );
85104

86105
// enable button
87106
mEnabledAction = new QAction( tr( "Toggle Snapping" ), this );
@@ -206,7 +225,7 @@ QgsSnappingWidget::QgsSnappingWidget( QgsProject *project, QgsMapCanvas *canvas,
206225
advConfigButton->setPopupMode( QToolButton::InstantPopup );
207226
QMenu *advConfigMenu = new QMenu( this );
208227
QWidgetAction *advConfigWidgetAction = new QWidgetAction( advConfigMenu );
209-
advConfigWidgetAction->setDefaultWidget( mLayerTreeView );
228+
advConfigWidgetAction->setDefaultWidget( mAdvancedConfigWidget );
210229
advConfigMenu->addAction( advConfigWidgetAction );
211230
advConfigButton->setIcon( QIcon( QgsApplication::getThemeIcon( "/mActionShowAllLayers.svg" ) ) );
212231
advConfigButton->setToolTip( tr( "Edit advanced configuration" ) );
@@ -255,7 +274,7 @@ QgsSnappingWidget::QgsSnappingWidget( QgsProject *project, QgsMapCanvas *canvas,
255274

256275
QGridLayout *topLayout = new QGridLayout();
257276
topLayout->addLayout( layout, 0, 0, Qt::AlignLeft | Qt::AlignTop );
258-
topLayout->addWidget( mLayerTreeView, 1, 0 );
277+
topLayout->addWidget( mAdvancedConfigWidget, 1, 0 );
259278
setLayout( topLayout );
260279
}
261280

@@ -333,7 +352,7 @@ void QgsSnappingWidget::projectSnapSettingsChanged()
333352
mToleranceSpinBox->setValue( config.tolerance() );
334353
}
335354

336-
if ( ( QgsTolerance::UnitType )mUnitsComboBox->currentData().toInt() != config.units() )
355+
if ( static_cast<QgsTolerance::UnitType>( mUnitsComboBox->currentData().toInt() ) != config.units() )
337356
{
338357
mUnitsComboBox->setCurrentIndex( mUnitsComboBox->findData( config.units() ) );
339358
}
@@ -367,9 +386,9 @@ void QgsSnappingWidget::toggleSnappingWidgets( bool enabled )
367386
mTypeButton->setEnabled( enabled );
368387
mToleranceSpinBox->setEnabled( enabled );
369388
mUnitsComboBox->setEnabled( enabled );
370-
if ( mLayerTreeView )
389+
if ( mAdvancedConfigWidget )
371390
{
372-
mLayerTreeView->setEnabled( enabled );
391+
mAdvancedConfigWidget->setEnabled( enabled );
373392
}
374393
mIntersectionSnappingAction->setEnabled( enabled );
375394
mEnableTracingAction->setEnabled( enabled );
@@ -383,7 +402,7 @@ void QgsSnappingWidget::changeTolerance( double tolerance )
383402

384403
void QgsSnappingWidget::changeUnit( int idx )
385404
{
386-
QgsTolerance::UnitType unit = ( QgsTolerance::UnitType )mUnitsComboBox->itemData( idx ).toInt();
405+
QgsTolerance::UnitType unit = static_cast<QgsTolerance::UnitType>( mUnitsComboBox->itemData( idx ).toInt() );
387406
mConfig.setUnits( unit );
388407
mProject->setSnappingConfig( mConfig );
389408

@@ -495,9 +514,9 @@ void QgsSnappingWidget::modeChanged()
495514
mTypeButton->setVisible( !advanced );
496515
mToleranceSpinBox->setVisible( !advanced );
497516
mUnitsComboBox->setVisible( !advanced );
498-
if ( mDisplayMode == Widget && mLayerTreeView )
517+
if ( mDisplayMode == Widget && mAdvancedConfigWidget )
499518
{
500-
mLayerTreeView->setVisible( advanced );
519+
mAdvancedConfigWidget->setVisible( advanced );
501520
}
502521
}
503522
}

‎src/app/qgssnappingwidget.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ class APP_EXPORT QgsSnappingWidget : public QWidget
150150
QAction *mEnableTracingAction = nullptr;
151151
QgsDoubleSpinBox *mTracingOffsetSpinBox = nullptr;
152152
QTreeView *mLayerTreeView = nullptr;
153+
QWidget *mAdvancedConfigWidget = nullptr;
153154
QgsFloatingWidget *mAdvancedConfigContainer = nullptr;
154155

155156
void cleanGroup( QgsLayerTreeNode *node );

0 commit comments

Comments
 (0)
Please sign in to comment.