Skip to content

Commit 30f1153

Browse files
author
Werner Macho
committedAug 27, 2011
Merge branch 'master' of https://github.com/qgis/Quantum-GIS
2 parents a0b8e3c + 96f7736 commit 30f1153

23 files changed

+2329
-186
lines changed
 

‎python/core/core.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
%Include qgsdataprovider.sip
3636
%Include qgsdatasourceuri.sip
3737
%Include qgsdistancearea.sip
38+
%Include qgsexpression.sip
3839
%Include qgsfeature.sip
3940
%Include qgsfield.sip
4041
%Include qgsgeometry.sip

‎python/core/qgsexpression.sip

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
class QgsExpression
3+
{
4+
%TypeHeaderCode
5+
#include "qgsexpression.h"
6+
%End
7+
8+
public:
9+
QgsExpression( const QString& expr );
10+
~QgsExpression();
11+
12+
//! Returns true if an error occurred when parsing the input expression
13+
bool hasParserError() const;
14+
//! Returns parser error
15+
QString parserErrorString() const;
16+
17+
//! Get the expression ready for evaluation - find out column indexes.
18+
bool prepare( const QgsFieldMap& fields );
19+
20+
//! Get list of columns referenced by the expression
21+
QStringList referencedColumns();
22+
//! Returns true if the expression uses feature geometry for some computation
23+
bool needsGeometry();
24+
25+
// evaluation
26+
27+
//! Evaluate the feature and return the result
28+
//! @note prepare() should be called before calling this method
29+
QVariant evaluate( QgsFeature* f = NULL );
30+
31+
//! Evaluate the feature and return the result
32+
//! @note this method does not expect that prepare() has been called on this instance
33+
QVariant evaluate( QgsFeature* f, const QgsFieldMap& fields );
34+
35+
//! Returns true if an error occurred when evaluating last input
36+
bool hasEvalError() const;
37+
//! Returns evaluation error
38+
QString evalErrorString() const;
39+
//! Set evaluation error (used internally by evaluation functions)
40+
void setEvalErrorString( QString str );
41+
42+
//! Set the number for $rownum special column
43+
void setCurrentRowNumber( int rowNumber );
44+
//! Return the number used for $rownum special column
45+
int currentRowNumber();
46+
47+
//! Return the parsed expression as a string - useful for debugging
48+
QString dump() const;
49+
50+
//! Return calculator used for distance and area calculations
51+
//! (used by internal functions)
52+
QgsDistanceArea* geomCalculator();
53+
54+
//
55+
56+
// tells whether the identifier is a name of existing function
57+
static bool isFunctionName( QString name );
58+
59+
// return index of the function in BuiltinFunctions array
60+
static int functionIndex( QString name );
61+
62+
//! return quoted column reference (in double quotes)
63+
static QString quotedColumnRef( QString name );
64+
};

‎python/core/symbology-ng-core.sip

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,14 +407,15 @@ class QgsRuleBasedRendererV2 : QgsFeatureRendererV2
407407
~Rule();
408408
QString dump() const;
409409
QStringList needsFields() const;
410-
bool isFilterOK( const QgsFieldMap& fields, QgsFeature& f ) const;
410+
bool isFilterOK( QgsFeature& f ) const;
411411
bool isScaleOK( double scale ) const;
412412

413413
QgsSymbolV2* symbol();
414414
bool dependsOnScale() const;
415415
int scaleMinDenom() const;
416416
int scaleMaxDenom() const;
417417
QString filterExpression() const;
418+
QgsExpression* filter() const;
418419

419420
void setScaleMinDenom( int scaleMinDenom );
420421
void setScaleMaxDenom( int scaleMaxDenom );

‎python/plugins/plugin_installer/installer_data.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ def removeRepository(self, repo):
511511

512512

513513
# ----------------------------------------- #
514-
def getInstalledPlugin(self, key, readOnly):
514+
def getInstalledPlugin(self, key, readOnly, testLoad=False):
515515
""" get the metadata of an installed plugin """
516516
if readOnly:
517517
path = QgsApplication.pkgDataPath()
@@ -557,10 +557,11 @@ def getInstalledPlugin(self, key, readOnly):
557557
errorDetails = qgisMinimumVersion
558558
except:
559559
pass
560-
try:
561-
exec ("%s.classFactory(iface)" % key)
562-
except Exception, error:
563-
error = unicode(error.args[0])
560+
if testLoad:
561+
try:
562+
exec ("%s.classFactory(iface)" % key)
563+
except Exception, error:
564+
error = unicode(error.args[0])
564565
except Exception, error:
565566
error = unicode(error.args[0])
566567

