diff options
Diffstat (limited to 'desktop-utilities/lumina-textedit/PlainTextEditor.cpp')
-rw-r--r-- | desktop-utilities/lumina-textedit/PlainTextEditor.cpp | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/desktop-utilities/lumina-textedit/PlainTextEditor.cpp b/desktop-utilities/lumina-textedit/PlainTextEditor.cpp index 7b105d20..adfa4d8a 100644 --- a/desktop-utilities/lumina-textedit/PlainTextEditor.cpp +++ b/desktop-utilities/lumina-textedit/PlainTextEditor.cpp @@ -16,9 +16,11 @@ PlainTextEditor::PlainTextEditor(QWidget *parent) : QPlainTextEdit(parent){ LNW = new LNWidget(this); showLNW = true; + matchleft = matchright = -1; connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(LNW_updateWidth()) ); connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(LNW_highlightLine()) ); connect(this, SIGNAL(updateRequest(const QRect&, int)), this, SLOT(LNW_update(const QRect&, int)) ); + connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(checkMatchChar()) ); LNW_updateWidth(); LNW_highlightLine(); @@ -67,6 +69,55 @@ void PlainTextEditor::paintLNW(QPaintEvent *ev){ //============== // PRIVATE //============== +void PlainTextEditor::clearMatchData(){ + if(matchleft>=0 || matchright>=0){ + QList<QTextEdit::ExtraSelection> sel = this->extraSelections(); + for(int i=0; i<sel.length(); i++){ + if(sel[i].cursor.selectedText().length()==1){ sel.takeAt(i); i--; } + } + this->setExtraSelections(sel); + matchleft = -1; + matchright = -1; + } +} + +void PlainTextEditor::highlightMatch(QChar ch, bool forward, int fromPos){ + if(forward){ + matchleft = fromPos; + QTextCursor cur = this->document()->find(ch, fromPos); + if(!cur.isNull()){ matchright = cur.position(); } + }else{ + matchright = fromPos; + QTextCursor cur = this->document()->find(ch, fromPos, QTextDocument::FindBackward); + if(!cur.isNull()){ matchleft = cur.position(); } + } + + //Now highlight the two characters + QList<QTextEdit::ExtraSelection> sels = this->extraSelections(); + if(matchleft>=0){ + QTextEdit::ExtraSelection sel; + if(matchright>=0){ sel.format.setBackground(Qt::darkGreen); } + else{ sel.format.setBackground(Qt::darkRed); } + QTextCursor cur = this->textCursor(); + cur.setPosition(matchleft); + if(forward){ cur.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor); } + else{ cur.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor); } + sel.cursor = cur; + sels << sel; + } + if(matchright>=0){ + QTextEdit::ExtraSelection sel; + if(matchleft>=0){ sel.format.setBackground(Qt::darkGreen); } + else{ sel.format.setBackground(Qt::darkRed); } + QTextCursor cur = this->textCursor(); + cur.setPosition(matchright); + if(!forward){ cur.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor); } + else{ cur.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor); } + sel.cursor = cur; + sels << sel; + } + this->setExtraSelections(sels); +} //=================== // PRIVATE SLOTS @@ -103,6 +154,17 @@ void PlainTextEditor::LNW_update(const QRect &rect, int dy){ } } +//Function for running the matching routine +void PlainTextEditor::checkMatchChar(){ + clearMatchData(); + int pos = this->textCursor().position(); + QChar ch = this->document()->characterAt(pos); + if(ch==QChar('(')){ highlightMatch(QChar(')'),true, pos); } + else if(ch==QChar(')')){ highlightMatch(QChar('('),false, pos); } + else if(ch==QChar('{')){ highlightMatch(QChar('}'),true, pos); } + else if(ch==QChar('}')){ highlightMatch(QChar('{'),false, pos); } +} + //================== // PROTECTED //================== |