1 /***************************************************************************
2                         qgslayoutlocatorfilters.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 #include "qgslayoutlocatorfilter.h"
19 #include "qgsproject.h"
20 #include "qgsmasterlayoutinterface.h"
21 #include "qgslayoutmanager.h"
22 #include "qgisapp.h"
23 
24 
QgsLayoutLocatorFilter(QObject * parent)25 QgsLayoutLocatorFilter::QgsLayoutLocatorFilter( QObject *parent )
26   : QgsLocatorFilter( parent )
27 {}
28 
clone() const29 QgsLayoutLocatorFilter *QgsLayoutLocatorFilter::clone() const
30 {
31   return new QgsLayoutLocatorFilter();
32 }
33 
fetchResults(const QString & string,const QgsLocatorContext & context,QgsFeedback *)34 void QgsLayoutLocatorFilter::fetchResults( const QString &string, const QgsLocatorContext &context, QgsFeedback * )
35 {
36   const QList< QgsMasterLayoutInterface * > layouts = QgsProject::instance()->layoutManager()->layouts();
37   for ( QgsMasterLayoutInterface *layout : layouts )
38   {
39     // if the layout is broken, don't include it in the results
40     if ( ! layout )
41       continue;
42 
43     QgsLocatorResult result;
44     result.displayString = layout->name();
45     result.userData = layout->name();
46 
47     if ( context.usingPrefix && string.isEmpty() )
48     {
49       emit resultFetched( result );
50       continue;
51     }
52 
53     result.score = fuzzyScore( result.displayString, string );
54 
55     if ( result.score > 0 )
56       emit resultFetched( result );
57   }
58 }
59 
triggerResult(const QgsLocatorResult & result)60 void QgsLayoutLocatorFilter::triggerResult( const QgsLocatorResult &result )
61 {
62   const QString layoutName = result.userData.toString();
63   QgsMasterLayoutInterface *layout = QgsProject::instance()->layoutManager()->layoutByName( layoutName );
64   if ( !layout )
65     return;
66 
67   QgisApp::instance()->openLayoutDesignerDialog( layout );
68 }
69