@@ -596,7 +597,7 @@ def getInstalledPlugin(self, key, readOnly):
596597

597598

598599
# ----------------------------------------- #
599-
def getAllInstalled(self):
600+
def getAllInstalled(self, testLoad=False):
600601
""" Build the localCache """
601602
self.localCache = {}
602603
# first, try to add the read-only plugins...
@@ -625,7 +626,7 @@ def getAllInstalled(self):
625626
for key in pluginDir.entryList():
626627
key = unicode(key)
627628
if not key in [".",".."]:
628-
plugin = self.getInstalledPlugin(key, False)
629+
plugin = self.getInstalledPlugin(key, False, testLoad)
629630
if key in self.localCache.keys() and compareVersions(self.localCache[key]["version_inst"],plugin["version_inst"]) == 1:
630631
# An obsolete plugin in the "user" location is masking a newer one in the "system" location!
631632
self.obsoletePlugins += [key]

‎python/plugins/plugin_installer/installer_gui.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,9 @@ def addItem(p):
528528
n +=1
529529
self.setWindowTitle(self.tr("QGIS Python Plugin Installer") + self.tr(" - %d plugins available" % len(plugins.all())))
530530
self.buttonUpgradeAll.setEnabled( len(self.upgradeablePlugins) )
531+
532+
# initially, keep insert order
533+
self.treePlugins.sortItems(100,Qt.AscendingOrder)
531534

532535
# resize the columns
533536
for i in [0,1,2,3,4,5]:
@@ -537,8 +540,6 @@ def addItem(p):
537540
self.treePlugins.setColumnWidth(i, 260)
538541
if self.treePlugins.columnWidth(3) > 560:
539542
self.treePlugins.setColumnWidth(3, 560)
540-
# initially, keep order of inserting
541-
self.treePlugins.sortItems(100,Qt.AscendingOrder)
542543

543544

544545
# ----------------------------------------- #
@@ -629,7 +630,7 @@ def installPlugin(self, key, quiet=False):
629630
exec ("reload (%s)" % plugin["localdir"])
630631
except:
631632
pass
632-
plugins.getAllInstalled()
633+
plugins.getAllInstalled(testLoad=True)
633634
plugins.rebuild()
634635
plugin = plugins.all()[key]
635636
if not plugin["error"]:

‎src/app/attributetable/qgsattributetabledialog.cpp

100755100644
Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
#include <qgsapplication.h>
2525
#include <qgsvectordataprovider.h>
2626
#include <qgsvectorlayer.h>
27-
#include <qgssearchstring.h>
28-
#include <qgssearchtreenode.h>
27+
#include <qgsexpression.h>
2928

3029
#include "qgisapp.h"
3130
#include "qgsaddattrdialog.h"
@@ -532,23 +531,21 @@ void QgsAttributeTableDialog::updateSelectionFromLayer()
532531
void QgsAttributeTableDialog::doSearch( QString searchString )
533532
{
534533
// parse search string and build parsed tree
535-
QgsSearchString search;
536-
if ( !search.setString( searchString ) )
534+
QgsExpression search( searchString );
535+
if ( search.hasParserError() )
537536
{
538-
QMessageBox::critical( this, tr( "Search string parsing error" ), search.parserErrorMsg() );
537+
QMessageBox::critical( this, tr( "Parsing error" ), search.parserErrorString() );
539538
return;
540539
}
541540

542-
QgsSearchTreeNode* searchTree = search.tree();
543-
if ( searchTree == NULL )
541+
if ( ! search.prepare( mLayer->pendingFields() ) )
544542
{
545-
QMessageBox::information( this, tr( "Search results" ), tr( "You've supplied an empty search string." ) );
546-
return;
543+
QMessageBox::critical( this, tr( "Evaluation error" ), search.evalErrorString() );
547544
}
548545

549546
// TODO: fetch only necessary columns
550-
// QStringList columns = searchTree->referencedColumns();
551-
bool fetchGeom = searchTree->needsGeometry();
547+
//QStringList columns = search.referencedColumns();
548+
bool fetchGeom = search.needsGeometry();
552549

553550
QApplication::setOverrideCursor( Qt::WaitCursor );
554551
mSelectedFeatures.clear();
@@ -558,11 +555,12 @@ void QgsAttributeTableDialog::doSearch( QString searchString )
558555
QgsFeatureList selectedFeatures = mLayer->selectedFeatures();
559556
for ( QgsFeatureList::Iterator it = selectedFeatures.begin(); it != selectedFeatures.end(); ++it )
560557
{
561-
if ( searchTree->checkAgainst( mLayer->pendingFields(), *it ) )
562-
mSelectedFeatures << it->id();
558+
QgsFeature& feat = *it;
559+
if ( search.evaluate( &feat ).toInt() != 0 )
560+
mSelectedFeatures << feat.id();
563561

564562
// check if there were errors during evaluating
565-
if ( searchTree->hasError() )
563+
if ( search.hasEvalError() )
566564
break;
567565
}
568566
}
@@ -573,20 +571,20 @@ void QgsAttributeTableDialog::doSearch( QString searchString )
573571

