QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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  font.setPointSize( 10 );
56 
57  QsciLexerPython* pyLexer = new QsciLexerPython();
58  pyLexer->setDefaultFont( font );
59  pyLexer->setFont( font, 1 ); // comment
60  pyLexer->setFont( font, 3 ); // singlequotes
61  pyLexer->setFont( font, 4 ); // doublequotes
62  pyLexer->setFont( font, 6 ); // triplequotes
63  pyLexer->setColor( Qt::red, 1 ); // comment color
64  pyLexer->setColor( Qt::darkGreen, 5 ); // keyword color
65  pyLexer->setColor( Qt::darkBlue, 15 ); // decorator color
66 
67  QsciAPIs* apis = new QsciAPIs( pyLexer );
68 
69  // check if the file is a prepared apis file.
70  //QString mPapFileName = QFileInfo( mAPISFilesList[0] ).fileName();
71  //QString isPapFile = mPapFileName.right( 3 );
72  //QgsDebugMsg( QString( "file extension: %1" ).arg( isPapFile ) );
73 
74  if ( mAPISFilesList.isEmpty() )
75  {
76  mPapFile = QgsApplication::pkgDataPath() + "/python/qsci_apis/pyqgis.pap";
77  apis->loadPrepared( mPapFile );
78  }
79  else if ( mAPISFilesList.length() == 1 && mAPISFilesList[0].right( 3 ) == "pap" )
80  {
81  if ( !QFileInfo( mAPISFilesList[0] ).exists() )
82  {
83  QgsDebugMsg( QString( "The apis file %1 not found" ).arg( mAPISFilesList[0] ) );
84  return;
85  }
86  mPapFile = mAPISFilesList[0];
87  apis->loadPrepared( mPapFile );
88  }
89  else
90  {
91  for ( int i = 0; i < mAPISFilesList.size(); i++ )
92  {
93  if ( !QFileInfo( mAPISFilesList[i] ).exists() )
94  {
95  QgsDebugMsg( QString( "The apis file %1 was not found" ).arg( mAPISFilesList[i] ) );
96  return;
97  }
98  else
99  {
100  apis->load( mAPISFilesList[i] );
101  }
102  }
103  apis->prepare();
104  pyLexer->setAPIs( apis );
105  }
106  setLexer( pyLexer );
107 
108  setMarginVisible( true );
109  setFoldingVisible( true );
110 }
111 
112 
113 void QgsCodeEditorPython::loadAPIs( const QList<QString> &filenames )
114 {
115  mAPISFilesList = filenames;
116  //QgsDebugMsg( QString( "The apis files: %1" ).arg( mAPISFilesList[0] ) );
117  setSciLexerPython();
118 }
119 
120 bool QgsCodeEditorPython::loadScript( const QString &script )
121 {
122  QgsDebugMsg( QString( "The script file: %1" ).arg( script ) );
123  QFile file( script );
124  if ( !file.open( QIODevice::ReadOnly ) )
125  {
126  return false;
127  }
128 
129  QTextStream in( &file );
130 
131  setText( in.readAll() );
132  file.close();
133 
134  setSciLexerPython();
135  return true;
136 }
static const QString pkgDataPath()
Returns the common root path of all application data directories.
bool loadScript(const QString &script)
Load a script file.
A text editor based on QScintilla2.
Definition: qgscodeeditor.h:31
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
void setFoldingVisible(bool folding)
Set folding visible state.
QFont getMonospaceFont()
void setTitle(const QString title)
Set the widget title.
void loadAPIs(QList< QString > const &filenames)
Load APIs from one or more files.
QString file
Definition: qgssvgcache.cpp:76
void setMarginVisible(bool margin)
Set margin visible state.
QgsCodeEditorPython(QWidget *parent=0, const QList< QString > &filenames=QList< QString >())
Construct a new Python editor.
#define tr(sourceText)