void QgsAfsSourceSelect::buildQuery( const QgsOwsConnection &connection, const QModelIndex& index )
{
  if ( !index.isValid() )
  {
    return;
  }
  QModelIndex filterIndex = index.sibling( index.row(), 4 );
  QString id = index.sibling( index.row(), 0 ).data().toString();

  // Query available fields
  QgsDataSourceUri ds = connection.uri();
  QString url = ds.param( "url" ) + "/" + id;
  ds.removeParam( "url" );
  ds.setParam( "url", url );
  QgsAfsProvider provider( ds.uri() );
  if ( !provider.isValid() )
  {
    return;
  }

  //show expression builder
  QgsExpressionBuilderDialog d( 0, filterIndex.data().toString() );

  //add available attributes to expression builder
  QgsExpressionBuilderWidget* w = d.expressionBuilder();
  w->loadFieldNames( provider.fields() );

  if ( d.exec() == QDialog::Accepted )
  {
    QgsDebugMsg( "Expression text = " + w->expressionText() );
    mModelProxy->setData( filterIndex, QVariant( w->expressionText() ) );
  }
}
QString QgsAmsSourceSelect::getLayerURI( const QgsOwsConnection &connection,
    const QString &layerTitle, const QString & /*layerName*/,
    const QString &crs,
    const QString & /*filter*/,
    const QgsRectangle & /*bBox*/, const QString &layerId ) const
{
  QgsDataSourceUri ds = connection.uri();
  QString url = layerTitle;
  QString trimmedUrl = layerId.isEmpty() ? url : url.left( url.length() - 1 - layerId.length() ); // trim '/0' from end of url -- AMS provider requires this omitted
  ds.removeParam( QStringLiteral( "url" ) );
  ds.setParam( QStringLiteral( "url" ), trimmedUrl );
  ds.setParam( QStringLiteral( "layer" ), layerId );
  ds.setParam( QStringLiteral( "crs" ), crs );
  ds.setParam( QStringLiteral( "format" ), getSelectedImageEncoding() );
  return ds.uri();
}
QString QgsAfsSourceSelect::getLayerURI( const QgsOwsConnection& connection,
    const QString& layerTitle, const QString& /*layerName*/,
    const QString& crs,
    const QString& filter,
    const QgsRectangle& bBox ) const
{
  QgsDataSourceUri ds = connection.uri();
  QString url = ds.param( "url" ) + "/" + layerTitle;
  ds.removeParam( "url" );
  ds.setParam( "url", url );
  ds.setParam( "filter", filter );
  ds.setParam( "crs", crs );
  if ( !bBox.isEmpty() )
  {
    ds.setParam( "bbox", QString( "%1,%2,%3,%4" ).arg( bBox.xMinimum() ).arg( bBox.yMinimum() ).arg( bBox.xMaximum() ).arg( bBox.yMaximum() ) );
  }
  return ds.uri();
}