/* * 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 "subwindow.h" #include "browser.h" #include "configuration.h" #include "webengine/webprofile.h" #include "webengine/webprofilemanager.h" #include "webengine/webview.h" #include #include #include #include #include #include #include #include #include #include SubWindow::SubWindow(QWidget *parent, Qt::WindowFlags flags) : QWidget(parent, flags) , tabWidget(new TabWidget(this)) { // delete this window when it closes //setAttribute(Qt::WA_DeleteOnClose, true); auto *layout = new QVBoxLayout; layout->setSpacing(0); layout->setContentsMargins(0, 0, 0, 0); layout->addWidget(tabWidget); setLayout(layout); m_profile = WebProfile::defaultProfile(); Configuration config; QKeySequence fullscreen_shortcut; config.shortcut(fullscreen_shortcut, "shortcuts.subwindow.fullscreen"); auto *fullScreen_shortcut = new QShortcut(fullscreen_shortcut, this); connect(fullScreen_shortcut, &QShortcut::activated, this, [this]() { auto *w = this->window(); if(w->isFullScreen()) w->showNormal(); else w->showFullScreen(); }); connect(tabWidget, &TabWidget::currentChanged, [this](int index) { if(index < 0) { // last tab has been closed close(); } else { auto *view = dynamic_cast(tabWidget->widget(index)); Q_CHECK_PTR(view); disconnect(titleConnection); disconnect(linkHoveredConnection); titleConnection = connect(view, &WebView::titleChanged, this, [this](const QString &title) { auto *v = qobject_cast(sender()); this->setWindowTitle(QString("%1 :%2").arg(title, v->profile()->name())); }); setWindowTitle(QString("%1 :%2").arg(view->title(), view->profile()->name())); linkHoveredConnection = connect(view->page(), &WebPage::linkHovered, this, [this](const QString &url) { if(!url.isEmpty()) emit showStatusMessage(url, 3000); }); emit currentViewChanged(view); } }); } SubWindow::SubWindow(const Session::SubWindow &data, QWidget *parent, Qt::WindowFlags flags) : SubWindow(parent, flags) { WebProfileManager profileManager; auto *profile = profileManager.profile(data.profile); if(profile != nullptr) { setProfile(profile); } for(const auto &data : data.tabs) { addTab(data); } } Session::SubWindow SubWindow::serialize() const { QVector tabs(tabCount()); for(int i = 0; i < tabCount(); ++i) { tabs[i] = view(i)->serialize(); } return { profile()->getId(), tabs }; } void SubWindow::setProfile(WebProfile *profile) { if(profile == nullptr) { setProfile(WebProfile::defaultProfile()); return; } this->m_profile = profile; for(int i = 0; i < tabWidget->count(); ++i) { auto *view = qobject_cast(tabWidget->widget(i)); view->setProfile(profile); } } void SubWindow::setTabData(TabData &data, int index) { tabWidget->tabBar()->setTabData(index, QVariant::fromValue(data)); } SubWindow::TabData SubWindow::tabData(int index) const { return tabWidget->tabBar()->tabData(index).value(); } WebView *SubWindow::createView(QWebEnginePage::WebWindowType type) { auto *view = new WebView(m_profile, std::bind(&SubWindow::createView, this, std::placeholders::_1), this); tabWidget->addTab(view); return view; } int SubWindow::addTab(const QUrl &url, WebProfile *profile) { Q_CHECK_PTR(m_profile); auto *_profile = (profile == nullptr) ? m_profile : profile; auto *view = new WebView(_profile, std::bind(&SubWindow::createView, this, std::placeholders::_1), this); if(url.isEmpty()) view->load(_profile->newtab()); else view->load(url); return tabWidget->addTab(view); } int SubWindow::addTab(const Session::WebView &data) { auto *view = new WebView(data, std::bind(&SubWindow::createView, this, std::placeholders::_1), this); return tabWidget->addTab(view); } void SubWindow::moveTab(int from, int to) { tabWidget->tabBar()->moveTab(from, to); } void SubWindow::closeEvent(QCloseEvent *event) { emit aboutToClose(); event->accept(); } void SubWindow::hideEvent(QHideEvent *event) { emit visibilityChanged(false); event->accept(); } void SubWindow::showEvent(QShowEvent *event) { emit visibilityChanged(true); event->accept(); }