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 const 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 );