12 |
12 |
13 class DownloadModel(QAbstractListModel): |
13 class DownloadModel(QAbstractListModel): |
14 """ |
14 """ |
15 Class implementing the download model. |
15 Class implementing the download model. |
16 """ |
16 """ |
|
17 |
17 def __init__(self, manager, parent=None): |
18 def __init__(self, manager, parent=None): |
18 """ |
19 """ |
19 Constructor |
20 Constructor |
20 |
21 |
21 @param manager reference to the download manager |
22 @param manager reference to the download manager |
22 @type DownloadManager |
23 @type DownloadManager |
23 @param parent reference to the parent object |
24 @param parent reference to the parent object |
24 @type QObject |
25 @type QObject |
25 """ |
26 """ |
26 super().__init__(parent) |
27 super().__init__(parent) |
27 |
28 |
28 self.__manager = manager |
29 self.__manager = manager |
29 |
30 |
30 def data(self, index, role): |
31 def data(self, index, role): |
31 """ |
32 """ |
32 Public method to get data from the model. |
33 Public method to get data from the model. |
33 |
34 |
34 @param index index to get data for |
35 @param index index to get data for |
35 @type QModelIndex |
36 @type QModelIndex |
36 @param role role of the data to retrieve |
37 @param role role of the data to retrieve |
37 @type int |
38 @type int |
38 @return requested data |
39 @return requested data |
39 @rtype any |
40 @rtype any |
40 """ |
41 """ |
41 if index.row() < 0 or index.row() >= self.rowCount(index.parent()): |
42 if index.row() < 0 or index.row() >= self.rowCount(index.parent()): |
42 return None |
43 return None |
43 |
44 |
44 if ( |
45 if ( |
45 role == Qt.ItemDataRole.ToolTipRole and |
46 role == Qt.ItemDataRole.ToolTipRole |
46 self.__manager.downloads()[index.row()].downloadedSuccessfully() |
47 and self.__manager.downloads()[index.row()].downloadedSuccessfully() |
47 ): |
48 ): |
48 return self.__manager.downloads()[index.row()].getInfoData() |
49 return self.__manager.downloads()[index.row()].getInfoData() |
49 |
50 |
50 return None |
51 return None |
51 |
52 |
52 def rowCount(self, parent=None): |
53 def rowCount(self, parent=None): |
53 """ |
54 """ |
54 Public method to get the number of rows of the model. |
55 Public method to get the number of rows of the model. |
55 |
56 |
56 @param parent parent index |
57 @param parent parent index |
57 @type QModelIndex |
58 @type QModelIndex |
58 @return number of rows |
59 @return number of rows |
59 @rtype int |
60 @rtype int |
60 """ |
61 """ |
61 if parent is None: |
62 if parent is None: |
62 parent = QModelIndex() |
63 parent = QModelIndex() |
63 |
64 |
64 if parent.isValid(): |
65 if parent.isValid(): |
65 return 0 |
66 return 0 |
66 else: |
67 else: |
67 return self.__manager.downloadsCount() |
68 return self.__manager.downloadsCount() |
68 |
69 |
69 def removeRows(self, row, count, parent=None): |
70 def removeRows(self, row, count, parent=None): |
70 """ |
71 """ |
71 Public method to remove downloads from the model. |
72 Public method to remove downloads from the model. |
72 |
73 |
73 @param row row of the first download to remove |
74 @param row row of the first download to remove |
74 @type int |
75 @type int |
75 @param count number of downloads to remove |
76 @param count number of downloads to remove |
76 @type int |
77 @type int |
77 @param parent index of the parent download node |
78 @param parent index of the parent download node |
79 @return flag indicating successful removal |
80 @return flag indicating successful removal |
80 @rtype bool |
81 @rtype bool |
81 """ |
82 """ |
82 if parent is None: |
83 if parent is None: |
83 parent = QModelIndex() |
84 parent = QModelIndex() |
84 |
85 |
85 if parent.isValid(): |
86 if parent.isValid(): |
86 return False |
87 return False |
87 |
88 |
88 if row < 0 or count <= 0 or row + count > self.rowCount(parent): |
89 if row < 0 or count <= 0 or row + count > self.rowCount(parent): |
89 return False |
90 return False |
90 |
91 |
91 lastRow = row + count - 1 |
92 lastRow = row + count - 1 |
92 for i in range(lastRow, row - 1, -1): |
93 for i in range(lastRow, row - 1, -1): |
93 if not self.__manager.downloads()[i].downloading(): |
94 if not self.__manager.downloads()[i].downloading(): |
94 self.beginRemoveRows(parent, i, i) |
95 self.beginRemoveRows(parent, i, i) |
95 del self.__manager.downloads()[i] |
96 del self.__manager.downloads()[i] |
96 self.endRemoveRows() |
97 self.endRemoveRows() |
97 self.__manager.changeOccurred() |
98 self.__manager.changeOccurred() |
98 return True |
99 return True |
99 |
100 |
100 def flags(self, index): |
101 def flags(self, index): |
101 """ |
102 """ |
102 Public method to get flags for an item. |
103 Public method to get flags for an item. |
103 |
104 |
104 @param index index of the node cell |
105 @param index index of the node cell |
105 @type QModelIndex |
106 @type QModelIndex |
106 @return flags |
107 @return flags |
107 @rtype Qt.ItemFlags |
108 @rtype Qt.ItemFlags |
108 """ |
109 """ |
109 if index.row() < 0 or index.row() >= self.rowCount(index.parent()): |
110 if index.row() < 0 or index.row() >= self.rowCount(index.parent()): |
110 return Qt.ItemFlag.NoItemFlags |
111 return Qt.ItemFlag.NoItemFlags |
111 |
112 |
112 defaultFlags = QAbstractListModel.flags(self, index) |
113 defaultFlags = QAbstractListModel.flags(self, index) |
113 |
114 |
114 itm = self.__manager.downloads()[index.row()] |
115 itm = self.__manager.downloads()[index.row()] |
115 if itm.downloadedSuccessfully(): |
116 if itm.downloadedSuccessfully(): |
116 return defaultFlags | Qt.ItemFlag.ItemIsDragEnabled |
117 return defaultFlags | Qt.ItemFlag.ItemIsDragEnabled |
117 |
118 |
118 return defaultFlags |
119 return defaultFlags |
119 |
120 |
120 def mimeData(self, indexes): |
121 def mimeData(self, indexes): |
121 """ |
122 """ |
122 Public method to return the mime data. |
123 Public method to return the mime data. |
123 |
124 |
124 @param indexes list of indexes |
125 @param indexes list of indexes |
125 @type QModelIndexList |
126 @type QModelIndexList |
126 @return mime data |
127 @return mime data |
127 @rtype QMimeData |
128 @rtype QMimeData |
128 """ |
129 """ |