aboutsummaryrefslogtreecommitdiff
path: root/src/mainwindow/window.cpp
blob: 735df4a07e5f98723c52c8cd814b1712581e34ed (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
/*
 * 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 "window.h"
#include "webengine/webprofile.h"
#include "webengine/webview.h"
#include "widgets/tabwidget.h"
#include <QUrl>
#include <QToolButton>
#include <QStyle>
#include <QAction>

Window::Window(const QHash<QString, QString> &config, QWidget *parent, Qt::WindowFlags flags)
    : QMdiSubWindow(parent, flags)
    , tabWidget(new TabWidget(this))
{
    // delete this window when it closes
    setAttribute(Qt::WA_DeleteOnClose, true);

    resize(800, 600);
    setWidget(tabWidget);

    // new tab button
    auto *newTab_button = new QToolButton(this);
    newTab_button->setIcon(style()->standardIcon(QStyle::SP_FileIcon));
    newTab_button->setToolTip(tr("Add tab"));
    newTab_button->setShortcut(QKeySequence(config.value("window.shortcuts.new")));
    connect(newTab_button, &QToolButton::clicked, this, [=]() {
        addTab(WebProfile::defaultProfile()->newtab());
    });
    tabWidget->setCornerWidget(newTab_button, Qt::TopRightCorner);

    // general actions
    auto *closeTab_action = new QAction(this);
    closeTab_action->setShortcut(QKeySequence(config.value("window.shortcuts.close")));
    connect(closeTab_action, &QAction::triggered, this, [=]() {
        tabWidget->deleteTab(tabWidget->currentIndex());
    });
    addAction(closeTab_action);

    auto *leftTab_action = new QAction(this);
    leftTab_action->setShortcut(QKeySequence(config.value("window.shortcuts.left")));
    connect(leftTab_action, &QAction::triggered, this, [=]() {
        tabWidget->setCurrentIndex(qMax(0, tabWidget->currentIndex() - 1));
    });
    addAction(leftTab_action);

    auto *rightTab_action = new QAction(this);
    rightTab_action->setShortcut(QKeySequence(config.value("window.shortcuts.right")));
    connect(rightTab_action, &QAction::triggered, this, [=]() {
        tabWidget->setCurrentIndex(qMin(tabWidget->currentIndex() + 1, tabWidget->count() - 1));
    });
    addAction(rightTab_action);

    connect(tabWidget, &TabWidget::currentChanged, [this](int index) {
        if(index < 0) {
            // last tab has been closed
            close();
        } else {
            auto *view = dynamic_cast<WebView *>(tabWidget->widget(index));
            Q_CHECK_PTR(view);

            disconnect(titleConnection);
            disconnect(linkHoveredConnection);

            connect(view, &WebView::titleChanged, this, &Window::setWindowTitle);
            setWindowTitle(view->title());

            connect(view->page(), &WebPage::linkHovered, this, [this](const QString &url) {
                if(!url.isEmpty())
                    emit showStatusMessage(url, 3000);
            });

            emit currentViewChanged(view);
        }
    });
}

Window::~Window()
{
    delete tabWidget;
}

WebView *Window::currentView()
{
    return qobject_cast<WebView *>(tabWidget->currentWidget());
}

int Window::addTab(WebView *view)
{
    Q_CHECK_PTR(view);
    return tabWidget->addTab(view);
}

int Window::addTab(const QUrl &url)
{
    auto *view = new WebView(WebProfile::defaultProfile(), this);
    view->load(url);
    return tabWidget->addTab(view);
}

void Window::swapToTab(int index)
{
    tabWidget->setCurrentIndex(index);
}