src/eric7/WebBrowser/StatusBar/ImagesIcon.py

branch
eric7
changeset 9209
b99e7fd55fd3
parent 8881
54e42bc2437a
child 9221
bf71ee032bb4
equal deleted inserted replaced
9208:3fc8dfeb6ebe 9209:b99e7fd55fd3
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2016 - 2022 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 PyQt6.QtCore import pyqtSlot, Qt, QPoint
16 from PyQt6.QtWidgets import QGraphicsColorizeEffect, QMenu
17 from PyQt6.QtWebEngineCore import QWebEngineSettings
18
19 from .StatusBarIcon import StatusBarIcon
20
21 import UI.PixmapCache
22 import Preferences
23
24
25 class ImagesIcon(StatusBarIcon):
26 """
27 Class implementing the images loading status bar icon.
28 """
29 def __init__(self, window):
30 """
31 Constructor
32
33 @param window reference to the web browser window
34 @type WebBrowserWindow
35 """
36 super().__init__(window)
37
38 self.setToolTip(self.tr("Modify images loading settings temporarily"
39 " or globally"))
40 self.__icon = UI.PixmapCache.getPixmap("filePixmap").scaled(16, 16)
41 self.setPixmap(self.__icon)
42
43 self._window.tabWidget().currentChanged.connect(self.__updateIcon)
44 self.clicked.connect(self.__showMenu)
45
46 self.__updateIcon()
47
48 def preferencesChanged(self):
49 """
50 Public method to handle changes of the settings.
51 """
52 self.__updateIcon()
53
54 @pyqtSlot(QPoint)
55 def __showMenu(self, pos):
56 """
57 Private slot to show the menu.
58
59 @param pos position to show the menu at
60 @type QPoint
61 """
62 boldFont = self.font()
63 boldFont.setBold(True)
64
65 menu = QMenu()
66 menu.addAction(self.tr("Current Page Settings")).setFont(boldFont)
67
68 if self._testCurrentPageWebAttribute(
69 QWebEngineSettings.WebAttribute.AutoLoadImages):
70 menu.addAction(self.tr("Disable loading images (temporarily)"),
71 self.__toggleLoadingImages)
72 else:
73 menu.addAction(self.tr("Enable loading images (temporarily)"),
74 self.__toggleLoadingImages)
75
76 menu.addSeparator()
77 menu.addAction(self.tr("Global Settings")).setFont(boldFont)
78 act = menu.addAction(self.tr("Automatically load images"))
79 act.setCheckable(True)
80 act.setChecked(Preferences.getWebBrowser("AutoLoadImages"))
81 act.toggled.connect(self.__setGlobalLoadingImages)
82
83 menu.exec(pos)
84
85 @pyqtSlot()
86 def __updateIcon(self):
87 """
88 Private slot to update the icon.
89 """
90 if self._testCurrentPageWebAttribute(
91 QWebEngineSettings.WebAttribute.AutoLoadImages):
92 self.setGraphicsEffect(None)
93 else:
94 effect = QGraphicsColorizeEffect(self)
95 effect.setColor(Qt.GlobalColor.gray)
96 self.setGraphicsEffect(effect)
97
98 @pyqtSlot()
99 def __toggleLoadingImages(self):
100 """
101 Private slot to toggle the images loading setting.
102 """
103 if self._currentPage() is None:
104 return
105
106 current = self._testCurrentPageWebAttribute(
107 QWebEngineSettings.WebAttribute.AutoLoadImages)
108 self._setCurrentPageWebAttribute(
109 QWebEngineSettings.WebAttribute.AutoLoadImages, not current)
110
111 if current:
112 # reload page upon disabling loading images
113 self._window.currentBrowser().reload()
114
115 self.__updateIcon()
116
117 @pyqtSlot(bool)
118 def __setGlobalLoadingImages(self, enable):
119 """
120 Private slot to toggle the global images loading setting.
121
122 @param enable flag indicating the state to set
123 @type bool
124 """
125 from WebBrowser.WebBrowserWindow import WebBrowserWindow
126 WebBrowserWindow.webSettings().setAttribute(
127 QWebEngineSettings.WebAttribute.AutoLoadImages, enable)
128 Preferences.setWebBrowser("AutoLoadImages", enable)
129
130 Preferences.syncPreferences()
131 self._window.preferencesChanged()
132
133 self.__updateIcon()
134
135 if not enable:
136 # reload page upon disabling loading images
137 self._window.currentBrowser().reload()

eric ide

mercurial