aboutsummaryrefslogtreecommitdiff
path: root/src/mainwindow/widgets/navigationbar.cpp
blob: 648bb23977cec67566fb6fb39adff84c77c7f71a (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
/*
 * 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: git://neueland.iserlohn-fortress.net/smolbote.git
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "navigationbar.h"
#include "mainwindow/mainwindow.h"
#include "webengine/webview.h"
#include <QHBoxLayout>
#include <QStyle>
#include <QToolBar>
#include <QToolButton>
#include <QWebEngineHistory>

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

    backButton = new QToolButton(parent);
    backButton->setIcon(qStyle->standardIcon(QStyle::SP_ArrowBack));
    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);
            });
        }
    });

    forwardButton = new QToolButton(parent);
    forwardButton->setIcon(qStyle->standardIcon(QStyle::SP_ArrowForward));
    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);
            });
        }
    });

    stopReloadButton = new QToolButton(parent);
    stopReloadButton->setIcon(qStyle->standardIcon(QStyle::SP_BrowserReload));
    connect(stopReloadButton, &QToolButton::clicked, this, [this]() {
        if(m_view->isLoaded())
            m_view->reload();
        else
            m_view->stop();
    });

    homeButton = new QToolButton(parent);
    homeButton->setIcon(qStyle->standardIcon(QStyle::SP_DirHomeIcon));
    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, &QWebEngineView::loadFinished, 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));
}