QGIS/src/core/qgsvirtuallayertask.cpp

65 lines
2.0 KiB
C++

/***************************************************************************
qgsvirtuallayertask.cpp - description
-------------------
begin : Jan 19, 2018
copyright : (C) 2017 by Paul Blottiere
email : blottiere.paul@gmail.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 "qgsvirtuallayertask.h"
#include "qgslogger.h"
QgsVirtualLayerTask::QgsVirtualLayerTask( const QgsVirtualLayerDefinition &definition )
: QgsTask()
, mDefinition( definition )
{
mDefinition.setLazy( true );
mLayer = qgis::make_unique<QgsVectorLayer>( mDefinition.toString(), "layer", "virtual" );
}
bool QgsVirtualLayerTask::run()
{
bool rc = false;
try
{
mLayer->reload(); // blocking call because the loading is postponed
rc = mLayer->isValid();
}
catch ( std::exception &e )
{
QgsDebugMsg( tr( "Reload error: %1" ).arg( e.what() ) );
rc = false;
}
return rc;
}
QgsVirtualLayerDefinition QgsVirtualLayerTask::definition() const
{
return mDefinition;
}
QgsVectorLayer *QgsVirtualLayerTask::layer()
{
return mLayer.get();
}
QgsVectorLayer *QgsVirtualLayerTask::takeLayer()
{
return mLayer.release();
}
void QgsVirtualLayerTask::cancel()
{
mLayer->dataProvider()->cancelReload();
QgsTask::cancel();
}