src/eric7/WebBrowser/History/HistoryModel.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9413
80c06d472826
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
16 16
17 class HistoryModel(QAbstractTableModel): 17 class HistoryModel(QAbstractTableModel):
18 """ 18 """
19 Class implementing the history model. 19 Class implementing the history model.
20 """ 20 """
21
21 DateRole = Qt.ItemDataRole.UserRole + 1 22 DateRole = Qt.ItemDataRole.UserRole + 1
22 DateTimeRole = Qt.ItemDataRole.UserRole + 2 23 DateTimeRole = Qt.ItemDataRole.UserRole + 2
23 UrlRole = Qt.ItemDataRole.UserRole + 3 24 UrlRole = Qt.ItemDataRole.UserRole + 3
24 UrlStringRole = Qt.ItemDataRole.UserRole + 4 25 UrlStringRole = Qt.ItemDataRole.UserRole + 4
25 TitleRole = Qt.ItemDataRole.UserRole + 5 26 TitleRole = Qt.ItemDataRole.UserRole + 5
26 VisitCountRole = Qt.ItemDataRole.UserRole + 6 27 VisitCountRole = Qt.ItemDataRole.UserRole + 6
27 MaxRole = VisitCountRole 28 MaxRole = VisitCountRole
28 29
29 def __init__(self, historyManager, parent=None): 30 def __init__(self, historyManager, parent=None):
30 """ 31 """
31 Constructor 32 Constructor
32 33
33 @param historyManager reference to the history manager object 34 @param historyManager reference to the history manager object
34 (HistoryManager) 35 (HistoryManager)
35 @param parent reference to the parent object (QObject) 36 @param parent reference to the parent object (QObject)
36 """ 37 """
37 super().__init__(parent) 38 super().__init__(parent)
38 39
39 self.__historyManager = historyManager 40 self.__historyManager = historyManager
40 41
41 self.__headers = [ 42 self.__headers = [self.tr("Title"), self.tr("Address"), self.tr("Visit Count")]
42 self.tr("Title"), 43
43 self.tr("Address"),
44 self.tr("Visit Count")
45 ]
46
47 self.__historyManager.historyReset.connect(self.historyReset) 44 self.__historyManager.historyReset.connect(self.historyReset)
48 self.__historyManager.entryRemoved.connect(self.historyReset) 45 self.__historyManager.entryRemoved.connect(self.historyReset)
49 self.__historyManager.entryAdded.connect(self.entryAdded) 46 self.__historyManager.entryAdded.connect(self.entryAdded)
50 self.__historyManager.entryUpdated.connect(self.entryUpdated) 47 self.__historyManager.entryUpdated.connect(self.entryUpdated)
51 48
52 def historyReset(self): 49 def historyReset(self):
53 """ 50 """
54 Public slot to reset the model. 51 Public slot to reset the model.
55 """ 52 """
56 self.beginResetModel() 53 self.beginResetModel()
57 self.endResetModel() 54 self.endResetModel()
58 55
59 def entryAdded(self): 56 def entryAdded(self):
60 """ 57 """
61 Public slot to handle the addition of a history entry. 58 Public slot to handle the addition of a history entry.
62 """ 59 """
63 self.beginInsertRows(QModelIndex(), 0, 0) 60 self.beginInsertRows(QModelIndex(), 0, 0)
64 self.endInsertRows() 61 self.endInsertRows()
65 62
66 def entryUpdated(self, row): 63 def entryUpdated(self, row):
67 """ 64 """
68 Public slot to handle the update of a history entry. 65 Public slot to handle the update of a history entry.
69 66
70 @param row row number of the updated entry (integer) 67 @param row row number of the updated entry (integer)
71 """ 68 """
72 idx = self.index(row, 0) 69 idx = self.index(row, 0)
73 self.dataChanged.emit(idx, idx) 70 self.dataChanged.emit(idx, idx)
74 71
75 def headerData(self, section, orientation, 72 def headerData(self, section, orientation, role=Qt.ItemDataRole.DisplayRole):
76 role=Qt.ItemDataRole.DisplayRole):
77 """ 73 """
78 Public method to get the header data. 74 Public method to get the header data.
79 75
80 @param section section number (integer) 76 @param section section number (integer)
81 @param orientation header orientation (Qt.Orientation) 77 @param orientation header orientation (Qt.Orientation)
82 @param role data role (Qt.ItemDataRole) 78 @param role data role (Qt.ItemDataRole)
83 @return header data 79 @return header data
84 """ 80 """
85 if ( 81 if (
86 orientation == Qt.Orientation.Horizontal and 82 orientation == Qt.Orientation.Horizontal
87 role == Qt.ItemDataRole.DisplayRole 83 and role == Qt.ItemDataRole.DisplayRole
88 ): 84 ):
89 with contextlib.suppress(IndexError): 85 with contextlib.suppress(IndexError):
90 return self.__headers[section] 86 return self.__headers[section]
91 return QAbstractTableModel.headerData(self, section, orientation, role) 87 return QAbstractTableModel.headerData(self, section, orientation, role)
92 88
93 def data(self, index, role=Qt.ItemDataRole.DisplayRole): 89 def data(self, index, role=Qt.ItemDataRole.DisplayRole):
94 """ 90 """
95 Public method to get data from the model. 91 Public method to get data from the model.
96 92
97 @param index index of history entry to get data for (QModelIndex) 93 @param index index of history entry to get data for (QModelIndex)
98 @param role data role (integer) 94 @param role data role (integer)
99 @return history entry data 95 @return history entry data
100 """ 96 """
101 lst = self.__historyManager.history() 97 lst = self.__historyManager.history()
102 if index.row() < 0 or index.row() > len(lst): 98 if index.row() < 0 or index.row() > len(lst):
103 return None 99 return None
104 100
105 itm = lst[index.row()] 101 itm = lst[index.row()]
106 if role == self.DateTimeRole: 102 if role == self.DateTimeRole:
107 return itm.dateTime 103 return itm.dateTime
108 elif role == self.DateRole: 104 elif role == self.DateRole:
109 return itm.dateTime.date() 105 return itm.dateTime.date()
120 return itm.userTitle() 116 return itm.userTitle()
121 elif index.column() == 1: 117 elif index.column() == 1:
122 return itm.url 118 return itm.url
123 elif index.column() == 2: 119 elif index.column() == 2:
124 return itm.visitCount 120 return itm.visitCount
125 elif ( 121 elif role == Qt.ItemDataRole.DecorationRole and index.column() == 0:
126 role == Qt.ItemDataRole.DecorationRole and 122 return WebBrowser.WebBrowserWindow.WebBrowserWindow.icon(QUrl(itm.url))
127 index.column() == 0 123
128 ):
129 return WebBrowser.WebBrowserWindow.WebBrowserWindow.icon(
130 QUrl(itm.url))
131
132 return None 124 return None
133 125
134 def columnCount(self, parent=None): 126 def columnCount(self, parent=None):
135 """ 127 """
136 Public method to get the number of columns. 128 Public method to get the number of columns.
137 129
138 @param parent index of parent (QModelIndex) 130 @param parent index of parent (QModelIndex)
139 @return number of columns (integer) 131 @return number of columns (integer)
140 """ 132 """
141 if parent is None: 133 if parent is None:
142 parent = QModelIndex() 134 parent = QModelIndex()
143 135
144 if parent.isValid(): 136 if parent.isValid():
145 return 0 137 return 0
146 else: 138 else:
147 return len(self.__headers) 139 return len(self.__headers)
148 140
149 def rowCount(self, parent=None): 141 def rowCount(self, parent=None):
150 """ 142 """
151 Public method to determine the number of rows. 143 Public method to determine the number of rows.
152 144
153 @param parent index of parent (QModelIndex) 145 @param parent index of parent (QModelIndex)
154 @return number of rows (integer) 146 @return number of rows (integer)
155 """ 147 """
156 if parent is None: 148 if parent is None:
157 parent = QModelIndex() 149 parent = QModelIndex()
158 150
159 if parent.isValid(): 151 if parent.isValid():
160 return 0 152 return 0
161 else: 153 else:
162 return len(self.__historyManager.history()) 154 return len(self.__historyManager.history())
163 155
164 def removeRows(self, row, count, parent=None): 156 def removeRows(self, row, count, parent=None):
165 """ 157 """
166 Public method to remove history entries from the model. 158 Public method to remove history entries from the model.
167 159
168 @param row row of the first history entry to remove (integer) 160 @param row row of the first history entry to remove (integer)
169 @param count number of history entries to remove (integer) 161 @param count number of history entries to remove (integer)
170 @param parent index of the parent entry (QModelIndex) 162 @param parent index of the parent entry (QModelIndex)
171 @return flag indicating successful removal (boolean) 163 @return flag indicating successful removal (boolean)
172 """ 164 """
173 if parent is None: 165 if parent is None:
174 parent = QModelIndex() 166 parent = QModelIndex()
175 167
176 if parent.isValid(): 168 if parent.isValid():
177 return False 169 return False
178 170
179 lastRow = row + count - 1 171 lastRow = row + count - 1
180 self.beginRemoveRows(parent, row, lastRow) 172 self.beginRemoveRows(parent, row, lastRow)
181 lst = self.__historyManager.history()[:] 173 lst = self.__historyManager.history()[:]
182 for index in range(lastRow, row - 1, -1): 174 for index in range(lastRow, row - 1, -1):
183 del lst[index] 175 del lst[index]

eric ide

mercurial