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

reporegistry: use xml for drag and drop mime data serialization

Changeset 542f056c216c

Parent 19f41afd2f3b

by Adrian Buehlmann

Changes to one file · Browse files at 542f056c216c Showing diff from parent 19f41afd2f3b Diff from another changeset...

 
5
6
7
 
8
9
10
 
18
19
20
 
 
 
21
22
23
 
65
66
67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
69
70
 
71
72
73
 
90
91
92
 
 
 
 
 
 
 
 
 
 
 
 
93
94
95
 
107
108
109
 
 
 
 
 
 
 
 
 
 
 
110
111
112
 
186
187
188
 
 
 
 
 
189
190
191
192
 
193
194
195
196
197
198
199
 
 
200
201
 
202
203
204
 
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
 
5
6
7
8
9
10
11
 
19
20
21
22
23
24
25
26
27
 
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
 
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
 
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
 
256
257
258
259
260
261
262
263
264
265
266
 
267
268
269
270
271
272
 
 
273
274
275
 
276
277
278
279
 
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
@@ -5,6 +5,7 @@
 # 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 PyQt4 import QtCore, QtGui @@ -18,6 +19,9 @@
   connect = QtCore.QObject.connect   +extractXmlElementName = 'reporegextract' +repoRegMimeType = 'application/thg-reporegistry' +  class RepoTreeItem:   def __init__(self, parent=None):   self._parent = parent @@ -65,9 +69,52 @@
  c._row = i   return True   + def dump(self, xw): + for c in self.childs: + c.dumpObject(xw) + + def undump(self, xr): + print "RepoTreeItem.undump()" + while not xr.atEnd(): + xr.readNext() + if xr.isStartElement(): + item = undumpObject(xr) + self.appendChild(item) + xr.skipCurrentElement() + elif xr.isEndElement(): + break + + def dumpObject(self, xw): + xw.writeStartElement(self.__class__.__name__) + self.dump(xw) + xw.writeEndElement() + + +def encodeToXml(item, rootElementName): + buf = QtCore.QByteArray() + xw = QtCore.QXmlStreamWriter(buf) + xw.setAutoFormatting(True) + xw.setAutoFormattingIndent(2) + xw.writeStartDocument() + xw.writeStartElement(rootElementName) + item.dumpObject(xw) + xw.writeEndElement() + xw.writeEndDocument() + return buf + + +def undumpObject(xr): + print "undumpObject()" + classname = str(xr.name().toString()) + print "classname = %s" % classname + class_ = getattr(sys.modules[RepoTreeItem.__module__], classname) + obj = class_() + obj.undump(xr) + return obj +    class RepoItem(RepoTreeItem): - def __init__(self, rootpath, parent=None): + def __init__(self, rootpath='', parent=None):   RepoTreeItem.__init__(self, parent)   self._rootpath = rootpath   @@ -90,6 +137,18 @@
  def removeRows(self, row, count):   return False   + def dump(self, xw): + RepoTreeItem.dump(self, xw) + xw.writeAttribute('rootpath', self._rootpath) + + def undump(self, xr): + print "RepoItem.undump()" + a = xr.attributes() + self._rootpath = str(a.value('', 'rootpath').toString()) + print "self._rootpath = %s" % self._rootpath + RepoTreeItem.undump(self, xr) + print "RepoItem.undump() finished" +    class RepoGroupItem(RepoTreeItem):   def __init__(self, name, parent=None): @@ -107,6 +166,17 @@
  def flags(self):   return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsDropEnabled   + def dump(self, xw): + xw.writeAttribute('name', self.name) + RepoTreeItem.dump(self, xw) + + def undump(self, xr): + print "RepoGroupItem.undump()" + a = xr.attributes() + self.name = str(a.value('', 'name').toString()) + print "self.name = %s" % self.name + RepoTreeItem.undump(self, xr) +    class RepoTreeModel(QtCore.QAbstractItemModel):   def __init__(self, parent=None): @@ -186,19 +256,24 @@
  item = parent.internalPointer()   res = item.removeRows(row, count)   self.endRemoveRows() + + # dump everything for debug purposes + all = encodeToXml(self.rootItem, 'reporegistry') + print all +   return res     def mimeTypes(self): - return QtCore.QStringList('application/thg-reporegistry') + return QtCore.QStringList(repoRegMimeType)     def mimeData(self, indexes):   print "mimeData()"   i = indexes[0]   item = i.internalPointer() - buf = 'repo=%s' % item.rootpath() - print buf + buf = encodeToXml(item, extractXmlElementName) + print str(buf)   d = QtCore.QMimeData() - d.setData('application/thg-reporegistry', buf) + d.setData(repoRegMimeType, buf)   return d     def dropMimeData(self, data, action, row, column, parent): @@ -207,25 +282,33 @@
  print "formats:"   for s in data.formats():   print s - d = str(data.data('application/thg-reporegistry')) - d = d.split('=') - print d - if d[0] != 'repo': - print "not a repo" - return False - path = d[1] - print "path = %s" % path + d = str(data.data(repoRegMimeType)) + print "d = %s" % d + + xr = QtCore.QXmlStreamReader(d) + if xr.readNextStartElement(): + ele = str(xr.name().toString()) + if ele != extractXmlElementName: + print "unexpected xml element '%s' "\ + "(was looking for %s)" % (ele, extractXmlElementName) + return + if xr.hasError(): + print str(xr.errorString()) + if xr.readNextStartElement(): + itemread = undumpObject(xr) + xr.skipCurrentElement() + if xr.hasError(): + print str(xr.errorString()) + + print "itemread = %s" % itemread +   group = parent.internalPointer() - print "group = %s" % group   cc = group.childCount()   self.beginInsertRows(parent, cc, cc) - print "1: group.childCount() = %s" % group.childCount() - group.appendChild(RepoItem(path)) - print "2: group.childCount() = %s" % group.childCount() + group.appendChild(itemread)   self.endInsertRows()   return True   -   # functions not defined in QAbstractItemModel     def allreposIndex(self):