Skip to content
Permalink
Browse files

Cleanup handling of margins in QgsCodeEditor and subclasses

  • Loading branch information
nyalldawson committed Oct 13, 2020
1 parent ad3f0a8 commit 1ac59415ec68007dbaee2940e4ec3f99f918dd3c
@@ -33,6 +33,8 @@
import traceback

from qgis.core import QgsApplication, QgsSettings, Qgis
from qgis.gui import QgsCodeEditor

from .ui_console_history_dlg import Ui_HistoryDialogPythonConsole

_init_commands = ["import sys", "import os", "import re", "import math", "from qgis.core import *",
@@ -119,10 +121,8 @@ def refreshSettingsShell(self):
self._setMinimumHeight()

self.setCaretLineVisible(False)
self.setMarginLineNumbers(0, False) # NO linenumbers for the input line
self.setMarginWidth(0, 0)
# margin 2 is the folding
self.setMarginWidth(2, 0)
self.setLineNumbersVisible(False) # NO linenumbers for the input line
self.setMarginWidth(QgsCodeEditor.FoldingControls, 0)
# Margin 1 is used for the '>>>' prompt (console input)
self.setMarginLineNumbers(1, True)
self.setMarginWidth(1, "00000")
@@ -30,14 +30,21 @@ A text editor based on QScintilla2.
%End
public:

enum MarginRole
{
LineNumbers,
ErrorIndicators,
FoldingControls,
};

QgsCodeEditor( QWidget *parent /TransferThis/ = 0, const QString &title = QString(), bool folding = false, bool margin = false );
%Docstring
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 folding for code editor
:param margin: ``False``: Enable margin for code editor
:param margin: ``False``: Enable margin for code editor (deprecated)

.. versionadded:: 2.6
%End
@@ -49,13 +56,43 @@ Set the widget title
:param title: widget title
%End

void setMarginVisible( bool margin );
void setMarginVisible( bool margin ) /Deprecated/;
%Docstring
Set margin visible state

:param margin: Set margin in the editor

.. deprecated::
Use base class methods for individual margins instead, or setLineNumbersVisible()
%End

bool marginVisible() /Deprecated/;
%Docstring
Returns whether margins are in a visible state

.. deprecated::
Use base class methods for individual margins instead, or lineNumbersVisible()
%End

void setLineNumbersVisible( bool visible );
%Docstring
Sets whether line numbers should be visible in the editor.

Defaults to ``False``.

.. seealso:: :py:func:`lineNumbersVisible`

.. versionadded:: 3.16
%End

bool lineNumbersVisible() const;
%Docstring
Returns whether line numbers are visible in the editor.

.. seealso:: :py:func:`setLineNumbersVisible`

.. versionadded:: 3.16
%End
bool marginVisible();

void setFoldingVisible( bool folding );
%Docstring
@@ -90,9 +90,9 @@ QgsCodeEditor::QgsCodeEditor( QWidget *parent, const QString &title, bool foldin
SendScintilla( SCI_SETMULTIPASTE, 1 );
SendScintilla( SCI_SETVIRTUALSPACEOPTIONS, SCVS_RECTANGULARSELECTION );

SendScintilla( SCI_SETMARGINTYPEN, 3, SC_MARGIN_SYMBOL );
SendScintilla( SCI_SETMARGINMASKN, 3, 1 << MARKER_NUMBER );
setMarginWidth( 3, 0 );
SendScintilla( SCI_SETMARGINTYPEN, QgsCodeEditor::MarginRole::ErrorIndicators, SC_MARGIN_SYMBOL );
SendScintilla( SCI_SETMARGINMASKN, QgsCodeEditor::MarginRole::ErrorIndicators, 1 << MARKER_NUMBER );
setMarginWidth( QgsCodeEditor::MarginRole::ErrorIndicators, 0 );
setAnnotationDisplay( QsciScintilla::AnnotationBoxed );

connect( QgsGui::instance(), &QgsGui::optionsChanged, this, [ = ]
@@ -228,8 +228,12 @@ void QgsCodeEditor::setSciWidget()
setBraceMatching( QsciScintilla::SloppyBraceMatch );
setMatchedBraceForegroundColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::MatchedBraceForeground ) );
setMatchedBraceBackgroundColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::MatchedBraceBackground ) );
// whether margin will be shown
setMarginVisible( mMargin );

