Skip to content

Commit

Permalink
move part of the logic of QgsLocatorWidget to core
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Nov 6, 2018
1 parent 79c5b35 commit 078b445
Show file tree
Hide file tree
Showing 8 changed files with 366 additions and 107 deletions.
93 changes: 93 additions & 0 deletions python/core/auto_generated/locator/qgslocatorwidgetcore.sip.in
@@ -0,0 +1,93 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/locator/qgslocatorwidgetcore.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/







class QgsLocatorWidgetCore : QObject
{
%Docstring
The QgsLocatorWidgetCore class provides the core functionality
to be used in a locator widget.

.. versionadded:: 3.6
%End

%TypeHeaderCode
#include "qgslocatorwidgetcore.h"
%End
public:
explicit QgsLocatorWidgetCore( QObject *parent = 0 );
%Docstring
Constructor of QgsLocatorWidgetCore
%End

void setMapCanvasInterface( QgsMapCanvasInterface *canvasInterface );
%Docstring
Set the map canvas interface
%End

void performSearch( const QString &text );
%Docstring
Perform a search
%End

QgsLocator *locator() const;
%Docstring
Returns the locator
%End

QgsLocatorProxyModel *proxyModel() const;
%Docstring
Returns the proxy model
%End

bool hasQueueRequested() const;
%Docstring
Returns true if some text to be search is pending in the queue
%End

bool isRunning() const;
%Docstring
Returns true if the a search is currently running
%End

signals:
void resultAdded();
%Docstring
Emitted when a result is added
%End

void isRunningChanged();
%Docstring
Emitted when the running status changes
%End

void resultsCleared();
%Docstring
Emitted when the results are cleared
%End

public slots:
void invalidateResults();
%Docstring
This will invalidate current search results
%End

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/core/locator/qgslocatorwidgetcore.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
1 change: 1 addition & 0 deletions python/core/core_auto.sip
Expand Up @@ -383,6 +383,7 @@
%Include auto_generated/locator/qgslocator.sip
%Include auto_generated/locator/qgslocatorfilter.sip
%Include auto_generated/locator/qgslocatormodel.sip
%Include auto_generated/locator/qgslocatorwidgetcore.sip
%Include auto_generated/processing/qgsprocessingalgrunnertask.sip
%Include auto_generated/processing/qgsprocessingfeedback.sip
%Include auto_generated/processing/qgsprocessingprovider.sip
Expand Down
1 change: 0 additions & 1 deletion python/gui/auto_generated/locator/qgslocatorwidget.sip.in
Expand Up @@ -64,7 +64,6 @@ Emitted when the configure option is triggered in the widget.
%End

protected:

virtual bool eventFilter( QObject *obj, QEvent *event );


Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -109,6 +109,7 @@ SET(QGIS_CORE_SRCS
locator/qgslocator.cpp
locator/qgslocatorfilter.cpp
locator/qgslocatormodel.cpp
locator/qgslocatorwidgetcore.cpp

processing/qgsprocessingalgorithm.cpp
processing/qgsprocessingalgrunnertask.cpp
Expand Down Expand Up @@ -681,6 +682,7 @@ SET(QGIS_CORE_MOC_HDRS
locator/qgslocator.h
locator/qgslocatorfilter.h
locator/qgslocatormodel.h
locator/qgslocatorwidgetcore.h

processing/qgsprocessingalgrunnertask.h
processing/qgsprocessingfeedback.h
Expand Down
130 changes: 130 additions & 0 deletions src/core/locator/qgslocatorwidgetcore.cpp
@@ -0,0 +1,130 @@
/***************************************************************************
qgslocatorwidgetcore.cpp
------------------
begin : November 2018
copyright : (C) 2018 by Denis Rouzaud
email : denis@opengis.ch
***************************************************************************/

/***************************************************************************
* *
* 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 "qgslocatorwidgetcore.h"
#include "qgslocator.h"
#include "qgslocatormodel.h"
#include "qgsmapcanvasinterface.h"
#include "qgsmapsettings.h"

QgsLocatorWidgetCore::QgsLocatorWidgetCore( QObject *parent )
: QObject( parent )
, mLocator( new QgsLocator( this ) )
, mLocatorModel( new QgsLocatorModel( this ) )
{
mProxyModel = new QgsLocatorProxyModel( mLocatorModel );
mProxyModel->setSourceModel( mLocatorModel );

connect( mLocator, &QgsLocator::foundResult, this, &QgsLocatorWidgetCore::addResult );
connect( mLocator, &QgsLocator::finished, this, &QgsLocatorWidgetCore::searchFinished );
}

void QgsLocatorWidgetCore::setMapCanvasInterface( QgsMapCanvasInterface *canvasInterface )
{
mCanvasInterface = canvasInterface;
}

bool QgsLocatorWidgetCore::isRunning() const
{
return mIsRunning;
}

void QgsLocatorWidgetCore::setIsRunning( bool isRunning )
{
if ( mIsRunning == isRunning )
return;

mIsRunning = isRunning;
emit isRunningChanged();
}

void QgsLocatorWidgetCore::invalidateResults()
{
mLocator->cancelWithoutBlocking();
mLocatorModel->clear();
}

void QgsLocatorWidgetCore::addResult( const QgsLocatorResult &result )
{
mLocatorModel->addResult( result );
emit resultAdded();
}


void QgsLocatorWidgetCore::searchFinished()
{
if ( mHasQueuedRequest )
{
// a queued request was waiting for this - run the queued search now
QString nextSearch = mNextRequestedString;
mNextRequestedString.clear();
mHasQueuedRequest = false;
performSearch( nextSearch );
}
else
{
if ( !mLocator->isRunning() )
setIsRunning( false );
}
}

void QgsLocatorWidgetCore::performSearch( const QString &text )
{
setIsRunning( true );
if ( mLocator->isRunning() )
{
// can't do anything while a query is running, and can't block
// here waiting for the current query to cancel
// so we queue up this string until cancel has happened
mLocator->cancelWithoutBlocking();
mNextRequestedString = text;
mHasQueuedRequest = true;
return;
}
else
{
emit resultsCleared();
mLocatorModel->deferredClear();
mLocator->fetchResults( text, createContext() );
}
}

QgsLocator *QgsLocatorWidgetCore::locator() const
{
return mLocator;
}

QgsLocatorProxyModel *QgsLocatorWidgetCore::proxyModel() const
{
return mProxyModel;
}

bool QgsLocatorWidgetCore::hasQueueRequested() const
{
return mHasQueuedRequest;
}

QgsLocatorContext QgsLocatorWidgetCore::createContext()
{
QgsLocatorContext context;
if ( mCanvasInterface )
{
context.targetExtent = mCanvasInterface->mapSettings().visibleExtent();
context.targetExtentCrs = mCanvasInterface->mapSettings().destinationCrs();
}
return context;
}
97 changes: 97 additions & 0 deletions src/core/locator/qgslocatorwidgetcore.h
@@ -0,0 +1,97 @@
/***************************************************************************
qgslocatorwidgetcore.h
------------------
begin : November 2018
copyright : (C) 2018 by Denis Rouzaud
email : denis@opengis.ch
***************************************************************************/

/***************************************************************************
* *
* 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 QGSLOCATORWIDGETCORE_H
#define QGSLOCATORWIDGETCORE_H

#include <QObject>

#include "qgis_core.h"

class QgsLocatorResult;
class QgsLocator;
class QgsLocatorContext;
class QgsLocatorModel;
class QgsLocatorProxyModel;
class QgsMapCanvasInterface;


/**
* \ingroup core
* The QgsLocatorWidgetCore class provides the core functionality
* to be used in a locator widget.
* \since QGIS 3.6
*/
class CORE_EXPORT QgsLocatorWidgetCore : public QObject
{
Q_OBJECT
Q_PROPERTY( bool isRunning READ isRunning NOTIFY isRunningChanged )
public:
//! Constructor of QgsLocatorWidgetCore
explicit QgsLocatorWidgetCore( QObject *parent = nullptr );

//! Set the map canvas interface
void setMapCanvasInterface( QgsMapCanvasInterface *canvasInterface );

//! Perform a search
Q_INVOKABLE void performSearch( const QString &text );

//! Returns the locator
QgsLocator *locator() const;

//! Returns the proxy model
QgsLocatorProxyModel *proxyModel() const;

//! Returns true if some text to be search is pending in the queue
bool hasQueueRequested() const;

//! Returns true if the a search is currently running
bool isRunning() const;

signals:
//! Emitted when a result is added
void resultAdded();

//! Emitted when the running status changes
void isRunningChanged();

//! Emitted when the results are cleared
void resultsCleared();

public slots:
//! This will invalidate current search results
void invalidateResults();

private slots:
void searchFinished();
void addResult( const QgsLocatorResult &result );

private:
QgsLocatorContext createContext();
void setIsRunning( bool isRunning );

QgsLocator *mLocator = nullptr;
QgsLocatorModel *mLocatorModel = nullptr;
QgsLocatorProxyModel *mProxyModel = nullptr;
QgsMapCanvasInterface *mCanvasInterface = nullptr;

QString mNextRequestedString;
bool mHasQueuedRequest = false;
bool mIsRunning = false;
};

#endif // QGSLOCATORWIDGETCORE_H

0 comments on commit 078b445

Please sign in to comment.