Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Code editor
  • Loading branch information
NathanW2 committed Jul 12, 2014
1 parent 877f07a commit a284c4e
Show file tree
Hide file tree
Showing 14 changed files with 607 additions and 24 deletions.
4 changes: 4 additions & 0 deletions python/gui/gui.sip
Expand Up @@ -5,6 +5,7 @@
%Import QtCore/QtCoremod.sip
%Import QtGui/QtGuimod.sip
%Import QtXml/QtXmlmod.sip
%Import Qsci/qscimod4.sip

%Import core/core.sip

Expand All @@ -19,6 +20,9 @@
%Include qgsattributeforminterface.sip
%Include qgsbusyindicatordialog.sip
%Include qgscollapsiblegroupbox.sip
%Include qgscodeeditor.sip
%Include qgscodeeditorpython.sip
%Include qgscodeeditorsql.sip
%Include qgscolorbutton.sip
%Include qgscolordialog.sip
%Include qgscomposerview.sip
Expand Down
14 changes: 14 additions & 0 deletions python/gui/qgscodeeditor.sip
@@ -0,0 +1,14 @@
class QgsCodeEditor: QsciScintilla
{
%TypeHeaderCode
#include <qgscodeeditor.h>
%End

public:
QgsCodeEditor( QWidget *parent /TransferThis/ = 0, QString title = "" , bool folding = false, bool margin = false );
~QgsCodeEditor();

bool enableMargin( bool margin );

void enableFolding( bool folding);
};
17 changes: 17 additions & 0 deletions python/gui/qgscodeeditorpython.sip
@@ -0,0 +1,17 @@
class QgsCodeEditorPython: QgsCodeEditor
{
%TypeHeaderCode
#include <qgscodeeditorpython.h>
%End

public:
QgsCodeEditorPython( QWidget *parent /TransferThis/ = 0, const QList<QString> &filenames = QList<QString>() );
~QgsCodeEditorPython();

void setTitle( QString );

void loadAPIs(const QList<QString> &filenames );

void loadScript( const QString &script );

};
17 changes: 17 additions & 0 deletions python/gui/qgscodeeditorsql.sip
@@ -0,0 +1,17 @@
class QgsCodeEditorSQL: QgsCodeEditor
{
%TypeHeaderCode
#include <qgscodeeditorsql.h>
%End

public:
QgsCodeEditorSQL( QWidget *parent /TransferThis/ = 0 );
~QgsCodeEditorSQL();

void setTitle( QString );

void showMargin( bool withMargin );

void showFolding( bool withFolding );

};
6 changes: 3 additions & 3 deletions src/app/qgsprojectproperties.cpp
Expand Up @@ -463,7 +463,7 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas* mapCanvas, QWidget *pa
grpPythonMacros->setChecked( !pythonMacros.isEmpty() );
if ( !pythonMacros.isEmpty() )
{
ptePythonMacros->setPlainText( pythonMacros );
ptePythonMacros->setText( pythonMacros );
}
else
{
Expand Down Expand Up @@ -867,7 +867,7 @@ void QgsProjectProperties::apply()
QgsProject::instance()->writeEntry( "DefaultStyles", "/RandomColors", cbxStyleRandomColors->isChecked() );

// store project macros
QString pythonMacros = ptePythonMacros->toPlainText();
QString pythonMacros = ptePythonMacros->text();
if ( !grpPythonMacros->isChecked() || pythonMacros.isEmpty() )
{
pythonMacros = QString::null;
Expand Down Expand Up @@ -1467,7 +1467,7 @@ void QgsProjectProperties::editSymbol( QComboBox* cbo )
void QgsProjectProperties::resetPythonMacros()
{
grpPythonMacros->setChecked( false );
ptePythonMacros->setPlainText( "def openProject():\n pass\n\n" \
ptePythonMacros->setText( "def openProject():\n pass\n\n" \
"def saveProject():\n pass\n\n" \
"def closeProject():\n pass\n" );
}
Expand Down
1 change: 1 addition & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -725,6 +725,7 @@ TARGET_LINK_LIBRARIES(qgis_core
${QT_QTNETWORK_LIBRARY}
${QT_QTSVG_LIBRARY}
${QT_QTWEBKIT_LIBRARY}
${QSCINTILLA_LIBRARY}

${PROJ_LIBRARY}
${GEOS_LIBRARY}
Expand Down
6 changes: 6 additions & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -126,6 +126,9 @@ qgscharacterselectdialog.cpp
qgscollapsiblegroupbox.cpp
qgscolorbutton.cpp
qgscolordialog.cpp
qgscodeeditor.cpp
qgscodeeditorpython.cpp
qgscodeeditorsql.cpp
qgscomposerruler.cpp
qgscomposerview.cpp
qgsprevieweffect.cpp
Expand Down Expand Up @@ -316,6 +319,9 @@ qgsblendmodecombobox.h
qgsbusyindicatordialog.h
qgscharacterselectdialog.h
qgscollapsiblegroupbox.h
qgscodeeditor.h
qgscodeeditorpython.h
qgscodeeditorsql.h
qgscolordialog.h
qgsprevieweffect.h
qgscomposerruler.h
Expand Down
133 changes: 133 additions & 0 deletions src/gui/qgscodeeditor.cpp
@@ -0,0 +1,133 @@
/***************************************************************************
qgscodeeditor.cpp - description
--------------------------------------
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( "QScintilla2 Text Editor" );
setMinimumSize( 800, 300 );
}
else
{
setWindowTitle( mWidgetTitle );
}
setSciWidget();
}

QgsCodeEditor::~QgsCodeEditor()
{
}

void QgsCodeEditor::setSciWidget()
{
setUtf8( true );
setCaretLineVisible( true );
setCaretLineBackgroundColor( QColor( "#fcf3ed" ) );

setBraceMatching( QsciScintilla::SloppyBraceMatch );
setMatchedBraceBackgroundColor( QColor( "#b7f907" ) );
// whether margin will be shown
enableMargin( mMargin );
// whether margin will be shown
enableFolding( mFolding );
// indentation
setAutoIndent( true );
setIndentationWidth( 4 );
setTabIndents( true );
setBackspaceUnindents( true );
setTabWidth( 4 );
// autocomplete
setAutoCompletionThreshold( 2 );
setAutoCompletionSource( QsciScintilla::AcsAPIs );
}

bool QgsCodeEditor::enableMargin( bool margin )
{
if ( margin )
{
QFont marginFont( "Courier", 10 );
setMarginLineNumbers( 1, true );
setMarginsFont( marginFont );
setMarginWidth( 1, "00000" );
setMarginsForegroundColor( QColor( "#3E3EE3" ) );
setMarginsBackgroundColor( QColor( "#f9f9f9" ) );
return true;
}
else
{
setMarginWidth( 0, 0 );
setMarginWidth( 1, 0 );
setMarginWidth( 2, 0 );
return false;
}
}

void QgsCodeEditor::enableFolding( bool 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 );
qDebug() << fi.family() << fi.fixedPitch();
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;
}
73 changes: 73 additions & 0 deletions src/gui/qgscodeeditor.h
@@ -0,0 +1,73 @@
/***************************************************************************
qgscodeeditor.h - description
--------------------------------------
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. *
* *
***************************************************************************/

#ifndef QGSCODEEDITOR_H
#define QGSCODEEDITOR_H

#define QSCINTILLA_DLL

#include <QString>
// qscintilla includes
#include <Qsci/qsciapis.h>

class QWidget;

/** \ingroup gui
* A text editor based on QScintilla2.
* \note added in 2.1
*/
class GUI_EXPORT QgsCodeEditor : public QsciScintilla
{
Q_OBJECT

public:
/**
* Construct a new code editor.
*
* @param parent The parent QWidget
* @param title The title to show in the code editor dialog
* @param folding False: Enable margin for code editor
* @param margin False: Enable folding for code editor
* @note added in 2.1
*/
QgsCodeEditor( QWidget *parent = 0, QString title = "" , bool folding = false, bool margin = false );
~QgsCodeEditor();

/** Enable folding
* @param margin Set margin in the editor
*/
bool enableMargin( bool margin );

/** Enable margin
* @param folding Set folding in the editor
*/
void enableFolding( bool folding );

protected:

bool isFixedPitch( const QFont& font );

QFont getMonospaceFont();

private:

void setSciWidget();

QString mWidgetTitle;
bool mFolding;
bool mMargin;
};

#endif

0 comments on commit a284c4e

Please sign in to comment.