QGIS API Documentation  3.6.0-Noosa (5873452)
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 );
26  mLayer = qgis::make_unique<QgsVectorLayer>( mDefinition.toString(), "layer", "virtual" );
27 }
28 
30 {
31  bool rc = false;
32  try
33  {
34  mLayer->reload(); // blocking call because the loading is postponed
35  rc = mLayer->isValid();
36  }
37  catch ( std::exception &e )
38  {
39  QgsDebugMsg( QStringLiteral( "Reload error: %1" ).arg( e.what() ) );
40  setExceptionText( e.what() );
41  rc = false;
42  }
43  return rc;
44 }
45 
47 {
48  return mDefinition;
49 }
50 
52 {
53  return mLayer.get();
54 }
55 
57 {
58  return mLayer.release();
59 }
60 
62 {
63  mLayer->dataProvider()->cancelReload();
65 }
66 
68 {
69  return mExceptionText;
70 }
71 
73 {
74  mExceptionText = exceptionText;
75 }
bool run() override
Reloads the data.
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QString toString() const
Convert into a QString that can be read by the virtual layer provider.
QString exceptionText() const
Returns the exception text or an empty string if no exceptions were raised.
QgsVectorLayer * takeLayer()
Returns the underlying virtual layer and ownership.
QgsVectorLayer * layer()
Returns the underlying virtual layer.
virtual void cancel()
Notifies the task that it should terminate.
void cancel() override
Cancels the pending query and the parent task.
QgsVirtualLayerDefinition definition() const
Returns the virtual layer definition.
void setLazy(bool lazy)
Sets the lazy mode.
Represents a vector layer which manages a vector based data sets.
QgsVirtualLayerTask(const QgsVirtualLayerDefinition &definition)
Constructor.
void setExceptionText(const QString &exceptionText)
Sets the exceptionText.
Class to manipulate the definition of a virtual layer.