Skip to content

Commit a284c4e

Browse files
committed
Code editor
1 parent 877f07a commit a284c4e

14 files changed

+607
-24
lines changed

python/gui/gui.sip

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
%Import QtCore/QtCoremod.sip
66
%Import QtGui/QtGuimod.sip
77
%Import QtXml/QtXmlmod.sip
8+
%Import Qsci/qscimod4.sip
89

910
%Import core/core.sip
1011

@@ -19,6 +20,9 @@
1920
%Include qgsattributeforminterface.sip
2021
%Include qgsbusyindicatordialog.sip
2122
%Include qgscollapsiblegroupbox.sip
23+
%Include qgscodeeditor.sip
24+
%Include qgscodeeditorpython.sip
25+
%Include qgscodeeditorsql.sip
2226
%Include qgscolorbutton.sip
2327
%Include qgscolordialog.sip
2428
%Include qgscomposerview.sip

python/gui/qgscodeeditor.sip

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class QgsCodeEditor: QsciScintilla
2+
{
3+
%TypeHeaderCode
4+
#include <qgscodeeditor.h>
5+
%End
6+
7+
public:
8+
QgsCodeEditor( QWidget *parent /TransferThis/ = 0, QString title = "" , bool folding = false, bool margin = false );
9+
~QgsCodeEditor();
10+
11+
bool enableMargin( bool margin );
12+
13+
void enableFolding( bool folding);
14+
};

python/gui/qgscodeeditorpython.sip

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class QgsCodeEditorPython: QgsCodeEditor
2+
{
3+
%TypeHeaderCode
4+
#include <qgscodeeditorpython.h>
5+
%End
6+
7+
public:
8+
QgsCodeEditorPython( QWidget *parent /TransferThis/ = 0, const QList<QString> &filenames = QList<QString>() );
9+
~QgsCodeEditorPython();
10+
11+
void setTitle( QString );
12+
13+
void loadAPIs(const QList<QString> &filenames );
14+
15+
void loadScript( const QString &script );
16+
17+
};

python/gui/qgscodeeditorsql.sip

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class QgsCodeEditorSQL: QgsCodeEditor
2+
{
3+
%TypeHeaderCode
4+
#include <qgscodeeditorsql.h>
5+
%End
6+
7+
public:
8+
QgsCodeEditorSQL( QWidget *parent /TransferThis/ = 0 );
9+
~QgsCodeEditorSQL();
10+
11+
void setTitle( QString );
12+
13+
void showMargin( bool withMargin );
14+
15+
void showFolding( bool withFolding );
16+
17+
};

