WebBrowser/CookieJar/CookieModel.py

branch
QtWebEngine
changeset 4845
2d22ff71c005
parent 4631
5c1a96925da4
child 4895
3baaf8303a7f
equal deleted inserted replaced
4840:69ee7965ba27 4845:2d22ff71c005
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2009 - 2016 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the cookie model.
8 """
9
10 from __future__ import unicode_literals
11
12 from PyQt5.QtCore import Qt, QAbstractTableModel, QSize, QModelIndex
13 from PyQt5.QtGui import QFont, QFontMetrics
14
15
16 class CookieModel(QAbstractTableModel):
17 """
18 Class implementing the cookie model.
19 """
20 def __init__(self, cookieJar, parent=None):
21 """
22 Constructor
23
24 @param cookieJar reference to the cookie jar (CookieJar)
25 @param parent reference to the parent object (QObject)
26 """
27 super(CookieModel, self).__init__(parent)
28
29 self.__headers = [
30 self.tr("Website"),
31 self.tr("Name"),
32 self.tr("Path"),
33 self.tr("Secure"),
34 self.tr("Expires"),
35 ]
36 self.__cookieJar = cookieJar
37 self.__cookieJar.cookiesChanged.connect(self.__cookiesChanged)
38
39 def headerData(self, section, orientation, role):
40 """
41 Public method to get header data from the model.
42
43 @param section section number (integer)
44 @param orientation orientation (Qt.Orientation)
45 @param role role of the data to retrieve (integer)
46 @return requested data
47 """
48 if role == Qt.SizeHintRole:
49 fm = QFontMetrics(QFont())
50 height = fm.height() + fm.height() // 3
51 width = \
52 fm.width(self.headerData(section, orientation, Qt.DisplayRole))
53 return QSize(width, height)
54
55 if orientation == Qt.Horizontal:
56 if role == Qt.DisplayRole:
57 try:
58 return self.__headers[section]
59 except IndexError:
60 return None
61
62 return None
63
64 return QAbstractTableModel.headerData(self, section, orientation, role)
65
66 def data(self, index, role):
67 """
68 Public method to get data from the model.
69
70 @param index index to get data for (QModelIndex)
71 @param role role of the data to retrieve (integer)
72 @return requested data
73 """
74 lst = []
75 if self.__cookieJar is not None:
76 lst = self.__cookieJar.cookies()
77 if index.row() < 0 or index.row() >= len(lst):
78 return None
79
80 if role in (Qt.DisplayRole, Qt.EditRole):
81 cookie = lst[index.row()]
82 col = index.column()
83 if col == 0:
84 return cookie.domain()
85 elif col == 1:
86 return bytes(cookie.name()).decode()
87 elif col == 2:
88 return cookie.path()
89 elif col == 3:
90 return cookie.isSecure()
91 elif col == 4:
92 return cookie.expirationDate()
93 elif col == 5:
94 return cookie.value()
95 else:
96 return None
97
98 return None
99
100 def columnCount(self, parent=QModelIndex()):
101 """
102 Public method to get the number of columns of the model.
103
104 @param parent parent index (QModelIndex)
105 @return number of columns (integer)
106 """
107 if parent.isValid():
108 return 0
109 else:
110 return len(self.__headers)
111
112 def rowCount(self, parent=QModelIndex()):
113 """
114 Public method to get the number of rows of the model.
115
116 @param parent parent index (QModelIndex)
117 @return number of columns (integer)
118 """
119 if parent.isValid() or self.__cookieJar is None:
120 return 0
121 else:
122 return len(self.__cookieJar.cookies())
123
124 def removeRows(self, row, count, parent=QModelIndex()):
125 """
126 Public method to remove entries from the model.
127
128 @param row start row (integer)
129 @param count number of rows to remove (integer)
130 @param parent parent index (QModelIndex)
131 @return flag indicating success (boolean)
132 """
133 if parent.isValid() or self.__cookieJar is None:
134 return False
135
136 lst = self.__cookieJar.cookies()[row:row + count]
137 self.__cookieJar.removeCookies(lst)
138
139 return True
140
141 def __cookiesChanged(self):
142 """
143 Private slot handling changes of the cookies list in the cookie jar.
144 """
145 self.beginResetModel()
146 self.endResetModel()

eric ide

mercurial