Implement breakpoint setting/unsetting
MODIFY
resources/breakpoint_disabled.png
resources/breakpoint_disabled.png
Binary files a/resources/breakpoint_disabled.png and b/resources/breakpoint_disabled.png differ

MODIFY
resources/breakpoint_enabled.png
resources/breakpoint_enabled.png
Binary files a/resources/breakpoint_enabled.png and b/resources/breakpoint_enabled.png differ

MODIFY
src/codeeditor.cpp
src/codeeditor.cpp
@@ -17,7 +17,7 @@ CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent) {
SLOT(highlightCurrentLine()));
// add some breakpoints
- m_breakpoints = QList<int>() << 3 << 5 << 10 << 20;
+ m_breakpoints = QSet<int>() << 3 << 5 << 10 << 20;
updateSidebarWidth(0);
highlightCurrentLine();
@@ -53,6 +53,14 @@ void CodeEditor::updateSidebar(const QRect &rect, int dy) {
if (rect.contains(viewport()->rect()))
updateSidebarWidth(0);
+
+ // Remove breakpoints if a breakpoint line has been removed
+ auto a = blockCount();
+ while (!m_breakpoints.isEmpty() &&
+ *(m_breakpoints.end() - 1) > (blockCount() - 1)) {
+
+ m_breakpoints.erase(m_breakpoints.end() - 1);
+ }
}
void CodeEditor::resizeEvent(QResizeEvent *e) {
@@ -121,11 +129,9 @@ void CodeEditor::breakpointAreaPaintEvent(QPaintEvent *event) {
while (block.isValid() && top <= event->rect().bottom()) {
if (block.isVisible() && bottom >= event->rect().top()) {
if (m_breakpoints.contains(blockNumber)) {
- painter.drawPixmap(m_breakpointArea->padding, top,
- m_breakpointArea->imageWidth,
- m_breakpointArea->imageHeight,
- m_breakpointArea->m_breakpoint.scaledToHeight(
- fontMetrics().height() * 0.7));
+ painter.drawPixmap(
+ m_breakpointArea->padding, top, m_breakpointArea->imageWidth,
+ m_breakpointArea->imageHeight, m_breakpointArea->m_breakpoint);
}
}
@@ -136,4 +142,37 @@ void CodeEditor::breakpointAreaPaintEvent(QPaintEvent *event) {
}
}
-void BreakpointArea::mouseReleaseEvent(QMouseEvent *event) { return; }
+void CodeEditor::breakpointClick(QMouseEvent *event) {
+ // Get line height
+ QTextBlock block = firstVisibleBlock();
+ auto height = blockBoundingRect(block).height();
+
+ // Find block index in the codeeditor
+ int index;
+ if (block == document()->findBlockByLineNumber(0)) {
+ index = -1 + (event->pos().y() + contentOffset().y()) / height;
+ } else {
+ index = (event->pos().y() + contentOffset().y()) / height;
+ }
+ // Get actual block index
+ while (index > 0) {
+ block = block.next();
+ index--;
+ }
+ // Set or unset breakpoint
+ int blockNumber = block.blockNumber();
+ if (block.isValid()) {
+ // Set/unset breakpoint
+ if (m_breakpoints.contains(blockNumber)) {
+ m_breakpoints.remove(blockNumber);
+ } else {
+ m_breakpoints.insert(blockNumber);
+ }
+ repaint();
+ }
+}
+
+BreakpointArea::BreakpointArea(CodeEditor *editor) : QWidget(editor) {
+ codeEditor = editor;
+ setCursor(Qt::PointingHandCursor);
+}

MODIFY
src/codeeditor.h
src/codeeditor.h
#ifndef CODEEDITOR_H
#define CODEEDITOR_H
-#include <QList>
#include <QPlainTextEdit>
+#include <QSet>
// Extended version of Qt's CodeEditor example
// http://doc.qt.io/qt-5/qtwidgets-widgets-codeeditor-example.html
@@ -17,6 +17,7 @@ public:
void lineNumberAreaPaintEvent(QPaintEvent *event);
void breakpointAreaPaintEvent(QPaintEvent *event);
+ void breakpointClick(QMouseEvent *event);
int lineNumberAreaWidth();
protected:
@@ -31,7 +32,7 @@ private:
LineNumberArea *m_lineNumberArea;
BreakpointArea *m_breakpointArea;
- QList<int> m_breakpoints;
+ QSet<int> m_breakpoints;
};
// base class for side area widgets that are attached to the code editor
@@ -62,7 +63,7 @@ private:
class BreakpointArea : public QWidget {
public:
- BreakpointArea(CodeEditor *editor) : QWidget(editor) { codeEditor = editor; }
+ BreakpointArea(CodeEditor *editor);
QSize sizeHint() const override { return QSize(width(), 0); }
int width() const { return imageWidth + padding * 2; }
@@ -76,6 +77,8 @@ public:
QPixmap m_breakpoint_disabled =
QPixmap(":/logos/breakpoint_disabled.png").scaled(imageWidth, imageWidth);
+ void setBlockHeight(int height) { m_blockHeight = height; }
+
protected:
void paintEvent(QPaintEvent *event) override {
codeEditor->breakpointAreaPaintEvent(event);
@@ -83,8 +86,11 @@ protected:
private:
CodeEditor *codeEditor;
+ int m_blockHeight = 0;
- void mouseReleaseEvent(QMouseEvent *event);
+ void mouseReleaseEvent(QMouseEvent *event) {
+ codeEditor->breakpointClick(event);
+ }
};
#endif // CODEEDITOR_H

Implement breakpoint setting/unsetting