--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WebBrowser/StatusBar/ImagesIcon.py Thu May 05 16:24:20 2016 +0200 @@ -0,0 +1,157 @@ +# -*- coding: utf-8 -*- + +# Copyright (c) 2016 Detlev Offenbach <detlev@die-offenbachs.de> +# + +""" +Module implementing the images loading status bar icon. +""" + +# +# This is modeled after the code found in Qupzilla +# Copyright (C) 2014 David Rosca <nowrep@gmail.com> +# + +from __future__ import unicode_literals + +from PyQt5.QtCore import pyqtSlot, Qt, QPoint +from PyQt5.QtWidgets import QGraphicsColorizeEffect, QMenu +from PyQt5.QtWebEngineWidgets import QWebEngineSettings + +from .StatusBarIcon import StatusBarIcon + +import UI.PixmapCache +import Preferences + + +class ImagesIcon(StatusBarIcon): + """ + Class implementing the images loading status bar icon. + """ + def __init__(self, window): + """ + Constructor + + @param window reference to the web browser window + @type WebBrowserWindow + """ + super(ImagesIcon, self).__init__(window) + + self.setToolTip(self.tr("Modify images loading settings temporarily" + " or globally")) + self.__icon = UI.PixmapCache.getPixmap("filePixmap").scaled(16, 16) + self.setPixmap(self.__icon) + + self._window.tabWidget().currentChanged.connect(self.__updateIcon) + self.clicked.connect(self.__showMenu) + + self.__updateIcon() + + def preferencesChanged(self): + """ + Public method to handle changes of the settings. + """ + self.__updateIcon() + + @pyqtSlot(QPoint) + def __showMenu(self, pos): + """ + Private slot to show the menu. + + @param pos position to show the menu at + @type QPoint + """ + boldFont = self.font() + boldFont.setBold(True) + + menu = QMenu() + menu.addAction(self.tr("Current Page Settings")).setFont(boldFont) + + if self._testCurrentPageWebAttribute( + QWebEngineSettings.AutoLoadImages): + menu.addAction(self.tr("Disable loading images (temporarily)"), + self.__toggleLoadingImages) + else: + menu.addAction(self.tr("Enable loading images (temporarily)"), + self.__toggleLoadingImages) + + menu.addSeparator() + menu.addAction(self.tr("Global Settings")).setFont(boldFont) + act = menu.addAction(self.tr("Automatically load images")) + act.setCheckable(True) + act.setChecked(Preferences.getWebBrowser("AutoLoadImages")) + act.toggled.connect(self.__setGlobalLoadingImages) + + menu.exec(pos) + + @pyqtSlot() + def __updateIcon(self): + """ + Private slot to update the icon. + """ + if self._testCurrentPageWebAttribute( + QWebEngineSettings.AutoLoadImages): + self.setGraphicsEffect(None) + else: + effect = QGraphicsColorizeEffect(self) + effect.setColor(Qt.gray) + self.setGraphicsEffect(effect) + + @pyqtSlot() + def __toggleLoadingImages(self): + """ + Private slot to toggle the images loading setting. + """ + if self._currentPage() is None: + return + + current = self._testCurrentPageWebAttribute( + QWebEngineSettings.AutoLoadImages) + self._setCurrentPageWebAttribute(QWebEngineSettings.AutoLoadImages, + not current) + + if current: + # reload page upon disabling loading images + self._window.currentBrowser().reload() + + self.__updateIcon() + + @pyqtSlot(bool) + def __setGlobalLoadingImages(self, enable): + """ + Private slot to toggle the global images loading setting. + + @param enable flag indicating the state to set + @type bool + """ + QWebEngineSettings.globalSettings().setAttribute( + QWebEngineSettings.AutoLoadImages, enable) + Preferences.setWebBrowser("AutoLoadImages", enable) + + Preferences.syncPreferences() + self._window.preferencesChanged() + + self.__updateIcon() +##void SBI_ImagesIcon::setGlobalLoadingImages(bool enable) +##{ +## // Save it permanently +## QSettings settings(m_settingsFile, QSettings::IniFormat); +## settings.beginGroup("StatusBarIcons_Images"); +## settings.setValue("LoadImages", enable); +## settings.endGroup(); +## +## // Switch it in websettings +## m_loadingImages = enable; +## QWebEngineSettings::defaultSettings()->setAttribute(QWebEngineSettings::AutoLoadImages, m_loadingImages); +## updateIcon(); +## +## // We should reload page on disabling images +## if (!enable) { +## m_window->weView()->reload(); +## } +##} + +## from .JavaScriptSettingsDialog import JavaScriptSettingsDialog +## dlg = JavaScriptSettingsDialog(self._window) +## if dlg.exec_() == QDialog.Accepted: +## self._window.preferencesChanged()