aboutsummaryrefslogtreecommitdiff
path: root/src/addressbar/addressbar.cpp
blob: b86230ce42ecfc5ec3103eb7fb8063e539f0f858 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * 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/smolbote.hg
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "addressbar.h"
#include "urllineedit.h"
#include "webengine/webview.h"
#include <QProgressBar>
#include <QShortcut>
#include <QVBoxLayout>

AddressBar::AddressBar(const QHash<QString, QString> &config, QWidget *parent)
    : QWidget(parent)
{
    setLayout(new QVBoxLayout());
    layout()->setContentsMargins(0, 0, 0, 0);
    layout()->setSpacing(0);

    urlBar = new UrlLineEdit(this);
    layout()->addWidget(urlBar);

    auto *focusShortcut = new QShortcut(QKeySequence(config.value("addressbar.shortcuts.focus")), parent);
    connect(focusShortcut, &QShortcut::activated, urlBar, [=]() {
        urlBar->setFocus();
        urlBar->selectAll();
    });

    urlBar->pageMenu_action->setShortcut(QKeySequence(config.value("addressbar.shortcuts.pageMenu")));
    urlBar->pageMenu_action->setToolTip(tr("Page Actions (%1)").arg(urlBar->pageMenu_action->shortcut().toString()));

    urlBar->toolsMenu_action->setShortcut(QKeySequence(config.value("addressbar.shortcuts.toolsMenu")));
    urlBar->toolsMenu_action->setToolTip(tr("Tools (%1)").arg(urlBar->toolsMenu_action->shortcut().toString()));

    connect(urlBar, &UrlLineEdit::textEdited, [=](const QString &text) {
        std::function<void(QStringList &)> callback = std::bind(&UrlLineEdit::updateCompleter, urlBar, std::placeholders::_1);
        emit complete(text, callback);
    });

    progressBar = new QProgressBar(this);
    progressBar->setMaximumHeight(5);
    progressBar->setTextVisible(false);
    layout()->addWidget(progressBar);
}

AddressBar::~AddressBar()
{
    disconnect(urlChangedConnection);
    disconnect(loadUrlConnection);
    disconnect(progressBarConnection);
}

void AddressBar::connectWebView(WebView *view)
{
    disconnect(urlChangedConnection);
    disconnect(loadUrlConnection);
    disconnect(progressBarConnection);

    if(view == nullptr) {
        urlBar->clear();
        urlBar->pageMenu_action->setMenu(nullptr);
        urlBar->toolsMenu_action->setMenu(nullptr);

        progressBar->setValue(100);
        return;
    }

    urlBar->setUrl(view->url());
    urlBar->pageMenu_action->setMenu(view->pageMenu());
    urlBar->toolsMenu_action->setMenu(view->toolsMenu());
    urlChangedConnection = connect(view, &WebView::urlChanged, urlBar, &UrlLineEdit::setUrl);
    loadUrlConnection = connect(urlBar, &UrlLineEdit::returnPressed, [=]() {
        if(urlBar->text().startsWith('#')) {
            ;
            view->search(urlBar->text().mid(1));
        } else {
            view->load(QUrl::fromUserInput(urlBar->text()));
        }
        view->setFocus();
    });

    progressBar->setValue(view->loadProgress());
    progressBarConnection = connect(view, &QWebEngineView::loadProgress, progressBar, &QProgressBar::setValue);
}