move part of the logic of QgsLocatorWidget to core

This commit is contained in:
Denis Rouzaud 2018-11-02 11:29:10 -04:00
parent 79c5b35a39
commit 078b445057
8 changed files with 366 additions and 107 deletions

View File

@ -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 *
************************************************************************/

View File

@ -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

View File

@ -64,7 +64,6 @@ Emitted when the configure option is triggered in the widget.
%End
protected:
virtual bool eventFilter( QObject *obj, QEvent *event );

View File

@ -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
@ -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

View File

@ -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;
}

View File

@ -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

View File

@ -15,10 +15,10 @@
* *
***************************************************************************/
#include "qgslocatorwidget.h"
#include "qgslocator.h"
#include "qgslocatormodel.h"
#include "qgslocatorwidget.h"
#include "qgslocatorwidgetcore.h"
#include "qgsfilterlineedit.h"
#include "qgsmapcanvas.h"
#include "qgsapplication.h"
@ -29,9 +29,8 @@
QgsLocatorWidget::QgsLocatorWidget( QWidget *parent )
: QWidget( parent )
, mLocator( new QgsLocator( this ) )
, mWidgetCore( new QgsLocatorWidgetCore( this ) )
, mLineEdit( new QgsFilterLineEdit() )
, mLocatorModel( new QgsLocatorModel( this ) )
, mResultsView( new QgsLocatorResultsView() )
{
mLineEdit->setShowClearButton( true );
@ -71,17 +70,16 @@ QgsLocatorWidget::QgsLocatorWidget( QWidget *parent )
mResultsContainer->setLayout( containerLayout );
mResultsContainer->hide();
mProxyModel = new QgsLocatorProxyModel( mLocatorModel );
mProxyModel->setSourceModel( mLocatorModel );
mResultsView->setModel( mProxyModel );
mResultsView->setModel( mWidgetCore->proxyModel() );
mResultsView->setUniformRowHeights( true );
mResultsView->setIconSize( QSize( 16, 16 ) );
mResultsView->recalculateSize();
connect( mLocator, &QgsLocator::foundResult, this, &QgsLocatorWidget::addResult );
connect( mLocator, &QgsLocator::finished, this, &QgsLocatorWidget::searchFinished );
connect( mLineEdit, &QLineEdit::textChanged, this, &QgsLocatorWidget::scheduleDelayedPopup );
connect( mResultsView, &QAbstractItemView::activated, this, &QgsLocatorWidget::acceptCurrentEntry );
connect( mWidgetCore, &QgsLocatorWidgetCore::resultAdded, this, &QgsLocatorWidget::resultAdded );
connect( mWidgetCore, &QgsLocatorWidgetCore::isRunningChanged, this, [ = ]() {mLineEdit->setShowSpinner( mWidgetCore->isRunning() );} );
connect( mWidgetCore, & QgsLocatorWidgetCore::resultsCleared, this, [ = ]() {mHasSelectedResult = false;} );
// have a tiny delay between typing text in line edit and showing the window
mPopupTimer.setInterval( 100 );
@ -97,7 +95,7 @@ QgsLocatorWidget::QgsLocatorWidget( QWidget *parent )
installEventFilter( this );
window()->installEventFilter( this );
mLocator->registerFilter( new QgsLocatorFilterFilter( this, this ) );
mWidgetCore->locator()->registerFilter( new QgsLocatorFilterFilter( this, this ) );
mMenu = new QMenu( this );
QAction *menuAction = mLineEdit->addAction( QgsApplication::getThemeIcon( QStringLiteral( "/search.svg" ) ), QLineEdit::LeadingPosition );
@ -113,12 +111,12 @@ QgsLocatorWidget::QgsLocatorWidget( QWidget *parent )
QgsLocator *QgsLocatorWidget::locator()
{
return mLocator;
return mWidgetCore->locator();
}
void QgsLocatorWidget::setMapCanvas( QgsMapCanvas *canvas )
{
mMapCanvas = canvas;
mWidgetCore->setMapCanvasInterface( canvas );
}
void QgsLocatorWidget::search( const QString &string )
@ -131,8 +129,7 @@ void QgsLocatorWidget::search( const QString &string )
void QgsLocatorWidget::invalidateResults()
{
mLocator->cancelWithoutBlocking();
mLocatorModel->clear();
mWidgetCore->invalidateResults();
mResultsContainer->hide();
}
@ -141,10 +138,27 @@ void QgsLocatorWidget::scheduleDelayedPopup()
mPopupTimer.start();
}
void QgsLocatorWidget::resultAdded()
{
bool selectFirst = !mHasSelectedResult || mWidgetCore->proxyModel()->rowCount() == 0;
if ( selectFirst )
{
int row = -1;
bool selectable = false;
while ( !selectable && row < mWidgetCore->proxyModel()->rowCount() )
{
row++;
selectable = mWidgetCore->proxyModel()->flags( mWidgetCore->proxyModel()->index( row, 0 ) ).testFlag( Qt::ItemIsSelectable );
}
if ( selectable )
mResultsView->setCurrentIndex( mWidgetCore->proxyModel()->index( row, 0 ) );
}
}
void QgsLocatorWidget::performSearch()
{
mPopupTimer.stop();
updateResults( mLineEdit->text() );
mWidgetCore->performSearch( mLineEdit->text() );
showList();
}
@ -156,29 +170,12 @@ void QgsLocatorWidget::showList()
void QgsLocatorWidget::triggerSearchAndShowList()
{
if ( mProxyModel->rowCount() == 0 )
if ( mWidgetCore->proxyModel()->rowCount() == 0 )
performSearch();
else
showList();
}
void QgsLocatorWidget::searchFinished()
{
if ( mHasQueuedRequest )
{
// a queued request was waiting for this - run the queued search now
QString nextSearch = mNextRequestedString;
mNextRequestedString.clear();
mHasQueuedRequest = false;
updateResults( nextSearch );
}
else
{
if ( !mLocator->isRunning() )
mLineEdit->setShowSpinner( false );
}
}
bool QgsLocatorWidget::eventFilter( QObject *obj, QEvent *event )
{
if ( obj == mLineEdit && event->type() == QEvent::KeyPress )
@ -246,28 +243,10 @@ bool QgsLocatorWidget::eventFilter( QObject *obj, QEvent *event )
return QWidget::eventFilter( obj, event );
}
void QgsLocatorWidget::addResult( const QgsLocatorResult &result )
{
bool selectFirst = !mHasSelectedResult || mProxyModel->rowCount() == 0;
mLocatorModel->addResult( result );
if ( selectFirst )
{
int row = -1;
bool selectable = false;
while ( !selectable && row < mProxyModel->rowCount() )
{
row++;
selectable = mProxyModel->flags( mProxyModel->index( row, 0 ) ).testFlag( Qt::ItemIsSelectable );
}
if ( selectable )
mResultsView->setCurrentIndex( mProxyModel->index( row, 0 ) );
}
}
void QgsLocatorWidget::configMenuAboutToShow()
{
mMenu->clear();
for ( QgsLocatorFilter *filter : mLocator->filters() )
for ( QgsLocatorFilter *filter : mWidgetCore->locator()->filters() )
{
if ( !filter->enabled() )
continue;
@ -281,7 +260,7 @@ void QgsLocatorWidget::configMenuAboutToShow()
else
{
QStringList parts = currentText.split( ' ' );
if ( parts.count() > 1 && mLocator->filters( parts.at( 0 ) ).count() > 0 )
if ( parts.count() > 1 && mWidgetCore->locator()->filters( parts.at( 0 ) ).count() > 0 )
{
parts.pop_front();
currentText = parts.join( ' ' );
@ -297,33 +276,13 @@ void QgsLocatorWidget::configMenuAboutToShow()
QAction *configAction = new QAction( tr( "Configure…" ), mMenu );
connect( configAction, &QAction::triggered, this, &QgsLocatorWidget::configTriggered );
mMenu->addAction( configAction );
}
void QgsLocatorWidget::updateResults( const QString &text )
{
mLineEdit->setShowSpinner( 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
{
mHasSelectedResult = false;
mLocatorModel->deferredClear();
mLocator->fetchResults( text, createContext() );
}
}
void QgsLocatorWidget::acceptCurrentEntry()
{
if ( mHasQueuedRequest )
if ( mWidgetCore->hasQueueRequested() )
{
return;
}
@ -336,24 +295,15 @@ void QgsLocatorWidget::acceptCurrentEntry()
if ( !index.isValid() )
return;
QgsLocatorResult result = mProxyModel->data( index, QgsLocatorModel::ResultDataRole ).value< QgsLocatorResult >();
QgsLocatorResult result = mWidgetCore->proxyModel()->data( index, QgsLocatorModel::ResultDataRole ).value< QgsLocatorResult >();
mResultsContainer->hide();
mLineEdit->clearFocus();
mLocator->clearPreviousResults();
mWidgetCore->locator()->clearPreviousResults();
result.filter->triggerResult( result );
}
}
QgsLocatorContext QgsLocatorWidget::createContext()
{
QgsLocatorContext context;
if ( mMapCanvas )
{
context.targetExtent = mMapCanvas->mapSettings().visibleExtent();
context.targetExtentCrs = mMapCanvas->mapSettings().destinationCrs();
}
return context;
}
///@cond PRIVATE

View File

@ -29,10 +29,9 @@
class QgsLocator;
class QgsFilterLineEdit;
class QgsLocatorModel;
class QgsLocatorResultsView;
class QgsMapCanvas;
class QgsLocatorProxyModel;
class QgsLocatorWidgetCore;
/**
* \class QgsLocatorWidget
@ -86,40 +85,29 @@ class GUI_EXPORT QgsLocatorWidget : public QWidget
void configTriggered();
protected:
bool eventFilter( QObject *obj, QEvent *event ) override;
private slots:
void scheduleDelayedPopup();
void performSearch();
void showList();
void triggerSearchAndShowList();
void searchFinished();
void addResult( const QgsLocatorResult &result );
void configMenuAboutToShow();
void scheduleDelayedPopup();
void resultAdded();
private:
QgsLocator *mLocator = nullptr;
QgsLocatorWidgetCore *mWidgetCore = nullptr;
QgsFilterLineEdit *mLineEdit = nullptr;
QgsLocatorModel *mLocatorModel = nullptr;
QgsLocatorProxyModel *mProxyModel = nullptr;
QgsFloatingWidget *mResultsContainer = nullptr;
QgsLocatorResultsView *mResultsView = nullptr;
QgsMapCanvas *mMapCanvas = nullptr;
QMenu *mMenu = nullptr;
QString mNextRequestedString;
bool mHasQueuedRequest = false;
bool mHasSelectedResult = false;
QTimer mPopupTimer;
QTimer mFocusTimer;
QTimer mPopupTimer;
bool mHasSelectedResult = false;
void updateResults( const QString &text );
void acceptCurrentEntry();
QgsLocatorContext createContext();
};
#ifndef SIP_RUN
@ -144,7 +132,6 @@ class QgsLocatorFilterFilter : public QgsLocatorFilter
void triggerResult( const QgsLocatorResult &result ) override;
private:
QgsLocatorWidget *mLocator = nullptr;
};