|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2010 - 2016 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=QModelIndex()): |
|
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.isValid(): |
|
56 return 0 |
|
57 else: |
|
58 return self.__manager.count() |
|
59 |
|
60 def removeRows(self, row, count, parent=QModelIndex()): |
|
61 """ |
|
62 Public method to remove bookmarks from the model. |
|
63 |
|
64 @param row row of the first bookmark to remove (integer) |
|
65 @param count number of bookmarks to remove (integer) |
|
66 @param parent index of the parent bookmark node (QModelIndex) |
|
67 @return flag indicating successful removal (boolean) |
|
68 """ |
|
69 if parent.isValid(): |
|
70 return False |
|
71 |
|
72 if row < 0 or count <= 0 or row + count > self.rowCount(parent): |
|
73 return False |
|
74 |
|
75 lastRow = row + count - 1 |
|
76 for i in range(lastRow, row - 1, -1): |
|
77 if not self.__manager.downloads()[i].downloading(): |
|
78 self.beginRemoveRows(parent, i, i) |
|
79 del self.__manager.downloads()[i] |
|
80 self.endRemoveRows() |
|
81 self.__manager.changeOccurred() |
|
82 return True |
|
83 |
|
84 def flags(self, index): |
|
85 """ |
|
86 Public method to get flags for an item. |
|
87 |
|
88 @param index index of the node cell (QModelIndex) |
|
89 @return flags (Qt.ItemFlags) |
|
90 """ |
|
91 if index.row() < 0 or index.row() >= self.rowCount(index.parent()): |
|
92 return Qt.NoItemFlags |
|
93 |
|
94 defaultFlags = QAbstractListModel.flags(self, index) |
|
95 |
|
96 itm = self.__manager.downloads()[index.row()] |
|
97 if itm.downloadedSuccessfully(): |
|
98 return defaultFlags | Qt.ItemIsDragEnabled |
|
99 |
|
100 return defaultFlags |
|
101 |
|
102 def mimeData(self, indexes): |
|
103 """ |
|
104 Public method to return the mime data. |
|
105 |
|
106 @param indexes list of indexes (QModelIndexList) |
|
107 @return mime data (QMimeData) |
|
108 """ |
|
109 mimeData = QMimeData() |
|
110 urls = [] |
|
111 for index in indexes: |
|
112 if index.isValid(): |
|
113 itm = self.__manager.downloads()[index.row()] |
|
114 urls.append(QUrl.fromLocalFile(itm.absoluteFilePath())) |
|
115 mimeData.setUrls(urls) |
|
116 return mimeData |