diff --git a/python/console/console_settings.py b/python/console/console_settings.py index 9126340f465..bb73bf2da18 100644 --- a/python/console/console_settings.py +++ b/python/console/console_settings.py @@ -212,11 +212,11 @@ class ConsoleOptionsWidget(QWidget, Ui_SettingsDialogPythonConsole): settings.setValue("pythonConsole/autoInsertImport", self.autoInsertImport.isChecked()) settings.setValue("pythonConsole/formatOnSave", self.formatOnSave.isChecked()) - settings.setValue("pythonConsole/sortImports", self.sortImports.isChecked()) - settings.setValue("pythonConsole/formatter", self.formatter.currentText()) - settings.setValue("pythonConsole/autopep8Level", self.autopep8Level.value()) - settings.setValue("pythonConsole/blackNormalizeQuotes", self.blackNormalizeQuotes.isChecked()) - settings.setValue("pythonConsole/maxLineLength", self.maxLineLength.value()) + settings.setValue("gui/code-editor/python/sortImports", self.sortImports.isChecked()) + settings.setValue("gui/code-editor/python/formatter", self.formatter.currentText()) + settings.setValue("gui/code-editor/python/autopep8Level", self.autopep8Level.value()) + settings.setValue("gui/code-editor/python/blackNormalizeQuotes", self.blackNormalizeQuotes.isChecked()) + settings.setValue("gui/code-editor/python/maxLineLength", self.maxLineLength.value()) def restoreSettings(self): settings = QgsSettings() @@ -244,11 +244,11 @@ class ConsoleOptionsWidget(QWidget, Ui_SettingsDialogPythonConsole): self.autoInsertImport.setChecked(settings.value("pythonConsole/autoInsertImport", False, type=bool)) self.formatOnSave.setChecked(settings.value("pythonConsole/formatOnSave", False, type=bool)) - self.sortImports.setChecked(settings.value("pythonConsole/sortImports", True, type=bool)) - self.formatter.setCurrentText(settings.value("pythonConsole/formatter", "autopep8", type=str)) - self.autopep8Level.setValue(settings.value("pythonConsole/autopep8Level", 1, type=int)) - self.blackNormalizeQuotes.setChecked(settings.value("pythonConsole/blackNormalizeQuotes", True, type=bool)) - self.maxLineLength.setValue(settings.value("pythonConsole/maxLineLength", 80, type=int)) + self.sortImports.setChecked(settings.value("gui/code-editor/python/sortImports", True, type=bool)) + self.formatter.setCurrentText(settings.value("gui/code-editor/python/formatter", "autopep8", type=str)) + self.autopep8Level.setValue(settings.value("gui/code-editor/python/autopep8Level", 1, type=int)) + self.blackNormalizeQuotes.setChecked(settings.value("gui/code-editor/python/blackNormalizeQuotes", True, type=bool)) + self.maxLineLength.setValue(settings.value("gui/code-editor/python/maxLineLength", 80, type=int)) if settings.value("pythonConsole/autoCompleteSource") == 'fromDoc': self.autoCompFromDoc.setChecked(True) diff --git a/python/gui/auto_generated/codeeditors/qgscodeeditor.sip.in b/python/gui/auto_generated/codeeditors/qgscodeeditor.sip.in index 54c2f957672..bac4b5540b6 100644 --- a/python/gui/auto_generated/codeeditors/qgscodeeditor.sip.in +++ b/python/gui/auto_generated/codeeditors/qgscodeeditor.sip.in @@ -80,6 +80,8 @@ A text editor based on QScintilla2. %End public: + + enum class Mode { ScriptEditor, diff --git a/python/gui/auto_generated/codeeditors/qgscodeeditorpython.sip.in b/python/gui/auto_generated/codeeditors/qgscodeeditorpython.sip.in index 382c9072930..114defd9a5b 100644 --- a/python/gui/auto_generated/codeeditors/qgscodeeditorpython.sip.in +++ b/python/gui/auto_generated/codeeditors/qgscodeeditorpython.sip.in @@ -10,6 +10,7 @@ + class QgsCodeEditorPython : QgsCodeEditor { %Docstring(signature="appended") @@ -28,6 +29,7 @@ code autocompletion. %End public: + QgsCodeEditorPython( QWidget *parent /TransferThis/ = 0, const QList &filenames = QList(), QgsCodeEditor::Mode mode = QgsCodeEditor::Mode::ScriptEditor ); %Docstring diff --git a/src/gui/codeeditors/qgscodeeditor.h b/src/gui/codeeditors/qgscodeeditor.h index 2f3d739d6d0..5506ed8c620 100644 --- a/src/gui/codeeditors/qgscodeeditor.h +++ b/src/gui/codeeditors/qgscodeeditor.h @@ -20,6 +20,7 @@ #include #include "qgscodeeditorcolorscheme.h" #include "qgis.h" +#include "qgssettingstree.h" // qscintilla includes #include @@ -95,6 +96,13 @@ class GUI_EXPORT QgsCodeEditor : public QsciScintilla public: + +#ifndef SIP_RUN + + static inline QgsSettingsTreeNode *sTreeCodeEditor = QgsSettingsTree::sTreeGui->createChildNode( QStringLiteral( "code-editor" ) ); + +#endif + /** * Code editor modes. * diff --git a/src/gui/codeeditors/qgscodeeditorpython.cpp b/src/gui/codeeditors/qgscodeeditorpython.cpp index afb66ec9740..06af6ef2a9a 100644 --- a/src/gui/codeeditors/qgscodeeditorpython.cpp +++ b/src/gui/codeeditors/qgscodeeditorpython.cpp @@ -17,10 +17,11 @@ #include "qgscodeeditorpython.h" #include "qgslogger.h" #include "qgssymbollayerutils.h" -#include "qgssettings.h" #include "qgis.h" #include "qgspythonrunner.h" #include "qgsprocessingutils.h" +#include "qgssettingsentryimpl.h" +#include "qgssettings.h" #include #include #include @@ -42,6 +43,13 @@ const QMap QgsCodeEditorPython::sCompletionPairs }; const QStringList QgsCodeEditorPython::sCompletionSingleCharacters{"`", "*"}; +const QgsSettingsEntryString *QgsCodeEditorPython::settingCodeFormatter = new QgsSettingsEntryString( QStringLiteral( "formatter" ), sTreePythonCodeEditor, QStringLiteral( "autopep8" ), QStringLiteral( "Python code autoformatter" ) ); +const QgsSettingsEntryInteger *QgsCodeEditorPython::settingMaxLineLength = new QgsSettingsEntryInteger( QStringLiteral( "maxLineLength" ), sTreePythonCodeEditor, 80, QStringLiteral( "Maximum line length" ) ); +const QgsSettingsEntryBool *QgsCodeEditorPython::settingSortImports = new QgsSettingsEntryBool( QStringLiteral( "sortImports" ), sTreePythonCodeEditor, true, QStringLiteral( "Whether imports should be sorted when auto-formatting code" ) ); +const QgsSettingsEntryInteger *QgsCodeEditorPython::settingAutopep8Level = new QgsSettingsEntryInteger( QStringLiteral( "autopep8Level" ), sTreePythonCodeEditor, 1, QStringLiteral( "Autopep8 aggressive level" ) ); +const QgsSettingsEntryBool *QgsCodeEditorPython::settingBlackNormalizeQuotes = new QgsSettingsEntryBool( QStringLiteral( "blackNormalizeQuotes" ), sTreePythonCodeEditor, true, QStringLiteral( "Whether quotes should be normalized when auto-formatting code using black" ) ); + + QgsCodeEditorPython::QgsCodeEditorPython( QWidget *parent, const QList &filenames, Mode mode ) : QgsCodeEditor( parent, QString(), @@ -74,11 +82,9 @@ Qgis::ScriptLanguageCapabilities QgsCodeEditorPython::languageCapabilities() con void QgsCodeEditorPython::initializeLexer() { - const QgsSettings settings; - // current line setEdgeMode( QsciScintilla::EdgeLine ); - setEdgeColumn( settings.value( QStringLiteral( "pythonConsole/maxLineLength" ), 80 ).toInt() ); + setEdgeColumn( settingMaxLineLength->value() ); setEdgeColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Edge ) ); setWhitespaceVisibility( QsciScintilla::WsVisibleAfterIndent ); @@ -123,6 +129,7 @@ void QgsCodeEditorPython::initializeLexer() std::unique_ptr< QsciAPIs > apis = std::make_unique< QsciAPIs >( pyLexer ); + QgsSettings settings; if ( mAPISFilesList.isEmpty() ) { if ( settings.value( QStringLiteral( "pythonConsole/preloadAPI" ), true ).toBool() ) @@ -363,15 +370,14 @@ QString QgsCodeEditorPython::reformatCodeString( const QString &string ) return string; } - QgsSettings settings; - const QString formatter = settings.value( QStringLiteral( "pythonConsole/formatter" ), QStringLiteral( "autopep8" ) ).toString(); - const int maxLineLength = settings.value( QStringLiteral( "pythonConsole/maxLineLength" ), 80 ).toInt(); + const QString formatter = settingCodeFormatter->value(); + const int maxLineLength = settingMaxLineLength->value(); QString newText = string; QStringList missingModules; - if ( settings.value( "pythonConsole/sortImports", true ).toBool() ) + if ( settingSortImports->value() ) { const QString defineSortImports = QStringLiteral( "def __qgis_sort_imports(script):\n" @@ -412,7 +418,7 @@ QString QgsCodeEditorPython::reformatCodeString( const QString &string ) if ( formatter == QLatin1String( "autopep8" ) ) { - const int level = settings.value( QStringLiteral( "pythonConsole/autopep8Level" ), 1 ).toInt(); + const int level = settingAutopep8Level->value(); const QString defineReformat = QStringLiteral( "def __qgis_reformat(script):\n" @@ -452,7 +458,7 @@ QString QgsCodeEditorPython::reformatCodeString( const QString &string ) } else if ( formatter == QLatin1String( "black" ) ) { - const bool normalize = settings.value( QStringLiteral( "pythonConsole/blackNormalizeQuotes" ), true ).toBool(); + const bool normalize = settingBlackNormalizeQuotes->value(); if ( !checkSyntax() ) { diff --git a/src/gui/codeeditors/qgscodeeditorpython.h b/src/gui/codeeditors/qgscodeeditorpython.h index 0721dd5cef1..ef8d84f27f8 100644 --- a/src/gui/codeeditors/qgscodeeditorpython.h +++ b/src/gui/codeeditors/qgscodeeditorpython.h @@ -21,6 +21,9 @@ #include "qgis_gui.h" #include +class QgsSettingsEntryInteger; +class QgsSettingsEntryBool; + SIP_IF_MODULE( HAVE_QSCI_SIP ) #ifndef SIP_RUN @@ -51,6 +54,46 @@ class GUI_EXPORT QgsCodeEditorPython : public QgsCodeEditor public: +#ifndef SIP_RUN + + static inline QgsSettingsTreeNode *sTreePythonCodeEditor = QgsCodeEditor::sTreeCodeEditor->createChildNode( QStringLiteral( "python" ) ); + + /** + * Code auto formatter. + * + * \since QGIS 3.32 + */ + static const QgsSettingsEntryString *settingCodeFormatter; + + /** + * Maximum line length. + * + * \since QGIS 3.32 + */ + static const QgsSettingsEntryInteger *settingMaxLineLength; + + /** + * Whether imports should be sorted when auto formatting code. + * + * \since QGIS 3.32 + */ + static const QgsSettingsEntryBool *settingSortImports; + + /** + * Autopep8 aggressive level. + * + * \since QGIS 3.32 + */ + static const QgsSettingsEntryInteger *settingAutopep8Level; + + /** + * Whether imports should be sorted when auto formatting code. + * + * \since QGIS 3.32 + */ + static const QgsSettingsEntryBool *settingBlackNormalizeQuotes; +#endif + /** * Construct a new Python editor. *