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 {
public:
_editor(editor)
{
_editor->viewport()->installEventFilter(this);
_editor->viewport()->setMouseTracking(true);
}
{
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);
}
} else {
if ( _last_cursor.isNull() ) {
QTextCursor mouse_cursor
= _editor
->cursorForPosition
(mouse_event
->pos
());
StartHover(link_cursor);
_last_cursor = link_cursor;
}
}
} else if ( event
->type
() == QEvent::Leave ) {
if ( ! _last_cursor.isNull() ) {
EndHover(_last_cursor);
}
}
}
return QObject::eventFilter(watched, event
);
}
private:
{
if ( skip_first && !cursor.atBlockEnd() ) {
}
return cursor.charFormat();
}
{
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);
return cursor;
}
{
hover_cursor.setCharFormat(fmt);
}
{
hover_cursor.setCharFormat(fmt);
}
};
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::SingleUnderline);
hover_cursor.setCharFormat(fmt);
}
void EndHover(QTextCursor hover_cursor)
{
QTextCharFormat fmt = GetFormatOf(hover_cursor);
fmt.setUnderlineStyle(QTextCharFormat::NoUnderline);
hover_cursor.setCharFormat(fmt);
}
};
To copy to clipboard, switch view to plain text mode
Bookmarks