QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
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();
57  pyLexer->setDefaultFont( font );
58  pyLexer->setFont( font, 1 ); // comment
59  pyLexer->setFont( font, 3 ); // singlequotes
60  pyLexer->setFont( font, 4 ); // doublequotes
61  pyLexer->setFont( font, 6 ); // triplequotes
62  pyLexer->setColor( Qt::red, 1 ); // comment color
63  pyLexer->setColor( Qt::darkGreen, 5 ); // keyword color
64  pyLexer->setColor( Qt::darkBlue, 15 ); // decorator color
65 
66  QsciAPIs* apis = new QsciAPIs( pyLexer );
67 
68  // check if the file is a prepared apis file.
69  //QString mPapFileName = QFileInfo( mAPISFilesList[0] ).fileName();
70  //QString isPapFile = mPapFileName.right( 3 );
71  //QgsDebugMsg( QString( "file extension: %1" ).arg( isPapFile ) );
72 
73  if ( mAPISFilesList.isEmpty() )
74  {
75  mPapFile = QgsApplication::pkgDataPath() + "/python/qsci_apis/pyqgis.pap";
76  apis->loadPrepared( mPapFile );
77  }
78  else if ( mAPISFilesList.length() == 1 && mAPISFilesList[0].right( 3 ) == "pap" )
79  {
80  if ( !QFileInfo( mAPISFilesList[0] ).exists() )
81  {
82  QgsDebugMsg( QString( "The apis file %1 not found" ).arg( mAPISFilesList[0] ) );
83  return;
84  }
85  mPapFile = mAPISFilesList[0];
86  apis->loadPrepared( mPapFile );
87  }
88  else
89  {
90  for ( int i = 0; i < mAPISFilesList.size(); i++ )
91  {
92  if ( !QFileInfo( mAPISFilesList[i] ).exists() )
93  {
94  QgsDebugMsg( QString( "The apis file %1 was not found" ).arg( mAPISFilesList[i] ) );
95  return;
96  }
97  else
98  {
99  apis->load( mAPISFilesList[i] );
100  }
101  }
102  apis->prepare();
103  pyLexer->setAPIs( apis );
104  }
105  setLexer( pyLexer );
106 
107  setMarginVisible( true );
108  setFoldingVisible( true );
109 }
110 
111 
112 void QgsCodeEditorPython::loadAPIs( const QList<QString> &filenames )
113 {
114  mAPISFilesList = filenames;
115  //QgsDebugMsg( QString( "The apis files: %1" ).arg( mAPISFilesList[0] ) );
116  setSciLexerPython();
117 }
118 
119 bool QgsCodeEditorPython::loadScript( const QString &script )
120 {
121  QgsDebugMsg( QString( "The script file: %1" ).arg( script ) );
122  QFile file( script );
123  if ( !file.open( QIODevice::ReadOnly ) )
124  {
125  return false;
126  }
127 
128  QTextStream in( &file );
129 
130  setText( in.readAll() );
131  file.close();
132 
133  setSciLexerPython();
134  return true;
135 }