aboutsummaryrefslogtreecommitdiff
path: root/src/subwindow/subwindow.cpp
blob: 58acee6837ea44a9d5d12bd9243e02caba6a9400 (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
132
133
134
135
136
137
138
139
/*
 * 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/gitea/aqua/smolbote
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "subwindow.h"
#include "browser.h"
#include "configuration.h"
#include "webengine/webprofile.h"
#include "webengine/webview.h"
#include <QAction>
#include <QCloseEvent>
#include <QHideEvent>
#include <QMenu>
#include <QShortcut>
#include <QShowEvent>
#include <QTabBar>
#include <QToolButton>
#include <QVBoxLayout>

SubWindow::SubWindow(QWidget *parent, Qt::WindowFlags flags)
    : QWidget(parent, flags)
    , tabWidget(new TabWidget(this))
{
    // delete this window when it closes
    //setAttribute(Qt::WA_DeleteOnClose, true);

    auto *layout = new QVBoxLayout;
    layout->setSpacing(0);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->addWidget(tabWidget);
    setLayout(layout);

    m_profile = WebProfile::defaultProfile();

    Configuration config;

    QKeySequence fullscreen_shortcut;
    config.shortcut<QKeySequence>(fullscreen_shortcut, "shortcuts.subwindow.fullscreen");
    auto *fullScreen_shortcut = new QShortcut(fullscreen_shortcut, this);
    connect(fullScreen_shortcut, &QShortcut::activated, this, [this]() {
        auto *w = this->window();
        if(w->isFullScreen())
            w->showNormal();
        else
            w->showFullScreen();
    });

    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);

            titleConnection = connect(view, &WebView::titleChanged, this, [this](const QString &title) {
                auto *v = qobject_cast<WebView *>(sender());
                this->setWindowTitle(QString("%1 :%2").arg(title, v->profile()->name()));
            });
            setWindowTitle(QString("%1 :%2").arg(view->title(), view->profile()->name()));

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

            emit currentViewChanged(view);
        }
    });
}

void SubWindow::setProfile(WebProfile *profile)
{
    if(profile == nullptr) {
        setProfile(WebProfile::defaultProfile());
        return;
    }

    this->m_profile = profile;
    for(int i = 0; i < tabWidget->count(); ++i) {
        auto *view = qobject_cast<WebView *>(tabWidget->widget(i));
        view->setProfile(profile);
    }
}

void SubWindow::setTabData(TabData &data, int index)
{
    tabWidget->tabBar()->setTabData(index, QVariant::fromValue<TabData>(data));
}

SubWindow::TabData SubWindow::tabData(int index) const
{
    return tabWidget->tabBar()->tabData(index).value<TabData>();
}

int SubWindow::addTab(const QUrl &url, WebProfile *profile)
{
    Q_CHECK_PTR(m_profile);

    auto *_profile = (profile == nullptr) ? m_profile : profile;

    auto *view = new WebView(_profile, this);
    if(url.isEmpty())
        view->load(_profile->newtab());
    else
        view->load(url);

    return tabWidget->addTab(view);
}

void SubWindow::moveTab(int from, int to)
{
    tabWidget->tabBar()->moveTab(from, to);
}

void SubWindow::closeEvent(QCloseEvent *event)
{
    emit aboutToClose();
    event->accept();
}

void SubWindow::hideEvent(QHideEvent *event)
{
    emit visibilityChanged(false);
    event->accept();
}

void SubWindow::showEvent(QShowEvent *event)
{
    emit visibilityChanged(true);
    event->accept();
}