Skip to content
Permalink
Browse files

Stored expressions initial implementation

  • Loading branch information
elpaso committed Nov 28, 2019
1 parent b42a31e commit e8d76e2a92085d53010a60562a47e010bf2c6870
@@ -217,14 +217,38 @@ Registers a node item for the expression builder.

void saveToRecent( const QString &collection = "generic" );
%Docstring
Adds the current expression to the given collection.
Adds the current expression to the given ``collection``.
By default it is saved to the collection "generic".
%End

void loadRecent( const QString &collection = "generic" );
%Docstring
Loads the recent expressions from the given collection.
Loads the recent expressions from the given ``collection``.
By default it is loaded from the collection "generic".
%End

void loadStored( const QString &collection = "generic" );
%Docstring
Loads the stored expressions from the given ``collection``.
By default it is loaded from the collection "generic".

.. versionadded:: 3.12
%End

void saveToStored( const QString &label, const QString expression, const QString &helpText, const QString &collection = "generic" );
%Docstring
Stores the ``expression`` in the given ``collection`` with given ``label`` and ``helpText``.
By default it is saved to the collection "generic".

.. versionadded:: 3.12
%End

void removeFromStored( const QString &name, const QString &collection = "generic" );
%Docstring
Removes the expression ``name`` from the stored expressions in the given ``collection``.
By default it is removed from the collection "generic".

.. versionadded:: 3.12
%End

void newFunctionFile( const QString &fileName = "scratch" );
@@ -318,6 +342,21 @@ Enabled or disable auto saving. When enabled Python scripts will be auto saved
when text changes.

:param enabled: ``True`` to enable auto saving.
%End

void storeCurrentExpression( );
%Docstring
Adds the current expressions to the stored expressions.

.. versionadded:: 3.12
%End

void removeSelectedExpression( );
%Docstring
Removes the selected expression from the stored expressions,
the selected expression must be a stored expression.

.. versionadded:: 3.12
%End

signals:
@@ -59,7 +59,8 @@ QgsExpressionBuilderWidget::QgsExpressionBuilderWidget( QWidget *parent )
connect( txtSearchEdit, &QgsFilterLineEdit::textChanged, this, &QgsExpressionBuilderWidget::txtSearchEdit_textChanged );
connect( lblPreview, &QLabel::linkActivated, this, &QgsExpressionBuilderWidget::lblPreview_linkActivated );
connect( mValuesListView, &QListView::doubleClicked, this, &QgsExpressionBuilderWidget::mValuesListView_doubleClicked );

connect( btnSaveExpression, &QPushButton::pressed, this, &QgsExpressionBuilderWidget::storeCurrentExpression );
connect( btnRemoveExpression, &QPushButton::pressed, this, &QgsExpressionBuilderWidget::removeSelectedExpression );
txtHelpText->setOpenExternalLinks( true );

mValueGroupBox->hide();
@@ -591,6 +592,65 @@ void QgsExpressionBuilderWidget::loadRecent( const QString &collection )
}
}

void QgsExpressionBuilderWidget::loadStored( const QString &collection )
{
mRecentKey = collection;
QString groupName = tr( "Stored (%1)" ).arg( collection );

// Cleanup
if ( mExpressionGroups.contains( groupName ) )
{
QgsExpressionItem *node = mExpressionGroups.value( groupName );
node->removeRows( 0, node->rowCount() );
}

const QList<QStandardItem *> groupItems { mModel->findItems( groupName ) };
if ( ! groupItems.isEmpty() )
{
const QModelIndex idx { mModel->indexFromItem( groupItems.first() ) };
if ( idx.isValid() )
{
mModel->removeRow( idx.row(), idx.parent() );
}
}

QgsSettings settings;
QString location = QStringLiteral( "/expressions/stored/%1" ).arg( collection );
settings.beginGroup( location, QgsSettings::Section::Gui );
QString label;
QString helpText;
QString expression;
int i = 0;
for ( const auto &label : settings.childKeys() )
{
settings.beginGroup( groupName );
expression = settings.value( QStringLiteral( "expression" ) ).toString();
helpText = settings.value( QStringLiteral( "helpText" ) ).toString();
this->registerItem( groupName, label, expression, helpText, QgsExpressionItem::ExpressionNode, false, i++ );
settings.endGroup();
}
}

