]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/widgets/UrlLineEdit.cpp
Add a default folder icon to the edit folder dialog. https://redmine.stoutner.com...
[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     // Only highlight the URL if it starts with a known schema.
59     if (urlString.startsWith(QLatin1String("https://")) || urlString.startsWith(QLatin1String("http://")) || urlString.startsWith(QLatin1String("view-source:")))
60     {
61         // Create an input method event attribute list.
62         QList<QInputMethodEvent::Attribute> inputMethodEventAttributeList;
63
64         // Calculate the URL string length.
65         int urlStringLength = urlString.length();
66
67         // Get the index of end of the schema.
68         int endOfSchemaIndex = urlString.indexOf(QLatin1String("//")) + 2;
69
70         // Get the index of the `/` immediately after the domain name.  If this doesn't exit it will be set to `-1`.
71         int endOfDomainNameIndex = urlString.indexOf(QLatin1Char('/'), endOfSchemaIndex);
72
73         // Get the index of the last `.` in the domain.
74         int lastDotIndex = urlString.lastIndexOf(QLatin1Char('.'), endOfDomainNameIndex - urlStringLength);
75
76         // Get the index of the penultimate `.` in the domain.  If this doesn't exist it will be set to `-1`.
77         int penultimateDotIndex = urlString.lastIndexOf(QLatin1Char('.'), lastDotIndex - urlStringLength - 1);
78
79         // Create the text character formats.
80         QTextCharFormat grayTextCharFormat;
81         QTextCharFormat schemaTextCharFormat;
82
83         // Set the text character format colors.
84         grayTextCharFormat.setForeground(Qt::darkGray);
85
86         // Set the schema text character format color.
87         if (urlString.startsWith(QLatin1String("http://")) || urlString.startsWith(QLatin1String("view-source:http://")))
88             schemaTextCharFormat.setForeground(Qt::red);
89         else
90             schemaTextCharFormat = grayTextCharFormat;
91
92         // Append the schema text format format.
93         inputMethodEventAttributeList.append(QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, 0, endOfSchemaIndex, schemaTextCharFormat));
94
95         // Append the subdomain format if the subdomain exists.  If it doesn't, the penultimate dot index will be `-1`.
96         if (penultimateDotIndex > 0)
97             inputMethodEventAttributeList.append(QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, endOfSchemaIndex, penultimateDotIndex + 1 - endOfSchemaIndex, grayTextCharFormat));
98
99         // 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`.
100         if (endOfDomainNameIndex > 0)
101             inputMethodEventAttributeList.append(QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, endOfDomainNameIndex, urlStringLength - endOfDomainNameIndex, grayTextCharFormat));
102
103         // Create an input method event with an empty pre-edit string.
104         QInputMethodEvent inputMethodEvent(QString(""), inputMethodEventAttributeList);
105
106         // Apply the URL highlighting.
107         event(&inputMethodEvent);
108     }
109
110     // Run the default commands.
111     KLineEdit::setText(urlString);
112 }