Skip to content

Commit

Permalink
Port more editing features from QTextPad
Browse files Browse the repository at this point in the history
  • Loading branch information
zrax committed Aug 30, 2019
1 parent d45e255 commit 4765189
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 4 deletions.
104 changes: 100 additions & 4 deletions src/QPlasmaTextEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ void QPlasmaTextEdit::setTheme(const KSyntaxHighlighting::Theme& theme)
pal.setColor(QPalette::Text, theme.textColor(KSyntaxHighlighting::Theme::Normal));
pal.setColor(QPalette::Base, theme.editorColor(KSyntaxHighlighting::Theme::BackgroundColor));
pal.setColor(QPalette::Highlight, theme.editorColor(KSyntaxHighlighting::Theme::TextSelection));
pal.setBrush(QPalette::HighlightedText, Qt::NoBrush);
setPalette(pal);

fLineMarginFg = theme.editorColor(KSyntaxHighlighting::Theme::LineNumbers);
Expand All @@ -229,6 +230,73 @@ void QPlasmaTextEdit::setSyntax(const QString& name)
fHighlighter->setDefinition(syntaxDef);
}

void QPlasmaTextEdit::moveLines(QTextCursor::MoveOperation op)
{
QTextCursor cursor = textCursor();

cursor.beginEditBlock();
int startPos = cursor.position();
int endPos = cursor.anchor();
cursor.setPosition(qMin(startPos, endPos));
cursor.movePosition(QTextCursor::StartOfBlock);
cursor.setPosition(qMax(startPos, endPos), QTextCursor::KeepAnchor);
if (startPos == endPos || !cursor.atBlockStart())
cursor.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor);

const auto moveText = cursor.selectedText();
cursor.removeSelectedText();
const int positionStart = cursor.position();
cursor.movePosition(op);
const int postionDelta = cursor.position() - positionStart;
cursor.insertText(moveText);

cursor.setPosition(endPos + postionDelta);
if (startPos != endPos)
cursor.setPosition(startPos + postionDelta, QTextCursor::KeepAnchor);
cursor.endEditBlock();

setTextCursor(cursor);
}

void QPlasmaTextEdit::smartHome(QTextCursor::MoveMode moveMode)
{
QTextCursor cursor = textCursor();

int leadingIndent = 0;
const QString blockText = cursor.block().text();
for (const auto& ch : blockText) {
if (ch.isSpace())
leadingIndent += 1;
else
break;
}
int cursorPos = cursor.positionInBlock();
cursor.movePosition(QTextCursor::StartOfLine, moveMode);
if (cursor.positionInBlock() == 0 && cursorPos != leadingIndent)
cursor.movePosition(QTextCursor::NextCharacter, moveMode, leadingIndent);

setTextCursor(cursor);
updateCursor();
}

void QPlasmaTextEdit::deleteLines()
{
QTextCursor cursor = textCursor();
if (haveSelection()) {
const int startPos = cursor.selectionStart();
const int endPos = cursor.selectionEnd();
cursor.setPosition(startPos);
cursor.movePosition(QTextCursor::StartOfBlock);
cursor.setPosition(endPos, QTextCursor::KeepAnchor);
if (!cursor.atBlockStart())
cursor.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor);
} else {
cursor.movePosition(QTextCursor::StartOfBlock);
cursor.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor);
}
cursor.removeSelectedText();
}

void QPlasmaTextEdit::updateMargins()
{
setViewportMargins(lineMarginWidth(), 0, 0, 0);
Expand Down Expand Up @@ -512,6 +580,16 @@ void QPlasmaTextEdit::outdentSelection()

void QPlasmaTextEdit::keyPressEvent(QKeyEvent* e)
{
// "Smart" home key
if (e->matches(QKeySequence::MoveToStartOfLine)) {
smartHome(QTextCursor::MoveAnchor);
return;
}
if (e->matches(QKeySequence::SelectStartOfLine)) {
smartHome(QTextCursor::KeepAnchor);
return;
}

switch (e->key()) {
case Qt::Key_Tab:
if (haveSelection()) {
Expand Down Expand Up @@ -586,17 +664,35 @@ void QPlasmaTextEdit::keyPressEvent(QKeyEvent* e)

case Qt::Key_Up:
if (e->modifiers() & Qt::ControlModifier) {
auto scrollBar = verticalScrollBar();
scrollBar->setValue(scrollBar->value() - 1);
if (e->modifiers() & Qt::ShiftModifier) {
moveLines(QTextCursor::PreviousBlock);
} else {
auto scrollBar = verticalScrollBar();
scrollBar->setValue(scrollBar->value() - 1);
}
} else {
QPlainTextEdit::keyPressEvent(e);
}
break;

case Qt::Key_Down:
if (e->modifiers() & Qt::ControlModifier) {
auto scrollBar = verticalScrollBar();
scrollBar->setValue(scrollBar->value() + 1);
if (e->modifiers() & Qt::ShiftModifier) {
moveLines(QTextCursor::NextBlock);
} else {
auto scrollBar = verticalScrollBar();
scrollBar->setValue(scrollBar->value() + 1);
}
} else {
QPlainTextEdit::keyPressEvent(e);
}
break;

case Qt::Key_D:
if (e->modifiers() & Qt::ControlModifier) {
// Default of Ctrl+D is the same as "Del"; This means we can
// repurpose it for "Delete Line"
deleteLines();
} else {
QPlainTextEdit::keyPressEvent(e);
}
Expand Down
4 changes: 4 additions & 0 deletions src/QPlasmaTextEdit.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ class QPlasmaTextEdit : public QPlainTextEdit
void setTheme(const KSyntaxHighlighting::Theme& theme);
void setSyntax(const QString& name);

void moveLines(QTextCursor::MoveOperation op);
void smartHome(QTextCursor::MoveMode mode);
void deleteLines();

protected:
void resizeEvent(QResizeEvent* e) Q_DECL_OVERRIDE;
void keyPressEvent(QKeyEvent* e) Q_DECL_OVERRIDE;
Expand Down

0 comments on commit 4765189

Please sign in to comment.