MCPcopy Create free account
hub / github.com/KDAB/KDChart / MyTaskModel

Class MyTaskModel

python/examples/reorder/main.py:25–79  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

23## Test model that uses beginMoveRows() ##
24# pylint: disable=too-few-public-methods
25class MyTaskModel(QAbstractTableModel):
26 class Task():
27 def __init__(self):
28 self.title = ""
29 self.start = QDateTime()
30 self.end = QDateTime()
31
32 def __init__(self, parent=None):
33 super().__init__(parent)
34 self.tasks = []
35
36 for i in range(0, 6, 2):
37 task = self.Task()
38 task.title = "Item " + str(len(self.tasks) + 1)
39 task.start = QDateTime.currentDateTime().addDays(i)
40 task.end = task.start.addDays(1)
41 self.tasks.append(task)
42
43 def rowCount(self, index=QModelIndex()):
44 if index.isValid():
45 return 0
46 return len(self.tasks)
47
48 def columnCount(self, index=QModelIndex()): # pylint: disable=no-self-use
49 if index.isValid():
50 return 0
51 return 4
52
53 def data(self, index, role=Qt.DisplayRole):
54 if index.isValid() and index.row() < self.rowCount() and index.column() < self.columnCount():
55 column = index.column()
56 row = index.row()
57 if column == 0 and role == Qt.DisplayRole:
58 return self.tasks[row].title
59 if column == 1 and role == Qt.DisplayRole:
60 return KDGantt.TypeTask
61 if column == 2 and (role in [KDGantt.StartTimeRole, Qt.DisplayRole]):
62 return self.tasks[row].start
63 if column == 3 and (role in [KDGantt.EndTimeRole, Qt.DisplayRole]):
64 return self.tasks[row].end
65
66 return None
67
68 def moveRow(self, fromIndex, toIndex):
69 if fromIndex == toIndex:
70 return
71
72 if fromIndex >= len(self.tasks) or toIndex >= len(self.tasks)+1:
73 return
74
75 if self.beginMoveRows(QModelIndex(), fromIndex, fromIndex, QModelIndex(), toIndex):
76 self.tasks.insert(toIndex, self.tasks.pop(fromIndex))
77 self.endMoveRows()
78 else:
79 return
80
81
82if __name__ == '__main__':

Callers 1

main.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected