2014-03-17 10:43:18 +01:00
|
|
|
/* ============================================================
|
2017-08-25 17:11:29 +02:00
|
|
|
* Falkon - Qt web browser
|
2018-01-13 15:36:58 +01:00
|
|
|
* Copyright (C) 2014-2018 David Rosca <nowrep@gmail.com>
|
2014-03-17 10:43:18 +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/>.
|
|
|
|
* ============================================================ */
|
|
|
|
#include "tabicon.h"
|
|
|
|
#include "webtab.h"
|
2016-12-30 17:13:15 +01:00
|
|
|
#include "webpage.h"
|
2014-03-18 20:00:34 +01:00
|
|
|
#include "iconprovider.h"
|
2014-03-17 10:43:18 +01:00
|
|
|
#include "tabbedwebview.h"
|
|
|
|
|
|
|
|
#include <QTimer>
|
2017-01-02 15:29:56 +01:00
|
|
|
#include <QToolTip>
|
2016-08-05 10:50:50 +02:00
|
|
|
#include <QMouseEvent>
|
2014-03-17 10:43:18 +01:00
|
|
|
|
|
|
|
#define ANIMATION_INTERVAL 70
|
|
|
|
|
2016-10-25 18:09:34 +02:00
|
|
|
TabIcon::Data *TabIcon::s_data = Q_NULLPTR;
|
|
|
|
|
2014-03-17 10:43:18 +01:00
|
|
|
TabIcon::TabIcon(QWidget* parent)
|
|
|
|
: QWidget(parent)
|
|
|
|
, m_tab(0)
|
|
|
|
, m_currentFrame(0)
|
|
|
|
, m_animationRunning(false)
|
2016-08-05 10:50:50 +02:00
|
|
|
, m_audioIconDisplayed(false)
|
2014-03-17 10:43:18 +01:00
|
|
|
{
|
|
|
|
setObjectName(QSL("tab-icon"));
|
|
|
|
|
2016-10-25 18:09:34 +02:00
|
|
|
if (!s_data) {
|
|
|
|
s_data = new TabIcon::Data;
|
|
|
|
s_data->animationPixmap = QIcon(QSL(":icons/other/loading.png")).pixmap(288, 16);
|
|
|
|
s_data->framesCount = s_data->animationPixmap.width() / s_data->animationPixmap.height();
|
2016-12-30 16:04:37 +01:00
|
|
|
s_data->audioPlayingPixmap = QIcon::fromTheme(QSL("audio-volume-high"), QIcon(QSL(":icons/other/audioplaying.svg"))).pixmap(16);
|
|
|
|
s_data->audioMutedPixmap = QIcon::fromTheme(QSL("audio-volume-muted"), QIcon(QSL(":icons/other/audiomuted.svg"))).pixmap(16);
|
2016-10-25 18:09:34 +02:00
|
|
|
}
|
2016-08-05 10:50:50 +02:00
|
|
|
|
2014-03-17 10:43:18 +01:00
|
|
|
m_updateTimer = new QTimer(this);
|
|
|
|
m_updateTimer->setInterval(ANIMATION_INTERVAL);
|
|
|
|
connect(m_updateTimer, SIGNAL(timeout()), this, SLOT(updateAnimationFrame()));
|
|
|
|
|
2016-12-30 17:13:15 +01:00
|
|
|
m_hideTimer = new QTimer(this);
|
|
|
|
m_hideTimer->setInterval(250);
|
|
|
|
connect(m_hideTimer, &QTimer::timeout, this, &TabIcon::hide);
|
2014-03-18 20:00:34 +01:00
|
|
|
|
2016-12-30 17:13:15 +01:00
|
|
|
resize(16, 16);
|
2014-03-17 10:43:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TabIcon::setWebTab(WebTab* tab)
|
|
|
|
{
|
|
|
|
m_tab = tab;
|
|
|
|
|
2014-03-18 20:00:34 +01:00
|
|
|
connect(m_tab->webView(), SIGNAL(loadStarted()), this, SLOT(showLoadingAnimation()));
|
|
|
|
connect(m_tab->webView(), SIGNAL(loadFinished(bool)), this, SLOT(hideLoadingAnimation()));
|
2016-12-30 17:13:15 +01:00
|
|
|
connect(m_tab->webView(), &WebView::iconChanged, this, &TabIcon::updateIcon);
|
2017-01-20 15:09:57 +01:00
|
|
|
connect(m_tab->webView(), &WebView::backgroundActivityChanged, this, [this]() { update(); });
|
2018-01-28 22:14:52 +01:00
|
|
|
|
|
|
|
auto pageChanged = [this](WebPage *page) {
|
|
|
|
connect(page, &QWebEnginePage::recentlyAudibleChanged, this, &TabIcon::updateAudioIcon);
|
|
|
|
};
|
|
|
|
pageChanged(m_tab->webView()->page());
|
|
|
|
connect(m_tab->webView(), &WebView::pageChanged, this, pageChanged);
|
2014-03-17 10:43:18 +01:00
|
|
|
|
2016-12-30 17:13:15 +01:00
|
|
|
updateIcon();
|
2014-03-17 10:43:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TabIcon::showLoadingAnimation()
|
|
|
|
{
|
|
|
|
m_currentFrame = 0;
|
|
|
|
|
2016-02-14 11:17:36 +01:00
|
|
|
updateAnimationFrame();
|
2016-12-30 17:13:15 +01:00
|
|
|
show();
|
2014-03-17 10:43:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TabIcon::hideLoadingAnimation()
|
|
|
|
{
|
|
|
|
m_animationRunning = false;
|
|
|
|
|
|
|
|
m_updateTimer->stop();
|
2016-12-30 17:13:15 +01:00
|
|
|
updateIcon();
|
2014-03-17 10:43:18 +01:00
|
|
|
}
|
|
|
|
|
2016-12-30 17:13:15 +01:00
|
|
|
void TabIcon::updateIcon()
|
2014-03-17 10:43:18 +01:00
|
|
|
{
|
2016-12-30 17:13:15 +01:00
|
|
|
m_sitePixmap = m_tab->icon(/*allowNull*/ true).pixmap(16);
|
|
|
|
if (m_sitePixmap.isNull()) {
|
2017-08-25 17:11:29 +02:00
|
|
|
if (m_tab->url().isEmpty() || m_tab->url().scheme() == QL1S("falkon")) {
|
2017-02-07 09:29:21 +01:00
|
|
|
hide();
|
|
|
|
} else {
|
|
|
|
m_hideTimer->start();
|
|
|
|
}
|
2016-12-30 17:13:15 +01:00
|
|
|
} else {
|
|
|
|
show();
|
|
|
|
}
|
2014-03-17 10:43:18 +01:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TabIcon::updateAnimationFrame()
|
|
|
|
{
|
|
|
|
if (!m_animationRunning) {
|
2016-02-14 11:17:36 +01:00
|
|
|
m_updateTimer->start();
|
2014-03-17 10:43:18 +01:00
|
|
|
m_animationRunning = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
update();
|
2016-10-25 18:09:34 +02:00
|
|
|
m_currentFrame = (m_currentFrame + 1) % s_data->framesCount;
|
2014-03-17 10:43:18 +01:00
|
|
|
}
|
|
|
|
|
2016-12-30 17:13:15 +01:00
|
|
|
void TabIcon::show()
|
|
|
|
{
|
|
|
|
if (!shouldBeVisible()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_hideTimer->stop();
|
|
|
|
|
|
|
|
if (isVisible()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setFixedSize(16, 16);
|
|
|
|
emit resized();
|
|
|
|
QWidget::show();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TabIcon::hide()
|
|
|
|
{
|
|
|
|
if (shouldBeVisible() || isHidden()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit resized();
|
2018-01-13 15:36:58 +01:00
|
|
|
setFixedSize(1, 16);
|
2016-12-30 17:13:15 +01:00
|
|
|
QWidget::hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TabIcon::shouldBeVisible() const
|
|
|
|
{
|
2016-12-31 17:23:06 +01:00
|
|
|
if (m_tab && m_tab->isPinned()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return !m_sitePixmap.isNull() || m_animationRunning || m_audioIconDisplayed || (m_tab && m_tab->isPinned());
|
2016-12-30 17:13:15 +01:00
|
|
|
}
|
|
|
|
|
2017-01-02 15:29:56 +01:00
|
|
|
bool TabIcon::event(QEvent *event)
|
|
|
|
{
|
|
|
|
if (event->type() == QEvent::ToolTip) {
|
|
|
|
QHelpEvent *e = static_cast<QHelpEvent*>(event);
|
|
|
|
if (m_audioIconDisplayed && m_audioIconRect.contains(e->pos())) {
|
|
|
|
QToolTip::showText(e->globalPos(), m_tab->isMuted() ? tr("Unmute Tab") : tr("Mute Tab"), this);
|
|
|
|
event->accept();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return QWidget::event(event);
|
|
|
|
}
|
|
|
|
|
2016-08-05 10:50:50 +02:00
|
|
|
void TabIcon::updateAudioIcon(bool recentlyAudible)
|
|
|
|
{
|
2016-10-25 17:59:27 +02:00
|
|
|
if (m_tab->isMuted() || (!m_tab->isMuted() && recentlyAudible)) {
|
2016-08-05 10:50:50 +02:00
|
|
|
m_audioIconDisplayed = true;
|
2016-12-30 17:13:15 +01:00
|
|
|
show();
|
2016-10-25 17:59:27 +02:00
|
|
|
} else {
|
2016-08-05 10:50:50 +02:00
|
|
|
m_audioIconDisplayed = false;
|
2016-12-30 17:13:15 +01:00
|
|
|
hide();
|
2016-10-25 17:59:27 +02:00
|
|
|
}
|
2016-08-05 10:50:50 +02:00
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2014-03-17 10:43:18 +01:00
|
|
|
void TabIcon::paintEvent(QPaintEvent* event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event);
|
|
|
|
|
|
|
|
QPainter p(this);
|
2017-01-20 15:09:57 +01:00
|
|
|
p.setRenderHint(QPainter::Antialiasing);
|
2014-03-17 10:43:18 +01:00
|
|
|
|
2016-01-27 12:26:30 +01:00
|
|
|
const int size = 16;
|
2016-10-25 18:09:34 +02:00
|
|
|
const int pixmapSize = qRound(size * s_data->animationPixmap.devicePixelRatioF());
|
2016-01-25 12:51:05 +01:00
|
|
|
|
|
|
|
// Center the pixmap in rect
|
|
|
|
QRect r = rect();
|
|
|
|
r.setX((r.width() - size) / 2);
|
|
|
|
r.setY((r.height() - size) / 2);
|
|
|
|
r.setWidth(size);
|
|
|
|
r.setHeight(size);
|
2015-10-24 12:35:53 +02:00
|
|
|
|
2016-12-30 17:13:15 +01:00
|
|
|
if (m_animationRunning) {
|
|
|
|
p.drawPixmap(r, s_data->animationPixmap, QRect(m_currentFrame * pixmapSize, 0, pixmapSize, pixmapSize));
|
2017-01-02 13:31:22 +01:00
|
|
|
} else if (m_audioIconDisplayed && !m_tab->isPinned()) {
|
|
|
|
m_audioIconRect = r;
|
2016-12-30 17:13:15 +01:00
|
|
|
p.drawPixmap(r, m_tab->isMuted() ? s_data->audioMutedPixmap : s_data->audioPlayingPixmap);
|
2016-12-31 17:23:06 +01:00
|
|
|
} else if (!m_sitePixmap.isNull()) {
|
2016-12-30 17:13:15 +01:00
|
|
|
p.drawPixmap(r, m_sitePixmap);
|
2016-12-31 17:23:06 +01:00
|
|
|
} else if (m_tab && m_tab->isPinned()) {
|
|
|
|
p.drawPixmap(r, IconProvider::emptyWebIcon().pixmap(size));
|
2016-08-05 10:50:50 +02:00
|
|
|
}
|
2017-01-01 21:34:45 +01:00
|
|
|
|
|
|
|
// Draw audio icon on top of site icon for pinned tabs
|
2017-01-02 13:31:22 +01:00
|
|
|
if (!m_animationRunning && m_audioIconDisplayed && m_tab->isPinned()) {
|
|
|
|
const int s = size - 4;
|
|
|
|
const QRect r(width() - s, 0, s, s);
|
|
|
|
m_audioIconRect = r;
|
|
|
|
QColor c = palette().color(QPalette::Window);
|
|
|
|
c.setAlpha(180);
|
|
|
|
p.setPen(c);
|
|
|
|
p.setBrush(c);
|
|
|
|
p.drawEllipse(r);
|
2017-01-01 21:34:45 +01:00
|
|
|
p.drawPixmap(r, m_tab->isMuted() ? s_data->audioMutedPixmap : s_data->audioPlayingPixmap);
|
|
|
|
}
|
2017-01-20 15:09:57 +01:00
|
|
|
|
|
|
|
// Draw background activity indicator
|
|
|
|
if (m_tab && m_tab->isPinned() && m_tab->webView()->backgroundActivity()) {
|
|
|
|
const int s = 5;
|
|
|
|
// Background
|
|
|
|
const QRect r1(width() - s - 2, height() - s - 2, s + 2, s + 2);
|
|
|
|
QColor c1 = palette().color(QPalette::Window);
|
|
|
|
c1.setAlpha(180);
|
|
|
|
p.setPen(Qt::transparent);
|
|
|
|
p.setBrush(c1);
|
|
|
|
p.drawEllipse(r1);
|
|
|
|
// Foreground
|
|
|
|
const QRect r2(width() - s - 1, height() - s - 1, s, s);
|
|
|
|
QColor c2 = palette().color(QPalette::Text);
|
|
|
|
p.setPen(Qt::transparent);
|
|
|
|
p.setBrush(c2);
|
|
|
|
p.drawEllipse(r2);
|
|
|
|
}
|
2016-08-05 10:50:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TabIcon::mousePressEvent(QMouseEvent *event)
|
|
|
|
{
|
2016-10-25 17:59:27 +02:00
|
|
|
// If audio icon is clicked - we don't propagate mouse press to the tab
|
2017-01-02 13:31:22 +01:00
|
|
|
if (m_audioIconDisplayed && event->button() == Qt::LeftButton && m_audioIconRect.contains(event->pos())) {
|
2016-08-05 10:50:50 +02:00
|
|
|
m_tab->toggleMuted();
|
2017-01-02 13:31:22 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QWidget::mousePressEvent(event);
|
2014-03-17 10:43:18 +01:00
|
|
|
}
|