WebBrowser/History/HistoryModel.py

branch
QtWebEngine
changeset 4734
ce0b1f024da9
parent 4631
5c1a96925da4
child 5389
9b1c800daff3
equal deleted inserted replaced
4733:ae291a307ea6 4734:ce0b1f024da9
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2009 - 2016 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 WebBrowser.WebBrowserWindow
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 WebBrowser.WebBrowserWindow.WebBrowserWindow.icon(
120 QUrl(itm.url))
121
122 return None
123
124 def columnCount(self, parent=QModelIndex()):
125 """
126 Public method to get the number of columns.
127
128 @param parent index of parent (QModelIndex)
129 @return number of columns (integer)
130 """
131 if parent.isValid():
132 return 0
133 else:
134 return len(self.__headers)
135
136 def rowCount(self, parent=QModelIndex()):
137 """
138 Public method to determine the number of rows.
139
140 @param parent index of parent (QModelIndex)
141 @return number of rows (integer)
142 """
143 if parent.isValid():
144 return 0
145 else:
146 return len(self.__historyManager.history())
147
148 def removeRows(self, row, count, parent=QModelIndex()):
149 """
150 Public method to remove history entries from the model.
151
152 @param row row of the first history entry to remove (integer)
153 @param count number of history entries to remove (integer)
154 @param parent index of the parent entry (QModelIndex)
155 @return flag indicating successful removal (boolean)
156 """
157 if parent.isValid():
158 return False
159
160 lastRow = row + count - 1
161 self.beginRemoveRows(parent, row, lastRow)
162 lst = self.__historyManager.history()[:]
163 for index in range(lastRow, row - 1, -1):
164 del lst[index]
165 self.__historyManager.historyReset.disconnect(self.historyReset)
166 self.__historyManager.setHistory(lst)
167 self.__historyManager.historyReset.connect(self.historyReset)
168 self.endRemoveRows()
169 return True

eric ide

mercurial