aboutsummaryrefslogtreecommitdiff
path: root/src/mainwindow/window.cpp
blob: 67397fe9f3368523f55a554a8f7c4d423821f4ed (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
/*
 * 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>

Window::Window(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);

    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);
            connect(view, &WebView::titleChanged, this, &Window::setWindowTitle);
            setWindowTitle(view->title());

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