2011-03-03 18:29:20 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2013-02-09 13:00:45 +01:00
|
|
|
* Copyright (C) 2010-2013 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"
|
2012-04-08 21:45:40 +02:00
|
|
|
#include "tabpreview.h"
|
2011-03-02 16:57:41 +01:00
|
|
|
#include "qupzilla.h"
|
2011-03-25 19:16:21 +01:00
|
|
|
#include "webtab.h"
|
2011-09-30 21:44:18 +02:00
|
|
|
#include "iconprovider.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"
|
2013-03-03 20:18:34 +01:00
|
|
|
#include "proxystyle.h"
|
2012-02-29 18:33:50 +01:00
|
|
|
|
|
|
|
#include <QMenu>
|
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>
|
2011-03-25 19:16:21 +01:00
|
|
|
|
2011-10-14 23:12:10 +02:00
|
|
|
#define MAXIMUM_TAB_WIDTH 250
|
2012-08-27 14:02:35 +02:00
|
|
|
#define MINIMUM_TAB_WIDTH 125
|
2011-12-28 13:16:04 +01:00
|
|
|
|
2011-10-18 21:07:58 +02:00
|
|
|
TabBar::TabBar(QupZilla* mainClass, TabWidget* tabWidget)
|
|
|
|
: QTabBar()
|
2011-10-14 23:12:10 +02:00
|
|
|
, p_QupZilla(mainClass)
|
2011-10-18 21:07:58 +02:00
|
|
|
, m_tabWidget(tabWidget)
|
2012-04-08 21:45:40 +02:00
|
|
|
, m_tabPreview(new TabPreview(mainClass, tabWidget))
|
2012-08-27 14:02:35 +02:00
|
|
|
, m_showTabPreviews(false)
|
2011-10-14 23:12:10 +02:00
|
|
|
, m_clickedTab(0)
|
|
|
|
, m_pinnedTabsCount(0)
|
2011-10-23 14:44:18 +02:00
|
|
|
, m_normalTabWidth(0)
|
2012-12-10 00:26:25 +01:00
|
|
|
, m_activeTabWidth(0)
|
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
|
|
|
setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
|
setElideMode(Qt::ElideRight);
|
|
|
|
setDocumentMode(true);
|
2011-10-24 17:46:45 +02:00
|
|
|
setFocusPolicy(Qt::NoFocus);
|
2012-09-03 22:40:52 +02:00
|
|
|
setTabsClosable(true);
|
2012-04-08 21:45:40 +02:00
|
|
|
setMouseTracking(true);
|
2012-06-27 00:14:51 +02:00
|
|
|
setMovable(true);
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2012-03-05 11:30:18 +01:00
|
|
|
setAcceptDrops(true);
|
|
|
|
|
2012-04-09 14:09:40 +02:00
|
|
|
connect(this, SIGNAL(currentChanged(int)), this, SLOT(currentTabChanged(int)));
|
2011-03-02 16:57:41 +01:00
|
|
|
connect(this, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(contextMenuRequested(const QPoint &)));
|
2011-10-14 23:12:10 +02:00
|
|
|
connect(m_tabWidget, SIGNAL(pinnedTabClosed()), this, SLOT(pinnedTabClosed()));
|
|
|
|
connect(m_tabWidget, SIGNAL(pinnedTabAdded()), this, SLOT(pinnedTabAdded()));
|
2012-04-09 14:09:40 +02:00
|
|
|
|
|
|
|
m_tabPreviewTimer = new QTimer(this);
|
|
|
|
m_tabPreviewTimer->setInterval(200);
|
|
|
|
m_tabPreviewTimer->setSingleShot(true);
|
|
|
|
connect(m_tabPreviewTimer, SIGNAL(timeout()), m_tabPreview, SLOT(hideAnimated()));
|
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");
|
|
|
|
|
2012-04-10 18:29:22 +02:00
|
|
|
m_tabPreview->setAnimationsEnabled(settings.value("tabPreviewAnimationsEnabled", true).toBool());
|
|
|
|
m_showTabPreviews = settings.value("showTabPreviews", true).toBool();
|
2012-08-27 14:02:35 +02:00
|
|
|
bool activateLastTab = settings.value("ActivateLastTabWhenClosingActual", false).toBool();
|
2012-04-04 18:48:54 +02:00
|
|
|
|
2012-08-27 14:02:35 +02:00
|
|
|
setSelectionBehaviorOnRemove(activateLastTab ? QTabBar::SelectPreviousTab : QTabBar::SelectRightTab);
|
2011-03-02 16:57:41 +01:00
|
|
|
|
|
|
|
settings.endGroup();
|
|
|
|
}
|
|
|
|
|
2011-10-24 17:46:45 +02:00
|
|
|
void TabBar::updateVisibilityWithFullscreen(bool visible)
|
|
|
|
{
|
2013-02-09 15:44:17 +01:00
|
|
|
// It is needed to save original geometry, otherwise
|
|
|
|
// tabbar will get 3px height in fullscreen once it was hidden
|
|
|
|
QTabBar::setVisible(visible);
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (visible) {
|
2013-02-09 15:44:17 +01:00
|
|
|
setGeometry(m_originalGeometry);
|
2011-10-24 17:46:45 +02:00
|
|
|
emit showButtons();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2013-02-09 15:44:17 +01:00
|
|
|
m_originalGeometry = geometry();
|
2011-10-24 17:46:45 +02:00
|
|
|
emit hideButtons();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-24 17:46:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TabBar::setVisible(bool visible)
|
|
|
|
{
|
|
|
|
if (visible) {
|
2011-11-06 17:01:23 +01:00
|
|
|
if (p_QupZilla->isFullScreen()) {
|
2011-10-24 17:46:45 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-24 17:46:45 +02:00
|
|
|
|
|
|
|
emit showButtons();
|
|
|
|
}
|
|
|
|
else {
|
2013-02-09 15:44:17 +01:00
|
|
|
m_originalGeometry = geometry();
|
2011-10-24 17:46:45 +02:00
|
|
|
emit hideButtons();
|
|
|
|
}
|
|
|
|
|
2012-04-09 19:45:00 +02:00
|
|
|
hideTabPreview(false);
|
2012-04-08 21:45:40 +02:00
|
|
|
|
2011-10-24 17:46:45 +02:00
|
|
|
QTabBar::setVisible(visible);
|
|
|
|
}
|
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
void TabBar::contextMenuRequested(const QPoint &position)
|
|
|
|
{
|
2011-03-25 19:16:21 +01:00
|
|
|
int index = tabAt(position);
|
|
|
|
m_clickedTab = index;
|
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
QMenu menu;
|
2012-12-08 14:10:30 +01:00
|
|
|
menu.addAction(QIcon(":/icons/menu/new-tab.png"), tr("&New tab"), p_QupZilla, SLOT(addTab()));
|
2011-03-02 16:57:41 +01:00
|
|
|
menu.addSeparator();
|
2011-11-06 17:01:23 +01:00
|
|
|
if (index != -1) {
|
2011-10-14 23:12:10 +02:00
|
|
|
WebTab* webTab = qobject_cast<WebTab*>(m_tabWidget->widget(m_clickedTab));
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!webTab) {
|
2011-03-26 13:34:08 +01:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
if (p_QupZilla->weView(m_clickedTab)->isLoading()) {
|
2012-04-22 20:51:28 +02:00
|
|
|
menu.addAction(qIconProvider->standardIcon(QStyle::SP_BrowserStop), tr("&Stop Tab"), this, SLOT(stopTab()));
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2012-04-22 20:51:28 +02:00
|
|
|
menu.addAction(qIconProvider->standardIcon(QStyle::SP_BrowserReload), tr("&Reload Tab"), this, SLOT(reloadTab()));
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-09-30 21:44:18 +02:00
|
|
|
|
2011-04-09 00:22:50 +02:00
|
|
|
menu.addAction(tr("&Duplicate Tab"), this, SLOT(duplicateTab()));
|
|
|
|
menu.addAction(webTab->isPinned() ? tr("Un&pin Tab") : tr("&Pin Tab"), this, SLOT(pinTab()));
|
2011-03-25 19:16:21 +01:00
|
|
|
menu.addSeparator();
|
2011-10-14 23:12:10 +02:00
|
|
|
menu.addAction(tr("Re&load All Tabs"), m_tabWidget, SLOT(reloadAllTabs()));
|
2011-04-09 00:22:50 +02:00
|
|
|
menu.addAction(tr("&Bookmark This Tab"), this, SLOT(bookmarkTab()));
|
|
|
|
menu.addAction(tr("Bookmark &All Tabs"), p_QupZilla, SLOT(bookmarkAllTabs()));
|
2011-03-02 16:57:41 +01:00
|
|
|
menu.addSeparator();
|
2011-05-07 12:59:53 +02:00
|
|
|
QAction* action = p_QupZilla->actionRestoreTab();
|
2011-12-12 21:14:43 +01:00
|
|
|
action->setEnabled(m_tabWidget->canRestoreTab());
|
2011-05-07 12:59:53 +02:00
|
|
|
menu.addAction(action);
|
2011-03-02 16:57:41 +01:00
|
|
|
menu.addSeparator();
|
2011-04-09 00:22:50 +02:00
|
|
|
menu.addAction(tr("Close Ot&her Tabs"), this, SLOT(closeAllButCurrent()));
|
2011-11-06 17:01:23 +01:00
|
|
|
menu.addAction(QIcon::fromTheme("window-close"), tr("Cl&ose"), this, SLOT(closeTab()));
|
2011-03-02 16:57:41 +01:00
|
|
|
menu.addSeparator();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2011-10-14 23:12:10 +02:00
|
|
|
menu.addAction(tr("Reloa&d All Tabs"), m_tabWidget, SLOT(reloadAllTabs()));
|
2011-04-09 00:22:50 +02:00
|
|
|
menu.addAction(tr("Bookmark &All Ta&bs"), p_QupZilla, SLOT(bookmarkAllTabs()));
|
2011-03-02 16:57:41 +01:00
|
|
|
menu.addSeparator();
|
2011-11-06 17:01:23 +01:00
|
|
|
QAction* action = menu.addAction(QIcon::fromTheme("user-trash"), tr("Restore &Closed Tab"), m_tabWidget, SLOT(restoreClosedTab()));
|
2011-12-12 21:14:43 +01:00
|
|
|
action->setEnabled(m_tabWidget->canRestoreTab());
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-09-02 14:18:07 +02:00
|
|
|
// Prevent choosing first option with double rightclick
|
2012-03-09 16:02:27 +01:00
|
|
|
const QPoint &pos = mapToGlobal(position);
|
2011-11-06 17:01:23 +01:00
|
|
|
QPoint p(pos.x(), pos.y() + 1);
|
2011-03-02 16:57:41 +01:00
|
|
|
menu.exec(p);
|
2012-02-04 19:43:43 +01:00
|
|
|
|
2011-05-07 12:59:53 +02:00
|
|
|
p_QupZilla->actionRestoreTab()->setEnabled(true);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2011-03-25 19:16:21 +01:00
|
|
|
QSize TabBar::tabSizeHint(int index) const
|
|
|
|
{
|
2013-03-03 20:18:34 +01:00
|
|
|
if (!isVisible() || !mApp->proxyStyle()) {
|
2012-09-02 14:18:07 +02:00
|
|
|
// Don't calculate it when tabbar is not visible
|
|
|
|
// It produces invalid size anyway
|
2013-03-03 20:18:34 +01:00
|
|
|
//
|
|
|
|
// We also need ProxyStyle to be set before calculating minimum sizes for tabs
|
2012-09-02 14:18:07 +02:00
|
|
|
return QSize(-1, -1);
|
|
|
|
}
|
|
|
|
|
2012-09-02 12:22:11 +02:00
|
|
|
static int PINNED_TAB_WIDTH = -1;
|
2012-12-10 00:26:25 +01:00
|
|
|
static int MINIMUM_ACTIVE_TAB_WIDTH = -1;
|
2012-09-02 12:22:11 +02:00
|
|
|
|
|
|
|
if (PINNED_TAB_WIDTH == -1) {
|
2013-03-03 20:18:34 +01:00
|
|
|
PINNED_TAB_WIDTH = 16 + mApp->proxyStyle()->pixelMetric(QStyle::PM_TabBarTabHSpace, 0, this);
|
|
|
|
MINIMUM_ACTIVE_TAB_WIDTH = PINNED_TAB_WIDTH + mApp->proxyStyle()->pixelMetric(QStyle::PM_TabCloseIndicatorWidth, 0, this);
|
|
|
|
|
|
|
|
// We want to be sure buttonAddTab and buttonListTabs can't cover the active tab
|
2012-12-10 00:26:25 +01:00
|
|
|
MINIMUM_ACTIVE_TAB_WIDTH = qMax(MINIMUM_ACTIVE_TAB_WIDTH, 6 + m_tabWidget->buttonListTabs()->width() + m_tabWidget->buttonAddTab()->width());
|
2012-09-02 12:22:11 +02:00
|
|
|
}
|
|
|
|
|
2011-03-25 19:16:21 +01:00
|
|
|
QSize size = QTabBar::tabSizeHint(index);
|
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);
|
2012-12-10 00:26:25 +01:00
|
|
|
bool adjustingActiveTab = false;
|
2011-10-14 23:12:10 +02:00
|
|
|
|
|
|
|
if (webTab && webTab->isPinned()) {
|
|
|
|
size.setWidth(PINNED_TAB_WIDTH);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2011-10-23 14:44:18 +02:00
|
|
|
int availableWidth = width() - (PINNED_TAB_WIDTH * m_pinnedTabsCount) - m_tabWidget->buttonListTabs()->width() - m_tabWidget->buttonAddTab()->width();
|
2013-01-19 16:34:19 +01:00
|
|
|
if (availableWidth < 0) {
|
|
|
|
return QSize(-1, -1);
|
|
|
|
}
|
|
|
|
|
2011-10-14 23:12:10 +02:00
|
|
|
int normalTabsCount = count() - m_pinnedTabsCount;
|
2011-10-23 14:44:18 +02:00
|
|
|
if (availableWidth >= MAXIMUM_TAB_WIDTH * normalTabsCount) {
|
2012-04-21 22:19:35 +02:00
|
|
|
m_normalTabWidth = MAXIMUM_TAB_WIDTH;
|
2011-10-23 14:44:18 +02:00
|
|
|
size.setWidth(m_normalTabWidth);
|
|
|
|
}
|
2013-03-02 18:15:05 +01:00
|
|
|
else {
|
2012-08-27 14:02:35 +02:00
|
|
|
int maxWidthForTab = availableWidth / normalTabsCount;
|
2013-03-02 18:15:05 +01:00
|
|
|
int realTabWidth = maxWidthForTab;
|
|
|
|
if (realTabWidth < MINIMUM_ACTIVE_TAB_WIDTH) {
|
2012-12-10 00:26:25 +01:00
|
|
|
maxWidthForTab = (availableWidth - MINIMUM_ACTIVE_TAB_WIDTH) / (normalTabsCount - 1);
|
2013-03-02 18:15:05 +01:00
|
|
|
realTabWidth = MINIMUM_ACTIVE_TAB_WIDTH;
|
2012-12-10 00:26:25 +01:00
|
|
|
adjustingActiveTab = true;
|
|
|
|
}
|
|
|
|
|
2013-03-02 18:15:05 +01:00
|
|
|
bool tryAdjusting = false;
|
|
|
|
|
|
|
|
if (availableWidth < MINIMUM_TAB_WIDTH * normalTabsCount) {
|
|
|
|
// Tabs don't fit at all in tabbar even with MINIMUM_TAB_WIDTH
|
|
|
|
// We will try to use as low width of tabs as possible
|
|
|
|
// to try avoid overflowing tabs into tabbar buttons
|
|
|
|
if (maxWidthForTab < PINNED_TAB_WIDTH) {
|
|
|
|
// FIXME: It overflows now
|
|
|
|
m_normalTabWidth = PINNED_TAB_WIDTH;
|
|
|
|
if (index == currentIndex()) {
|
|
|
|
size.setWidth(realTabWidth);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
size.setWidth(m_normalTabWidth);
|
|
|
|
}
|
2012-12-10 00:26:25 +01:00
|
|
|
}
|
|
|
|
else {
|
2013-03-02 18:15:05 +01:00
|
|
|
tryAdjusting = true;
|
|
|
|
|
|
|
|
if (tabsClosable()) {
|
|
|
|
// Hiding close buttons to save some space
|
|
|
|
tabBar->setTabsClosable(false);
|
|
|
|
|
|
|
|
tabBar->showCloseButton(currentIndex());
|
|
|
|
}
|
2012-12-10 00:26:25 +01:00
|
|
|
}
|
2012-08-27 14:02:35 +02:00
|
|
|
}
|
|
|
|
else {
|
2013-03-02 18:15:05 +01:00
|
|
|
// Tabs fit into tabbar with size between MAXIMUM_TAB_WIDTH and MINIMUM_TAB_WIDTH
|
|
|
|
// There won't be any empty space in tabbar
|
|
|
|
tryAdjusting = true;
|
|
|
|
|
|
|
|
// Restore close buttons according to preferences
|
|
|
|
if (!tabsClosable()) {
|
|
|
|
tabBar->setTabsClosable(true);
|
|
|
|
|
|
|
|
// Hide close buttons on pinned tabs
|
|
|
|
for (int i = 0; i < count(); ++i) {
|
|
|
|
tabBar->updatePinnedTabCloseButton(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
if (index == currentIndex()) {
|
2012-12-11 09:54:40 +01:00
|
|
|
if (adjustingActiveTab) {
|
2013-03-02 23:18:49 +01:00
|
|
|
m_activeTabWidth = (availableWidth - MINIMUM_ACTIVE_TAB_WIDTH
|
|
|
|
- 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
|
|
|
adjustingActiveTab = true;
|
|
|
|
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
|
|
|
}
|
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) {
|
|
|
|
WebTab* currentTab = qobject_cast<WebTab*>(m_tabWidget->widget(currentIndex()));
|
2011-10-23 14:44:18 +02:00
|
|
|
int xForAddTabButton = (PINNED_TAB_WIDTH * m_pinnedTabsCount) + (count() - m_pinnedTabsCount) * (m_normalTabWidth);
|
2013-03-02 18:15:05 +01:00
|
|
|
|
|
|
|
if (currentTab && !currentTab->isPinned() && 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
|
|
|
|
|
|
|
// RTL Support
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2012-09-03 22:40:52 +02:00
|
|
|
QAbstractButton* closeButton = new CloseButton(this);
|
|
|
|
connect(closeButton, SIGNAL(clicked()), this, SLOT(closeTabFromButton()));
|
2013-02-20 21:22:38 +01:00
|
|
|
setTabButton(index, closeButtonPosition(), closeButton);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void TabBar::closeCurrentTab()
|
|
|
|
{
|
2012-09-03 22:40:52 +02:00
|
|
|
m_tabWidget->closeTab(currentIndex());
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
m_tabWidget->closeTab(tabToClose);
|
|
|
|
}
|
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
|
|
|
|
|
|
|
hideTabPreview(false);
|
2012-09-03 22:40:52 +02:00
|
|
|
|
|
|
|
showCloseButton(index);
|
|
|
|
hideCloseButton(m_tabWidget->lastTabIndex());
|
2013-02-09 13:00:45 +01:00
|
|
|
|
|
|
|
m_tabWidget->currentTabChanged(index);
|
2012-04-09 14:09:40 +02:00
|
|
|
}
|
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
void TabBar::bookmarkTab()
|
|
|
|
{
|
2012-02-04 19:43:43 +01:00
|
|
|
TabbedWebView* view = p_QupZilla->weView(m_clickedTab);
|
|
|
|
if (!view) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-11 15:17:12 +01:00
|
|
|
WebTab* tab = view->webTab();
|
|
|
|
|
|
|
|
p_QupZilla->addBookmark(tab->url(), tab->title(), tab->icon());
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2011-03-25 19:16:21 +01:00
|
|
|
void TabBar::pinTab()
|
|
|
|
{
|
2011-10-14 23:12:10 +02:00
|
|
|
WebTab* webTab = qobject_cast<WebTab*>(m_tabWidget->widget(m_clickedTab));
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!webTab) {
|
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
|
|
|
|
|
|
|
webTab->pinTab(m_clickedTab);
|
2011-10-14 23:12:10 +02:00
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (webTab->isPinned()) {
|
2011-10-14 23:12:10 +02:00
|
|
|
m_pinnedTabsCount++;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2011-10-14 23:12:10 +02:00
|
|
|
m_pinnedTabsCount--;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-23 14:44:18 +02:00
|
|
|
|
2012-03-14 14:04:20 +01:00
|
|
|
// We need to recalculate size of all tabs and repaint tabbar
|
|
|
|
// Unfortunately, Qt doesn't offer refresh() function as a public API
|
|
|
|
|
|
|
|
// So we are calling the lightest function that calls d->refresh()
|
|
|
|
setElideMode(elideMode());
|
2011-10-14 23:12:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TabBar::pinnedTabClosed()
|
|
|
|
{
|
|
|
|
m_pinnedTabsCount--;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TabBar::pinnedTabAdded()
|
|
|
|
{
|
|
|
|
m_pinnedTabsCount++;
|
2011-03-25 19:16:21 +01:00
|
|
|
}
|
|
|
|
|
2011-11-05 15:04:29 +01:00
|
|
|
int TabBar::pinnedTabsCount()
|
|
|
|
{
|
|
|
|
return m_pinnedTabsCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TabBar::normalTabsCount()
|
|
|
|
{
|
|
|
|
return count() - m_pinnedTabsCount;
|
|
|
|
}
|
|
|
|
|
2013-02-20 21:22:38 +01:00
|
|
|
QTabBar::ButtonPosition TabBar::iconButtonPosition()
|
|
|
|
{
|
|
|
|
return (closeButtonPosition() == QTabBar::RightSide ? QTabBar::LeftSide : QTabBar::RightSide);
|
|
|
|
}
|
|
|
|
|
|
|
|
QTabBar::ButtonPosition TabBar::closeButtonPosition()
|
|
|
|
{
|
|
|
|
return (QTabBar::ButtonPosition)style()->styleHint(QStyle::SH_TabBar_CloseButtonPosition, 0, this);
|
|
|
|
}
|
|
|
|
|
2012-04-08 21:45:40 +02:00
|
|
|
void TabBar::showTabPreview()
|
|
|
|
{
|
2012-04-09 19:45:00 +02:00
|
|
|
WebTab* webTab = qobject_cast<WebTab*>(m_tabWidget->widget(m_tabPreview->previewIndex()));
|
2012-04-08 21:45:40 +02:00
|
|
|
if (!webTab) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-09 14:09:40 +02:00
|
|
|
m_tabPreviewTimer->stop();
|
2012-04-09 19:45:00 +02:00
|
|
|
m_tabPreview->setWebTab(webTab, m_tabPreview->previewIndex() == currentIndex());
|
|
|
|
m_tabPreview->showOnRect(tabRect(m_tabPreview->previewIndex()));
|
2012-04-08 21:45:40 +02:00
|
|
|
}
|
|
|
|
|
2012-04-09 14:09:40 +02:00
|
|
|
void TabBar::hideTabPreview(bool delayed)
|
2012-04-08 21:45:40 +02:00
|
|
|
{
|
2012-04-09 14:09:40 +02:00
|
|
|
if (delayed) {
|
|
|
|
m_tabPreviewTimer->start();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_tabPreview->hideAnimated();
|
|
|
|
}
|
2012-04-08 21:45:40 +02:00
|
|
|
}
|
|
|
|
|
2012-09-05 19:57:36 +02:00
|
|
|
void TabBar::tabRemoved(int index)
|
|
|
|
{
|
|
|
|
Q_UNUSED(index)
|
|
|
|
|
2013-02-09 13:00:45 +01:00
|
|
|
m_tabWidget->showNavigationBar(p_QupZilla->navigationContainer());
|
2012-09-05 19:57:36 +02:00
|
|
|
showCloseButton(currentIndex());
|
|
|
|
}
|
|
|
|
|
2012-03-05 13:16:34 +01:00
|
|
|
void TabBar::mouseDoubleClickEvent(QMouseEvent* event)
|
|
|
|
{
|
|
|
|
if (mApp->plugins()->processMouseDoubleClick(Qz::ON_TabBar, this, event)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event->button() == Qt::LeftButton && tabAt(event->pos()) == -1) {
|
|
|
|
m_tabWidget->addView(QUrl(), Qz::NT_SelectedTabAtTheEnd, true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTabBar::mouseDoubleClickEvent(event);
|
|
|
|
}
|
|
|
|
|
2012-01-02 17:23:31 +01:00
|
|
|
void TabBar::mousePressEvent(QMouseEvent* event)
|
2011-12-29 21:50:03 +01:00
|
|
|
{
|
2012-04-09 19:45:00 +02:00
|
|
|
hideTabPreview(false);
|
|
|
|
|
2012-03-05 13:16:34 +01:00
|
|
|
if (mApp->plugins()->processMousePress(Qz::ON_TabBar, this, event)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-29 21:50:03 +01:00
|
|
|
if (event->buttons() & Qt::LeftButton && tabAt(event->pos()) != -1) {
|
|
|
|
m_dragStartPosition = mapFromGlobal(event->globalPos());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_dragStartPosition = QPoint();
|
|
|
|
}
|
|
|
|
|
|
|
|
QTabBar::mousePressEvent(event);
|
|
|
|
}
|
|
|
|
|
2012-01-02 17:23:31 +01:00
|
|
|
void TabBar::mouseMoveEvent(QMouseEvent* event)
|
2011-12-29 21:50:03 +01:00
|
|
|
{
|
2012-03-05 13:16:34 +01:00
|
|
|
if (mApp->plugins()->processMouseMove(Qz::ON_TabBar, this, event)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-29 21:50:03 +01:00
|
|
|
if (!m_dragStartPosition.isNull() && m_tabWidget->buttonAddTab()->isVisible()) {
|
|
|
|
int manhattanLength = (event->pos() - m_dragStartPosition).manhattanLength();
|
|
|
|
if (manhattanLength > QApplication::startDragDistance()) {
|
|
|
|
m_tabWidget->buttonAddTab()->hide();
|
2012-04-09 14:09:40 +02:00
|
|
|
hideTabPreview();
|
2011-12-29 21:50:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-09 15:44:17 +01:00
|
|
|
// Tab Preview
|
2012-04-08 21:45:40 +02:00
|
|
|
|
|
|
|
const int tab = tabAt(event->pos());
|
|
|
|
|
2012-04-09 19:45:00 +02:00
|
|
|
if (tab != -1 && tab != m_tabPreview->previewIndex() && event->buttons() == Qt::NoButton && m_dragStartPosition.isNull()) {
|
|
|
|
m_tabPreview->setPreviewIndex(tab);
|
2012-04-08 21:45:40 +02:00
|
|
|
if (m_tabPreview->isVisible()) {
|
|
|
|
showTabPreview();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-29 21:50:03 +01:00
|
|
|
QTabBar::mouseMoveEvent(event);
|
|
|
|
}
|
|
|
|
|
2012-03-05 13:16:34 +01:00
|
|
|
void TabBar::mouseReleaseEvent(QMouseEvent* event)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
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
|
|
|
|
2011-12-29 21:50:03 +01:00
|
|
|
if (m_tabWidget->buttonAddTab()->isHidden()) {
|
|
|
|
QTimer::singleShot(500, m_tabWidget->buttonAddTab(), SLOT(show()));
|
|
|
|
}
|
|
|
|
|
2011-12-12 21:14:43 +01:00
|
|
|
if (!rect().contains(event->pos())) {
|
2011-12-15 17:03:50 +01:00
|
|
|
QTabBar::mouseReleaseEvent(event);
|
2011-12-12 21:14:43 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-14 23:32:12 +02:00
|
|
|
int id = tabAt(event->pos());
|
2011-12-12 21:14:43 +01:00
|
|
|
if (id != -1 && event->button() == Qt::MiddleButton) {
|
2011-10-17 23:34:57 +02:00
|
|
|
m_tabWidget->closeTab(id);
|
|
|
|
return;
|
|
|
|
}
|
2011-12-12 21:14:43 +01:00
|
|
|
if (id == -1 && event->button() == Qt::MiddleButton) {
|
2012-01-26 20:19:56 +01:00
|
|
|
m_tabWidget->addView(QUrl(), Qz::NT_SelectedTabAtTheEnd, true);
|
2011-10-14 23:32:12 +02:00
|
|
|
return;
|
|
|
|
}
|
2011-12-15 17:43:06 +01:00
|
|
|
|
2011-12-12 21:14:43 +01:00
|
|
|
QTabBar::mouseReleaseEvent(event);
|
2012-04-08 21:45:40 +02:00
|
|
|
}
|
|
|
|
|
2012-04-10 18:29:22 +02:00
|
|
|
bool TabBar::event(QEvent* event)
|
2012-04-08 21:45:40 +02:00
|
|
|
{
|
2012-04-10 18:29:22 +02:00
|
|
|
switch (event->type()) {
|
|
|
|
case QEvent::Leave:
|
2012-04-09 19:45:00 +02:00
|
|
|
hideTabPreview();
|
2012-04-10 18:29:22 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case QEvent::ToolTip:
|
|
|
|
if (m_showTabPreviews) {
|
2013-02-09 15:44:17 +01:00
|
|
|
QHelpEvent* ev = static_cast<QHelpEvent*>(event);
|
|
|
|
if (tabAt(ev->pos()) != -1 && !m_tabPreview->isVisible()) {
|
2012-04-10 18:29:22 +02:00
|
|
|
showTabPreview();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2012-04-09 19:45:00 +02:00
|
|
|
}
|
2012-04-08 21:45:40 +02:00
|
|
|
|
2012-04-09 19:45:00 +02:00
|
|
|
return QTabBar::event(event);
|
2011-10-14 23:32:12 +02:00
|
|
|
}
|
2012-01-14 11:46:06 +01: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTabBar::wheelEvent(event);
|
|
|
|
}
|
|
|
|
|
2012-03-05 11:30:18 +01:00
|
|
|
void TabBar::dragEnterEvent(QDragEnterEvent* event)
|
|
|
|
{
|
|
|
|
const QMimeData* mime = event->mimeData();
|
|
|
|
|
|
|
|
if (mime->hasUrls()) {
|
|
|
|
event->acceptProposedAction();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTabBar::dragEnterEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TabBar::dropEvent(QDropEvent* event)
|
|
|
|
{
|
|
|
|
const QMimeData* mime = event->mimeData();
|
|
|
|
|
|
|
|
if (!mime->hasUrls()) {
|
|
|
|
QTabBar::dropEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int index = tabAt(event->pos());
|
|
|
|
if (index == -1) {
|
|
|
|
foreach(const QUrl & url, mime->urls()) {
|
|
|
|
m_tabWidget->addView(url, Qz::NT_SelectedTabAtTheEnd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2012-03-11 15:17:12 +01:00
|
|
|
WebTab* tab = p_QupZilla->weView(index)->webTab();
|
|
|
|
if (tab->isRestored()) {
|
2012-03-28 16:42:50 +02:00
|
|
|
tab->view()->load(mime->urls().at(0));
|
2012-03-11 15:17:12 +01:00
|
|
|
}
|
2012-03-05 11:30:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-14 11:46:06 +01:00
|
|
|
void TabBar::disconnectObjects()
|
|
|
|
{
|
|
|
|
disconnect(this);
|
|
|
|
}
|
2012-09-03 22:40:52 +02:00
|
|
|
|
|
|
|
CloseButton::CloseButton(QWidget* parent)
|
|
|
|
: QAbstractButton(parent)
|
|
|
|
{
|
|
|
|
setFocusPolicy(Qt::NoFocus);
|
|
|
|
setCursor(Qt::ArrowCursor);
|
|
|
|
setToolTip(QupZilla::tr("Close Tab"));
|
|
|
|
|
|
|
|
resize(sizeHint());
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize CloseButton::sizeHint() const
|
|
|
|
{
|
|
|
|
ensurePolished();
|
|
|
|
int width = style()->pixelMetric(QStyle::PM_TabCloseIndicatorWidth, 0, this);
|
|
|
|
int height = style()->pixelMetric(QStyle::PM_TabCloseIndicatorHeight, 0, this);
|
|
|
|
return QSize(width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloseButton::enterEvent(QEvent* event)
|
|
|
|
{
|
|
|
|
if (isEnabled()) {
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
QAbstractButton::enterEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloseButton::leaveEvent(QEvent* event)
|
|
|
|
{
|
|
|
|
if (isEnabled()) {
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
QAbstractButton::leaveEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloseButton::paintEvent(QPaintEvent*)
|
|
|
|
{
|
|
|
|
QPainter p(this);
|
|
|
|
QStyleOption opt;
|
|
|
|
opt.init(this);
|
|
|
|
opt.state |= QStyle::State_AutoRaise;
|
|
|
|
|
|
|
|
if (isEnabled() && underMouse() && !isChecked() && !isDown()) {
|
|
|
|
opt.state |= QStyle::State_Raised;
|
|
|
|
}
|
|
|
|
if (isChecked()) {
|
|
|
|
opt.state |= QStyle::State_On;
|
|
|
|
}
|
|
|
|
if (isDown()) {
|
|
|
|
opt.state |= QStyle::State_Sunken;
|
|
|
|
}
|
|
|
|
|
2013-02-20 21:22:38 +01:00
|
|
|
if (TabBar* tb = qobject_cast<TabBar*>(parent())) {
|
2012-09-03 22:40:52 +02:00
|
|
|
int index = tb->currentIndex();
|
2013-02-20 21:22:38 +01:00
|
|
|
if (tb->tabButton(index, tb->closeButtonPosition()) == this) {
|
2012-09-03 22:40:52 +02:00
|
|
|
opt.state |= QStyle::State_Selected;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
style()->drawPrimitive(QStyle::PE_IndicatorTabClose, &opt, &p, this);
|
|
|
|
}
|