Skip to content
This repository has been archived by the owner on Jul 15, 2021. It is now read-only.

Commit

Permalink
[Update] Improve blur background système and allow its disable at com…
Browse files Browse the repository at this point in the history
…pilation
  • Loading branch information
citorva committed Jun 24, 2018
1 parent e16e33c commit 23291d7
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 91 deletions.
65 changes: 62 additions & 3 deletions Core/BrowserWindow.cpp
Expand Up @@ -50,6 +50,10 @@
#include "Widgets/Tab/TabWidget.hpp"
#include "Widgets/Tab/MainTabBar.hpp"

QT_BEGIN_NAMESPACE
extern Q_WIDGETS_EXPORT void qt_blurImage(QPainter *p, QImage &blurImage, qreal radius, bool quality, bool alphaOnly, int transposed = 0);
QT_END_NAMESPACE

namespace Sn
{
BrowserWindow::BrowserWindow(Application::WindowType type, const QUrl& url) :
Expand Down Expand Up @@ -474,11 +478,16 @@ int BrowserWindow::tabWidgetsCount() const
return m_tabWidgets.count();
}

const QPixmap *BrowserWindow::background()
const QImage *BrowserWindow::background()
{
return m_bg;
}

const QImage *BrowserWindow::processedBackground()
{
return m_blur_bg;
}

void BrowserWindow::setWindowTitle(const QString& title)
{
QString t{title};
Expand Down Expand Up @@ -581,11 +590,61 @@ void BrowserWindow::shotBackground()
m_fButton->hide();
m_titleBar->hide();

m_bg = new QPixmap(size());
render(m_bg, QPoint(), QRect(0, 0, width(), height()));
QPixmap *bg = new QPixmap(size());
render(bg, QPoint(), QRect(0, 0, width(), height()));
m_bg = new QImage(bg->toImage());
m_mainSplitter->show();
m_titleBar->show();
if(m_fButton) m_fButton->show();
m_blur_bg = new QImage(applyBlur(m_bg, 100));
}

QImage BrowserWindow::applyBlur(const QImage *src, qreal radius, bool quality, bool alphaOnly, int transposed)
{
QPixmap ret(src->size());
QPainter painter(&ret);
{
QPixmap big(QSize(src->width() + 2 * radius, src->height() + 2 * radius));
QPainter big_painter(&big);

big_painter.drawImage(QPoint(radius, radius), src->copy());

{
QPixmap left(QSize(1, big.height())),
right(QSize(1, big.height()));

QPainter painter_left(&left),
painter_right(&right);

painter_left.drawImage(QPoint(0, radius), src->copy());
painter_right.drawImage(QPoint(1 - src->width(), radius), src->copy());

for (int i = 0; i < radius; i++) {
big_painter.drawImage(QPoint(i, 0), left.toImage());
big_painter.drawImage(QPoint(radius + src->width() + i, 0), right.toImage());
}
}

{
QPixmap top(QSize(big.width(), 1)),
bottom(QSize(big.width(), 1));

QPainter painter_top(&top),
painter_bottom(&bottom);

painter_top.drawImage(QPoint(0, -radius), big.toImage());
painter_bottom.drawImage(QPoint(0, 1 + radius - big.height()), big.toImage());

for (int i = 0; i < radius; i++) {
big_painter.drawImage(QPoint(0, i), top.toImage());
big_painter.drawImage(QPoint(0, radius + src->height() + i), bottom.toImage());
}
}

qt_blurImage(&big_painter, big.toImage(), radius, quality, alphaOnly, transposed);
painter.drawImage(QPoint(-radius, -radius), big.toImage());
}
return ret.toImage();
}

void BrowserWindow::paintEvent(QPaintEvent* event)
Expand Down
7 changes: 5 additions & 2 deletions Core/BrowserWindow.hpp
Expand Up @@ -167,7 +167,8 @@ Q_OBJECT
StatusBarMessage* statusBarMessage() const { return m_statusBarMessage; }
TitleBar* titleBar() const { return m_titleBar; }

const QPixmap* background();
const QImage* background();
const QImage* processedBackground();

public slots:
void setWindowTitle(const QString& title);
Expand All @@ -183,6 +184,7 @@ public slots:

protected:
void shotBackground();
QImage applyBlur(const QImage *src, qreal radius, bool quality = true, bool alphaOnly = false, int transposed = 0);
void paintEvent(QPaintEvent* event);
void resizeEvent(QResizeEvent* event);

Expand Down Expand Up @@ -228,7 +230,8 @@ private slots:

RootFloatingButton* m_fButton{nullptr};

QPixmap* m_bg{ nullptr };
QImage* m_bg{ nullptr };
QImage* m_blur_bg{ nullptr };
bool m_upd_ss{ false };
};

Expand Down
92 changes: 31 additions & 61 deletions Core/Web/Tab/TabbedWebView.cpp
Expand Up @@ -40,12 +40,9 @@
#include "Widgets/Tab/TabBar.hpp"

