Falkon Develop
Cross-platform Qt-based web browser
tabcontextmenu.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "tabcontextmenu.h"
19#include "tabbar.h"
20#include "tabwidget.h"
21#include "browserwindow.h"
22#include "webtab.h"
23#include "settings.h"
24#include "tabbedwebview.h"
25#include "mainapplication.h"
26#include "iconprovider.h"
27#include "checkboxdialog.h"
28
29
30TabContextMenu::TabContextMenu(int index, BrowserWindow *window, Options options)
31 : QMenu()
32 , m_clickedTab(index)
33 , m_window(window)
34 , m_options(options)
35{
36 setObjectName(QStringLiteral("tabcontextmenu"));
37
38 TabWidget *tabWidget = m_window->tabWidget();
40 connect(this, SIGNAL(reloadTab(int)), tabWidget, SLOT(reloadTab(int)));
41 connect(this, SIGNAL(stopTab(int)), tabWidget, SLOT(stopTab(int)));
42 connect(this, SIGNAL(closeAllButCurrent(int)), tabWidget, SLOT(closeAllButCurrent(int)));
43 connect(this, SIGNAL(closeToRight(int)), tabWidget, SLOT(closeToRight(int)));
44 connect(this, SIGNAL(closeToLeft(int)), tabWidget, SLOT(closeToLeft(int)));
45 connect(this, SIGNAL(duplicateTab(int)), tabWidget, SLOT(duplicateTab(int)));
46 connect(this, SIGNAL(detachTab(int)), tabWidget, SLOT(detachTab(int)));
47 connect(this, SIGNAL(loadTab(int)), tabWidget, SLOT(loadTab(int)));
48 connect(this, SIGNAL(unloadTab(int)), tabWidget, SLOT(unloadTab(int)));
49
50 init();
51}
52
53static bool canCloseTabs(const QString &settingsKey, const QString &title, const QString &description)
54{
55 Settings settings;
56 bool ask = settings.value(QSL("Browser-Tabs-Settings/") + settingsKey, true).toBool();
57
58 if (ask) {
59 CheckBoxDialog dialog(QMessageBox::Yes | QMessageBox::No, mApp->activeWindow());
60 dialog.setDefaultButton(QMessageBox::No);
61 dialog.setWindowTitle(title);
62 dialog.setText(description);
63 dialog.setCheckBoxText(TabBar::tr("Don't ask again"));
64 dialog.setIcon(QMessageBox::Question);
65
66 if (dialog.exec() != QMessageBox::Yes) {
67 return false;
68 }
69
70 if (dialog.isChecked()) {
71 settings.setValue(QSL("Browser-Tabs-Settings/") + settingsKey, false);
72 }
73 }
74
75 return true;
76}
77
79{
80 if (canCloseTabs(QLatin1String("AskOnClosingAllButCurrent"), tr("Close Tabs"), tr("Do you really want to close other tabs?"))) {
81 Q_EMIT closeAllButCurrent(m_clickedTab);
82 }
83}
84
86{
87 const QString label = m_options & HorizontalTabs
88 ? tr("Do you really want to close all tabs to the right?")
89 : tr("Do you really want to close all tabs to the bottom?");
90
91 if (canCloseTabs(QLatin1String("AskOnClosingToRight"), tr("Close Tabs"), label)) {
92 Q_EMIT closeToRight(m_clickedTab);
93 }
94}
95
97{
98 const QString label = m_options & HorizontalTabs
99 ? tr("Do you really want to close all tabs to the left?")
100 : tr("Do you really want to close all tabs to the top?");
101
102 if (canCloseTabs(QLatin1String("AskOnClosingToLeft"), tr("Close Tabs"), label)) {
103 Q_EMIT closeToLeft(m_clickedTab);
104 }
105}
106
107void TabContextMenu::init()
108{
109 TabWidget *tabWidget = m_window->tabWidget();
110 if (m_clickedTab != -1) {
111 WebTab* webTab = tabWidget->webTab(m_clickedTab);
112 if (!webTab) {
113 return;
114 }
115
116 if (m_window->weView(m_clickedTab)->isLoading()) {
117 addAction(QIcon::fromTheme(QSL("process-stop")), tr("&Stop Tab"), this, SLOT(stopTab()));
118 }
119 else {
120 addAction(QIcon::fromTheme(QSL("view-refresh")), tr("&Reload Tab"), this, SLOT(reloadTab()));
121 }
122
123 addAction(QIcon::fromTheme(QSL("tab-duplicate")), tr("&Duplicate Tab"), this, SLOT(duplicateTab()));
124
125 if (m_options & ShowDetachTabAction && (mApp->windowCount() > 1 || tabWidget->count() > 1)) {
126 addAction(QIcon::fromTheme(QSL("tab-detach")), tr("D&etach Tab"), this, SLOT(detachTab()));
127 }
128
129 addAction(webTab->isPinned() ? tr("Un&pin Tab") : tr("&Pin Tab"), this, &TabContextMenu::pinTab);
130 addAction(webTab->isMuted() ? tr("Un&mute Tab") : tr("&Mute Tab"), this, &TabContextMenu::muteTab);
131
132 if (!webTab->isRestored()) {
133 addAction(tr("Load Tab"), this, SLOT(loadTab()));
134 } else {
135 addAction(tr("Unload Tab"), this, SLOT(unloadTab()));
136 }
137
138 addSeparator();
139 addAction(tr("Re&load All Tabs"), tabWidget, &TabWidget::reloadAllTabs);
140 addAction(tr("Bookmark &All Tabs"), m_window, &BrowserWindow::bookmarkAllTabs);
141 addSeparator();
142
143 if (m_options & ShowCloseOtherTabsActions) {
144 addAction(tr("Close Ot&her Tabs"), this, SLOT(closeAllButCurrent()));
145 addAction(m_options & HorizontalTabs ? tr("Close Tabs To The Right") : tr("Close Tabs To The Bottom"), this, SLOT(closeToRight()));
146 addAction(m_options & HorizontalTabs ? tr("Close Tabs To The Left") : tr("Close Tabs To The Top"), this, SLOT(closeToLeft()));
147 addSeparator();
148 }
149
150 addAction(m_window->action(QSL("Other/RestoreClosedTab")));
151 addAction(QIcon::fromTheme(QSL("window-close")), tr("Cl&ose Tab"), this, &TabContextMenu::closeTab);
152 } else {
153 addAction(IconProvider::newTabIcon(), tr("&New tab"), m_window, &BrowserWindow::addTab);
154 addSeparator();
155 addAction(tr("Reloa&d All Tabs"), tabWidget, &TabWidget::reloadAllTabs);
156 addAction(tr("Bookmark &All Tabs"), m_window, &BrowserWindow::bookmarkAllTabs);
157 addSeparator();
158 addAction(m_window->action(QSL("Other/RestoreClosedTab")));
159 }
160
161 m_window->action(QSL("Other/RestoreClosedTab"))->setEnabled(tabWidget->canRestoreTab());
162 connect(this, &QMenu::aboutToHide, this, [this]() {
163 m_window->action(QSL("Other/RestoreClosedTab"))->setEnabled(true);
164 });
165}
166
167void TabContextMenu::pinTab()
168{
169 WebTab* webTab = m_window->tabWidget()->webTab(m_clickedTab);
170 if (webTab) {
171 webTab->togglePinned();
172 }
173}
174
175void TabContextMenu::muteTab()
176{
177 WebTab* webTab = m_window->tabWidget()->webTab(m_clickedTab);
178 if (webTab) {
179 webTab->toggleMuted();
180 }
181}
QAction * action(const QString &name) const
TabWidget * tabWidget() const
void bookmarkAllTabs()
TabbedWebView * weView() const
void tabCloseRequested(int index)
static QIcon newTabIcon()
QVariant value(const QString &key, const QVariant &defaultValue=QVariant())
Definition: settings.cpp:74
void setValue(const QString &key, const QVariant &defaultValue=QVariant())
Definition: settings.cpp:69
void loadTab(int index)
void closeToRight(int index)
void tabCloseRequested(int index)
void unloadTab(int index)
void stopTab(int index)
TabContextMenu(int index, BrowserWindow *window, Options options=DefaultOptions)
void duplicateTab(int index)
void detachTab(int index)
void closeToLeft(int index)
void closeAllButCurrent(int index)
void reloadTab(int index)
WebTab * webTab(int index=-1) const
Definition: tabwidget.cpp:570
bool canRestoreTab() const
Definition: tabwidget.cpp:813
void reloadAllTabs()
Definition: tabwidget.cpp:590
TabBar * tabBar() const
Definition: tabwidget.cpp:580
Definition: webtab.h:40
bool isMuted() const
Definition: webtab.cpp:433
bool isRestored() const
Definition: webtab.cpp:548
void toggleMuted()
Definition: webtab.cpp:448
bool isPinned() const
Definition: webtab.cpp:414
void togglePinned()
Definition: webtab.cpp:715
bool isLoading() const
Definition: webview.cpp:214
#define mApp
#define QSL(x)
Definition: qzcommon.h:40