Skip to content
Permalink
Browse files

Sort functions and variables in expression builder

  • Loading branch information
nyalldawson committed Aug 22, 2015
1 parent 85bda6c commit 440926bbc4969151cb03691390bd97f0478ede02
@@ -12,6 +12,10 @@ class QgsExpressionItemSearchProxy : QSortFilterProxyModel
QgsExpressionItemSearchProxy();

bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const;

protected:

bool lessThan( const QModelIndex &left, const QModelIndex &right ) const;
};

/** An expression item that can be used in the QgsExpressionBuilderWidget tree.
@@ -122,11 +126,12 @@ class QgsExpressionBuilderWidget : QWidget
* @param helpText The help text that the user will see when item is selected.
* @param type The type of the expression item.
* @param highlightedItem set to true to make the item highlighted, which inserts a bold copy of the item at the top level
* @param sortOrder sort ranking for item
*/
void registerItem( QString group, QString label, QString expressionText,
QString helpText = "",
QgsExpressionItem::ItemType type = QgsExpressionItem::ExpressionNode,
bool highlightedItem = false );
bool highlightedItem = false, int sortOrder = 1 );

bool isExpressionValid();

@@ -27,6 +27,8 @@
#include <QDir>
#include <QComboBox>



QgsExpressionBuilderWidget::QgsExpressionBuilderWidget( QWidget *parent )
: QWidget( parent )
, mLayer( NULL )
@@ -41,8 +43,11 @@ QgsExpressionBuilderWidget::QgsExpressionBuilderWidget( QWidget *parent )

mModel = new QStandardItemModel();
mProxyModel = new QgsExpressionItemSearchProxy();
mProxyModel->setDynamicSortFilter( true );
mProxyModel->setSourceModel( mModel );
expressionTree->setModel( mProxyModel );
expressionTree->setSortingEnabled( true );
expressionTree->sortByColumn( 0, Qt::AscendingOrder );

expressionTree->setContextMenuPolicy( Qt::CustomContextMenu );
connect( this, SIGNAL( expressionParsed( bool ) ), this, SLOT( setExpressionState( bool ) ) );
@@ -277,7 +282,7 @@ void QgsExpressionBuilderWidget::loadFieldNames( const QgsFields& fields )
{
QString fieldName = fields[i].name();
fieldNames << fieldName;
registerItem( "Fields and Values", fieldName, " \"" + fieldName + "\" ", "", QgsExpressionItem::Field );
registerItem( "Fields and Values", fieldName, " \"" + fieldName + "\" ", "", QgsExpressionItem::Field, false, i );
}
// highlighter->addFields( fieldNames );
}
@@ -336,10 +341,11 @@ void QgsExpressionBuilderWidget::registerItem( QString group,
QString label,
QString expressionText,
QString helpText,
QgsExpressionItem::ItemType type, bool highlightedItem )
QgsExpressionItem::ItemType type, bool highlightedItem, int sortOrder )
{
QgsExpressionItem* item = new QgsExpressionItem( label, expressionText, helpText, type );
item->setData( label, Qt::UserRole );
item->setData( sortOrder, Qt::UserRole + 1 );

// Look up the group and insert the new function.
if ( mExpressionGroups.contains( group ) )
@@ -352,6 +358,7 @@ void QgsExpressionBuilderWidget::registerItem( QString group,
// If the group doesn't exist yet we make it first.
QgsExpressionItem *newgroupNode = new QgsExpressionItem( QgsExpression::group( group ), "", QgsExpressionItem::Header );
newgroupNode->setData( group, Qt::UserRole );
newgroupNode->setData( group == "Recent (Selection)" ? 2 : 1, Qt::UserRole + 1 );
newgroupNode->appendRow( item );
mModel->appendRow( newgroupNode );
mExpressionGroups.insert( group, newgroupNode );
@@ -362,6 +369,7 @@ void QgsExpressionBuilderWidget::registerItem( QString group,
//insert a copy as a top level item
QgsExpressionItem* topLevelItem = new QgsExpressionItem( label, expressionText, helpText, type );
topLevelItem->setData( label, Qt::UserRole );
item->setData( 0, Qt::UserRole + 1 );
QFont font = topLevelItem->font();
font.setBold( true );
topLevelItem->setFont( font );
@@ -717,3 +725,6 @@ QString QgsExpressionBuilderWidget::loadFunctionHelp( QgsExpressionItem* express
QString myStyle = QgsApplication::reportStyleSheet();
return "<head><style>" + myStyle + "</style></head><body>" + helpContents + "</body>";
}



@@ -45,6 +45,27 @@ class QgsExpressionItemSearchProxy : public QSortFilterProxyModel

return QSortFilterProxyModel::filterAcceptsRow( source_row, source_parent );
}

protected:

bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override
{
int leftSort = sourceModel()->data( left, Qt::UserRole + 1 ).toInt();
int rightSort = sourceModel()->data( right, Qt::UserRole + 1 ).toInt();
if ( leftSort != rightSort )
return leftSort < rightSort;

QString leftString = sourceModel()->data( left, Qt::DisplayRole ).toString();
QString rightString = sourceModel()->data( right, Qt::DisplayRole ).toString();

//ignore $ prefixes when sorting
if ( leftString.startsWith( "$" ) )
leftString = leftString.mid( 1 );
if ( rightString.startsWith( "$" ) )
rightString = rightString.mid( 1 );

return QString::localeAwareCompare( leftString, rightString ) < 0;
}
};

/** An expression item that can be used in the QgsExpressionBuilderWidget tree.
@@ -164,11 +185,12 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
* @param helpText The help text that the user will see when item is selected.
* @param type The type of the expression item.
* @param highlightedItem set to true to make the item highlighted, which inserts a bold copy of the item at the top level
* @param sortOrder sort ranking for item
*/
void registerItem( QString group, QString label, QString expressionText,
QString helpText = "",
QgsExpressionItem::ItemType type = QgsExpressionItem::ExpressionNode,
bool highlightedItem = false );
bool highlightedItem = false, int sortOrder = 1 );

bool isExpressionValid();

@@ -47,6 +47,7 @@ QgsExpressionSelectionDialog::QgsExpressionSelectionDialog( QgsVectorLayer* laye
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::layerScope( mLayer );
context.setHighlightedVariables( QStringList() << "layer_id" << "layer_name" );
mExpressionBuilder->setExpressionContext( context );

QSettings settings;

0 comments on commit 440926b

Please sign in to comment.
You can’t perform that action at this time.