QGIS API Documentation  3.8.0-Zanzibar (11aff65)
qgscodeeditorpython.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscodeeditorpython.cpp - A Python editor based on QScintilla
3  --------------------------------------
4  Date : 06-Oct-2013
5  Copyright : (C) 2013 by Salvatore Larosa
6  Email : lrssvtml (at) gmail (dot) com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgsapplication.h"
17 #include "qgscodeeditorpython.h"
18 #include "qgslogger.h"
19 #include "qgssymbollayerutils.h"
20 
21 #include <QWidget>
22 #include <QString>
23 #include <QFont>
24 #include <QFileInfo>
25 #include <QMessageBox>
26 #include <QTextStream>
27 #include <Qsci/qscilexerpython.h>
28 
29 QgsCodeEditorPython::QgsCodeEditorPython( QWidget *parent, const QList<QString> &filenames )
30  : QgsCodeEditor( parent )
31  , mAPISFilesList( filenames )
32 {
33  if ( !parent )
34  {
35  setTitle( tr( "Python Editor" ) );
36  }
37  setSciLexerPython();
38 }
39 
40 void QgsCodeEditorPython::setSciLexerPython()
41 {
42  QHash< QString, QColor > colors;
43  if ( QgsApplication::instance()->themeName() != QStringLiteral( "default" ) )
44  {
45  QSettings ini( QgsApplication::instance()->uiThemes().value( QgsApplication::instance()->themeName() ) + "/qscintilla.ini", QSettings::IniFormat );
46  for ( const auto &key : ini.allKeys() )
47  {
48  colors.insert( key, QgsSymbolLayerUtils::decodeColor( ini.value( key ).toString() ) );
49  }
50  }
51 
52  // current line
53  setCaretWidth( 2 );
54 
55  setEdgeMode( QsciScintilla::EdgeLine );
56  setEdgeColumn( 80 );
57  setEdgeColor( colors.value( QStringLiteral( "edgeColor" ), QColor( 255, 0, 0 ) ) );
58 
59  setWhitespaceVisibility( QsciScintilla::WsVisibleAfterIndent );
60 
61  QFont font = getMonospaceFont();
62  QColor defaultColor = colors.value( QStringLiteral( "python/defaultFontColor" ), Qt::black );
63 
64  QsciLexerPython *pyLexer = new QsciLexerPython( this );
65  pyLexer->setDefaultFont( font );
66  pyLexer->setDefaultColor( defaultColor );
67  pyLexer->setDefaultPaper( colors.value( QStringLiteral( "python/paperBackgroundColor" ), Qt::white ) );
68  pyLexer->setFont( font, -1 );
69  pyLexer->setColor( defaultColor, QsciLexerPython::Default );
70  pyLexer->setColor( colors.value( QStringLiteral( "python/classFontColor" ), QColor( 66, 113, 174 ) ), QsciLexerPython::ClassName );
71  pyLexer->setColor( colors.value( QStringLiteral( "python/numberFontColor" ), QColor( 200, 40, 41 ) ), QsciLexerPython::Number );
72  pyLexer->setColor( colors.value( QStringLiteral( "python/commentFontColor" ), QColor( 142, 144, 140 ) ), QsciLexerPython::Comment );
73  pyLexer->setColor( colors.value( QStringLiteral( "python/commentBlockFontColor" ), QColor( 142, 144, 140 ) ), QsciLexerPython::CommentBlock );
74  pyLexer->setColor( colors.value( QStringLiteral( "python/keywordFontColor" ), QColor( 137, 89, 168 ) ), QsciLexerPython::Keyword );
75  pyLexer->setColor( colors.value( QStringLiteral( "python/decoratorFontColor" ), QColor( 62, 153, 159 ) ), QsciLexerPython::Decorator );
76  pyLexer->setColor( colors.value( QStringLiteral( "python/singleQuoteFontColor" ), QColor( 113, 140, 0 ) ), QsciLexerPython::SingleQuotedString );
77  pyLexer->setColor( colors.value( QStringLiteral( "python/doubleQuoteFontColor" ), QColor( 113, 140, 0 ) ), QsciLexerPython::DoubleQuotedString );
78  pyLexer->setColor( colors.value( QStringLiteral( "python/tripleSingleQuoteFontColor" ), QColor( 234, 183, 0 ) ), QsciLexerPython::TripleSingleQuotedString );
79  pyLexer->setColor( colors.value( QStringLiteral( "python/tripleDoubleQuoteFontColor" ), QColor( 234, 183, 0 ) ), QsciLexerPython::TripleDoubleQuotedString );
80 
81  QsciAPIs *apis = new QsciAPIs( pyLexer );
82 
83  // check if the file is a prepared apis file.
84  //QString mPapFileName = QFileInfo( mAPISFilesList[0] ).fileName();
85  //QString isPapFile = mPapFileName.right( 3 );
86  //QgsDebugMsg( QStringLiteral( "file extension: %1" ).arg( isPapFile ) );
87 
88  if ( mAPISFilesList.isEmpty() )
89  {
90  mPapFile = QgsApplication::pkgDataPath() + QStringLiteral( "/python/qsci_apis/pyqgis.pap" );
91  apis->loadPrepared( mPapFile );
92  }
93  else if ( mAPISFilesList.length() == 1 && mAPISFilesList[0].right( 3 ) == QLatin1String( "pap" ) )
94  {
95  if ( !QFileInfo::exists( mAPISFilesList[0] ) )
96  {
97  QgsDebugMsg( QStringLiteral( "The apis file %1 not found" ).arg( mAPISFilesList.at( 0 ) ) );
98  return;
99  }
100  mPapFile = mAPISFilesList[0];
101  apis->loadPrepared( mPapFile );
102  }
103  else
104  {
105  for ( int i = 0; i < mAPISFilesList.size(); i++ )
106  {
107  if ( !QFileInfo::exists( mAPISFilesList[i] ) )
108  {
109  QgsDebugMsg( QStringLiteral( "The apis file %1 was not found" ).arg( mAPISFilesList.at( i ) ) );
110  return;
111  }
112  else
113  {
114  apis->load( mAPISFilesList[i] );
115  }
116  }
117  apis->prepare();
118  pyLexer->setAPIs( apis );
119  }
120  setLexer( pyLexer );
121 
122  setMarginVisible( true );
123  setFoldingVisible( true );
124  setIndentationsUseTabs( false );
125 }
126 
127 
128 void QgsCodeEditorPython::loadAPIs( const QList<QString> &filenames )
129 {
130  mAPISFilesList = filenames;
131  //QgsDebugMsg( QStringLiteral( "The apis files: %1" ).arg( mAPISFilesList[0] ) );
132  setSciLexerPython();
133 }
134 
135 bool QgsCodeEditorPython::loadScript( const QString &script )
136 {
137  QgsDebugMsg( QStringLiteral( "The script file: %1" ).arg( script ) );
138  QFile file( script );
139  if ( !file.open( QIODevice::ReadOnly ) )
140  {
141  return false;
142  }
143 
144  QTextStream in( &file );
145 
146  setText( in.readAll().trimmed() );
147  file.close();
148 
149  setSciLexerPython();
150  return true;
151 }
bool loadScript(const QString &script)
Load a script file.
A text editor based on QScintilla2.
Definition: qgscodeeditor.h:38
static QgsApplication * instance()
Returns the singleton instance of the QgsApplication.
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
void setFoldingVisible(bool folding)
Set folding visible state.
QFont getMonospaceFont()
QgsCodeEditorPython(QWidget *parent=nullptr, const QList< QString > &filenames=QList< QString >())
Construct a new Python editor.
static QString pkgDataPath()
Returns the common root path of all application data directories.
void setMarginVisible(bool margin)
Set margin visible state.
void setTitle(const QString &title)
Set the widget title.
void loadAPIs(const QList< QString > &filenames)
Load APIs from one or more files.
static QColor decodeColor(const QString &str)