|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2016 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the images loading status bar icon. |
|
8 """ |
|
9 |
|
10 # |
|
11 # This is modeled after the code found in Qupzilla |
|
12 # Copyright (C) 2014 David Rosca <nowrep@gmail.com> |
|
13 # |
|
14 |
|
15 from __future__ import unicode_literals |
|
16 |
|
17 from PyQt5.QtCore import pyqtSlot, Qt, QPoint |
|
18 from PyQt5.QtWidgets import QGraphicsColorizeEffect, QMenu |
|
19 from PyQt5.QtWebEngineWidgets import QWebEngineSettings |
|
20 |
|
21 from .StatusBarIcon import StatusBarIcon |
|
22 |
|
23 import UI.PixmapCache |
|
24 import Preferences |
|
25 |
|
26 |
|
27 class ImagesIcon(StatusBarIcon): |
|
28 """ |
|
29 Class implementing the images loading status bar icon. |
|
30 """ |
|
31 def __init__(self, window): |
|
32 """ |
|
33 Constructor |
|
34 |
|
35 @param window reference to the web browser window |
|
36 @type WebBrowserWindow |
|
37 """ |
|
38 super(ImagesIcon, self).__init__(window) |
|
39 |
|
40 self.setToolTip(self.tr("Modify images loading settings temporarily" |
|
41 " or globally")) |
|
42 self.__icon = UI.PixmapCache.getPixmap("filePixmap").scaled(16, 16) |
|
43 self.setPixmap(self.__icon) |
|
44 |
|
45 self._window.tabWidget().currentChanged.connect(self.__updateIcon) |
|
46 self.clicked.connect(self.__showMenu) |
|
47 |
|
48 self.__updateIcon() |
|
49 |
|
50 def preferencesChanged(self): |
|
51 """ |
|
52 Public method to handle changes of the settings. |
|
53 """ |
|
54 self.__updateIcon() |
|
55 |
|
56 @pyqtSlot(QPoint) |
|
57 def __showMenu(self, pos): |
|
58 """ |
|
59 Private slot to show the menu. |
|
60 |
|
61 @param pos position to show the menu at |
|
62 @type QPoint |
|
63 """ |
|
64 boldFont = self.font() |
|
65 boldFont.setBold(True) |
|
66 |
|
67 menu = QMenu() |
|
68 menu.addAction(self.tr("Current Page Settings")).setFont(boldFont) |
|
69 |
|
70 if self._testCurrentPageWebAttribute( |
|
71 QWebEngineSettings.AutoLoadImages): |
|
72 menu.addAction(self.tr("Disable loading images (temporarily)"), |
|
73 self.__toggleLoadingImages) |
|
74 else: |
|
75 menu.addAction(self.tr("Enable loading images (temporarily)"), |
|
76 self.__toggleLoadingImages) |
|
77 |
|
78 menu.addSeparator() |
|
79 menu.addAction(self.tr("Global Settings")).setFont(boldFont) |
|
80 act = menu.addAction(self.tr("Automatically load images")) |
|
81 act.setCheckable(True) |
|
82 act.setChecked(Preferences.getWebBrowser("AutoLoadImages")) |
|
83 act.toggled.connect(self.__setGlobalLoadingImages) |
|
84 |
|
85 menu.exec(pos) |
|
86 |
|
87 @pyqtSlot() |
|
88 def __updateIcon(self): |
|
89 """ |
|
90 Private slot to update the icon. |
|
91 """ |
|
92 if self._testCurrentPageWebAttribute( |
|
93 QWebEngineSettings.AutoLoadImages): |
|
94 self.setGraphicsEffect(None) |
|
95 else: |
|
96 effect = QGraphicsColorizeEffect(self) |
|
97 effect.setColor(Qt.gray) |
|
98 self.setGraphicsEffect(effect) |
|
99 |
|
100 @pyqtSlot() |
|
101 def __toggleLoadingImages(self): |
|
102 """ |
|
103 Private slot to toggle the images loading setting. |
|
104 """ |
|
105 if self._currentPage() is None: |
|
106 return |
|
107 |
|
108 current = self._testCurrentPageWebAttribute( |
|
109 QWebEngineSettings.AutoLoadImages) |
|
110 self._setCurrentPageWebAttribute(QWebEngineSettings.AutoLoadImages, |
|
111 not current) |
|
112 |
|
113 if current: |
|
114 # reload page upon disabling loading images |
|
115 self._window.currentBrowser().reload() |
|
116 |
|
117 self.__updateIcon() |
|
118 |
|
119 @pyqtSlot(bool) |
|
120 def __setGlobalLoadingImages(self, enable): |
|
121 """ |
|
122 Private slot to toggle the global images loading setting. |
|
123 |
|
124 @param enable flag indicating the state to set |
|
125 @type bool |
|
126 """ |
|
127 QWebEngineSettings.globalSettings().setAttribute( |
|
128 QWebEngineSettings.AutoLoadImages, enable) |
|
129 Preferences.setWebBrowser("AutoLoadImages", enable) |
|
130 |
|
131 Preferences.syncPreferences() |
|
132 self._window.preferencesChanged() |
|
133 |
|
134 self.__updateIcon() |
|
135 ##void SBI_ImagesIcon::setGlobalLoadingImages(bool enable) |
|
136 ##{ |
|
137 ## // Save it permanently |
|
138 ## QSettings settings(m_settingsFile, QSettings::IniFormat); |
|
139 ## settings.beginGroup("StatusBarIcons_Images"); |
|
140 ## settings.setValue("LoadImages", enable); |
|
141 ## settings.endGroup(); |
|
142 ## |
|
143 ## // Switch it in websettings |
|
144 ## m_loadingImages = enable; |
|
145 ## QWebEngineSettings::defaultSettings()->setAttribute(QWebEngineSettings::AutoLoadImages, m_loadingImages); |
|
146 ## updateIcon(); |
|
147 ## |
|
148 ## // We should reload page on disabling images |
|
149 ## if (!enable) { |
|
150 ## m_window->weView()->reload(); |
|
151 ## } |
|
152 ##} |
|
153 |
|
154 ## from .JavaScriptSettingsDialog import JavaScriptSettingsDialog |
|
155 ## dlg = JavaScriptSettingsDialog(self._window) |
|
156 ## if dlg.exec_() == QDialog.Accepted: |
|
157 ## self._window.preferencesChanged() |