#include "Web/WebPage.hpp"
#include "Web/Scripts.hpp"
#include "Web/WebHitTestResult.hpp"

QT_BEGIN_NAMESPACE
extern Q_WIDGETS_EXPORT void qt_blurImage(QPainter *p, QImage &blurImage, qreal radius, bool quality, bool alphaOnly, int transposed = 0);
QT_END_NAMESPACE

namespace Sn {
TabbedWebView::TabbedWebView(WebTab* tab) :
WebView(tab),
Expand All @@ -68,6 +65,9 @@ void TabbedWebView::setWebPage(WebPage* page)
setPage(page);

connect(page, &WebPage::linkHovered, this, &TabbedWebView::linkHovered);
#ifdef EXP_TRANSPARENT_BG
connect(page, &WebPage::pageRendering, this, &TabbedWebView::sPageRendering);
#endif // EXP_TRANSPARENT_BG
}

void TabbedWebView::setBrowserWindow(BrowserWindow* window)
Expand Down Expand Up @@ -126,73 +126,21 @@ void TabbedWebView::userLoadAction(const LoadRequest& request)
load(request);
}

QImage TabbedWebView::applyBlur(QImage src, qreal radius, bool quality, bool alphaOnly, int transposed)
{
QPixmap ret(src.size());
QPainter painter(&ret);
{
QPixmap big(QSize(src.width() + 2 * radius, src.height() + 2 * radius));
QPainter big_painter(&big);

big_painter.drawImage(QPoint(radius, radius), src);

{
QPixmap left(QSize(1, big.height())),
right(QSize(1, big.height()));

QPainter painter_left(&left),
painter_right(&right);

painter_left.drawImage(QPoint(0, radius), src);
painter_right.drawImage(QPoint(1 - src.width(), radius), src);

for (int i = 0; i < radius; i++) {
big_painter.drawImage(QPoint(i, 0), left.toImage());
big_painter.drawImage(QPoint(radius + src.width() + i, 0), right.toImage());
}
}

{
QPixmap top(QSize(big.width(), 1)),
bottom(QSize(big.width(), 1));

QPainter painter_top(&top),
painter_bottom(&bottom);

painter_top.drawImage(QPoint(0, -radius), big.toImage());
painter_bottom.drawImage(QPoint(0, 1 + radius - big.height()), big.toImage());

for (int i = 0; i < radius; i++) {
big_painter.drawImage(QPoint(0, i), top.toImage());
big_painter.drawImage(QPoint(0, radius + src.height() + i), bottom.toImage());
}
}

qt_blurImage(&big_painter, big.toImage(), radius, quality, alphaOnly, transposed);
painter.drawImage(QPoint(-radius, -radius), big.toImage());
}
return ret.toImage();
}
#ifdef EXP_TRANSPARENT_BG

void TabbedWebView::paintEvent(QPaintEvent* event)
{
QPainter painter(this);
if (m_window->background() != nullptr && isTransparent()) {
QPoint global_position = mapTo(m_window, QPoint(0, 0));
QRect shot_rect(global_position.x(), global_position.y(), width(), height());
if (m_processed_bg == nullptr)
{
m_processed_bg = new QImage(applyBlur(m_window->background()->toImage(), 100));
}
else if (m_processed_bg->size() != m_window->size())
{
m_processed_bg = new QImage(applyBlur(m_window->background()->toImage(), 100));
}
painter.drawImage(QPoint(), *m_processed_bg, shot_rect);
painter.drawImage(QPoint(), *m_window->processedBackground(), shot_rect);
}
WebView::paintEvent(event);
}

#endif // EXP_TRANSPARENT_BG

void TabbedWebView::sLoadStarted()
{
m_currentIp.clear();
Expand All @@ -208,11 +156,33 @@ void TabbedWebView::sLoadFinished()
QHostInfo::lookupHost(url().host(), this, SLOT(setIp(QHostInfo)));
}

#ifdef EXP_TRANSPARENT_BG
void TabbedWebView::sPageRendering()
{
page()->runJavaScript(Scripts::getAllMetaAttributes(), QWebEngineScript::ApplicationWorld, [this](const QVariant &res) {
const QVariantList& list = res.toList();

page()->setBackgroundColor(Qt::white);

foreach(const QVariant& val, list) {
const QVariantMap& meta = val.toMap();
QString name = meta.value(QStringLiteral("name")).toString();
QString content = meta.value(QStringLiteral("content")).toString();

if (name == "sielo-transparent-background" && content.toLower() != "false") {
page()->setBackgroundColor(Qt::transparent);

repaint();
}
}
});
}
#endif // EXP_TRANSPARENT_BG

void TabbedWebView::urlChanged(const QUrl& url)
{
if (Application::instance()->useTopToolBar() && (m_webTab->isCurrentTab() && m_webTab->tabBar()->tabWidget()))
m_webTab->tabBar()->tabWidget()->navigationToolBar()->refreshBackForwardButtons();

}

