eric6/Helpviewer/History/HistoryModel.py

branch
maintenance
changeset 7286
7eb04391adf7
parent 7226
babe80d84a3e
parent 7285
1ff497f33f31
child 7287
1c17f2191bdd
equal deleted inserted replaced
7226:babe80d84a3e 7286:7eb04391adf7
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2009 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the history model.
8 """
9
10 from __future__ import unicode_literals
11
12 from PyQt5.QtCore import Qt, QAbstractTableModel, QModelIndex, QUrl
13
14 import Helpviewer.HelpWindow
15
16
17 class HistoryModel(QAbstractTableModel):
18 """
19 Class implementing the history model.
20 """
21 DateRole = Qt.UserRole + 1
22 DateTimeRole = Qt.UserRole + 2
23 UrlRole = Qt.UserRole + 3
24 UrlStringRole = Qt.UserRole + 4
25 TitleRole = Qt.UserRole + 5
26 MaxRole = TitleRole
27
28 def __init__(self, historyManager, parent=None):
29 """
30 Constructor
31
32 @param historyManager reference to the history manager object
33 (HistoryManager)
34 @param parent reference to the parent object (QObject)
35 """
36 super(HistoryModel, self).__init__(parent)
37
38 self.__historyManager = historyManager
39
40 self.__headers = [
41 self.tr("Title"),
42 self.tr("Address"),
43 ]
44
45 self.__historyManager.historyReset.connect(self.historyReset)
46 self.__historyManager.entryRemoved.connect(self.historyReset)
47 self.__historyManager.entryAdded.connect(self.entryAdded)
48 self.__historyManager.entryUpdated.connect(self.entryUpdated)
49
50 def historyReset(self):
51 """
52 Public slot to reset the model.
53 """
54 self.beginResetModel()
55 self.endResetModel()
56
57 def entryAdded(self):
58 """
59 Public slot to handle the addition of a history entry.
60 """
61 self.beginInsertRows(QModelIndex(), 0, 0)
62 self.endInsertRows()
63
64 def entryUpdated(self, row):
65 """
66 Public slot to handle the update of a history entry.
67
68 @param row row number of the updated entry (integer)
69 """
70 idx = self.index(row, 0)
71 self.dataChanged.emit(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
81 """
82 if orientation == Qt.Horizontal and role == Qt.DisplayRole:
83 try:
84 return 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
96 """
97 lst = self.__historyManager.history()
98 if index.row() < 0 or index.row() > len(lst):
99 return None
100
101 itm = lst[index.row()]
102 if role == self.DateTimeRole:
103 return itm.dateTime
104 elif role == self.DateRole:
105 return itm.dateTime.date()
106 elif role == self.UrlRole:
107 return QUrl(itm.url)
108 elif role == self.UrlStringRole:
109 return itm.url
110 elif role == self.TitleRole:
111 return itm.userTitle()
112 elif role in [Qt.DisplayRole, Qt.EditRole]:
113 if index.column() == 0:
114 return itm.userTitle()
115 elif index.column() == 1:
116 return itm.url
117 elif role == Qt.DecorationRole:
118 if index.column() == 0:
119 return Helpviewer.HelpWindow.HelpWindow.icon(QUrl(itm.url))
120
121 return None
122
123 def columnCount(self, parent=None):
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 is None:
131 parent = QModelIndex()
132
133 if parent.isValid():
134 return 0
135 else:
136 return len(self.__headers)
137
138 def rowCount(self, parent=None):
139 """
140 Public method to determine the number of rows.
141
142 @param parent index of parent (QModelIndex)
143 @return number of rows (integer)
144 """
145 if parent is None:
146 parent = QModelIndex()
147
148 if parent.isValid():
149 return 0
150 else:
151 return len(self.__historyManager.history())
152
153 def removeRows(self, row, count, parent=None):
154 """
155 Public method to remove history entries from the model.
156
157 @param row row of the first history entry to remove (integer)
158 @param count number of history entries to remove (integer)
159 @param parent index of the parent entry (QModelIndex)
160 @return flag indicating successful removal (boolean)
161 """
162 if parent is None:
163 parent = QModelIndex()
164
165 if parent.isValid():
166 return False
167
168 lastRow = row + count - 1
169 self.beginRemoveRows(parent, row, lastRow)
170 lst = self.__historyManager.history()[:]
171 for index in range(lastRow, row - 1, -1):
172 del lst[index]
173 self.__historyManager.historyReset.disconnect(self.historyReset)
174 self.__historyManager.setHistory(lst)
175 self.__historyManager.historyReset.connect(self.historyReset)
176 self.endRemoveRows()
177 return True

eric ide

mercurial