Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nyalldawson committed May 17, 2017
1 parent 71f7872 commit 1174b33
Show file tree
Hide file tree
Showing 10 changed files with 238 additions and 49 deletions.
7 changes: 7 additions & 0 deletions python/core/qgsmaplayermodel.sip
Expand Up @@ -134,6 +134,13 @@ returns if the items can be checked or not
virtual Qt::ItemFlags flags( const QModelIndex &index ) const;


static QIcon iconForLayer( QgsMapLayer *layer );
%Docstring
Returns the icon corresponding to a specified map ``layer``.
.. versionadded:: 3.0
:rtype: QIcon
%End

protected slots:
void removeLayers( const QStringList &layerIds );
void addLayers( const QList<QgsMapLayer *> &layers );
Expand Down
5 changes: 5 additions & 0 deletions python/gui/locator/qgslocatorfilter.sip
Expand Up @@ -44,6 +44,11 @@ class QgsLocatorResult
Custom reference or other data set by the filter.
%End

QIcon icon;
%Docstring
Icon for result.
%End

};

class QgsLocatorFilter : QObject
Expand Down
6 changes: 6 additions & 0 deletions src/app/CMakeLists.txt
Expand Up @@ -155,6 +155,8 @@ SET(QGIS_APP_SRCS
composer/qgscompositionwidget.cpp
composer/qgsatlascompositionwidget.cpp

locator/qgsinbuiltlocatorfilters.cpp

ogr/qgsogrhelperfunctions.cpp
ogr/qgsopenvectorlayerdialog.cpp
ogr/qgsnewogrconnection.cpp
Expand Down Expand Up @@ -332,6 +334,8 @@ SET (QGIS_APP_MOC_HDRS
composer/qgscompositionwidget.h
composer/qgsatlascompositionwidget.h

locator/qgsinbuiltlocatorfilters.h

ogr/qgsopenvectorlayerdialog.h
ogr/qgsnewogrconnection.h
ogr/qgsvectorlayersaveasdialog.h
Expand Down Expand Up @@ -520,6 +524,8 @@ INCLUDE_DIRECTORIES(
openstreetmap
dwg
dwg/libdxfrw
dwg/libdxfrw
locator
${CMAKE_SOURCE_DIR}/src/native
${CMAKE_BINARY_DIR}/src/native
)
Expand Down
99 changes: 99 additions & 0 deletions src/app/locator/qgsinbuiltlocatorfilters.cpp
@@ -0,0 +1,99 @@
/***************************************************************************
qgsinbuiltlocatorfilters.cpp
----------------------------
begin : May 2017
copyright : (C) 2017 by Nyall Dawson
email : nyall dot dawson at gmail dot 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 "qgsinbuiltlocatorfilters.h"
#include "qgsproject.h"
#include "qgslayertree.h"
#include "qgsfeedback.h"
#include "qgisapp.h"
#include "qgsstringutils.h"
#include "qgsmaplayermodel.h"
#include "qgscomposition.h"
#include "qgslayoutmanager.h"

QgsLayerTreeLocatorFilter::QgsLayerTreeLocatorFilter( QObject *parent )
: QgsLocatorFilter( parent )
{}

void QgsLayerTreeLocatorFilter::fetchResults( const QString &string, QgsFeedback *feedback )
{
QgsLayerTree *tree = QgsProject::instance()->layerTreeRoot();
QList<QgsLayerTreeLayer *> layers = tree->findLayers();
Q_FOREACH ( QgsLayerTreeLayer *layer, layers )
{
if ( feedback->isCanceled() )
return;

if ( layer->layer() && layer->layer()->name().contains( string, Qt::CaseInsensitive ) )
{
QgsLocatorResult result;
result.filter = this;
result.displayString = layer->layer()->name();
result.userData = layer->layerId();
result.icon = QgsMapLayerModel::iconForLayer( layer->layer() );
emit resultFetched( result );
}
}
}

void QgsLayerTreeLocatorFilter::triggerResult( const QgsLocatorResult &result )
{
QString layerId = result.userData.toString();
QgsMapLayer *layer = QgsProject::instance()->mapLayer( layerId );
QgisApp::instance()->setActiveLayer( layer );
}

//
// QgsLayoutLocatorFilter
//

QgsLayoutLocatorFilter::QgsLayoutLocatorFilter( QObject *parent )
: QgsLocatorFilter( parent )
{}

void QgsLayoutLocatorFilter::fetchResults( const QString &string, QgsFeedback *feedback )
{
Q_FOREACH ( QgsComposition *composition, QgsProject::instance()->layoutManager()->compositions() )
{
if ( feedback->isCanceled() )
return;

if ( composition && composition->name().contains( string, Qt::CaseInsensitive ) )
{
QgsLocatorResult result;
result.filter = this;
result.displayString = composition->name();
result.userData = composition->name();
//result.icon = QgsMapLayerModel::iconForLayer( layer->layer() );
emit resultFetched( result );
}
}
}

void QgsLayoutLocatorFilter::triggerResult( const QgsLocatorResult &result )
{
QString layoutName = result.userData.toString();
QgsComposition *composition = QgsProject::instance()->layoutManager()->compositionByName( layoutName );
if ( !composition )
return;

QgisApp::instance()->openComposer( composition );
}



49 changes: 49 additions & 0 deletions src/app/locator/qgsinbuiltlocatorfilters.h
@@ -0,0 +1,49 @@
/***************************************************************************
qgsinbuiltlocatorfilters.h
--------------------------
begin : May 2017
copyright : (C) 2017 by Nyall Dawson
email : nyall dot dawson at gmail dot 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. *
* *
***************************************************************************/

#ifndef QGSINBUILTLOCATORFILTERS_H
#define QGSINBUILTLOCATORFILTERS_H

#include "qgslocatorfilter.h"

class QgsLayerTreeLocatorFilter : public QgsLocatorFilter
{
Q_OBJECT

public:

QgsLayerTreeLocatorFilter( QObject *parent = nullptr );
void fetchResults( const QString &string, QgsFeedback *feedback ) override;
void triggerResult( const QgsLocatorResult &result ) override;

};

class QgsLayoutLocatorFilter : public QgsLocatorFilter
{
Q_OBJECT

public:

QgsLayoutLocatorFilter( QObject *parent = nullptr );
void fetchResults( const QString &string, QgsFeedback *feedback ) override;
void triggerResult( const QgsLocatorResult &result ) override;

};

#endif // QGSINBUILTLOCATORFILTERS_H


9 changes: 8 additions & 1 deletion src/app/qgisapp.cpp
Expand Up @@ -189,6 +189,8 @@ Q_GUI_EXPORT extern int qt_defaultDpiX();
#include "qgslayertreeviewdefaultactions.h"
#include "qgslayoutmanager.h"
#include "qgslocatorwidget.h"
#include "qgslocator.h"
#include "qgsinbuiltlocatorfilters.h"
#include "qgslogger.h"
#include "qgsmapcanvas.h"
#include "qgsmapcanvasdockwidget.h"
Expand Down Expand Up @@ -2643,6 +2645,9 @@ void QgisApp::createStatusBar()
locatorShortCut->setObjectName( QStringLiteral( "Locator" ) );
locatorShortCut->setWhatsThis( tr( "Trigger Locator" ) );

mLocatorWidget->locator()->registerFilter( new QgsLayerTreeLocatorFilter() );
mLocatorWidget->locator()->registerFilter( new QgsLayoutLocatorFilter() );

}

void QgisApp::setIconSizes( int size )
Expand Down Expand Up @@ -7197,7 +7202,9 @@ QgsComposer *QgisApp::openComposer( QgsComposition *composition )
{
if ( composer->composition() == composition )
{
composer->open();
composer->show();
composer->activate();
composer->raise();
return composer;
}
}
Expand Down
97 changes: 49 additions & 48 deletions src/core/qgsmaplayermodel.cpp
Expand Up @@ -311,54 +311,7 @@ QVariant QgsMapLayerModel::data( const QModelIndex &index, int role ) const
if ( !layer )
return QVariant();

QgsMapLayer::LayerType type = layer->type();
if ( role == Qt::DecorationRole )
{
switch ( type )
{
case QgsMapLayer::RasterLayer:
{
return QgsLayerItem::iconRaster();
}

case QgsMapLayer::VectorLayer:
{
QgsVectorLayer *vl = dynamic_cast<QgsVectorLayer *>( layer );
if ( !vl )
{
return QIcon();
}
QgsWkbTypes::GeometryType geomType = vl->geometryType();
switch ( geomType )
{
case QgsWkbTypes::PointGeometry:
{
return QgsLayerItem::iconPoint();
}
case QgsWkbTypes::PolygonGeometry :
{
return QgsLayerItem::iconPolygon();
}
case QgsWkbTypes::LineGeometry :
{
return QgsLayerItem::iconLine();
}
case QgsWkbTypes::NullGeometry :
{
return QgsLayerItem::iconTable();
}
default:
{
return QIcon();
}
}
}
default:
{
return QIcon();
}
}
}
return iconForLayer( layer );
}
}

