QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
20 #include <QWidget>
21 #include <QString>
22 #include <QFont>
23 #include <QFileInfo>
24 #include <QMessageBox>
25 #include <QTextStream>
26 #include <Qsci/qscilexerpython.h>
27 
29  : QgsCodeEditor( parent )
30  , mAPISFilesList( filenames )
31 {
32  if ( !parent )
33  {
34  setTitle( tr( "Python Editor" ) );
35  }
36  setSciLexerPython();
37 }
38 
40 {
41 }
42 
43 void QgsCodeEditorPython::setSciLexerPython()
44 {
45  // current line
46  setCaretWidth( 2 );
47 
48  setEdgeMode( QsciScintilla::EdgeLine );
49  setEdgeColumn( 80 );
50  setEdgeColor( QColor( "#FF0000" ) );
51 
52  setWhitespaceVisibility( QsciScintilla::WsVisibleAfterIndent );
53 
54  QFont font = getMonospaceFont();
55 
56  QsciLexerPython* pyLexer = new QsciLexerPython( this );
57  pyLexer->setDefaultFont( font );
58  pyLexer->setFont( font, -1 );
59  pyLexer->setColor( Qt::red, QsciLexerPython::Comment );
60  pyLexer->setColor( Qt::darkGreen, QsciLexerPython::Keyword );
61  pyLexer->setColor( Qt::darkBlue, QsciLexerPython::Decorator );
62 
63  QsciAPIs* apis = new QsciAPIs( pyLexer );
64 
65  // check if the file is a prepared apis file.
66  //QString mPapFileName = QFileInfo( mAPISFilesList[0] ).fileName();
67  //QString isPapFile = mPapFileName.right( 3 );
68  //QgsDebugMsg( QString( "file extension: %1" ).arg( isPapFile ) );
69 
70  if ( mAPISFilesList.isEmpty() )
71  {
72  mPapFile = QgsApplication::pkgDataPath() + "/python/qsci_apis/pyqgis.pap";
73  apis->loadPrepared( mPapFile );
74  }
75  else if ( mAPISFilesList.length() == 1 && mAPISFilesList[0].right( 3 ) == "pap" )
76  {
77  if ( !QFileInfo( mAPISFilesList[0] ).exists() )
78  {
79  QgsDebugMsg( QString( "The apis file %1 not found" ).arg( mAPISFilesList.at( 0 ) ) );
80  return;
81  }
82  mPapFile = mAPISFilesList[0];
83  apis->loadPrepared( mPapFile );
84  }
85  else
86  {
87  for ( int i = 0; i < mAPISFilesList.size(); i++ )
88  {
89  if ( !QFileInfo( mAPISFilesList[i] ).exists() )
90  {
91  QgsDebugMsg( QString( "The apis file %1 was not found" ).arg( mAPISFilesList.at( i ) ) );
92  return;
93  }
94  else
95  {
96  apis->load( mAPISFilesList[i] );
97  }
98  }
99  apis->prepare();
100  pyLexer->setAPIs( apis );
101  }
102  setLexer( pyLexer );
103 
104  setMarginVisible( true );
105  setFoldingVisible( true );
106 }
107 
108 
110 {
111  mAPISFilesList = filenames;
112  //QgsDebugMsg( QString( "The apis files: %1" ).arg( mAPISFilesList[0] ) );
113  setSciLexerPython();
114 }
115 
117 {
118  QgsDebugMsg( QString( "The script file: %1" ).arg( script ) );
119  QFile file( script );
120  if ( !file.open( QIODevice::ReadOnly ) )
121  {
122  return false;
123  }
124 
125  QTextStream in( &file );
126 
127  setText( in.readAll().trimmed() );
128  file.close();
129 
130  setSciLexerPython();
131  return true;
132 }
bool loadScript(const QString &script)
Load a script file.
A text editor based on QScintilla2.
Definition: qgscodeeditor.h:32
int length() const
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
void setFoldingVisible(bool folding)
Set folding visible state.
QFont getMonospaceFont()
const T & at(int i) const
int size() const
bool isEmpty() const
QString trimmed() const
QgsCodeEditorPython(QWidget *parent=nullptr, const QList< QString > &filenames=QList< QString >())
Construct a new Python editor.
void loadAPIs(QList< QString > const &filenames)
Load APIs from one or more files.
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
bool exists() const
static QString pkgDataPath()
Returns the common root path of all application data directories.
virtual void close()
void setMarginVisible(bool margin)
Set margin visible state.
void setTitle(const QString &title)
Set the widget title.
QString readAll()