Skip to content
Permalink
Browse files

Add saving of recent expressions. Fix #6091

  • Loading branch information
NathanW2 committed Jan 22, 2014
1 parent 12a463d commit 5c1fed5b2eb0f6c4dc30e39abd12f11a750d5f32
@@ -16,8 +16,8 @@
#include "qgsexpressionbuilderdialog.h"
#include <QSettings>

QgsExpressionBuilderDialog::QgsExpressionBuilderDialog( QgsVectorLayer* layer, QString startText, QWidget* parent )
: QDialog( parent )
QgsExpressionBuilderDialog::QgsExpressionBuilderDialog( QgsVectorLayer* layer, QString startText, QWidget* parent, QString key )
: QDialog( parent ), mRecentKey( key )
{
setupUi( this );

@@ -27,6 +27,7 @@ QgsExpressionBuilderDialog::QgsExpressionBuilderDialog( QgsVectorLayer* layer, Q
builder->setLayer( layer );
builder->setExpressionText( startText );
builder->loadFieldNames();
builder->loadRecent(mRecentKey);

QSettings settings;
restoreGeometry( settings.value( "/Windows/ExpressionBuilderDialog/geometry" ).toByteArray() );
@@ -55,6 +56,12 @@ void QgsExpressionBuilderDialog::done( int r )
settings.setValue( "/Windows/ExpressionBuilderDialog/geometry", saveGeometry() );
}

void QgsExpressionBuilderDialog::accept()
{
builder->saveToRecent(mRecentKey);
QDialog::accept();
}

void QgsExpressionBuilderDialog::setGeomCalculator( const QgsDistanceArea & da )
{
// Store in child widget only.
@@ -26,7 +26,7 @@
class GUI_EXPORT QgsExpressionBuilderDialog : public QDialog, private Ui::QgsExpressionBuilderDialogBase
{
public:
QgsExpressionBuilderDialog( QgsVectorLayer* layer, QString startText = QString(), QWidget* parent = NULL );
QgsExpressionBuilderDialog(QgsVectorLayer* layer, QString startText = QString(), QWidget* parent = NULL , QString key = "generic" );

/** The builder widget that is used by the dialog */
QgsExpressionBuilderWidget* expressionBuilder();
@@ -48,6 +48,11 @@ class GUI_EXPORT QgsExpressionBuilderDialog : public QDialog, private Ui::QgsExp
* @param r result value (unused)
*/
virtual void done( int r );

virtual void accept();

private:
QString mRecentKey;
};

#endif
@@ -239,6 +239,41 @@ bool QgsExpressionBuilderWidget::isExpressionValid()
return mExpressionValid;
}

void QgsExpressionBuilderWidget::saveToRecent( QString key )
{
QSettings settings;
QString location = QString( "/expressions/recent/%1" ).arg( key );
QStringList expressions = settings.value( location ).toStringList();
expressions.removeAll( this->expressionText() );

expressions.prepend( this->expressionText() );

while ( expressions.count() > 20 )
{
expressions.pop_back();
}

settings.setValue( location, expressions );
this->loadRecent( key );
}

void QgsExpressionBuilderWidget::loadRecent( QString key )
{
QString name = tr( "Recent (%1)" ).arg( key );
if ( mExpressionGroups.contains( name ) )
{
QgsExpressionItem* node = mExpressionGroups.value( name );
node->removeRows(0, node->rowCount() );
}

QSettings settings;
QString location = QString( "/expressions/recent/%1" ).arg( key );
QStringList expressions = settings.value( location ).toStringList();
foreach ( QString expression, expressions ) {
this->registerItem( name, expression, expression, expression );
}
}

void QgsExpressionBuilderWidget::setGeomCalculator( const QgsDistanceArea & da )
{
mDa = da;
@@ -148,6 +148,10 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp

bool isExpressionValid();

void saveToRecent(QString key);

void loadRecent( QString key);

public slots:
void currentChanged( const QModelIndex &index, const QModelIndex & );
void on_expressionTree_doubleClicked( const QModelIndex &index );
@@ -39,6 +39,7 @@ QgsExpressionSelectionDialog::QgsExpressionSelectionDialog( QgsVectorLayer* laye
mExpressionBuilder->setLayer( layer );
mExpressionBuilder->setExpressionText( startText );
mExpressionBuilder->loadFieldNames();
mExpressionBuilder->loadRecent( "Selection" );

QSettings settings;
restoreGeometry( settings.value( "/Windows/ExpressionSelectionDialog/geometry" ).toByteArray() );
@@ -90,6 +91,7 @@ void QgsExpressionSelectionDialog::on_mActionSelect_triggered()
mLayer->setSelectedFeatures( newSelection );

delete expression;
saveRecent();
}

void QgsExpressionSelectionDialog::on_mActionAddToSelection_triggered()
@@ -117,6 +119,7 @@ void QgsExpressionSelectionDialog::on_mActionAddToSelection_triggered()
mLayer->setSelectedFeatures( newSelection );

delete expression;
saveRecent();
}

void QgsExpressionSelectionDialog::on_mActionSelectInstersect_triggered()
@@ -153,6 +156,7 @@ void QgsExpressionSelectionDialog::on_mActionSelectInstersect_triggered()
mLayer->setSelectedFeatures( newSelection );

delete expression;
saveRecent();
}

void QgsExpressionSelectionDialog::on_mActionRemoveFromSelection_triggered()
@@ -189,6 +193,8 @@ void QgsExpressionSelectionDialog::on_mActionRemoveFromSelection_triggered()
mLayer->setSelectedFeatures( newSelection );

delete expression;

saveRecent();
}

void QgsExpressionSelectionDialog::closeEvent( QCloseEvent *closeEvent )
@@ -209,3 +215,8 @@ void QgsExpressionSelectionDialog::done( int r )
QDialog::done( r );
close();
}

void QgsExpressionSelectionDialog::saveRecent()
{
mExpressionBuilder->saveToRecent( "Selection" );
}
@@ -86,6 +86,7 @@ class GUI_EXPORT QgsExpressionSelectionDialog : public QDialog, private Ui::QgsE
virtual void done( int r );

private:
void saveRecent();
QgsVectorLayer* mLayer;
};

0 comments on commit 5c1fed5

Please sign in to comment.