setLineNumbersVisible( false );

setMarginWidth( QgsCodeEditor::MarginRole::ErrorIndicators, 0 );
setMarginWidth( QgsCodeEditor::MarginRole::FoldingControls, 0 );

setMarginsForegroundColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::MarginForeground ) );
setMarginsBackgroundColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::MarginBackground ) );
setIndentationGuidesForegroundColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::MarginForeground ) );
@@ -266,18 +270,42 @@ void QgsCodeEditor::setMarginVisible( bool margin )
marginFont.setPointSize( 10 );
setMarginLineNumbers( 0, true );
setMarginsFont( marginFont );
setMarginWidth( 0, QStringLiteral( "00000" ) );
setMarginWidth( QgsCodeEditor::MarginRole::LineNumbers, QStringLiteral( "00000" ) );
setMarginsForegroundColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::MarginForeground ) );
setMarginsBackgroundColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::MarginBackground ) );
}
else
{
setMarginWidth( QgsCodeEditor::MarginRole::LineNumbers, 0 );
setMarginWidth( QgsCodeEditor::MarginRole::ErrorIndicators, 0 );
setMarginWidth( QgsCodeEditor::MarginRole::FoldingControls, 0 );
}
}

void QgsCodeEditor::setLineNumbersVisible( bool visible )
{
if ( visible )
{
QFont marginFont = lexerFont();
marginFont.setPointSize( 10 );
setMarginLineNumbers( QgsCodeEditor::MarginRole::LineNumbers, true );
setMarginsFont( marginFont );
setMarginWidth( QgsCodeEditor::MarginRole::LineNumbers, QStringLiteral( "00000" ) );
setMarginsForegroundColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::MarginForeground ) );
setMarginsBackgroundColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::MarginBackground ) );
}
else
{
setMarginWidth( 0, 0 );
setMarginWidth( 1, 0 );
setMarginWidth( 2, 0 );
setMarginLineNumbers( QgsCodeEditor::MarginRole::LineNumbers, false );
setMarginWidth( QgsCodeEditor::MarginRole::LineNumbers, 0 );
}
}

bool QgsCodeEditor::lineNumbersVisible() const
{
return marginLineNumbers( QgsCodeEditor::MarginRole::LineNumbers );
}

