Kiln » TortoiseHg » TortoiseHg
Clone URL:  
Pushed to one repository · View In Graph Contained in 1.9, 1.9.1, and 1.9.2

extract new module repotreeitem

Changeset 24bba354ddf2

Parent 2f206961358f

by Adrian Buehlmann

Changes to 2 files · Browse files at 24bba354ddf2 Showing diff from parent 2f206961358f Diff from another changeset...

Change 1 of 1 Show Entire File tortoisehg/​hgqt/​repotreeitem.py Stacked
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
@@ -0,0 +1,329 @@
+# repotreeitem.py - treeitems for the reporegistry +# +# Copyright 2010 Adrian Buehlmann <adrian@cadifra.com> +# +# This software may be used and distributed according to the terms of the +# GNU General Public License version 2 or any later version. + +import sys +import os + +from mercurial import hg, url + +from PyQt4 import QtCore, QtGui + +from PyQt4.QtCore import Qt, QVariant, QString + +from tortoisehg.hgqt.i18n import _ +from tortoisehg.hgqt.qtlib import geticon + +from tortoisehg.hgqt.settings import SettingsDialog + + +xmlClassMap = { + 'allgroup': 'AllRepoGroupItem', + 'group': 'RepoGroupItem', + 'repo': 'RepoItem', + 'treeitem': 'RepoTreeItem', + 'paths': 'RepoPathsItem', + 'path': 'RepoPathItem', + } + +inverseXmlClassMap = {} + +def xmlToClass(ele): + return xmlClassMap[ele] + +def classToXml(classname): + if len(inverseXmlClassMap) == 0: + for k,v in xmlClassMap.iteritems(): + inverseXmlClassMap[v] = k + return inverseXmlClassMap[classname] + +def undumpObject(xr, model): + classname = xmlToClass(str(xr.name().toString())) + class_ = getattr(sys.modules[RepoTreeItem.__module__], classname) + obj = class_(model) + obj.undump(xr) + return obj + + +class RepoTreeItem(object): + def __init__(self, model, parent=None): + self.model = model + self._parent = parent + self.childs = [] + self._row = 0 + + def appendChild(self, child): + child._row = len(self.childs) + child._parent = self + self.childs.append(child) + + def child(self, row): + return self.childs[row] + + def childCount(self): + return len(self.childs) + + def columnCount(self): + return 2 + + def data(self, column, role): + return QVariant() + + def setData(self, column, value): + return False + + def row(self): + return self._row + + def parent(self): + return self._parent + + def menulist(self): + return [] + + def flags(self): + return Qt.NoItemFlags + + def removeRows(self, row, count): + cs = self.childs + remove = cs[row : row + count] + keep = cs[:row] + cs[row + count:] + self.childs = keep + for c in remove: + c._row = 0 + c._parent = None + for i, c in enumerate(keep): + c._row = i + return True + + def dump(self, xw): + for c in self.childs: + c.dumpObject(xw) + + def undump(self, xr): + while not xr.atEnd(): + xr.readNext() + if xr.isStartElement(): + item = undumpObject(xr, self.model) + self.appendChild(item) + elif xr.isEndElement(): + break + + def dumpObject(self, xw): + xw.writeStartElement(classToXml(self.__class__.__name__)) + self.dump(xw) + xw.writeEndElement() + + def open(self): + pass + + def details(self): + return '' + + +class RepoItem(RepoTreeItem): + def __init__(self, model, rootpath='', parent=None): + RepoTreeItem.__init__(self, model, parent) + self._root = rootpath + self._setttingsdlg = None + if rootpath: + pi = RepoPathsItem(model) + self.appendChild(pi) + repo = hg.repository(model.ui, path=rootpath) + for alias, path in repo.ui.configitems('paths'): + item = RepoPathItem(model, alias, path) + pi.appendChild(item) + + def rootpath(self): + return self._root + + def data(self, column, role): + if role == Qt.DecorationRole: + if column == 0: + ico = geticon('hg') + return QVariant(ico) + return QVariant() + if column == 0: + return QVariant(os.path.basename(self._root)) + elif column == 1: + return QVariant(self._root) + return QVariant() + + def menulist(self): + return ['open', 'remove', None, 'settings'] + + def flags(self): + return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsDragEnabled + + def removeRows(self, row, count): + return False + + def dump(self, xw): + xw.writeAttribute('root', self._root) + RepoTreeItem.dump(self, xw) + + def undump(self, xr): + a = xr.attributes() + self._root = str(a.value('', 'root').toString()) + RepoTreeItem.undump(self, xr) + + def open(self): + self.model.openrepofunc(self._root) + + def startSettings(self, parent): + if self._setttingsdlg is None: + self._setttingsdlg = SettingsDialog( + configrepo=True, focus='web.name', parent=parent, + root=self._root) + self._setttingsdlg.show() + + def details(self): + return _('Local Repository %s') % self._root + + +class RepoPathsItem(RepoTreeItem): + def __init__(self, model, parent=None): + RepoTreeItem.__init__(self, model, parent) + + def data(self, column, role): + if role == Qt.DisplayRole and column == 0: + return QVariant(_('Synchronize')) + return QVariant() + + def setData(self, column, value): + return False + + def menulist(self): + return [] + + def flags(self): + return Qt.ItemIsEnabled | Qt.ItemIsSelectable + + def dump(self, xw): + RepoTreeItem.dump(self, xw) + + def undump(self, xr): + RepoTreeItem.undump(self, xr) + + +class RepoPathItem(RepoTreeItem): + def __init__(self, model, alias='', path='', parent=None): + RepoTreeItem.__init__(self, model, parent) + self._alias = alias + self._path = path + + def url(self): + return self._path + + def data(self, column, role): + if role == Qt.DecorationRole: + if column == 0: + ico = geticon('sync') + return QVariant(ico) + return QVariant() + if column == 0: + return QVariant(self._alias) + elif column == 1: + path = url.hidepassword(self._path) + return QVariant(path) + return QVariant() + + def menulist(self): + return ['pull'] + + def flags(self): + return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsDragEnabled + + def removeRows(self, row, count): + return False + + def dump(self, xw): + xw.writeAttribute('alias', self._alias) + xw.writeAttribute('path', self._path) + RepoTreeItem.dump(self, xw) + + def undump(self, xr): + a = xr.attributes() + self._alias = str(a.value('', 'alias').toString()) + self._path = str(a.value('', 'path').toString()) + RepoTreeItem.undump(self, xr) + + def details(self): + path = url.hidepassword(self._path) + return _('Repository URL %s') % path + + +class RepoGroupItem(RepoTreeItem): + def __init__(self, model, name=None, parent=None): + RepoTreeItem.__init__(self, model, parent) + if name: + self.name = name + else: + self.name = QString() + + def data(self, column, role): + if role == Qt.DecorationRole: + if column == 0: + s = QtGui.QApplication.style() + ico = s.standardIcon(QtGui.QStyle.SP_DirIcon) + return QVariant(ico) + return QVariant() + if column == 0: + return QVariant(self.name) + return QVariant() + + def setData(self, column, value): + if column == 0: + self.name = value.toString() + return True + return False + + def menulist(self): + return ['newGroup', None, 'rename', 'remove'] + + def flags(self): + return (Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsDropEnabled + | Qt.ItemIsEditable) + + def dump(self, xw): + xw.writeAttribute('name', self.name) + RepoTreeItem.dump(self, xw) + + def undump(self, xr): + a = xr.attributes() + self.name = a.value('', 'name').toString() + RepoTreeItem.undump(self, xr) + + +class AllRepoGroupItem(RepoTreeItem): + def __init__(self, model, parent=None): + RepoTreeItem.__init__(self, model, parent) + + def data(self, column, role): + if role == Qt.DecorationRole: + if column == 0: + s = QtGui.QApplication.style() + ico = s.standardIcon(QtGui.QStyle.SP_DirIcon) + return QVariant(ico) + return QVariant() + if column == 0: + return QVariant(_('all')) + return QVariant() + + def setData(self, column, value): + return False + + def menulist(self): + return ['newGroup'] + + def flags(self): + return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsDropEnabled + + def dump(self, xw): + RepoTreeItem.dump(self, xw) + + def undump(self, xr): + RepoTreeItem.undump(self, xr)
 