void TabbedWebView::linkHovered(const QString& link)
Expand Down
8 changes: 7 additions & 1 deletion Core/Web/Tab/TabbedWebView.hpp
Expand Up @@ -26,6 +26,8 @@
#ifndef SIELOBROWSER_TABBEDWEBVIEW_HPP
#define SIELOBROWSER_TABBEDWEBVIEW_HPP

#define EXP_TRANSPARENT_BG

#include <QLabel>
#include <QMenu>

Expand Down Expand Up @@ -74,14 +76,18 @@ public slots:
void setAsCurrentTab();
void userLoadAction(const LoadRequest& request);

#ifdef EXP_TRANSPARENT_BG
protected:
QImage applyBlur(QImage src, qreal radius, bool quality = true, bool alphaOnly = false, int transposed = 0);
void paintEvent(QPaintEvent* event);
#endif // EXP_TRANSPARENT_BG

private slots:
void sLoadStarted();
void sLoadProgress(int progress);
void sLoadFinished();
#ifdef EXP_TRANSPARENT_BG
void sPageRendering();
#endif // EXP_TRANSPARENT_BG
void urlChanged(const QUrl& url);
void linkHovered(const QString& link);
void setIp(const QHostInfo& info);
Expand Down
18 changes: 2 additions & 16 deletions Core/Web/WebPage.cpp
Expand Up @@ -264,23 +264,9 @@ void WebPage::finished()

cleanBlockedObject();

runJavaScript(Scripts::getAllMetaAttributes(), QWebEngineScript::ApplicationWorld, [this](const QVariant &res) {
const QVariantList& list = res.toList();

setBackgroundColor(Qt::white);

foreach(const QVariant& val, list) {
const QVariantMap& meta = val.toMap();
QString name = meta.value(QStringLiteral("name")).toString();
QString content = meta.value(QStringLiteral("content")).toString();

if (name == "sielo-transparent-background" && content.toLower() != "false") {
setBackgroundColor(Qt::transparent);
}
}
});

m_passwordEntries = Application::instance()->autoFill()->completePage(this, url());

emit pageRendering();
}

void WebPage::cleanBlockedObject()
Expand Down
1 change: 1 addition & 0 deletions Core/Web/WebPage.hpp
Expand Up @@ -75,6 +75,7 @@ Q_OBJECT
static QString setCSS(const QString& css);
signals:
void privacyChanged(bool status);
void pageRendering();

protected slots:
void progress(int progression);
Expand Down
12 changes: 12 additions & 0 deletions Core/Web/WebView.cpp
Expand Up @@ -48,6 +48,17 @@

#include "Plugins/PluginProxy.hpp"

void Output(const char* szFormat, ...)
{
char szBuff[1024];
va_list arg;
va_start(arg, szFormat);
_vsnprintf(szBuff, sizeof(szBuff), szFormat, arg);
va_end(arg);

OutputDebugString(szBuff);
}

namespace Sn {

bool WebView::isUrlValide(const QUrl& url)
Expand Down Expand Up @@ -222,6 +233,7 @@ void WebView::setPage(WebPage* page)
QWebEngineView::setPage(page);

connect(m_page, &WebPage::privacyChanged, this, &WebView::privacyChanged);
connect(m_page, &WebPage::pageRendering, this, &WebView::pageRendering);

zoomReset();
initActions();
Expand Down
4 changes: 3 additions & 1 deletion Core/Web/WebView.hpp
Expand Up @@ -91,6 +91,7 @@ Q_OBJECT
bool isTransparent() const;

signals:
void pageRendering();
void focusChanged(bool);
void viewportResized(QSize);
void privacyChanged(bool);
Expand Down Expand Up @@ -192,8 +193,9 @@ private slots:
QLabel* m_zoomLabel{nullptr};
QTimer* m_zoomTimer{nullptr};

WebPage* m_page{nullptr};
QPointer<QOpenGLWidget> m_child{};

WebPage* m_page{nullptr};
};
}

Expand Down
14 changes: 7 additions & 7 deletions Core/Widgets/FloatingButton.cpp
Expand Up @@ -204,14 +204,14 @@ void RootFloatingButton::expandAround(QPoint around)
int x = around.x() + radius * qCos(qDegreesToRadians(degrees));
int y = around.y() + radius * qSin(qDegreesToRadians(degrees));

foreach (FloatingButton* button, m_buttons) {
if (button->index() == i) {
button->move(around);
button->show();
button->moveButton(QPoint(x, y));
break;
}
foreach (FloatingButton* button, m_buttons) {
if (button->index() == i) {
button->move(around);
button->show();
button->moveButton(QPoint(x, y));
break;
}
}
}
}

Expand Down
Binary file modified ic_sielo.ico
Binary file not shown.

0 comments on commit 23291d7

Please sign in to comment.