Expand Down Expand Up @@ -392,6 +345,54 @@ Qt::ItemFlags QgsMapLayerModel::flags( const QModelIndex &index ) const
return flags;
}

QIcon QgsMapLayerModel::iconForLayer( QgsMapLayer *layer )
{
switch ( layer->type() )
{
case QgsMapLayer::RasterLayer:
{
return QgsLayerItem::iconRaster();
}

case QgsMapLayer::VectorLayer:
{
QgsVectorLayer *vl = dynamic_cast<QgsVectorLayer *>( layer );
if ( !vl )
{
return QIcon();
}
QgsWkbTypes::GeometryType geomType = vl->geometryType();
switch ( geomType )
{
case QgsWkbTypes::PointGeometry:
{
return QgsLayerItem::iconPoint();
}
case QgsWkbTypes::PolygonGeometry :
{
return QgsLayerItem::iconPolygon();
}
case QgsWkbTypes::LineGeometry :
{
return QgsLayerItem::iconLine();
}
case QgsWkbTypes::NullGeometry :
{
return QgsLayerItem::iconTable();
}
default:
{
return QIcon();
}
}
}
default:
{
return QIcon();
}
}
}


bool QgsMapLayerModel::setData( const QModelIndex &index, const QVariant &value, int role )
{
Expand Down
6 changes: 6 additions & 0 deletions src/core/qgsmaplayermodel.h
Expand Up @@ -145,6 +145,12 @@ class CORE_EXPORT QgsMapLayerModel : public QAbstractItemModel
bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
Qt::ItemFlags flags( const QModelIndex &index ) const override;

/**
* Returns the icon corresponding to a specified map \a layer.
* \since QGIS 3.0
*/
static QIcon iconForLayer( QgsMapLayer *layer );

protected slots:
void removeLayers( const QStringList &layerIds );
void addLayers( const QList<QgsMapLayer *> &layers );
Expand Down
6 changes: 6 additions & 0 deletions src/gui/locator/qgslocatorfilter.h
Expand Up @@ -22,6 +22,7 @@
#include "qgslogger.h"
#include <QString>
#include <QVariant>
#include <QIcon>

class QgsFeedback;
class QgsLocatorFilter;
Expand Down Expand Up @@ -62,6 +63,11 @@ class GUI_EXPORT QgsLocatorResult
*/
QVariant userData;

/**
* Icon for result.
*/
QIcon icon;

};

/**
Expand Down
3 changes: 3 additions & 0 deletions src/gui/locator/qgslocatorwidget.cpp
Expand Up @@ -272,6 +272,9 @@ QVariant QgsLocatorModel::data( const QModelIndex &index, int role ) const
case Qt::EditRole:
return mResults.at( index.row() ).displayString;

case Qt::DecorationRole:
return mResults.at( index.row() ).icon;

case ResultDataRole:
return QVariant::fromValue( mResults.at( index.row() ) );
}
Expand Down

0 comments on commit 1174b33

Please sign in to comment.