QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgscodeeditor.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscodeeditor.cpp - A base code editor for QGIS and plugins. Provides
3  a base editor using QScintilla for editors
4  --------------------------------------
5  Date : 06-Oct-2013
6  Copyright : (C) 2013 by Salvatore Larosa
7  Email : lrssvtml (at) gmail (dot) com
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #include "qgscodeeditor.h"
18 
19 #include <QSettings>
20 #include <QWidget>
21 #include <QFont>
22 #include <QDebug>
23 
24 QgsCodeEditor::QgsCodeEditor( QWidget *parent, QString title, bool folding, bool margin )
25  : QsciScintilla( parent )
26  , mWidgetTitle( title )
27  , mFolding( folding )
28  , mMargin( margin )
29 {
30  if ( !parent && mWidgetTitle.isEmpty() )
31  {
32  setWindowTitle( "Text Editor" );
33  setMinimumSize( 800, 300 );
34  }
35  else
36  {
37  setWindowTitle( mWidgetTitle );
38  }
39  setSciWidget();
40  setHorizontalScrollBarPolicy( Qt::ScrollBarAsNeeded );
41 }
42 
44 {
45 }
46 
47 void QgsCodeEditor::setSciWidget()
48 {
49  setUtf8( true );
50  setCaretLineVisible( true );
51  setCaretLineBackgroundColor( QColor( "#fcf3ed" ) );
52 
53  setBraceMatching( QsciScintilla::SloppyBraceMatch );
54  setMatchedBraceBackgroundColor( QColor( "#b7f907" ) );
55  // whether margin will be shown
56  setMarginVisible( mMargin );
57  // whether margin will be shown
58  setFoldingVisible( mFolding );
59  // indentation
60  setAutoIndent( true );
61  setIndentationWidth( 4 );
62  setTabIndents( true );
63  setBackspaceUnindents( true );
64  setTabWidth( 4 );
65  // autocomplete
66  setAutoCompletionThreshold( 2 );
67  setAutoCompletionSource( QsciScintilla::AcsAPIs );
68 }
69 
70 void QgsCodeEditor::setTitle( const QString title )
71 {
72  setWindowTitle( title );
73 }
74 
76 {
77  mMargin = margin;
78  if ( margin )
79  {
80  QFont marginFont( "Courier", 10 );
81  setMarginLineNumbers( 1, true );
82  setMarginsFont( marginFont );
83  setMarginWidth( 1, "00000" );
84  setMarginsForegroundColor( QColor( "#3E3EE3" ) );
85  setMarginsBackgroundColor( QColor( "#f9f9f9" ) );
86  }
87  else
88  {
89  setMarginWidth( 0, 0 );
90  setMarginWidth( 1, 0 );
91  setMarginWidth( 2, 0 );
92  }
93 }
94 
96 {
97  mFolding = folding;
98  if ( folding )
99  {
100  setFolding( QsciScintilla::PlainFoldStyle );
101  setFoldMarginColors( QColor( "#f4f4f4" ), QColor( "#f4f4f4" ) );
102  }
103  else
104  {
105  setFolding( QsciScintilla::NoFoldStyle );
106  }
107 }
108 
109 void QgsCodeEditor::insertText( const QString theText )
110 {
111  // Insert the text or replace selected text
112  if ( hasSelectedText() )
113  {
114  replaceSelectedText( theText );
115  }
116  else
117  {
118  int line, index;
119  getCursorPosition( &line, &index );
120  insertAt( theText, line, index );
121  setCursorPosition( line, index + theText.length() );
122  }
123 }
124 
125 // Settings for font and fontsize
126 bool QgsCodeEditor::isFixedPitch( const QFont& font )
127 {
128  const QFontInfo fi( font );
129  return fi.fixedPitch();
130 }
131 
133 {
134  QSettings settings;
135  QString loadFont = settings.value( "pythonConsole/fontfamilytextEditor", "Monospace" ).toString();
136  int fontSize = settings.value( "pythonConsole/fontsizeEditor", 10 ).toInt();
137 
138  QFont font( loadFont );
139  font.setFixedPitch( true );
140  font.setPointSize( fontSize );
141  font.setStyleHint( QFont::TypeWriter );
142  font.setStretch( QFont::SemiCondensed );
143  font.setLetterSpacing( QFont::PercentageSpacing, 87.0 );
144  font.setBold( false );
145  return font;
146 }