aboutsummaryrefslogtreecommitdiff
path: root/src/mainwindow/mainwindow.cpp
blob: 0d4a9780274e707685cd76c9222b3e25b6272a16 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
 * 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 "mainwindow.h"
#include "addressbar.h"
#include "bookmarks/bookmarkstoolbar.h"
#include "bookmarks/bookmarkswidget.h"
#include "browser.h"
#include "configuration.h"
#include "menubar.h"
#include "webprofile.h"
#include "webprofilemanager.h"
#include "webview.h"
#include "widgets/dockwidget.h"
#include "widgets/navigationbar.h"
#include "widgets/searchform.h"
#include <QApplication>
#include <QCloseEvent>
#include <QMessageBox>
#include <QStatusBar>

MainWindow::MainWindow(const Session::MainWindow &mainwindow_data, QMenu *appMenu, QWidget *parent)
    : QMainWindow(parent)
{
    Configuration config;

    // create UI
    defaultWindowTitle = config.value<QString>("mainwindow.title").value();
    setWindowTitle(defaultWindowTitle);
    resize(config.value<int>("mainwindow.width").value(), config.value<int>("mainwindow.height").value());
    if(config.value<bool>("mainwindow.maximized").value_or(false)) {
        setWindowState(Qt::WindowMaximized);
    }
    show();

    navigationToolBar = new NavigationBar(this);
    navigationToolBar->setMovable(config.value<bool>("navigation.movable").value_or(false));
    addToolBar(Qt::TopToolBarArea, navigationToolBar);
    navigationToolBar->connectWebView(nullptr);

    addressBar = new AddressBar(this);
    navigationToolBar->addWidget(addressBar);

    auto *app = qobject_cast<Browser *>(qApp);
    this->addToolBarBreak();
    this->addToolBar(new BookmarksToolbar(app->bookmarks()->model(), this));

    m_menuBar = new MenuBar(appMenu, this);
    setMenuBar(m_menuBar);

    // status bar
    searchBox = new SearchForm(this);
    statusBar()->addPermanentWidget(searchBox);
    searchBox->setVisible(false);

    // search box
    auto *searchAction = new QAction(this);
    config.shortcut<QAction>(*searchAction, "shortcuts.window.search");
    connect(searchAction, &QAction::triggered, this, [this]() {
        searchBox->setVisible(!searchBox->isVisible());
    });
    QMainWindow::addAction(searchAction);

    for(const auto &s : mainwindow_data.subwindows) {
        createSubWindow(s);
    }
    if(m_subwindows.isEmpty()) {
        const Session::SubWindow s;
        createSubWindow(s);
    }
}

void MainWindow::addDockWidget(Qt::DockWidgetArea area, QWidget *widget)
{
    QDockWidget *lastDock = nullptr;
    const auto docks = findChildren<QDockWidget *>();
    for(QDockWidget *dock : docks) {
        if(dockWidgetArea(dock) == area) {
            lastDock = dock;
        }
    }

    auto *dock = new DockWidget(widget->windowTitle(), this);
    dock->setMinimumWidth(460);
    dock->setWidget(widget);

    if(lastDock == nullptr) {
        QMainWindow::addDockWidget(area, dock);
    } else {
        tabifyDockWidget(lastDock, dock);
    }
}

void MainWindow::removeDockWidget(QWidget *widget)
{
    const auto docks = this->findChildren<QDockWidget *>();
    for(QDockWidget *dock : docks) {
        if(dock->widget() == widget) {
            dock->widget()->setParent(nullptr);
            dock->close();
        }
    }
}

void MainWindow::createTab(const QUrl &url)
{
    auto *w = qobject_cast<SubWindow *>(centralWidget());
    if(w != nullptr) {
        w->addTab(url);
    }
}

void MainWindow::createTab(const Session::WebView &tab_data)
{
    auto *w = qobject_cast<SubWindow *>(centralWidget());
    if(w != nullptr) {
        w->addTab(tab_data);
    }
}

SubWindow *MainWindow::createSubWindow(const Session::SubWindow &subwindow_data)
{
    WebProfileManager profileManager;

    auto *profile = profileManager.profile(subwindow_data.profile);
    if(profile == nullptr) {
        profile = WebProfile::defaultProfile();
    }
    auto *w = new SubWindow(this);
    w->setProfile(profile);
    m_subwindows.append(w);
    setCurrentSubWindow(w);
    m_menuBar->insertSubWindow(w);

    connect(w, &SubWindow::windowTitleChanged, this, [this, w](const QString &title) {
        if(w == currentSubWindow()) {
            setWindowTitle(QString("[%1] - %2").arg(title, defaultWindowTitle));
        }
    });

    connect(w, &SubWindow::aboutToClose, this, [this, w]() {
        m_subwindows.removeAll(w);
        if(m_subwindows.isEmpty()) {
            close();
        } else {
            setCurrentSubWindow(m_subwindows.last());
            w->deleteLater();
        }
    });

    if(subwindow_data.tabs.count() == 0) {
        w->addTab(profile->newtab());
        return w;
    }
    for(const auto &tab : subwindow_data.tabs) {
        w->addTab(tab);
    }
    return w;
}

void MainWindow::setCurrentSubWindow(SubWindow *subwindow)
{
    auto *previous = centralWidget();
    if(previous != nullptr) {
        previous->setParent(nullptr);
        previous->hide();
    }
    setCentralWidget(subwindow);
    subwindow->show();
    subwindow->setFocus();
    setWindowTitle(QString("[%1] - %2").arg(subwindow->windowTitle(), defaultWindowTitle));

    // connect signlas
    disconnect(viewChangedConnection);
    disconnect(statusBarConnection);

    setView(subwindow->currentView());
    viewChangedConnection = connect(subwindow, &SubWindow::currentViewChanged, this, &MainWindow::setView);
    statusBarConnection = connect(subwindow, &SubWindow::showStatusMessage, statusBar(), &QStatusBar::showMessage);
}

void MainWindow::setView(WebView *view)
{
    addressBar->connectView(view);
    navigationToolBar->connectWebView(view);
    searchBox->setView(view);
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    if(m_subwindows.count() > 1) {
        const int choice = QMessageBox::question(this, tr("Close multiple subwindows?"), tr("Do you want to close all subwindows?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
        if(choice == QMessageBox::No) {
            event->ignore();
            return;
        }
    }
    event->accept();
}