diff options
Diffstat (limited to 'src/mainwindow/addressbar.cpp')
-rw-r--r-- | src/mainwindow/addressbar.cpp | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/mainwindow/addressbar.cpp b/src/mainwindow/addressbar.cpp new file mode 100644 index 0000000..42fa890 --- /dev/null +++ b/src/mainwindow/addressbar.cpp @@ -0,0 +1,63 @@ +/* + * This file is part of smolbote. It's copyrighted by the contributors recorded + * in the version control history of the file, available from its original + * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#include "addressbar.h" +#include "ui_addressbar.h" +#include <QShortcut> +#include "configuration.h" + +AddressBar::AddressBar(QWidget *parent) + : QWidget(parent) + , ui(new Ui::AddressBar) +{ + ui->setupUi(this); + + Configuration conf; + ui->urlBar->menuAction->setShortcut(QKeySequence(conf.value<QString>("shortcuts.address.menu").value())); + + auto *focusShortcut = new QShortcut(QKeySequence(conf.value<QString>("shortcuts.address.focus").value()), parent); + connect(focusShortcut, &QShortcut::activated, ui->urlBar, [=]() { + ui->urlBar->setFocus(); + ui->urlBar->selectAll(); + }); + + 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(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(ui->urlBar->text().contains(QRegularExpression("\\w+://|\\.")) && url.isValid()) { + emit load(url); + } else { + emit search(ui->urlBar->text()); + } + }); +} + +AddressBar::~AddressBar() +{ + disconnect(this); +} + +void AddressBar::setUrl(const QUrl &url) +{ + if(url.isEmpty()) + ui->urlBar->clear(); + else + ui->urlBar->setUrl(url); +} + +void AddressBar::setProgress(int value) { + ui->loadingBar->setValue(std::min(value, 100)); +} |