30#include <Qsci/qscilexerpython.h>
31#include <QDesktopServices>
39 , mAPISFilesList( filenames )
54 setEdgeMode( QsciScintilla::EdgeLine );
58 setWhitespaceVisibility( QsciScintilla::WsVisibleAfterIndent );
63 QsciLexerPython *pyLexer =
new QgsQsciLexerPython(
this );
65 pyLexer->setIndentationWarning( QsciLexerPython::Inconsistent );
66 pyLexer->setFoldComments(
true );
67 pyLexer->setFoldQuotes(
true );
69 pyLexer->setDefaultFont( font );
72 pyLexer->setFont( font, -1 );
74 font.setItalic(
true );
75 pyLexer->setFont( font, QsciLexerPython::Comment );
76 pyLexer->setFont( font, QsciLexerPython::CommentBlock );
78 font.setItalic(
false );
80 pyLexer->setFont( font, QsciLexerPython::SingleQuotedString );
81 pyLexer->setFont( font, QsciLexerPython::DoubleQuotedString );
83 pyLexer->setColor(
defaultColor, QsciLexerPython::Default );
98 QsciAPIs *apis =
new QsciAPIs( pyLexer );
102 if ( mAPISFilesList.isEmpty() )
104 if ( settings.
value( QStringLiteral(
"pythonConsole/preloadAPI" ),
true ).toBool() )
107 apis->loadPrepared( mPapFile );
109 else if ( settings.
value( QStringLiteral(
"pythonConsole/usePreparedAPIFile" ),
false ).toBool() )
111 apis->loadPrepared( settings.
value( QStringLiteral(
"pythonConsole/preparedAPIFile" ) ).toString() );
115 const QStringList apiPaths = settings.
value( QStringLiteral(
"pythonConsole/userAPI" ) ).toStringList();
116 for (
const QString &path : apiPaths )
118 if ( !QFileInfo::exists( path ) )
120 QgsDebugMsg( QStringLiteral(
"The apis file %1 was not found" ).arg( path ) );
128 pyLexer->setAPIs( apis );
131 else if ( mAPISFilesList.length() == 1 && mAPISFilesList[0].right( 3 ) == QLatin1String(
"pap" ) )
133 if ( !QFileInfo::exists( mAPISFilesList[0] ) )
135 QgsDebugMsg( QStringLiteral(
"The apis file %1 not found" ).arg( mAPISFilesList.at( 0 ) ) );
138 mPapFile = mAPISFilesList[0];
139 apis->loadPrepared( mPapFile );
143 for (
const QString &path : mAPISFilesList )
145 if ( !QFileInfo::exists( path ) )
147 QgsDebugMsg( QStringLiteral(
"The apis file %1 was not found" ).arg( path ) );
155 pyLexer->setAPIs( apis );
159 const int threshold = settings.
value( QStringLiteral(
"pythonConsole/autoCompThreshold" ), 2 ).toInt();
160 setAutoCompletionThreshold( threshold );
161 if ( !settings.
value(
"pythonConsole/autoCompleteEnabled",
true ).toBool() )
163 setAutoCompletionSource( AcsNone );
167 const QString autoCompleteSource = settings.
value( QStringLiteral(
"pythonConsole/autoCompleteSource" ), QStringLiteral(
"fromAPI" ) ).toString();
168 if ( autoCompleteSource == QLatin1String(
"fromDoc" ) )
169 setAutoCompletionSource( AcsDocument );
170 else if ( autoCompleteSource == QLatin1String(
"fromDocAPI" ) )
171 setAutoCompletionSource( AcsAll );
173 setAutoCompletionSource( AcsAPIs );
177 setIndentationsUseTabs(
false );
178 setIndentationGuides(
true );
185 switch ( autoCompletionSource() )
188 autoCompleteFromDocument();
192 autoCompleteFromAPIs();
196 autoCompleteFromAll();
206 mAPISFilesList = filenames;
213 QgsDebugMsgLevel( QStringLiteral(
"The script file: %1" ).arg( script ), 2 );
214 QFile file( script );
215 if ( !file.open( QIODevice::ReadOnly ) )
220 QTextStream in( &file );
222 setText( in.readAll().trimmed() );
231 if ( !hasSelectedText() )
234 QString text = selectedText();
235 text = text.replace( QLatin1String(
">>> " ), QString() ).replace( QLatin1String(
"... " ), QString() ).trimmed();
236 const QString version = QString(
Qgis::version() ).split(
'.' ).mid( 0, 2 ).join(
'.' );
237 QDesktopServices::openUrl( QUrl( QStringLiteral(
"https://qgis.org/pyqgis/%1/search.html?q=%2" ).arg( version, text ) ) );
244QgsQsciLexerPython::QgsQsciLexerPython( QObject *parent )
245 : QsciLexerPython( parent )
250const char *QgsQsciLexerPython::keywords(
int set )
const
254 return "True False and as assert break class continue def del elif else except exec "
255 "finally for from global if import in is lambda None not or pass "
256 "print raise return try while with yield";
259 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 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.
Flag
Flags controlling behavior of code editor.
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.
This class is a composition of two QSettings instances:
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
#define QgsDebugMsgLevel(str, level)