Skip to content

Commit 1174b33

Browse files
committedMay 17, 2017
Add inbuilt filters for project layers and compositions
The project layer filter allows you to quickly select a layer from the current project and highlight it in the layer tree. It's useful for complex project with lots of groups, where it's easy to "lose" layers somewhere in the tree... The composition filter allows searching for and opening compositions from the current project
1 parent 71f7872 commit 1174b33

File tree

10 files changed

+238
-49
lines changed

10 files changed

+238
-49
lines changed
 

‎python/core/qgsmaplayermodel.sip

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,13 @@ returns if the items can be checked or not
134134
virtual Qt::ItemFlags flags( const QModelIndex &index ) const;
135135

136136

137+
static QIcon iconForLayer( QgsMapLayer *layer );
138+
%Docstring
139+
Returns the icon corresponding to a specified map ``layer``.
140+
.. versionadded:: 3.0
141+
:rtype: QIcon
142+
%End
143+
137144
protected slots:
138145
void removeLayers( const QStringList &layerIds );
139146
void addLayers( const QList<QgsMapLayer *> &layers );

‎python/gui/locator/qgslocatorfilter.sip

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ class QgsLocatorResult
4444
Custom reference or other data set by the filter.
4545
%End
4646

47+
QIcon icon;
48+
%Docstring
49+
Icon for result.
50+
%End
51+
4752
};
4853

4954
class QgsLocatorFilter : QObject

