-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpychmbookmarks.py
More file actions
189 lines (153 loc) · 6.03 KB
/
pychmbookmarks.py
File metadata and controls
189 lines (153 loc) · 6.03 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
#!/usr/bin/python
# vim: set fileencoding=utf-8 :
" Provides the bookmark panel. "
import cPickle as Pickle
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QListWidgetItem
from Ui_panelbookmarks import Ui_PanelBookmarks
class PyChmBookmarkItem(QListWidgetItem):
" Represent one item of bookmark"
def __init__(self, parent, name=None, url=None, pos=None):
QListWidgetItem.__init__(self, parent)
self.name = name
self.url = url
self.pos = pos
def saveTo(self, db):
" Save this item into external db."
name = self.name.encode('utf-8')
value = Pickle.dumps((self.url, self.pos))
db[name] = value
db.sync()
def delFrom(self, db):
" Delete this item from external db."
name = self.name.encode('utf-8')
try:
del db[name]
db.sync()
except StandardError :
pass
def setValue(self, db_value):
" Update the value "
self.url, self.pos = Pickle.loads(db_value)
class PyChmBookmarksView(QtGui.QWidget, Ui_PanelBookmarks):
" Implements the bookmark panel. "
def __init__(self, mainwin=None, parent=None, ):
QtGui.QWidget.__init__(self, parent)
self.setupUi(self)
self.mainwin = mainwin
# { } used as dummy database, to prevent NoneType Error
self.db = { }
self.connect(self.buttonAddBookmark,
QtCore.SIGNAL('clicked()'),
self.onAddBookmark)
self.connect(self.buttonEditBookmark,
QtCore.SIGNAL('clicked()'),
self.onEditBookmark)
self.connect(self.buttonDelBookmark,
QtCore.SIGNAL('clicked()'),
self.onDelBookmark)
self.connect(self.list,
QtCore.SIGNAL('itemDoubleClicked(QListWidgetItem*)'),
self.onOpenBookmark
)
def _getNameFromUser( self,
title=u"add bookmark",
prompt=u"input the name of this bookmark",
default=u"new bookmark",
):
" Prompt user to input name. "
#FIXME ; the dialog maybe not wide enough to show title completely.
name, ok = QtGui.QInputDialog.getText( self,
title,
prompt,
QtGui.QLineEdit.Normal,
default,
)
if not ok or not name:
return u""
else:
# transform QString into unicode.
return unicode(name)
def _getNameForBookmark(self,
title=u"add bookmar",
prompt=u"input the name of this bookmark",
default=u"new bookmark",
):
" Prompt user to specify name for bookmark item. "
name = self._getNameFromUser( title=title,
prompt=prompt,
default=default,
)
if name :
# name confliction is not allowed
while self.db.has_key(name.encode('utf-8')):
title = u"Name confliction"
prompt = u"Bookmark '%s' already exists, choose another name."\
% name
name = self._getNameFromUser( title=title,
prompt=prompt,
default=name,
)
if not name:
break
return name
def onTabSwitched(self):
" update the contents to fit with current file."
self.clear()
if self.mainwin.currentView:
chmfile = self._getCurrentChmFile()
self.db = chmfile.bookmarkdb
self.loadBookmarks()
def loadBookmarks(self):
" Load bookmarks from external db. "
self.clear()
for key, value in self.db.iteritems():
item = PyChmBookmarkItem(self.list)
key = key.decode('utf-8')
item.name = key
item.setValue(value)
item.setText(key)
self.list.update()
def clear(self):
" Clear current display. "
self.list.clear()
def _getCurrentView(self):
"Convenient method for getting current view. "
return self.mainwin.currentView
def _getCurrentChmFile(self):
"Convenient method for getting current file. "
return self._getCurrentView().chmfile
def onAddBookmark(self):
"Add new bookmark item. "
webview = self._getCurrentView()
url = unicode( webview.loadedURL.path() )
title = webview.title()
pos = webview.currentPos()
default_name = title or u"new bookmark"
name = self._getNameForBookmark(default=default_name)
if name:
item = PyChmBookmarkItem(self.list, name, url, pos)
item.setText(name)
item.saveTo(self.db)
def onEditBookmark(self):
" Edit selected bookmark item. "
item = self.list.currentItem()
if item :
name = self._getNameForBookmark( title=u"rename bookmark",
prompt=u'input new name',
)
if name:
item.name = name
item.setText(name)
def onDelBookmark(self):
" Delete selectd bookmark item. "
item = self.list.currentItem()
if item :
item.delFrom(self.db)
self.list.takeItem(self.list.row(item))
def onOpenBookmark(self, item):
" Open selected bookmark item. "
if item :
webview = self._getCurrentView()
webview.loadURL(item.url)
webview.suggestedPos = item.pos