20 #include "qgssettings.h"
28 #include <QMessageBox>
29 #include <QTextStream>
30 #include <Qsci/qscilexerpython.h>
31 #include <QDesktopServices>
35 , mAPISFilesList( filenames )
50 setEdgeMode( QsciScintilla::EdgeLine );
54 setWhitespaceVisibility( QsciScintilla::WsVisibleAfterIndent );
59 QsciLexerPython *pyLexer =
new QgsQsciLexerPython(
this );
61 pyLexer->setIndentationWarning( QsciLexerPython::Inconsistent );
62 pyLexer->setFoldComments(
true );
63 pyLexer->setFoldQuotes(
true );
65 pyLexer->setDefaultFont( font );
68 pyLexer->setFont( font, -1 );
70 font.setItalic(
true );
71 pyLexer->setFont( font, QsciLexerPython::Comment );
72 pyLexer->setFont( font, QsciLexerPython::CommentBlock );
74 font.setItalic(
false );
76 pyLexer->setFont( font, QsciLexerPython::SingleQuotedString );
77 pyLexer->setFont( font, QsciLexerPython::DoubleQuotedString );
79 pyLexer->setColor(
defaultColor, QsciLexerPython::Default );
94 QsciAPIs *apis =
new QsciAPIs( pyLexer );
98 if ( mAPISFilesList.isEmpty() )
100 if ( settings.value( QStringLiteral(
"pythonConsole/preloadAPI" ),
true ).toBool() )
103 apis->loadPrepared( mPapFile );
105 else if ( settings.value( QStringLiteral(
"pythonConsole/usePreparedAPIFile" ),
false ).toBool() )
107 apis->loadPrepared( settings.value( QStringLiteral(
"pythonConsole/preparedAPIFile" ) ).toString() );
111 const QStringList apiPaths = settings.value( QStringLiteral(
"pythonConsole/userAPI" ) ).toStringList();
112 for (
const QString &path : apiPaths )
114 if ( !QFileInfo::exists( path ) )
116 QgsDebugMsg( QStringLiteral(
"The apis file %1 was not found" ).arg( path ) );
124 pyLexer->setAPIs( apis );
127 else if ( mAPISFilesList.length() == 1 && mAPISFilesList[0].right( 3 ) == QLatin1String(
"pap" ) )
129 if ( !QFileInfo::exists( mAPISFilesList[0] ) )
131 QgsDebugMsg( QStringLiteral(
"The apis file %1 not found" ).arg( mAPISFilesList.at( 0 ) ) );
134 mPapFile = mAPISFilesList[0];
135 apis->loadPrepared( mPapFile );
139 for (
const QString &path : mAPISFilesList )
141 if ( !QFileInfo::exists( path ) )
143 QgsDebugMsg( QStringLiteral(
"The apis file %1 was not found" ).arg( path ) );
151 pyLexer->setAPIs( apis );
155 const int threshold = settings.value( QStringLiteral(
"pythonConsole/autoCompThreshold" ), 2 ).toInt();
156 setAutoCompletionThreshold( threshold );
157 if ( !settings.value(
"pythonConsole/autoCompleteEnabled",
true ).toBool() )
159 setAutoCompletionSource( AcsNone );
163 QString autoCompleteSource = settings.value( QStringLiteral(
"pythonConsole/autoCompleteSource" ), QStringLiteral(
"fromAPI" ) ).toString();
164 if ( autoCompleteSource == QLatin1String(
"fromDoc" ) )
165 setAutoCompletionSource( AcsDocument );
166 else if ( autoCompleteSource == QLatin1String(
"fromDocAPI" ) )
167 setAutoCompletionSource( AcsAll );
169 setAutoCompletionSource( AcsAPIs );
174 setIndentationsUseTabs(
false );
175 setIndentationGuides(
true );
182 switch ( autoCompletionSource() )
185 autoCompleteFromDocument();
189 autoCompleteFromAPIs();
193 autoCompleteFromAll();
203 mAPISFilesList = filenames;
210 QgsDebugMsgLevel( QStringLiteral(
"The script file: %1" ).arg( script ), 2 );
211 QFile file( script );
212 if ( !file.open( QIODevice::ReadOnly ) )
217 QTextStream in( &file );
219 setText( in.readAll().trimmed() );
228 if ( !hasSelectedText() )
231 QString text = selectedText();
232 text = text.replace( QLatin1String(
">>> " ), QString() ).replace( QLatin1String(
"... " ), QString() ).trimmed();
233 const QString version = QString(
Qgis::version() ).split(
'.' ).mid( 0, 2 ).join(
'.' );
234 QDesktopServices::openUrl( QUrl( QStringLiteral(
"https://qgis.org/pyqgis/%1/search.html?q=%2" ).arg( version, text ) ) );
241 QgsQsciLexerPython::QgsQsciLexerPython( QObject *parent )
242 : QsciLexerPython( parent )
247 const char *QgsQsciLexerPython::keywords(
int set )
const
251 return "True False and as assert break class continue def del elif else except exec "
252 "finally for from global if import in is lambda None not or pass "
253 "print raise return try while with yield";
256 return QsciLexerPython::keywords( set );
static QString version()
Version string.
static QString pkgDataPath()
Returns the common root path of all application data directories.
@ TripleSingleQuote
Triple single quote color.
@ CommentBlock
Comment block color.
@ Decoration
Decoration color.
@ Identifier
Identifier color.
@ DoubleQuote
Double quote color.
@ Default
Default text color.
@ Background
Background color.
@ SingleQuote
Single quote color.
@ Operator
Operator color.
@ TripleDoubleQuote
Triple double quote color.
void autoComplete()
Triggers the autocompletion popup.
void searchSelectedTextInPyQGISDocs()
Searches the selected text in the official PyQGIS online documentation.
void loadAPIs(const QList< QString > &filenames)
Load APIs from one or more files.
void initializeLexer() override
Called when the dialect specific code lexer needs to be initialized (or reinitialized).
bool loadScript(const QString &script)
Load a script file.
QgsCodeEditorPython(QWidget *parent=nullptr, const QList< QString > &filenames=QList< QString >())
Construct a new Python editor.
A text editor based on QScintilla2.
void setFoldingVisible(bool folding)
Set whether the folding controls are visible in the editor.
void runPostLexerConfigurationTasks()
Performs tasks which must be run after a lexer has been set for the widget.
void setTitle(const QString &title)
Set the widget title.
void setLineNumbersVisible(bool visible)
Sets whether line numbers should be visible in the editor.
QFont lexerFont() const
Returns the font to use in the lexer.
QColor lexerColor(QgsCodeEditorColorScheme::ColorRole role) const
Returns the color to use in the lexer for the specified role.
static QColor defaultColor(QgsCodeEditorColorScheme::ColorRole role, const QString &theme=QString())
Returns the default color for the specified role.
#define QgsDebugMsgLevel(str, level)