/* * 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 "mainwindow.h" #include "webengine/webview.h" #include "widgets/navigationbar.h" #include "window.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include "addressbar/addressbar.h" MainWindow::MainWindow(std::shared_ptr &config, QWidget *parent) : QMainWindow(parent) , mdiArea(new QMdiArea(this)) { Q_ASSERT(config); m_config = config; // create UI resize(config->value("mainwindow.width").value(), config->value("mainwindow.height").value()); titleSuffix = QString::fromStdString(config->value("mainwindow.title").value()); setWindowTitle(tr("smolbote")); if(config->value("mainwindow.maximized").value()) showMaximized(); else show(); createMenuBar(); auto *navigationToolBar = new NavigationBar(config->section("navigation"), this); navigationToolBar->setMovable(config->value("navigation.movable").value()); addToolBar(Qt::TopToolBarArea, navigationToolBar); navigationToolBar->connectWebView(nullptr); addressBar = new AddressBar(config->section("addressbar"), this); navigationToolBar->addWidget(addressBar); setCentralWidget(mdiArea); mdiArea->setFocus(); connect(mdiArea, &QMdiArea::subWindowActivated, this, [this, navigationToolBar](QMdiSubWindow *window) { disconnect(titleChangedConnection); disconnect(addressBarConnection); disconnect(navigationBarConnection); auto *w = qobject_cast(window); if(w == nullptr) { // no current subwindow, clear everything setWindowTitle(tr("smolbote")); addressBar->connectWebView(nullptr); navigationToolBar->connectWebView(nullptr); } else { setWindowTitle(w->windowTitle() + titleSuffix); titleChangedConnection = connect(w, &Window::windowTitleChanged, this, [this](const QString &title) { this->setWindowTitle(title + titleSuffix); }); addressBar->connectWebView(w->currentView()); addressBarConnection = connect(w, &Window::currentViewChanged, addressBar, &AddressBar::connectWebView); navigationToolBar->connectWebView(w->currentView()); navigationBarConnection = connect(w, &Window::currentViewChanged, navigationToolBar, &NavigationBar::connectWebView); } }); auto *tileShortcut = new QShortcut(QKeySequence(config->value("mainwindow.shortcuts.tileWindows").value().c_str()), this); connect(tileShortcut, &QShortcut::activated, this, [this]() { mdiArea->tileSubWindows(); }); } MainWindow::~MainWindow() { disconnect(titleChangedConnection); disconnect(addressBarConnection); disconnect(navigationBarConnection); } void MainWindow::createMenuBar() { auto *smolboteMenu = menuBar()->addMenu(qApp->applicationDisplayName()); smolboteMenu->addAction(tr("New tab"), this, [this]() { createTab(QUrl::fromUserInput("about:blank")); }, QKeySequence(m_config->value("mainwindow.shortcuts.newTab").value().c_str())); smolboteMenu->addAction(tr("New tab group"), this, [this]() { createSubWindow(QUrl::fromUserInput("about:blank")); }, QKeySequence(m_config->value("mainwindow.shortcuts.newGroup").value().c_str())); smolboteMenu->addAction(tr("New window"))->setEnabled(false); smolboteMenu->addSeparator(); smolboteMenu->addAction(tr("About"), this, [this]() { auto *dlg = new AboutDialog(this); dlg->exec(); }, QKeySequence(m_config->value("mainwindow.shortcuts.about").value().c_str())); smolboteMenu->addAction(tr("About Qt"), qApp, &QApplication::aboutQt); smolboteMenu->addSeparator(); smolboteMenu->addAction(tr("Quit"), qApp, &QApplication::quit, QKeySequence(m_config->value("mainwindow.shortcuts.quit").value().c_str())); toolsMenu = menuBar()->addMenu(tr("Tools")); } void MainWindow::addAction(ActionLocation where, QAction *action) { switch(where) { case ToolsMenu: toolsMenu->addAction(action); break; default: QMainWindow::addAction(action); break; } } void MainWindow::addDockWidget(Qt::DockWidgetArea area, QWidget *widget) { QDockWidget *dock = new QDockWidget(widget->windowTitle(), this); dock->setAttribute(Qt::WA_DeleteOnClose, true); dock->setWidget(widget); connect(dock, &QDockWidget::visibilityChanged, [dock](bool visible) { if(!visible && dock->widget()) { dock->widget()->setParent(nullptr); } }); QMainWindow::addDockWidget(area, dock); } void MainWindow::createTab(const QUrl &url) { auto *w = qobject_cast(mdiArea->currentSubWindow()); if(w == nullptr) { w = createSubWindow(url); } else { w->addTab(url); } } Window *MainWindow::createSubWindow(const QUrl &url) { auto *w = new Window(this); mdiArea->addSubWindow(w); w->showMaximized(); w->setFocus(); w->addTab(url); return w; } void MainWindow::closeEvent(QCloseEvent *event) { if(mdiArea->subWindowList().count() > 1) { 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; } } mdiArea->closeAllSubWindows(); if(mdiArea->currentSubWindow()) event->ignore(); else event->accept(); }