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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
|
# -*- coding: utf-8 -*-
#
# PartMgr GUI - Tree widget
#
# Copyright 2014-2015 Michael Buesch <m@bues.ch>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
from partmgr.gui.util import *
from partmgr.core.category import *
from partmgr.core.stockitem import *
from partmgr.core.part import *
class TreeItem(object):
# Types
CATEGORY = 0
STOCKITEM = 1
def __init__(self, entityType, entityId, modelIndex=None):
self.entityType = entityType # CATEGORY or STOCKITEM
self.entityId = entityId # Entity.id
self.modelIndex = modelIndex
def toEntity(self, db):
if self.entityType == self.CATEGORY:
return db.getCategory(self.entityId)
elif self.entityType == self.STOCKITEM:
return db.getStockItem(self.entityId)
else:
assert(0)
class TreeModel(QAbstractItemModel):
def __init__(self, db, parent=None):
QAbstractItemModel.__init__(self, parent)
self.db = db
self.fileIconProv = QFileIconProvider()
self.__clearLookup()
def __clearLookup(self):
self.__idCount = 0
self.__fwdLookup = {}
self.__bwdLookup = {}
def __treeItemToUniqueId(self, treeItem):
key = (treeItem.entityType, treeItem.entityId)
try:
return self.__fwdLookup[key]
except KeyError as e:
newId = self.__idCount
self.__idCount += 1
self.__fwdLookup[key] = newId
self.__bwdLookup[newId] = key
return newId
def __uniqueIdToTreeItem(self, uid):
try:
entityType, entityId = self.__bwdLookup[uid]
return TreeItem(entityType, entityId)
except KeyError as e:
return None
def __makeModelIndex(self, row, column, entity):
if isinstance(entity, Category):
entityType = TreeItem.CATEGORY
elif isinstance(entity, StockItem):
entityType = TreeItem.STOCKITEM
else:
assert(0)
treeItem = TreeItem(entityType, entity.getId())
uid = self.__treeItemToUniqueId(treeItem)
return self.createIndex(row, column, int(uid))
def entityToRowNumber(self, entity):
parentCategory = entity.getParent()
if not parentCategory:
# entity is a root category.
return self.db.getRootCategories().index(entity)
if isinstance(entity, Category):
return parentCategory.getChildCategories().index(entity)
elif isinstance(entity, StockItem):
return parentCategory.countChildCategories() +\
parentCategory.getChildStockItems().index(entity)
assert(0)
def entityToModelIndex(self, entity):
if not entity:
return QModelIndex()
return self.__makeModelIndex(self.entityToRowNumber(entity),
0, entity)
def modelIndexToTreeItem(self, modelIndex):
if not modelIndex.isValid():
return None
uid = int(modelIndex.internalId())
treeItem = self.__uniqueIdToTreeItem(uid)
treeItem.modelIndex = modelIndex
return treeItem
def flags(self, index):
if not index.isValid():
return Qt.NoItemFlags
return Qt.ItemIsEnabled |\
Qt.ItemIsSelectable |\
Qt.ItemIsEditable
def columnCount(self, parentIndex=QModelIndex()):
return 1
def rowCount(self, parentIndex=QModelIndex()):
if parentIndex.isValid():
treeItem = self.modelIndexToTreeItem(parentIndex)
if treeItem.entityType == TreeItem.CATEGORY:
parentCatId = treeItem.entityId
nrCats = self.db.countChildCategories(parentCatId)
nrItems = self.db.countStockItemsByCategory(parentCatId)
return nrCats + nrItems
else:
return self.db.countRootCategories()
return 0
def index(self, row, column, parentIndex=QModelIndex()):
origRow, origColumn = row, column
if row < 0 or column < 0:
return QModelIndex()
if parentIndex.isValid():
treeItem = self.modelIndexToTreeItem(parentIndex)
if treeItem.entityType != TreeItem.CATEGORY:
return QModelIndex()
parentCatId = treeItem.entityId
childCats = self.db.getChildCategories(parentCatId)
if row < len(childCats):
return self.__makeModelIndex(origRow, origColumn,
childCats[row])
row -= len(childCats)
childItems = self.db.getStockItemsByCategory(parentCatId)
if row < len(childItems):
return self.__makeModelIndex(origRow, origColumn,
childItems[row])
assert(0)
else:
cats = self.db.getRootCategories()
if row < len(cats):
return self.__makeModelIndex(origRow, origColumn,
cats[row])
return QModelIndex()
def parent(self, index):
if not index.isValid():
return QModelIndex()
treeItem = self.modelIndexToTreeItem(index)
if treeItem.entityType == TreeItem.CATEGORY:
category = treeItem.toEntity(self.db)
if not category:
return QModelIndex()
parentCat = category.getParent()
elif treeItem.entityType == TreeItem.STOCKITEM:
stockItem = treeItem.toEntity(self.db)
if not stockItem:
return QModelIndex()
parentCat = stockItem.getCategory()
else:
assert(0)
if parentCat is None:
return QModelIndex()
return self.entityToModelIndex(parentCat)
def data(self, index, role=Qt.DisplayRole):
if not index.isValid():
return None
treeItem = self.modelIndexToTreeItem(index)
if role in (Qt.DisplayRole, Qt.EditRole):
entity = treeItem.toEntity(self.db)
return entity.getName()
elif role == Qt.DecorationRole:
if treeItem.entityType == TreeItem.CATEGORY:
icon = self.fileIconProv.icon(
QFileIconProvider.Folder)
else:
icon = self.fileIconProv.icon(
QFileIconProvider.File)
return icon
return None
def setData(self, index, value, role=Qt.EditRole):
if not index.isValid():
return False
if role != Qt.EditRole:
return False
treeItem = self.modelIndexToTreeItem(index)
self.renameTreeItem(treeItem, value)
return QAbstractItemModel.setData(self, index, value, role)
def headerData(self, section, orientation, role=Qt.DisplayRole):
if role == Qt.DisplayRole:
return "Stock items"
return None
# Add a new StockItem to the database
def addStockItem(self, parentTreeItem, stockItem):
assert(not stockItem.hasValidId() and\
not stockItem.hasCategory())
parentCat = parentTreeItem.toEntity(self.db) if parentTreeItem\
else None
stockItem.setCategory(parentCat) # reparent item
self.db.modifyStockItem(stockItem) # Add it
entityRow = self.entityToRowNumber(stockItem)
self.rowsInserted.emit(self.entityToModelIndex(parentCat),
entityRow, entityRow)
return self.entityToModelIndex(stockItem)
# Add a new Category to the database
def addCategory(self, parentTreeItem, category):
assert(not category.hasValidId())
parentCat = parentTreeItem.toEntity(self.db) if parentTreeItem\
else None
category.setParent(parentCat) # reparent category
self.db.modifyCategory(category) # Add it
entityRow = self.entityToRowNumber(category)
self.rowsInserted.emit(self.entityToModelIndex(parentCat),
entityRow, entityRow)
return self.entityToModelIndex(category)
# Delete a TreeItem (stock or category) from the database
def delTreeItem(self, treeItem):
if not treeItem:
return
entity = treeItem.toEntity(self.db)
parentCat = entity.getParent()
entityRow = self.entityToRowNumber(entity)
entity.delete()
self.rowsRemoved.emit(self.entityToModelIndex(parentCat),
entityRow, entityRow)
# Rename a TreeItem (stock or category).
def renameTreeItem(self, treeItem, newName):
entity = treeItem.toEntity(self.db)
oldName = entity.getName()
if newName == oldName:
return
# Set the new entity name.
entity.setName(newName)
if treeItem.entityType == TreeItem.STOCKITEM:
stockItem = entity
# Wheeeee, magic happens:
# If a part is assigned to the stock item and we are
# the only user of that part and the part has the same name,
# the part is renamed, too.
# If there was no old name and no part, we try to make a part
# for this stock item, but only if the part does not exist.
# If there is a part by that name, we take that.
# Otherwise no part is assigned.
db = stockItem.getDatabase()
part = stockItem.getPart()
category = stockItem.getCategory()
allStockItems = db.getStockItemsByCategory(category)\
if category else ()
allParts = db.getPartsByCategory(category)\
if category else ()
itemsWithSamePart = [ si for si in allStockItems
if si.getPart() == part ]
partsWithNewName = [ p for p in allParts\
if p.getName() == newName ]
if part and\
len(itemsWithSamePart) == 1 and\
itemsWithSamePart[0] == stockItem and\
part.getName().strip() == oldName.strip():
# Also rename the part.
part.setName(newName)
elif not part and len(partsWithNewName) == 1:
# Take this part which already has the new name.
stockItem.setPart(partsWithNewName[0])
elif not part and not oldName.strip():
# Create a new part.
newPart = Part(newName, category = category)
db.modifyPart(newPart)
stockItem.setPart(newPart)
# Our data changed. Emit the signal.
modelIndex = self.entityToModelIndex(entity)
self.dataChanged.emit(modelIndex, modelIndex)
class Tree(QTreeView):
itemChanged = Signal(int)
def __init__(self, db, parent=None):
QTreeView.__init__(self, parent)
self.db = db
model = TreeModel(db, self)
proxyModel = QSortFilterProxyModel(self)
proxyModel.setSourceModel(model)
model.dataChanged.connect(self.__modelDataChanged)
self.setModel(proxyModel)
self.setSortingEnabled(True)
self.sortByColumn(0, Qt.AscendingOrder)
def __modelDataChanged(self):
selected = self.model().mapToSource(self.currentIndex())
treeItem = self.realModel().modelIndexToTreeItem(selected)
self.itemChanged.emit(treeItem.entityId)
def realModel(self):
return self.model().sourceModel()
def currentChanged(self, selected, deselected):
QTreeView.currentChanged(self, selected, deselected)
selected = self.model().mapToSource(selected)
treeItem = self.realModel().modelIndexToTreeItem(selected)
if treeItem.entityType == TreeItem.STOCKITEM:
self.itemChanged.emit(treeItem.entityId)
else:
self.itemChanged.emit(None)
def contextMenuEvent(self, event):
index = self.indexAt(event.pos())
index = self.model().mapToSource(index)
treeItem = self.realModel().modelIndexToTreeItem(index)
self.contextTreeItem = treeItem
menu = QMenu(self)
if treeItem:
if treeItem.entityType == TreeItem.CATEGORY:
menu.addAction("Add sub-&category...",
self.addCategory)
menu.addAction("Add &stock item...",
self.addStockItem)
menu.addSeparator()
menu.addAction("&Rename category",
self.renameCategory)
menu.addAction("&Delete category",
self.delCategory)
elif treeItem.entityType == TreeItem.STOCKITEM:
menu.addAction("R&ename stock item",
self.renameStockItem)
menu.addAction("De&lete stock item",
self.delStockItem)
else:
assert(0)
else:
menu.addAction("Add &root-category...", self.addCategory)
menu.addSeparator()
menu.addAction("Colla&pse all", self.collapseAll)
menu.addAction("E&xpand all", self.expandAll)
menu.exec_(event.globalPos())
super(Tree, self).contextMenuEvent(event)
def keyPressEvent(self, ev):
super(Tree, self).keyPressEvent(ev)
if ev.key() == Qt.Key_Delete:
index = self.model().mapToSource(self.currentIndex())
self.contextTreeItem = self.realModel().modelIndexToTreeItem(index)
if not self.contextTreeItem:
return
if self.contextTreeItem.entityType == TreeItem.CATEGORY:
self.delCategory()
elif self.contextTreeItem.entityType == TreeItem.STOCKITEM:
self.delStockItem()
else:
assert(0)
def addCategory(self):
parentTreeItem = self.contextTreeItem
assert(parentTreeItem is None or\
parentTreeItem.entityType == TreeItem.CATEGORY)
category = Category("New category")
newModelIndex = self.realModel().addCategory(
parentTreeItem, category)
newModelIndex = self.model().mapFromSource(newModelIndex)
if parentTreeItem:
self.expand(self.model().mapFromSource(parentTreeItem.modelIndex))
self.setCurrentIndex(newModelIndex)
self.edit(newModelIndex)
def delCategory(self):
assert(self.contextTreeItem.entityType == TreeItem.CATEGORY)
category = self.contextTreeItem.toEntity(self.db)
ret = QMessageBox.question(self,
"Really delete category?",
"Really delete category '%s'?" % category.getName(),
QMessageBox.Yes | QMessageBox.No)
if ret & QMessageBox.Yes == 0:
return
self.realModel().delTreeItem(self.contextTreeItem)
def renameCategory(self):
assert(self.contextTreeItem.entityType == TreeItem.CATEGORY)
category = self.contextTreeItem.toEntity(self.db)
newName, ok = QInputDialog.getText(
self, "Rename category",
"Rename category",
QLineEdit.Normal,
category.getName())
if not ok:
return
self.realModel().renameTreeItem(self.contextTreeItem, newName)
def renameStockItem(self):
assert(self.contextTreeItem.entityType == TreeItem.STOCKITEM)
stockItem = self.contextTreeItem.toEntity(self.db)
newName, ok = QInputDialog.getText(
self, "Rename item",
"Rename item",
QLineEdit.Normal,
stockItem.getName())
if not ok:
return
self.realModel().renameTreeItem(self.contextTreeItem, newName)
def addStockItem(self):
assert(self.contextTreeItem.entityType == TreeItem.CATEGORY)
stockItem = StockItem("")
newModelIndex = self.realModel().addStockItem(
self.contextTreeItem, stockItem)
newModelIndex = self.model().mapFromSource(newModelIndex)
self.expand(self.model().mapFromSource(self.contextTreeItem.modelIndex))
self.setCurrentIndex(newModelIndex)
self.edit(newModelIndex)
def delStockItem(self):
assert(self.contextTreeItem.entityType == TreeItem.STOCKITEM)
stockItem = self.contextTreeItem.toEntity(self.db)
ret = QMessageBox.question(self,
"Really delete item?",
"Really delete item '%s'?" %\
stockItem.getName(),
QMessageBox.Yes | QMessageBox.No)
if ret & QMessageBox.Yes == 0:
return
self.realModel().delTreeItem(self.contextTreeItem)
|