diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-08-07 13:01:36 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-08-07 13:10:10 +0200 |
commit | 8091212801c82bbbcf2ef6e22f948c71b39f0f54 (patch) | |
tree | 471953ed5992b796300506aed20bc3dd4634561c | |
parent | Add move tab left/right shortcut (diff) | |
download | smolbote-8091212801c82bbbcf2ef6e22f948c71b39f0f54.tar.xz |
AddressBar: prevent URL formatting caused by context menu
-rw-r--r-- | lib/addressbar/CMakeLists.txt | 1 | ||||
-rw-r--r-- | lib/addressbar/addressbar.cpp | 43 | ||||
-rw-r--r-- | lib/addressbar/addressbar.h | 9 | ||||
-rw-r--r-- | lib/addressbar/addressbar.ui | 59 | ||||
-rw-r--r-- | lib/addressbar/urllineedit.cpp | 14 |
5 files changed, 95 insertions, 31 deletions
diff --git a/lib/addressbar/CMakeLists.txt b/lib/addressbar/CMakeLists.txt index 63cf2bc..3a1ab2d 100644 --- a/lib/addressbar/CMakeLists.txt +++ b/lib/addressbar/CMakeLists.txt @@ -9,6 +9,7 @@ set(CMAKE_AUTORCC ON) add_library(addressbar addressbar.cpp addressbar.h + addressbar.ui completer.cpp completer.h urllineedit.cpp diff --git a/lib/addressbar/addressbar.cpp b/lib/addressbar/addressbar.cpp index 355393e..82c2d2e 100644 --- a/lib/addressbar/addressbar.cpp +++ b/lib/addressbar/addressbar.cpp @@ -7,51 +7,40 @@ */ #include "addressbar.h" -#include "urllineedit.h" -#include <QProgressBar> +#include "ui_addressbar.h" #include <QShortcut> -#include <QVBoxLayout> AddressBar::AddressBar(const QHash<QString, QString> &config, QWidget *parent) : QWidget(parent) + , ui(new Ui::AddressBar) { - setLayout(new QVBoxLayout()); - layout()->setContentsMargins(0, 0, 0, 0); - layout()->setSpacing(0); + ui->setupUi(this); - urlBar = new UrlLineEdit(this); - urlBar->menuAction->setShortcut(QKeySequence(config.value("addressbar.shortcuts.menu"))); - layout()->addWidget(urlBar); + ui->urlBar->menuAction->setShortcut(QKeySequence(config.value("addressbar.shortcuts.menu"))); auto *focusShortcut = new QShortcut(QKeySequence(config.value("addressbar.shortcuts.focus")), parent); - connect(focusShortcut, &QShortcut::activated, urlBar, [=]() { - urlBar->setFocus(); - urlBar->selectAll(); + connect(focusShortcut, &QShortcut::activated, ui->urlBar, [=]() { + ui->urlBar->setFocus(); + ui->urlBar->selectAll(); }); - connect(urlBar, &UrlLineEdit::textEdited, [=](const QString &text) { - std::function<void(QStringList &)> callback = std::bind(&UrlLineEdit::updateCompleter, urlBar, std::placeholders::_1); + connect(ui->urlBar, &UrlLineEdit::textEdited, [=](const QString &text) { + std::function<void(QStringList &)> callback = std::bind(&UrlLineEdit::updateCompleter, ui->urlBar, std::placeholders::_1); emit complete(text, callback); }); - connect(urlBar, &UrlLineEdit::returnPressed, [=]() { - const QUrl url = QUrl::fromUserInput(urlBar->text()); + connect(ui->urlBar, &UrlLineEdit::returnPressed, [=]() { + const QUrl url = QUrl::fromUserInput(ui->urlBar->text()); // check if url contains \w+:// (matches protocol://) or contains a '.' (matches site.domain) // this is because single words are valid URLs for QUrl (searchterm becomes http://searchterm) // check for protocol://site because \. wouldn't match it (localhost is a search term; http://localhost is an address) - if(urlBar->text().contains(QRegularExpression("\\w+://|\\.")) && url.isValid()) { + if(ui->urlBar->text().contains(QRegularExpression("\\w+://|\\.")) && url.isValid()) { emit load(url); } else { - emit search(urlBar->text()); + emit search(ui->urlBar->text()); } }); - - progressBar = new QProgressBar(this); - progressBar->setObjectName("LoadingBar"); - progressBar->setMaximumHeight(5); - progressBar->setTextVisible(false); - layout()->addWidget(progressBar); } AddressBar::~AddressBar() @@ -62,11 +51,11 @@ AddressBar::~AddressBar() void AddressBar::setUrl(const QUrl &url) { if(url.isEmpty()) - urlBar->clear(); + ui->urlBar->clear(); else - urlBar->setUrl(url); + ui->urlBar->setUrl(url); } void AddressBar::setProgress(int value) { - progressBar->setValue(std::min(value, 100)); + ui->loadingBar->setValue(std::min(value, 100)); } diff --git a/lib/addressbar/addressbar.h b/lib/addressbar/addressbar.h index 6ee9b74..0da9cb3 100644 --- a/lib/addressbar/addressbar.h +++ b/lib/addressbar/addressbar.h @@ -12,8 +12,10 @@ #include <QWidget> #include <functional> -class UrlLineEdit; -class QProgressBar; +namespace Ui { +class AddressBar; +} + class AddressBar : public QWidget { Q_OBJECT @@ -34,8 +36,7 @@ public slots: void setProgress(int value); private: - UrlLineEdit *urlBar; - QProgressBar *progressBar; + Ui::AddressBar *ui; }; #endif // SMOLBOTE_ADDRESSBAR_H diff --git a/lib/addressbar/addressbar.ui b/lib/addressbar/addressbar.ui new file mode 100644 index 0000000..a5b4a4d --- /dev/null +++ b/lib/addressbar/addressbar.ui @@ -0,0 +1,59 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>AddressBar</class> + <widget class="QWidget" name="AddressBar"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>400</width> + <height>31</height> + </rect> + </property> + <property name="windowTitle"> + <string>Form</string> + </property> + <layout class="QVBoxLayout" name="verticalLayout"> + <property name="spacing"> + <number>0</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <widget class="UrlLineEdit" name="urlBar"/> + </item> + <item> + <widget class="QProgressBar" name="loadingBar"> + <property name="maximumSize"> + <size> + <width>16777215</width> + <height>5</height> + </size> + </property> + <property name="textVisible"> + <bool>false</bool> + </property> + </widget> + </item> + </layout> + </widget> + <customwidgets> + <customwidget> + <class>UrlLineEdit</class> + <extends>QLineEdit</extends> + <header>urllineedit.h</header> + </customwidget> + </customwidgets> + <resources/> + <connections/> +</ui> diff --git a/lib/addressbar/urllineedit.cpp b/lib/addressbar/urllineedit.cpp index edbe327..6af6cf5 100644 --- a/lib/addressbar/urllineedit.cpp +++ b/lib/addressbar/urllineedit.cpp @@ -101,12 +101,26 @@ void UrlLineEdit::updateCompleter(const QStringList &l) void UrlLineEdit::focusInEvent(QFocusEvent *event) { + // a context menu event also causes a focusInEvent, so if text is selected + // skip the formatting step + if(event->reason() == Qt::PopupFocusReason) { + QLineEdit::focusInEvent(event); + return; + } + clearTextFormat(); QLineEdit::focusInEvent(event); } void UrlLineEdit::focusOutEvent(QFocusEvent *event) { + // a context menu event causes a focusOutEvent, and setUrl will clear the + // selection, and this would prevent the menu from working properly + if(event->reason() == Qt::PopupFocusReason) { + QLineEdit::focusOutEvent(event); + return; + } + const QUrl url = QUrl::fromUserInput(text()); if(url.isValid()) setUrl(url); |