Warning, file /network/falkon/src/lib/tabwidget/tabcontextmenu.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "tabcontextmenu.h"
0019 #include "tabbar.h"
0020 #include "tabwidget.h"
0021 #include "browserwindow.h"
0022 #include "webtab.h"
0023 #include "settings.h"
0024 #include "tabbedwebview.h"
0025 #include "mainapplication.h"
0026 #include "iconprovider.h"
0027 #include "checkboxdialog.h"
0028 
0029 
0030 TabContextMenu::TabContextMenu(int index, BrowserWindow *window, Options options)
0031     : QMenu()
0032     , m_clickedTab(index)
0033     , m_window(window)
0034     , m_options(options)
0035 {
0036     setObjectName(QStringLiteral("tabcontextmenu"));
0037 
0038     TabWidget *tabWidget = m_window->tabWidget();
0039     connect(this, &TabContextMenu::tabCloseRequested, tabWidget->tabBar(), &ComboTabBar::tabCloseRequested);
0040     connect(this, SIGNAL(reloadTab(int)), tabWidget, SLOT(reloadTab(int)));
0041     connect(this, SIGNAL(stopTab(int)), tabWidget, SLOT(stopTab(int)));
0042     connect(this, SIGNAL(closeAllButCurrent(int)), tabWidget, SLOT(closeAllButCurrent(int)));
0043     connect(this, SIGNAL(closeToRight(int)), tabWidget, SLOT(closeToRight(int)));
0044     connect(this, SIGNAL(closeToLeft(int)), tabWidget, SLOT(closeToLeft(int)));
0045     connect(this, SIGNAL(duplicateTab(int)), tabWidget, SLOT(duplicateTab(int)));
0046     connect(this, SIGNAL(detachTab(int)), tabWidget, SLOT(detachTab(int)));
0047     connect(this, SIGNAL(loadTab(int)), tabWidget, SLOT(loadTab(int)));
0048     connect(this, SIGNAL(unloadTab(int)), tabWidget, SLOT(unloadTab(int)));
0049 
0050     init();
0051 }
0052 
0053 static bool canCloseTabs(const QString &settingsKey, const QString &title, const QString &description)
0054 {
0055     Settings settings;
0056     bool ask = settings.value(QSL("Browser-Tabs-Settings/") + settingsKey, true).toBool();
0057 
0058     if (ask) {
0059         CheckBoxDialog dialog(QMessageBox::Yes | QMessageBox::No, mApp->activeWindow());
0060         dialog.setDefaultButton(QMessageBox::No);
0061         dialog.setWindowTitle(title);
0062         dialog.setText(description);
0063         dialog.setCheckBoxText(TabBar::tr("Don't ask again"));
0064         dialog.setIcon(QMessageBox::Question);
0065 
0066         if (dialog.exec() != QMessageBox::Yes) {
0067             return false;
0068         }
0069 
0070         if (dialog.isChecked()) {
0071             settings.setValue(QSL("Browser-Tabs-Settings/") + settingsKey, false);
0072         }
0073     }
0074 
0075     return true;
0076 }
0077 
0078 void TabContextMenu::closeAllButCurrent()
0079 {
0080     if (canCloseTabs(QLatin1String("AskOnClosingAllButCurrent"), tr("Close Tabs"), tr("Do you really want to close other tabs?"))) {
0081         Q_EMIT closeAllButCurrent(m_clickedTab);
0082     }
0083 }
0084 
0085 void TabContextMenu::closeToRight()
0086 {
0087     const QString label = m_options & HorizontalTabs
0088             ? tr("Do you really want to close all tabs to the right?")
0089             : tr("Do you really want to close all tabs to the bottom?");
0090 
0091     if (canCloseTabs(QLatin1String("AskOnClosingToRight"), tr("Close Tabs"), label)) {
0092         Q_EMIT closeToRight(m_clickedTab);
0093     }
0094 }
0095 
0096 void TabContextMenu::closeToLeft()
0097 {
0098     const QString label = m_options & HorizontalTabs
0099             ? tr("Do you really want to close all tabs to the left?")
0100             : tr("Do you really want to close all tabs to the top?");
0101 
0102     if (canCloseTabs(QLatin1String("AskOnClosingToLeft"), tr("Close Tabs"), label)) {
0103         Q_EMIT closeToLeft(m_clickedTab);
0104     }
0105 }
0106 
0107 void TabContextMenu::init()
0108 {
0109     TabWidget *tabWidget = m_window->tabWidget();
0110     if (m_clickedTab != -1) {
0111         WebTab* webTab = tabWidget->webTab(m_clickedTab);
0112         if (!webTab) {
0113             return;
0114         }
0115 
0116         if (m_window->weView(m_clickedTab)->isLoading()) {
0117             addAction(QIcon::fromTheme(QSL("process-stop")), tr("&Stop Tab"), this, SLOT(stopTab()));
0118         }
0119         else {
0120             addAction(QIcon::fromTheme(QSL("view-refresh")), tr("&Reload Tab"), this, SLOT(reloadTab()));
0121         }
0122 
0123         addAction(QIcon::fromTheme(QSL("tab-duplicate")), tr("&Duplicate Tab"), this, SLOT(duplicateTab()));
0124 
0125         if (m_options & ShowDetachTabAction && (mApp->windowCount() > 1 || tabWidget->count() > 1)) {
0126             addAction(QIcon::fromTheme(QSL("tab-detach")), tr("D&etach Tab"), this, SLOT(detachTab()));
0127         }
0128 
0129         addAction(webTab->isPinned() ? tr("Un&pin Tab") : tr("&Pin Tab"), this, &TabContextMenu::pinTab);
0130         addAction(webTab->isMuted() ? tr("Un&mute Tab") : tr("&Mute Tab"), this, &TabContextMenu::muteTab);
0131 
0132         if (!webTab->isRestored()) {
0133             addAction(tr("Load Tab"), this, SLOT(loadTab()));
0134         } else {
0135             addAction(tr("Unload Tab"), this, SLOT(unloadTab()));
0136         }
0137 
0138         addSeparator();
0139         addAction(tr("Re&load All Tabs"), tabWidget, &TabWidget::reloadAllTabs);
0140         addAction(tr("Bookmark &All Tabs"), m_window, &BrowserWindow::bookmarkAllTabs);
0141         addSeparator();
0142 
0143         if (m_options & ShowCloseOtherTabsActions) {
0144             addAction(tr("Close Ot&her Tabs"), this, SLOT(closeAllButCurrent()));
0145             addAction(m_options & HorizontalTabs ? tr("Close Tabs To The Right") : tr("Close Tabs To The Bottom"), this, SLOT(closeToRight()));
0146             addAction(m_options & HorizontalTabs ? tr("Close Tabs To The Left") : tr("Close Tabs To The Top"), this, SLOT(closeToLeft()));
0147             addSeparator();
0148         }
0149 
0150         addAction(m_window->action(QSL("Other/RestoreClosedTab")));
0151         addAction(QIcon::fromTheme(QSL("window-close")), tr("Cl&ose Tab"), this, &TabContextMenu::closeTab);
0152     } else {
0153         addAction(IconProvider::newTabIcon(), tr("&New tab"), m_window, &BrowserWindow::addTab);
0154         addSeparator();
0155         addAction(tr("Reloa&d All Tabs"), tabWidget, &TabWidget::reloadAllTabs);
0156         addAction(tr("Bookmark &All Tabs"), m_window, &BrowserWindow::bookmarkAllTabs);
0157         addSeparator();
0158         addAction(m_window->action(QSL("Other/RestoreClosedTab")));
0159     }
0160 
0161     m_window->action(QSL("Other/RestoreClosedTab"))->setEnabled(tabWidget->canRestoreTab());
0162     connect(this, &QMenu::aboutToHide, this, [this]() {
0163         m_window->action(QSL("Other/RestoreClosedTab"))->setEnabled(true);
0164     });
0165 }
0166 
0167 void TabContextMenu::pinTab()
0168 {
0169     WebTab* webTab = m_window->tabWidget()->webTab(m_clickedTab);
0170     if (webTab) {
0171         webTab->togglePinned();
0172     }
0173 }
0174 
0175 void TabContextMenu::muteTab()
0176 {
0177     WebTab* webTab = m_window->tabWidget()->webTab(m_clickedTab);
0178     if (webTab) {
0179         webTab->toggleMuted();
0180     }
0181 }