|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2009 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the history model. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtCore import * |
|
11 |
|
12 import Helpviewer.HelpWindow |
|
13 |
|
14 class HistoryModel(QAbstractTableModel): |
|
15 """ |
|
16 Class implementing the history model. |
|
17 """ |
|
18 DateRole = Qt.UserRole + 1 |
|
19 DateTimeRole = Qt.UserRole + 2 |
|
20 UrlRole = Qt.UserRole + 3 |
|
21 UrlStringRole = Qt.UserRole + 4 |
|
22 TitleRole = Qt.UserRole + 5 |
|
23 MaxRole = TitleRole |
|
24 |
|
25 def __init__(self, historyManager, parent = None): |
|
26 """ |
|
27 Constructor |
|
28 |
|
29 @param historyManager reference to the history manager object (HistoryManager) |
|
30 @param parent reference to the parent object (QObject) |
|
31 """ |
|
32 QAbstractTableModel.__init__(self, parent) |
|
33 |
|
34 self.__historyManager = historyManager |
|
35 |
|
36 self.__headers = [ |
|
37 self.trUtf8("Title"), |
|
38 self.trUtf8("Address"), |
|
39 ] |
|
40 |
|
41 self.connect(self.__historyManager, SIGNAL("historyReset()"), |
|
42 self.historyReset) |
|
43 self.connect(self.__historyManager, SIGNAL("entryRemoved"), |
|
44 self.historyReset) |
|
45 self.connect(self.__historyManager, SIGNAL("entryAdded"), |
|
46 self.entryAdded) |
|
47 self.connect(self.__historyManager, SIGNAL("entryUpdated(int)"), |
|
48 self.entryUpdated) |
|
49 |
|
50 def historyReset(self): |
|
51 """ |
|
52 Public slot to reset the model. |
|
53 """ |
|
54 self.reset() |
|
55 |
|
56 def entryAdded(self): |
|
57 """ |
|
58 Public slot to handle the addition of a history entry. |
|
59 """ |
|
60 self.beginInsertRows(QModelIndex(), 0, 0) |
|
61 self.endInsertRows() |
|
62 |
|
63 def entryUpdated(self, row): |
|
64 """ |
|
65 Public slot to handle the update of a history entry. |
|
66 |
|
67 @param row row number of the updated entry (integer) |
|
68 """ |
|
69 idx = self.index(row, 0) |
|
70 self.emit(SIGNAL("dataChanged(const QModelIndex&, const QModelIndex&)"), |
|
71 idx, idx) |
|
72 |
|
73 def headerData(self, section, orientation, role = Qt.DisplayRole): |
|
74 """ |
|
75 Public method to get the header data. |
|
76 |
|
77 @param section section number (integer) |
|
78 @param orientation header orientation (Qt.Orientation) |
|
79 @param role data role (integer) |
|
80 @return header data (QVariant) |
|
81 """ |
|
82 if orientation == Qt.Horizontal and role == Qt.DisplayRole: |
|
83 try: |
|
84 return QVariant(self.__headers[section]) |
|
85 except IndexError: |
|
86 pass |
|
87 return QAbstractTableModel.headerData(self, section, orientation, role) |
|
88 |
|
89 def data(self, index, role = Qt.DisplayRole): |
|
90 """ |
|
91 Public method to get data from the model. |
|
92 |
|
93 @param index index of history entry to get data for (QModelIndex) |
|
94 @param role data role (integer) |
|
95 @return history entry data (QVariant) |
|
96 """ |
|
97 lst = self.__historyManager.history() |
|
98 if index.row() < 0 or index.row() > len(lst): |
|
99 return QVariant() |
|
100 |
|
101 itm = lst[index.row()] |
|
102 if role == self.DateTimeRole: |
|
103 return QVariant(itm.dateTime) |
|
104 elif role == self.DateRole: |
|
105 return QVariant(itm.dateTime.date()) |
|
106 elif role == self.UrlRole: |
|
107 return QVariant(QUrl(itm.url)) |
|
108 elif role == self.UrlStringRole: |
|
109 return QVariant(itm.url) |
|
110 elif role == self.TitleRole: |
|
111 return QVariant(itm.userTitle()) |
|
112 elif role in [Qt.DisplayRole, Qt.EditRole]: |
|
113 if index.column() == 0: |
|
114 return QVariant(itm.userTitle()) |
|
115 elif index.column() == 1: |
|
116 return QVariant(itm.url) |
|
117 elif role == Qt.DecorationRole: |
|
118 if index.column() == 0: |
|
119 return QVariant(Helpviewer.HelpWindow.HelpWindow.icon(QUrl(itm.url))) |
|
120 |
|
121 return QVariant() |
|
122 |
|
123 def columnCount(self, parent = QModelIndex()): |
|
124 """ |
|
125 Public method to get the number of columns. |
|
126 |
|
127 @param parent index of parent (QModelIndex) |
|
128 @return number of columns (integer) |
|
129 """ |
|
130 if parent.isValid(): |
|
131 return 0 |
|
132 else: |
|
133 return len(self.__headers) |
|
134 |
|
135 def rowCount(self, parent = QModelIndex()): |
|
136 """ |
|
137 Public method to determine the number of rows. |
|
138 |
|
139 @param parent index of parent (QModelIndex) |
|
140 @return number of rows (integer) |
|
141 """ |
|
142 if parent.isValid(): |
|
143 return 0 |
|
144 else: |
|
145 return len(self.__historyManager.history()) |
|
146 |
|
147 def removeRows(self, row, count, parent = QModelIndex()): |
|
148 """ |
|
149 Public method to remove history entries from the model. |
|
150 |
|
151 @param row row of the first history entry to remove (integer) |
|
152 @param count number of history entries to remove (integer) |
|
153 @param index of the parent entry (QModelIndex) |
|
154 @return flag indicating successful removal (boolean) |
|
155 """ |
|
156 if parent.isValid(): |
|
157 return False |
|
158 |
|
159 lastRow = row + count - 1 |
|
160 self.beginRemoveRows(parent, row, lastRow) |
|
161 lst = self.__historyManager.history()[:] |
|
162 for index in range(lastRow, row - 1, -1): |
|
163 del lst[index] |
|
164 self.disconnect(self.__historyManager, SIGNAL("historyReset()"), |
|
165 self.historyReset) |
|
166 self.__historyManager.setHistory(lst) |
|
167 self.connect(self.__historyManager, SIGNAL("historyReset()"), |
|
168 self.historyReset) |
|
169 self.endRemoveRows() |
|
170 return True |