-
Notifications
You must be signed in to change notification settings - Fork 4
/
pychmwebview.py
371 lines (338 loc) · 12.4 KB
/
pychmwebview.py
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
#!/usr/bin/python
# coding: utf8
#########################################################################
# Author: Xinyu.Xiang(John)
# email: zorrohunter@gmail.com
# Created Time: 2009年05月26日 星期二 01时17分03秒
# File Name: pychmwebview.py
# Description:
#########################################################################
from PyQt4.QtNetwork import QNetworkReply,QNetworkRequest,QNetworkAccessManager
from PyQt4.QtCore import QTimer,QLatin1String,QUrl,QVariant
from PyQt4 import QtCore,QtGui
from PyQt4.QtCore import QIODevice,Qt
import urltools
import globalvalue
import urllib
from PyQt4.QtWebKit import QWebView,QWebPage
import sys
import os.path
from content_type import content_type
import cStringIO as StringIO
urldecode=urllib.unquote_plus
class PyChmNetReply(QNetworkReply):
def __init__(self,request,url,parent=None,qwebview=None):
QNetworkReply.__init__(self,parent)
self.qwebview=qwebview
self.setRequest(request)
self.setOpenMode(QIODevice.ReadOnly)
self.m_data=self.loadResource(url)
if self.m_data!=None:
self.m_length=len(self.m_data)
self.m_data=StringIO.StringIO(self.m_data)
else:
self.m_length=0
self.m_data=StringIO.StringIO('')
self.left=self.m_length
self.setHeader(QNetworkRequest.ContentLengthHeader,QVariant(QtCore.QByteArray.number(self.m_length)))
# QTimer.singleShot(0,self,QtCore.SIGNAL('metaDataChanged()'))
QTimer.singleShot(0,self,QtCore.SIGNAL('readyRead()'))
def bytesAvailable(self):
return self.left+QNetworkReply.bytesAvailable(self)
def abort(self):
pass
def readData(self,maxlen):
data=self.m_data.read(maxlen)
self.left=self.m_length-self.m_data.tell()
if self.left==0:
QTimer.singleShot(0,self,QtCore.SIGNAL('finished()'))
return data
def loadResource(self,url):
chm=globalvalue.chmFile
if not chm:
return ''
path=unicode(url.path())
try:
pos=path.index(u'#')
path=path[0:pos]
except:
pass
path=urldecode(path)
data=chm.GetFileAsStrByUrl(path)
if data==None:
self.setError(404,'')
return None
self.setContentTypeHeader(path)
#self.setHeader(QNetworkRequest.ContentTypeHeader,QVariant("text/html; charset=UTF-8"))
# ext=os.path.splitext(path)[1].lower()
# if len(ext)>0:
# ext=ext[1:]
# ctt_type=content_type.get(ext,'binary/octet')
# if ctt_type.lower().startswith('text'):
# print data.decode('gb18030')
return data
def setContentTypeHeader(self,path):
ext=os.path.splitext(path)[1].lower()
if len(ext)>0:
ext=ext[1:]
ctt_type=content_type.get(ext,'binary/octet')
if ctt_type.lower().startswith('text') and globalvalue.encoding!=None:
ctt_type+='; charset='+ globalvalue.encoding
self.setHeader(QNetworkRequest.ContentTypeHeader,QVariant(ctt_type))
class PyChmNetworkAccessManager(QNetworkAccessManager):
def __init__(self,parent):
QNetworkAccessManager.__init__(self,parent)
self.qwebview=parent
def createRequest(self,op,request,outgoingdata):
scheme=request.url().scheme()
# print unicode(request.url().path())
if scheme==QLatin1String('ms-its'):
return PyChmNetReply(request,request.url(),self.qwebview,self.qwebview)
return QNetworkAccessManager.createRequest(self,op,request,outgoingdata)
class PyChmWebView(QWebView):
def __init__(self,parent=None):
'''
zoom: zoom out times
openedpg: current openedpage
signal 'openUrl' will be emited(with param url:unicode)
signal 'openatnewtab' will be emited(with param url:unicode)
signal 'openremoteatnewtab' will be emited(with param url:unicode)
signal 'openRemoteUrl' will be emited(with param url:unicode)
'''
QWebView.__init__(self,parent)
self.page().setNetworkAccessManager(PyChmNetworkAccessManager(self))
self.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks)
#self.setUrl(QtCore.QUrl('http://www.baidu.com'))
self.connect(self,QtCore.SIGNAL('linkClicked(const QUrl&)'),self.onLinkClicked)
# self.connect(self.zob,QtCore.SIGNAL('clicked()'),self.zoomout)
# self.connect(self.zib,QtCore.SIGNAL('clicked()'),self.zoomin)
# self.connect(self.normb,QtCore.SIGNAL('clicked()'),self.normsize)
# self.connect(self.bb,QtCore.SIGNAL('clicked()'),self.back)
# self.connect(self.fb,QtCore.SIGNAL('clicked()'),self.forward)
self.connect(self,QtCore.SIGNAL('loadFinished(bool)'),self.onLoadFinished)
self.zoom=1.0
self.setTextSizeMultiplier(1.0)
self.reload()
self.openedpg=None
self.scrolltopos=0
def zoomout(self):
'''
zoom out fontsize
'''
self.zoom/=1.2
self.setTextSizeMultiplier(self.zoom)
def zoomin(self):
'''
zoom in the fontsize
'''
self.zoom*=1.2
self.setTextSizeMultiplier(self.zoom)
def normsize(self):
'''
to make the font size normal
'''
self.zoom=1.0
self.setTextSizeMultiplier(self.zoom)
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)
def onLoadFinished(self,ok):
'''
inner method
'''
if ok:
pass
self.page().currentFrame().setScrollBarValue(Qt.Vertical,self.scrolltopos)
self.scrolltopos=0
globalvalue.tabs.setTabName(self)
else:
# mb=QtGui.QMessageBox(self)
# mb.setText(u'file not found')
# mb.exec_()
#self.openPage(globalvalue.chmFile.HomeUrl)
#tell user file not found!########################
print 'file not found'
def printPage(self):
printer=QtGui.QPrinter(QtGui.QPrinter.HighResolution)
dlg=QtGui.QPrintDialog(printer,self)
if dlg.exec_()!=QtGui.QDialog.Accepted:
return
self.print_(printer)
def back(self):
self.history().back()
def forward(self):
self.history().forward()
def contextMenuEvent(self,e):
'''
inner method
'''
m=QtGui.QMenu(self)
link=self.anchorAt(e.pos())
if link!=None:
self.keepnewtaburl=link
m.addAction(u'在新标签页打开',self.openinnewtab)
m.exec_(e.globalPos())
if not self.selectedText().isEmpty():
m.addAction(u'复制', self.copyToClipboard)
m.exec_(e.globalPos())
def copyToClipboard(self):
QtGui.QApplication.clipboard().setText(self.selectedText())
def mousePressEvent(self,e):
'''
inner method
'''
if e.button()!=QtCore.Qt.MidButton:
QWebView.mousePressEvent(self,e)
return
link=self.anchorAt(e.pos())
self.keepnewtaburl=link
if self.keepnewtaburl!=None and len(self.keepnewtaburl)!=0:
if self.keepnewtaburl[0:4]=='http':
self.emit(QtCore.SIGNAL('openremoteatnewtab'),self.keepnewtaburl)
return
self.emit(QtCore.SIGNAL('openatnewtab'),self.keepnewtaburl)
def anchorAt(self,pos):
'''
inner method
'''
res=self.page().currentFrame().hitTestContent(pos)
if not res.linkUrl().isValid():
return None
qurl=res.linkUrl()
if qurl.scheme()=='http' or qurl.scheme()=='https':
return unicode(qurl.toString())
if qurl.scheme()!='ms-its':
return None
url=unicode(qurl.path())
if url==u'/':
url=globalvalue.chmFile.HomeUrl
isnew,ochm,pg=urltools.isnewchmurl(unicode(qurl.toString()))
if isnew:
ochm=os.path.join(os.path.dirname(globalvalue.chmpath),ochm)
if ochm!=globalvalue.chmpath:
url=pg
else:
return None
url=os.path.normpath(url)
return url
def openinnewtab(self):
'''
inner method
'''
if self.keepnewtaburl!=None and len(self.keepnewtaburl)!=0:
if self.keepnewtaburl[0:4]=='http':
self.emit(QtCore.SIGNAL('openremoteatnewtab'),self.keepnewtaburl)
return
self.emit(QtCore.SIGNAL('openatnewtab'),self.keepnewtaburl)
def onLinkClicked(self,qurl):
'''
inner method
'''
if qurl.scheme()=='http' or qurl.scheme()=='https':
#self.openPage(unicode(qurl.toString())) #delete this and emit the url #######################################
self.emit(QtCore.SIGNAL('openRemoteUrl'),unicode(qurl.toString()))
return
if qurl.scheme()!='ms-its':
return
url=unicode(qurl.path())
if url==u'/':
url=globalvalue.chmFile.HomeUrl
isnew,ochm,pg=urltools.isnewchmurl(unicode(qurl.toString()))
if isnew:
ochm=os.path.join(os.path.dirname(globalvalue.chmpath),ochm)
if ochm!=globalvalue.chmpath:
url=pg
else:
pass
return
url=os.path.normpath(url)
#self.openPage(url) #delete this and emit the url #######################################
self.emit(QtCore.SIGNAL('openUrl'),url)
# def clearurl(self,url):
# if len(url)==0:
# return None
# if urltools.isjsurl(url):
# return None
# if urltools.isRemoteURL(url)[0]:
# return None
# isnew,ochm,pg=urltools.isnewchmurl(url)
# if isnew:
# if os.path.abspath(ochm)!=os.path.abspath(globalvalue.chmpath):
# url=pg
# else:
# pass
# return None
# if url.lower().startswith(u'ms-its:'):
# url=url[7:]
# if url[0]!=u'/':
# url=self.baseurl+u'/'+url
# url=os.path.normpath(url)
# return url
def openPage(self,url):
'''
url: unicode or Qstring. must be absolute url(ignore the first '/' is ok) in current chmfile
'''
assert isinstance(url,QtCore.QString) or isinstance(url,unicode)
if isinstance(url,QtCore.QString):
url=unicode(url)
if url[0:4]=='http':
# if not globalvalue.globalcfg.openremote:
# return
self.load(QtCore.QUrl(url))
globalvalue.tabs.setTabName(self)
self.openedpg=url
return
if url==u'/':
url=globalvalue.chmFile.HomeUrl
try:
pos=url.index(u'://')
if url[0:pos]!=u'ms-its': #just for url in chmfile
return
url=url[0:pos]+os.path.normpath(url[pos+3:])
except:
url=os.path.normpath(url)
if url[0]!=u'/':
url=u'/'+url
url=os.path.normpath(url)
if not url.lower().startswith(u'ms-its://'):
url=u'ms-its://'+url
#self.setUrl(QtCore.QUrl(url))
self.load(QtCore.QUrl(url))
#set title on tab
globalvalue.tabs.setTabName(self)
self.openedpg=url[9:]
return True
def getScrollPos(self):
'''
get current pos of the frame
'''
return self.page().currentFrame().scrollBarValue(Qt.Vertical)
def setScrollPos(self,pos):
'''
set current pos of the frame
'''
self.scrolltopos=pos
self.page().currentFrame().setScrollBarValue(Qt.Vertical,pos)
def canback(self):
return self.history().canGoBack()
def canforward(self):
return self.history().canGoForward()
if __name__=='__main__':
app = QtGui.QApplication(sys.argv)
from pychmfile import PyChmFile
globalvalue.chmFile=PyChmFile()
globalvalue.chmpath=u'python261.chm'
globalvalue.chmFile.loadFile(globalvalue.chmpath)
Form = PyChmWebView()
# Form.code='gbk'
Form.openPage(globalvalue.chmFile.HomeUrl)
Form.show()
sys.exit(app.exec_())