574572
while ( mLayer->nextFeature( f ) )
575573
{
576-
if ( searchTree->checkAgainst( mLayer->pendingFields(), f ) )
574+
if ( search.evaluate( &f ).toInt() != 0 )
577575
mSelectedFeatures << f.id();
578576

579577
// check if there were errors during evaluating
580-
if ( searchTree->hasError() )
578+
if ( search.hasEvalError() )
581579
break;
582580
}
583581
}
584582

585583
QApplication::restoreOverrideCursor();
586584

587-
if ( searchTree->hasError() )
585+
if ( search.hasEvalError() )
588586
{
589-
QMessageBox::critical( this, tr( "Error during search" ), searchTree->errorMsg() );
587+
QMessageBox::critical( this, tr( "Error during search" ), search.evalErrorString() );
590588
return;
591589
}
592590

@@ -625,12 +623,12 @@ void QgsAttributeTableDialog::search()
625623
QString str;
626624
if ( mQuery->displayText() == nullValue )
627625
{
628-
str = QString( "%1 IS NULL" ).arg( QgsSearchTreeNode::quotedColumnRef( fieldName ) );
626+
str = QString( "%1 IS NULL" ).arg( QgsExpression::quotedColumnRef( fieldName ) );
629627
}
630628
else
631629
{
632630
str = QString( "%1 %2 '%3'" )
633-
.arg( QgsSearchTreeNode::quotedColumnRef( fieldName ) )
631+
.arg( QgsExpression::quotedColumnRef( fieldName ) )
634632
.arg( numeric ? "=" : sensString )
635633
.arg( numeric
636634
? mQuery->displayText().replace( "'", "''" )

‎src/app/qgsattributedialog.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
#include "qgssymbol.h"
2525
#include "qgsattributeeditor.h"
2626
#include "qgshighlight.h"
27-
#include "qgssearchstring.h"
28-
#include "qgssearchtreenode.h"
27+
#include "qgsexpression.h"
2928
#include "qgspythonrunner.h"
3029

3130
#include "qgisapp.h"
@@ -208,15 +207,12 @@ QgsAttributeDialog::QgsAttributeDialog( QgsVectorLayer *vl, QgsFeature *thepFeat
208207
QString expr = le->text();
209208
le->setText( tr( "Error" ) );
210209

211-
QgsSearchString ss;
212-
if ( !ss.setString( expr ) )
210+
QgsExpression exp( expr );
211+
if ( exp.hasParserError() )
213212
continue;
214213

215-
QgsSearchTreeNode *st = ss.tree();
216-
if ( !st )
217-
continue;
218214

219-
if ( !mFeature->geometry() && st->needsGeometry() )
215+
if ( !mFeature->geometry() && exp.needsGeometry() )
220216
{
221217
QgsFeature f;
222218
if ( vl->featureAtId( mFeature->id(), f, true, false ) && f.geometry() )
@@ -225,19 +221,24 @@ QgsAttributeDialog::QgsAttributeDialog( QgsVectorLayer *vl, QgsFeature *thepFeat
225221
}
226222
}
227223

228-
QgsSearchTreeValue value;
229-
st->getValue( value, st, vl->pendingFields(), *mFeature );
224+
QVariant value = exp.evaluate( mFeature, vl->pendingFields() );
230225

231-
if ( !value.isError() )
226+
if ( !exp.hasEvalError() )
232227
{
233-
if ( value.isNumeric() )
234-
le->setText( QString::number( value.number() ) );
235-
else
236-
le->setText( value.string() );
228+
QString text;
229+
switch ( value.type() )
230+
{
231+
case QVariant::Invalid: text = "NULL"; break;
232+
case QVariant::Int: text = QString::number( value.toInt() ); break;
233+
case QVariant::Double: text = QString::number( value.toDouble() ); break;
234+
case QVariant::String:
235+
default: text = value.toString();
236+
}
237+
le->setText( text );
237238
}
238239
else
239240
{
240-
le->setText( tr( "Error: %1" ).arg( st->errorMsg() ) );
241+
le->setText( tr( "Error: %1" ).arg( exp.evalErrorString() ) );
241242
}
242243
}
243244
}

0 commit comments

Comments
 (0)
Please sign in to comment.