aboutsummaryrefslogtreecommitdiff
path: root/src/mainwindow.cpp
blob: 5c3048c1803a1bdff912fa63582a56ecb6de5956 (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * 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 "mainwindow.h"
#include "ui_mainwindow.h"
#include "widgets/mainwindowmenubar.h"
#include <QMessageBox>
#include "forms/aboutdialog.h"

#include <QToolButton>
#include <QStyle>

#include <QWebEngineHistory>

#include <QDockWidget>

#include <settings/configuration.h>

#include <bookmarks/bookmarkswidget.h>
#include <downloads/downloadswidget.h>

#include "browser.h"
#include <settings/settingsdialog.h>

#include <navigation/urllineedit.h>

#include "forms/searchform.h"

MainWindow::MainWindow(std::shared_ptr<Configuration> config, QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    tabBar(new MainWindowTabBar(config, this)),
    menuBar(new MainWindowMenuBar(config, this)),
    m_addressBar(new UrlLineEdit(this)),
    m_progressBar(new LoadingBar(this))
{
    Q_ASSERT(config);
    m_config = config;

    // delete this window when it closes
    setAttribute(Qt::WA_DeleteOnClose, true);

    // set up UI
    ui->setupUi(this);

    QAction *fullscreenAction = new QAction(this);
    fullscreenAction->setShortcut(QKeySequence(QString::fromStdString(m_config->value<std::string>("browser.shortcuts.fullscreen").value())));
    connect(fullscreenAction, &QAction::triggered, this, &MainWindow::toggleFullscreen);
    addAction(fullscreenAction);

    // Dockable widget styling
    setDockOptions(dockOptions() | AllowTabbedDocks | ForceTabbedDocks);
    setTabPosition(Qt::LeftDockWidgetArea, QTabWidget::North);
    setTabPosition(Qt::RightDockWidgetArea, QTabWidget::North);

    // Add the toolbars
    // tabToolBar: main menu and tab list
    ui->mainToolBar->setMovable(m_config->value<bool>("browser.ui.tabtoolbarMovable").value());
    ui->mainToolBar->addWidget(menuBar);
    //tabToolBar->addWidget(tabBar);

    // navigationToolBar: address bar
    ui->navigationToolBar->setMovable(m_config->value<bool>("browser.ui.navtoolbarMovable").value());
    insertToolBarBreak(ui->navigationToolBar);

    // page actions
    m_backButton = new NavigationButton(NavigationButton::BackButton, this);

    m_forwardButton = new NavigationButton(NavigationButton::ForwardButton, this);

    m_reloadButton = new NavigationButton(NavigationButton::ReloadButton, this);

    QToolButton *homepageButton = new QToolButton(this);
    homepageButton->setIcon(style()->standardIcon(QStyle::SP_DirHomeIcon));
    connect(homepageButton, &QToolButton::clicked, this, [&]() {
        tabBar->currentView()->load(m_profile->homepage());
    });

    ui->navigationToolBar->addWidget(m_backButton);
    ui->navigationToolBar->addWidget(m_forwardButton);
    ui->navigationToolBar->addWidget(m_reloadButton);
    ui->navigationToolBar->addWidget(homepageButton);
    ui->navigationToolBar->addWidget(m_addressBar);

    // connect signals
    connect(m_addressBar, &UrlLineEdit::addressEntered, this, [&](const QUrl &url) {
        tabBar->currentView()->load(url);
    });
    connect(m_addressBar, &UrlLineEdit::searchTermEntered, this, [&](const QString &term) {
        QString t = term;
        t.replace(' ', '+');
        QString url = QString::fromStdString(m_config->value<std::string>("profile.search").value());
        url.replace("$term", t);
        tabBar->currentView()->load(QUrl::fromUserInput(url));
    });
    connect(tabBar, SIGNAL(currentTabChanged(WebView*)), this, SLOT(handleTabChanged(WebView*)));

    // loading bar
    ui->statusBar->addPermanentWidget(m_progressBar);

    // search box
    m_searchBox = new SearchForm(this);
    ui->statusBar->addWidget(m_searchBox);
    m_searchBox->setVisible(false);

    // shortcuts
    QAction *focusAddressAction = new QAction(this);
    focusAddressAction->setShortcut(QKeySequence(QString::fromStdString(m_config->value<std::string>("browser.shortcuts.focusAddress").value())));
    //focusAddressAction->setShortcut(QKeySequence::fromString(browser->settings()->value("window.shortcuts.focusAddress").toString()));
    //connect(focusAddressAction, SIGNAL(triggered(bool)), this, SLOT(focusAddress()));
    connect(focusAddressAction, &QAction::triggered, this, [this]() {
        m_addressBar->setFocus();
        m_addressBar->selectAll();
    });
    addAction(focusAddressAction);

    resize(m_config->value<int>("browser.window.width").value(), m_config->value<int>("browser.window.height").value());
    if(m_config->value<bool>("browser.window.maximized").value()) {
        showMaximized();
    }
}

MainWindow::~MainWindow()
{
    // Release all dock widgets before deleting so we don't accidentally delete them
    // Also fixes that annoying crash when closing
    QList<QDockWidget*> allDockWidgets = findChildren<QDockWidget*>();
    for(QDockWidget *w : allDockWidgets) {
        if(w->widget()) {
            w->widget()->setParent(nullptr);
        }
    }

    delete ui;
}

void MainWindow::addTabbedDock(Qt::DockWidgetArea area, QWidget *widget)
{
    // get all dock widgets
    QList<QDockWidget*> allDockWidgets = findChildren<QDockWidget*>();

    // make a list of widgets in the area we want
    QVector<QDockWidget*> areaDockWidgets;
    for(QDockWidget *w : allDockWidgets) {
        if(dockWidgetArea(w) == area) {
            areaDockWidgets.append(w);
        }
    }

    // create a dock widget
    QDockWidget *dock = new QDockWidget(widget->windowTitle(), this);

    // release the held widget when deleted
    connect(dock, &QDockWidget::destroyed, this, [dock]() {
        dock->widget()->setParent(nullptr);
    });
    dock->setWidget(widget);

    if(areaDockWidgets.empty()) {
        // no other widgets
        addDockWidget(area, dock);
    } else {
        // there are other widgets, so put it after the last one
        tabifyDockWidget(areaDockWidgets.last(), dock);
    }
}

void MainWindow::newTab(const QUrl &url)
{
    if(!m_tabBarAdded) {
        m_tabBarAdded = true;
        ui->mainToolBar->addWidget(tabBar);
    }
    tabBar->addTab(createWebView(url, m_profile.get(), this));
}

MainWindow *MainWindow::newWindow(const QUrl &url)
{
    Browser *instance = static_cast<Browser*>(qApp->instance());
    MainWindow *window = instance->createWindow();
    window->setProfile(m_profile);
    if(!url.isEmpty()) {
        window->newTab(url);
    }
    return window;
}

void MainWindow::focusAddress()
{
    m_addressBar->setFocus();
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    if(tabBar->count() > 1) {
        int ret = QMessageBox::warning(this, tr("Close window?"), tr("Close multiple tabs?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
        if(ret == QMessageBox::No) {
            event->ignore();
            return;
        }
    }
    QMainWindow::closeEvent(event);
}

void MainWindow::about()
{
    AboutDialog *dlg = new AboutDialog(this);
    dlg->exec();
}

void MainWindow::showSettingsDialog()
{
    SettingsDialog *dlg = new SettingsDialog(m_config, this);
    dlg->exec();
}

void MainWindow::setProfile(std::shared_ptr<WebEngineProfile> profile)
{
    Q_ASSERT(profile);
    m_profile = profile;
    tabBar->setProfile(profile.get());
    menuBar->setProfileName(profile->name());
}

WebEngineProfile *MainWindow::profile()
{
    Q_ASSERT(m_profile);
    return m_profile.get();
}

void MainWindow::setBookmarksWidget(std::shared_ptr<BookmarksWidget> &widget)
{
    Q_ASSERT(widget);
    m_bookmarksWidget = widget;
    connect(menuBar->bookmarksAction(), &QAction::triggered, this, [this]() {
        addTabbedDock(Qt::RightDockWidgetArea, m_bookmarksWidget.get());
    });
    connect(m_bookmarksWidget.get(), &BookmarksWidget::openUrl, this, [this](const QUrl &url) {
        if(isActiveWindow()) {
            newTab(url);
        }
    });
}

void MainWindow::setDownloadsWidget(std::shared_ptr<DownloadsWidget> &widget)
{
    Q_ASSERT(widget);
    m_downloadsWidget = widget;
    connect(menuBar->downloadsAction(), &QAction::triggered, this, [this]() {
        addTabbedDock(Qt::RightDockWidgetArea, m_downloadsWidget.get());
    });
}

void MainWindow::toggleFullscreen()
{
    if(isFullScreen()) {
        setWindowState(Qt::WindowMaximized | Qt::WindowActive);
    } else {
        setWindowState(Qt::WindowFullScreen | Qt::WindowActive);
    }
}

void MainWindow::handleTabChanged(WebView *view)
{
    Q_CHECK_PTR(view);

    m_currentView = view;

    // centralWidget can be a nullptr
    if(centralWidget()) {
        // clear the parent of the central widget so it doesn't get deleted
        centralWidget()->setParent(nullptr);

        // disconnect signals
        disconnect(centralWidget());
    }

    // set new central widget
    setCentralWidget(view);

    // connect signals
    m_backButton->setView(view);
    m_forwardButton->setView(view);
    m_reloadButton->setView(view);

    connect(view, &WebView::urlChanged, m_addressBar, &UrlLineEdit::setUrl);
    m_addressBar->setUrl(view->url());
    m_addressBar->pageAction()->setMenu(view->pageMenu());

    connect(view, &WebView::titleChanged, this, &MainWindow::handleTitleUpdated);

    m_progressBar->connectWebView(view);

    // update UI
    this->handleTitleUpdated(view->title());
    centralWidget()->setFocus();
}

void MainWindow::handleTitleUpdated(const QString &title)
{
    QString t = QString::fromStdString(m_config->value<std::string>("browser.window.title").value());
    t.replace("title", title);
    t.replace("profile", m_profile->name());
    setWindowTitle(t);
    //setWindowTitle(browser->settings()->value("window.title").toString().replace("title", title).replace("profile", tabBar->profile()->name()));
}