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