Ejemplo n.º 1
0
void QgsAttributeTypeDialog::defaultExpressionChanged()
{
  QString expression = mExpressionWidget->expression();
  if ( expression.isEmpty() )
  {
    mDefaultPreviewLabel->setText( QString() );
    return;
  }

  QgsExpressionContext context = mLayer->createExpressionContext();

  if ( !mPreviewFeature.isValid() )
  {
    // get first feature
    QgsFeatureIterator it = mLayer->getFeatures( QgsFeatureRequest().setLimit( 1 ) );
    it.nextFeature( mPreviewFeature );
  }

  context.setFeature( mPreviewFeature );

  QgsExpression exp = QgsExpression( expression );
  exp.prepare( &context );

  if ( exp.hasParserError() )
  {
    mDefaultPreviewLabel->setText( "<i>" + exp.parserErrorString() + "</i>" );
    return;
  }

  QVariant val = exp.evaluate( &context );
  if ( exp.hasEvalError() )
  {
    mDefaultPreviewLabel->setText( "<i>" + exp.evalErrorString() + "</i>" );
    return;
  }

  QString previewText = val.toString();

  QgsEditorWidgetFactory *factory = QgsEditorWidgetRegistry::instance()->factory( editorWidgetType() );
  if ( factory )
  {
    previewText = factory->representValue( mLayer, mFieldIdx, editorWidgetConfig(), QVariant(), val );
  }

  mDefaultPreviewLabel->setText( "<i>" + previewText + "</i>" );
}
bool QgsFeatureListModel::setDisplayExpression( const QString expression )
{
  const QgsFields fields = mFilterModel->layer()->dataProvider()->fields();

  QgsExpression* exp = new QgsExpression( expression );

  exp->prepare( fields );

  if ( exp->hasParserError() )
  {
    mParserErrorString = exp->parserErrorString();
    delete exp;
    return false;
  }

  delete mExpression;
  mExpression = exp;

  emit( dataChanged( index( 0, 0 ), index( rowCount() - 1, 0 ) ) );
  return true;
}
Ejemplo n.º 3
0
    void test_from_ogc_filter()
    {
      QFETCH( QString, xmlText );
      QFETCH( QString, dumpText );

      QDomDocument doc;
      QVERIFY(doc.setContent(xmlText, true));
      QDomElement rootElem = doc.documentElement();

      QgsExpression* expr = QgsExpression::createFromOgcFilter( rootElem );
      QVERIFY( expr );

      qDebug("OGC XML  : %s", xmlText.toAscii().data() );
      qDebug("EXPR-DUMP: %s", expr->dump().toAscii().data() );

      if ( expr->hasParserError() )
        qDebug( "ERROR: %s ", expr->parserErrorString().toAscii().data() );
      QVERIFY( !expr->hasParserError() );

      QCOMPARE( dumpText, expr->dump() );

      delete expr;
    }
Ejemplo n.º 4
0
bool QgsFeatureListModel::setDisplayExpression( const QString& expression )
{
    QgsExpression* exp = new QgsExpression( expression );

    QgsExpressionContext context;
    context << QgsExpressionContextUtils::globalScope()
            << QgsExpressionContextUtils::projectScope()
            << QgsExpressionContextUtils::layerScope( mFilterModel->layer() );

    exp->prepare( &context );

    if ( exp->hasParserError() )
    {
        mParserErrorString = exp->parserErrorString();
        delete exp;
        return false;
    }

    delete mExpression;
    mExpression = exp;

    emit dataChanged( index( 0, 0 ), index( rowCount() - 1, 0 ) );
    return true;
}