]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/commitdiff
Add URL syntax highlighting. https://redmine.stoutner.com/issues/1024
authorSoren Stoutner <soren@stoutner.com>
Wed, 21 Feb 2024 18:11:07 +0000 (11:11 -0700)
committerSoren Stoutner <soren@stoutner.com>
Wed, 21 Feb 2024 18:11:07 +0000 (11:11 -0700)
src/helpers/UserAgentHelper.cpp
src/widgets/CMakeLists.txt
src/widgets/UrlLineEdit.cpp [new file with mode: 0644]
src/widgets/UrlLineEdit.h [new file with mode: 0644]
src/windows/BrowserWindow.cpp
src/windows/BrowserWindow.h

index de58905f08b2f355874c6cae7e57c74705acb358..891702d2d371e95d71e38eb35a9fc307cfe87139 100644 (file)
@@ -39,10 +39,10 @@ const QString UserAgentHelper::SAFARI_MACOS_DATABASE = QLatin1String("Safari mac
 // Define the public user agent constants.
 const QString UserAgentHelper::PRIVACY_BROWSER_USER_AGENT = QLatin1String("PrivacyBrowser/1.0");
 const QString UserAgentHelper::FIREFOX_LINUX_USER_AGENT = QLatin1String("Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0");
-const QString UserAgentHelper::CHROMIUM_LINUX_USER_AGENT = QLatin1String("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36");
-const QString UserAgentHelper::FIREFOX_WINDOWS_USER_AGENT = QLatin1String("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/119.0");
-const QString UserAgentHelper::CHROME_WINDOWS_USER_AGENT = QLatin1String("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36");
-const QString UserAgentHelper::EDGE_WINDOWS_USER_AGENT = QLatin1String("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0");
+const QString UserAgentHelper::CHROMIUM_LINUX_USER_AGENT = QLatin1String("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36");
+const QString UserAgentHelper::FIREFOX_WINDOWS_USER_AGENT = QLatin1String("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0");
+const QString UserAgentHelper::CHROME_WINDOWS_USER_AGENT = QLatin1String("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36");
+const QString UserAgentHelper::EDGE_WINDOWS_USER_AGENT = QLatin1String("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 Edg/121.0.0.0");
 const QString UserAgentHelper::SAFARI_MACOS_USER_AGENT = QLatin1String("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15");
 
 // Construct the class.
index f161dec2afc0bd216808bf5b466f5be4b621cf6b..9fe904f0761dcfec36dac8c3b4827d8ed298dc40 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright 2022-2023 Soren Stoutner <soren@stoutner.com>.
+# Copyright 2022-2024 Soren Stoutner <soren@stoutner.com>.
 #
 # This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
 #
@@ -22,4 +22,5 @@ target_sources(privacybrowser PRIVATE
     DraggableTreeView.cpp
     PrivacyWebEngineView.cpp
     TabWidget.cpp
+    UrlLineEdit.cpp
 )
diff --git a/src/widgets/UrlLineEdit.cpp b/src/widgets/UrlLineEdit.cpp
new file mode 100644 (file)
index 0000000..a06cf97
--- /dev/null
@@ -0,0 +1,108 @@
+/*
+ * Copyright 2024 Soren Stoutner <soren@stoutner.com>.
+ *
+ * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
+ *
+ * Privacy Browser PC is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Privacy Browser PC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Privacy Browser PC.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+// Application headers.
+#include "UrlLineEdit.h"
+
+// Qt toolkit headers.
+#include <QInputMethodEvent>
+
+// Construct the class.
+UrlLineEdit::UrlLineEdit(QWidget *parentWidgetPointer) : KLineEdit(parentWidgetPointer) {}
+
+void UrlLineEdit::focusInEvent(QFocusEvent *focusEventPointer)
+{
+    // Create an input method event attribute list.
+    QList<QInputMethodEvent::Attribute> inputMethodEventAttributeList;
+
+    // Create an input method event with no formatting.
+    QInputMethodEvent inputMethodEvent(QString(QLatin1String("")), inputMethodEventAttributeList);
+
+    // Apply the empty formatting to remove the URL highlighting.
+    event(&inputMethodEvent);
+
+    // Run the default commands.
+    KLineEdit::focusInEvent(focusEventPointer);
+}
+
+void UrlLineEdit::focusOutEvent(QFocusEvent *focusEventPointer)
+{
+    // Reapply the highlight to the current text.
+    setText(text());
+
+    // Run the default commands.
+    KLineEdit::focusOutEvent(focusEventPointer);
+}
+
+void UrlLineEdit::setText(const QString &urlString)
+{
+    // Wipe the currently displayed text.
+    KLineEdit::setText(QLatin1String(""));
+
+    // Create an input method event attribute list.
+    QList<QInputMethodEvent::Attribute> inputMethodEventAttributeList;
+
+    // Calculate the URL string length.
+    int urlStringLength = urlString.length();
+
+    // Get the index of end of the schema.
+    int endOfSchemaIndex = urlString.indexOf(QLatin1String("//")) + 2;
+
+    // Get the index of the last `.` in the domain.
+    int lastDotIndex = urlString.lastIndexOf(QLatin1Char('.'));
+
+    // Get the index of the penultimate `.` in the domain.  If this doesn't exist it will be set to `-1`.
+    int penultimateDotIndex = urlString.lastIndexOf(QLatin1Char('.'), lastDotIndex - urlStringLength - 1);
+
+    // Get the index of the `/` immediately after the domain name.  If this doesn't exit it will be set to `-1`.
+    int endOfDomainNameIndex = urlString.indexOf(QLatin1Char('/'), endOfSchemaIndex);
+
+    // Create the text character formats.
+    QTextCharFormat grayTextCharFormat;
+    QTextCharFormat schemaTextCharFormat;
+
+    // Set the text character format colors.
+    grayTextCharFormat.setForeground(Qt::darkGray);
+
+    // Set the schema text character format color.
+    if (urlString.startsWith(QLatin1String("http://")) || urlString.startsWith(QLatin1String("view-source:http://")))
+        schemaTextCharFormat.setForeground(Qt::red);
+    else
+        schemaTextCharFormat = grayTextCharFormat;
+
+    // Append the schema text format format.
+    inputMethodEventAttributeList.append(QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, 0, endOfSchemaIndex, schemaTextCharFormat));
+
+    // Append the subdomain format if the subdomain exists.  If it doesn't, the penultimate dot index will be `-1`.
+    if (penultimateDotIndex > 0)
+        inputMethodEventAttributeList.append(QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, endOfSchemaIndex, penultimateDotIndex + 1 - endOfSchemaIndex, grayTextCharFormat));
+
+    // 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`.
+    if (endOfDomainNameIndex > 0)
+        inputMethodEventAttributeList.append(QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, endOfDomainNameIndex, urlStringLength - endOfDomainNameIndex, grayTextCharFormat));
+
+    // Create an input method event with an empty pre-edit string.
+    QInputMethodEvent inputMethodEvent(QString(""), inputMethodEventAttributeList);
+
+    // Apply the URL highlighting.
+    event(&inputMethodEvent);
+
+    // Run the default commands.
+    KLineEdit::setText(urlString);
+}
diff --git a/src/widgets/UrlLineEdit.h b/src/widgets/UrlLineEdit.h
new file mode 100644 (file)
index 0000000..f57232a
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2024 Soren Stoutner <soren@stoutner.com>.
+ *
+ * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
+ *
+ * Privacy Browser PC is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Privacy Browser PC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Privacy Browser PC.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef URLLINEEDIT_H
+#define URLLINEEDIT_H
+
+// KDE Framework headers.
+#include <KLineEdit>
+
+class UrlLineEdit : public KLineEdit
+{
+    // Include the Q_OBJECT macro.
+    Q_OBJECT
+
+public:
+    // The default constructor.
+    explicit UrlLineEdit(QWidget *parentWidgetPointer = nullptr);
+
+    // The public functions.
+    void setText(const QString &urlString) override;
+
+protected:
+    // The protected functions.
+    void focusInEvent(QFocusEvent *focusEventPointer) override;
+    void focusOutEvent(QFocusEvent *focusEventPointer) override;
+};
+#endif
index 88f9894aeebb71ca1f43c5ae14c55d9878f8e1f0..f0cd4dfc76e33223e4d75c44deacefd08b3c72ce 100644 (file)
@@ -398,7 +398,7 @@ BrowserWindow::BrowserWindow(bool firstWindow, QString *initialUrlStringPointer)
     connect(bookmarksToolBarPointer, SIGNAL(visibilityChanged(bool)), this, SLOT(updateViewBookmarksToolBarCheckbox(bool)));
 
     // Create the line edits.
-    urlLineEditPointer = new KLineEdit();
+    urlLineEditPointer = new UrlLineEdit();
     findTextLineEditPointer = new KLineEdit();
 
     // Get the line edit size policies.
index 74e31b88dc6947359f7c695fb7fa16ff9f918329..29c8d60d86b97cf584f9d72ccf107cbeed8764ba 100644 (file)
@@ -22,6 +22,7 @@
 
 // Application headers.
 #include "widgets/TabWidget.h"
+#include "widgets/UrlLineEdit.h"
 
 // KDE Frameworks headers.
 #include <KConfigDialog>
@@ -168,7 +169,7 @@ private:
     QAction *userAgentEdgeWindowsActionPointer;
     QAction *userAgentSafariMacosActionPointer;
     QAction *userAgentCustomActionPointer;
-    KLineEdit *urlLineEditPointer;
+    UrlLineEdit *urlLineEditPointer;
     KToolBar *urlToolBarPointer;
     QAction *viewBookmarksToolBarActionPointer;
     QAction *viewSourceActionPointer;