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 (DownloadManager) |
|
24 @param parent reference to the parent object (QObject) |
|
25 """ |
|
26 super(DownloadModel, self).__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 (QModelIndex) |
|
35 @param role role of the data to retrieve (integer) |
|
36 @return requested data |
|
37 """ |
|
38 if index.row() < 0 or index.row() >= self.rowCount(index.parent()): |
|
39 return None |
|
40 |
|
41 if role == Qt.ToolTipRole: |
|
42 if self.__manager.downloads()[index.row()]\ |
|
43 .downloadedSuccessfully(): |
|
44 return self.__manager.downloads()[index.row()].getInfoData() |
|
45 |
|
46 return None |
|
47 |
|
48 def rowCount(self, parent=None): |
|
49 """ |
|
50 Public method to get the number of rows of the model. |
|
51 |
|
52 @param parent parent index (QModelIndex) |
|
53 @return number of rows (integer) |
|
54 """ |
|
55 if parent is None: |
|
56 parent = QModelIndex() |
|
57 |
|
58 if parent.isValid(): |
|
59 return 0 |
|
60 else: |
|
61 return self.__manager.count() |
|
62 |
|
63 def removeRows(self, row, count, parent=None): |
|
64 """ |
|
65 Public method to remove bookmarks from the model. |
|
66 |
|
67 @param row row of the first bookmark to remove (integer) |
|
68 @param count number of bookmarks to remove (integer) |
|
69 @param parent index of the parent bookmark node (QModelIndex) |
|
70 @return flag indicating successful removal (boolean) |
|
71 """ |
|
72 if parent is None: |
|
73 parent = QModelIndex() |
|
74 |
|
75 if parent.isValid(): |
|
76 return False |
|
77 |
|
78 if row < 0 or count <= 0 or row + count > self.rowCount(parent): |
|
79 return False |
|
80 |
|
81 lastRow = row + count - 1 |
|
82 for i in range(lastRow, row - 1, -1): |
|
83 if not self.__manager.downloads()[i].downloading(): |
|
84 self.beginRemoveRows(parent, i, i) |
|
85 del self.__manager.downloads()[i] |
|
86 self.endRemoveRows() |
|
87 self.__manager.changeOccurred() |
|
88 return True |
|
89 |
|
90 def flags(self, index): |
|
91 """ |
|
92 Public method to get flags for an item. |
|
93 |
|
94 @param index index of the node cell (QModelIndex) |
|
95 @return flags (Qt.ItemFlags) |
|
96 """ |
|
97 if index.row() < 0 or index.row() >= self.rowCount(index.parent()): |
|
98 return Qt.NoItemFlags |
|
99 |
|
100 defaultFlags = QAbstractListModel.flags(self, index) |
|
101 |
|
102 itm = self.__manager.downloads()[index.row()] |
|
103 if itm.downloadedSuccessfully(): |
|
104 return defaultFlags | Qt.ItemIsDragEnabled |
|
105 |
|
106 return defaultFlags |
|
107 |
|
108 def mimeData(self, indexes): |
|
109 """ |
|
110 Public method to return the mime data. |
|
111 |
|
112 @param indexes list of indexes (QModelIndexList) |
|
113 @return mime data (QMimeData) |
|
114 """ |
|
115 mimeData = QMimeData() |
|
116 urls = [] |
|
117 for index in indexes: |
|
118 if index.isValid(): |
|
119 itm = self.__manager.downloads()[index.row()] |
|
120 urls.append(QUrl.fromLocalFile(itm.absoluteFilePath())) |
|
121 mimeData.setUrls(urls) |
|
122 return mimeData |
|