-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpychmwebview.py
More file actions
380 lines (297 loc) · 11.8 KB
/
pychmwebview.py
File metadata and controls
380 lines (297 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/usr/bin/python
# vim: set fileencoding=utf-8 :
import os.path
import urllib
import cStringIO as StringIO
from PyQt4 import QtCore, QtGui
from PyQt4.QtWebKit import QWebView, QWebPage
from PyQt4.QtCore import QTimer, QString, QUrl, QVariant, QIODevice
from PyQt4.QtNetwork import (QNetworkReply,
QNetworkRequest,
QNetworkAccessManager)
from content_type import content_types
def path2ChmURL(path, default):
if path == '/':
path = default
if path[0] != '/':
path = '/' + path
if not path.startswith("ms-its://"):
return QUrl( "ms-its://" + path )
else:
return QUrl(path)
class PyChmNetReply(QNetworkReply):
def __init__(self, parent, request, qwebview ):
QNetworkReply.__init__(self, parent)
self.qwebview = qwebview
self.setRequest(request)
self.setOpenMode(QIODevice.ReadOnly)
rawdata = self._loadResource( request.url() )
if rawdata :
self._data = StringIO.StringIO(rawdata)
self._length = len(rawdata)
else:
self._data = StringIO.StringIO("")
self._length = 0
self._left = self._length
self.setHeader(QNetworkRequest.ContentLengthHeader,
QVariant(QtCore.QByteArray.number(self._length)),
)
# [Notes for using QTimer]
# We need to queue the signal rather than emit it directly,
# because when this reply is being created, the browser does not
# know about it yet
QTimer.singleShot(0, self, QtCore.SIGNAL('metaDataChanged()'))
QTimer.singleShot(0, self, QtCore.SIGNAL('readyRead()'))
# [important] never remove this line
QTimer.singleShot(0, self, QtCore.SIGNAL('finished()'))
def abort(self):
pass
def bytesAvailable(self):
return self._left + QNetworkReply.bytesAvailable(self)
def readData(self, maxlen):
data = self._data.read(maxlen)
self._left = self._length - self._data.tell()
if self._left == 0:
QTimer.singleShot(0, self, QtCore.SIGNAL('finished()'))
return data
def _loadResource(self, url):
chmfile = self.qwebview.chmfile
if not chmfile:
return ""
print "[Netreply] asked to load %s " % \
unicode(url.toString()).encode('utf-8')
path = unicode(url.path())
path = urllib.unquote_plus(path)
data = chmfile.getContentsByURL(path)
if data:
self._setContentTypeHeader(path)
return data
else:
print "[NetReply] failed to load %s" % path.encode('utf-8')
self.setError(QNetworkReply.ContentNotFoundError,
"%s not found." % path,
)
return ""
def _setContentTypeHeader(self, path):
"provide necessary charset info, so that webkit can show it nicely"
ext = os.path.splitext(path)[1].lower()
if ext :
content_type = content_types.get(ext, 'binary/octet').lower()
if content_type.startswith('text') and self.qwebview.encoding :
content_type += ("; charset=%s" % self.qwebview.encoding)
self.setHeader(QNetworkRequest.ContentTypeHeader,
QVariant(content_type),
)
class PyChmNetworkAccessManager(QNetworkAccessManager):
def __init__(self, parent):
QNetworkAccessManager.__init__(self, parent)
self.qwebview = parent
def createRequest(self, op, request, outgoingdata):
# special case for links related with .CHM
print "[createRequest] %s" % \
unicode(request.url().toString()).encode('utf-8')
if request.url().scheme() == QString("ms-its"):
return PyChmNetReply(self,
request,
self.qwebview,
)
else:
return QNetworkAccessManager.createRequest(self,
op,
request,
outgoingdata
)
class PyChmWebView(QWebView):
def __init__(self, tabmanager, chmfile, parent):
'''
signal 'openURL' will be emited(with param url:unicode)
signal 'openURLatNewTab' will be emited(with param url:unicode)
'''
QWebView.__init__(self, parent)
# Whenever a link is activated, the linkClicked() signal is emitted.
self.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks)
self.page().setNetworkAccessManager(PyChmNetworkAccessManager(self))
self.connect(self,
QtCore.SIGNAL('linkClicked(const QUrl&)'),
self.onLinkClicked)
self.connect(self,
QtCore.SIGNAL('loadFinished(bool)'),
self.onLoadFinished)
self.connect(self,
QtCore.SIGNAL('loadStarted()'),
self.onLoadStarted)
self.connect(self,
QtCore.SIGNAL('loadProgress(int)'),
self.onLoadProgress)
self.tabmanager = tabmanager
self.session = tabmanager.session
self.chmfile = chmfile
#self.encoding = chmfile.encoding or "gb18030"
self.encoding = "gb18030"
self.loadedURL = None
self.suggestedPos = 0
self.zoom = 1.0
# FIXME; maybe not needed?
def clone(self):
view = PyChmWebView(tabmanager=self.tabmanager,
chmfile=self.chmfile,
parent=self.parent() )
view.encoding = self.encoding
view.loadedURL = self.loadedURL
view.suggestedPos = self.currentPos()
return view
def keyPressEvent(self, event):
if event.matches(QtGui.QKeySequence.Refresh):
self.reload()
elif event.matches(QtGui.QKeySequence.Back):
self.goBack()
elif event.matches(QtGui.QKeySequence.Forward):
self.goForward()
else:
QWebView.keyPressEvent(self, event)
def contextMenuEvent(self, event):
# triggerd when user right-click mouse or press context key
menu = QtGui.QMenu(self)
link = self.anchorAt(event.pos())
if link :
self.keepnewtaburl = link
menu.addAction(u"Open in new tab", self.openURLatNewTab)
copyLink = self.pageAction(QWebPage.CopyLinkToClipboard)
copyLink.setText(u"Copy link ")
menu.addAction(copyLink)
menu.addSeparator()
if self.selectedText().isEmpty():
menu.addAction(u"Select all", self.onSelectAll)
else:
menu.addAction(u"Copy selected text", self.onCopy)
menu.exec_(event.globalPos())
def mousePressEvent(self, event):
# open url at new tab, with middle button
if event.button() == QtCore.Qt.MidButton:
url = self.anchorAt(event.pos())
if url:
self.keepnewtaburl = url
self.openURLatNewTab()
else:
QWebView.mousePressEvent(self, event)
def openURLatNewTab(self):
if self.keepnewtaburl :
self.emit(QtCore.SIGNAL('openURLatNewTab'), self.keepnewtaburl)
def normalizeChmURL(self, chmurl):
if chmurl.hasFragment():
archor = u"#" + unicode( chmurl.encodedFragment() )
else:
archor = ""
path = unicode(chmurl.path() ) + archor
home = self.chmfile.home
return path2ChmURL(path, home)
def onLinkClicked(self, qurl):
print "[linkClicked] original url: %s" \
% unicode(qurl.toString()).encode('utf-8')
if qurl.scheme() in [ "http", "https"] :
self.emit(QtCore.SIGNAL('openURL'), qurl)
elif qurl.scheme() == 'ms-its':
url = self.normalizeChmURL(qurl)
self.emit(QtCore.SIGNAL('openURL'), url)
else:
raise NotImplementedError("")
# this method is only reponsible for loading url in itself
# creating new tab is not with its concern
def loadURL(self, url):
assert isinstance(url, unicode) or isinstance(url, QUrl)
if isinstance(url, QUrl):
finalurl = url
else:
qurl = QUrl(url)
finalurl = self.normalizeChmURL(qurl)
print ("[loadURL] final url: %s" % \
unicode(finalurl.toString()).encode('utf-8') )
self.load(finalurl)
self.show()
self.tabmanager.setTabName(self, self.title() )
self.loadedURL = finalurl
def anchorAt(self, pos):
# Performs a hit test on the frame contents at the given position
result = self.page().currentFrame().hitTestContent(pos)
qurl = result.linkUrl()
if not qurl.isValid():
return None
else:
print "[contextLink] %s" % unicode(qurl.toString()).encode('utf-8')
if qurl.scheme() in [ "http", "https"] :
return unicode(qurl.toString())
elif qurl.scheme() == "ms-its":
return unicode( qurl.toString() )
else:
return None
def onCopy(self):
self.triggerPageAction(QWebPage.Copy)
def onSelectAll(self):
self.triggerPageAction(QWebPage.SelectAll)
def zoomIn(self):
self.zoom *= 1.2
# only influence text
self.setTextSizeMultiplier(self.zoom)
# influence both text and image
#self.setZoomFactor(self.zoom)
def zoomOut(self):
self.zoom /= 1.2
self.setTextSizeMultiplier(self.zoom)
def zoomOff(self):
" to make the font size normal "
self.zoom = 1.0
self.setTextSizeMultiplier(self.zoom)
def printPage(self):
printer = QtGui.QPrinter(QtGui.QPrinter.HighResolution)
dialog = QtGui.QPrintDialog(printer, self)
if dialog.exec_() == QtGui.QDialog.Accepted:
self.print_(printer)
def goHome(self):
self.loadURL(self.chmfile.home)
def goBack(self):
self.history().back()
def goForward(self):
self.history().forward()
def canGoBack(self):
return self.history().canGoBack()
def canGoForward(self):
return self.history().canGoForward()
def onEncodingChanged(self, encoding):
self.encoding = encoding
self.chmfile.loadFile(self.chmfile.path, encoding)
self.reload()
def onLoadFinished(self, ok):
if ok:
self.setCurrentPos(self.suggestedPos)
self.suggestedPos = 0
self.tabmanager.setTabName(self, self.title() )
else:
print ("[loadFinished] failed to load page")
def onLoadStarted(self):
#print "[onLoadStarted]"
pass
def onLoadProgress(self, percent):
#print "[onLoadProgress] %d" % percent
pass
def title(self):
"override the QWebView.title() "
page_title = QWebView.title(self)
chm_title = self.chmfile.title
return page_title or chm_title or u"No Title"
def currentPos(self):
return self.page().currentFrame().scrollBarValue(QtCore.Qt.Vertical)
def setCurrentPos(self, pos):
self.page().currentFrame().setScrollBarValue(QtCore.Qt.Vertical, pos)
def find(self, text, backward=False, casesens=False):
'''
text: the search text
backward:bool
'''
flags = QWebPage.FindWrapsAroundDocument
if backward:
flags |= QWebPage.FindBackward
if casesens:
flags |= QWebPage.FindCaseSensitively
self.findText(text, flags)
if __name__ == '__main__':
raise NotImplementedError()