Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add autocompletion to the virtual layer SQL editor
  • Loading branch information
Hugo Mercier committed Jan 7, 2016
1 parent 8bbdff4 commit 0eb6cca
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/providers/virtual/CMakeLists.txt
Expand Up @@ -2,6 +2,8 @@
########################################################
# Files

QT4_ADD_RESOURCES(QGIS_VLAYER_SQL_FUNCTIONS_RCC sqlfunctionslist.qrc)

QT4_WRAP_CPP(vlayer_provider_MOC_SRCS
qgsvirtuallayerprovider.h
qgsslottofunction.h
Expand All @@ -13,6 +15,7 @@ QT4_WRAP_UI(vlayer_provider_UI_H qgsvirtuallayersourceselectbase.ui)
SET(QGIS_VLAYER_PROVIDER_SRCS
${vlayer_provider_MOC_SRCS}
${vlayer_provider_UI_H}
${QGIS_VLAYER_SQL_FUNCTIONS_RCC}
qgsvirtuallayerprovider.cpp
qgsvirtuallayerfeatureiterator.cpp
qgsvirtuallayerblob.cpp
Expand Down
34 changes: 34 additions & 0 deletions src/providers/virtual/qgsvirtuallayersourceselect.cpp
Expand Up @@ -21,11 +21,13 @@ email : hugo dot mercier at oslandia dot com
#include <layertree/qgslayertreeview.h>
#include <qgsvectorlayer.h>
#include <qgsvectordataprovider.h>
#include <qgsmaplayerregistry.h>
#include <qgsgenericprojectionselector.h>

#include <QUrl>
#include <Qsci/qscilexer.h>
#include <QMessageBox>
#include <QTextStream>

QgsVirtualLayerSourceSelect::QgsVirtualLayerSourceSelect( QWidget* parent, Qt::WindowFlags fl )
: QDialog( parent, fl )
Expand Down Expand Up @@ -73,6 +75,38 @@ QgsVirtualLayerSourceSelect::QgsVirtualLayerSourceSelect( QWidget* parent, Qt::W
}
}
}

// configure auto completion with SQL functions
QsciAPIs* apis = new QsciAPIs( mQueryEdit->lexer() );

Q_INIT_RESOURCE( sqlfunctionslist );
QFile fFile( ":/sqlfunctions/list.txt" );
if ( fFile.open( QIODevice::ReadOnly ) )
{
QTextStream in( &fFile );
while ( !in.atEnd() )
{
apis->add( in.readLine().toLower() + "()" );
}
fFile.close();
}

// configure auto completion with table and column names
foreach ( QgsMapLayer* l, QgsMapLayerRegistry::instance()->mapLayers().values() )
{
if ( l->type() == QgsMapLayer::VectorLayer )
{
apis->add( l->name() );
QgsVectorLayer* vl = static_cast<QgsVectorLayer*>( l );
foreach ( const QgsField& f, vl->fields().toList() )
{
apis->add( f.name() );
}
}
}

apis->prepare();
mQueryEdit->lexer()->setAPIs( apis );
}

QgsVirtualLayerSourceSelect::~QgsVirtualLayerSourceSelect()
Expand Down
60 changes: 60 additions & 0 deletions src/providers/virtual/qgsvirtuallayersqlfunctions.txt
@@ -0,0 +1,60 @@
MakePoint
BuildMbr
ST_MinX
ST_MinY
ST_MaxX
ST_MaxY
ST_GeomFromText
ST_AsText
ST_AsBinary
ST_Dimension
ST_GeometryType
ST_Srid
ST_SetSrid
ST_isEmpty
ST_isSimple
ST_isValid
ST_Boundary
ST_Envelope
ST_X
ST_Y
ST_StartPoint
ST_EndPoint
ST_Length
ST_isClosed
ST_isRing
ST_Simplify
ST_NumPoints
ST_PointN
ST_Centroid
ST_PointOnSurface
ST_Area
ST_ExteriorRing
ST_InteriorRingN
ST_NumGeometries
ST_GeometryN
MbrEqual
MbrDisjoint
MbrTouches
MbrWithin
MbrOverlaps
MbrIntersects
MbrContains
ST_Equals
ST_Disjoint
ST_Touches
ST_Within
ST_Overlaps
ST_Crosses
ST_Intersects
ST_Contains
ST_Relate
ST_Distance
ST_Intersection
ST_Difference
ST_Union
ST_SymDifference
ST_Buffer
ST_ConvexHull
ST_Transform

5 changes: 5 additions & 0 deletions src/providers/virtual/sqlfunctionslist.qrc
@@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/sqlfunctions">
<file alias="list.txt">qgsvirtuallayersqlfunctions.txt</file>
</qresource>
</RCC>

0 comments on commit 0eb6cca

Please sign in to comment.