Skip to content

Commit

Permalink
Add QgsCodeEditorShell code editor subclass for Bash or Batch scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Apr 24, 2023
1 parent 2cc500d commit 8282b66
Show file tree
Hide file tree
Showing 12 changed files with 572 additions and 2 deletions.
4 changes: 3 additions & 1 deletion python/core/auto_additions/qgis.py
Expand Up @@ -2675,8 +2675,10 @@
Qgis.ScriptLanguage.Python.__doc__ = "Python"
Qgis.ScriptLanguage.R.__doc__ = "R Stats"
Qgis.ScriptLanguage.Sql.__doc__ = "SQL"
Qgis.ScriptLanguage.Batch.__doc__ = "Windows batch files"
Qgis.ScriptLanguage.Bash.__doc__ = "Bash scripts"
Qgis.ScriptLanguage.Unknown.__doc__ = "Unknown/other language"
Qgis.ScriptLanguage.__doc__ = 'Scripting languages.\n\n.. versionadded:: 3.30\n\n' + '* ``Css``: ' + Qgis.ScriptLanguage.Css.__doc__ + '\n' + '* ``QgisExpression``: ' + Qgis.ScriptLanguage.QgisExpression.__doc__ + '\n' + '* ``Html``: ' + Qgis.ScriptLanguage.Html.__doc__ + '\n' + '* ``JavaScript``: ' + Qgis.ScriptLanguage.JavaScript.__doc__ + '\n' + '* ``Json``: ' + Qgis.ScriptLanguage.Json.__doc__ + '\n' + '* ``Python``: ' + Qgis.ScriptLanguage.Python.__doc__ + '\n' + '* ``R``: ' + Qgis.ScriptLanguage.R.__doc__ + '\n' + '* ``Sql``: ' + Qgis.ScriptLanguage.Sql.__doc__ + '\n' + '* ``Unknown``: ' + Qgis.ScriptLanguage.Unknown.__doc__
Qgis.ScriptLanguage.__doc__ = 'Scripting languages.\n\n.. versionadded:: 3.30\n\n' + '* ``Css``: ' + Qgis.ScriptLanguage.Css.__doc__ + '\n' + '* ``QgisExpression``: ' + Qgis.ScriptLanguage.QgisExpression.__doc__ + '\n' + '* ``Html``: ' + Qgis.ScriptLanguage.Html.__doc__ + '\n' + '* ``JavaScript``: ' + Qgis.ScriptLanguage.JavaScript.__doc__ + '\n' + '* ``Json``: ' + Qgis.ScriptLanguage.Json.__doc__ + '\n' + '* ``Python``: ' + Qgis.ScriptLanguage.Python.__doc__ + '\n' + '* ``R``: ' + Qgis.ScriptLanguage.R.__doc__ + '\n' + '* ``Sql``: ' + Qgis.ScriptLanguage.Sql.__doc__ + '\n' + '* ``Batch``: ' + Qgis.ScriptLanguage.Batch.__doc__ + '\n' + '* ``Bash``: ' + Qgis.ScriptLanguage.Bash.__doc__ + '\n' + '* ``Unknown``: ' + Qgis.ScriptLanguage.Unknown.__doc__
# --
Qgis.ScriptLanguage.baseClass = Qgis
# monkey patching scoped based enum
Expand Down
2 changes: 2 additions & 0 deletions python/core/auto_generated/qgis.sip.in
Expand Up @@ -1641,6 +1641,8 @@ The development version
Python,
R,
Sql,
Batch,
Bash,
Unknown,
};

Expand Down
54 changes: 54 additions & 0 deletions python/gui/auto_generated/codeeditors/qgscodeeditorshell.sip.in
@@ -0,0 +1,54 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/codeeditors/qgscodeeditorshell.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/





