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

649 lines
18 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 "webview.h"
#include "webpage.h"
#include "qupzilla.h"
#include "tabwidget.h"
#include "tabbar.h"
#include "iconprovider.h"
2011-03-02 16:57:41 +01:00
#include "mainapplication.h"
#include "webtab.h"
#include "clickablelabel.h"
#include "closedtabsmanager.h"
#include "progressbar.h"
#include "navigationbar.h"
#include "toolbutton.h"
#include "locationbar.h"
#include "websearchbar.h"
#include "settings.h"
class NewTabButton : public QToolButton
{
public:
explicit NewTabButton(QWidget* parent) : QToolButton(parent) {
#ifndef Q_WS_WIN
setIcon(QIcon::fromTheme("list-add"));
setIconSize(QSize(16, 16));
setAutoRaise(true);
#endif
}
QSize sizeHint() const {
QSize siz = QToolButton::sizeHint();
siz.setWidth(26);
return siz;
}
#ifdef Q_WS_WIN
private:
void paintEvent(QPaintEvent*) {
QPainter p(this);
QStyleOptionTabV3 opt;
opt.init(this);
style()->drawControl(QStyle::CE_TabBarTab, &opt, &p, this);
QPixmap pix(":/icons/other/list-add.png");
QRect r = this->rect();
r.setHeight(r.height() + 3);
r.setWidth(r.width() + 3);
style()->drawItemPixmap(&p, r, Qt::AlignCenter, pix);
}
#endif
};
class TabListButton : public QToolButton
{
public:
explicit TabListButton(QWidget* parent) : QToolButton(parent) {
}
QSize sizeHint() const {
QSize siz = QToolButton::sizeHint();
siz.setWidth(20);
return siz;
}
private:
void paintEvent(QPaintEvent*) {
QPainter p(this);
QStyleOptionToolButton opt;
opt.init(this);
if (isDown()) {
opt.state |= QStyle::State_On;
}
if (opt.state & QStyle::State_MouseOver) {
opt.activeSubControls = QStyle::SC_ToolButton;
}
if (!isChecked() && !isDown()) {
opt.state |= QStyle::State_Raised;
}
opt.state |= QStyle::State_AutoRaise;
style()->drawComplexControl(QStyle::CC_ToolButton, &opt, &p, this);
}
};
2011-03-02 16:57:41 +01:00
TabWidget::TabWidget(QupZilla* mainClass, QWidget* parent)
: QTabWidget(parent)
, p_QupZilla(mainClass)
, m_lastTabIndex(0)
, m_isClosingToLastTabIndex(false)
, m_closedTabsManager(new ClosedTabsManager(this))
, m_locationBars(new QStackedWidget())
2011-03-02 16:57:41 +01:00
{
setObjectName("tabwidget");
m_tabBar = new TabBar(p_QupZilla, this);
2011-03-02 16:57:41 +01:00
setTabBar(m_tabBar);
connect(this, SIGNAL(currentChanged(int)), this, SLOT(currentTabChanged(int)));
2011-03-02 16:57:41 +01:00
connect(this, SIGNAL(currentChanged(int)), p_QupZilla, SLOT(refreshHistory()));
connect(this, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int)));
connect(m_tabBar, SIGNAL(backTab(int)), this, SLOT(backTab(int)));
connect(m_tabBar, SIGNAL(forwardTab(int)), this, SLOT(forwardTab(int)));
connect(m_tabBar, SIGNAL(reloadTab(int)), this, SLOT(reloadTab(int)));
connect(m_tabBar, SIGNAL(stopTab(int)), this, SLOT(stopTab(int)));
connect(m_tabBar, SIGNAL(closeTab(int)), this, SLOT(closeTab(int)));
connect(m_tabBar, SIGNAL(closeAllButCurrent(int)), this, SLOT(closeAllButCurrent(int)));
2011-04-08 17:27:08 +02:00
connect(m_tabBar, SIGNAL(duplicateTab(int)), this, SLOT(duplicateTab(int)));
connect(m_tabBar, SIGNAL(tabMoved(int, int)), this, SLOT(tabMoved(int, int)));
connect(m_tabBar, SIGNAL(moveAddTabButton(int)), this, SLOT(moveAddTabButton(int)));
connect(m_tabBar, SIGNAL(showButtons()), this, SLOT(showButtons()));
connect(m_tabBar, SIGNAL(hideButtons()), this, SLOT(hideButtons()));
2011-03-02 16:57:41 +01:00
m_buttonListTabs = new ToolButton(this);
m_buttonListTabs->setObjectName("tabwidget-button-opentabs");
m_menuTabs = new QMenu(this);
2011-03-02 16:57:41 +01:00
m_buttonListTabs->setMenu(m_menuTabs);
m_buttonListTabs->setPopupMode(QToolButton::InstantPopup);
m_buttonListTabs->setToolTip(tr("Show list of opened tabs"));
m_buttonListTabs->setAutoRaise(true);
m_buttonListTabs->setFocusPolicy(Qt::NoFocus);
2011-03-02 16:57:41 +01:00
m_buttonAddTab = new ToolButton(this);
m_buttonAddTab->setObjectName("tabwidget-button-addtab");
m_buttonAddTab->setAutoRaise(true);
m_buttonAddTab->setToolTip(tr("New Tab"));
m_buttonAddTab->setFocusPolicy(Qt::NoFocus);
connect(m_buttonAddTab, SIGNAL(clicked()), p_QupZilla, SLOT(addTab()));
connect(m_menuTabs, SIGNAL(aboutToShow()), this, SLOT(aboutToShowTabsMenu()));
loadSettings();
2011-03-02 16:57:41 +01:00
}
void TabWidget::loadSettings()
{
Settings settings;
2011-03-02 16:57:41 +01:00
settings.beginGroup("Browser-Tabs-Settings");
m_hideTabBarWithOneTab = settings.value("hideTabsWithOneTab", false).toBool();
2011-03-02 16:57:41 +01:00
settings.endGroup();
settings.beginGroup("Web-URL-Settings");
m_urlOnNewTab = settings.value("newTabUrl", "qupzilla:speeddial").toUrl();
2011-03-02 16:57:41 +01:00
settings.endGroup();
m_tabBar->loadSettings();
}
void TabWidget::resizeEvent(QResizeEvent* e)
{
QPoint posit;
posit.setY(0);
posit.setX(width() - m_buttonListTabs->width());
m_buttonListTabs->move(posit);
QTabWidget::resizeEvent(e);
}
void TabWidget::createKeyPressEvent(QKeyEvent* event)
{
QTabWidget::keyPressEvent(event);
}
void TabWidget::showButtons()
{
m_buttonListTabs->show();
m_buttonAddTab->show();
}
void TabWidget::hideButtons()
{
m_buttonListTabs->hide();
m_buttonAddTab->hide();
}
void TabWidget::moveAddTabButton(int posX)
{
int posY = (m_tabBar->height() - m_buttonAddTab->height()) / 2;
m_buttonAddTab->move(posX, posY);
}
2011-03-02 16:57:41 +01:00
void TabWidget::aboutToShowTabsMenu()
{
m_menuTabs->clear();
WebView* actView = weView();
if (!actView) {
2011-03-02 16:57:41 +01:00
return;
}
for (int i = 0; i < count(); i++) {
2011-03-02 16:57:41 +01:00
WebView* view = weView(i);
if (!view) {
2011-03-02 16:57:41 +01:00
continue;
}
2011-03-02 16:57:41 +01:00
QAction* action = new QAction(this);
if (view == actView) {
action->setIcon(QIcon(":/icons/menu/dot.png"));
}
else {
action->setIcon(view->siteIcon());
}
2011-03-02 16:57:41 +01:00
if (view->title().isEmpty()) {
if (view->isLoading()) {
action->setText(tr("Loading..."));
action->setIcon(QIcon(":/icons/other/progress.gif"));
}
else {
2011-03-02 16:57:41 +01:00
action->setText(tr("No Named Page"));
}
2011-03-02 16:57:41 +01:00
}
else {
2011-03-02 16:57:41 +01:00
QString title = view->title();
if (title.length() > 40) {
2011-03-02 16:57:41 +01:00
title.truncate(40);
title += "..";
2011-03-02 16:57:41 +01:00
}
action->setText(title);
}
action->setData(i);
connect(action, SIGNAL(triggered()), this, SLOT(actionChangeIndex()));
m_menuTabs->addAction(action);
}
m_menuTabs->addSeparator();
m_menuTabs->addAction(tr("Actually you have %1 opened tabs").arg(count()))->setEnabled(false);
2011-03-02 16:57:41 +01:00
}
void TabWidget::actionChangeIndex()
{
2011-03-17 17:03:04 +01:00
if (QAction* action = qobject_cast<QAction*>(sender())) {
2011-03-02 16:57:41 +01:00
setCurrentIndex(action->data().toInt());
}
}
int TabWidget::addView(QUrl url, const QString &title, OpenUrlIn openIn, bool selectLine, int position)
2011-03-02 16:57:41 +01:00
{
m_lastTabIndex = currentIndex();
if (url.isEmpty() && openIn != CleanPage && openIn != CleanSelectedPage) {
2011-03-02 16:57:41 +01:00
url = m_urlOnNewTab;
}
2011-03-02 16:57:41 +01:00
if (openIn == NewBackgroundTab) {
// If we are opening newBgTab from pinned tab, make sure it won't be
// opened between other pinned tabs
position = qMax(currentIndex() + 1, m_tabBar->pinnedTabsCount());
}
LocationBar* locBar = new LocationBar(p_QupZilla);
m_locationBars->addWidget(locBar);
int index;
if (position == -1) {
index = addTab(new WebTab(p_QupZilla, locBar), "");
}
else {
index = insertTab(position, new WebTab(p_QupZilla, locBar), "");
}
WebView* webView = weView(index);
locBar->setWebView(webView);
2011-03-02 16:57:41 +01:00
setTabText(index, title);
webView->animationLoading(index, true)->movie()->stop();
webView->animationLoading(index, false)->setPixmap(_iconForUrl(url).pixmap(16, 16));
2011-03-02 16:57:41 +01:00
if (openIn == NewSelectedTab || openIn == CleanSelectedPage) {
2011-03-02 16:57:41 +01:00
setCurrentIndex(index);
}
if (count() == 1 && m_hideTabBarWithOneTab) {
2011-03-02 16:57:41 +01:00
tabBar()->setVisible(false);
}
else {
tabBar()->setVisible(true);
}
2011-03-02 16:57:41 +01:00
connect(webView, SIGNAL(wantsCloseTab(int)), this, SLOT(closeTab(int)));
connect(webView, SIGNAL(changed()), mApp, SLOT(setStateChanged()));
connect(webView, SIGNAL(ipChanged(QString)), p_QupZilla->ipLabel(), SLOT(setText(QString)));
2011-03-02 16:57:41 +01:00
if (url.isValid()) {
webView->load(url);
}
if (selectLine) {
2011-03-02 16:57:41 +01:00
p_QupZilla->locationBar()->setFocus();
}
if (openIn == NewSelectedTab || openIn == CleanSelectedPage) {
m_isClosingToLastTabIndex = true;
m_locationBars->setCurrentWidget(locBar);
}
2011-03-02 16:57:41 +01:00
return index;
}
void TabWidget::setTabText(int index, const QString &text)
2011-03-02 16:57:41 +01:00
{
QString newtext = text;
newtext.remove("&"); // Avoid Alt+letter shortcuts
2011-03-25 19:16:21 +01:00
if (WebTab* webTab = qobject_cast<WebTab*>(p_QupZilla->tabWidget()->widget(index))) {
if (webTab->isPinned()) {
2011-03-25 19:16:21 +01:00
newtext = "";
}
2011-03-25 19:16:21 +01:00
}
2011-03-02 16:57:41 +01:00
QTabWidget::setTabText(index, newtext);
}
void TabWidget::closeTab(int index)
{
if (count() == 1) {
p_QupZilla->close();
}
if (index == -1) {
2011-03-02 16:57:41 +01:00
index = currentIndex();
}
2011-03-02 16:57:41 +01:00
WebView* webView = weView(index);
if (!webView) {
return;
}
if (webView->webTab()->isPinned()) {
emit pinnedTabClosed();
}
m_locationBars->removeWidget(webView->webTab()->locationBar());
disconnect(webView, SIGNAL(wantsCloseTab(int)), this, SLOT(closeTab(int)));
disconnect(webView, SIGNAL(changed()), mApp, SLOT(setStateChanged()));
disconnect(webView, SIGNAL(ipChanged(QString)), p_QupZilla->ipLabel(), SLOT(setText(QString)));
//Save last tab url and history
m_closedTabsManager->saveView(webView, index);
if (m_isClosingToLastTabIndex && m_lastTabIndex < count() && index == currentIndex()) {
setCurrentIndex(m_lastTabIndex);
}
if (count() == 2 && m_hideTabBarWithOneTab) {
tabBar()->setVisible(false);
}
2012-01-10 21:15:03 +01:00
webView->webPage()->disconnect();
webView->disconnect();
widget(index)->disconnect();
widget(index)->deleteLater();
}
void TabWidget::showTabBar()
{
if (count() == 1 && m_hideTabBarWithOneTab) {
tabBar()->hide();
}
else {
tabBar()->show();
}
2011-03-02 16:57:41 +01:00
}
void TabWidget::tabMoved(int before, int after)
{
Q_UNUSED(before)
Q_UNUSED(after)
m_isClosingToLastTabIndex = false;
}
void TabWidget::currentTabChanged(int index)
2011-03-02 16:57:41 +01:00
{
if (index < 0) {
2011-03-02 16:57:41 +01:00
return;
}
2011-03-02 16:57:41 +01:00
m_isClosingToLastTabIndex = false;
WebView* webView = weView();
LocationBar* locBar = webView->webTab()->locationBar();
if (m_locationBars->indexOf(locBar) != -1) {
m_locationBars->setCurrentWidget(locBar);
}
2011-03-02 16:57:41 +01:00
p_QupZilla->currentTabChanged();
2011-03-25 19:16:21 +01:00
m_tabBar->updateCloseButton(index);
2011-03-02 16:57:41 +01:00
}
void TabWidget::reloadAllTabs()
{
for (int i = 0; i < count(); i++) {
2011-03-02 16:57:41 +01:00
reloadTab(i);
}
}
void TabWidget::closeAllButCurrent(int index)
{
WebTab* akt = qobject_cast<WebTab*>(widget(index));
foreach(WebTab * tab, allTabs(false)) {
if (akt == widget(tab->view()->tabIndex())) {
continue;
}
closeTab(tab->view()->tabIndex());
2011-03-02 16:57:41 +01:00
}
}
int TabWidget::duplicateTab(int index)
2011-04-08 17:27:08 +02:00
{
QUrl url = weView(index)->url();
QByteArray history;
QDataStream tabHistoryStream(&history, QIODevice::WriteOnly);
tabHistoryStream << *weView(index)->history();
int id = addView(url, tabText(index), TabWidget::NewNotSelectedTab);
2011-04-08 17:27:08 +02:00
QDataStream historyStream(history);
historyStream >> *weView(id)->history();
return id;
2011-04-08 17:27:08 +02:00
}
2011-03-02 16:57:41 +01:00
void TabWidget::restoreClosedTab()
{
if (!m_closedTabsManager->isClosedTabAvailable()) {
2011-03-02 16:57:41 +01:00
return;
}
ClosedTabsManager::Tab tab;
QAction* action = qobject_cast<QAction*>(sender());
if (action && action->data().toInt() != 0) {
tab = m_closedTabsManager->getTabAt(action->data().toInt());
}
else {
tab = m_closedTabsManager->getFirstClosedTab();
}
int index = addView(QUrl(), tab.title, TabWidget::NewSelectedTab, false, tab.position);
QDataStream historyStream(tab.history);
2011-03-02 16:57:41 +01:00
historyStream >> *weView(index)->history();
weView(index)->load(tab.url);
}
void TabWidget::restoreAllClosedTabs()
{
if (!m_closedTabsManager->isClosedTabAvailable()) {
return;
}
QList<ClosedTabsManager::Tab> closedTabs = m_closedTabsManager->allClosedTabs();
foreach(ClosedTabsManager::Tab tab, closedTabs) {
int index = addView(QUrl(), tab.title);
QDataStream historyStream(tab.history);
historyStream >> *weView(index)->history();
weView(index)->load(tab.url);
}
m_closedTabsManager->clearList();
}
void TabWidget::clearClosedTabsList()
{
m_closedTabsManager->clearList();
}
bool TabWidget::canRestoreTab()
{
return m_closedTabsManager->isClosedTabAvailable();
2011-03-02 16:57:41 +01:00
}
QList<WebTab*> TabWidget::allTabs(bool withPinned)
{
QList<WebTab*> allTabs;
for (int i = 0; i < count(); i++) {
WebTab* tab = qobject_cast<WebTab*>(widget(i));
if (!tab || (!withPinned && tab->isPinned())) {
continue;
}
allTabs.append(tab);
}
return allTabs;
}
2011-03-25 19:16:21 +01:00
void TabWidget::savePinnedTabs()
{
QByteArray data;
QDataStream stream(&data, QIODevice::WriteOnly);
QStringList tabs;
QList<QByteArray> tabsHistory;
for (int i = 0; i < count(); ++i) {
if (WebView* tab = weView(i)) {
WebTab* webTab = qobject_cast<WebTab*>(widget(i));
if (!webTab || !webTab->isPinned()) {
2011-03-25 19:16:21 +01:00
continue;
}
2011-03-25 19:16:21 +01:00
tabs.append(tab->url().toString());
2011-03-25 19:16:21 +01:00
if (tab->history()->count() != 0) {
QByteArray tabHistory;
QDataStream tabHistoryStream(&tabHistory, QIODevice::WriteOnly);
tabHistoryStream << *tab->history();
tabsHistory.append(tabHistory);
}
else {
2011-03-25 19:16:21 +01:00
tabsHistory << QByteArray();
}
}
else {
2011-03-25 19:16:21 +01:00
tabs.append(QString::null);
tabsHistory.append(QByteArray());
}
}
stream << tabs;
stream << tabsHistory;
QFile file(mApp->getActiveProfilPath() + "pinnedtabs.dat");
2011-03-25 19:16:21 +01:00
file.open(QIODevice::WriteOnly);
file.write(data);
file.close();
}
void TabWidget::restorePinnedTabs()
{
QFile file(mApp->getActiveProfilPath() + "pinnedtabs.dat");
2011-03-25 19:16:21 +01:00
file.open(QIODevice::ReadOnly);
QByteArray sd = file.readAll();
file.close();
QDataStream stream(&sd, QIODevice::ReadOnly);
if (stream.atEnd()) {
2011-03-25 19:16:21 +01:00
return;
}
2011-03-25 19:16:21 +01:00
QStringList pinnedTabs;
stream >> pinnedTabs;
QList<QByteArray> tabHistory;
stream >> tabHistory;
for (int i = 0; i < pinnedTabs.count(); ++i) {
QUrl url = QUrl::fromEncoded(pinnedTabs.at(i).toUtf8());
QByteArray historyState = tabHistory.value(i);
int addedIndex;
if (!historyState.isEmpty()) {
addedIndex = addView(QUrl(), tr("New tab"), CleanPage);
2011-03-25 19:16:21 +01:00
QDataStream historyStream(historyState);
historyStream >> *weView(addedIndex)->history();
weView(addedIndex)->load(url);
}
else {
2011-03-25 19:16:21 +01:00
addedIndex = addView(url);
}
WebTab* webTab = qobject_cast<WebTab*>(widget(addedIndex));
if (webTab) {
2011-03-25 19:16:21 +01:00
webTab->setPinned(true);
emit pinnedTabAdded();
}
2011-03-25 19:16:21 +01:00
m_tabBar->updateCloseButton(addedIndex);
2011-03-25 19:16:21 +01:00
m_tabBar->moveTab(addedIndex, i);
}
}
2011-03-02 16:57:41 +01:00
QByteArray TabWidget::saveState()
{
QByteArray data;
QDataStream stream(&data, QIODevice::WriteOnly);
QStringList tabs;
QList<QByteArray> tabsHistory;
for (int i = 0; i < count(); ++i) {
2011-03-17 17:03:04 +01:00
if (WebView* tab = weView(i)) {
2011-03-25 19:16:21 +01:00
WebTab* webTab = qobject_cast<WebTab*>(widget(i));
if (webTab && webTab->isPinned()) {
2011-03-25 19:16:21 +01:00
continue;
}
2011-03-25 19:16:21 +01:00
tabs.append(tab->url().toString());
2011-03-02 16:57:41 +01:00
if (tab->history()->count() != 0) {
QByteArray tabHistory;
QDataStream tabHistoryStream(&tabHistory, QIODevice::WriteOnly);
tabHistoryStream << *tab->history();
tabsHistory.append(tabHistory);
}
else {
2011-03-02 16:57:41 +01:00
tabsHistory << QByteArray();
}
}
else {
2011-03-02 16:57:41 +01:00
tabs.append(QString::null);
tabsHistory.append(QByteArray());
}
}
stream << tabs;
stream << currentIndex();
stream << tabsHistory;
return data;
}
bool TabWidget::restoreState(const QByteArray &state)
{
QByteArray sd = state;
QDataStream stream(&sd, QIODevice::ReadOnly);
if (stream.atEnd()) {
2011-03-02 16:57:41 +01:00
return false;
}
2011-03-02 16:57:41 +01:00
QStringList openTabs;
int currentTab;
QList<QByteArray> tabHistory;
stream >> openTabs;
stream >> currentTab;
2011-03-02 16:57:41 +01:00
stream >> tabHistory;
for (int i = 0; i < openTabs.count(); ++i) {
QUrl url = QUrl::fromEncoded(openTabs.at(i).toUtf8());
QByteArray historyState = tabHistory.value(i);
if (!historyState.isEmpty()) {
int index = addView(QUrl(), tr("New tab"), CleanPage);
2011-03-02 16:57:41 +01:00
QDataStream historyStream(historyState);
historyStream >> *weView(index)->history();
weView(index)->load(url);
}
else {
2011-03-02 16:57:41 +01:00
addView(url);
}
}
2011-03-25 19:16:21 +01:00
setCurrentIndex(currentTab);
2011-03-02 16:57:41 +01:00
return true;
}
TabWidget::~TabWidget()
{
}