Skip to content

Commit a6b4742

Browse files
committedMar 20, 2023
Use settings registry for new settings
1 parent 1c2aead commit a6b4742

File tree

6 files changed

+81
-20
lines changed

6 files changed

+81
-20
lines changed
 

‎python/console/console_settings.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,11 @@ def saveSettings(self):
212212
settings.setValue("pythonConsole/autoInsertImport", self.autoInsertImport.isChecked())
213213

214214
settings.setValue("pythonConsole/formatOnSave", self.formatOnSave.isChecked())
215-
settings.setValue("pythonConsole/sortImports", self.sortImports.isChecked())
216-
settings.setValue("pythonConsole/formatter", self.formatter.currentText())
217-
settings.setValue("pythonConsole/autopep8Level", self.autopep8Level.value())
218-
settings.setValue("pythonConsole/blackNormalizeQuotes", self.blackNormalizeQuotes.isChecked())
219-
settings.setValue("pythonConsole/maxLineLength", self.maxLineLength.value())
215+
settings.setValue("gui/code-editor/python/sortImports", self.sortImports.isChecked())
216+
settings.setValue("gui/code-editor/python/formatter", self.formatter.currentText())
217+
settings.setValue("gui/code-editor/python/autopep8Level", self.autopep8Level.value())
218+
settings.setValue("gui/code-editor/python/blackNormalizeQuotes", self.blackNormalizeQuotes.isChecked())
219+
settings.setValue("gui/code-editor/python/maxLineLength", self.maxLineLength.value())
220220

221221
def restoreSettings(self):
222222
settings = QgsSettings()
@@ -244,11 +244,11 @@ def restoreSettings(self):
244244
self.autoInsertImport.setChecked(settings.value("pythonConsole/autoInsertImport", False, type=bool))
245245

246246
self.formatOnSave.setChecked(settings.value("pythonConsole/formatOnSave", False, type=bool))
247-
self.sortImports.setChecked(settings.value("pythonConsole/sortImports", True, type=bool))
248-
self.formatter.setCurrentText(settings.value("pythonConsole/formatter", "autopep8", type=str))
249-
self.autopep8Level.setValue(settings.value("pythonConsole/autopep8Level", 1, type=int))
250-
self.blackNormalizeQuotes.setChecked(settings.value("pythonConsole/blackNormalizeQuotes", True, type=bool))
251-
self.maxLineLength.setValue(settings.value("pythonConsole/maxLineLength", 80, type=int))
247+
self.sortImports.setChecked(settings.value("gui/code-editor/python/sortImports", True, type=bool))
248+
self.formatter.setCurrentText(settings.value("gui/code-editor/python/formatter", "autopep8", type=str))
249+
self.autopep8Level.setValue(settings.value("gui/code-editor/python/autopep8Level", 1, type=int))
250+
self.blackNormalizeQuotes.setChecked(settings.value("gui/code-editor/python/blackNormalizeQuotes", True, type=bool))
251+
self.maxLineLength.setValue(settings.value("gui/code-editor/python/maxLineLength", 80, type=int))
252252

253253
if settings.value("pythonConsole/autoCompleteSource") == 'fromDoc':
254254
self.autoCompFromDoc.setChecked(True)

‎python/gui/auto_generated/codeeditors/qgscodeeditor.sip.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ A text editor based on QScintilla2.
8080
%End
8181
public:
8282

