2011-03-03 18:29:20 +01:00
|
|
|
/* ============================================================
|
2017-08-25 17:11:29 +02:00
|
|
|
* Falkon - Qt web browser
|
2018-01-01 14:59:15 +01:00
|
|
|
* Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
|
2011-03-03 18:29:20 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
2011-03-02 16:57:41 +01:00
|
|
|
#include "tabbar.h"
|
|
|
|
#include "tabwidget.h"
|
2014-02-19 22:07:21 +01:00
|
|
|
#include "browserwindow.h"
|
2011-03-25 19:16:21 +01:00
|
|
|
#include "webtab.h"
|
2011-10-14 23:12:10 +02:00
|
|
|
#include "toolbutton.h"
|
2012-01-11 21:58:25 +01:00
|
|
|
#include "settings.h"
|
2012-02-29 18:33:50 +01:00
|
|
|
#include "tabbedwebview.h"
|
2012-03-05 13:16:34 +01:00
|
|
|
#include "mainapplication.h"
|
|
|
|
#include "pluginproxy.h"
|
2014-03-24 16:08:33 +01:00
|
|
|
#include "iconprovider.h"
|
2017-10-03 10:10:32 +02:00
|
|
|
#include "tabcontextmenu.h"
|
2017-12-31 19:13:25 +01:00
|
|
|
#include "searchenginesmanager.h"
|
2012-02-29 18:33:50 +01:00
|
|
|
|
2012-12-20 14:45:35 +01:00
|
|
|
#include <QMimeData>
|
2012-08-31 15:19:07 +02:00
|
|
|
#include <QMouseEvent>
|
2012-09-03 22:40:52 +02:00
|
|
|
#include <QStyleOption>
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QTimer>
|
2012-04-08 21:45:40 +02:00
|
|
|
#include <QRect>
|
2014-03-31 09:50:54 +02:00
|
|
|
#include <QLabel>
|
2013-11-26 14:14:36 +01:00
|
|
|
#include <QScrollArea>
|
|
|
|
#include <QHBoxLayout>
|
2018-01-01 14:59:15 +01:00
|
|
|
#include <QDrag>
|
|
|
|
|
2018-01-01 17:00:42 +01:00
|
|
|
#define MIMETYPE QSL("application/falkon.tabbar.tab")
|
2011-03-25 19:16:21 +01:00
|
|
|
|
2014-02-19 22:07:21 +01:00
|
|
|
TabBar::TabBar(BrowserWindow* window, TabWidget* tabWidget)
|
2013-11-26 14:14:36 +01:00
|
|
|
: ComboTabBar()
|
2014-02-19 22:07:21 +01:00
|
|
|
, m_window(window)
|
2011-10-18 21:07:58 +02:00
|
|
|
, m_tabWidget(tabWidget)
|
2014-02-01 19:21:49 +01:00
|
|
|
, m_hideTabBarWithOneTab(false)
|
2014-02-23 14:49:32 +01:00
|
|
|
, m_showCloseOnInactive(0)
|
2011-10-23 14:44:18 +02:00
|
|
|
, m_normalTabWidth(0)
|
2012-12-10 00:26:25 +01:00
|
|
|
, m_activeTabWidth(0)
|
2015-10-18 15:56:12 +02:00
|
|
|
, m_forceHidden(false)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2011-09-11 19:15:06 +02:00
|
|
|
setObjectName("tabbar");
|
2011-03-02 16:57:41 +01:00
|
|
|
setElideMode(Qt::ElideRight);
|
2011-10-24 17:46:45 +02:00
|
|
|
setFocusPolicy(Qt::NoFocus);
|
2014-02-23 14:49:32 +01:00
|
|
|
setTabsClosable(false);
|
2012-04-08 21:45:40 +02:00
|
|
|
setMouseTracking(true);
|
2014-03-10 09:38:16 +01:00
|
|
|
setDocumentMode(true);
|
2012-03-05 11:30:18 +01:00
|
|
|
setAcceptDrops(true);
|
2014-03-10 09:38:16 +01:00
|
|
|
setDrawBase(false);
|
|
|
|
setMovable(true);
|
2012-03-05 11:30:18 +01:00
|
|
|
|
2012-04-09 14:09:40 +02:00
|
|
|
connect(this, SIGNAL(currentChanged(int)), this, SLOT(currentTabChanged(int)));
|
|
|
|
|
2013-11-26 14:14:36 +01:00
|
|
|
// ComboTabBar features
|
|
|
|
setUsesScrollButtons(true);
|
2014-02-19 22:07:21 +01:00
|
|
|
setCloseButtonsToolTip(BrowserWindow::tr("Close Tab"));
|
2014-03-30 16:40:36 +02:00
|
|
|
connect(this, SIGNAL(overFlowChanged(bool)), this, SLOT(overflowChanged(bool)));
|
2014-03-31 09:50:54 +02:00
|
|
|
|
|
|
|
if (mApp->isPrivate()) {
|
|
|
|
QLabel* privateBrowsing = new QLabel(this);
|
|
|
|
privateBrowsing->setObjectName(QSL("private-browsing-icon"));
|
2015-10-24 12:43:15 +02:00
|
|
|
privateBrowsing->setPixmap(IconProvider::privateBrowsingIcon().pixmap(16));
|
2014-03-31 09:50:54 +02:00
|
|
|
privateBrowsing->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
|
|
|
|
privateBrowsing->setFixedWidth(30);
|
|
|
|
addCornerWidget(privateBrowsing, Qt::TopLeftCorner);
|
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TabBar::loadSettings()
|
|
|
|
{
|
2012-01-11 21:58:25 +01:00
|
|
|
Settings settings;
|
2011-03-02 16:57:41 +01:00
|
|
|
settings.beginGroup("Browser-Tabs-Settings");
|
2013-06-06 13:14:12 +02:00
|
|
|
m_hideTabBarWithOneTab = settings.value("hideTabsWithOneTab", false).toBool();
|
2012-08-27 14:02:35 +02:00
|
|
|
bool activateLastTab = settings.value("ActivateLastTabWhenClosingActual", false).toBool();
|
2014-02-23 14:49:32 +01:00
|
|
|
m_showCloseOnInactive = settings.value("showCloseOnInactiveTabs", 0).toInt(0);
|
2013-06-06 13:14:12 +02:00
|
|
|
settings.endGroup();
|
2012-04-04 18:48:54 +02:00
|
|
|
|
2012-08-27 14:02:35 +02:00
|
|
|
setSelectionBehaviorOnRemove(activateLastTab ? QTabBar::SelectPreviousTab : QTabBar::SelectRightTab);
|
2018-01-01 15:10:11 +01:00
|
|
|
setVisible(!(count() <= 1 && m_hideTabBarWithOneTab));
|
2013-11-26 14:14:36 +01:00
|
|
|
|
|
|
|
setUpLayout();
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2014-04-01 18:47:19 +02:00
|
|
|
TabWidget* TabBar::tabWidget() const
|
|
|
|
{
|
|
|
|
return m_tabWidget;
|
|
|
|
}
|
|
|
|
|
2011-10-24 17:46:45 +02:00
|
|
|
void TabBar::setVisible(bool visible)
|
|
|
|
{
|
2015-10-18 15:56:12 +02:00
|
|
|
if (m_forceHidden) {
|
|
|
|
ComboTabBar::setVisible(false);
|
2013-06-06 13:14:12 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure to honor user preference
|
2011-10-24 17:46:45 +02:00
|
|
|
if (visible) {
|
2018-01-01 15:10:11 +01:00
|
|
|
visible = !(count() <= 1 && m_hideTabBarWithOneTab);
|
2013-06-06 13:14:12 +02:00
|
|
|
}
|
2011-10-24 17:46:45 +02:00
|
|
|
|
2013-11-26 14:14:36 +01:00
|
|
|
ComboTabBar::setVisible(visible);
|
2011-10-24 17:46:45 +02:00
|
|
|
}
|
|
|
|
|
2015-10-18 15:56:12 +02:00
|
|
|
void TabBar::setForceHidden(bool hidden)
|
|
|
|
{
|
|
|
|
m_forceHidden = hidden;
|
|
|
|
setVisible(!m_forceHidden);
|
|
|
|
}
|
|
|
|
|
2014-03-30 16:40:36 +02:00
|
|
|
void TabBar::overflowChanged(bool overflowed)
|
|
|
|
{
|
|
|
|
// Make sure close buttons on inactive tabs are hidden
|
|
|
|
// This is needed for when leaving fullscreen from non-overflowed to overflowed state
|
|
|
|
if (overflowed && m_showCloseOnInactive != 1) {
|
|
|
|
setTabsClosable(false);
|
|
|
|
showCloseButton(currentIndex());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-26 14:14:36 +01:00
|
|
|
QSize TabBar::tabSizeHint(int index, bool fast) const
|
2011-03-25 19:16:21 +01:00
|
|
|
{
|
2016-07-10 16:34:05 +02:00
|
|
|
if (!m_window->isVisible()) {
|
|
|
|
// Don't calculate it when window is not visible
|
2012-09-02 14:18:07 +02:00
|
|
|
// It produces invalid size anyway
|
|
|
|
return QSize(-1, -1);
|
|
|
|
}
|
|
|
|
|
2014-06-05 20:15:58 +02:00
|
|
|
const int pinnedTabWidth = comboTabBarPixelMetric(ComboTabBar::PinnedTabWidth);
|
|
|
|
const int minTabWidth = comboTabBarPixelMetric(ComboTabBar::NormalTabMinimumWidth);
|
2013-11-26 14:14:36 +01:00
|
|
|
|
|
|
|
QSize size = ComboTabBar::tabSizeHint(index);
|
2013-03-03 20:18:34 +01:00
|
|
|
|
2014-03-30 16:40:36 +02:00
|
|
|
// The overflowed tabs have same size and we can use this fast method
|
2013-11-26 14:14:36 +01:00
|
|
|
if (fast) {
|
2014-06-05 20:15:58 +02:00
|
|
|
size.setWidth(index >= pinnedTabsCount() ? minTabWidth : pinnedTabWidth);
|
2013-11-26 14:14:36 +01:00
|
|
|
return size;
|
2012-09-02 12:22:11 +02:00
|
|
|
}
|
|
|
|
|
2011-10-14 23:12:10 +02:00
|
|
|
WebTab* webTab = qobject_cast<WebTab*>(m_tabWidget->widget(index));
|
2011-10-23 14:44:18 +02:00
|
|
|
TabBar* tabBar = const_cast <TabBar*>(this);
|
2011-10-14 23:12:10 +02:00
|
|
|
|
|
|
|
if (webTab && webTab->isPinned()) {
|
2014-06-05 20:15:58 +02:00
|
|
|
size.setWidth(pinnedTabWidth);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2014-03-18 17:35:44 +01:00
|
|
|
int availableWidth = mainTabBarWidth() - comboTabBarPixelMetric(ExtraReservedWidth);
|
2013-11-26 14:14:36 +01:00
|
|
|
|
2013-01-19 16:34:19 +01:00
|
|
|
if (availableWidth < 0) {
|
|
|
|
return QSize(-1, -1);
|
|
|
|
}
|
|
|
|
|
2013-11-26 14:14:36 +01:00
|
|
|
const int normalTabsCount = ComboTabBar::normalTabsCount();
|
2014-06-05 20:15:58 +02:00
|
|
|
const int maxTabWidth = comboTabBarPixelMetric(ComboTabBar::NormalTabMaximumWidth);
|
2013-11-27 22:27:24 +01:00
|
|
|
|
2014-06-05 20:15:58 +02:00
|
|
|
if (availableWidth >= maxTabWidth * normalTabsCount) {
|
|
|
|
m_normalTabWidth = maxTabWidth;
|
2011-10-23 14:44:18 +02:00
|
|
|
size.setWidth(m_normalTabWidth);
|
|
|
|
}
|
2013-12-26 19:52:07 +01:00
|
|
|
else if (normalTabsCount > 0) {
|
2014-06-05 20:15:58 +02:00
|
|
|
const int minActiveTabWidth = comboTabBarPixelMetric(ComboTabBar::ActiveTabMinimumWidth);
|
|
|
|
|
2012-08-27 14:02:35 +02:00
|
|
|
int maxWidthForTab = availableWidth / normalTabsCount;
|
2013-03-02 18:15:05 +01:00
|
|
|
int realTabWidth = maxWidthForTab;
|
2013-04-28 17:48:51 +02:00
|
|
|
bool adjustingActiveTab = false;
|
|
|
|
|
2014-06-05 20:15:58 +02:00
|
|
|
if (realTabWidth < minActiveTabWidth) {
|
|
|
|
maxWidthForTab = normalTabsCount > 1 ? (availableWidth - minActiveTabWidth) / (normalTabsCount - 1) : 0;
|
|
|
|
realTabWidth = minActiveTabWidth;
|
2012-12-10 00:26:25 +01:00
|
|
|
adjustingActiveTab = true;
|
|
|
|
}
|
|
|
|
|
2014-06-05 20:15:58 +02:00
|
|
|
bool tryAdjusting = availableWidth >= minTabWidth * normalTabsCount;
|
2013-03-02 18:15:05 +01:00
|
|
|
|
2014-06-05 20:15:58 +02:00
|
|
|
if (m_showCloseOnInactive != 1 && tabsClosable() && availableWidth < (minTabWidth + 25) * normalTabsCount) {
|
2013-11-27 22:27:24 +01:00
|
|
|
// Hiding close buttons to save some space
|
|
|
|
tabBar->setTabsClosable(false);
|
|
|
|
tabBar->showCloseButton(currentIndex());
|
2013-03-02 18:15:05 +01:00
|
|
|
}
|
2014-02-23 14:49:32 +01:00
|
|
|
if (m_showCloseOnInactive == 1) {
|
2014-02-23 14:54:22 +01:00
|
|
|
// Always showing close buttons
|
2014-02-23 14:49:32 +01:00
|
|
|
tabBar->setTabsClosable(true);
|
|
|
|
tabBar->showCloseButton(currentIndex());
|
|
|
|
}
|
2013-03-02 18:15:05 +01:00
|
|
|
|
|
|
|
if (tryAdjusting) {
|
2012-08-27 14:02:35 +02:00
|
|
|
m_normalTabWidth = maxWidthForTab;
|
|
|
|
|
2012-12-10 00:26:25 +01:00
|
|
|
// Fill any empty space (we've got from rounding) with active tab
|
2013-11-26 14:14:36 +01:00
|
|
|
if (index == mainTabBarCurrentIndex()) {
|
2012-12-11 09:54:40 +01:00
|
|
|
if (adjustingActiveTab) {
|
2014-06-05 20:15:58 +02:00
|
|
|
m_activeTabWidth = (availableWidth - minActiveTabWidth
|
2013-03-02 23:18:49 +01:00
|
|
|
- maxWidthForTab * (normalTabsCount - 1)) + realTabWidth;
|
2012-12-11 09:54:40 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_activeTabWidth = (availableWidth - maxWidthForTab * normalTabsCount) + maxWidthForTab;
|
|
|
|
}
|
2012-12-10 00:26:25 +01:00
|
|
|
size.setWidth(m_activeTabWidth);
|
2012-08-27 14:02:35 +02:00
|
|
|
}
|
|
|
|
else {
|
2012-12-10 00:26:25 +01:00
|
|
|
size.setWidth(m_normalTabWidth);
|
2012-08-27 14:02:35 +02:00
|
|
|
}
|
|
|
|
}
|
2011-10-14 23:12:10 +02:00
|
|
|
}
|
2013-11-27 22:27:24 +01:00
|
|
|
|
|
|
|
// Restore close buttons according to preferences
|
2014-06-05 20:15:58 +02:00
|
|
|
if (m_showCloseOnInactive != 2 && !tabsClosable() && availableWidth >= (minTabWidth + 25) * normalTabsCount) {
|
2013-11-27 22:27:24 +01:00
|
|
|
tabBar->setTabsClosable(true);
|
|
|
|
|
|
|
|
// Hide close buttons on pinned tabs
|
|
|
|
for (int i = 0; i < count(); ++i) {
|
|
|
|
tabBar->updatePinnedTabCloseButton(i);
|
|
|
|
}
|
|
|
|
}
|
2011-03-25 19:16:21 +01:00
|
|
|
}
|
2011-10-23 14:44:18 +02:00
|
|
|
|
2013-03-02 18:15:05 +01:00
|
|
|
if (index == count() - 1) {
|
2013-11-26 14:14:36 +01:00
|
|
|
WebTab* lastMainActiveTab = qobject_cast<WebTab*>(m_tabWidget->widget(mainTabBarCurrentIndex()));
|
2014-03-31 09:50:54 +02:00
|
|
|
int xForAddTabButton = cornerWidth(Qt::TopLeftCorner) + pinTabBarWidth() + normalTabsCount() * m_normalTabWidth;
|
2013-03-02 18:15:05 +01:00
|
|
|
|
2013-11-26 14:14:36 +01:00
|
|
|
if (lastMainActiveTab && m_activeTabWidth > m_normalTabWidth) {
|
2012-12-10 00:26:25 +01:00
|
|
|
xForAddTabButton += m_activeTabWidth - m_normalTabWidth;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2012-08-27 14:02:35 +02:00
|
|
|
|
2012-08-09 04:00:09 +02:00
|
|
|
if (QApplication::layoutDirection() == Qt::RightToLeft) {
|
|
|
|
xForAddTabButton = width() - xForAddTabButton;
|
|
|
|
}
|
2013-03-02 18:15:05 +01:00
|
|
|
|
2011-10-23 14:44:18 +02:00
|
|
|
emit tabBar->moveAddTabButton(xForAddTabButton);
|
|
|
|
}
|
|
|
|
|
2011-03-25 19:16:21 +01:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2013-11-26 14:14:36 +01:00
|
|
|
int TabBar::comboTabBarPixelMetric(ComboTabBar::SizeType sizeType) const
|
|
|
|
{
|
|
|
|
switch (sizeType) {
|
|
|
|
case ComboTabBar::PinnedTabWidth:
|
2016-01-25 13:33:05 +01:00
|
|
|
return iconButtonSize().width() + style()->pixelMetric(QStyle::PM_TabBarTabHSpace, 0, this);
|
2013-11-27 22:27:24 +01:00
|
|
|
|
2013-11-26 14:14:36 +01:00
|
|
|
case ComboTabBar::ActiveTabMinimumWidth:
|
|
|
|
case ComboTabBar::NormalTabMinimumWidth:
|
|
|
|
case ComboTabBar::OverflowedTabWidth:
|
2013-11-27 22:27:24 +01:00
|
|
|
return 100;
|
|
|
|
|
|
|
|
case ComboTabBar::NormalTabMaximumWidth:
|
|
|
|
return 250;
|
|
|
|
|
2013-11-26 14:14:36 +01:00
|
|
|
case ComboTabBar::ExtraReservedWidth:
|
2014-03-18 17:35:44 +01:00
|
|
|
return m_tabWidget->extraReservedWidth();
|
2013-11-27 22:27:24 +01:00
|
|
|
|
2013-11-26 14:14:36 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-03-18 20:00:34 +01:00
|
|
|
WebTab* TabBar::webTab(int index) const
|
|
|
|
{
|
|
|
|
if (index == -1) {
|
|
|
|
return qobject_cast<WebTab*>(m_tabWidget->widget(currentIndex()));
|
|
|
|
}
|
|
|
|
return qobject_cast<WebTab*>(m_tabWidget->widget(index));
|
|
|
|
}
|
|
|
|
|
2012-09-03 22:40:52 +02:00
|
|
|
void TabBar::showCloseButton(int index)
|
2011-03-25 19:16:21 +01:00
|
|
|
{
|
2012-09-03 22:40:52 +02:00
|
|
|
if (!validIndex(index)) {
|
2011-03-25 19:16:21 +01:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-07-19 15:18:49 +02:00
|
|
|
|
2011-10-14 23:12:10 +02:00
|
|
|
WebTab* webTab = qobject_cast<WebTab*>(m_tabWidget->widget(index));
|
2013-02-20 21:22:38 +01:00
|
|
|
QAbstractButton* button = qobject_cast<QAbstractButton*>(tabButton(index, closeButtonPosition()));
|
2011-07-19 15:18:49 +02:00
|
|
|
|
2012-09-03 22:40:52 +02:00
|
|
|
if (button || (webTab && webTab->isPinned())) {
|
2011-07-19 15:18:49 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-07-19 15:18:49 +02:00
|
|
|
|
2013-11-26 14:14:36 +01:00
|
|
|
insertCloseButton(index);
|
2011-07-19 15:18:49 +02:00
|
|
|
}
|
|
|
|
|
2014-05-02 13:39:02 +02:00
|
|
|
void TabBar::contextMenuEvent(QContextMenuEvent* event)
|
|
|
|
{
|
|
|
|
int index = tabAt(event->pos());
|
|
|
|
|
2017-10-03 10:10:32 +02:00
|
|
|
TabContextMenu menu(index, Qt::Horizontal, m_window, m_tabWidget);
|
2014-05-02 13:39:02 +02:00
|
|
|
|
|
|
|
// Prevent choosing first option with double rightclick
|
|
|
|
const QPoint pos = event->globalPos();
|
|
|
|
QPoint p(pos.x(), pos.y() + 1);
|
|
|
|
menu.exec(p);
|
|
|
|
|
|
|
|
m_window->action(QSL("Other/RestoreClosedTab"))->setEnabled(true);
|
|
|
|
}
|
|
|
|
|
2011-07-19 15:18:49 +02:00
|
|
|
void TabBar::hideCloseButton(int index)
|
|
|
|
{
|
2012-09-03 22:40:52 +02:00
|
|
|
if (!validIndex(index) || tabsClosable()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-20 21:22:38 +01:00
|
|
|
CloseButton* button = qobject_cast<CloseButton*>(tabButton(index, closeButtonPosition()));
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!button) {
|
2011-07-19 15:18:49 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2012-09-03 22:40:52 +02:00
|
|
|
|
2013-02-20 21:22:38 +01:00
|
|
|
setTabButton(index, closeButtonPosition(), 0);
|
2012-09-03 22:40:52 +02:00
|
|
|
button->deleteLater();
|
2011-07-19 15:18:49 +02:00
|
|
|
}
|
2011-03-25 19:16:21 +01:00
|
|
|
|
2012-09-03 22:40:52 +02:00
|
|
|
void TabBar::updatePinnedTabCloseButton(int index)
|
2011-03-25 19:16:21 +01:00
|
|
|
{
|
2012-09-03 22:40:52 +02:00
|
|
|
if (!validIndex(index)) {
|
2011-03-25 19:16:21 +01:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-25 19:16:21 +01:00
|
|
|
|
2011-10-14 23:12:10 +02:00
|
|
|
WebTab* webTab = qobject_cast<WebTab*>(m_tabWidget->widget(index));
|
2013-02-20 21:22:38 +01:00
|
|
|
QAbstractButton* button = qobject_cast<QAbstractButton*>(tabButton(index, closeButtonPosition()));
|
2012-09-03 22:40:52 +02:00
|
|
|
|
|
|
|
bool pinned = webTab && webTab->isPinned();
|
|
|
|
|
|
|
|
if (pinned) {
|
|
|
|
if (button) {
|
|
|
|
button->hide();
|
|
|
|
}
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2012-09-03 22:40:52 +02:00
|
|
|
if (button) {
|
|
|
|
button->show();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
showCloseButton(index);
|
|
|
|
}
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-25 19:16:21 +01:00
|
|
|
}
|
|
|
|
|
2012-09-03 22:40:52 +02:00
|
|
|
void TabBar::closeTabFromButton()
|
|
|
|
{
|
|
|
|
QWidget* button = qobject_cast<QWidget*>(sender());
|
|
|
|
|
|
|
|
int tabToClose = -1;
|
|
|
|
|
|
|
|
for (int i = 0; i < count(); ++i) {
|
2013-02-20 21:22:38 +01:00
|
|
|
if (tabButton(i, closeButtonPosition()) == button) {
|
2012-09-03 22:40:52 +02:00
|
|
|
tabToClose = i;
|
|
|
|
break;
|
|
|
|
}
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-14 23:12:10 +02:00
|
|
|
|
2012-09-03 22:40:52 +02:00
|
|
|
if (tabToClose != -1) {
|
2015-10-12 12:04:48 +02:00
|
|
|
m_tabWidget->requestCloseTab(tabToClose);
|
2012-09-03 22:40:52 +02:00
|
|
|
}
|
2011-03-25 19:16:21 +01:00
|
|
|
}
|
|
|
|
|
2012-04-09 14:09:40 +02:00
|
|
|
void TabBar::currentTabChanged(int index)
|
|
|
|
{
|
2012-09-03 22:40:52 +02:00
|
|
|
if (!validIndex(index)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-04-09 14:09:40 +02:00
|
|
|
|
2014-03-29 13:02:17 +01:00
|
|
|
// Don't hide close buttons when dragging tabs
|
|
|
|
if (m_dragStartPosition.isNull()) {
|
|
|
|
showCloseButton(index);
|
|
|
|
hideCloseButton(m_tabWidget->lastTabIndex());
|
2013-02-09 13:00:45 +01:00
|
|
|
|
2016-12-26 14:35:25 +01:00
|
|
|
QTimer::singleShot(100, this, [this]() { ensureVisible(); });
|
2014-03-29 13:02:17 +01:00
|
|
|
}
|
2013-11-26 14:14:36 +01:00
|
|
|
|
2013-02-09 13:00:45 +01:00
|
|
|
m_tabWidget->currentTabChanged(index);
|
2012-04-09 14:09:40 +02:00
|
|
|
}
|
|
|
|
|
2014-03-18 20:00:34 +01:00
|
|
|
void TabBar::setTabText(int index, const QString &text)
|
|
|
|
{
|
|
|
|
QString tabText = text;
|
|
|
|
|
|
|
|
// Avoid Alt+letter shortcuts
|
|
|
|
tabText.replace(QLatin1Char('&'), QLatin1String("&&"));
|
|
|
|
|
|
|
|
if (WebTab* tab = webTab(index)) {
|
|
|
|
if (tab->isPinned()) {
|
|
|
|
tabText.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setTabToolTip(index, text);
|
|
|
|
ComboTabBar::setTabText(index, tabText);
|
|
|
|
}
|
|
|
|
|
2013-06-06 13:14:12 +02:00
|
|
|
void TabBar::tabInserted(int index)
|
|
|
|
{
|
|
|
|
Q_UNUSED(index)
|
|
|
|
|
2018-01-01 15:10:11 +01:00
|
|
|
setVisible(!(count() <= 1 && m_hideTabBarWithOneTab));
|
2013-06-06 13:14:12 +02:00
|
|
|
}
|
|
|
|
|
2012-09-05 19:57:36 +02:00
|
|
|
void TabBar::tabRemoved(int index)
|
|
|
|
{
|
|
|
|
Q_UNUSED(index)
|
|
|
|
|
|
|
|
showCloseButton(currentIndex());
|
2018-01-01 15:10:11 +01:00
|
|
|
setVisible(!(count() <= 1 && m_hideTabBarWithOneTab));
|
2014-05-02 20:15:05 +02:00
|
|
|
|
|
|
|
// Make sure to move add tab button to correct position when there are no normal tabs
|
|
|
|
if (normalTabsCount() == 0) {
|
|
|
|
int xForAddTabButton = cornerWidth(Qt::TopLeftCorner) + pinTabBarWidth();
|
|
|
|
if (QApplication::layoutDirection() == Qt::RightToLeft)
|
|
|
|
xForAddTabButton = width() - xForAddTabButton;
|
|
|
|
emit moveAddTabButton(xForAddTabButton);
|
|
|
|
}
|
2012-09-05 19:57:36 +02:00
|
|
|
}
|
|
|
|
|
2012-03-05 13:16:34 +01:00
|
|
|
void TabBar::mouseDoubleClickEvent(QMouseEvent* event)
|
|
|
|
{
|
|
|
|
if (mApp->plugins()->processMouseDoubleClick(Qz::ON_TabBar, this, event)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-15 23:46:10 +01:00
|
|
|
if (event->buttons() == Qt::LeftButton && emptyArea(event->pos())) {
|
2012-03-05 13:16:34 +01:00
|
|
|
m_tabWidget->addView(QUrl(), Qz::NT_SelectedTabAtTheEnd, true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-26 14:14:36 +01:00
|
|
|
ComboTabBar::mouseDoubleClickEvent(event);
|
2012-03-05 13:16:34 +01:00
|
|
|
}
|
|
|
|
|
2012-01-02 17:23:31 +01:00
|
|
|
void TabBar::mousePressEvent(QMouseEvent* event)
|
2011-12-29 21:50:03 +01:00
|
|
|
{
|
2018-01-08 12:24:20 +01:00
|
|
|
ComboTabBar::mousePressEvent(event);
|
|
|
|
|
2012-03-05 13:16:34 +01:00
|
|
|
if (mApp->plugins()->processMousePress(Qz::ON_TabBar, this, event)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-15 23:46:10 +01:00
|
|
|
if (event->buttons() == Qt::LeftButton && !emptyArea(event->pos())) {
|
2018-01-01 14:59:15 +01:00
|
|
|
m_dragStartPosition = event->pos();
|
|
|
|
} else {
|
2011-12-29 21:50:03 +01:00
|
|
|
m_dragStartPosition = QPoint();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-02 17:23:31 +01:00
|
|
|
void TabBar::mouseMoveEvent(QMouseEvent* event)
|
2011-12-29 21:50:03 +01:00
|
|
|
{
|
2018-01-08 12:24:20 +01:00
|
|
|
ComboTabBar::mouseMoveEvent(event);
|
|
|
|
|
2012-03-05 13:16:34 +01:00
|
|
|
if (mApp->plugins()->processMouseMove(Qz::ON_TabBar, this, event)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-01 14:59:15 +01:00
|
|
|
if (!m_dragStartPosition.isNull()) {
|
|
|
|
if (m_tabWidget->buttonAddTab()->isVisible()) {
|
|
|
|
int manhattanLength = (event->pos() - m_dragStartPosition).manhattanLength();
|
|
|
|
if (manhattanLength > QApplication::startDragDistance()) {
|
|
|
|
m_tabWidget->buttonAddTab()->hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int offset = 0;
|
|
|
|
const int eventY = event->pos().y();
|
|
|
|
if (eventY < 0) {
|
|
|
|
offset = qAbs(eventY);
|
|
|
|
} else if (eventY > height()) {
|
|
|
|
offset = eventY - height();
|
|
|
|
}
|
2018-01-01 15:26:58 +01:00
|
|
|
if (offset > QApplication::startDragDistance()) {
|
2018-01-01 14:59:15 +01:00
|
|
|
const QPoint global = mapToGlobal(m_dragStartPosition);
|
|
|
|
QWidget *w = QApplication::widgetAt(global);
|
|
|
|
if (w) {
|
|
|
|
QMouseEvent mouse(QEvent::MouseButtonRelease, w->mapFromGlobal(global), Qt::LeftButton, Qt::LeftButton, event->modifiers());
|
|
|
|
QApplication::sendEvent(w, &mouse);
|
|
|
|
}
|
|
|
|
QDrag *drag = new QDrag(this);
|
|
|
|
QMimeData *mime = new QMimeData;
|
|
|
|
mime->setData(MIMETYPE, QByteArray());
|
|
|
|
drag->setMimeData(mime);
|
|
|
|
drag->setPixmap(tabPixmap(currentIndex()));
|
|
|
|
if (drag->exec() == Qt::IgnoreAction) {
|
|
|
|
m_tabWidget->detachTab(currentIndex());
|
|
|
|
}
|
|
|
|
return;
|
2011-12-29 21:50:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-05 13:16:34 +01:00
|
|
|
void TabBar::mouseReleaseEvent(QMouseEvent* event)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2018-01-08 12:24:20 +01:00
|
|
|
ComboTabBar::mouseReleaseEvent(event);
|
|
|
|
|
2012-04-09 14:09:40 +02:00
|
|
|
m_dragStartPosition = QPoint();
|
|
|
|
|
2012-03-05 13:16:34 +01:00
|
|
|
if (mApp->plugins()->processMouseRelease(Qz::ON_TabBar, this, event)) {
|
2011-03-02 16:57:41 +01:00
|
|
|
return;
|
|
|
|
}
|
2011-12-15 17:43:06 +01:00
|
|
|
|
2014-03-18 20:38:30 +01:00
|
|
|
if (m_tabWidget->buttonAddTab()->isHidden() && !isMainBarOverflowed()) {
|
2017-01-28 10:31:45 +01:00
|
|
|
QTimer::singleShot(ComboTabBar::slideAnimationDuration(), m_tabWidget->buttonAddTab(), &AddTabButton::show);
|
2011-12-29 21:50:03 +01:00
|
|
|
}
|
|
|
|
|
2011-12-12 21:14:43 +01:00
|
|
|
if (!rect().contains(event->pos())) {
|
2013-11-26 14:14:36 +01:00
|
|
|
ComboTabBar::mouseReleaseEvent(event);
|
2011-12-12 21:14:43 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-19 18:47:44 +02:00
|
|
|
if (event->button() == Qt::MiddleButton) {
|
|
|
|
if (emptyArea(event->pos())) {
|
|
|
|
m_tabWidget->addView(QUrl(), Qz::NT_SelectedTabAtTheEnd, true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int id = tabAt(event->pos());
|
|
|
|
if (id != -1) {
|
2015-10-12 12:04:48 +02:00
|
|
|
m_tabWidget->requestCloseTab(id);
|
2014-04-19 18:47:44 +02:00
|
|
|
return;
|
|
|
|
}
|
2011-10-14 23:32:12 +02:00
|
|
|
}
|
2012-04-08 21:45:40 +02:00
|
|
|
}
|
|
|
|
|
2012-03-15 19:35:37 +01:00
|
|
|
void TabBar::wheelEvent(QWheelEvent* event)
|
2012-03-13 17:51:06 +01:00
|
|
|
{
|
|
|
|
if (mApp->plugins()->processWheelEvent(Qz::ON_TabBar, this, event)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-26 14:14:36 +01:00
|
|
|
ComboTabBar::wheelEvent(event);
|
2012-03-13 17:51:06 +01:00
|
|
|
}
|
|
|
|
|
2017-12-31 23:13:16 +01:00
|
|
|
enum TabDropAction {
|
|
|
|
NoAction,
|
|
|
|
SelectTab,
|
|
|
|
PrependTab,
|
|
|
|
AppendTab
|
|
|
|
};
|
|
|
|
|
2018-01-01 14:59:15 +01:00
|
|
|
static TabDropAction tabDropAction(const QPoint &pos, const QRect &tabRect, bool allowSelect)
|
2017-12-31 23:13:16 +01:00
|
|
|
{
|
|
|
|
if (!tabRect.contains(pos)) {
|
|
|
|
return NoAction;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QPoint c = tabRect.center();
|
|
|
|
const QSize csize = QSize(tabRect.width() * 0.7, tabRect.height() * 0.7);
|
|
|
|
const QRect center(c.x() - csize.width() / 2, c.y() - csize.height() / 2, csize.width(), csize.height());
|
|
|
|
|
2018-01-01 14:59:15 +01:00
|
|
|
if (allowSelect && center.contains(pos)) {
|
2017-12-31 23:13:16 +01:00
|
|
|
return SelectTab;
|
|
|
|
} else if (pos.x() < c.x()) {
|
|
|
|
return PrependTab;
|
|
|
|
} else {
|
|
|
|
return AppendTab;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-05 11:30:18 +01:00
|
|
|
void TabBar::dragEnterEvent(QDragEnterEvent* event)
|
|
|
|
{
|
|
|
|
const QMimeData* mime = event->mimeData();
|
|
|
|
|
2018-01-02 21:02:38 +01:00
|
|
|
if (mime->hasText() || mime->hasUrls() || (mime->hasFormat(MIMETYPE) && event->source())) {
|
2012-03-05 11:30:18 +01:00
|
|
|
event->acceptProposedAction();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-26 14:14:36 +01:00
|
|
|
ComboTabBar::dragEnterEvent(event);
|
2012-03-05 11:30:18 +01:00
|
|
|
}
|
|
|
|
|
2017-12-31 23:13:16 +01:00
|
|
|
void TabBar::dragMoveEvent(QDragMoveEvent *event)
|
|
|
|
{
|
|
|
|
const int index = tabAt(event->pos());
|
2018-01-01 14:59:15 +01:00
|
|
|
const QMimeData* mime = event->mimeData();
|
|
|
|
|
|
|
|
if (index == -1 || (mime->hasFormat(MIMETYPE) && event->source() == this)) {
|
2017-12-31 23:13:16 +01:00
|
|
|
ComboTabBar::dragMoveEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-01 14:59:15 +01:00
|
|
|
switch (tabDropAction(event->pos(), tabRect(index), !mime->hasFormat(MIMETYPE))) {
|
2017-12-31 23:13:16 +01:00
|
|
|
case PrependTab:
|
|
|
|
showDropIndicator(index, BeforeTab);
|
|
|
|
break;
|
|
|
|
case AppendTab:
|
|
|
|
showDropIndicator(index, AfterTab);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
clearDropIndicator();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-01 14:59:15 +01:00
|
|
|
void TabBar::dragLeaveEvent(QDragLeaveEvent *event)
|
|
|
|
{
|
|
|
|
clearDropIndicator();
|
|
|
|
|
|
|
|
ComboTabBar::dragLeaveEvent(event);
|
|
|
|
}
|
|
|
|
|
2012-03-05 11:30:18 +01:00
|
|
|
void TabBar::dropEvent(QDropEvent* event)
|
|
|
|
{
|
2017-12-31 23:13:16 +01:00
|
|
|
clearDropIndicator();
|
|
|
|
|
2012-03-05 11:30:18 +01:00
|
|
|
const QMimeData* mime = event->mimeData();
|
|
|
|
|
2018-01-01 14:59:15 +01:00
|
|
|
if (!mime->hasText() && !mime->hasUrls() && !mime->hasFormat(MIMETYPE)) {
|
2013-11-26 14:14:36 +01:00
|
|
|
ComboTabBar::dropEvent(event);
|
2012-03-05 11:30:18 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-01 14:59:15 +01:00
|
|
|
event->acceptProposedAction();
|
|
|
|
|
|
|
|
if (mime->hasFormat(MIMETYPE) && event->source() == this) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
TabBar *sourceTabBar = qobject_cast<TabBar*>(event->source());
|
|
|
|
|
2012-03-05 11:30:18 +01:00
|
|
|
int index = tabAt(event->pos());
|
|
|
|
if (index == -1) {
|
2017-12-31 19:13:25 +01:00
|
|
|
if (mime->hasUrls()) {
|
|
|
|
foreach (const QUrl &url, mime->urls()) {
|
|
|
|
m_tabWidget->addView(url, Qz::NT_SelectedTabAtTheEnd);
|
|
|
|
}
|
|
|
|
} else if (mime->hasText()) {
|
|
|
|
m_tabWidget->addView(mApp->searchEnginesManager()->searchResult(mime->text()), Qz::NT_SelectedNewEmptyTab);
|
2018-01-01 14:59:15 +01:00
|
|
|
} else if (mime->hasFormat(MIMETYPE) && sourceTabBar) {
|
|
|
|
WebTab *tab = sourceTabBar->webTab();
|
|
|
|
if (tab) {
|
|
|
|
sourceTabBar->m_tabWidget->detachTab(tab);
|
|
|
|
tab->setPinned(false);
|
|
|
|
m_tabWidget->addView(tab, Qz::NT_SelectedTab);
|
|
|
|
}
|
2012-03-05 11:30:18 +01:00
|
|
|
}
|
2018-01-01 14:59:15 +01:00
|
|
|
} else {
|
2017-12-31 23:13:16 +01:00
|
|
|
LoadRequest req;
|
2014-02-19 22:07:21 +01:00
|
|
|
WebTab* tab = m_window->weView(index)->webTab();
|
2018-01-01 14:59:15 +01:00
|
|
|
TabDropAction action = tabDropAction(event->pos(), tabRect(index), !mime->hasFormat(MIMETYPE));
|
2017-12-31 23:13:16 +01:00
|
|
|
if (mime->hasUrls()) {
|
|
|
|
req = mime->urls().at(0);
|
|
|
|
} else if (mime->hasText()) {
|
|
|
|
req = mApp->searchEnginesManager()->searchResult(mime->text());
|
|
|
|
}
|
|
|
|
if (action == SelectTab) {
|
2018-01-02 14:30:25 +01:00
|
|
|
if (tab->isRestored() && req.isValid()) {
|
2017-12-31 23:13:16 +01:00
|
|
|
tab->webView()->load(req);
|
2017-12-31 19:30:16 +01:00
|
|
|
}
|
2017-12-31 23:13:16 +01:00
|
|
|
} else if (action == PrependTab || action == AppendTab) {
|
|
|
|
const int newIndex = action == PrependTab ? index : index + 1;
|
2018-01-02 14:30:25 +01:00
|
|
|
if (req.isValid()) {
|
2018-01-01 14:59:15 +01:00
|
|
|
m_tabWidget->addView(req, QString(), Qz::NT_SelectedNewEmptyTab, false, newIndex, index < pinnedTabsCount());
|
|
|
|
} else if (mime->hasFormat(MIMETYPE) && sourceTabBar) {
|
|
|
|
WebTab *tab = sourceTabBar->webTab();
|
|
|
|
if (tab) {
|
|
|
|
sourceTabBar->m_tabWidget->detachTab(tab);
|
|
|
|
tab->setPinned(index < pinnedTabsCount());
|
|
|
|
m_tabWidget->insertView(newIndex, tab, Qz::NT_SelectedTab);
|
|
|
|
}
|
|
|
|
}
|
2012-03-11 15:17:12 +01:00
|
|
|
}
|
2012-03-05 11:30:18 +01:00
|
|
|
}
|
|
|
|
}
|