-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpychmtabs.py
275 lines (208 loc) · 8.42 KB
/
pychmtabs.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
#!/usr/bin/python
# vim: set fileencoding=utf-8 :
import cPickle as Pickle
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import QUrl
from pychmfile import PyChmFile
from pychmwebview import PyChmWebView
from Ui_window_browser import Ui_TabbedBrowser
class PyChmTabs(QtGui.QWidget, Ui_TabbedBrowser):
def __init__(self, mainwin=None, parent=None):
'''
webviews: list of PyChmWebView
'''
QtGui.QWidget.__init__(self, parent)
self.setupUi(self)
self.mainwin = mainwin
self.config = mainwin.config
self.session = mainwin.session
self.webviews = []
self.currentView = None
self._setupButtonClose()
self._setupButtonNew()
self.frameFind.setVisible(False)
self.connect(self.tabWidget,
QtCore.SIGNAL('currentChanged(int)'),
self.onTabSwitched
)
self.connect(self.editFind,
QtCore.SIGNAL('textEdited(const QString&)'),
self.onTextEdited)
self.connect(self.editFind,
QtCore.SIGNAL('returnPressed()'),
self.onFindReturnPressed)
self.connect(self.toolClose,
QtCore.SIGNAL('clicked()'),
self.frameFind.hide)
self.connect(self.toolPrevious,
QtCore.SIGNAL('clicked()'),
self.onFindPrevious)
self.connect(self.toolNext,
QtCore.SIGNAL('clicked()'),
self.onFindNext)
def _setupButtonClose(self):
buttonClose = self._createButton(pixmap=":/images/closetab.png",
tooltip=u"close current page",
)
buttonClose.setEnabled(False)
self.tabWidget.setCornerWidget(buttonClose, QtCore.Qt.TopRightCorner)
self.connect(buttonClose,
QtCore.SIGNAL('clicked()'),
self.onCloseCurrentTab)
self.buttonClose = buttonClose
def _setupButtonNew(self):
buttonNew = self._createButton(pixmap=":/images/addtab.png",
tooltip=u"open new page",
)
self.tabWidget.setCornerWidget(buttonNew, QtCore.Qt.TopLeftCorner)
self.connect(buttonNew,
QtCore.SIGNAL('clicked()'),
self.onOpenNewTab)
self.buttonNew = buttonNew
def _createButton(self, pixmap, tooltip=u""):
def createIcon(pixmap_path):
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(pixmap_path), QtGui.QIcon.Normal, QtGui.QIcon.Off)
return icon
button = QtGui.QToolButton(self)
button.setCursor(QtCore.Qt.ArrowCursor)
button.setAutoRaise(True)
button.setIcon(createIcon(pixmap))
button.setToolTip(tooltip)
return button
def keyPressEvent(self, keyevent):
if keyevent.key() == QtCore.Qt.Key_Escape:
# Vim user will love this behaviour !
# cancel find, return focus back
self.frameFind.hide()
self.currentView.setFocus()
else:
self.tabWidget.keyPressEvent(keyevent)
def openChmFile(self, path):
try:
chmfile = PyChmFile(self.session, path)
view = PyChmWebView(tabmanager=self,
chmfile=chmfile,
parent=self.tabWidget
)
self.addNewTab(view)
# FIXME; dirty hack, but we sitll need it now.
view.goHome()
self.emit(QtCore.SIGNAL('fileOpened'), path)
return view
except StandardError :
self.emit(QtCore.SIGNAL('fileNotOpened'), path)
return None
def onOpenNewTab(self):
"duplicate a new view showing same url as this one"
url = self.tabWidget.currentWidget().loadedURL
self.onOpenURLatNewTab(url)
def onOpenURLatNewTab(self, url):
"open specified url in a newly created view"
if not self.currentView:
raise ValueError("[Terrible]! Shame of the coder!")
view = self.currentView.clone()
self.addNewTab(view)
view.loadURL(url)
return view
def addNewTab(self, view, active=True):
"only responsible for putting view into widgetmanager"
self.webviews.append(view)
self.tabWidget.addTab(view, '')
if active or len(self.webviews) == 1:
self.tabWidget.setCurrentWidget(view)
self.currentView = view
self.editFind.installEventFilter(self)
self.connect(view,
QtCore.SIGNAL('openURL'),
view.loadURL)
self.connect(view,
QtCore.SIGNAL('openURLatNewTab'),
self.onOpenURLatNewTab)
self.connect(view.page(),
QtCore.SIGNAL('loadFinished(bool)'),
self.onLoadFinished)
self.emit(QtCore.SIGNAL('newTabAdded'), view)
self.updateButtonClose()
return view
def closeAll(self):
for webview in self.webviews:
self.closeTab(webview)
def closeTab(self, view):
pos = -1
for index, webview in enumerate(self.webviews):
if webview == view:
pos = index
if pos != -1:
del self.webviews[pos]
else:
raise ValueError("[Terrible] asked to close non-managed view! ")
self.tabWidget.removeTab(self.tabWidget.indexOf(view))
self.updateButtonClose()
def updateButtonClose(self):
enable = len(self.webviews) > 0
self.buttonClose.setEnabled(enable)
def onCloseCurrentTab(self):
self.closeTab(self.tabWidget.currentWidget())
self.currentView = self.tabWidget.currentWidget()
def onTabSwitched(self, tabnum):
self.currentView = self.tabWidget.widget(tabnum)
# Maybe no views exists now
if self.currentView:
self.currentView.setFocus()
self.emit(QtCore.SIGNAL('tabSwitched'))
def onLoadFinished(self):
self.emit(QtCore.SIGNAL('checkToolBar'))
def setTabName(self, view, title):
def shortenTitle(title):
return title[0:12] + u"..." if len(title) > 15 else title
index = self.tabWidget.indexOf(view)
if index != -1 :
title = shortenTitle(title)
self.tabWidget.setTabText(index, title)
def onFind(self):
self.frameFind.show()
self.editFind.setFocus()
self.editFind.setSelection(0, len(self.editFind.text()))
def onFindReturnPressed(self):
self.find()
def onTextEdited(self, _text):
self.find()
def find(self):
self.tabWidget.currentWidget().find(self.editFind.text(),
False, self.checkCase.isChecked())
if not self.frameFind.isVisible():
self.frameFind.show()
def onFindPrevious(self):
self.tabWidget.currentWidget().find(self.editFind.text(),
True, self.checkCase.isChecked())
def onFindNext(self):
self.tabWidget.currentWidget().find(self.editFind.text(),
False , self.checkCase.isChecked())
def saveTo(self, db):
"save snapshot of opened views "
# we only remember now, not past...
db.clear()
def isRemoteURL(url):
return url.scheme() in ["http", "https"]
for index, view in enumerate(self.webviews):
if isRemoteURL(view.loadedURL) and not self.config.openRemoteURL:
continue
key = str(index + 1) # avoid 0
value = Pickle.dumps((view.chmfile.path,
unicode(view.loadedURL.toString()),
view.currentPos(),
)
)
db[key] = value
db.sync()
def loadFrom(self, db):
"restore previously open views from snapshot"
for value in db.values():
path, url, pos = Pickle.loads(value)
view = self.openChmFile(path)
if view :
view.suggestedPos = pos
view.loadURL(QUrl(url) )
if __name__ == '__main__':
raise NotImplementedError()