void QgsCodeEditor::setFoldingVisible( bool folding )
{
mFolding = folding;
@@ -457,7 +485,7 @@ void QgsCodeEditor::setCustomAppearance( const QString &scheme, const QMap<QgsCo

void QgsCodeEditor::addWarning( const int lineNumber, const QString &warning )
{
setMarginWidth( 3, "000" );
setMarginWidth( QgsCodeEditor::MarginRole::ErrorIndicators, "000" );
markerAdd( lineNumber, MARKER_NUMBER );
QFont font = lexerFont();
font.setItalic( true );
@@ -477,6 +505,6 @@ void QgsCodeEditor::clearWarnings()
markerDelete( line );
clearAnnotations( line );
}
setMarginWidth( 3, 0 );
setMarginWidth( QgsCodeEditor::MarginRole::ErrorIndicators, 0 );
mWarningLines.clear();
}
@@ -44,13 +44,27 @@ class GUI_EXPORT QgsCodeEditor : public QsciScintilla

public:

/**
* Margin roles.
*
* This enum contains the roles which the different numbered margins are used for.
*
* \since QGIS 3.16
*/
enum MarginRole
{
LineNumbers = 0, //!< Line numbers
ErrorIndicators = 1, //!< Error indicators
FoldingControls = 2, //!< Folding controls
};

/**
* 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 folding for code editor
* \param margin FALSE: Enable margin for code editor
* \param margin FALSE: Enable margin for code editor (deprecated)
* \since QGIS 2.6
*/
QgsCodeEditor( QWidget *parent SIP_TRANSFERTHIS = nullptr, const QString &title = QString(), bool folding = false, bool margin = false );
@@ -63,10 +77,34 @@ class GUI_EXPORT QgsCodeEditor : public QsciScintilla

/**
* Set margin visible state
* \param margin Set margin in the editor
* \param margin Set margin in the editor
* \deprecated Use base class methods for individual margins instead, or setLineNumbersVisible()
*/
Q_DECL_DEPRECATED void setMarginVisible( bool margin ) SIP_DEPRECATED;

/**
* Returns whether margins are in a visible state
* \deprecated Use base class methods for individual margins instead, or lineNumbersVisible()
*/
Q_DECL_DEPRECATED bool marginVisible() SIP_DEPRECATED { return mMargin; }

/**
* Sets whether line numbers should be visible in the editor.
*
* Defaults to FALSE.
*
* \see lineNumbersVisible()
* \since QGIS 3.16
*/
void setLineNumbersVisible( bool visible );

/**
* Returns whether line numbers are visible in the editor.
*
* \see setLineNumbersVisible()
* \since QGIS 3.16
*/
void setMarginVisible( bool margin );
bool marginVisible() { return mMargin; }
bool lineNumbersVisible() const;

/**
* Set folding visible state
@@ -29,7 +29,6 @@ QgsCodeEditorCSS::QgsCodeEditorCSS( QWidget *parent )
{
setTitle( tr( "CSS Editor" ) );
}
setMarginVisible( false );
setFoldingVisible( true );
QgsCodeEditorCSS::initializeLexer();
}
@@ -27,7 +27,6 @@ QgsCodeEditorExpression::QgsCodeEditorExpression( QWidget *parent )
{
setTitle( tr( "Expression Editor" ) );
}
setMarginVisible( false );
setFoldingVisible( false );
setAutoCompletionCaseSensitivity( false );
QgsCodeEditorExpression::initializeLexer(); // avoid cppcheck warning by explicitly specifying namespace
@@ -30,7 +30,6 @@ QgsCodeEditorHTML::QgsCodeEditorHTML( QWidget *parent )
{
setTitle( tr( "HTML Editor" ) );
}
setMarginVisible( false );
setFoldingVisible( true );
QgsCodeEditorHTML::initializeLexer();
}
@@ -29,7 +29,6 @@ QgsCodeEditorJavascript::QgsCodeEditorJavascript( QWidget *parent )
{
setTitle( tr( "JavaScript Editor" ) );
}
setMarginVisible( false );
setFoldingVisible( true );
QgsCodeEditorJavascript::initializeLexer();
}
@@ -168,18 +168,17 @@ void QgsCodeEditorPython::initializeLexer()
setAutoCompletionSource( AcsAPIs );
}

setMarginVisible( true );
setLineNumbersVisible( true );

// Margin 2 is used for the 'folding'
setMarginWidth( 2, "0" );
setMarginWidth( QgsCodeEditor::MarginRole::FoldingControls, "0" );
setMarginsForegroundColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::MarginForeground ) );
setMarginsBackgroundColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::MarginBackground ) );

setFoldingVisible( true );
setIndentationsUseTabs( false );
setIndentationGuides( true );


runPostLexerConfigurationTasks();
}

@@ -29,7 +29,6 @@ QgsCodeEditorSQL::QgsCodeEditorSQL( QWidget *parent )
{
setTitle( tr( "SQL Editor" ) );
}
setMarginVisible( false );
setFoldingVisible( false );
setAutoCompletionCaseSensitivity( false );
QgsCodeEditorSQL::initializeLexer(); // avoid cppcheck warning by explicitly specifying namespace
@@ -202,7 +202,8 @@ QgsExpressionBuilderWidget::QgsExpressionBuilderWidget( QWidget *parent )
txtExpressionString->setCallTipsVisible( 0 );

setExpectedOutputFormat( QString() );
mFunctionBuilderHelp->setMarginVisible( false );
mFunctionBuilderHelp->setLineNumbersVisible( false );
mFunctionBuilderHelp->setMarginWidth( QgsCodeEditor::MarginRole::FoldingControls, 0 );
mFunctionBuilderHelp->setEdgeMode( QsciScintilla::EdgeNone );
mFunctionBuilderHelp->setEdgeColumn( 0 );
mFunctionBuilderHelp->setReadOnly( true );

0 comments on commit 1ac5941

Please sign in to comment.
You can’t perform that action at this time.