2011-03-03 18:29:20 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2013-01-31 11:54:42 +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 "webtab.h"
|
|
|
|
#include "qupzilla.h"
|
2012-01-21 20:27:45 +01:00
|
|
|
#include "tabbedwebview.h"
|
2011-07-11 20:30:49 +02:00
|
|
|
#include "webpage.h"
|
2011-03-25 19:16:21 +01:00
|
|
|
#include "tabbar.h"
|
2012-01-21 23:19:38 +01:00
|
|
|
#include "tabwidget.h"
|
2011-07-11 20:30:49 +02:00
|
|
|
#include "locationbar.h"
|
2013-01-22 19:04:22 +01:00
|
|
|
#include "qztools.h"
|
2012-08-10 21:16:43 +02:00
|
|
|
#include "qzsettings.h"
|
2013-02-11 16:45:10 +01:00
|
|
|
#include "mainapplication.h"
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QVBoxLayout>
|
2012-03-11 15:17:12 +01:00
|
|
|
#include <QWebHistory>
|
2012-04-08 21:45:40 +02:00
|
|
|
#include <QWebFrame>
|
2012-03-11 15:17:12 +01:00
|
|
|
#include <QLabel>
|
2012-04-15 22:08:41 +02:00
|
|
|
#include <QStyle>
|
2013-02-11 09:51:22 +01:00
|
|
|
#include <QTimer>
|
2012-03-11 15:17:12 +01:00
|
|
|
|
|
|
|
WebTab::SavedTab::SavedTab(WebTab* webTab)
|
|
|
|
{
|
2012-03-12 18:22:01 +01:00
|
|
|
title = webTab->title();
|
|
|
|
url = webTab->url();
|
|
|
|
icon = webTab->icon();
|
|
|
|
history = webTab->historyData();
|
2012-03-11 15:17:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebTab::SavedTab::clear()
|
|
|
|
{
|
|
|
|
title.clear();
|
|
|
|
url.clear();
|
|
|
|
icon = QIcon();
|
|
|
|
history.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
QDataStream &operator <<(QDataStream &stream, const WebTab::SavedTab &tab)
|
|
|
|
{
|
|
|
|
stream << tab.title;
|
|
|
|
stream << tab.url;
|
|
|
|
stream << tab.icon;
|
|
|
|
stream << tab.history;
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
QDataStream &operator >>(QDataStream &stream, WebTab::SavedTab &tab)
|
|
|
|
{
|
|
|
|
stream >> tab.title;
|
|
|
|
stream >> tab.url;
|
|
|
|
stream >> tab.icon;
|
|
|
|
stream >> tab.history;
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
2012-02-29 18:33:50 +01:00
|
|
|
|
2011-07-11 20:30:49 +02:00
|
|
|
WebTab::WebTab(QupZilla* mainClass, LocationBar* locationBar)
|
2011-10-14 20:14:57 +02:00
|
|
|
: QWidget()
|
|
|
|
, p_QupZilla(mainClass)
|
2013-02-09 13:00:45 +01:00
|
|
|
, m_navigationContainer(0)
|
2011-10-14 20:14:57 +02:00
|
|
|
, m_locationBar(locationBar)
|
|
|
|
, m_pinned(false)
|
|
|
|
, m_inspectorVisible(false)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2013-01-31 14:18:28 +01:00
|
|
|
setObjectName("webtab");
|
|
|
|
|
|
|
|
// This fixes background of pages with dark themes
|
|
|
|
setStyleSheet("#webtab {background-color:white;}");
|
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
m_layout = new QVBoxLayout(this);
|
2011-11-06 17:01:23 +01:00
|
|
|
m_layout->setContentsMargins(0, 0, 0, 0);
|
2011-03-02 16:57:41 +01:00
|
|
|
m_layout->setSpacing(0);
|
2011-07-19 15:18:49 +02:00
|
|
|
|
2012-01-21 20:27:45 +01:00
|
|
|
m_view = new TabbedWebView(p_QupZilla, this);
|
2013-02-09 13:00:45 +01:00
|
|
|
m_view->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
|
2012-01-21 20:27:45 +01:00
|
|
|
WebPage* page = new WebPage(p_QupZilla);
|
|
|
|
m_view->setWebPage(page);
|
|
|
|
m_layout->addWidget(m_view);
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2011-07-19 15:18:49 +02:00
|
|
|
setLayout(m_layout);
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2012-01-21 20:27:45 +01:00
|
|
|
connect(m_view, SIGNAL(showNotification(QWidget*)), this, SLOT(showNotification(QWidget*)));
|
|
|
|
connect(m_view, SIGNAL(iconChanged()), m_locationBar.data(), SLOT(siteIconChanged()));
|
|
|
|
connect(m_view, SIGNAL(loadStarted()), m_locationBar.data(), SLOT(clearIcon()));
|
|
|
|
connect(m_view, SIGNAL(loadFinished(bool)), m_locationBar.data(), SLOT(siteIconChanged()));
|
|
|
|
connect(m_view, SIGNAL(urlChanged(QUrl)), m_locationBar.data(), SLOT(showUrl(QUrl)));
|
|
|
|
connect(m_view, SIGNAL(rssChanged(bool)), m_locationBar.data(), SLOT(showRSSIcon(bool)));
|
2012-04-03 20:23:15 +02:00
|
|
|
connect(m_view, SIGNAL(privacyChanged(bool)), m_locationBar.data(), SLOT(setPrivacy(bool)));
|
2012-07-13 11:04:14 +02:00
|
|
|
connect(m_locationBar.data(), SIGNAL(loadUrl(QUrl)), m_view, SLOT(userLoadAction(QUrl)));
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-03-11 15:17:12 +01:00
|
|
|
TabbedWebView* WebTab::view() const
|
2011-12-18 16:57:14 +01:00
|
|
|
{
|
2012-01-21 20:27:45 +01:00
|
|
|
return m_view;
|
2011-12-18 16:57:14 +01:00
|
|
|
}
|
|
|
|
|
2012-03-11 15:17:12 +01:00
|
|
|
void WebTab::setCurrentTab()
|
|
|
|
{
|
|
|
|
if (!isRestored()) {
|
2013-02-11 16:45:10 +01:00
|
|
|
// When session is being restored, restore the tab immediately
|
|
|
|
if (mApp->isRestoring()) {
|
|
|
|
slotRestore();
|
2013-02-11 11:02:42 +01:00
|
|
|
}
|
|
|
|
else {
|
2013-02-11 16:45:10 +01:00
|
|
|
QTimer::singleShot(0, this, SLOT(slotRestore()));
|
2013-02-11 11:02:42 +01:00
|
|
|
}
|
2012-03-11 15:17:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QUrl WebTab::url() const
|
|
|
|
{
|
|
|
|
if (isRestored()) {
|
|
|
|
return m_view->url();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return m_savedTab.url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString WebTab::title() const
|
|
|
|
{
|
|
|
|
if (isRestored()) {
|
|
|
|
return m_view->title();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return m_savedTab.title;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QIcon WebTab::icon() const
|
|
|
|
{
|
|
|
|
if (isRestored()) {
|
|
|
|
return m_view->icon();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return m_savedTab.icon;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QWebHistory* WebTab::history() const
|
|
|
|
{
|
|
|
|
return m_view->history();
|
|
|
|
}
|
|
|
|
|
2012-03-12 18:22:01 +01:00
|
|
|
void WebTab::setHistoryData(const QByteArray &data)
|
|
|
|
{
|
|
|
|
QDataStream historyStream(data);
|
|
|
|
historyStream >> *m_view->history();
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray WebTab::historyData() const
|
|
|
|
{
|
|
|
|
if (isRestored()) {
|
|
|
|
QByteArray historyArray;
|
|
|
|
QDataStream historyStream(&historyArray, QIODevice::WriteOnly);
|
|
|
|
historyStream << *m_view->history();
|
|
|
|
|
|
|
|
return historyArray;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return m_savedTab.history;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-11 15:17:12 +01:00
|
|
|
void WebTab::reload()
|
|
|
|
{
|
|
|
|
m_view->reload();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebTab::stop()
|
|
|
|
{
|
|
|
|
m_view->stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebTab::isLoading() const
|
|
|
|
{
|
|
|
|
return m_view->isLoading();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebTab::isPinned() const
|
2011-12-18 16:57:14 +01:00
|
|
|
{
|
|
|
|
return m_pinned;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebTab::setPinned(bool state)
|
|
|
|
{
|
|
|
|
m_pinned = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebTab::setLocationBar(LocationBar* bar)
|
|
|
|
{
|
|
|
|
m_locationBar = bar;
|
|
|
|
}
|
|
|
|
|
2012-03-11 15:17:12 +01:00
|
|
|
LocationBar* WebTab::locationBar() const
|
2011-12-18 16:57:14 +01:00
|
|
|
{
|
|
|
|
return m_locationBar.data();
|
|
|
|
}
|
|
|
|
|
2012-03-11 15:17:12 +01:00
|
|
|
bool WebTab::inspectorVisible() const
|
2011-12-18 16:57:14 +01:00
|
|
|
{
|
|
|
|
return m_inspectorVisible;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebTab::setInspectorVisible(bool v)
|
|
|
|
{
|
|
|
|
m_inspectorVisible = v;
|
|
|
|
}
|
|
|
|
|
2012-03-11 15:17:12 +01:00
|
|
|
WebTab::SavedTab WebTab::savedTab() const
|
|
|
|
{
|
|
|
|
return m_savedTab;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebTab::isRestored() const
|
|
|
|
{
|
|
|
|
return m_savedTab.isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebTab::restoreTab(const WebTab::SavedTab &tab)
|
|
|
|
{
|
2012-08-10 21:16:43 +02:00
|
|
|
if (qzSettings->loadTabsOnActivation) {
|
2012-03-11 15:17:12 +01:00
|
|
|
m_savedTab = tab;
|
|
|
|
int index = tabIndex();
|
|
|
|
|
2012-04-04 14:45:45 +02:00
|
|
|
m_view->tabWidget()->setTabIcon(index, tab.icon);
|
2012-03-11 15:17:12 +01:00
|
|
|
m_view->tabWidget()->setTabText(index, tab.title);
|
2012-04-10 18:29:22 +02:00
|
|
|
m_view->tabWidget()->setTabToolTip(index, tab.title);
|
2012-03-11 15:17:12 +01:00
|
|
|
m_locationBar.data()->showUrl(tab.url);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p_restoreTab(tab);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebTab::p_restoreTab(const QUrl &url, const QByteArray &history)
|
|
|
|
{
|
2013-03-13 11:55:22 +01:00
|
|
|
m_view->load(url);
|
|
|
|
|
2012-03-11 15:17:12 +01:00
|
|
|
QDataStream historyStream(history);
|
|
|
|
historyStream >> *m_view->history();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebTab::p_restoreTab(const WebTab::SavedTab &tab)
|
|
|
|
{
|
|
|
|
p_restoreTab(tab.url, tab.history);
|
|
|
|
}
|
|
|
|
|
2012-04-08 21:45:40 +02:00
|
|
|
QPixmap WebTab::renderTabPreview()
|
|
|
|
{
|
2013-02-06 23:45:43 +01:00
|
|
|
TabbedWebView* currentWebView = p_QupZilla->weView();
|
2012-04-08 21:45:40 +02:00
|
|
|
WebPage* page = m_view->page();
|
2013-02-07 12:35:54 +01:00
|
|
|
const QSize oldSize = page->viewportSize();
|
2013-02-06 23:45:43 +01:00
|
|
|
const QPoint originalScrollPosition = page->mainFrame()->scrollPosition();
|
|
|
|
|
2013-02-07 11:04:09 +01:00
|
|
|
// Hack to ensure rendering the same preview before and after the page was shown for the first time
|
2013-02-06 23:45:43 +01:00
|
|
|
// This can occur eg. with opening background tabs
|
2013-02-07 12:35:54 +01:00
|
|
|
if (currentWebView) {
|
|
|
|
page->setViewportSize(currentWebView->size());
|
|
|
|
}
|
2012-04-08 21:45:40 +02:00
|
|
|
|
2012-04-12 06:24:51 +02:00
|
|
|
const int previewWidth = 230;
|
|
|
|
const int previewHeight = 150;
|
2012-04-15 22:08:41 +02:00
|
|
|
const int scrollBarExtent = style()->pixelMetric(QStyle::PM_ScrollBarExtent);
|
2012-04-12 15:41:28 +02:00
|
|
|
const int pageWidth = qMin(page->mainFrame()->contentsSize().width(), 1280);
|
|
|
|
const int pageHeight = (pageWidth / 23 * 15);
|
|
|
|
const qreal scalingFactor = 2 * static_cast<qreal>(previewWidth) / pageWidth;
|
2012-04-12 15:24:57 +02:00
|
|
|
|
2012-04-12 15:41:28 +02:00
|
|
|
page->setViewportSize(QSize(pageWidth, pageHeight));
|
2012-04-12 06:24:51 +02:00
|
|
|
|
2012-04-15 22:08:41 +02:00
|
|
|
QPixmap pageImage((2 * previewWidth) - scrollBarExtent, (2 * previewHeight) - scrollBarExtent);
|
|
|
|
pageImage.fill(Qt::transparent);
|
2012-04-08 21:45:40 +02:00
|
|
|
|
|
|
|
QPainter p(&pageImage);
|
2012-04-12 06:24:51 +02:00
|
|
|
p.scale(scalingFactor, scalingFactor);
|
2012-04-09 14:09:40 +02:00
|
|
|
m_view->page()->mainFrame()->render(&p, QWebFrame::ContentsLayer);
|
2012-04-08 21:45:40 +02:00
|
|
|
p.end();
|
|
|
|
|
|
|
|
page->setViewportSize(oldSize);
|
2013-02-06 23:45:43 +01:00
|
|
|
// Restore also scrollbar positions, to prevent messing scrolling to anchor links
|
|
|
|
page->mainFrame()->setScrollBarValue(Qt::Vertical, originalScrollPosition.y());
|
|
|
|
page->mainFrame()->setScrollBarValue(Qt::Horizontal, originalScrollPosition.x());
|
2012-04-08 21:45:40 +02:00
|
|
|
|
2012-04-12 06:24:51 +02:00
|
|
|
return pageImage.scaled(previewWidth, previewHeight, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
|
2012-04-08 21:45:40 +02:00
|
|
|
}
|
2012-03-11 15:17:12 +01:00
|
|
|
|
2011-03-17 17:03:04 +01:00
|
|
|
void WebTab::showNotification(QWidget* notif)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2013-02-17 11:41:42 +01:00
|
|
|
const int notifPos = qzSettings->tabsOnTop ? 1 : 0;
|
2013-02-09 13:00:45 +01:00
|
|
|
|
2013-02-17 11:41:42 +01:00
|
|
|
if (m_layout->count() > notifPos + 1) {
|
|
|
|
delete m_layout->itemAt(notifPos)->widget();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2013-02-17 11:41:42 +01:00
|
|
|
m_layout->insertWidget(notifPos, notif);
|
2011-03-02 17:54:12 +01:00
|
|
|
notif->show();
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2013-02-11 09:51:22 +01:00
|
|
|
void WebTab::slotRestore()
|
|
|
|
{
|
|
|
|
p_restoreTab(m_savedTab);
|
|
|
|
m_savedTab.clear();
|
|
|
|
}
|
|
|
|
|
2012-03-11 15:17:12 +01:00
|
|
|
int WebTab::tabIndex() const
|
2011-03-25 19:16:21 +01:00
|
|
|
{
|
2012-01-21 20:27:45 +01:00
|
|
|
return m_view->tabIndex();
|
2011-03-25 19:16:21 +01:00
|
|
|
}
|
|
|
|
|
2013-02-09 13:00:45 +01:00
|
|
|
void WebTab::showNavigationBar(QWidget* bar)
|
|
|
|
{
|
|
|
|
if (bar) {
|
|
|
|
m_navigationContainer = bar;
|
|
|
|
m_layout->insertWidget(0, m_navigationContainer);
|
|
|
|
|
|
|
|
// Needed to prevent flickering when closing tabs
|
|
|
|
m_navigationContainer->setUpdatesEnabled(true);
|
|
|
|
m_navigationContainer->show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-25 19:16:21 +01:00
|
|
|
void WebTab::pinTab(int index)
|
|
|
|
{
|
|
|
|
TabWidget* tabWidget = p_QupZilla->tabWidget();
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!tabWidget) {
|
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
|
|
|
|
|
|
|
if (m_pinned) { //Unpin tab
|
|
|
|
m_pinned = false;
|
2012-01-21 20:27:45 +01:00
|
|
|
tabWidget->setTabText(index, m_view->title());
|
2012-09-03 22:40:52 +02:00
|
|
|
tabWidget->getTabBar()->updatePinnedTabCloseButton(index);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else { // Pin tab
|
2011-03-25 19:16:21 +01:00
|
|
|
m_pinned = true;
|
2013-02-23 16:18:01 +01:00
|
|
|
tabWidget->setCurrentIndex(0); // <<-- those 2 lines fixes
|
|
|
|
tabWidget->getTabBar()->moveTab(index, 0); // | weird behavior with bad
|
|
|
|
tabWidget->setTabText(0, QString()); // | tabwidget update if we
|
|
|
|
tabWidget->setCurrentIndex(0); // <<-- are moving current tab
|
2012-09-03 22:40:52 +02:00
|
|
|
tabWidget->getTabBar()->updatePinnedTabCloseButton(0);
|
2011-03-25 19:16:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-14 11:46:06 +01:00
|
|
|
void WebTab::disconnectObjects()
|
|
|
|
{
|
|
|
|
disconnect(this);
|
|
|
|
disconnect(m_locationBar.data());
|
2012-01-21 20:27:45 +01:00
|
|
|
disconnect(m_view);
|
2012-01-14 11:46:06 +01:00
|
|
|
}
|
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
WebTab::~WebTab()
|
|
|
|
{
|
2013-04-09 14:06:59 +02:00
|
|
|
if (m_navigationContainer && qzSettings->tabsOnTop && !mApp->isClosing()) {
|
2013-02-09 13:00:45 +01:00
|
|
|
m_layout->removeWidget(m_navigationContainer);
|
|
|
|
|
|
|
|
// Needed to prevent flickering when closing tabs
|
|
|
|
m_navigationContainer->setUpdatesEnabled(false);
|
|
|
|
m_navigationContainer->hide();
|
|
|
|
|
|
|
|
// Needed to prevent deleting m_navigationContainer in ~QWidget
|
|
|
|
m_navigationContainer->setParent(p_QupZilla);
|
|
|
|
}
|
|
|
|
|
2012-09-11 11:43:11 +02:00
|
|
|
delete m_locationBar.data();
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|