aboutsummaryrefslogtreecommitdiff
path: root/src/mainwindow/widgets/navigationbar.cpp
blob: 98a79328e5e1eaefd18c811b65ccab3227553040 (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
/*
 * 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 "addressbar/urllineedit.h"
#include "configuration/configuration.h"
#include "webengine/webview.h"
#include <QHBoxLayout>
#include <QMenu>
#include <QShortcut>
#include <QStyle>
#include <QToolBar>
#include <QToolButton>
#include <QWebEngineHistory>

NavigationBar::NavigationBar(const QHash<QString, QString> &conf, QWidget *parent)
    : QToolBar(parent)
{
    qStyle = parent->style();

    // Back button
    backAction = addAction(qStyle->standardIcon(QStyle::SP_ArrowBack), tr("Back"));
    backAction->setShortcut(QKeySequence(conf.value("navigation.shortcuts.back")));
    connect(backAction, &QAction::triggered, this, [this]() {
        m_view->history()->back();
    });

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

    // Forward button
    forwardAction = addAction(qStyle->standardIcon(QStyle::SP_ArrowForward), tr("Forward"));
    forwardAction->setShortcut(QKeySequence(conf.value("navigation.shortcuts.forward")));
    connect(forwardAction, &QAction::triggered, this, [this]() {
        m_view->history()->forward();
    });

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

    // Stop/Refresh button
    stopReloadAction = addAction(qStyle->standardIcon(QStyle::SP_BrowserReload), tr("Reload"));
    stopReloadAction->setShortcut(QKeySequence(conf.value("navigation.shortcuts.refresh")));
    connect(stopReloadAction, &QAction::triggered, this, [this]() {
        if(m_view->isLoaded())
            m_view->reload();
        else
            m_view->stop();
    });

    // Home button
    homeAction = addAction(qStyle->standardIcon(QStyle::SP_DirHomeIcon), tr("Home"));
    homeAction->setShortcut(QKeySequence(conf.value("navigation.shortcuts.home")));
    connect(homeAction, &QAction::triggered, this, [this]() {
        m_view->triggerViewAction(WebView::GoHome);
    });
}

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

    disconnect(loadStartedConnection);
    disconnect(loadFinishedConnection);

    if(view == nullptr) {
        backAction->setEnabled(false);
        forwardAction->setEnabled(false);
        stopReloadAction->setEnabled(false);
        homeAction->setEnabled(false);

        return;
    }

    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);
    stopReloadAction->setEnabled(true);
    homeAction->setEnabled(true);
}

void NavigationBar::update_loadStarted()
{
    backAction->setEnabled(m_view->history()->canGoForward());
    forwardAction->setEnabled(m_view->history()->canGoForward());
    stopReloadAction->setIcon(qStyle->standardIcon(QStyle::SP_BrowserStop));
}

void NavigationBar::update_loadFinished()
{
    backAction->setEnabled(m_view->history()->canGoBack());
    forwardAction->setEnabled(m_view->history()->canGoForward());
    stopReloadAction->setIcon(qStyle->standardIcon(QStyle::SP_BrowserReload));
}