aboutsummaryrefslogtreecommitdiff
path: root/src/mainwindow/widgets/navigationbar.cpp
blob: e488d7766970ed59dcf16c33da0f194a158a3ee1 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * 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 "navigationbar.h"
#include "mainwindow/mainwindow.h"
#include <QHBoxLayout>
#include <QMenu>
#include <QShortcut>
#include <QStyle>
#include <QToolBar>
#include <QToolButton>
#include <QWebEngineHistory>

NavigationBar::NavigationBar(MainWindow *parent)
    : QObject(parent)
{
    qStyle = parent->style();

    // Back button
    backButton = new QToolButton(parent);
    backButton->setIcon(qStyle->standardIcon(QStyle::SP_ArrowBack));
    backButton->setShortcut(QString::fromStdString(parent->m_config->value<std::string>("browser.shortcuts.back").value()));
    connect(backButton, &QToolButton::clicked, this, [this]() {
        m_view->history()->back();
    });

    auto *backMenu = new QMenu(backButton);
    backButton->setMenu(backMenu);
    connect(backMenu, &QMenu::aboutToShow, this, [this, backMenu]() {
        backMenu->clear();
        const QList<QWebEngineHistoryItem> items = m_view->history()->backItems(10);
        for(const QWebEngineHistoryItem &i : items) {
            QAction *a = backMenu->addAction(i.title());
            connect(a, &QAction::triggered, this, [i, this]() {
                m_view->history()->goToItem(i);
            });
        }
    });

    // Forward button
    forwardButton = new QToolButton(parent);
    forwardButton->setIcon(qStyle->standardIcon(QStyle::SP_ArrowForward));
    forwardButton->setShortcut(QString::fromStdString(parent->m_config->value<std::string>("browser.shortcuts.forward").value()));
    connect(forwardButton, &QToolButton::clicked, this, [this]() {
        m_view->history()->forward();
    });

    auto *forwardMenu = new QMenu(forwardButton);
    forwardButton->setMenu(forwardMenu);
    connect(forwardMenu, &QMenu::aboutToShow, this, [this, forwardMenu]() {
        forwardMenu->clear();
        const QList<QWebEngineHistoryItem> items = m_view->history()->forwardItems(10);
        for(const QWebEngineHistoryItem &i : items) {
            QAction *a = forwardMenu->addAction(i.title());
            connect(a, &QAction::triggered, this, [i, this]() {
                m_view->history()->goToItem(i);
            });
        }
    });

    // Stop/Refresh button
    stopReloadButton = new QToolButton(parent);
    stopReloadButton->setIcon(qStyle->standardIcon(QStyle::SP_BrowserReload));
    stopReloadButton->setShortcut(QString::fromStdString(parent->m_config->value<std::string>("browser.shortcuts.refresh").value()));
    connect(stopReloadButton, &QToolButton::clicked, this, [this]() {
        if(m_view->isLoaded())
            m_view->reload();
        else
            m_view->stop();
    });

    auto *reloadShortcut = new QShortcut(
        QString::fromStdString(parent->m_config->value<std::string>("browser.shortcuts.reload").value()),
        parent);
    connect(reloadShortcut, &QShortcut::activated, this, [this]() {
        m_view->reload();
    });

    // Home button
    homeButton = new QToolButton(parent);
    homeButton->setIcon(qStyle->standardIcon(QStyle::SP_DirHomeIcon));
    homeButton->setShortcut(QString::fromStdString(parent->m_config->value<std::string>("browser.shortcuts.home").value()));
    connect(homeButton, &QToolButton::clicked, this, [this, parent]() {
        m_view->load(parent->m_profile->homepage());
    });
}

void NavigationBar::addWidgetsTo(QToolBar *toolBar)
{
    toolBar->addWidget(backButton);
    toolBar->addWidget(forwardButton);
    toolBar->addWidget(stopReloadButton);
    toolBar->addWidget(homeButton);
}

void NavigationBar::connectWebView(WebView *view)
{
    Q_CHECK_PTR(view);
    m_view = view;

    disconnect(loadStartedConnection);
    disconnect(loadFinishedConnection);

    if(view->isLoaded()) {
        update_loadFinished();
    } else {
        update_loadStarted();
    }

    loadStartedConnection = connect(view, &QWebEngineView::loadStarted, this, &NavigationBar::update_loadStarted);
    loadFinishedConnection = connect(view, &WebView::loaded, this, &NavigationBar::update_loadFinished);
}

void NavigationBar::update_loadStarted()
{
    backButton->setEnabled(m_view->history()->canGoForward());
    forwardButton->setEnabled(m_view->history()->canGoForward());
    stopReloadButton->setIcon(qStyle->standardIcon(QStyle::SP_BrowserStop));
}

void NavigationBar::update_loadFinished()
{
    backButton->setEnabled(m_view->history()->canGoBack());
    forwardButton->setEnabled(m_view->history()->canGoForward());
    stopReloadButton->setIcon(qStyle->standardIcon(QStyle::SP_BrowserReload));
}