/* * 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 "tabwidget.h" #include "webengine/webview.h" #include #include #include #include #include #include #include #include #include #include "configuration.h" #include "webengine/webprofile.h" SubWindow::SubWindow(QWidget *parent, Qt::WindowFlags flags) : QMdiSubWindow(parent, flags) , tabWidget(new TabWidget(this)) { // delete this window when it closes setAttribute(Qt::WA_DeleteOnClose, true); resize(800, 600); setWidget(tabWidget); m_profile = WebProfile::defaultProfile(); Configuration config; auto *fullScreen_shortcut = new QShortcut(QKeySequence(config.value("shortcuts.subwindow.fullscreen").value()), this); connect(fullScreen_shortcut, &QShortcut::activated, 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() { delete tabWidget; } int SubWindow::currentTabIndex() const { return tabWidget->currentIndex(); } WebView *SubWindow::currentView() { return qobject_cast(tabWidget->currentWidget()); } WebView *SubWindow::view(int index) const { return qobject_cast(tabWidget->widget(index)); } int SubWindow::tabCount() const { return tabWidget->count(); } 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); } } WebProfile *SubWindow::profile() const { return m_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(); } 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, this); if(url.isEmpty()) view->load(_profile->newtab()); else view->load(url); return tabWidget->addTab(view); } void SubWindow::closeTab(int index) { tabWidget->removeTab(index); } void SubWindow::setCurrentTab(int index) { if(index >= 0) tabWidget->setCurrentIndex(index); } void SubWindow::moveTab(int from, int to) { tabWidget->tabBar()->moveTab(from, to); } int SubWindow::restoreLastTab() { return tabWidget->restoreLastTab(); } void SubWindow::restoreTabMenu(QMenu *menu) { tabWidget->restoreTabMenu(menu); }