Skip to content

Commit

Permalink
Add warning methods to QgsCodeEditorWidget
Browse files Browse the repository at this point in the history
These call the underlying QgsCodeEditor methods to show inline
warnings, but also add corresponding highlights on the scroll bar
  • Loading branch information
nyalldawson committed May 15, 2024
1 parent e435e3f commit 1ea04ae
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@ Returns the message bar associated with the widget, to use for user feedback.
%Docstring
Returns the scrollbar highlight controller, which can be used to add highlights
in the code editor scrollbar.
%End

void addWarning( int lineNumber, const QString &warning );
%Docstring
Adds a ``warning`` message and indicator to the specified a ``lineNumber``.

This method calls :py:func:`QgsCodeEditor.addWarning()`, but also automatically adds
highlights to the widget scrollbars locating the warning location.

.. seealso:: :py:func:`clearWarnings`
%End

void clearWarnings();
%Docstring
Clears all warning messages from the editor.

This method calls :py:func:`QgsCodeEditor.clearWarnings()`, but also removes
highlights from the widget scrollbars at the warning locations.

.. seealso:: :py:func:`addWarning`
%End

public slots:
Expand Down
6 changes: 4 additions & 2 deletions python/console/console_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def __init__(self,
self.editor_tab: EditorTab = editor_tab
self.console_widget: PythonConsoleWidget = console_widget
self.tab_widget: EditorTabWidget = tab_widget
self.code_editor_widget: Optional[QgsCodeEditorWidget] = None

self.path: Optional[str] = None
# recent modification time
Expand Down Expand Up @@ -384,7 +385,7 @@ def goToLine(self, objName, linenr):
self.setFocus()

def syntaxCheck(self):
self.clearWarnings()
self.code_editor_widget.clearWarnings()
source = self.text().encode("utf-8")
try:
compile(source, "", "exec")
Expand All @@ -393,7 +394,7 @@ def syntaxCheck(self):
eline -= 1
ecolumn = detail.offset or 1
edescr = detail.msg
self.addWarning(eline, edescr)
self.code_editor_widget.addWarning(eline, edescr)
self.setCursorPosition(eline, ecolumn - 1)
self.ensureLineVisible(eline)
return False
Expand Down Expand Up @@ -546,6 +547,7 @@ def __init__(self,
self._editor_code_widget = QgsCodeEditorWidget(
self._editor
)
self._editor.code_editor_widget = self._editor_code_widget
self._editor_code_widget.searchBarToggled.connect(
self.search_bar_toggled
)
Expand Down
20 changes: 20 additions & 0 deletions python/gui/auto_generated/codeeditors/qgscodeeditorwidget.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@ Returns the message bar associated with the widget, to use for user feedback.
%Docstring
Returns the scrollbar highlight controller, which can be used to add highlights
in the code editor scrollbar.
%End

void addWarning( int lineNumber, const QString &warning );
%Docstring
Adds a ``warning`` message and indicator to the specified a ``lineNumber``.

This method calls :py:func:`QgsCodeEditor.addWarning()`, but also automatically adds
highlights to the widget scrollbars locating the warning location.

.. seealso:: :py:func:`clearWarnings`
%End

void clearWarnings();
%Docstring
Clears all warning messages from the editor.

This method calls :py:func:`QgsCodeEditor.clearWarnings()`, but also removes
highlights from the widget scrollbars at the warning locations.

.. seealso:: :py:func:`addWarning`
%End

public slots:
Expand Down
25 changes: 25 additions & 0 deletions src/gui/codeeditors/qgscodeeditorwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include <QCheckBox>
#include <QShortcut>

constexpr int WARNING_HIGHLIGHT_CATEGORY = 48;

QgsCodeEditorWidget::QgsCodeEditorWidget(
QgsCodeEditor *editor,
QgsMessageBar *messageBar,
Expand Down Expand Up @@ -192,6 +194,29 @@ QgsScrollBarHighlightController *QgsCodeEditorWidget::scrollbarHighlightControll
return mHighlightController.get();
}

void QgsCodeEditorWidget::addWarning( int lineNumber, const QString &warning )
{
mEditor->addWarning( lineNumber, warning );

mHighlightController->addHighlight(
QgsScrollBarHighlight(
WARNING_HIGHLIGHT_CATEGORY,
lineNumber,
QColor( 255, 0, 0 ),
QgsScrollBarHighlight::Priority::HighestPriority
)
);
}

void QgsCodeEditorWidget::clearWarnings()
{
mEditor->clearWarnings();

mHighlightController->removeHighlights(
WARNING_HIGHLIGHT_CATEGORY
);
}

void QgsCodeEditorWidget::showSearchBar()
{
addSearchHighlights();
Expand Down
20 changes: 20 additions & 0 deletions src/gui/codeeditors/qgscodeeditorwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,26 @@ class GUI_EXPORT QgsCodeEditorWidget : public QgsPanelWidget
*/
QgsScrollBarHighlightController *scrollbarHighlightController();

/**
* Adds a \a warning message and indicator to the specified a \a lineNumber.
*
* This method calls QgsCodeEditor::addWarning(), but also automatically adds
* highlights to the widget scrollbars locating the warning location.
*
* \see clearWarnings()
*/
void addWarning( int lineNumber, const QString &warning );

/**
* Clears all warning messages from the editor.
*
* This method calls QgsCodeEditor::clearWarnings(), but also removes
* highlights from the widget scrollbars at the warning locations.
*
* \see addWarning()
*/
void clearWarnings();

public slots:

/**
Expand Down

0 comments on commit 1ea04ae

Please sign in to comment.