class QgsCodeEditorShell : QgsCodeEditor
{
%Docstring(signature="appended")
A shell script code editor based on QScintilla2. Adds syntax highlighting and
code autocompletion.

:py:class:`QgsCodeEditorShell` supports either Bash (Linux) or Batch (Windows) code interpreters.
By default the Batch interpreter will be used on Windows platforms and the Bash interpreter
on all other platforms.

.. versionadded:: 3.32
%End

%TypeHeaderCode
#include "qgscodeeditorshell.h"
%End
public:

QgsCodeEditorShell( QWidget *parent /TransferThis/ = 0, QgsCodeEditor::Mode mode = QgsCodeEditor::Mode::ScriptEditor, Qgis::ScriptLanguage language = Qgis::ScriptLanguage::Unknown );
%Docstring
Constructor for QgsCodeEditorShell.

The ``language`` argument may be :py:class:`Qgis`.ScriptLanguage.Unknown, :py:class:`Qgis`.ScriptLanguage.Bash or :py:class:`Qgis`.ScriptLanguage.Batch.
If the ``language`` is :py:class:`Qgis`.ScriptLanguage.Unknown, then the :py:class:`Qgis`.ScriptLanguage.Batch interpreter will be used on Windows platforms and the :py:class:`Qgis`.ScriptLanguage.Bash interpreter
on all other platforms.
%End
virtual Qgis::ScriptLanguage language() const;


protected:
virtual void initializeLexer();


};

/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/codeeditors/qgscodeeditorshell.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
3 changes: 3 additions & 0 deletions python/gui/gui_auto.sip
Expand Up @@ -305,6 +305,9 @@
%Include auto_generated/codeeditors/qgscodeeditorr.sip
%End
%If ( HAVE_QSCI_SIP )
%Include auto_generated/codeeditors/qgscodeeditorshell.sip
%End
%If ( HAVE_QSCI_SIP )
%Include auto_generated/codeeditors/qgscodeeditorsql.sip
%End
%Include auto_generated/devtools/qgsdevtoolwidget.sip
Expand Down
65 changes: 64 additions & 1 deletion src/app/options/qgscodeeditoroptions.cpp
Expand Up @@ -19,6 +19,7 @@
#include "qgis.h"
#include "qgsgui.h"
#include "qgscodeeditorcolorschemeregistry.h"
#include "qgscodeeditorshell.h"

//
// QgsCodeEditorOptionsWidget
Expand Down Expand Up @@ -160,13 +161,27 @@ QgsCodeEditorOptionsWidget::QgsCodeEditorOptionsWidget( QWidget *parent )
updatePreview();
} );

mBashPreview = new QgsCodeEditorShell( nullptr, QgsCodeEditor::Mode::ScriptEditor, Qgis::ScriptLanguage::Bash );
QVBoxLayout *vl = new QVBoxLayout();
vl->setContentsMargins( 0, 0, 0, 0 );
vl->addWidget( mBashPreview );
pageBash->setLayout( vl );

mBatchPreview = new QgsCodeEditorShell( nullptr, QgsCodeEditor::Mode::ScriptEditor, Qgis::ScriptLanguage::Batch );
vl = new QVBoxLayout();
vl->setContentsMargins( 0, 0, 0, 0 );
vl->addWidget( mBatchPreview );
pageBatch->setLayout( vl );

mListLanguage->addItem( tr( "Python" ) );
mListLanguage->addItem( tr( "QGIS Expression" ) );
mListLanguage->addItem( tr( "SQL" ) );
mListLanguage->addItem( tr( "HTML" ) );
mListLanguage->addItem( tr( "CSS" ) );
mListLanguage->addItem( tr( "JavaScript" ) );
mListLanguage->addItem( tr( "R" ) );
mListLanguage->addItem( tr( "Bash" ) );
mListLanguage->addItem( tr( "Batch" ) );