void QgsExpressionBuilderWidget::saveToStored( const QString &label, const QString expression, const QString &helpText, const QString &collection )
{
QgsSettings settings;
QString location = QStringLiteral( "/expressions/stored/%1" ).arg( collection );
settings.beginGroup( location, QgsSettings::Section::Gui );
settings.beginGroup( label );
settings.setValue( QStringLiteral( "expression" ), expression );
settings.setValue( QStringLiteral( "helpText" ), helpText );
this->loadStored( collection );
}

void QgsExpressionBuilderWidget::removeFromStored( const QString &name, const QString &collection )
{
QgsSettings settings;
QString location = QStringLiteral( "/expressions/stored/%1" ).arg( collection );
settings.beginGroup( location, QgsSettings::Section::Gui );
settings.remove( name );
this->loadStored( collection );
}

void QgsExpressionBuilderWidget::loadLayers()
{
if ( !mProject )
@@ -1149,6 +1209,27 @@ void QgsExpressionBuilderWidget::autosave()
anim->start( QAbstractAnimation::DeleteWhenStopped );
}

void QgsExpressionBuilderWidget::storeCurrentExpression()
{
const QString expression { this->expressionText() };
saveToStored( expression, expression, expression, mRecentKey );
}

void QgsExpressionBuilderWidget::removeSelectedExpression()
{
QModelIndex idx { expressionTree->currentIndex() };
QgsExpressionItem *item = dynamic_cast<QgsExpressionItem *>( mModel->itemFromIndex( idx ) );
if ( !item )
return;

// Don't handle the double-click if we are on a header node.
if ( item->getItemType() == QgsExpressionItem::Header )
return;

removeFromStored( item->text(), mRecentKey );

}

void QgsExpressionBuilderWidget::indicatorClicked( int line, int index, Qt::KeyboardModifiers state )
{
if ( state & Qt::ControlModifier )
@@ -225,17 +225,38 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
bool isExpressionValid();

/**
* Adds the current expression to the given collection.
* Adds the current expression to the given \a collection.
* By default it is saved to the collection "generic".
*/
void saveToRecent( const QString &collection = "generic" );

/**
* Loads the recent expressions from the given collection.
* Loads the recent expressions from the given \a collection.
* By default it is loaded from the collection "generic".
*/
void loadRecent( const QString &collection = "generic" );

/**
* Loads the stored expressions from the given \a collection.
* By default it is loaded from the collection "generic".
* \since QGIS 3.12
*/
void loadStored( const QString &collection = "generic" );

/**
* Stores the \a expression in the given \a collection with given \a label and \a helpText.
* By default it is saved to the collection "generic".
* \since QGIS 3.12
*/
void saveToStored( const QString &label, const QString expression, const QString &helpText, const QString &collection = "generic" );

/**
* Removes the expression \a name from the stored expressions in the given \a collection.
* By default it is removed from the collection "generic".
* \since QGIS 3.12
*/
void removeFromStored( const QString &name, const QString &collection = "generic" );

/**
* Create a new file in the function editor
*/
@@ -323,6 +344,19 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
*/
void setAutoSave( bool enabled ) { mAutoSave = enabled; }

/**
* Adds the current expressions to the stored expressions.
* \since QGIS 3.12
*/
void storeCurrentExpression( );

/**
* Removes the selected expression from the stored expressions,
* the selected expression must be a stored expression.
* \since QGIS 3.12
*/
void removeSelectedExpression( );

private slots:
void indicatorClicked( int line, int index, Qt::KeyboardModifiers state );
void showContextMenu( QPoint );

0 comments on commit e8d76e2

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