5
6
7
8
9
10
11
12
13
 
14
15
16
17
18
19
20
21
 
22
23
24
 
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
 
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
 
5
6
7
 
 
 
 
 
 
8
9
10
11
12
13
 
14
 
15
16
17
18
 
20
21
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
24
25
 
49
50
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
53
54
@@ -5,20 +5,14 @@
 # This software may be used and distributed according to the terms of the  # GNU General Public License version 2 or any later version.   -import sys -import os - -from mercurial import hg, url - -from PyQt4 import QtCore, QtGui +from PyQt4 import QtCore    from PyQt4.QtCore import Qt, QVariant, SIGNAL, SLOT  from PyQt4.QtCore import QModelIndex, QString    from tortoisehg.hgqt.i18n import _ -from tortoisehg.hgqt.qtlib import geticon   -from tortoisehg.hgqt.settings import SettingsDialog +from repotreeitem import undumpObject, AllRepoGroupItem, RepoGroupItem, RepoItem      extractXmlElementName = 'reporegextract' @@ -26,25 +20,6 @@
   repoRegMimeType = 'application/thg-reporegistry'   -xmlClassMap = { - 'allgroup': 'AllRepoGroupItem', - 'group': 'RepoGroupItem', - 'repo': 'RepoItem', - 'treeitem': 'RepoTreeItem', - 'paths': 'RepoPathsItem', - 'path': 'RepoPathItem', - } - -inverseXmlClassMap = {} - -def xmlToClass(ele): - return xmlClassMap[ele] - -def classToXml(classname): - if len(inverseXmlClassMap) == 0: - for k,v in xmlClassMap.iteritems(): - inverseXmlClassMap[v] = k - return inverseXmlClassMap[classname]    def writeXml(target, item, rootElementName):   xw = QtCore.QXmlStreamWriter(target) @@ -74,294 +49,6 @@
  print str(xr.errorString())   return itemread   -def undumpObject(xr, model): - classname = xmlToClass(str(xr.name().toString())) - class_ = getattr(sys.modules[RepoTreeItem.__module__], classname) - obj = class_(model) - obj.undump(xr) - return obj - - -class RepoTreeItem: - def __init__(self, model, parent=None): - self.model = model - self._parent = parent - self.childs = [] - self._row = 0 - - def appendChild(self, child): - child._row = len(self.childs) - child._parent = self - self.childs.append(child) - - def child(self, row): - return self.childs[row] - - def childCount(self): - return len(self.childs) - - def columnCount(self): - return 2 - - def data(self, column, role): - return QVariant() - - def setData(self, column, value): - return False - - def row(self): - return self._row - - def parent(self): - return self._parent - - def menulist(self): - return [] - - def flags(self): - return Qt.NoItemFlags - - def removeRows(self, row, count): - cs = self.childs - remove = cs[row : row + count] - keep = cs[:row] + cs[row + count:] - self.childs = keep - for c in remove: - c._row = 0 - c._parent = None - for i, c in enumerate(keep): - c._row = i - return True - - def dump(self, xw): - for c in self.childs: - c.dumpObject(xw) - - def undump(self, xr): - while not xr.atEnd(): - xr.readNext() - if xr.isStartElement(): - item = undumpObject(xr, self.model) - self.appendChild(item) - elif xr.isEndElement(): - break - - def dumpObject(self, xw): - xw.writeStartElement(classToXml(self.__class__.__name__)) - self.dump(xw) - xw.writeEndElement() - - def open(self): - pass - - def details(self): - return '' - - -class RepoItem(RepoTreeItem): - def __init__(self, model, rootpath='', parent=None): - RepoTreeItem.__init__(self, model, parent) - self._root = rootpath - self._setttingsdlg = None - if rootpath: - pi = RepoPathsItem(model) - self.appendChild(pi) - repo = hg.repository(model.ui, path=rootpath) - for alias, path in repo.ui.configitems('paths'): - item = RepoPathItem(model, alias, path) - pi.appendChild(item) - - def rootpath(self): - return self._root - - def data(self, column, role): - if role == Qt.DecorationRole: - if column == 0: - ico = geticon('hg') - return QVariant(ico) - return QVariant() - if column == 0: - return QVariant(os.path.basename(self._root)) - elif column == 1: - return QVariant(self._root) - return QVariant() - - def menulist(self): - return ['open', 'remove', None, 'settings'] - - def flags(self): - return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsDragEnabled - - def removeRows(self, row, count): - return False - - def dump(self, xw): - xw.writeAttribute('root', self._root) - RepoTreeItem.dump(self, xw) - - def undump(self, xr): - a = xr.attributes() - self._root = str(a.value('', 'root').toString()) - RepoTreeItem.undump(self, xr) - - def open(self): - self.model.openrepofunc(self._root) - - def startSettings(self, parent): - if self._setttingsdlg is None: - self._setttingsdlg = SettingsDialog( - configrepo=True, focus='web.name', parent=parent, - root=self._root) - self._setttingsdlg.show() - - def details(self): - return _('Local Repository %s') % self._root - - -class RepoPathsItem(RepoTreeItem): - def __init__(self, model, parent=None): - RepoTreeItem.__init__(self, model, parent) - - def data(self, column, role): - if role == Qt.DisplayRole and column == 0: - return QVariant(_('Synchronize')) - return QVariant() - - def setData(self, column, value): - return False - - def menulist(self): - return [] - - def flags(self): - return Qt.ItemIsEnabled | Qt.ItemIsSelectable - - def dump(self, xw): - RepoTreeItem.dump(self, xw) - - def undump(self, xr): - RepoTreeItem.undump(self, xr) - - -class RepoPathItem(RepoTreeItem): - def __init__(self, model, alias='', path='', parent=None): - RepoTreeItem.__init__(self, model, parent) - self._alias = alias - self._path = path - - def url(self): - return self._path - - def data(self, column, role): - if role == Qt.DecorationRole: - if column == 0: - ico = geticon('sync') - return QVariant(ico) - return QVariant() - if column == 0: - return QVariant(self._alias) - elif column == 1: - path = url.hidepassword(self._path) - return QVariant(path) - return QVariant() - - def menulist(self): - return ['pull'] - - def flags(self): - return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsDragEnabled - - def removeRows(self, row, count): - return False - - def dump(self, xw): - xw.writeAttribute('alias', self._alias) - xw.writeAttribute('path', self._path) - RepoTreeItem.dump(self, xw) - - def undump(self, xr): - a = xr.attributes() - self._alias = str(a.value('', 'alias').toString()) - self._path = str(a.value('', 'path').toString()) - RepoTreeItem.undump(self, xr) - - def details(self): - path = url.hidepassword(self._path) - return _('Repository URL %s') % path - - -class RepoGroupItem(RepoTreeItem): - def __init__(self, model, name=None, parent=None): - RepoTreeItem.__init__(self, model, parent) - if name: - self.name = name - else: - self.name = QString() - - def data(self, column, role): - if role == Qt.DecorationRole: - if column == 0: - s = QtGui.QApplication.style() - ico = s.standardIcon(QtGui.QStyle.SP_DirIcon) - return QVariant(ico) - return QVariant() - if column == 0: - return QVariant(self.name) - return QVariant() - - def setData(self, column, value): - if column == 0: - self.name = value.toString() - return True - return False - - def menulist(self): - return ['newGroup', None, 'rename', 'remove'] - - def flags(self): - return (Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsDropEnabled - | Qt.ItemIsEditable) - - def dump(self, xw): - xw.writeAttribute('name', self.name) - RepoTreeItem.dump(self, xw) - - def undump(self, xr): - a = xr.attributes() - self.name = a.value('', 'name').toString() - RepoTreeItem.undump(self, xr) - - -class AllRepoGroupItem(RepoTreeItem): - def __init__(self, model, parent=None): - RepoTreeItem.__init__(self, model, parent) - - def data(self, column, role): - if role == Qt.DecorationRole: - if column == 0: - s = QtGui.QApplication.style() - ico = s.standardIcon(QtGui.QStyle.SP_DirIcon) - return QVariant(ico) - return QVariant() - if column == 0: - return QVariant(_('all')) - return QVariant() - - def setData(self, column, value): - return False - - def menulist(self): - return ['newGroup'] - - def flags(self): - return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsDropEnabled - - def dump(self, xw): - RepoTreeItem.dump(self, xw) - - def undump(self, xr): - RepoTreeItem.undump(self, xr) -    class RepoTreeModel(QtCore.QAbstractItemModel):   def __init__(self, openrepofunc, ui, filename=None, parent=None):