aboutsummaryrefslogtreecommitdiff
path: root/src/widgets/urllineedit.cpp
blob: 25bf157aed9b3df5581a654d820fb527715ad5a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "urllineedit.h"
#include <QUrl>

UrlLineEdit::UrlLineEdit(QWidget *parent) :
    QLineEdit(parent)
{
    //setStyleSheet("color: #808080");
}

void UrlLineEdit::setUrl(const QUrl &url)
{
    QString urlText = url.toString();
    QString domain = url.host();

    QTextCharFormat f_host;

    f_host.setFontWeight(QFont::Bold);
    //f_host.setForeground(QBrush(QColor::fromRgb(255, 255, 255)));

    QTextLayout::FormatRange fr_tracker;
    fr_tracker.start = urlText.indexOf(domain);
    fr_tracker.length = domain.length();
    fr_tracker.format = f_host;

    clear();
    clearTextFormat();
    setTextFormat(fr_tracker);
    setText(urlText);
}

void UrlLineEdit::setTextFormat(const QTextLayout::FormatRange &format)
{
    QList<QInputMethodEvent::Attribute> attributes;
    attributes.append(QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, format.start, format.length, format.format));
    QInputMethodEvent ev(QString(), attributes);
    event(&ev);

}

void UrlLineEdit::clearTextFormat()
{
    setTextFormat(QTextLayout::FormatRange());
}