Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #35901 from alexbruy/remove-functions
[feature] ability to remove custom functions (fix #27513)
  • Loading branch information
alexbruy committed Apr 30, 2020
2 parents 765aa9c + 075b8eb commit 60d5684
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
38 changes: 36 additions & 2 deletions src/gui/qgsexpressionbuilderwidget.cpp
Expand Up @@ -78,7 +78,8 @@ QgsExpressionBuilderWidget::QgsExpressionBuilderWidget( QWidget *parent )
setupUi( this );

connect( btnRun, &QToolButton::pressed, this, &QgsExpressionBuilderWidget::btnRun_pressed );
connect( btnNewFile, &QToolButton::pressed, this, &QgsExpressionBuilderWidget::btnNewFile_pressed );
connect( btnNewFile, &QPushButton::pressed, this, &QgsExpressionBuilderWidget::btnNewFile_pressed );
connect( btnRemoveFile, &QPushButton::pressed, this, &QgsExpressionBuilderWidget::btnRemoveFile_pressed );
connect( cmbFileNames, &QListWidget::currentItemChanged, this, &QgsExpressionBuilderWidget::cmbFileNames_currentItemChanged );
connect( txtExpressionString, &QgsCodeEditorExpression::textChanged, this, &QgsExpressionBuilderWidget::txtExpressionString_textChanged );
connect( txtPython, &QgsCodeEditorPython::textChanged, this, &QgsExpressionBuilderWidget::txtPython_textChanged );
Expand Down Expand Up @@ -150,7 +151,6 @@ QgsExpressionBuilderWidget::QgsExpressionBuilderWidget( QWidget *parent )
mShowHelpButton->setEnabled( functionsplit->sizes().at( 1 ) == 0 );
} );


QgsSettings settings;
splitter->restoreState( settings.value( QStringLiteral( "Windows/QgsExpressionBuilderWidget/splitter" ) ).toByteArray() );
editorSplit->restoreState( settings.value( QStringLiteral( "Windows/QgsExpressionBuilderWidget/editorsplitter" ) ).toByteArray() );
Expand All @@ -163,6 +163,7 @@ QgsExpressionBuilderWidget::QgsExpressionBuilderWidget( QWidget *parent )
{
QgsPythonRunner::eval( QStringLiteral( "qgis.user.expressionspath" ), mFunctionsPath );
updateFunctionFileList( mFunctionsPath );
btnRemoveFile->setEnabled( cmbFileNames->count() > 0 );
}
else
{
Expand Down Expand Up @@ -422,6 +423,39 @@ void QgsExpressionBuilderWidget::btnNewFile_pressed()
}
}

void QgsExpressionBuilderWidget::btnRemoveFile_pressed()
{
if ( QMessageBox::question( this, tr( "Remove File" ),
tr( "Are you sure you want to remove current functions file?" ),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) == QMessageBox::No )
return;

int currentRow = cmbFileNames->currentRow();
QString fileName = cmbFileNames->currentItem()->text();
if ( QFile::remove( mFunctionsPath + QDir::separator() + fileName.append( ".py" ) ) )
{
{
QListWidgetItem *itemToRemove = whileBlocking( cmbFileNames )->takeItem( currentRow );
delete itemToRemove;
}

if ( cmbFileNames->count() > 0 )
{
cmbFileNames->setCurrentRow( currentRow > 0 ? currentRow - 1 : 0 );
loadCodeFromFile( mFunctionsPath + QDir::separator() + cmbFileNames->currentItem()->text() );
}
else
{
btnRemoveFile->setEnabled( false );
txtPython->clear();
}
}
else
{
QMessageBox::warning( this, tr( "Remove file" ), tr( "Failed to remove function file '%1'." ).arg( fileName ) );
}
}

void QgsExpressionBuilderWidget::cmbFileNames_currentItemChanged( QListWidgetItem *item, QListWidgetItem *lastitem )
{
if ( lastitem )
Expand Down
1 change: 1 addition & 0 deletions src/gui/qgsexpressionbuilderwidget.h
Expand Up @@ -340,6 +340,7 @@ class GUI_EXPORT QgsExpressionBuilderWidget : public QWidget, private Ui::QgsExp
void operatorButtonClicked();
void btnRun_pressed();
void btnNewFile_pressed();
void btnRemoveFile_pressed();

/**
* Display a file dialog to choose where to store the exported expressions JSON file
Expand Down
24 changes: 22 additions & 2 deletions src/ui/qgsexpressionbuilder.ui
Expand Up @@ -35,7 +35,7 @@
<item row="0" column="0">
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>0</number>
<number>1</number>
</property>
<property name="documentMode">
<bool>true</bool>
Expand Down Expand Up @@ -835,7 +835,27 @@ Change the name of the script and save to allow QGIS to auto load on startup.</s
</property>
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/console/iconNewTabEditorConsole.svg</normaloff>:/images/themes/default/console/iconNewTabEditorConsole.svg</iconset>
<normaloff>:/images/themes/default/symbologyAdd.svg</normaloff>:/images/themes/default/symbologyAdd.svg</iconset>
</property>
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnRemoveFile">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Remove selected functions file.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/symbologyRemove.svg</normaloff>:/images/themes/default/symbologyRemove.svg</iconset>
</property>
<property name="iconSize">
<size>
Expand Down

0 comments on commit 60d5684

Please sign in to comment.