27 #include <QMessageBox>
28 #include <QTextStream>
29 #include <Qsci/qscilexerpython.h>
30 #include <QDesktopServices>
34 , mAPISFilesList( filenames )
49 setEdgeMode( QsciScintilla::EdgeLine );
53 setWhitespaceVisibility( QsciScintilla::WsVisibleAfterIndent );
58 QsciLexerPython *pyLexer =
new QgsQsciLexerPython(
this );
60 pyLexer->setIndentationWarning( QsciLexerPython::Inconsistent );
61 pyLexer->setFoldComments(
true );
62 pyLexer->setFoldQuotes(
true );
64 pyLexer->setDefaultFont( font );
67 pyLexer->setFont( font, -1 );
69 font.setItalic(
true );
70 pyLexer->setFont( font, QsciLexerPython::Comment );
71 pyLexer->setFont( font, QsciLexerPython::CommentBlock );
73 font.setItalic(
false );
75 pyLexer->setFont( font, QsciLexerPython::SingleQuotedString );
76 pyLexer->setFont( font, QsciLexerPython::DoubleQuotedString );
78 pyLexer->setColor(
defaultColor, QsciLexerPython::Default );
93 QsciAPIs *apis =
new QsciAPIs( pyLexer );
97 if ( mAPISFilesList.isEmpty() )
99 if ( settings.
value( QStringLiteral(
"pythonConsole/preloadAPI" ),
true ).toBool() )
102 apis->loadPrepared( mPapFile );
104 else if ( settings.
value( QStringLiteral(
"pythonConsole/usePreparedAPIFile" ),
false ).toBool() )
106 apis->loadPrepared( settings.
value( QStringLiteral(
"pythonConsole/preparedAPIFile" ) ).toString() );
110 const QStringList apiPaths = settings.
value( QStringLiteral(
"pythonConsole/userAPI" ) ).toStringList();
111 for (
const QString &path : apiPaths )
113 if ( !QFileInfo::exists( path ) )
115 QgsDebugMsg( QStringLiteral(
"The apis file %1 was not found" ).arg( path ) );
123 pyLexer->setAPIs( apis );
126 else if ( mAPISFilesList.length() == 1 && mAPISFilesList[0].right( 3 ) == QLatin1String(
"pap" ) )
128 if ( !QFileInfo::exists( mAPISFilesList[0] ) )
130 QgsDebugMsg( QStringLiteral(
"The apis file %1 not found" ).arg( mAPISFilesList.at( 0 ) ) );
133 mPapFile = mAPISFilesList[0];
134 apis->loadPrepared( mPapFile );
138 for (
const QString &path : mAPISFilesList )
140 if ( !QFileInfo::exists( path ) )
142 QgsDebugMsg( QStringLiteral(
"The apis file %1 was not found" ).arg( path ) );
150 pyLexer->setAPIs( apis );
154 const int threshold = settings.
value( QStringLiteral(
"pythonConsole/autoCompThreshold" ), 2 ).toInt();
155 setAutoCompletionThreshold( threshold );
156 if ( !settings.
value(
"pythonConsole/autoCompleteEnabled",
true ).toBool() )
158 setAutoCompletionSource( AcsNone );
162 QString autoCompleteSource = settings.
value( QStringLiteral(
"pythonConsole/autoCompleteSource" ), QStringLiteral(
"fromAPI" ) ).toString();
163 if ( autoCompleteSource == QLatin1String(
"fromDoc" ) )
164 setAutoCompletionSource( AcsDocument );
165 else if ( autoCompleteSource == QLatin1String(
"fromDocAPI" ) )
166 setAutoCompletionSource( AcsAll );
168 setAutoCompletionSource( AcsAPIs );
173 setIndentationsUseTabs(
false );
174 setIndentationGuides(
true );
181 switch ( autoCompletionSource() )
184 autoCompleteFromDocument();
188 autoCompleteFromAPIs();
192 autoCompleteFromAll();
202 mAPISFilesList = filenames;
209 QgsDebugMsgLevel( QStringLiteral(
"The script file: %1" ).arg( script ), 2 );
210 QFile file( script );
211 if ( !file.open( QIODevice::ReadOnly ) )
216 QTextStream in( &file );
218 setText( in.readAll().trimmed() );
227 if ( !hasSelectedText() )
230 QString text = selectedText();
231 text = text.replace( QLatin1String(
">>> " ), QString() ).replace( QLatin1String(
"... " ), QString() ).trimmed();
232 const QString version = QString(
Qgis::version() ).split(
'.' ).mid( 0, 2 ).join(
'.' );
233 QDesktopServices::openUrl( QUrl( QStringLiteral(
"https://qgis.org/pyqgis/%1/search.html?q=%2" ).arg( version, text ) ) );
239 QgsQsciLexerPython::QgsQsciLexerPython( QObject *parent )
240 : QsciLexerPython( parent )
245 const char *QgsQsciLexerPython::keywords(
int set )
const
249 return "True False and as assert break class continue def del elif else except exec "
250 "finally for from global if import in is lambda None not or pass "
251 "print raise return try while with yield";
254 return QsciLexerPython::keywords( set );