PDA

View Full Version : Unicode problem in hoverlink



anju123
27th August 2007, 12:32
Hi ,
I am having a little problem with the links. I got his code from someone, which is used to give a hover effect on the links. The code is running fine on ASCII charactes, but goes haywire while using unicode characters, the problem seems to be that complex characters are built using more then one characters and hence the code so not show the whole text as underlined and infact breaks the characters on hover. I am not able to find the solution to this problem. Kindly help me. I am pasting the code below:


class HoverLinkEnabler_C : public QObject {

QTextEdit *_editor;
QTextCursor _last_cursor;

public:

HoverLinkEnabler_C(QTextEdit *editor) :
_editor(editor)
{
_editor->viewport()->installEventFilter(this);
_editor->viewport()->setMouseTracking(true);
}

virtual bool eventFilter(QObject *watched, QEvent *event)
{
if ( watched == _editor->viewport() ) {
if ( event->type() == QEvent::MouseMove ) {

QMouseEvent *mouse_event = static_cast<QMouseEvent*>(event);
QString anchor = _editor->anchorAt(mouse_event->pos());

if ( anchor.isEmpty() ) {
if ( ! _last_cursor.isNull() ) {
EndHover(_last_cursor);
_last_cursor = QTextCursor();
}
} else {
if ( _last_cursor.isNull() ) {
QTextCursor mouse_cursor = _editor->cursorForPosition(mouse_event->pos());
QTextCursor link_cursor = GetLinkAt(mouse_cursor);
StartHover(link_cursor);
_last_cursor = link_cursor;
}
}

} else if ( event->type() == QEvent::Leave ) {

if ( ! _last_cursor.isNull() ) {
EndHover(_last_cursor);
_last_cursor = QTextCursor();
}

}

}

return QObject::eventFilter(watched, event);
}

private:

QTextCharFormat GetFormatOf(QTextCursor cursor, bool skip_first = false)
{
if ( skip_first && !cursor.atBlockEnd() ) {
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
}
return cursor.charFormat();
}

QTextCursor GetLinkAt(QTextCursor cursor)
{
QTextCharFormat fmt = GetFormatOf(cursor, true);

int first_pos = cursor.position();
while ( !cursor.atBlockStart() && cursor.charFormat() == fmt && cursor.movePosition(QTextCursor::PreviousCharacter , QTextCursor::MoveAnchor) ) {
first_pos--;
}

int last_pos = cursor.position();
while ( !cursor.atBlockEnd() && cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor) && cursor.charFormat() == fmt ) {
last_pos++;
}

cursor.setPosition(first_pos, QTextCursor::MoveAnchor);
cursor.setPosition(last_pos, QTextCursor::KeepAnchor);
return cursor;
}

void StartHover(QTextCursor hover_cursor)
{
QTextCharFormat fmt = GetFormatOf(hover_cursor);
fmt.setUnderlineStyle(QTextCharFormat::SingleUnder line);
hover_cursor.setCharFormat(fmt);
}

void EndHover(QTextCursor hover_cursor)
{
QTextCharFormat fmt = GetFormatOf(hover_cursor);
fmt.setUnderlineStyle(QTextCharFormat::NoUnderline );
hover_cursor.setCharFormat(fmt);
}

};

marcel
27th August 2007, 12:58
Set an utf8 text codec for strings before using that class:


QTextCodec::setCodecForCStrings(QTextCodec::codecF orName("utf8"));


Regards

anju123
27th August 2007, 13:56
Well When I searched the forum for similar problem , I got the same solution but this is not working. It seems that we are using this API to display the text on the screen, but in my case everything is coming fine on the screen, only the hover link code seems to be workin incorrectly on the complex characters