Falkon Develop
Cross-platform Qt-based web browser
sbi_imagesicon.cpp
Go to the documentation of this file.
1/* ============================================================
2* StatusBarIcons - Extra icons in statusbar for Falkon
3* Copyright (C) 2013-2018 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "sbi_imagesicon.h"
19#include "browserwindow.h"
20#include "tabwidget.h"
21#include "tabbedwebview.h"
22#include "webpage.h"
23#include "mainapplication.h"
24
25#include <QGraphicsColorizeEffect>
26#include <QSettings>
27#include <QMenu>
28
29SBI_ImagesIcon::SBI_ImagesIcon(BrowserWindow* window, const QString &settingsPath)
30 : SBI_Icon(window, settingsPath)
31{
32 setObjectName(QSL("sbi_imagesicon"));
33 setCursor(Qt::PointingHandCursor);
34 setToolTip(tr("Modify images loading settings per-site and globally"));
35
36 m_icon = QIcon::fromTheme(QSL("image-x-generic"), QIcon(QSL(":sbi/data/images.png")));
37 setPixmap(m_icon.pixmap(16));
38
39 QSettings settings(m_settingsFile, QSettings::IniFormat);
40 settings.beginGroup("StatusBarIcons_Images");
41 m_loadingImages = settings.value("LoadImages", true).toBool();
42 settings.endGroup();
43
44 mApp->webSettings()->setAttribute(QWebEngineSettings::AutoLoadImages, m_loadingImages);
45
46 updateIcon();
47
48 connect(m_window->tabWidget(), SIGNAL(currentChanged(int)), this, SLOT(updateIcon()));
49 connect(this, &ClickableLabel::clicked, this, &SBI_ImagesIcon::showMenu);
50}
51
52void SBI_ImagesIcon::showMenu(const QPoint &point)
53{
54 QFont boldFont = font();
55 boldFont.setBold(true);
56
57 QMenu menu;
58 menu.addAction(m_icon, tr("Current Page Settings"))->setFont(boldFont);
59
60 if (testCurrentPageWebAttribute(QWebEngineSettings::AutoLoadImages)) {
61 menu.addAction(tr("Disable loading images (temporarily)"), this, &SBI_ImagesIcon::toggleLoadingImages);
62 }
63 else {
64 menu.addAction(tr("Enable loading images (temporarily)"), this, &SBI_ImagesIcon::toggleLoadingImages);
65 }
66
67 menu.addSeparator();
68 menu.addAction(m_icon, tr("Global Settings"))->setFont(boldFont);
69
70 QAction* act = menu.addAction(tr("Automatically load images"));
71 act->setCheckable(true);
72 act->setChecked(m_loadingImages);
73 connect(act, &QAction::toggled, this, &SBI_ImagesIcon::setGlobalLoadingImages);
74
75 menu.exec(point);
76}
77
78void SBI_ImagesIcon::toggleLoadingImages()
79{
80 bool current = testCurrentPageWebAttribute(QWebEngineSettings::AutoLoadImages);
81 setCurrentPageWebAttribute(QWebEngineSettings::AutoLoadImages, !current);
82
83 // We should reload page on disabling images
84 if (current) {
85 m_window->weView()->reload();
86 }
87
88 updateIcon();
89}
90
91void SBI_ImagesIcon::setGlobalLoadingImages(bool enable)
92{
93 // Save it permanently
94 QSettings settings(m_settingsFile, QSettings::IniFormat);
95 settings.beginGroup("StatusBarIcons_Images");
96 settings.setValue("LoadImages", enable);
97 settings.endGroup();
98
99 // Switch it in websettings
100 m_loadingImages = enable;
101 mApp->webSettings()->setAttribute(QWebEngineSettings::AutoLoadImages, m_loadingImages);
102 updateIcon();
103
104 // We should reload page on disabling images
105 if (!enable) {
106 m_window->weView()->reload();
107 }
108}
109
110void SBI_ImagesIcon::updateIcon()
111{
112 if (testCurrentPageWebAttribute(QWebEngineSettings::AutoLoadImages)) {
113 setGraphicsEffect(nullptr);
114 }
115 else {
116 auto* effect = new QGraphicsColorizeEffect(this);
117 effect->setColor(Qt::gray);
118 setGraphicsEffect(effect);
119 }
120}
TabWidget * tabWidget() const
TabbedWebView * weView() const
void clicked(QPoint)
BrowserWindow * m_window
Definition: sbi_icon.h:42
QString m_settingsFile
Definition: sbi_icon.h:43
void setCurrentPageWebAttribute(QWebEngineSettings::WebAttribute attr, bool value)
Definition: sbi_icon.cpp:35
bool testCurrentPageWebAttribute(QWebEngineSettings::WebAttribute attr) const
Definition: sbi_icon.cpp:30
SBI_ImagesIcon(BrowserWindow *window, const QString &settingsPath)
#define mApp
#define QSL(x)
Definition: qzcommon.h:40