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