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