PDA

View Full Version : Syntax highlight selection problem



s.toonen
1st June 2008, 11:07
Hey,

I have a problem with my syntax highlighting when the user selects some text. It seems that when keywords are selected, non keywords on the same line disappear and vice versa.

It has something to do with the current line marker, that is created in the overridden paint event of QTextEdit.

Below is an image with no text selected:

http://nicky.xept.nl/dennis/s1.png

Now another image with text selected:

http://nicky.xept.nl/dennis/s2.png

For a small project file containing just the issue follow this link:

http://nicky.xept.nl/dennis/highlight.zip

The code for the paint event


void CCodeWidgetText::paintEvent(QPaintEvent *event)
{
QTextEdit::ExtraSelection highlight;
QList<QTextEdit::ExtraSelection> extras;
QList<QTextCursor*> errorCursors;
QTextCursor originalCursor = this->textCursor();



QTextCursor cursor = this->textCursor();
// SAVE THE SELECTION TO LATER RESTORE IT!
// int start = cursor.selectionStart();
// int end = cursor.selectionEnd();

// CLEARING THE SELECTION SOLVES THE HIGHLIGHTING PROBLEM
// BUT IS NOT GOOD
// cursor.clearSelection();

// Mark current line
highlight.cursor = this->textCursor();
highlight.format.setProperty(QTextFormat::FullWidt hSelection, true);
highlight.format.setBackground(QColor(0,255,0));
extras << highlight;

// Add extra selections
this->setExtraSelections(extras);

QTextEdit::paintEvent(event);

// RESTORING THE SELECTION WILL INTRODUCE THE FAULT! (SAVE FIRST ABOVE)
// cursor.setPosition(start, QTextCursor::MoveAnchor);
// cursor.setPosition(end, QTextCursor::KeepAnchor);
// this->setTextCursor(cursor);
}

Thanks in advance for any help on this,

Sander

patrik08
1st June 2008, 11:23
is suppose you use qt version < 4.4 your print screen...

I use qt4.4 if i select only 2 char only this having selection not green line..

As example you can look:
http://fop-miniscribus.googlecode.com/svn/trunk/MiniScribus_2.0.Setup/a_structure/xmlhighlighter.h
http://fop-miniscribus.googlecode.com/svn/trunk/MiniScribus_2.0.Setup/a_structure/xmlhighlighter.cpp

s.toonen
1st June 2008, 11:34
I am sorry, but I don't exactly get what you meant..

patrik08
1st June 2008, 11:49
I am sorry, but I don't exactly get what you meant..

Build qt4.4 and test your highlight.pro , and you have a different result.

s.toonen
1st June 2008, 12:21
Ok thanks for that. However I must use qt 4.3 for this project...
What is the result exactly?

patrik08
1st June 2008, 12:59
Ok thanks for that. However I must use qt 4.3 for this project...
What is the result exactly?

exact as
http://nicky.xept.nl/dennis/s2.png whitout green background , only blue background of selected word...

Why you not get QRect from Paragraph line nr .. and paint this?
take the same effect..

QTextLine return all info..
http://doc.trolltech.com/4.2/qtextline.html


if you like real QSyntaxHighlighter subclass this..

http://wiki.qtcentre.org/index.php?title=XmlHighlighter








QTextLine SpanBorder::currentTextLine(const QTextCursor &cursor)
{
const QTextBlock block = cursor.block();
if (!block.isValid())
return QTextLine();

const QTextLayout *layout = block.layout();
if (!layout)
return QTextLine();

const int relativePos = cursor.position() - block.position();
return layout->lineForTextPosition(relativePos);
}

///// from http://fop-miniscribus.googlecode.com/svn/trunk/GraphicsViewEdit/include/mimedataeditor.cpp



Imo: on table html nothing selection is possibel only Paragraphs..

s.toonen
1st June 2008, 13:31
Thanks, I am trying it right now. I integrated your suggested curentTextLine function. In the paint event I now retrieve the QRectF from the currentTextLine. Now I want to draw this, but I have to figure that out now.

s.toonen
1st June 2008, 13:52
The rects returned to me all have coordinates 0 unfortunately.
Can you help me how to draw please?

Btw, the syntax highlighter just has to highlight some keywords, so I don't need a huge class for this project.

patrik08
1st June 2008, 15:20
this to your pro file....



TEMPLATE = app
TARGET = xx

DESTDIR += ./

CONFIG += console
CONFIG += qt warn_off debug


HEADERS += ccodewidgettext.h
SOURCES += ccodewidgettext.cpp main.cpp


otherwise you can not see debug output....

and launch getXcursor() on

void CCodeWidgetText::paintEvent(QPaintEvent *event)


++

// QT Includes
#include <QSyntaxHighlighter>
#include <QTextEdit>
#include <QWidget>
#include <QTextDocumentFragment>
#include <QTextLayout>
#include <QTextLine>
#include <QAbstractTextDocumentLayout>







void CCodeWidgetText::getXcursor()
{
QTextBlock block = textCursor().block();
int pos = textCursor().position() - block.position();
const QTextLayout *layout = block.layout();
QAbstractTextDocumentLayout *Layout = document()->documentLayout();
QRectF BBrect = Layout->blockBoundingRect ( block );

qreal x = 0;
qreal y = 0;

QTextLine line = layout->lineForTextPosition(pos);
if (line.isValid() && lastY !=-1 ) {
x = line.cursorToX(pos);
y = line.lineNumber() * line.height();
QPointF Qps = line.position();
qDebug() << "### x " << x;
qDebug() << "### BBrect " << BBrect;
}
else {
x = -1; // delayed init. Makes movePosition() call setX later on again.
}
}



BBrect has block QRect and x mouse position ..
so you can paint any area you like ....

on path /incude from code:
http://www.qt-apps.org/content/show.php/GraphicsViewEdit+Layer?content=80234
you find more hint to paint text... and swap selection color...

s.toonen
4th June 2008, 12:15
I got a response from a qt support engineer that I was dealing with a bug in qt 4.3. In qt 4.4 it is fixed but another bug shows up that is scheduled now.

Thanks for your help.