QSyntaxHighlighter problem with highlighting opening and closing braces
-
Hi,
I am trying to make highlighting the opening and closing braces in formula editor.
suppose for example.
((ab) +(ac))
if i select the first opening braces it should highlight the corresponding(last one ) closing braces.
my problem is its highlighting for the first line correctly but for the second line if i am selecting its also showing for the previous line.
i am attaching the screen shot of output as well as my code.
i tried may thing but does not work.
please advise something will be appreciated.thanks
output image link
!http://i.imgur.com/gnISbdN.png()!
@MyHighlighter::MyHighlighter(QTextDocument *parent) :
QSyntaxHighlighter(parent)
{
selectedPos = -1;
}void MyHighlighter::highlightBlock(const QString &text)
{
qDebug()<<text <<"#####";
QTextCursor cursorPos;if(textBlock.isEmpty()) { textBlock = text; qDebug()<<textBlock <<"****"; }
if(getSelectedPosition()>=0 && getSelectedPosition()<=textBlock.length()) { qDebug()<<getSelectedPosition()<<"this"; QChar selChar = textBlock.at(getSelectedPosition()); qDebug() <<"selchar" <<selChar; if(selChar == '(') { int charPos = getSelectedPosition()+1; qDebug() <<"charPos" <<charPos; int openBracketsCount = 1; int currentBlock = -1; int blockStartPosition = -1; for (int cBlock = 0; cBlock < document()->blockCount(); ++cBlock) { if(blockStartPosition==-1) { blockStartPosition = 0; } int ct = document()->findBlock(cBlock).length(); qDebug() <<"int ct" <<ct; if(getSelectedPosition()>=blockStartPosition && getSelectedPosition()<blockStartPosition+ct) { currentBlock = cBlock; break; } blockStartPosition += ct; } setCurrentBlockState(1); QTextBlock currentBlk = document()->begin(); while(currentBlk.isValid()) { for (int var = 0; var < currentBlk.length(); ++var) { if(getSelectedPosition()>=blockStartPosition && getSelectedPosition()<=blockStartPosition) { qDebug()<<"Current block text : "<<currentBlk.text(); } } currentBlk = currentBlk.next(); } while(charPos<= textBlock.length()) { QChar charToCheck = textBlock.at(charPos);
// qDebug()<<charToCheck<<"charToCheck"<<text.length();
if(charToCheck == ')')
{
if(openBracketsCount == 1)
{
// code to highlight charToCheck and selChar
qDebug()<<"sel : "<<getSelectedPosition()<<" - Match : "<<charPos;
qDebug()<<"block : "<<currentBlock<<" - "<<blockStartPosition;QTextCharFormat multiLineCommentFormat; multiLineCommentFormat.setForeground(Qt::red); cursorPos.setCharFormat(multiLineCommentFormat); setCurrentBlockState(0); setFormat(getSelectedPosition()-blockStartPosition,1,multiLineCommentFormat); setFormat(charPos-blockStartPosition,1,multiLineCommentFormat); //QString pattern = "\\b[A-Za-z]+\\b"; break; } else { openBracketsCount--; } } else if(charToCheck == '(') { openBracketsCount++; } charPos++; } } else if(selChar == ')') { int charPos = getSelectedPosition()-1; int closeBracketsCount = 1; while(charPos>=0) { QChar charToCheck = textBlock.at(charPos); if(charToCheck == '(') { if(closeBracketsCount == 1) { // code to highlight charToCheck and selChar qDebug()<<"sel : "<<getSelectedPosition()<<" - Match : "<<charPos;
// QTextCharFormat format;
// format.setForeground(QBrush(QColor(255,0,0)));
QTextCharFormat existingFormat = format(getSelectedPosition()+1);existingFormat.setForeground(Qt::green); existingFormat.setBackground(Qt::black);
// setCurrentBlockState(0);
setFormat(getSelectedPosition(),1,existingFormat); setFormat(charPos,1,existingFormat);
// qDebug()<<text.length();
break; } else { closeBracketsCount--; } } else if(charToCheck == ')') { closeBracketsCount++; } charPos--; } } qDebug()<<"ct block : "<<currentBlock().blockNumber(); }
}
void MyHighlighter::setSelectedPosition(int pos,const QString &text)
{
selectedPos = pos;
textBlock = text;
rehighlight();
}int MyHighlighter::getSelectedPosition()
{
return selectedPos;
}@