From 68e0425bde65e43397d84472dd0eec0c1fcfc510 Mon Sep 17 00:00:00 2001 From: David Rosca Date: Fri, 30 Dec 2016 17:13:15 +0100 Subject: [PATCH] Hide tab icon when page icon is null Completely hide the tab icon instead of showing generic webpage icon when page icon is null. --- src/lib/tabwidget/tabicon.cpp | 79 ++++++++++++++++++++++++---------- src/lib/tabwidget/tabicon.h | 13 ++++-- src/lib/tools/iconprovider.cpp | 31 ++++++------- src/lib/tools/iconprovider.h | 8 ++-- src/lib/webengine/webview.cpp | 4 +- src/lib/webengine/webview.h | 2 +- src/lib/webtab/webtab.cpp | 22 +++++++--- src/lib/webtab/webtab.h | 2 +- 8 files changed, 107 insertions(+), 54 deletions(-) diff --git a/src/lib/tabwidget/tabicon.cpp b/src/lib/tabwidget/tabicon.cpp index 28da9f495..0aa36098d 100644 --- a/src/lib/tabwidget/tabicon.cpp +++ b/src/lib/tabwidget/tabicon.cpp @@ -17,6 +17,7 @@ * ============================================================ */ #include "tabicon.h" #include "webtab.h" +#include "webpage.h" #include "iconprovider.h" #include "tabbedwebview.h" @@ -48,9 +49,11 @@ TabIcon::TabIcon(QWidget* parent) m_updateTimer->setInterval(ANIMATION_INTERVAL); connect(m_updateTimer, SIGNAL(timeout()), this, SLOT(updateAnimationFrame())); - resize(16, 16); + m_hideTimer = new QTimer(this); + m_hideTimer->setInterval(250); + connect(m_hideTimer, &QTimer::timeout, this, &TabIcon::hide); - setIcon(IconProvider::emptyWebIcon()); + resize(16, 16); } void TabIcon::setWebTab(WebTab* tab) @@ -59,15 +62,10 @@ void TabIcon::setWebTab(WebTab* tab) connect(m_tab->webView(), SIGNAL(loadStarted()), this, SLOT(showLoadingAnimation())); connect(m_tab->webView(), SIGNAL(loadFinished(bool)), this, SLOT(hideLoadingAnimation())); - connect(m_tab->webView(), &WebView::iconChanged, this, &TabIcon::showIcon); + connect(m_tab->webView(), &WebView::iconChanged, this, &TabIcon::updateIcon); + connect(m_tab->webView()->page(), &QWebEnginePage::recentlyAudibleChanged, this, &TabIcon::updateAudioIcon); - showIcon(); -} - -void TabIcon::setIcon(const QIcon &icon) -{ - m_sitePixmap = icon.pixmap(16); - update(); + updateIcon(); } void TabIcon::showLoadingAnimation() @@ -75,6 +73,7 @@ void TabIcon::showLoadingAnimation() m_currentFrame = 0; updateAnimationFrame(); + show(); } void TabIcon::hideLoadingAnimation() @@ -82,12 +81,17 @@ void TabIcon::hideLoadingAnimation() m_animationRunning = false; m_updateTimer->stop(); - showIcon(); + updateIcon(); } -void TabIcon::showIcon() +void TabIcon::updateIcon() { - m_sitePixmap = m_tab->icon().pixmap(16); + m_sitePixmap = m_tab->icon(/*allowNull*/ true).pixmap(16); + if (m_sitePixmap.isNull()) { + m_hideTimer->start(); + } else { + show(); + } update(); } @@ -102,14 +106,49 @@ void TabIcon::updateAnimationFrame() m_currentFrame = (m_currentFrame + 1) % s_data->framesCount; } +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(); + setFixedSize(0, 0); + QWidget::hide(); +} + +bool TabIcon::shouldBeVisible() const +{ + return !m_sitePixmap.isNull() || m_animationRunning || m_audioIconDisplayed; +} + void TabIcon::updateAudioIcon(bool recentlyAudible) { if (m_tab->isMuted() || (!m_tab->isMuted() && recentlyAudible)) { setToolTip(m_tab->isMuted() ? tr("Unmute Tab") : tr("Mute Tab")); m_audioIconDisplayed = true; + show(); } else { setToolTip(QString()); m_audioIconDisplayed = false; + hide(); } update(); @@ -131,16 +170,12 @@ void TabIcon::paintEvent(QPaintEvent* event) r.setWidth(size); r.setHeight(size); - if (m_audioIconDisplayed) { - if (m_tab->isMuted()) - p.drawPixmap(r, s_data->audioMutedPixmap); - else - p.drawPixmap(r, s_data->audioPlayingPixmap); + if (m_animationRunning) { + p.drawPixmap(r, s_data->animationPixmap, QRect(m_currentFrame * pixmapSize, 0, pixmapSize, pixmapSize)); + } else if (m_audioIconDisplayed) { + p.drawPixmap(r, m_tab->isMuted() ? s_data->audioMutedPixmap : s_data->audioPlayingPixmap); } else { - if (m_animationRunning) - p.drawPixmap(r, s_data->animationPixmap, QRect(m_currentFrame * pixmapSize, 0, pixmapSize, pixmapSize)); - else - p.drawPixmap(r, m_sitePixmap); + p.drawPixmap(r, m_sitePixmap); } } diff --git a/src/lib/tabwidget/tabicon.h b/src/lib/tabwidget/tabicon.h index 665afe08b..9082a6b4b 100644 --- a/src/lib/tabwidget/tabicon.h +++ b/src/lib/tabwidget/tabicon.h @@ -35,22 +35,29 @@ public: explicit TabIcon(QWidget* parent = 0); void setWebTab(WebTab* tab); - void setIcon(const QIcon &icon); - void updateAudioIcon(bool recentlyAudible); + void updateIcon(); + +signals: + void resized(); private slots: - void showIcon(); void showLoadingAnimation(); void hideLoadingAnimation(); + void updateAudioIcon(bool recentlyAudible); void updateAnimationFrame(); private: + void show(); + void hide(); + bool shouldBeVisible() const; + void paintEvent(QPaintEvent* event); void mousePressEvent(QMouseEvent* event); WebTab* m_tab; QTimer* m_updateTimer; + QTimer* m_hideTimer; QPixmap m_sitePixmap; int m_currentFrame; bool m_animationRunning; diff --git a/src/lib/tools/iconprovider.cpp b/src/lib/tools/iconprovider.cpp index 06d5dc568..47a4d72ef 100644 --- a/src/lib/tools/iconprovider.cpp +++ b/src/lib/tools/iconprovider.cpp @@ -49,6 +49,11 @@ void IconProvider::saveIcon(WebView* view) return; } + const QIcon icon = view->icon(true); + if (icon.isNull()) { + return; + } + const QStringList ignoredSchemes = { QStringLiteral("qupzilla"), QStringLiteral("ftp"), @@ -64,11 +69,7 @@ void IconProvider::saveIcon(WebView* view) BufferedIcon item; item.first = view->url(); - item.second = view->icon().pixmap(32).toImage(); - - if (item.second == IconProvider::emptyWebImage()) { - return; - } + item.second = icon.pixmap(16).toImage(); if (m_iconBuffer.contains(item)) { return; @@ -172,15 +173,15 @@ QImage IconProvider::emptyWebImage() return instance()->m_emptyWebImage; } -QIcon IconProvider::iconForUrl(const QUrl &url, bool allowEmpty) +QIcon IconProvider::iconForUrl(const QUrl &url, bool allowNull) { - return instance()->iconFromImage(imageForUrl(url, allowEmpty)); + return instance()->iconFromImage(imageForUrl(url, allowNull)); } -QImage IconProvider::imageForUrl(const QUrl &url, bool allowEmpty) +QImage IconProvider::imageForUrl(const QUrl &url, bool allowNull) { if (url.path().isEmpty()) { - return allowEmpty ? QImage() : IconProvider::emptyWebImage(); + return allowNull ? QImage() : IconProvider::emptyWebImage(); } const QByteArray encodedUrl = encodeUrl(url); @@ -200,18 +201,18 @@ QImage IconProvider::imageForUrl(const QUrl &url, bool allowEmpty) return QImage::fromData(query.value(0).toByteArray()); } - return allowEmpty ? QImage() : IconProvider::emptyWebImage(); + return allowNull ? QImage() : IconProvider::emptyWebImage(); } -QIcon IconProvider::iconForDomain(const QUrl &url, bool allowEmpty) +QIcon IconProvider::iconForDomain(const QUrl &url, bool allowNull) { - return instance()->iconFromImage(imageForDomain(url, allowEmpty)); + return instance()->iconFromImage(imageForDomain(url, allowNull)); } -QImage IconProvider::imageForDomain(const QUrl &url, bool allowEmpty) +QImage IconProvider::imageForDomain(const QUrl &url, bool allowNull) { if (url.host().isEmpty()) { - return allowEmpty ? QImage() : IconProvider::emptyWebImage(); + return allowNull ? QImage() : IconProvider::emptyWebImage(); } foreach (const BufferedIcon &ic, instance()->m_iconBuffer) { @@ -230,7 +231,7 @@ QImage IconProvider::imageForDomain(const QUrl &url, bool allowEmpty) return QImage::fromData(query.value(0).toByteArray()); } - return allowEmpty ? QImage() : IconProvider::emptyWebImage(); + return allowNull ? QImage() : IconProvider::emptyWebImage(); } IconProvider* IconProvider::instance() diff --git a/src/lib/tools/iconprovider.h b/src/lib/tools/iconprovider.h index 5d77cb176..0532dc38c 100644 --- a/src/lib/tools/iconprovider.h +++ b/src/lib/tools/iconprovider.h @@ -59,12 +59,12 @@ public: static QImage emptyWebImage(); // Icon for url (only available for urls in history) - static QIcon iconForUrl(const QUrl &url, bool allowEmpty = false); - static QImage imageForUrl(const QUrl &url, bool allowEmpty = false); + static QIcon iconForUrl(const QUrl &url, bool allowNull = false); + static QImage imageForUrl(const QUrl &url, bool allowNull = false); // Icon for domain (only available for urls in history) - static QIcon iconForDomain(const QUrl &url, bool allowEmpty = false); - static QImage imageForDomain(const QUrl &url, bool allowEmpty = false); + static QIcon iconForDomain(const QUrl &url, bool allowNull = false); + static QImage imageForDomain(const QUrl &url, bool allowNull = false); static IconProvider* instance(); diff --git a/src/lib/webengine/webview.cpp b/src/lib/webengine/webview.cpp index 8c8d5de30..99c529d7e 100644 --- a/src/lib/webengine/webview.cpp +++ b/src/lib/webengine/webview.cpp @@ -93,7 +93,7 @@ WebView::~WebView() WebScrollBarManager::instance()->removeWebView(this); } -QIcon WebView::icon() const +QIcon WebView::icon(bool allowNull) const { if (!QWebEngineView::icon().isNull()) { return QWebEngineView::icon(); @@ -107,7 +107,7 @@ QIcon WebView::icon() const return IconProvider::standardIcon(QStyle::SP_DriveHDIcon); } - return IconProvider::iconForUrl(url()); + return IconProvider::iconForUrl(url(), allowNull); } QString WebView::title() const diff --git a/src/lib/webengine/webview.h b/src/lib/webengine/webview.h index 88e24147e..85ba54c40 100644 --- a/src/lib/webengine/webview.h +++ b/src/lib/webengine/webview.h @@ -36,7 +36,7 @@ public: explicit WebView(QWidget* parent = 0); ~WebView(); - QIcon icon() const; + QIcon icon(bool allowNull = false) const; QString title() const; bool isTitleEmpty() const; diff --git a/src/lib/webtab/webtab.cpp b/src/lib/webtab/webtab.cpp index 871cefbc0..211b4ba56 100644 --- a/src/lib/webtab/webtab.cpp +++ b/src/lib/webtab/webtab.cpp @@ -27,6 +27,7 @@ #include "qztools.h" #include "qzsettings.h" #include "mainapplication.h" +#include "iconprovider.h" #include #include @@ -47,7 +48,7 @@ WebTab::SavedTab::SavedTab(WebTab* webTab) { title = webTab->title(); url = webTab->url(); - icon = webTab->icon(); + icon = webTab->icon(true); history = webTab->historyData(); isPinned = webTab->isPinned(); zoomLevel = webTab->zoomLevel(); @@ -142,7 +143,13 @@ WebTab::WebTab(BrowserWindow* window) connect(m_webView, SIGNAL(loadStarted()), this, SLOT(loadStarted())); connect(m_webView, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished())); connect(m_webView, SIGNAL(titleChanged(QString)), this, SLOT(titleChanged(QString))); - connect(m_webView->page(), &QWebEnginePage::recentlyAudibleChanged, tabIcon(), &TabIcon::updateAudioIcon); + + // Workaround QTabBar not immediately noticing resizing of tab buttons + connect(m_tabIcon, &TabIcon::resized, this, [this]() { + if (m_tabBar) { + m_tabBar->setTabButton(tabIndex(), m_tabBar->iconButtonPosition(), m_tabIcon); + } + }); } TabbedWebView* WebTab::webView() const @@ -196,14 +203,17 @@ QString WebTab::title() const } } -QIcon WebTab::icon() const +QIcon WebTab::icon(bool allowNull) const { if (isRestored()) { - return m_webView->icon(); + return m_webView->icon(allowNull); } - else { + + if (allowNull || !m_savedTab.icon.isNull()) { return m_savedTab.icon; } + + return IconProvider::emptyWebIcon(); } QWebEngineHistory* WebTab::history() const @@ -336,7 +346,7 @@ void WebTab::restoreTab(const WebTab::SavedTab &tab) m_tabBar->setTabText(index, tab.title); m_locationBar->showUrl(tab.url); - m_tabIcon->setIcon(tab.icon); + m_tabIcon->updateIcon(); if (!tab.url.isEmpty()) { QColor col = m_tabBar->palette().text().color(); diff --git a/src/lib/webtab/webtab.h b/src/lib/webtab/webtab.h index 7e5fe5a7a..ccc0c7548 100644 --- a/src/lib/webtab/webtab.h +++ b/src/lib/webtab/webtab.h @@ -65,7 +65,7 @@ public: QUrl url() const; QString title() const; - QIcon icon() const; + QIcon icon(bool allowNull = false) const; QWebEngineHistory* history() const; int zoomLevel() const; void setZoomLevel(int level);