QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsvirtuallayertask.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvirtuallayertask.cpp - description
3  -------------------
4  begin : Jan 19, 2018
5  copyright : (C) 2017 by Paul Blottiere
6  email : [email protected]
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 "qgsvirtuallayertask.h"
19 #include "qgslogger.h"
20 #include "qgsvectorlayer.h"
21 
23  : mDefinition( definition )
24 {
25  mDefinition.setLazy( true );
27  mLayer = qgis::make_unique<QgsVectorLayer>( mDefinition.toString(), QStringLiteral( "layer" ), QLatin1String( "virtual" ), options );
28 }
29 
31 {
32  bool rc = false;
33  try
34  {
35  mLayer->reload(); // blocking call because the loading is postponed
36  rc = mLayer->isValid();
37  }
38  catch ( std::exception &e )
39  {
40  QgsDebugMsg( QStringLiteral( "Reload error: %1" ).arg( e.what() ) );
41  setExceptionText( e.what() );
42  rc = false;
43  }
44  return rc;
45 }
46 
48 {
49  return mDefinition;
50 }
51 
53 {
54  return mLayer.get();
55 }
56 
58 {
59  return mLayer.release();
60 }
61 
63 {
64  mLayer->dataProvider()->cancelReload();
66 }
67 
69 {
70  return mExceptionText;
71 }
72 
73 void QgsVirtualLayerTask::setExceptionText( const QString &exceptionText )
74 {
75  mExceptionText = exceptionText;
76 }
QgsCoordinateTransformContext
Contains information about the context in which a coordinate transform is executed.
Definition: qgscoordinatetransformcontext.h:58
QgsVirtualLayerTask::layer
QgsVectorLayer * layer()
Returns the underlying virtual layer.
Definition: qgsvirtuallayertask.cpp:52
QgsVirtualLayerTask::exceptionText
QString exceptionText() const
Returns the exception text or an empty string if no exceptions were raised.
Definition: qgsvirtuallayertask.cpp:68
QgsVirtualLayerDefinition::setLazy
void setLazy(bool lazy)
Sets the lazy mode.
Definition: qgsvirtuallayerdefinition.h:143
QgsDebugMsg
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QgsVirtualLayerTask::run
bool run() override
Reloads the data.
Definition: qgsvirtuallayertask.cpp:30
QgsTask::cancel
virtual void cancel()
Notifies the task that it should terminate.
Definition: qgstaskmanager.cpp:91
QgsVirtualLayerTask::definition
QgsVirtualLayerDefinition definition() const
Returns the virtual layer definition.
Definition: qgsvirtuallayertask.cpp:47
QgsVirtualLayerTask::cancel
void cancel() override
Cancels the pending query and the parent task.
Definition: qgsvirtuallayertask.cpp:62
qgsvectorlayer.h
QgsVirtualLayerTask::setExceptionText
void setExceptionText(const QString &exceptionText)
Sets the exceptionText.
Definition: qgsvirtuallayertask.cpp:73
QgsVectorLayer::LayerOptions
Setting options for loading vector layers.
Definition: qgsvectorlayer.h:425
qgsvirtuallayertask.h
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsVirtualLayerDefinition
Class to manipulate the definition of a virtual layer.
Definition: qgsvirtuallayerdefinition.h:32
QgsVirtualLayerTask::takeLayer
QgsVectorLayer * takeLayer()
Returns the underlying virtual layer and ownership.
Definition: qgsvirtuallayertask.cpp:57
QgsVirtualLayerTask::QgsVirtualLayerTask
QgsVirtualLayerTask(const QgsVirtualLayerDefinition &definition)
Constructor.
Definition: qgsvirtuallayertask.cpp:22
qgslogger.h
QgsVirtualLayerDefinition::toString
QString toString() const
Convert into a QString that can be read by the virtual layer provider.
Definition: qgsvirtuallayerdefinition.cpp:319