connect( mListLanguage, &QListWidget::currentRowChanged, this, [ = ]
{
Expand Down Expand Up @@ -283,6 +298,52 @@ a_variable <- "My string"
}
)""");


mBashPreview->setText(R"""(#!/bin/bash
# This script takes two arguments: a directory and a file extension.
# It finds all the files in the directory that have the given extension
# and prints out their names and sizes.
[ $# -ne 2 ] && { echo "Usage: $0 <directory> <file_extension>"; exit 1; }
[ ! -d "$1" ] && { echo "Error: $1 does not exist or is not a directory."; exit 1; }
echo "Files with extension .$2 in $1:"
for file in "$1"/*."$2"; do
size=$(stat -c %s "$file")
echo "$(basename "$file"): $((size / 1024)) KB"
done
)""" );

mBatchPreview->setText( R"""(@echo off
REM This script takes two arguments: a directory and a file extension.
REM It finds all the files in the directory that have the given extension
REM and prints out their names and sizes.
if "%~2" == "" (
echo Usage: %0 directory file_extension
exit /b 1
)
if not exist %1 (
echo Error: %1 does not exist or is not a directory.
exit /b 1
)
echo Files with extension %2 in %1:
for %%f in (%1\*.%2) do (
for /f "tokens=3" %%s in ('dir /a:-d /b "%%f" ^| find "File(s)"') do (
echo %%~nxf: %%s bytes
)
)
echo Done.
)""" );

mListLanguage->setCurrentRow( 0 );
mPreviewStackedWidget->setCurrentIndex( 0 );

Expand Down Expand Up @@ -358,13 +419,15 @@ void QgsCodeEditorOptionsWidget::updatePreview()
mCssPreview->setCustomAppearance( theme, colors, fontFamily, fontSize );
mJsPreview->setCustomAppearance( theme, colors, fontFamily, fontSize );
mRPreview->setCustomAppearance( theme, colors, fontFamily, fontSize );
mBashPreview->setCustomAppearance( theme, colors, fontFamily, fontSize );
mBatchPreview->setCustomAppearance( theme, colors, fontFamily, fontSize );
}

//
// QgsCodeEditorOptionsFactory
//
QgsCodeEditorOptionsFactory::QgsCodeEditorOptionsFactory()
: QgsOptionsWidgetFactory( tr( "Code Editor" ), QIcon(), QStringLiteral("code_editor") )
: QgsOptionsWidgetFactory( tr( "Code Editor" ), QIcon(), QStringLiteral( "code_editor" ) )
{

}
Expand Down
5 changes: 5 additions & 0 deletions src/app/options/qgscodeeditoroptions.h
Expand Up @@ -19,6 +19,8 @@
#include "qgsoptionswidgetfactory.h"
#include "qgscodeeditor.h"

class QgsCodeEditorShell;

/**
* \ingroup app
* \class QgsCodeEditorOptionsWidget
Expand Down Expand Up @@ -49,6 +51,9 @@ class QgsCodeEditorOptionsWidget : public QgsOptionsPageWidget, private Ui::QgsC

void updatePreview();

QgsCodeEditorShell *mBashPreview = nullptr;
QgsCodeEditorShell *mBatchPreview = nullptr;


};

Expand Down
2 changes: 2 additions & 0 deletions src/core/qgis.h
Expand Up @@ -2836,6 +2836,8 @@ class CORE_EXPORT Qgis
Python, //!< Python
R, //!< R Stats
Sql, //!< SQL
Batch, //!< Windows batch files
Bash, //!< Bash scripts
Unknown, //!< Unknown/other language
};
Q_ENUM( ScriptLanguage )
Expand Down
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -148,6 +148,7 @@ set(QGIS_GUI_SRCS
codeeditors/qgscodeeditorjson.cpp
codeeditors/qgscodeeditorpython.cpp
codeeditors/qgscodeeditorr.cpp
codeeditors/qgscodeeditorshell.cpp
codeeditors/qgscodeeditorsql.cpp
codeeditors/qgscodeeditorexpression.cpp

Expand Down Expand Up @@ -1057,6 +1058,7 @@ set(QGIS_GUI_HDRS
codeeditors/qgscodeeditorjson.h
codeeditors/qgscodeeditorpython.h
codeeditors/qgscodeeditorr.h
codeeditors/qgscodeeditorshell.h
codeeditors/qgscodeeditorsql.h

devtools/qgsdevtoolwidget.h
Expand Down
4 changes: 4 additions & 0 deletions src/gui/codeeditors/qgscodeeditor.cpp
Expand Up @@ -495,6 +495,10 @@ QString QgsCodeEditor::languageToString( Qgis::ScriptLanguage language )
return tr( "R" );
case Qgis::ScriptLanguage::Sql:
return tr( "SQL" );
case Qgis::ScriptLanguage::Batch:
return tr( "Batch" );
case Qgis::ScriptLanguage::Bash:
return tr( "Bash" );
case Qgis::ScriptLanguage::Unknown:
return QString();
}
Expand Down

0 comments on commit 8282b66

Please sign in to comment.