]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/widgets/UrlLineEdit.cpp
a06cf973b6f434f0b6d5fc1c65a8df6084e9fe6b
[PrivacyBrowserPC.git] / src / widgets / UrlLineEdit.cpp
1 /*
2  * Copyright 2024 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
5  *
6  * Privacy Browser PC is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Privacy Browser PC is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Privacy Browser PC.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 // Application headers.
21 #include "UrlLineEdit.h"
22
23 // Qt toolkit headers.
24 #include <QInputMethodEvent>
25
26 // Construct the class.
27 UrlLineEdit::UrlLineEdit(QWidget *parentWidgetPointer) : KLineEdit(parentWidgetPointer) {}
28
29 void UrlLineEdit::focusInEvent(QFocusEvent *focusEventPointer)
30 {
31     // Create an input method event attribute list.
32     QList<QInputMethodEvent::Attribute> inputMethodEventAttributeList;
33
34     // Create an input method event with no formatting.
35     QInputMethodEvent inputMethodEvent(QString(QLatin1String("")), inputMethodEventAttributeList);
36
37     // Apply the empty formatting to remove the URL highlighting.
38     event(&inputMethodEvent);
39
40     // Run the default commands.
41     KLineEdit::focusInEvent(focusEventPointer);
42 }
43
44 void UrlLineEdit::focusOutEvent(QFocusEvent *focusEventPointer)
45 {
46     // Reapply the highlight to the current text.
47     setText(text());
48
49     // Run the default commands.
50     KLineEdit::focusOutEvent(focusEventPointer);
51 }
52
53 void UrlLineEdit::setText(const QString &urlString)
54 {
55     // Wipe the currently displayed text.
56     KLineEdit::setText(QLatin1String(""));
57
58     // Create an input method event attribute list.
59     QList<QInputMethodEvent::Attribute> inputMethodEventAttributeList;
60
61     // Calculate the URL string length.
62     int urlStringLength = urlString.length();
63
64     // Get the index of end of the schema.
65     int endOfSchemaIndex = urlString.indexOf(QLatin1String("//")) + 2;
66
67     // Get the index of the last `.` in the domain.
68     int lastDotIndex = urlString.lastIndexOf(QLatin1Char('.'));
69
70     // Get the index of the penultimate `.` in the domain.  If this doesn't exist it will be set to `-1`.
71     int penultimateDotIndex = urlString.lastIndexOf(QLatin1Char('.'), lastDotIndex - urlStringLength - 1);
72
73     // Get the index of the `/` immediately after the domain name.  If this doesn't exit it will be set to `-1`.
74     int endOfDomainNameIndex = urlString.indexOf(QLatin1Char('/'), endOfSchemaIndex);
75
76     // Create the text character formats.
77     QTextCharFormat grayTextCharFormat;
78     QTextCharFormat schemaTextCharFormat;
79
80     // Set the text character format colors.
81     grayTextCharFormat.setForeground(Qt::darkGray);
82
83     // Set the schema text character format color.
84     if (urlString.startsWith(QLatin1String("http://")) || urlString.startsWith(QLatin1String("view-source:http://")))
85         schemaTextCharFormat.setForeground(Qt::red);
86     else
87         schemaTextCharFormat = grayTextCharFormat;
88
89     // Append the schema text format format.
90     inputMethodEventAttributeList.append(QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, 0, endOfSchemaIndex, schemaTextCharFormat));
91
92     // Append the subdomain format if the subdomain exists.  If it doesn't, the penultimate dot index will be `-1`.
93     if (penultimateDotIndex > 0)
94         inputMethodEventAttributeList.append(QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, endOfSchemaIndex, penultimateDotIndex + 1 - endOfSchemaIndex, grayTextCharFormat));
95
96     // Append the post domain formatting if text exists after the domain name.  If it doesn't, the end of domain name index will be `-1`.
97     if (endOfDomainNameIndex > 0)
98         inputMethodEventAttributeList.append(QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, endOfDomainNameIndex, urlStringLength - endOfDomainNameIndex, grayTextCharFormat));
99
100     // Create an input method event with an empty pre-edit string.
101     QInputMethodEvent inputMethodEvent(QString(""), inputMethodEventAttributeList);
102
103     // Apply the URL highlighting.
104     event(&inputMethodEvent);
105
106     // Run the default commands.
107     KLineEdit::setText(urlString);
108 }