2014-03-17 10:43:18 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2016-02-14 11:17:36 +01:00
|
|
|
* Copyright (C) 2014-2016 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"
|
2014-03-18 20:00:34 +01:00
|
|
|
#include "iconprovider.h"
|
2014-03-17 10:43:18 +01:00
|
|
|
#include "tabbedwebview.h"
|
|
|
|
|
|
|
|
#include <QTimer>
|
2016-08-05 10:50:50 +02:00
|
|
|
#include <QMouseEvent>
|
2014-03-17 10:43:18 +01:00
|
|
|
|
|
|
|
#define ANIMATION_INTERVAL 70
|
|
|
|
|
|
|
|
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"));
|
|
|
|
|
2015-10-24 12:35:53 +02:00
|
|
|
m_animationPixmap = QIcon(QSL(":icons/other/loading.png")).pixmap(288, 16);
|
|
|
|
m_framesCount = m_animationPixmap.width() / m_animationPixmap.height();
|
2014-03-17 10:43:18 +01:00
|
|
|
|
2016-08-05 10:50:50 +02:00
|
|
|
m_audioPlayingPixmap = QIcon(QSL(":icons/other/audioplaying.png")).pixmap(15, 15);
|
|
|
|
m_audioMutedPixmap = QIcon(QSL(":icons/other/audiomuted.png")).pixmap(15, 15);
|
|
|
|
|
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-01-25 13:33:05 +01:00
|
|
|
resize(16, 16);
|
2014-03-18 20:00:34 +01:00
|
|
|
|
|
|
|
setIcon(IconProvider::emptyWebIcon());
|
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-10-24 21:19:27 +02:00
|
|
|
connect(m_tab->webView(), &WebView::iconChanged, this, &TabIcon::showIcon);
|
2014-03-17 10:43:18 +01:00
|
|
|
|
|
|
|
showIcon();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TabIcon::setIcon(const QIcon &icon)
|
|
|
|
{
|
2015-10-24 12:35:53 +02:00
|
|
|
m_sitePixmap = icon.pixmap(16);
|
2014-03-17 10:43:18 +01:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TabIcon::showLoadingAnimation()
|
|
|
|
{
|
|
|
|
m_currentFrame = 0;
|
|
|
|
|
2016-02-14 11:17:36 +01:00
|
|
|
updateAnimationFrame();
|
2014-03-17 10:43:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TabIcon::hideLoadingAnimation()
|
|
|
|
{
|
|
|
|
m_animationRunning = false;
|
|
|
|
|
|
|
|
m_updateTimer->stop();
|
|
|
|
showIcon();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TabIcon::showIcon()
|
|
|
|
{
|
2015-10-24 12:35:53 +02:00
|
|
|
m_sitePixmap = m_tab->icon().pixmap(16);
|
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();
|
|
|
|
m_currentFrame = (m_currentFrame + 1) % m_framesCount;
|
|
|
|
}
|
|
|
|
|
2016-08-05 10:50:50 +02:00
|
|
|
void TabIcon::updateAudioIcon(bool recentlyAudible)
|
|
|
|
{
|
|
|
|
if (m_tab->isMuted() || (!m_tab->isMuted() && recentlyAudible))
|
|
|
|
m_audioIconDisplayed = true;
|
|
|
|
else
|
|
|
|
m_audioIconDisplayed = false;
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2014-03-17 10:43:18 +01:00
|
|
|
void TabIcon::paintEvent(QPaintEvent* event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event);
|
|
|
|
|
|
|
|
QPainter p(this);
|
|
|
|
|
2016-01-27 12:26:30 +01:00
|
|
|
const int size = 16;
|
2016-07-17 09:38:01 +02:00
|
|
|
const int pixmapSize = qRound(size * m_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
|
|
|
|
|
|
|
if (m_animationRunning)
|
2016-01-27 12:26:30 +01:00
|
|
|
p.drawPixmap(r, m_animationPixmap, QRect(m_currentFrame * pixmapSize, 0, pixmapSize, pixmapSize));
|
2015-10-24 12:35:53 +02:00
|
|
|
else
|
2016-01-25 12:51:05 +01:00
|
|
|
p.drawPixmap(r, m_sitePixmap);
|
2016-08-05 10:50:50 +02:00
|
|
|
|
|
|
|
if (m_audioIconDisplayed) {
|
|
|
|
if (m_tab->isMuted())
|
|
|
|
p.drawPixmap(QPointF(width() * 0.25, 0), m_audioMutedPixmap);
|
|
|
|
else
|
|
|
|
p.drawPixmap(QPointF(width() * 0.25, 0), m_audioPlayingPixmap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TabIcon::mousePressEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
qreal x = event->localPos().x();
|
|
|
|
qreal y = event->localPos().y();
|
|
|
|
// if audio icon is clicked - we don't propagate mouse press to the tab
|
|
|
|
if (m_audioIconDisplayed && x >= width() * 0.25 && y < height() * 0.75)
|
|
|
|
m_tab->toggleMuted();
|
|
|
|
else
|
|
|
|
QWidget::mousePressEvent(event);
|
2014-03-17 10:43:18 +01:00
|
|
|
}
|