|
| 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 | + |
0 commit comments