From fdf1c0f1c9f6d1121cee90660a79f707f0bdf52f Mon Sep 17 00:00:00 2001 From: Saki Date: Sat, 21 Dec 2013 21:00:30 +0800 Subject: [PATCH] Major: 0.2.5.17 Bug Fixed: 1. Finally Fixed #5 Search bug. 2. Previously fixed #3 bug. --- src/app/kccodeeditor.cpp | 4 ++ src/app/kcsearchwidget.cpp | 10 +++ src/app/kcsearchwidget.h | 2 + src/app/kctextblockdata.cpp | 1 + src/app/kctexteditor.cpp | 125 ++++++++++++++++++++++-------------- src/app/kctexteditor.h | 8 +++ 6 files changed, 103 insertions(+), 47 deletions(-) diff --git a/src/app/kccodeeditor.cpp b/src/app/kccodeeditor.cpp index c6e957d..d0cefcb 100755 --- a/src/app/kccodeeditor.cpp +++ b/src/app/kccodeeditor.cpp @@ -162,6 +162,10 @@ void KCCodeEditor::connectSearchWidgetWithEditor(KCSearchWidget *widget) this, &KCCodeEditor::onShowNextSearchResult); searcherConnections+=connect(widget, &KCSearchWidget::requireShowPreviousResult, editor, &KCTextEditor::showPreviousSearchResult); + searcherConnections+=connect(editor, &KCTextEditor::matchedResult, + widget, &KCSearchWidget::setResultMatchStyle); + searcherConnections+=connect(editor, &KCTextEditor::nomatchedResult, + widget, &KCSearchWidget::setResultUnmatchStyle); } bool KCCodeEditor::getDebugging() const { diff --git a/src/app/kcsearchwidget.cpp b/src/app/kcsearchwidget.cpp index 171b773..ce45264 100755 --- a/src/app/kcsearchwidget.cpp +++ b/src/app/kcsearchwidget.cpp @@ -154,6 +154,16 @@ void KCSearchWidget::onMenuClicked() onTextChanged(searchText->text()); } +void KCSearchWidget::setResultMatchStyle() +{ + ; +} + +void KCSearchWidget::setResultUnmatchStyle() +{ + ; +} + void KCSearchWidget::setText(const QString &text) { searchText->setText(text); diff --git a/src/app/kcsearchwidget.h b/src/app/kcsearchwidget.h index b9bd1d7..3223fea 100755 --- a/src/app/kcsearchwidget.h +++ b/src/app/kcsearchwidget.h @@ -67,6 +67,8 @@ class KCSearchWidget : public QWidget public slots: void onTextChanged(const QString &text); void onMenuClicked(); + void setResultMatchStyle(); + void setResultUnmatchStyle(); protected: void resizeEvent(QResizeEvent *event); diff --git a/src/app/kctextblockdata.cpp b/src/app/kctextblockdata.cpp index eca4944..87feb9b 100755 --- a/src/app/kctextblockdata.cpp +++ b/src/app/kctextblockdata.cpp @@ -18,6 +18,7 @@ */ #include +#include #include "kctextblockdata.h" diff --git a/src/app/kctexteditor.cpp b/src/app/kctexteditor.cpp index 0014ae1..1c6f875 100755 --- a/src/app/kctexteditor.cpp +++ b/src/app/kctexteditor.cpp @@ -119,12 +119,12 @@ void KCTextEditor::checkWhetherBlockSearchedAndDealWith( bool KCTextEditor::showPreviousSearchResult() { - return findString(false); + return findString(true); } bool KCTextEditor::showNextSearchResult() { - return findString(true); + return findString(false); } void KCTextEditor::setTabWidth(int width) @@ -136,33 +136,42 @@ bool KCTextEditor::findString(bool forward) { QTextCursor _textCursor; + //Simbol to match a string bool hasMatch=false; + //Backup current cursor _textCursor=textCursor(); - if(!forward) - { - _textCursor.setPosition(_textCursor.selectionStart()); - } - - for(QTextBlock i=_textCursor.block(); - i.isValid() && !hasMatch; - i=(forward==true)?i.next():i.previous()) - { - KCTextBlockData *blockData=(KCTextBlockData *)i.userData(); + //Check search position + int currentCursorPostion=forward?_textCursor.selectionStart(): + _textCursor.selectionEnd(); + _textCursor.setPosition(currentCursorPostion); + //Search from current place to next place + for(QTextBlock i=_textCursor.block(); //From current block + i.isValid() && !hasMatch; //to the destination block + i=(forward)?i.previous():i.next()) //If search forward, to previous + { //otherwise to the next. + KCTextBlockData *blockData=(KCTextBlockData *) i.userData(); checkWhetherBlockSearchedAndDealWith(i); blockData->beginUsingSearchDatas(); - if(blockData->hasMatched()) + if(blockData->hasMatched()) //If current have search result { + //If position is forward if(forward) { - auto end=blockData->getEndMatchedTextPosition(); - for(auto j=blockData->getFirstMatchedTextPosition(); - jgetEndMatchedTextPosition(); + auto end=blockData->getFirstMatchedTextPosition(); + for(auto j=begin; + j>=end; + j--) { - if(j->pos >= ((i.blockNumber() == _textCursor.blockNumber())? - _textCursor.positionInBlock():0)) + /* + * If it is before the cursor in the current line. + * Or it is before the current block, then we find it. + */ + if((i.blockNumber() == _textCursor.blockNumber() && + j->pos < currentCursorPostion) || + i.blockNumber() != _textCursor.blockNumber()) { hasMatch=true; _textCursor.setPosition(i.position()+j->pos); @@ -175,14 +184,14 @@ bool KCTextEditor::findString(bool forward) } else { - auto end=blockData->getFirstMatchedTextPosition(); - for(auto j=blockData->getEndMatchedTextPosition()-1; - j>=end; - j--) + auto begin=blockData->getFirstMatchedTextPosition(); + auto end=blockData->getEndMatchedTextPosition(); + for(auto j=begin; + jpos < _textCursor.positionInBlock()) || - i.blockNumber() != _textCursor.blockNumber()) + if(j->pos >= ((i.blockNumber()==_textCursor.blockNumber())? + _textCursor.positionInBlock():0)) { hasMatch=true; _textCursor.setPosition(i.position()+j->pos); @@ -196,37 +205,42 @@ bool KCTextEditor::findString(bool forward) } blockData->endUsingSearchDatas(); } - + //If there's not find, check loop if(!hasMatch) { + //Set the end block number, it will be the current block int endBlockNumber=_textCursor.blockNumber(); - for(QTextBlock i= (forward==true)? - document()->firstBlock(): - document()->lastBlock().previous(); - + //If search forward, to the last block, else to the first + for(QTextBlock i=(forward)? + document()->lastBlock(): + document()->firstBlock(); + //Here, we should check to the end block i.isValid() && - (forward==true? - i.blockNumber()endBlockNumber) && + (forward? + i.blockNumber()>=endBlockNumber: + i.blockNumber()<=endBlockNumber) && !hasMatch; - - i=(forward==true)?i.next():i.previous()) + //Continue to the next one + i=forward?i.previous():i.next()) { + KCTextBlockData *blockData=(KCTextBlockData *)i.userData(); checkWhetherBlockSearchedAndDealWith(i); blockData->beginUsingSearchDatas(); if(blockData->hasMatched()) { + //If we search to the forward, do the same things. if(forward) { - auto end=blockData->getEndMatchedTextPosition(); - for(auto j=blockData->getFirstMatchedTextPosition(); - jgetFirstMatchedTextPosition(); + for(auto j=blockData->getEndMatchedTextPosition()-1; + j>=end; + j--) { - if(j->pos >= (i.blockNumber() == _textCursor.blockNumber())? - _textCursor.positionInBlock():0) + if((i.blockNumber() == _textCursor.blockNumber() && + j->pos > _textCursor.positionInBlock()) || + i.blockNumber() != _textCursor.blockNumber()) { hasMatch=true; _textCursor.setPosition(i.position()+j->pos); @@ -239,10 +253,11 @@ bool KCTextEditor::findString(bool forward) } else { - auto end=blockData->getFirstMatchedTextPosition(); - for(auto j=blockData->getEndMatchedTextPosition()-1; - j>=end; - j--) + //search from end to the first + auto end=blockData->getEndMatchedTextPosition(); + for(auto j=blockData->getFirstMatchedTextPosition(); + jpos < _textCursor.positionInBlock()) || @@ -270,6 +285,22 @@ bool KCTextEditor::findString(bool forward) return false; } +bool KCTextEditor::findPreviousString(KCTextBlockData *blockData, + QTextCursor currentCursor, + bool forward) +{ + +} + +bool KCTextEditor::findNextString(KCTextBlockData *blockData, + QTextCursor currentCursor, + bool forward) +{ + +} + + + void KCTextEditor::searchString(QString searchTextSets, bool regularExpressionSets, bool caseSensitivelySets, @@ -302,7 +333,7 @@ void KCTextEditor::searchString(QString searchTextSets, updateSearchResults(); searchOnOtherThread(searcherForNext,threadNext,firstVisibleBlock(),true); searchOnOtherThread(searcherForPrev,threadPrev,firstVisibleBlock(),false); - findString(true); + findString(false); } } diff --git a/src/app/kctexteditor.h b/src/app/kctexteditor.h index 693f831..d441d2e 100755 --- a/src/app/kctexteditor.h +++ b/src/app/kctexteditor.h @@ -52,6 +52,8 @@ class KCTextEditor : public QPlainTextEdit void updated(); void searchStringChangedByShortCut(QString searchText); void overwriteModeChanged(bool newValue); + void matchedResult(); + void nomatchedResult(); public slots: void updateHighlights(); @@ -89,6 +91,12 @@ private slots: QTextCursor cursor); QString parenthesesPair(const QString &parenthesesChar); bool findString(bool forward); + bool findPreviousString(KCTextBlockData *blockData, + QTextCursor currentCursor, + bool forward); + bool findNextString(KCTextBlockData *blockData, + QTextCursor currentCursor, + bool forward); void generalSearch(const QTextBlock &block, const int &lines, const bool forward);