Skip to content

Commit af19ebc

Browse files
committed
Breakpoints shifting and removing improvement
1 parent 16083e4 commit af19ebc

2 files changed

Lines changed: 35 additions & 9 deletions

File tree

‎codeeditor.cpp‎

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ CodeEditor::CodeEditor(QWidget *parent, bool withBeakpoints) :
4545
breakpointImage(":/images/breakpoint.png")
4646
{
4747
hasBreakpoints = withBeakpoints;
48+
prevBlockCount = -1;
4849
this->setDebugMode(false);
4950
debugAreaWidth = 3 + debugImage.width() + 1;
5051
this->setWordWrapMode(QTextOption::NoWrap);
@@ -54,6 +55,7 @@ CodeEditor::CodeEditor(QWidget *parent, bool withBeakpoints) :
5455
connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int)));
5556
connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int)));
5657
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine()));
58+
connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(shiftBreakpoints(int)));
5759

5860
updateLineNumberAreaWidth(0);
5961
highlightCurrentLine();
@@ -101,14 +103,6 @@ void CodeEditor::resizeEvent(QResizeEvent *e)
101103

102104
void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
103105
{
104-
//delete invisible breakpoints
105-
QMutableListIterator<int> i(breakpoints);
106-
while (i.hasNext()) {
107-
int num = i.next();
108-
if (num > this->document()->blockCount())
109-
i.remove();
110-
}
111-
112106
//paint on line number area
113107
QPainter painter(lineNumberArea);
114108
painter.fillRect(event->rect(), QColor(240, 240, 240));
@@ -298,7 +292,7 @@ void CodeEditor::deleteTab()
298292

299293
void CodeEditor::keyPressEvent(QKeyEvent *e)
300294
{
301-
QString curString = this->textCursor().block().text();
295+
QString curString = textCursor().block().text();
302296
int indentWidth = -1;
303297
switch (e->key()) {
304298
case (Qt::Key_Tab) :
@@ -331,6 +325,36 @@ void CodeEditor::keyPressEvent(QKeyEvent *e)
331325
}
332326
}
333327

328+
void CodeEditor::shiftBreakpoints(int blockCount)
329+
{
330+
if (prevBlockCount == -1) {
331+
prevBlockCount = this->blockCount();
332+
return;
333+
}
334+
//remove breakpoints in range
335+
int changedLine = textCursor().blockNumber() + 1; //blocks starts from 0
336+
if (blockCount < prevBlockCount) {
337+
for (int i = changedLine + 1; i <= changedLine + prevBlockCount - blockCount; i++) {
338+
breakpoints.removeOne(i);
339+
}
340+
}
341+
342+
//shift breakpoints
343+
if (blockCount > prevBlockCount) {
344+
for (int i = 0; i < breakpoints.size(); i++) {
345+
if (breakpoints[i] >= changedLine)
346+
breakpoints[i] += blockCount - prevBlockCount;
347+
}
348+
} else {
349+
for (int i = 0; i < breakpoints.size(); i++) {
350+
if (breakpoints[i] > changedLine)
351+
breakpoints[i] += blockCount - prevBlockCount;
352+
}
353+
}
354+
prevBlockCount = blockCount;
355+
repaintLineNumberArea();
356+
}
357+
334358
CodeEditor::~CodeEditor()
335359
{
336360
delete lineNumberArea;

‎codeeditor.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public slots:
8686
private slots:
8787
void updateLineNumberAreaWidth(int newBlockCount);
8888
void updateLineNumberArea(const QRect &, int);
89+
void shiftBreakpoints(int blockCount);
8990

9091
private:
9192
QWidget *lineNumberArea;
@@ -95,6 +96,7 @@ private slots:
9596
QList<int> breakpoints; //numbers of lines with breakpoints
9697
int firstTopMargin;
9798
bool hasBreakpoints;
99+
int prevBlockCount;
98100

99101
signals:
100102
void breakpointsChanged(int lineNumber, bool isAdded);

0 commit comments

Comments
 (0)