src/app/qgsprojectproperties.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas* mapCanvas, QWidget *pa
463463
grpPythonMacros->setChecked( !pythonMacros.isEmpty() );
464464
if ( !pythonMacros.isEmpty() )
465465
{
466-
ptePythonMacros->setPlainText( pythonMacros );
466+
ptePythonMacros->setText( pythonMacros );
467467
}
468468
else
469469
{
@@ -867,7 +867,7 @@ void QgsProjectProperties::apply()
867867
QgsProject::instance()->writeEntry( "DefaultStyles", "/RandomColors", cbxStyleRandomColors->isChecked() );
868868

869869
// store project macros
870-
QString pythonMacros = ptePythonMacros->toPlainText();
870+
QString pythonMacros = ptePythonMacros->text();
871871
if ( !grpPythonMacros->isChecked() || pythonMacros.isEmpty() )
872872
{
873873
pythonMacros = QString::null;
@@ -1467,7 +1467,7 @@ void QgsProjectProperties::editSymbol( QComboBox* cbo )
14671467
void QgsProjectProperties::resetPythonMacros()
14681468
{
14691469
grpPythonMacros->setChecked( false );
1470-
ptePythonMacros->setPlainText( "def openProject():\n pass\n\n" \
1470+
ptePythonMacros->setText( "def openProject():\n pass\n\n" \
14711471
"def saveProject():\n pass\n\n" \
14721472
"def closeProject():\n pass\n" );
14731473
}

src/core/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,7 @@ TARGET_LINK_LIBRARIES(qgis_core
725725
${QT_QTNETWORK_LIBRARY}
726726
${QT_QTSVG_LIBRARY}
727727
${QT_QTWEBKIT_LIBRARY}
728+
${QSCINTILLA_LIBRARY}
728729
729730
${PROJ_LIBRARY}
730731
${GEOS_LIBRARY}

src/gui/CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ qgscharacterselectdialog.cpp
126126
qgscollapsiblegroupbox.cpp
127127
qgscolorbutton.cpp
128128
qgscolordialog.cpp
129+
qgscodeeditor.cpp
130+
qgscodeeditorpython.cpp
131+
qgscodeeditorsql.cpp
129132
qgscomposerruler.cpp
130133
qgscomposerview.cpp
131134
qgsprevieweffect.cpp
@@ -316,6 +319,9 @@ qgsblendmodecombobox.h
316319
qgsbusyindicatordialog.h
317320
qgscharacterselectdialog.h
318321
qgscollapsiblegroupbox.h
322+
qgscodeeditor.h
323+
qgscodeeditorpython.h
324+
qgscodeeditorsql.h
319325
qgscolordialog.h
320326
qgsprevieweffect.h
321327
qgscomposerruler.h

src/gui/qgscodeeditor.cpp

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/***************************************************************************
2+
qgscodeeditor.cpp - description
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 "qgscodeeditor.h"
17+
18+
#include <QSettings>
19+
#include <QWidget>
20+
#include <QFont>
21+
#include <QDebug>
22+
23+
QgsCodeEditor::QgsCodeEditor( QWidget *parent, QString title, bool folding, bool margin )
24+
: QsciScintilla( parent ),
25+
mWidgetTitle( title ),
26+
mFolding( folding ),
27+
mMargin( margin )
28+
{
29+
if ( !parent && mWidgetTitle.isEmpty() )
30+
{
31+
setWindowTitle( "QScintilla2 Text Editor" );
32+
setMinimumSize( 800, 300 );
33+
}
34+
else
35+
{
36+
setWindowTitle( mWidgetTitle );
37+
}
38+
setSciWidget();
39+
}
40+
41+
QgsCodeEditor::~QgsCodeEditor()
42+
{
43+
}
44+
45+
void QgsCodeEditor::setSciWidget()
46+
{
47+
setUtf8( true );
48+
setCaretLineVisible( true );
49+
setCaretLineBackgroundColor( QColor( "#fcf3ed" ) );
50+
51+
setBraceMatching( QsciScintilla::SloppyBraceMatch );
52+
setMatchedBraceBackgroundColor( QColor( "#b7f907" ) );
53+
// whether margin will be shown
54+
enableMargin( mMargin );
55+
// whether margin will be shown
56+
enableFolding( mFolding );
57+
// indentation
58+
setAutoIndent( true );
59+
setIndentationWidth( 4 );
60+
setTabIndents( true );
61+
setBackspaceUnindents( true );
62+
setTabWidth( 4 );
63+
// autocomplete
64+
setAutoCompletionThreshold( 2 );
65+
setAutoCompletionSource( QsciScintilla::AcsAPIs );
66+
}
67+
68+
bool QgsCodeEditor::enableMargin( bool margin )
69+
{
70+
if ( margin )
71+
{
72+
QFont marginFont( "Courier", 10 );
73+
setMarginLineNumbers( 1, true );
74+
setMarginsFont( marginFont );
75+
setMarginWidth( 1, "00000" );
76+
setMarginsForegroundColor( QColor( "#3E3EE3" ) );
77+
setMarginsBackgroundColor( QColor( "#f9f9f9" ) );
78+
return true;
79+
}
80+
else
81+
{
82+
setMarginWidth( 0, 0 );
83+
setMarginWidth( 1, 0 );
84+
setMarginWidth( 2, 0 );
85+
return false;
86+
}
87+
}
88+
89+
void QgsCodeEditor::enableFolding( bool folding )
90+
{
91+
if ( folding )
92+
{
93+
setFolding( QsciScintilla::PlainFoldStyle );
94+
setFoldMarginColors( QColor( "#f4f4f4" ), QColor( "#f4f4f4" ) );
95+
}
96+
else
97+
{
98+
setFolding( QsciScintilla::NoFoldStyle );
99+
}
100+
}
101+
102+
// Settings for font and fontsize
103+
bool QgsCodeEditor::isFixedPitch( const QFont& font )
104+
{
105+
const QFontInfo fi( font );
106+
qDebug() << fi.family() << fi.fixedPitch();
107+
return fi.fixedPitch();
108+
}
109+
110+
QFont QgsCodeEditor::getMonospaceFont()
111+
{
112+
QFont font( "monospace" );
113+
if ( isFixedPitch( font ) )
114+
{
115+
return font;
116+
}
117+
font.setStyleHint( QFont::Monospace );
118+
if ( isFixedPitch( font ) )
119+
{
120+
return font;
121+
}
122+
font.setStyleHint( QFont::TypeWriter );
123+
if ( isFixedPitch( font ) )
124+
{
125+
return font;
126+
}
127+
font.setFamily( "courier" );
128+
if ( isFixedPitch( font ) )
129+
{
130+
return font;
131+
}
132+
return font;
133+
}

src/gui/qgscodeeditor.h

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/***************************************************************************
2+
qgscodeeditor.h - description
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+
#ifndef QGSCODEEDITOR_H
17+
#define QGSCODEEDITOR_H
18+
19+
#define QSCINTILLA_DLL
20+
21+
#include <QString>
22+
// qscintilla includes
23+
#include <Qsci/qsciapis.h>
24+
25+
class QWidget;
26+
27+
/** \ingroup gui
28+
* A text editor based on QScintilla2.
29+
* \note added in 2.1
30+
*/
31+
class GUI_EXPORT QgsCodeEditor : public QsciScintilla
32+
{
33+
Q_OBJECT
34+
35+
public:
36+
/**
37+
* Construct a new code editor.
38+
*
39+
* @param parent The parent QWidget
40+
* @param title The title to show in the code editor dialog
41+
* @param folding False: Enable margin for code editor
42+
* @param margin False: Enable folding for code editor
43+
* @note added in 2.1
44+
*/
45+
QgsCodeEditor( QWidget *parent = 0, QString title = "" , bool folding = false, bool margin = false );
46+
~QgsCodeEditor();
47+
48+
/** Enable folding
49+
* @param margin Set margin in the editor
50+
*/
51+
bool enableMargin( bool margin );
52+
53+
/** Enable margin
54+
* @param folding Set folding in the editor
55+
*/
56+
void enableFolding( bool folding );
57+
58+
protected:
59+
60+
bool isFixedPitch( const QFont& font );
61+
62+
QFont getMonospaceFont();
63+
64+
private:
65+
66+
void setSciWidget();
67+
68+
QString mWidgetTitle;
69+
bool mFolding;
70+
bool mMargin;
71+
};
72+
73+
#endif

0 commit comments

Comments
 (0)