aboutsummaryrefslogtreecommitdiff
path: root/src/mainwindow/mainwindow.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mainwindow/mainwindow.cpp')
-rw-r--r--src/mainwindow/mainwindow.cpp190
1 files changed, 42 insertions, 148 deletions
diff --git a/src/mainwindow/mainwindow.cpp b/src/mainwindow/mainwindow.cpp
index 36f2759..b607a63 100644
--- a/src/mainwindow/mainwindow.cpp
+++ b/src/mainwindow/mainwindow.cpp
@@ -13,61 +13,31 @@
#include "browser.h"
#include "configuration.h"
#include "menubar.h"
-#include "session/session.h"
-#include "session/sessiondialog.h"
-#include "subwindow/subwindow.h"
#include "webengine/webprofile.h"
-#include "webengine/webprofilemanager.h"
#include "webengine/webview.h"
#include "widgets/dockwidget.h"
#include "widgets/navigationbar.h"
#include "widgets/searchform.h"
#include <QApplication>
#include <QCloseEvent>
-#include <QFileDialog>
-#include <QJsonArray>
-#include <QJsonObject>
-#include <QLineEdit>
-#include <QMdiArea>
-#include <QMdiSubWindow>
-#include <QMenuBar>
#include <QMessageBox>
-#include <QShortcut>
#include <QStatusBar>
-#include <QToolBar>
-#include <QUrl>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
- , mdiArea(new QMdiArea(this))
{
Configuration config;
// create UI
- setWindowTitle(config.value<QString>("mainwindow.title").value());
+ 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();
- // current subwindow shortcut
- {
- QAction *subwindowMenuAction = new QAction(this);
- QMainWindow::addAction(subwindowMenuAction);
- config.shortcut<QAction>(*subwindowMenuAction, "shortcuts.subwindow.menu");
- connect(subwindowMenuAction, &QAction::triggered, this, [this]() {
- QMdiSubWindow *window = mdiArea->currentSubWindow();
- if(window != nullptr) {
- // show the menu at the subwindow position
- // position has to be global, and mapped by the mdiArea (parentWidget() of the subwindow)
- const auto position = mdiArea->mapToGlobal(window->pos());
- window->systemMenu()->exec(position);
- }
- });
- }
-
navigationToolBar = new NavigationBar(this);
navigationToolBar->setMovable(config.value<bool>("navigation.movable").value_or(false));
addToolBar(Qt::TopToolBarArea, navigationToolBar);
@@ -83,80 +53,20 @@ MainWindow::MainWindow(QWidget *parent)
m_menuBar = new MenuBar(this);
this->setMenuBar(m_menuBar);
- mdiArea->setBackground(Qt::NoBrush);
- setCentralWidget(mdiArea);
- mdiArea->setFocus();
-
// status bar
searchBox = new SearchForm(this);
statusBar()->addPermanentWidget(searchBox);
searchBox->setVisible(false);
- // connect signlas
- connect(mdiArea, &QMdiArea::subWindowActivated, this, [this](QMdiSubWindow *window) {
- disconnect(viewChangedConnection);
- disconnect(searchBoxConnection);
- disconnect(statusBarConnection);
-
- auto *w = qobject_cast<SubWindow *>(window);
- if(w == nullptr) {
- // no current subwindow, clear everything
- setView(nullptr);
- } else {
- setView(w->currentView());
- viewChangedConnection = connect(w, &SubWindow::currentViewChanged, this, &MainWindow::setView);
- statusBarConnection = connect(w, &SubWindow::showStatusMessage, statusBar(), &QStatusBar::showMessage);
- }
- });
-
- // address bar
- connect(addressBar, &AddressBar::search, this, [this](const QString &term) {
- if(this->currentView != nullptr) {
- currentView->search(term);
- currentView->setFocus();
- }
- });
- connect(addressBar, &AddressBar::load, this, [this](const QUrl &url) {
- if(this->currentView != nullptr) {
- currentView->load(url);
- currentView->setFocus();
- }
- });
-
- connect(addressBar, &AddressBar::giveFocus, this, [this]() {
- if(this->currentView != nullptr) {
- currentView->setFocus();
- }
- });
-
// search box
auto *searchAction = new QAction(this);
config.shortcut<QAction>(*searchAction, "shortcuts.window.search");
connect(searchAction, &QAction::triggered, this, [this]() {
- /* QTBUG-18665
- * When focusing out of the search box and hiding it, the first
- * (or earlier?) subwindow gets activated for some reason.
- */
- if(searchBox->isVisible()) {
- auto *w = mdiArea->currentSubWindow();
- searchBox->hide();
- mdiArea->setActiveSubWindow(w);
- } else {
- searchBox->show();
- }
+ searchBox->setVisible(!searchBox->isVisible());
});
QMainWindow::addAction(searchAction);
}
-MainWindow::~MainWindow()
-{
- disconnect(viewChangedConnection);
- disconnect(searchBoxConnection);
- disconnect(statusBarConnection);
-
- disconnect(addressBar);
-}
-
void MainWindow::addDockWidget(Qt::DockWidgetArea area, QWidget *widget)
{
QDockWidget *lastDock = nullptr;
@@ -189,49 +99,34 @@ void MainWindow::removeDockWidget(QWidget *widget)
void MainWindow::createTab(const QUrl &url)
{
- auto *w = qobject_cast<SubWindow *>(mdiArea->currentSubWindow());
+ auto *w = qobject_cast<SubWindow *>(centralWidget());
if(w != nullptr) {
w->addTab(url);
}
}
-const QVector<SubWindow *> MainWindow::subWindows() const
-{
- QVector<SubWindow *> list;
- const auto subwindows = mdiArea->subWindowList();
- for(auto *w : subwindows) {
- auto *subwindow = qobject_cast<SubWindow *>(w);
- if(subwindow != nullptr)
- list.append(subwindow);
- }
-
- return list;
-}
-
-SubWindow *MainWindow::currentSubWindow() const
-{
- return qobject_cast<SubWindow *>(mdiArea->currentSubWindow());
-}
-
SubWindow *MainWindow::createSubWindow(WebProfile *profile, bool openProfileNewtab)
{
- bool shouldMaximize = true;
- // if there is a current window, use its maximize state
- if(auto *currentWindow = qobject_cast<SubWindow *>(mdiArea->currentSubWindow()); currentWindow != nullptr) {
- shouldMaximize = currentWindow->isMaximized();
- }
-
auto *w = new SubWindow(this);
+ w->setProfile(profile);
+ m_subwindows.append(w);
+ setCurrentSubWindow(w);
m_menuBar->insertSubWindow(w);
- w->setProfile(profile);
- mdiArea->addSubWindow(w);
- if(shouldMaximize)
- w->showMaximized();
- else
- w->show();
+ connect(w, &SubWindow::windowTitleChanged, this, [this, w](const QString &title) {
+ if(w == currentSubWindow())
+ setWindowTitle(QString("[%1] - %2").arg(title, defaultWindowTitle));
+ });
- w->setFocus();
+ connect(w, &SubWindow::aboutToClose, this, [this, w]() {
+ m_subwindows.removeAll(w);
+ if(m_subwindows.isEmpty()) {
+ close();
+ } else {
+ setCurrentSubWindow(m_subwindows.last());
+ w->deleteLater();
+ }
+ });
if(openProfileNewtab)
w->addTab(w->profile()->newtab());
@@ -239,43 +134,42 @@ SubWindow *MainWindow::createSubWindow(WebProfile *profile, bool openProfileNewt
return w;
}
-void MainWindow::setView(WebView *view)
+void MainWindow::setCurrentSubWindow(SubWindow *subwindow)
{
- if(currentView != nullptr) {
- // disconnect old view
- disconnect(currentView, nullptr, addressBar, nullptr);
+ auto *previous = centralWidget();
+ if(previous != nullptr) {
+ previous->setParent(nullptr);
+ previous->hide();
}
- currentView = view;
-
- if(view != nullptr) {
- connect(view, &WebView::urlChanged, addressBar, &AddressBar::setUrl);
- addressBar->setUrl(view->url());
+ setCentralWidget(subwindow);
+ subwindow->show();
+ subwindow->setFocus();
+ setWindowTitle(QString("[%1] - %2").arg(subwindow->windowTitle(), defaultWindowTitle));
- connect(view, &WebView::loadProgress, addressBar, &AddressBar::setProgress);
- addressBar->setProgress(100);
+ // connect signlas
+ disconnect(viewChangedConnection);
+ disconnect(statusBarConnection);
- } else {
- addressBar->setUrl(QUrl());
- addressBar->setProgress(100);
- }
+ 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(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(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;
}
}
-
- mdiArea->closeAllSubWindows();
- if(mdiArea->currentSubWindow() != nullptr)
- event->ignore();
- else
- event->accept();
+ event->accept();
}