File indexing completed on 2024-05-19 04:59:20

0001 /* ============================================================
0002 * TabManager plugin for Falkon
0003 * Copyright (C) 2013-2018 S. Razi Alavizadeh <s.r.alavizadeh@gmail.com>
0004 * Copyright (C) 2017-2018 David Rosca <nowrep@gmail.com>
0005 *
0006 * This program is free software: you can redistribute it and/or modify
0007 * it under the terms of the GNU General Public License as published by
0008 * the Free Software Foundation, either version 3 of the License, or
0009 * (at your option) any later version.
0010 *
0011 * This program is distributed in the hope that it will be useful,
0012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014 * GNU General Public License for more details.
0015 *
0016 * You should have received a copy of the GNU General Public License
0017 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0018 * ============================================================ */
0019 #include "tabmanagerwidgetcontroller.h"
0020 #include "abstractbuttoninterface.h"
0021 #include "browserwindow.h"
0022 #include "tabwidget.h"
0023 #include "mainapplication.h"
0024 #include "tabbar.h"
0025 #include "statusbar.h"
0026 #include "navigationbar.h"
0027 
0028 #include <QScreen>
0029 #include <QAction>
0030 #include <QStyle>
0031 
0032 #include <QDebug>
0033 
0034 class TabManagerButton : public AbstractButtonInterface
0035 {
0036     Q_OBJECT
0037 public:
0038     explicit TabManagerButton(QObject *parent = nullptr)
0039         : AbstractButtonInterface(parent)
0040     {
0041     }
0042 
0043     QString id() const override
0044     {
0045         return QSL("tabmanager-icon");
0046     }
0047 
0048     QString name() const override
0049     {
0050         return tr("Tab Manager button");
0051     }
0052 };
0053 
0054 
0055 TabManagerWidgetController::TabManagerWidgetController(QObject* parent)
0056     : SideBarInterface(parent)
0057     , m_defaultTabManager(nullptr)
0058     , m_groupType(TabManagerWidget::GroupByWindow)
0059 {
0060 }
0061 
0062 TabManagerWidgetController::~TabManagerWidgetController()
0063 = default;
0064 
0065 QString TabManagerWidgetController::title() const
0066 {
0067     return tr("Tab Manager");
0068 }
0069 
0070 QAction* TabManagerWidgetController::createMenuAction()
0071 {
0072     auto* act = new QAction(tr("Tab Manager"), this);
0073     act->setCheckable(true);
0074     act->setIcon(QIcon(QSL(":tabmanager/data/tabmanager.png")));
0075     act->setShortcut(QKeySequence(QSL("Ctrl+Shift+M")));
0076     act->setData(QSL("TabManager"));
0077 
0078     return act;
0079 }
0080 
0081 QWidget* TabManagerWidgetController::createSideBarWidget(BrowserWindow* mainWindow)
0082 {
0083     return createTabManagerWidget(mainWindow, mainWindow);
0084 }
0085 
0086 AbstractButtonInterface* TabManagerWidgetController::createStatusBarIcon(BrowserWindow* mainWindow)
0087 {
0088     if (!defaultTabManager()) {
0089         return nullptr;
0090     }
0091 
0092     if (m_statusBarIcons.contains(mainWindow)) {
0093         return m_statusBarIcons.value(mainWindow);
0094     }
0095 
0096     auto* icon = new TabManagerButton(this);
0097     icon->setIcon(QPixmap(QSL(":tabmanager/data/tabmanager.png")));
0098     icon->setTitle(tr("Tab Manager"));
0099     icon->setToolTip(tr("Show Tab Manager"));
0100     connect(icon, &AbstractButtonInterface::clicked, this, [=](AbstractButtonInterface::ClickController *c) {
0101         if (!defaultTabManager()) {
0102             return;
0103         }
0104 
0105         static int frameWidth = (defaultTabManager()->frameGeometry().width() - defaultTabManager()->geometry().width()) / 2;
0106         static int titleBarHeight = defaultTabManager()->style()->pixelMetric(QStyle::PM_TitleBarHeight);
0107 
0108         QSize newSize(defaultTabManager()->width(), mainWindow->height() - titleBarHeight - frameWidth);
0109         QRect newGeo(c->popupPosition(newSize), newSize);
0110         defaultTabManager()->setGeometry(newGeo);
0111         raiseTabManager();
0112 
0113         QTimer::singleShot(0, this, [=]() {
0114             c->popupClosed();
0115         });
0116     });
0117 
0118     QAction* showAction = createMenuAction();
0119     showAction->setCheckable(false);
0120     showAction->setParent(icon);
0121     mainWindow->addAction(showAction);
0122     connect(showAction, SIGNAL(triggered()), this, SLOT(raiseTabManager()));
0123 
0124     m_statusBarIcons.insert(mainWindow, icon);
0125     m_actions.insert(mainWindow, showAction);
0126 
0127     return icon;
0128 }
0129 
0130 TabManagerWidget::GroupType TabManagerWidgetController::groupType()
0131 {
0132     return m_groupType;
0133 }
0134 
0135 void TabManagerWidgetController::setGroupType(TabManagerWidget::GroupType type)
0136 {
0137     m_groupType = type;
0138 }
0139 
0140 TabManagerWidget* TabManagerWidgetController::createTabManagerWidget(BrowserWindow* mainClass, QWidget* parent, bool defaultWidget)
0141 {
0142     auto* tabManagerWidget = new TabManagerWidget(mainClass, parent, defaultWidget);
0143     tabManagerWidget->setGroupType(m_groupType);
0144 
0145     if (defaultWidget) {
0146         m_defaultTabManager = tabManagerWidget;
0147         QAction* showAction = createMenuAction();
0148         showAction->setCheckable(false);
0149         showAction->setParent(m_defaultTabManager);
0150         m_defaultTabManager->addAction(showAction);
0151         connect(showAction, SIGNAL(triggered()), this, SLOT(raiseTabManager()));
0152         connect(tabManagerWidget, SIGNAL(showSideBySide()), this, SLOT(showSideBySide()));
0153     }
0154     else {
0155         m_defaultTabManager = nullptr;
0156     }
0157 
0158     connect(tabManagerWidget, SIGNAL(groupTypeChanged(TabManagerWidget::GroupType)), this, SLOT(setGroupType(TabManagerWidget::GroupType)));
0159     connect(this, SIGNAL(requestRefreshTree(WebPage*)), tabManagerWidget, SLOT(delayedRefreshTree(WebPage*)));
0160 
0161     Q_EMIT requestRefreshTree();
0162 
0163     return tabManagerWidget;
0164 }
0165 
0166 TabManagerWidget* TabManagerWidgetController::defaultTabManager()
0167 {
0168     return m_defaultTabManager;
0169 }
0170 
0171 void TabManagerWidgetController::addStatusBarIcon(BrowserWindow* window)
0172 {
0173     if (window) {
0174         window->statusBar()->addButton(createStatusBarIcon(window));
0175         window->navigationBar()->addToolButton(createStatusBarIcon(window));
0176     }
0177 }
0178 
0179 void TabManagerWidgetController::removeStatusBarIcon(BrowserWindow* window)
0180 {
0181     if (window) {
0182         window->statusBar()->removeButton(m_statusBarIcons.value(window));
0183         window->navigationBar()->removeToolButton(m_statusBarIcons.value(window));
0184         window->removeAction(m_actions.value(window));
0185         delete m_actions.value(window);
0186         delete m_statusBarIcons.value(window);
0187         m_statusBarIcons.remove(window);
0188         m_actions.remove(window);
0189     }
0190 }
0191 
0192 void TabManagerWidgetController::mainWindowDeleted(BrowserWindow* window)
0193 {
0194     removeStatusBarIcon(window);
0195 
0196     Q_EMIT requestRefreshTree();
0197 }
0198 
0199 void TabManagerWidgetController::raiseTabManager()
0200 {
0201     if (!defaultTabManager()) {
0202         return;
0203     }
0204 
0205     defaultTabManager()->activateWindow();
0206     defaultTabManager()->showNormal();
0207     defaultTabManager()->raise();
0208 }
0209 
0210 void TabManagerWidgetController::showSideBySide()
0211 {
0212     if (!defaultTabManager()) {
0213         return;
0214     }
0215 
0216     const QRect &availableGeometry = defaultTabManager()->screen()->availableGeometry();
0217     static int frameWidth = (defaultTabManager()->frameGeometry().width() - defaultTabManager()->geometry().width()) / 2;
0218     static int titleBarHeight = defaultTabManager()->style()->pixelMetric(QStyle::PM_TitleBarHeight);
0219 
0220     QRect managerRect(availableGeometry.left() + frameWidth, availableGeometry.top() + titleBarHeight,
0221                       defaultTabManager()->width(), availableGeometry.height() - titleBarHeight - frameWidth);
0222     QRect windowRect(managerRect.topRight().x() + 2 * frameWidth, managerRect.top(),
0223                        availableGeometry.width() - managerRect.width() - 4 * frameWidth, managerRect.height());
0224 
0225     defaultTabManager()->setGeometry(managerRect);
0226     mApp->getWindow()->setGeometry(windowRect);
0227     mApp->getWindow()->showNormal();
0228     mApp->getWindow()->raise();
0229 
0230     defaultTabManager()->show();
0231     defaultTabManager()->activateWindow();
0232     defaultTabManager()->raise();
0233 }
0234 
0235 void TabManagerWidgetController::emitRefreshTree()
0236 {
0237     Q_EMIT requestRefreshTree();
0238 }
0239 
0240 #include "tabmanagerwidgetcontroller.moc"