/
qgscodeeditor.cpp
139 lines (128 loc) · 3.56 KB
/
qgscodeeditor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/***************************************************************************
qgscodeeditor.cpp - A base code editor for QGIS and plugins. Provides
a base editor using QScintilla for editors
--------------------------------------
Date : 06-Oct-2013
Copyright : (C) 2013 by Salvatore Larosa
Email : lrssvtml (at) gmail (dot) com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgscodeeditor.h"
#include <QSettings>
#include <QWidget>
#include <QFont>
#include <QDebug>
QgsCodeEditor::QgsCodeEditor( QWidget *parent, QString title, bool folding, bool margin )
: QsciScintilla( parent )
, mWidgetTitle( title )
, mFolding( folding )
, mMargin( margin )
{
if ( !parent && mWidgetTitle.isEmpty() )
{
setWindowTitle( "Text Editor" );
setMinimumSize( 800, 300 );
}
else
{
setWindowTitle( mWidgetTitle );
}
setSciWidget();
setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
}
QgsCodeEditor::~QgsCodeEditor()
{
}
void QgsCodeEditor::setSciWidget()
{
setUtf8( true );
setCaretLineVisible( true );
setCaretLineBackgroundColor( QColor( "#fcf3ed" ) );
setBraceMatching( QsciScintilla::SloppyBraceMatch );
setMatchedBraceBackgroundColor( QColor( "#b7f907" ) );
// whether margin will be shown
setMarginVisible( mMargin );
// whether margin will be shown
setFoldingVisible( mFolding );
// indentation
setAutoIndent( true );
setIndentationWidth( 4 );
setTabIndents( true );
setBackspaceUnindents( true );
setTabWidth( 4 );
// autocomplete
setAutoCompletionThreshold( 2 );
setAutoCompletionSource( QsciScintilla::AcsAPIs );
}
void QgsCodeEditor::setTitle( QString title )
{
setWindowTitle( title );
}
void QgsCodeEditor::setMarginVisible( bool margin )
{
mMargin = margin;
if ( margin )
{
QFont marginFont( "Courier", 10 );
setMarginLineNumbers( 1, true );
setMarginsFont( marginFont );
setMarginWidth( 1, "00000" );
setMarginsForegroundColor( QColor( "#3E3EE3" ) );
setMarginsBackgroundColor( QColor( "#f9f9f9" ) );
}
else
{
setMarginWidth( 0, 0 );
setMarginWidth( 1, 0 );
setMarginWidth( 2, 0 );
}
}
void QgsCodeEditor::setFoldingVisible( bool folding )
{
mFolding = folding;
if ( folding )
{
setFolding( QsciScintilla::PlainFoldStyle );
setFoldMarginColors( QColor( "#f4f4f4" ), QColor( "#f4f4f4" ) );
}
else
{
setFolding( QsciScintilla::NoFoldStyle );
}
}
// Settings for font and fontsize
bool QgsCodeEditor::isFixedPitch( const QFont& font )
{
const QFontInfo fi( font );
return fi.fixedPitch();
}
QFont QgsCodeEditor::getMonospaceFont()
{
QFont font( "monospace" );
if ( isFixedPitch( font ) )
{
return font;
}
font.setStyleHint( QFont::Monospace );
if ( isFixedPitch( font ) )
{
return font;
}
font.setStyleHint( QFont::TypeWriter );
if ( isFixedPitch( font ) )
{
return font;
}
font.setFamily( "courier" );
if ( isFixedPitch( font ) )
{
return font;
}
return font;
}