1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 17:42:10 +01:00
falkonOfficial/src/lib/webview/tabbar.cpp

589 lines
16 KiB
C++
Raw Normal View History

2011-03-03 18:29:20 +01:00
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2010-2012 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"
#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"
#include "iconprovider.h"
#include "toolbutton.h"
#include "settings.h"
#include "tabbedwebview.h"
2012-03-05 13:16:34 +01:00
#include "mainapplication.h"
#include "pluginproxy.h"
#include <QMenu>
2012-08-31 15:19:07 +02:00
#include <QMouseEvent>
#include <QApplication>
#include <QTimer>
#include <QRect>
2011-03-25 19:16:21 +01:00
#define MAXIMUM_TAB_WIDTH 250
#define MINIMUM_TAB_WIDTH 125
TabBar::TabBar(QupZilla* mainClass, TabWidget* tabWidget)
: QTabBar()
, p_QupZilla(mainClass)
, m_tabWidget(tabWidget)
, m_tabPreview(new TabPreview(mainClass, tabWidget))
, m_showTabPreviews(false)
, m_showCloseButtons(false)
, m_clickedTab(0)
, m_pinnedTabsCount(0)
, m_normalTabWidth(0)
, m_lastTabWidth(0)
2011-03-02 16:57:41 +01:00
{
setObjectName("tabbar");
2011-03-02 16:57:41 +01:00
setContextMenuPolicy(Qt::CustomContextMenu);
setElideMode(Qt::ElideRight);
setDocumentMode(true);
setFocusPolicy(Qt::NoFocus);
setMouseTracking(true);
setMovable(true);
2011-03-02 16:57:41 +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 &)));
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()
{
Settings settings;
2011-03-02 16:57:41 +01:00
settings.beginGroup("Browser-Tabs-Settings");
m_tabPreview->setAnimationsEnabled(settings.value("tabPreviewAnimationsEnabled", true).toBool());
m_showTabPreviews = settings.value("showTabPreviews", true).toBool();
m_showCloseButtons = settings.value("showCloseButtonOnTabs", true).toBool();
bool activateLastTab = settings.value("ActivateLastTabWhenClosingActual", false).toBool();
setTabsClosable(m_showCloseButtons);
setSelectionBehaviorOnRemove(activateLastTab ? QTabBar::SelectPreviousTab : QTabBar::SelectRightTab);
2011-03-02 16:57:41 +01:00
settings.endGroup();
}
void TabBar::updateVisibilityWithFullscreen(bool visible)
{
if (visible) {
emit showButtons();
}
else {
emit hideButtons();
}
QTabBar::setVisible(visible);
}
void TabBar::setVisible(bool visible)
{
if (visible) {
if (p_QupZilla->isFullScreen()) {
return;
}
emit showButtons();
}
else {
emit hideButtons();
}
hideTabPreview(false);
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;
menu.addAction(QIcon(":/icons/menu/popup.png"), tr("&New tab"), p_QupZilla, SLOT(addTab()));
2011-03-02 16:57:41 +01:00
menu.addSeparator();
if (index != -1) {
WebTab* webTab = qobject_cast<WebTab*>(m_tabWidget->widget(m_clickedTab));
if (!webTab) {
return;
}
if (p_QupZilla->weView(m_clickedTab)->isLoading()) {
menu.addAction(qIconProvider->standardIcon(QStyle::SP_BrowserStop), tr("&Stop Tab"), this, SLOT(stopTab()));
}
else {
menu.addAction(qIconProvider->standardIcon(QStyle::SP_BrowserReload), tr("&Reload Tab"), this, SLOT(reloadTab()));
}
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();
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();
QAction* action = p_QupZilla->actionRestoreTab();
action->setEnabled(m_tabWidget->canRestoreTab());
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()));
menu.addAction(QIcon::fromTheme("window-close"), tr("Cl&ose"), this, SLOT(closeTab()));
2011-03-02 16:57:41 +01:00
menu.addSeparator();
}
else {
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();
QAction* action = menu.addAction(QIcon::fromTheme("user-trash"), tr("Restore &Closed Tab"), m_tabWidget, SLOT(restoreClosedTab()));
action->setEnabled(m_tabWidget->canRestoreTab());
2011-03-02 16:57:41 +01:00
}
// Prevent choosing first option with double rightclick
const QPoint &pos = mapToGlobal(position);
QPoint p(pos.x(), pos.y() + 1);
2011-03-02 16:57:41 +01:00
menu.exec(p);
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
{
if (!isVisible()) {
// Don't calculate it when tabbar is not visible
// It produces invalid size anyway
return QSize(-1, -1);
}
static int PINNED_TAB_WIDTH = -1;
if (PINNED_TAB_WIDTH == -1) {
PINNED_TAB_WIDTH = 16 + style()->pixelMetric(QStyle::PM_TabBarTabHSpace, 0, this);
}
2011-03-25 19:16:21 +01:00
QSize size = QTabBar::tabSizeHint(index);
WebTab* webTab = qobject_cast<WebTab*>(m_tabWidget->widget(index));
TabBar* tabBar = const_cast <TabBar*>(this);
bool adjustingLastTab = false;
if (webTab && webTab->isPinned()) {
size.setWidth(PINNED_TAB_WIDTH);
}
else {
int availableWidth = width() - (PINNED_TAB_WIDTH * m_pinnedTabsCount) - m_tabWidget->buttonListTabs()->width() - m_tabWidget->buttonAddTab()->width();
int normalTabsCount = count() - m_pinnedTabsCount;
if (availableWidth >= MAXIMUM_TAB_WIDTH * normalTabsCount) {
2012-04-21 22:19:35 +02:00
m_normalTabWidth = MAXIMUM_TAB_WIDTH;
size.setWidth(m_normalTabWidth);
}
else 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
int maxWidthForTab = availableWidth / normalTabsCount;
if (maxWidthForTab < PINNED_TAB_WIDTH) {
// FIXME: It overflows now
m_normalTabWidth = PINNED_TAB_WIDTH;
size.setWidth(m_normalTabWidth);
}
else {
m_normalTabWidth = maxWidthForTab;
// Fill any empty space (we've got from rounding) with last tab
if (index == count() - 1) {
m_lastTabWidth = (availableWidth - maxWidthForTab * normalTabsCount) + maxWidthForTab;
adjustingLastTab = true;
size.setWidth(m_lastTabWidth);
}
else {
m_lastTabWidth = maxWidthForTab;
size.setWidth(m_lastTabWidth);
}
// Hiding close buttons to save some space
tabBar->setTabsClosable(false);
}
}
else {
int maxWidthForTab = availableWidth / normalTabsCount;
2012-04-21 22:19:35 +02:00
m_normalTabWidth = maxWidthForTab;
// Fill any empty space (we've got from rounding) with last tab
if (index == count() - 1) {
2012-04-21 22:19:35 +02:00
m_lastTabWidth = (availableWidth - maxWidthForTab * normalTabsCount) + maxWidthForTab;
adjustingLastTab = true;
size.setWidth(m_lastTabWidth);
}
else {
2012-04-21 22:19:35 +02:00
m_lastTabWidth = maxWidthForTab;
size.setWidth(m_lastTabWidth);
}
// Restore close buttons according to preferences
if (tabsClosable() != m_showCloseButtons) {
tabBar->setTabsClosable(m_showCloseButtons);
// Hide close buttons on pinned tabs
for (int i = 0; i < count(); ++i) {
updateCloseButton(i);
}
}
}
2011-03-25 19:16:21 +01:00
}
if (index == count() - 1) {
int xForAddTabButton = (PINNED_TAB_WIDTH * m_pinnedTabsCount) + (count() - m_pinnedTabsCount) * (m_normalTabWidth);
if (adjustingLastTab) {
xForAddTabButton += m_lastTabWidth - m_normalTabWidth;
}
// RTL Support
if (QApplication::layoutDirection() == Qt::RightToLeft) {
xForAddTabButton = width() - xForAddTabButton;
}
emit tabBar->moveAddTabButton(xForAddTabButton);
}
2011-03-25 19:16:21 +01:00
return size;
}
//void TabBar::emitMoveAddTabButton(int pox)
//{
// emit moveAddTabButton(pox);
//}
#if 0
2011-03-25 19:16:21 +01:00
void TabBar::tabInserted(int index)
{
// CloseButton* closeButton = new CloseButton(this);
// closeButton->setAutoRaise(true);
// closeButton->setMaximumSize(17,17);
// closeButton->setIcon(style()->standardIcon(QStyle::SP_TitleBarMaxButton));
// connect(closeButton, SIGNAL(clicked()), this, SLOT(closeCurrentTab()));
// setTabButton(index, QTabBar::RightSide, closeButton);
QAbstractButton* button = (QAbstractButton*)tabButton(index, QTabBar::RightSide);
if (!button) {
2011-03-25 19:16:21 +01:00
return;
}
button->resize(17, 17);
2011-03-25 19:16:21 +01:00
}
void TabBar::showCloseButton(int index)
{
WebTab* webTab = qobject_cast<WebTab*>(m_tabWidget->widget(index));
if (webTab && webTab->isPinned()) {
return;
}
CloseButton* button = (CloseButton*)tabButton(index, QTabBar::RightSide);
if (!button) {
return;
}
button->show();
}
void TabBar::hideCloseButton(int index)
{
CloseButton* button = (CloseButton*)tabButton(index, QTabBar::RightSide);
if (!button) {
return;
}
button->hide();
}
#endif
2011-03-25 19:16:21 +01:00
void TabBar::updateCloseButton(int index) const
2011-03-25 19:16:21 +01:00
{
QAbstractButton* button = qobject_cast<QAbstractButton*>(tabButton(index, QTabBar::RightSide));
if (!button) {
2011-03-25 19:16:21 +01:00
return;
}
2011-03-25 19:16:21 +01:00
WebTab* webTab = qobject_cast<WebTab*>(m_tabWidget->widget(index));
if (webTab && webTab->isPinned()) {
2011-03-25 19:16:21 +01:00
button->hide();
}
else {
2011-03-25 19:16:21 +01:00
button->show();
}
2011-03-25 19:16:21 +01:00
}
void TabBar::closeCurrentTab()
{
int id = currentIndex();
if (id < 0) {
2011-03-25 19:16:21 +01:00
return;
}
m_tabWidget->closeTab(id);
2011-03-25 19:16:21 +01:00
}
2012-04-09 14:09:40 +02:00
void TabBar::currentTabChanged(int index)
{
Q_UNUSED(index)
hideTabPreview(false);
}
2011-03-02 16:57:41 +01:00
void TabBar::bookmarkTab()
{
TabbedWebView* view = p_QupZilla->weView(m_clickedTab);
if (!view) {
return;
}
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()
{
WebTab* webTab = qobject_cast<WebTab*>(m_tabWidget->widget(m_clickedTab));
if (!webTab) {
2011-03-25 19:16:21 +01:00
return;
}
2011-03-25 19:16:21 +01:00
webTab->pinTab(m_clickedTab);
if (webTab->isPinned()) {
m_pinnedTabsCount++;
}
else {
m_pinnedTabsCount--;
}
// 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());
}
void TabBar::pinnedTabClosed()
{
m_pinnedTabsCount--;
}
void TabBar::pinnedTabAdded()
{
m_pinnedTabsCount++;
2011-03-25 19:16:21 +01:00
}
int TabBar::pinnedTabsCount()
{
return m_pinnedTabsCount;
}
int TabBar::normalTabsCount()
{
return count() - m_pinnedTabsCount;
}
void TabBar::showTabPreview()
{
WebTab* webTab = qobject_cast<WebTab*>(m_tabWidget->widget(m_tabPreview->previewIndex()));
if (!webTab) {
return;
}
2012-04-09 14:09:40 +02:00
m_tabPreviewTimer->stop();
m_tabPreview->setWebTab(webTab, m_tabPreview->previewIndex() == currentIndex());
m_tabPreview->showOnRect(tabRect(m_tabPreview->previewIndex()));
}
2012-04-09 14:09:40 +02:00
void TabBar::hideTabPreview(bool delayed)
{
2012-04-09 14:09:40 +02:00
if (delayed) {
m_tabPreviewTimer->start();
}
else {
m_tabPreview->hideAnimated();
}
}
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);
}
void TabBar::mousePressEvent(QMouseEvent* event)
{
hideTabPreview(false);
2012-03-05 13:16:34 +01:00
if (mApp->plugins()->processMousePress(Qz::ON_TabBar, this, event)) {
return;
}
if (event->buttons() & Qt::LeftButton && tabAt(event->pos()) != -1) {
m_dragStartPosition = mapFromGlobal(event->globalPos());
}
else {
m_dragStartPosition = QPoint();
}
QTabBar::mousePressEvent(event);
}
void TabBar::mouseMoveEvent(QMouseEvent* event)
{
2012-03-05 13:16:34 +01:00
if (mApp->plugins()->processMouseMove(Qz::ON_TabBar, this, event)) {
return;
}
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();
}
}
//Tab Preview
const int tab = tabAt(event->pos());
if (tab != -1 && tab != m_tabPreview->previewIndex() && event->buttons() == Qt::NoButton && m_dragStartPosition.isNull()) {
m_tabPreview->setPreviewIndex(tab);
if (m_tabPreview->isVisible()) {
showTabPreview();
}
}
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;
}
if (m_tabWidget->buttonAddTab()->isHidden()) {
QTimer::singleShot(500, m_tabWidget->buttonAddTab(), SLOT(show()));
}
if (!rect().contains(event->pos())) {
QTabBar::mouseReleaseEvent(event);
return;
}
int id = tabAt(event->pos());
if (id != -1 && event->button() == Qt::MiddleButton) {
m_tabWidget->closeTab(id);
return;
}
if (id == -1 && event->button() == Qt::MiddleButton) {
m_tabWidget->addView(QUrl(), Qz::NT_SelectedTabAtTheEnd, true);
return;
}
QTabBar::mouseReleaseEvent(event);
}
bool TabBar::event(QEvent* event)
{
switch (event->type()) {
case QEvent::Leave:
hideTabPreview();
break;
case QEvent::ToolTip:
if (m_showTabPreviews) {
if (!m_tabPreview->isVisible()) {
showTabPreview();
}
return true;
}
break;
default:
break;
}
return QTabBar::event(event);
}
void TabBar::wheelEvent(QWheelEvent* event)
{
if (mApp->plugins()->processWheelEvent(Qz::ON_TabBar, this, event)) {
return;
}
QTabBar::wheelEvent(event);
}
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 {
WebTab* tab = p_QupZilla->weView(index)->webTab();
if (tab->isRestored()) {
tab->view()->load(mime->urls().at(0));
}
}
}
void TabBar::disconnectObjects()
{
disconnect(this);
}