src/eric7/WebBrowser/Tools/WebHitTestResult.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9482
a2bc06a54d9d
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
17 17
18 class WebHitTestResult: 18 class WebHitTestResult:
19 """ 19 """
20 Class implementing an object for testing certain aspects of a web page. 20 Class implementing an object for testing certain aspects of a web page.
21 """ 21 """
22
22 def __init__(self, page, pos): 23 def __init__(self, page, pos):
23 """ 24 """
24 Constructor 25 Constructor
25 26
26 @param page reference to the web page 27 @param page reference to the web page
27 @type WebBrowserPage 28 @type WebBrowserPage
28 @param pos position to be tested 29 @param pos position to be tested
29 @type QPoint 30 @type QPoint
30 """ 31 """
41 self.__linkTitle = "" 42 self.__linkTitle = ""
42 self.__linkUrl = QUrl() 43 self.__linkUrl = QUrl()
43 self.__mediaUrl = QUrl() 44 self.__mediaUrl = QUrl()
44 self.__tagName = "" 45 self.__tagName = ""
45 self.__viewportPos = page.mapToViewport(pos) 46 self.__viewportPos = page.mapToViewport(pos)
46 47
47 script = """ 48 script = """
48 (function() {{ 49 (function() {{
49 var e = document.elementFromPoint({0}, {1}); 50 var e = document.elementFromPoint({0}, {1});
50 if (!e) 51 if (!e)
51 return; 52 return;
104 }} 105 }}
105 e = e.parentElement; 106 e = e.parentElement;
106 }} 107 }}
107 return res; 108 return res;
108 }})() 109 }})()
109 """.format(self.__viewportPos.x(), self.__viewportPos.y()) 110 """.format(
111 self.__viewportPos.x(), self.__viewportPos.y()
112 )
110 self.__populate(page.url(), page.execJavaScript(script)) 113 self.__populate(page.url(), page.execJavaScript(script))
111 114
112 def updateWithContextMenuData(self, data): 115 def updateWithContextMenuData(self, data):
113 """ 116 """
114 Public method to update the hit test data with data from the context 117 Public method to update the hit test data with data from the context
115 menu event. 118 menu event.
116 119
117 @param data context menu data 120 @param data context menu data
118 @type QWebEngineContextMenuRequest 121 @type QWebEngineContextMenuRequest
119 """ 122 """
120 from PyQt6.QtWebEngineCore import QWebEngineContextMenuRequest 123 from PyQt6.QtWebEngineCore import QWebEngineContextMenuRequest
124
121 if data.position() != self.__pos: 125 if data.position() != self.__pos:
122 return 126 return
123 127
124 self.__linkTitle = data.linkText() 128 self.__linkTitle = data.linkText()
125 self.__linkUrl = data.linkUrl() 129 self.__linkUrl = data.linkUrl()
126 self.__isContentEditable = data.isContentEditable() 130 self.__isContentEditable = data.isContentEditable()
127 self.__isContentSelected = bool(data.selectedText()) 131 self.__isContentSelected = bool(data.selectedText())
128 132
129 if ( 133 if data.mediaType() == QWebEngineContextMenuRequest.MediaType.MediaTypeImage:
130 data.mediaType() ==
131 QWebEngineContextMenuRequest.MediaType.MediaTypeImage
132 ):
133 self.__imageUrl = data.mediaUrl() 134 self.__imageUrl = data.mediaUrl()
134 elif data.mediaType() in [ 135 elif data.mediaType() in [
135 QWebEngineContextMenuRequest.MediaType.MediaTypeAudio, 136 QWebEngineContextMenuRequest.MediaType.MediaTypeAudio,
136 QWebEngineContextMenuRequest.MediaType.MediaTypeVideo 137 QWebEngineContextMenuRequest.MediaType.MediaTypeVideo,
137 ]: 138 ]:
138 self.__mediaUrl = data.mediaUrl() 139 self.__mediaUrl = data.mediaUrl()
139 140
140 def baseUrl(self): 141 def baseUrl(self):
141 """ 142 """
142 Public method to get the base URL of the page. 143 Public method to get the base URL of the page.
143 144
144 @return base URL 145 @return base URL
145 @rtype QUrl 146 @rtype QUrl
146 """ 147 """
147 return self.__baseUrl 148 return self.__baseUrl
148 149
149 def alternateText(self): 150 def alternateText(self):
150 """ 151 """
151 Public method to get the alternate text. 152 Public method to get the alternate text.
152 153
153 @return alternate text 154 @return alternate text
154 @rtype str 155 @rtype str
155 """ 156 """
156 return self.__alternateText 157 return self.__alternateText
157 158
158 def boundingRect(self): 159 def boundingRect(self):
159 """ 160 """
160 Public method to get the bounding rectangle. 161 Public method to get the bounding rectangle.
161 162
162 @return bounding rectangle 163 @return bounding rectangle
163 @rtype QRect 164 @rtype QRect
164 """ 165 """
165 return QRect(self.__boundingRect) 166 return QRect(self.__boundingRect)
166 167
167 def imageUrl(self): 168 def imageUrl(self):
168 """ 169 """
169 Public method to get the URL of an image. 170 Public method to get the URL of an image.
170 171
171 @return image URL 172 @return image URL
172 @rtype QUrl 173 @rtype QUrl
173 """ 174 """
174 return self.__imageUrl 175 return self.__imageUrl
175 176
176 def isContentEditable(self): 177 def isContentEditable(self):
177 """ 178 """
178 Public method to check for editable content. 179 Public method to check for editable content.
179 180
180 @return flag indicating editable content 181 @return flag indicating editable content
181 @rtype bool 182 @rtype bool
182 """ 183 """
183 return self.__isContentEditable 184 return self.__isContentEditable
184 185
185 def isContentSelected(self): 186 def isContentSelected(self):
186 """ 187 """
187 Public method to check for selected content. 188 Public method to check for selected content.
188 189
189 @return flag indicating selected content 190 @return flag indicating selected content
190 @rtype bool 191 @rtype bool
191 """ 192 """
192 return self.__isContentSelected 193 return self.__isContentSelected
193 194
194 def isNull(self): 195 def isNull(self):
195 """ 196 """
196 Public method to test, if the hit test is empty. 197 Public method to test, if the hit test is empty.
197 198
198 @return flag indicating an empty object 199 @return flag indicating an empty object
199 @rtype bool 200 @rtype bool
200 """ 201 """
201 return self.__isNull 202 return self.__isNull
202 203
203 def linkTitle(self): 204 def linkTitle(self):
204 """ 205 """
205 Public method to get the title for a link element. 206 Public method to get the title for a link element.
206 207
207 @return title for a link element 208 @return title for a link element
208 @rtype str 209 @rtype str
209 """ 210 """
210 return self.__linkTitle 211 return self.__linkTitle
211 212
212 def linkUrl(self): 213 def linkUrl(self):
213 """ 214 """
214 Public method to get the URL for a link element. 215 Public method to get the URL for a link element.
215 216
216 @return URL for a link element 217 @return URL for a link element
217 @rtype QUrl 218 @rtype QUrl
218 """ 219 """
219 return self.__linkUrl 220 return self.__linkUrl
220 221
221 def mediaUrl(self): 222 def mediaUrl(self):
222 """ 223 """
223 Public method to get the URL for a media element. 224 Public method to get the URL for a media element.
224 225
225 @return URL for a media element 226 @return URL for a media element
226 @rtype QUrl 227 @rtype QUrl
227 """ 228 """
228 return self.__mediaUrl 229 return self.__mediaUrl
229 230
230 def mediaPaused(self): 231 def mediaPaused(self):
231 """ 232 """
232 Public method to check, if a media element is paused. 233 Public method to check, if a media element is paused.
233 234
234 @return flag indicating a paused media element 235 @return flag indicating a paused media element
235 @rtype bool 236 @rtype bool
236 """ 237 """
237 return self.__isMediaPaused 238 return self.__isMediaPaused
238 239
239 def mediaMuted(self): 240 def mediaMuted(self):
240 """ 241 """
241 Public method to check, if a media element is muted. 242 Public method to check, if a media element is muted.
242 243
243 @return flag indicating a muted media element 244 @return flag indicating a muted media element
244 @rtype bool 245 @rtype bool
245 """ 246 """
246 return self.__isMediaMuted 247 return self.__isMediaMuted
247 248
248 def pos(self): 249 def pos(self):
249 """ 250 """
250 Public method to get the position of the hit test. 251 Public method to get the position of the hit test.
251 252
252 @return position of hit test 253 @return position of hit test
253 @rtype QPoint 254 @rtype QPoint
254 """ 255 """
255 return QPoint(self.__pos) 256 return QPoint(self.__pos)
256 257
257 def viewportPos(self): 258 def viewportPos(self):
258 """ 259 """
259 Public method to get the viewport position. 260 Public method to get the viewport position.
260 261
261 @return viewport position 262 @return viewport position
262 @rtype QPoint 263 @rtype QPoint
263 """ 264 """
264 return QPoint(self.__viewportPos) 265 return QPoint(self.__viewportPos)
265 266
266 def tagName(self): 267 def tagName(self):
267 """ 268 """
268 Public method to get the name of the tested tag. 269 Public method to get the name of the tested tag.
269 270
270 @return name of the tested tag 271 @return name of the tested tag
271 @rtype str 272 @rtype str
272 """ 273 """
273 return self.__tagName 274 return self.__tagName
274 275
275 def __populate(self, url, res): 276 def __populate(self, url, res):
276 """ 277 """
277 Private method to populate the object. 278 Private method to populate the object.
278 279
279 @param url URL of the tested page 280 @param url URL of the tested page
280 @type QUrl 281 @type QUrl
281 @param res dictionary with result data from JavaScript 282 @param res dictionary with result data from JavaScript
282 @type dict 283 @type dict
283 """ 284 """
284 if not res: 285 if not res:
285 return 286 return
286 287
287 self.__baseUrl = QUrl(res["baseUrl"]) 288 self.__baseUrl = QUrl(res["baseUrl"])
288 self.__alternateText = res["alternateText"] 289 self.__alternateText = res["alternateText"]
289 self.__imageUrl = QUrl(res["imageUrl"]) 290 self.__imageUrl = QUrl(res["imageUrl"])
290 self.__isContentEditable = res["contentEditable"] 291 self.__isContentEditable = res["contentEditable"]
291 self.__isContentSelected = res["contentSelected"] 292 self.__isContentSelected = res["contentSelected"]
293 self.__linkUrl = QUrl(res["linkUrl"]) 294 self.__linkUrl = QUrl(res["linkUrl"])
294 self.__mediaUrl = QUrl(res["mediaUrl"]) 295 self.__mediaUrl = QUrl(res["mediaUrl"])
295 self.__isMediaPaused = res["mediaPaused"] 296 self.__isMediaPaused = res["mediaPaused"]
296 self.__isMediaMuted = res["mediaMuted"] 297 self.__isMediaMuted = res["mediaMuted"]
297 self.__tagName = res["tagName"] 298 self.__tagName = res["tagName"]
298 299
299 rect = res["boundingRect"] 300 rect = res["boundingRect"]
300 if len(rect) == 4: 301 if len(rect) == 4:
301 self.__boundingRect = QRect(int(rect[0]), int(rect[1]), 302 self.__boundingRect = QRect(
302 int(rect[2]), int(rect[3])) 303 int(rect[0]), int(rect[1]), int(rect[2]), int(rect[3])
303 304 )
305
304 if not self.__imageUrl.isEmpty(): 306 if not self.__imageUrl.isEmpty():
305 self.__imageUrl = url.resolved(self.__imageUrl) 307 self.__imageUrl = url.resolved(self.__imageUrl)
306 if not self.__linkUrl.isEmpty(): 308 if not self.__linkUrl.isEmpty():
307 self.__linkUrl = self.__baseUrl.resolved(self.__linkUrl) 309 self.__linkUrl = self.__baseUrl.resolved(self.__linkUrl)
308 if not self.__mediaUrl.isEmpty(): 310 if not self.__mediaUrl.isEmpty():

eric ide

mercurial