Skip to content

Commit

Permalink
moved backspace code into its own function
Browse files Browse the repository at this point in the history
  • Loading branch information
thunder422 committed Feb 10, 2013
1 parent cd7200d commit ee9e2ee
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 45 deletions.
104 changes: 59 additions & 45 deletions editbox.cpp
Expand Up @@ -126,51 +126,7 @@ void EditBox::keyPressEvent(QKeyEvent *event)
}
if (event->key() == Qt::Key_Backspace)
{
if (cursor.atBlockStart() && !cursor.atStart()
&& !cursor.hasSelection())
{
if (m_lineModType == LineInserted)
{
// this line has not actually been added yet (reset status)
m_lineModified = -1;
m_lineModType = LineChanged;
if (cursor.atBlockEnd()) // is line blank?
{
// prevent backspace setting modified line
m_ignoreChange = true;
}
}
else
{
int line = cursor.blockNumber();
if (document()->findBlockByLineNumber(line - 1).text()
.isEmpty())
{
// previous line blank, mark it for deletion instead
line--;
if (m_lineModified >= 0) // current line modified?
{
// adjust modified line to its new line
m_lineModified--;
}
m_ignoreChange = true;
}
else
{
// line was deleted, do not report as changed
m_lineModified = -1;
}
// indicate line is about to be deleted
emit linesDeleted(line, 1);

// is line is blank, combine won't modify previous line
if (cursor.atBlockEnd())
{
// prevent backspace setting modified line
m_ignoreChange = true;
}
}
}
backspace(cursor);
break;
}
}
Expand Down Expand Up @@ -200,6 +156,64 @@ void EditBox::remove(void)
}


// function to process a backspace (check if line will be combined with previous)
//
// - if not at beginning of line or at beginning of file or there is a
// selection, then there is nothing to do here
// - if current line is a new (inserted) line not reported yet,
// then the modified line and inserted status is reset
// - else if previous line is blank, then previous line is marked for deletion
// otherwise the current line is marked for deleted (modified line reset)
// - emits the line being deleted
// - if current line is blank, then previous line will not be modified
// when combined so ignore the change

void EditBox::backspace(QTextCursor &cursor)
{
if (!cursor.atBlockStart() || cursor.atStart() || cursor.hasSelection())
{
return;
}

if (m_lineModType == LineInserted)
{
// this line has not actually been added yet (reset status)
m_lineModified = -1;
m_lineModType = LineChanged;
}
else
{
int line = cursor.blockNumber();
if (document()->findBlockByLineNumber(line - 1).text()
.isEmpty())
{
// previous line blank, mark it for deletion instead
line--;
if (m_lineModified >= 0) // current line modified?
{
// adjust modified line to its new line
m_lineModified--;
}
m_ignoreChange = true;
}
else
{
// line was deleted, do not report as changed
m_lineModified = -1;
}
// indicate line is about to be deleted
emit linesDeleted(line, 1);
}

if (cursor.atBlockEnd()) // is line blank?
{
// prevent backspace setting modified line
// (previous line not actually be modified)
m_ignoreChange = true;
}
}


// function to select all of the text in the edit box

void EditBox::selectAll(void)
Expand Down
1 change: 1 addition & 0 deletions editbox.h
Expand Up @@ -103,6 +103,7 @@ private slots:
void updateLineNumberWidget(const QRect &rect, int dy);

private:
void backspace(QTextCursor &cursor);
void insertNewLine(void);
void captureModifiedLine(void);
void captureDeletedLines(void);
Expand Down

0 comments on commit ee9e2ee

Please sign in to comment.