diff options
Diffstat (limited to 'desktop-utilities')
-rw-r--r-- | desktop-utilities/lumina-textedit/PlainTextEditor.cpp | 62 | ||||
-rw-r--r-- | desktop-utilities/lumina-textedit/PlainTextEditor.h | 7 |
2 files changed, 69 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 //================== diff --git a/desktop-utilities/lumina-textedit/PlainTextEditor.h b/desktop-utilities/lumina-textedit/PlainTextEditor.h index 46841e59..81574b32 100644 --- a/desktop-utilities/lumina-textedit/PlainTextEditor.h +++ b/desktop-utilities/lumina-textedit/PlainTextEditor.h @@ -30,11 +30,18 @@ private: QWidget *LNW; //Line Number Widget bool showLNW; + //Bracket/Perentheses matching functions + int matchleft, matchright; //positions within the document + void clearMatchData(); + void highlightMatch(QChar ch, bool forward, int fromPos); + private slots: //Functions for managing the line number widget void LNW_updateWidth(); // Tied to the QPlainTextEdit::blockCountChanged() signal void LNW_highlightLine(); // Tied to the QPlainTextEdit::cursorPositionChanged() signal void LNW_update(const QRect&, int); // Tied to the QPlainTextEdit::updateRequest() signal + //Function for running the matching routine + void checkMatchChar(); protected: void resizeEvent(QResizeEvent *ev); |