eric6/Helpviewer/CookieJar/CookieModel.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 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 self.tr("Contents"),
36 ]
37 self.__cookieJar = cookieJar
38 self.__cookieJar.cookiesChanged.connect(self.__cookiesChanged)
39 self.__cookieJar.load()
40
41 def headerData(self, section, orientation, role):
42 """
43 Public method to get header data from the model.
44
45 @param section section number (integer)
46 @param orientation orientation (Qt.Orientation)
47 @param role role of the data to retrieve (integer)
48 @return requested data
49 """
50 if role == Qt.SizeHintRole:
51 fm = QFontMetrics(QFont())
52 height = fm.height() + fm.height() // 3
53 width = \
54 fm.width(self.headerData(section, orientation, Qt.DisplayRole))
55 return QSize(width, height)
56
57 if orientation == Qt.Horizontal:
58 if role == Qt.DisplayRole:
59 try:
60 return self.__headers[section]
61 except IndexError:
62 return None
63
64 return None
65
66 return QAbstractTableModel.headerData(self, section, orientation, role)
67
68 def data(self, index, role):
69 """
70 Public method to get data from the model.
71
72 @param index index to get data for (QModelIndex)
73 @param role role of the data to retrieve (integer)
74 @return requested data
75 """
76 lst = []
77 if self.__cookieJar is not None:
78 lst = self.__cookieJar.cookies()
79 if index.row() < 0 or index.row() >= len(lst):
80 return None
81
82 if role in (Qt.DisplayRole, Qt.EditRole):
83 cookie = lst[index.row()]
84 col = index.column()
85 if col == 0:
86 return cookie.domain()
87 elif col == 1:
88 return bytes(cookie.name()).decode()
89 elif col == 2:
90 return cookie.path()
91 elif col == 3:
92 return cookie.isSecure()
93 elif col == 4:
94 return cookie.expirationDate()
95 elif col == 5:
96 return cookie.value()
97 else:
98 return None
99
100 return None
101
102 def columnCount(self, parent=None):
103 """
104 Public method to get the number of columns of the model.
105
106 @param parent parent index (QModelIndex)
107 @return number of columns (integer)
108 """
109 if parent is None:
110 parent = QModelIndex()
111
112 if parent.isValid():
113 return 0
114 else:
115 return len(self.__headers)
116
117 def rowCount(self, parent=None):
118 """
119 Public method to get the number of rows of the model.
120
121 @param parent parent index (QModelIndex)
122 @return number of columns (integer)
123 """
124 if parent is None:
125 parent = QModelIndex()
126
127 if parent.isValid() or self.__cookieJar is None:
128 return 0
129 else:
130 return len(self.__cookieJar.cookies())
131
132 def removeRows(self, row, count, parent=None):
133 """
134 Public method to remove entries from the model.
135
136 @param row start row (integer)
137 @param count number of rows to remove (integer)
138 @param parent parent index (QModelIndex)
139 @return flag indicating success (boolean)
140 """
141 if parent is None:
142 parent = QModelIndex()
143
144 if parent.isValid() or self.__cookieJar is None:
145 return False
146
147 lastRow = row + count - 1
148 lst = self.__cookieJar.cookies()
149 del lst[row:lastRow + 1]
150 self.__cookieJar.setCookies(lst)
151
152 return True
153
154 def __cookiesChanged(self):
155 """
156 Private slot handling changes of the cookies list in the cookie jar.
157 """
158 self.beginResetModel()
159 self.endResetModel()

eric ide

mercurial