Helpviewer/CookieJar/CookieModel.py

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

eric ide

mercurial