Helpviewer/OpenSearch/OpenSearchEngineModel.py

changeset 7
c679fb30c8f3
parent 0
de9c2efb9d02
child 13
1af94a91f439
equal deleted inserted replaced
6:52e8c820d0dd 7:c679fb30c8f3
101 """ 101 """
102 Public method to get data from the model. 102 Public method to get data from the model.
103 103
104 @param index index to get data for (QModelIndex) 104 @param index index to get data for (QModelIndex)
105 @param role role of the data to retrieve (integer) 105 @param role role of the data to retrieve (integer)
106 @return requested data (QVariant) 106 @return requested data
107 """ 107 """
108 if index.row() >= self.__manager.enginesCount() or index.row() < 0: 108 if index.row() >= self.__manager.enginesCount() or index.row() < 0:
109 return QVariant() 109 return None
110 110
111 engine = self.__manager.engine(self.__manager.allEnginesNames()[index.row()]) 111 engine = self.__manager.engine(self.__manager.allEnginesNames()[index.row()])
112 112
113 if engine is None: 113 if engine is None:
114 return QVariant() 114 return None
115 115
116 if index.column() == 0: 116 if index.column() == 0:
117 if role == Qt.DisplayRole: 117 if role == Qt.DisplayRole:
118 return QVariant(engine.name()) 118 return engine.name()
119 119
120 elif role == Qt.DecorationRole: 120 elif role == Qt.DecorationRole:
121 image = engine.image() 121 image = engine.image()
122 if image.isNull(): 122 if image.isNull():
123 from Helpviewer.HelpWindow import HelpWindow 123 from Helpviewer.HelpWindow import HelpWindow
124 icon = HelpWindow.icon(QUrl(engine.imageUrl())) 124 icon = HelpWindow.icon(QUrl(engine.imageUrl()))
125 else: 125 else:
126 icon = QIcon(QPixmap.fromImage(image)) 126 icon = QIcon(QPixmap.fromImage(image))
127 return QVariant(icon) 127 return icon
128 128
129 elif role == Qt.ToolTipRole: 129 elif role == Qt.ToolTipRole:
130 description = self.trUtf8("<strong>Description:</strong> {0}")\ 130 description = self.trUtf8("<strong>Description:</strong> {0}")\
131 .format(engine.description()) 131 .format(engine.description())
132 if engine.providesSuggestions(): 132 if engine.providesSuggestions():
133 description.append("<br/>") 133 description.append("<br/>")
134 description.append( 134 description.append(
135 self.trUtf8("<strong>Provides contextual suggestions</strong>")) 135 self.trUtf8("<strong>Provides contextual suggestions</strong>"))
136 136
137 return QVariant(description) 137 return description
138 elif index.column() == 1: 138 elif index.column() == 1:
139 if role in [Qt.EditRole, Qt.DisplayRole]: 139 if role in [Qt.EditRole, Qt.DisplayRole]:
140 return QVariant( 140 return ",".join(self.__manager.keywordsForEngine(engine))
141 ",".join(self.__manager.keywordsForEngine(engine)))
142 elif role == Qt.ToolTipRole: 141 elif role == Qt.ToolTipRole:
143 return QVariant(self.trUtf8("Comma-separated list of keywords that may" 142 return self.trUtf8("Comma-separated list of keywords that may"
144 " be entered in the location bar followed by search terms to search" 143 " be entered in the location bar followed by search terms to search"
145 " with this engine")) 144 " with this engine")
146 145
147 return QVariant() 146 return None
148 147
149 def setData(self, index, value, role = Qt.EditRole): 148 def setData(self, index, value, role = Qt.EditRole):
150 """ 149 """
151 Public method to set the data of a model cell. 150 Public method to set the data of a model cell.
152 151
153 @param index index of the model cell (QModelIndex) 152 @param index index of the model cell (QModelIndex)
154 @param value value to be set (QVariant) 153 @param value value to be set
155 @param role role of the data (integer) 154 @param role role of the data (integer)
156 @return flag indicating success (boolean) 155 @return flag indicating success (boolean)
157 """ 156 """
158 if not index.isValid() or index.column() != 1: 157 if not index.isValid() or index.column() != 1:
159 return False 158 return False
163 162
164 if role != Qt.EditRole: 163 if role != Qt.EditRole:
165 return False 164 return False
166 165
167 engineName = self.__manager.allEnginesNames()[index.row()] 166 engineName = self.__manager.allEnginesNames()[index.row()]
168 keywords = re.split("[ ,]+", value.toString()) 167 keywords = re.split("[ ,]+", value)
169 self.__manager.setKeywordsForEngine(self.__manager.engine(engineName), keywords) 168 self.__manager.setKeywordsForEngine(self.__manager.engine(engineName), keywords)
170 169
171 return True 170 return True
172 171
173 def headerData(self, section, orientation, role = Qt.DisplayRole): 172 def headerData(self, section, orientation, role = Qt.DisplayRole):
175 Public method to get the header data. 174 Public method to get the header data.
176 175
177 @param section section number (integer) 176 @param section section number (integer)
178 @param orientation header orientation (Qt.Orientation) 177 @param orientation header orientation (Qt.Orientation)
179 @param role data role (integer) 178 @param role data role (integer)
180 @return header data (QVariant) 179 @return header data
181 """ 180 """
182 if orientation == Qt.Horizontal and role == Qt.DisplayRole: 181 if orientation == Qt.Horizontal and role == Qt.DisplayRole:
183 try: 182 try:
184 return QVariant(self.__headers[section]) 183 return self.__headers[section]
185 except IndexError: 184 except IndexError:
186 pass 185 pass
187 186
188 return QVariant() 187 return None
189 188
190 def __enginesChanged(self): 189 def __enginesChanged(self):
191 """ 190 """
192 Private slot handling a change of the registered engines. 191 Private slot handling a change of the registered engines.
193 """ 192 """

eric ide

mercurial