Skip to content

Commit 0eb6cca

Browse files
author
Hugo Mercier
committed
Add autocompletion to the virtual layer SQL editor
1 parent 8bbdff4 commit 0eb6cca

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

src/providers/virtual/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
########################################################
33
# Files
44

5+
QT4_ADD_RESOURCES(QGIS_VLAYER_SQL_FUNCTIONS_RCC sqlfunctionslist.qrc)
6+
57
QT4_WRAP_CPP(vlayer_provider_MOC_SRCS
68
qgsvirtuallayerprovider.h
79
qgsslottofunction.h
@@ -13,6 +15,7 @@ QT4_WRAP_UI(vlayer_provider_UI_H qgsvirtuallayersourceselectbase.ui)
1315
SET(QGIS_VLAYER_PROVIDER_SRCS
1416
${vlayer_provider_MOC_SRCS}
1517
${vlayer_provider_UI_H}
18+
${QGIS_VLAYER_SQL_FUNCTIONS_RCC}
1619
qgsvirtuallayerprovider.cpp
1720
qgsvirtuallayerfeatureiterator.cpp
1821
qgsvirtuallayerblob.cpp

src/providers/virtual/qgsvirtuallayersourceselect.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ email : hugo dot mercier at oslandia dot com
2121
#include <layertree/qgslayertreeview.h>
2222
#include <qgsvectorlayer.h>
2323
#include <qgsvectordataprovider.h>
24+
#include <qgsmaplayerregistry.h>
2425
#include <qgsgenericprojectionselector.h>
2526

2627
#include <QUrl>
2728
#include <Qsci/qscilexer.h>
2829
#include <QMessageBox>
30+
#include <QTextStream>
2931

3032
QgsVirtualLayerSourceSelect::QgsVirtualLayerSourceSelect( QWidget* parent, Qt::WindowFlags fl )
3133
: QDialog( parent, fl )
@@ -73,6 +75,38 @@ QgsVirtualLayerSourceSelect::QgsVirtualLayerSourceSelect( QWidget* parent, Qt::W
7375
}
7476
}
7577
}
78+
79+
// configure auto completion with SQL functions
80+
QsciAPIs* apis = new QsciAPIs( mQueryEdit->lexer() );
81+
82+
Q_INIT_RESOURCE( sqlfunctionslist );
83+
QFile fFile( ":/sqlfunctions/list.txt" );
84+
if ( fFile.open( QIODevice::ReadOnly ) )
85+
{
86+
QTextStream in( &fFile );
87+
while ( !in.atEnd() )
88+
{
89+
apis->add( in.readLine().toLower() + "()" );
90+
}
91+
fFile.close();
92+
}
93+
94+
// configure auto completion with table and column names
95+
foreach ( QgsMapLayer* l, QgsMapLayerRegistry::instance()->mapLayers().values() )
96+
{
97+
if ( l->type() == QgsMapLayer::VectorLayer )
98+
{
99+
apis->add( l->name() );
100+
QgsVectorLayer* vl = static_cast<QgsVectorLayer*>( l );
101+
foreach ( const QgsField& f, vl->fields().toList() )
102+
{
103+
apis->add( f.name() );
104+
}
105+
}
106+
}
107+
108+
apis->prepare();
109+
mQueryEdit->lexer()->setAPIs( apis );
76110
}
77111

78112
QgsVirtualLayerSourceSelect::~QgsVirtualLayerSourceSelect()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
MakePoint
2+
BuildMbr
3+
ST_MinX
4+
ST_MinY
5+
ST_MaxX
6+
ST_MaxY
7+
ST_GeomFromText
8+
ST_AsText
9+
ST_AsBinary
10+
ST_Dimension
11+
ST_GeometryType
12+
ST_Srid
13+
ST_SetSrid
14+
ST_isEmpty
15+
ST_isSimple
16+
ST_isValid
17+
ST_Boundary
18+
ST_Envelope
19+
ST_X
20+
ST_Y
21+
ST_StartPoint
22+
ST_EndPoint
23+
ST_Length
24+
ST_isClosed
25+
ST_isRing
26+
ST_Simplify
27+
ST_NumPoints
28+
ST_PointN
29+
ST_Centroid
30+
ST_PointOnSurface
31+
ST_Area
32+
ST_ExteriorRing
33+
ST_InteriorRingN
34+
ST_NumGeometries
35+
ST_GeometryN
36+
MbrEqual
37+
MbrDisjoint
38+
MbrTouches
39+
MbrWithin
40+
MbrOverlaps
41+
MbrIntersects
42+
MbrContains
43+
ST_Equals
44+
ST_Disjoint
45+
ST_Touches
46+
ST_Within
47+
ST_Overlaps
48+
ST_Crosses
49+
ST_Intersects
50+
ST_Contains
51+
ST_Relate
52+
ST_Distance
53+
ST_Intersection
54+
ST_Difference
55+
ST_Union
56+
ST_SymDifference
57+
ST_Buffer
58+
ST_ConvexHull
59+
ST_Transform
60+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<RCC>
2+
<qresource prefix="/sqlfunctions">
3+
<file alias="list.txt">qgsvirtuallayersqlfunctions.txt</file>
4+
</qresource>
5+
</RCC>

0 commit comments

Comments
 (0)