‎src/app/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ SET(QGIS_APP_SRCS
155155
composer/qgscompositionwidget.cpp
156156
composer/qgsatlascompositionwidget.cpp
157157

158+
locator/qgsinbuiltlocatorfilters.cpp
159+
158160
ogr/qgsogrhelperfunctions.cpp
159161
ogr/qgsopenvectorlayerdialog.cpp
160162
ogr/qgsnewogrconnection.cpp
@@ -332,6 +334,8 @@ SET (QGIS_APP_MOC_HDRS
332334
composer/qgscompositionwidget.h
333335
composer/qgsatlascompositionwidget.h
334336

337+
locator/qgsinbuiltlocatorfilters.h
338+
335339
ogr/qgsopenvectorlayerdialog.h
336340
ogr/qgsnewogrconnection.h
337341
ogr/qgsvectorlayersaveasdialog.h
@@ -520,6 +524,8 @@ INCLUDE_DIRECTORIES(
520524
openstreetmap
521525
dwg
522526
dwg/libdxfrw
527+
dwg/libdxfrw
528+
locator
523529
${CMAKE_SOURCE_DIR}/src/native
524530
${CMAKE_BINARY_DIR}/src/native
525531
)
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/***************************************************************************
2+
qgsinbuiltlocatorfilters.cpp
3+
----------------------------
4+
begin : May 2017
5+
copyright : (C) 2017 by Nyall Dawson
6+
email : nyall dot dawson at gmail dot com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
19+
#include "qgsinbuiltlocatorfilters.h"
20+
#include "qgsproject.h"
21+
#include "qgslayertree.h"
22+
#include "qgsfeedback.h"
23+
#include "qgisapp.h"
24+
#include "qgsstringutils.h"
25+
#include "qgsmaplayermodel.h"
26+
#include "qgscomposition.h"
27+
#include "qgslayoutmanager.h"
28+
29+
QgsLayerTreeLocatorFilter::QgsLayerTreeLocatorFilter( QObject *parent )
30+
: QgsLocatorFilter( parent )
31+
{}
32+
33+
void QgsLayerTreeLocatorFilter::fetchResults( const QString &string, QgsFeedback *feedback )
34+
{
35+
QgsLayerTree *tree = QgsProject::instance()->layerTreeRoot();
36+
QList<QgsLayerTreeLayer *> layers = tree->findLayers();
37+
Q_FOREACH ( QgsLayerTreeLayer *layer, layers )
38+
{
39+
if ( feedback->isCanceled() )
40+
return;
41+
42+
if ( layer->layer() && layer->layer()->name().contains( string, Qt::CaseInsensitive ) )
43+
{
44+
QgsLocatorResult result;
45+
result.filter = this;
46+
result.displayString = layer->layer()->name();
47+
result.userData = layer->layerId();
48+
result.icon = QgsMapLayerModel::iconForLayer( layer->layer() );
49+
emit resultFetched( result );
50+
}
51+
}
52+
}
53+
54+
void QgsLayerTreeLocatorFilter::triggerResult( const QgsLocatorResult &result )
55+
{
56+
QString layerId = result.userData.toString();
57+
QgsMapLayer *layer = QgsProject::instance()->mapLayer( layerId );
58+
QgisApp::instance()->setActiveLayer( layer );
59+
}
60+
61+
//
62+
// QgsLayoutLocatorFilter
63+
//
64+
65+
QgsLayoutLocatorFilter::QgsLayoutLocatorFilter( QObject *parent )
66+
: QgsLocatorFilter( parent )
67+
{}
68+
69+
void QgsLayoutLocatorFilter::fetchResults( const QString &string, QgsFeedback *feedback )
70+
{
71+
Q_FOREACH ( QgsComposition *composition, QgsProject::instance()->layoutManager()->compositions() )
72+
{
73+
if ( feedback->isCanceled() )
74+
return;
75+
76+
if ( composition && composition->name().contains( string, Qt::CaseInsensitive ) )
77+
{
78+
QgsLocatorResult result;
79+
result.filter = this;
80+
result.displayString = composition->name();
81+
result.userData = composition->name();
82+
//result.icon = QgsMapLayerModel::iconForLayer( layer->layer() );
83+
emit resultFetched( result );
84+
}
85+
}
86+
}
87+
88+
void QgsLayoutLocatorFilter::triggerResult( const QgsLocatorResult &result )
89+
{
90+
QString layoutName = result.userData.toString();
91+
QgsComposition *composition = QgsProject::instance()->layoutManager()->compositionByName( layoutName );
92+
if ( !composition )
93+
return;
94+
95+
QgisApp::instance()->openComposer( composition );
96+
}
97+
98+
99+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/***************************************************************************
2+
qgsinbuiltlocatorfilters.h
3+
--------------------------
4+
begin : May 2017
5+
copyright : (C) 2017 by Nyall Dawson
6+
email : nyall dot dawson at gmail dot com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#ifndef QGSINBUILTLOCATORFILTERS_H
19+
#define QGSINBUILTLOCATORFILTERS_H
20+
21+
#include "qgslocatorfilter.h"
22+
23+
class QgsLayerTreeLocatorFilter : public QgsLocatorFilter
24+
{
25+
Q_OBJECT
26+
27+
public:
28+
29+
QgsLayerTreeLocatorFilter( QObject *parent = nullptr );
30+
void fetchResults( const QString &string, QgsFeedback *feedback ) override;
31+
void triggerResult( const QgsLocatorResult &result ) override;
32+
33+
};
34+
35+
class QgsLayoutLocatorFilter : public QgsLocatorFilter
36+
{
37+
Q_OBJECT
38+
39+
public:
40+
41+
QgsLayoutLocatorFilter( QObject *parent = nullptr );
42+
void fetchResults( const QString &string, QgsFeedback *feedback ) override;
43+
void triggerResult( const QgsLocatorResult &result ) override;
44+
45+
};
46+
47+
#endif // QGSINBUILTLOCATORFILTERS_H
48+
49+

‎src/app/qgisapp.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ Q_GUI_EXPORT extern int qt_defaultDpiX();
189189
#include "qgslayertreeviewdefaultactions.h"
190190
#include "qgslayoutmanager.h"
191191
#include "qgslocatorwidget.h"
192+
#include "qgslocator.h"
193+
#include "qgsinbuiltlocatorfilters.h"
192194
#include "qgslogger.h"
193195
#include "qgsmapcanvas.h"
194196
#include "qgsmapcanvasdockwidget.h"
@@ -2643,6 +2645,9 @@ void QgisApp::createStatusBar()
26432645
locatorShortCut->setObjectName( QStringLiteral( "Locator" ) );
26442646
locatorShortCut->setWhatsThis( tr( "Trigger Locator" ) );
26452647

2648+
mLocatorWidget->locator()->registerFilter( new QgsLayerTreeLocatorFilter() );
2649+
mLocatorWidget->locator()->registerFilter( new QgsLayoutLocatorFilter() );
2650+
26462651
}
26472652

26482653
void QgisApp::setIconSizes( int size )
@@ -7197,7 +7202,9 @@ QgsComposer *QgisApp::openComposer( QgsComposition *composition )
71977202
{
71987203
if ( composer->composition() == composition )
71997204
{
7200-
composer->open();
7205+
composer->show();
7206+
composer->activate();
7207+
composer->raise();
72017208
return composer;
72027209
}
72037210
}

‎src/core/qgsmaplayermodel.cpp

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -311,54 +311,7 @@ QVariant QgsMapLayerModel::data( const QModelIndex &index, int role ) const
311311
if ( !layer )
312312
return QVariant();
313313

314-
QgsMapLayer::LayerType type = layer->type();
315-
if ( role == Qt::DecorationRole )
316-
{
317-
switch ( type )
318-
{
319-
case QgsMapLayer::RasterLayer:
320-
{
321-
return QgsLayerItem::iconRaster();
322-
}
323-
324-
case QgsMapLayer::VectorLayer:
325-
{
326-
QgsVectorLayer *vl = dynamic_cast<QgsVectorLayer *>( layer );
327-
if ( !vl )
328-
{
329-
return QIcon();
330-
}
331-
QgsWkbTypes::GeometryType geomType = vl->geometryType();
332-
switch ( geomType )
333-
{
334-
case QgsWkbTypes::PointGeometry:
335-
{
336-
return QgsLayerItem::iconPoint();
337-
}
338-
case QgsWkbTypes::PolygonGeometry :
339-
{
340-
return QgsLayerItem::iconPolygon();
341-
}
342-
case QgsWkbTypes::LineGeometry :
343-
{
344-
return QgsLayerItem::iconLine();
345-
}
346-
case QgsWkbTypes::NullGeometry :
347-
{
348-
return QgsLayerItem::iconTable();
349-
}
350-
default:
351-
{
352-
return QIcon();
353-
}
354-
}
355-
}
356-
default:
357-
{
358-
return QIcon();
359-
}
360-
}
361-
}
314+
return iconForLayer( layer );
362315
}
363316
}
364317

@@ -392,6 +345,54 @@ Qt::ItemFlags QgsMapLayerModel::flags( const QModelIndex &index ) const
392345
return flags;
393346
}
394347

348+
QIcon QgsMapLayerModel::iconForLayer( QgsMapLayer *layer )
349+
{
350+
switch ( layer->type() )
351+
{
352+
case QgsMapLayer::RasterLayer:
353+
{
354+
return QgsLayerItem::iconRaster();
355+
}
356+
357+
case QgsMapLayer::VectorLayer:
358+
{
359+
QgsVectorLayer *vl = dynamic_cast<QgsVectorLayer *>( layer );
360+
if ( !vl )
361+
{
362+
return QIcon();
363+
}
364+
QgsWkbTypes::GeometryType geomType = vl->geometryType();
365+
switch ( geomType )
366+
{
367+
case QgsWkbTypes::PointGeometry:
368+
{
369+
return QgsLayerItem::iconPoint();
370+
}
371+
case QgsWkbTypes::PolygonGeometry :
372+
{
373+
return QgsLayerItem::iconPolygon();
374+
}
375+
case QgsWkbTypes::LineGeometry :
376+
{
377+
return QgsLayerItem::iconLine();
378+
}
379+
case QgsWkbTypes::NullGeometry :
380+
{
381+
return QgsLayerItem::iconTable();
382+
}
383+
default:
384+
{
385+
return QIcon();
386+
}
387+
}
388+
}
389+
default:
390+
{
391+
return QIcon();
392+
}
393+
}
394+
}
395+
395396

396397
bool QgsMapLayerModel::setData( const QModelIndex &index, const QVariant &value, int role )
397398
{

‎src/core/qgsmaplayermodel.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ class CORE_EXPORT QgsMapLayerModel : public QAbstractItemModel
145145
bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
146146
Qt::ItemFlags flags( const QModelIndex &index ) const override;
147147

148+
/**
149+
* Returns the icon corresponding to a specified map \a layer.
150+
* \since QGIS 3.0
151+
*/
152+
static QIcon iconForLayer( QgsMapLayer *layer );
153+
148154
protected slots:
149155
void removeLayers( const QStringList &layerIds );
150156
void addLayers( const QList<QgsMapLayer *> &layers );

‎src/gui/locator/qgslocatorfilter.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "qgslogger.h"
2323
#include <QString>
2424
#include <QVariant>
25+
#include <QIcon>
2526

2627
class QgsFeedback;
2728
class QgsLocatorFilter;
@@ -62,6 +63,11 @@ class GUI_EXPORT QgsLocatorResult
6263
*/
6364
QVariant userData;
6465

66+
/**
67+
* Icon for result.
68+
*/
69+
QIcon icon;
70+
6571
};
6672

6773
/**

‎src/gui/locator/qgslocatorwidget.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,9 @@ QVariant QgsLocatorModel::data( const QModelIndex &index, int role ) const
272272
case Qt::EditRole:
273273
return mResults.at( index.row() ).displayString;
274274

275+
case Qt::DecorationRole:
276+
return mResults.at( index.row() ).icon;
277+
275278
case ResultDataRole:
276279
return QVariant::fromValue( mResults.at( index.row() ) );
277280
}

0 commit comments

Comments
 (0)