Helpviewer/OpenSearch/OpenSearchEngineModel.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 a model for search engines.
8 """
9
10 import re
11
12 from PyQt4.QtCore import *
13 from PyQt4.QtGui import QPixmap, QIcon
14
15 class OpenSearchEngineModel(QAbstractTableModel):
16 """
17 Class implementing a model for search engines.
18 """
19 def __init__(self, manager, parent = None):
20 """
21 Constructor
22
23 @param manager reference to the search engine manager (OpenSearchManager)
24 @param parent reference to the parent object (QObject)
25 """
26 QAbstractTableModel.__init__(self, parent)
27
28 self.__manager = manager
29 self.connect(manager, SIGNAL("changed()"), self.__enginesChanged)
30
31 self.__headers = [
32 self.trUtf8("Name"),
33 self.trUtf8("Keywords"),
34 ]
35
36 def removeRows(self, row, count, parent = QModelIndex()):
37 """
38 Public method to remove entries from the model.
39
40 @param row start row (integer)
41 @param count number of rows to remove (integer)
42 @param parent parent index (QModelIndex)
43 @return flag indicating success (boolean)
44 """
45 if parent.isValid():
46 return False
47
48 if count <= 0:
49 return False
50
51 if self.rowCount() <= 1:
52 return False
53
54 lastRow = row + count - 1
55
56 self.beginRemoveRows(parent, row, lastRow)
57
58 nameList = self.__manager.allEnginesNames()
59 for index in range(row, lastRow + 1):
60 self.__manager.removeEngine(nameList[index])
61
62 # removeEngine emits changed()
63 #self.endRemoveRows()
64
65 return True
66
67 def rowCount(self, parent = QModelIndex()):
68 """
69 Public method to get the number of rows of the model.
70
71 @param parent parent index (QModelIndex)
72 @return number of rows (integer)
73 """
74 if parent.isValid():
75 return 0
76 else:
77 return self.__manager.enginesCount()
78
79 def columnCount(self, parent = QModelIndex()):
80 """
81 Public method to get the number of columns of the model.
82
83 @param parent parent index (QModelIndex)
84 @return number of columns (integer)
85 """
86 return 2
87
88 def flags(self, index):
89 """
90 Public method to get flags for a model cell.
91
92 @param index index of the model cell (QModelIndex)
93 @return flags (Qt.ItemFlags)
94 """
95 if index.column() == 1:
96 return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsEditable
97 else:
98 return Qt.ItemIsEnabled | Qt.ItemIsSelectable
99
100 def data(self, index, role):
101 """
102 Public method to get data from the model.
103
104 @param index index to get data for (QModelIndex)
105 @param role role of the data to retrieve (integer)
106 @return requested data (QVariant)
107 """
108 if index.row() >= self.__manager.enginesCount() or index.row() < 0:
109 return QVariant()
110
111 engine = self.__manager.engine(self.__manager.allEnginesNames()[index.row()])
112
113 if engine is None:
114 return QVariant()
115
116 if index.column() == 0:
117 if role == Qt.DisplayRole:
118 return QVariant(engine.name())
119
120 elif role == Qt.DecorationRole:
121 image = engine.image()
122 if image.isNull():
123 from Helpviewer.HelpWindow import HelpWindow
124 icon = HelpWindow.icon(QUrl(engine.imageUrl()))
125 else:
126 icon = QIcon(QPixmap.fromImage(image))
127 return QVariant(icon)
128
129 elif role == Qt.ToolTipRole:
130 description = self.trUtf8("<strong>Description:</strong> {0}")\
131 .format(engine.description())
132 if engine.providesSuggestions():
133 description.append("<br/>")
134 description.append(
135 self.trUtf8("<strong>Provides contextual suggestions</strong>"))
136
137 return QVariant(description)
138 elif index.column() == 1:
139 if role in [Qt.EditRole, Qt.DisplayRole]:
140 return QVariant(
141 ",".join(self.__manager.keywordsForEngine(engine)))
142 elif role == Qt.ToolTipRole:
143 return QVariant(self.trUtf8("Comma-separated list of keywords that may"
144 " be entered in the location bar followed by search terms to search"
145 " with this engine"))
146
147 return QVariant()
148
149 def setData(self, index, value, role = Qt.EditRole):
150 """
151 Public method to set the data of a model cell.
152
153 @param index index of the model cell (QModelIndex)
154 @param value value to be set (QVariant)
155 @param role role of the data (integer)
156 @return flag indicating success (boolean)
157 """
158 if not index.isValid() or index.column() != 1:
159 return False
160
161 if index.row() >= self.rowCount() or index.row() < 0:
162 return False
163
164 if role != Qt.EditRole:
165 return False
166
167 engineName = self.__manager.allEnginesNames()[index.row()]
168 keywords = re.split("[ ,]+", value.toString())
169 self.__manager.setKeywordsForEngine(self.__manager.engine(engineName), keywords)
170
171 return True
172
173 def headerData(self, section, orientation, role = Qt.DisplayRole):
174 """
175 Public method to get the header data.
176
177 @param section section number (integer)
178 @param orientation header orientation (Qt.Orientation)
179 @param role data role (integer)
180 @return header data (QVariant)
181 """
182 if orientation == Qt.Horizontal and role == Qt.DisplayRole:
183 try:
184 return QVariant(self.__headers[section])
185 except IndexError:
186 pass
187
188 return QVariant()
189
190 def __enginesChanged(self):
191 """
192 Private slot handling a change of the registered engines.
193 """
194 QAbstractTableModel.reset(self)

eric ide

mercurial