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