QGIS API Documentation  2.14.0-Essen
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, const 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
127 {
128  return font.fixedPitch();
129 }
130 
132 {
133  QSettings settings;
134  QString loadFont = settings.value( "pythonConsole/fontfamilytextEditor", "Monospace" ).toString();
135  int fontSize = settings.value( "pythonConsole/fontsizeEditor", 10 ).toInt();
136 
137  QFont font( loadFont );
138  font.setFixedPitch( true );
139  font.setPointSize( fontSize );
140  font.setStyleHint( QFont::TypeWriter );
141  font.setStretch( QFont::SemiCondensed );
142  font.setLetterSpacing( QFont::PercentageSpacing, 87.0 );
143  font.setBold( false );
144  return font;
145 }
static unsigned index
void setPointSize(int pointSize)
void setFoldingVisible(bool folding)
Set folding visible state.
QFont getMonospaceFont()
bool fixedPitch() const
void setBold(bool enable)
QgsCodeEditor(QWidget *parent=nullptr, const QString &title="", bool folding=false, bool margin=false)
Construct a new code editor.
int toInt(bool *ok) const
bool isEmpty() const
void setStyleHint(StyleHint hint, StyleStrategy strategy)
void setMarginVisible(bool margin)
Set margin visible state.
QVariant value(const QString &key, const QVariant &defaultValue) const
int length() const
void setTitle(const QString &title)
Set the widget title.
bool isFixedPitch(const QFont &font)
void setLetterSpacing(SpacingType type, qreal spacing)
QString toString() const
void setFixedPitch(bool enable)
void insertText(const QString &theText)
Insert text at cursor position, or replace any selected text if user has made a selection.
void setStretch(int factor)