83+
84+
8385
enum class Mode
8486
{
8587
ScriptEditor,

‎python/gui/auto_generated/codeeditors/qgscodeeditorpython.sip.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111

1212

13+
1314
class QgsCodeEditorPython : QgsCodeEditor
1415
{
1516
%Docstring(signature="appended")
@@ -28,6 +29,7 @@ code autocompletion.
2829
%End
2930
public:
3031

32+
3133
QgsCodeEditorPython( QWidget *parent /TransferThis/ = 0, const QList<QString> &filenames = QList<QString>(),
3234
QgsCodeEditor::Mode mode = QgsCodeEditor::Mode::ScriptEditor );
3335
%Docstring

‎src/gui/codeeditors/qgscodeeditor.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <QString>
2121
#include "qgscodeeditorcolorscheme.h"
2222
#include "qgis.h"
23+
#include "qgssettingstree.h"
2324

2425
// qscintilla includes
2526
#include <Qsci/qsciapis.h>
@@ -95,6 +96,13 @@ class GUI_EXPORT QgsCodeEditor : public QsciScintilla
9596

9697
public:
9798

99+
100+
#ifndef SIP_RUN
101+
102+
static inline QgsSettingsTreeNode *sTreeCodeEditor = QgsSettingsTree::sTreeGui->createChildNode( QStringLiteral( "code-editor" ) );
103+
104+
#endif
105+
98106
/**
99107
* Code editor modes.
100108
*

‎src/gui/codeeditors/qgscodeeditorpython.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
#include "qgscodeeditorpython.h"
1818
#include "qgslogger.h"
1919
#include "qgssymbollayerutils.h"
20-
#include "qgssettings.h"
2120
#include "qgis.h"
2221
#include "qgspythonrunner.h"
2322
#include "qgsprocessingutils.h"
23+
#include "qgssettingsentryimpl.h"
24+
#include "qgssettings.h"
2425
#include <QWidget>
2526
#include <QString>
2627
#include <QFont>
@@ -42,6 +43,13 @@ const QMap<QString, QString> QgsCodeEditorPython::sCompletionPairs
4243
};
4344
const QStringList QgsCodeEditorPython::sCompletionSingleCharacters{"`", "*"};
4445

46+
const QgsSettingsEntryString *QgsCodeEditorPython::settingCodeFormatter = new QgsSettingsEntryString( QStringLiteral( "formatter" ), sTreePythonCodeEditor, QStringLiteral( "autopep8" ), QStringLiteral( "Python code autoformatter" ) );
47+
const QgsSettingsEntryInteger *QgsCodeEditorPython::settingMaxLineLength = new QgsSettingsEntryInteger( QStringLiteral( "maxLineLength" ), sTreePythonCodeEditor, 80, QStringLiteral( "Maximum line length" ) );
48+
const QgsSettingsEntryBool *QgsCodeEditorPython::settingSortImports = new QgsSettingsEntryBool( QStringLiteral( "sortImports" ), sTreePythonCodeEditor, true, QStringLiteral( "Whether imports should be sorted when auto-formatting code" ) );
49+
const QgsSettingsEntryInteger *QgsCodeEditorPython::settingAutopep8Level = new QgsSettingsEntryInteger( QStringLiteral( "autopep8Level" ), sTreePythonCodeEditor, 1, QStringLiteral( "Autopep8 aggressive level" ) );
50+
const QgsSettingsEntryBool *QgsCodeEditorPython::settingBlackNormalizeQuotes = new QgsSettingsEntryBool( QStringLiteral( "blackNormalizeQuotes" ), sTreePythonCodeEditor, true, QStringLiteral( "Whether quotes should be normalized when auto-formatting code using black" ) );
51+
52+
4553
QgsCodeEditorPython::QgsCodeEditorPython( QWidget *parent, const QList<QString> &filenames, Mode mode )
4654
: QgsCodeEditor( parent,
4755
QString(),
@@ -74,11 +82,9 @@ Qgis::ScriptLanguageCapabilities QgsCodeEditorPython::languageCapabilities() con
7482

7583
void QgsCodeEditorPython::initializeLexer()
7684
{
77-
const QgsSettings settings;
78-
7985
// current line
8086
setEdgeMode( QsciScintilla::EdgeLine );
81-
setEdgeColumn( settings.value( QStringLiteral( "pythonConsole/maxLineLength" ), 80 ).toInt() );
87+
setEdgeColumn( settingMaxLineLength->value() );
8288
setEdgeColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Edge ) );
8389

8490
setWhitespaceVisibility( QsciScintilla::WsVisibleAfterIndent );
@@ -123,6 +129,7 @@ void QgsCodeEditorPython::initializeLexer()
123129

124130
std::unique_ptr< QsciAPIs > apis = std::make_unique< QsciAPIs >( pyLexer );
125131

132+
QgsSettings settings;
126133
if ( mAPISFilesList.isEmpty() )
127134
{
128135
if ( settings.value( QStringLiteral( "pythonConsole/preloadAPI" ), true ).toBool() )
@@ -363,15 +370,14 @@ QString QgsCodeEditorPython::reformatCodeString( const QString &string )
363370
return string;
364371
}
365372

366-
QgsSettings settings;
367-
const QString formatter = settings.value( QStringLiteral( "pythonConsole/formatter" ), QStringLiteral( "autopep8" ) ).toString();
368-
const int maxLineLength = settings.value( QStringLiteral( "pythonConsole/maxLineLength" ), 80 ).toInt();
373+
const QString formatter = settingCodeFormatter->value();
374+
const int maxLineLength = settingMaxLineLength->value();
369375

370376
QString newText = string;
371377

372378
QStringList missingModules;
373379

374-
if ( settings.value( "pythonConsole/sortImports", true ).toBool() )
380+
if ( settingSortImports->value() )
375381
{
376382
const QString defineSortImports = QStringLiteral(
377383
"def __qgis_sort_imports(script):\n"
@@ -412,7 +418,7 @@ QString QgsCodeEditorPython::reformatCodeString( const QString &string )
412418

413419
if ( formatter == QLatin1String( "autopep8" ) )
414420
{
415-
const int level = settings.value( QStringLiteral( "pythonConsole/autopep8Level" ), 1 ).toInt();
421+
const int level = settingAutopep8Level->value();
416422

417423
const QString defineReformat = QStringLiteral(
418424
"def __qgis_reformat(script):\n"
@@ -452,7 +458,7 @@ QString QgsCodeEditorPython::reformatCodeString( const QString &string )
452458
}
453459
else if ( formatter == QLatin1String( "black" ) )
454460
{
455-
const bool normalize = settings.value( QStringLiteral( "pythonConsole/blackNormalizeQuotes" ), true ).toBool();
461+
const bool normalize = settingBlackNormalizeQuotes->value();
456462

457463
if ( !checkSyntax() )
458464
{

‎src/gui/codeeditors/qgscodeeditorpython.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
#include "qgis_gui.h"
2222
#include <Qsci/qscilexerpython.h>
2323

24+
class QgsSettingsEntryInteger;
25+
class QgsSettingsEntryBool;
26+
2427
SIP_IF_MODULE( HAVE_QSCI_SIP )
2528

2629
#ifndef SIP_RUN
@@ -51,6 +54,46 @@ class GUI_EXPORT QgsCodeEditorPython : public QgsCodeEditor
5154

5255
public:
5356

57+
#ifndef SIP_RUN
58+
59+
static inline QgsSettingsTreeNode *sTreePythonCodeEditor = QgsCodeEditor::sTreeCodeEditor->createChildNode( QStringLiteral( "python" ) );
60+
61+
/**
62+
* Code auto formatter.
63+
*
64+
* \since QGIS 3.32
65+
*/
66+
static const QgsSettingsEntryString *settingCodeFormatter;
67+
68+
/**
69+
* Maximum line length.
70+
*
71+
* \since QGIS 3.32
72+
*/
73+
static const QgsSettingsEntryInteger *settingMaxLineLength;
74+
75+
/**
76+
* Whether imports should be sorted when auto formatting code.
77+
*
78+
* \since QGIS 3.32
79+
*/
80+
static const QgsSettingsEntryBool *settingSortImports;
81+
82+
/**
83+
* Autopep8 aggressive level.
84+
*
85+
* \since QGIS 3.32
86+
*/
87+
static const QgsSettingsEntryInteger *settingAutopep8Level;
88+
89+
/**
90+
* Whether imports should be sorted when auto formatting code.
91+
*
92+
* \since QGIS 3.32
93+
*/
94+
static const QgsSettingsEntryBool *settingBlackNormalizeQuotes;
95+
#endif
96+
5497
/**
5598
* Construct a new Python editor.
5699
*

0 commit comments

Comments
 (0)
Please sign in to comment.