| 111 | | |
| 112 | | |
| 113 | | class ComboBoxEditorDelegate(ComboBoxDelegate): |
| 114 | | """Delegate for combobox which makes sure that the editor (of the combobox) |
| 115 | | is visible even if it isn't selected""" |
| 116 | | |
| 117 | | def __init__(self, choices, parent=None, editable=True, **kwargs): |
| 118 | | ComboBoxDelegate.__init__(self, parent, choices, editable, **kwargs) |
| 119 | | |
| 120 | | def setEditorData(self, editor, index): |
| 121 | | value = variant_to_pyobject(index.data(Qt.EditRole)) |
| 122 | | |
| 123 | | def create_choices_getter(model, row): |
| 124 | | |
| 125 | | def choices_getter(): |
| 126 | | print 'in set editor data', self.choices(None) |
| 127 | | return list(self.choices(None)) |
| 128 | | |
| 129 | | return choices_getter |
| 130 | | |
| 131 | | #editor.set_value(value) |
| 132 | | post(create_choices_getter(index.model(),index.row()), |
| 133 | | editor.set_choices) |
| 134 | | |
| 135 | | def setModelData(self, editor, model, index): |
| 136 | | return None |
| 137 | | #model.setData(index, create_constant_function(editor.get_value())) |
| 138 | | |
| 139 | | def paint(self, painter, option, index): |
| 140 | | """adapted from booldelegate""" |
| 141 | | |
| 142 | | painter.save() |
| 143 | | self.drawBackground(painter, option, index) |
| 144 | | #checked = index.model().data(index, Qt.EditRole).toString().__str__() |
| 145 | | value = index.model().data(index, Qt.DisplayRole).toString().__str__() |
| 146 | | #c = index.model().data(index, Qt.BackgroundRole) |
| 147 | | #background_color = QtGui.QColor(c) |
| 148 | | background_color = QtGui.QColor(Qt.blue) |
| 149 | | |
| 150 | | #check_option = QtGui.QStyleOptionComboBox() |
| 151 | | #check_option = QtGui.QStyleOptionButton() |
| 152 | | check_option = QtGui.QStyleOptionMenuItem() |
| 153 | | #check_option.OptionType = QStyleOption.SO_ComboBox |
| 154 | | |
| 155 | | #check_option.text = QString('choice') |
| 156 | | |
| 157 | | class ComboBoxEditorDelegate(ComboBoxDelegate): |
| 158 | | |
| 159 | | editor = editors.ChoicesEditor |
| 160 | | |
| 161 | | """ |
| 162 | | Delegate for combobox which makes sure that the editor (of the combobox) is visible |
| 163 | | even if it isn't selected |
| 164 | | """ |
| 165 | | def __init__(self, choices, parent=None, editable=True, **kwargs): |
| 166 | | ComboBoxDelegate.__init__(self, parent, choices, editable, **kwargs) |
| 167 | | |
| 168 | | def setEditorData(self, editor, index): |
| 169 | | value = variant_to_pyobject(index.data(Qt.EditRole)) |
| 170 | | |
| 171 | | def create_choices_getter(model, row): |
| 172 | | |
| 173 | | def choices_getter(): |
| 174 | | |
| 175 | | return list(self.choices(None)) |
| 176 | | |
| 177 | | return choices_getter |
| 178 | | |
| 179 | | #editor.set_value(value) |
| 180 | | post(create_choices_getter(index.model(),index.row()), editor.set_choices) |
| 181 | | |
| 182 | | def setModelData(self, editor, model, index): |
| 183 | | return None |
| 184 | | #model.setData(index, create_constant_function(editor.get_value())) |
| 185 | | |
| 186 | | """ adapted from booldelegate """ |
| 187 | | def paint(self, painter, option, index): |
| 188 | | painter.save() |
| 189 | | self.drawBackground(painter, option, index) |
| 190 | | #checked = index.model().data(index, Qt.EditRole).toString().__str__() |
| 191 | | value = index.model().data(index, Qt.DisplayRole).toString().__str__() |
| 192 | | print "index value in delegate", value |
| 193 | | |
| 194 | | #background_color = QtGui.QColor(index.model().data(index, Qt.BackgroundRole)) |
| 195 | | background_color = QtGui.QColor(Qt.blue) |
| 196 | | |
| 197 | | #check_option = QtGui.QStyleOptionComboBox() |
| 198 | | #check_option = QtGui.QStyleOptionButton() |
| 199 | | check_option = QtGui.QStyleOptionMenuItem() |
| 200 | | #check_option.OptionType = QStyleOption.SO_ComboBox |
| 201 | | |
| 202 | | #check_option.text = QString('choice') |
| 203 | | |
| 204 | | rect = QtCore.QRect(option.rect.left(), |
| 205 | | option.rect.top(), |
| 206 | | option.rect.width(), |
| 207 | | option.rect.height()) |
| 208 | | |
| 209 | | #check_option.rect = rect |
| 210 | | check_option.palette = option.palette |
| 211 | | |
| 212 | | #if (option.state & QtGui.QStyle.State_Selected): |
| 213 | | # painter.fillRect(option.rect, option.palette.highlight()) |
| 214 | | #elif not self.editable: |
| 215 | | # painter.fillRect(option.rect, option.palette.window()) |
| 216 | | #else: |
| 217 | | painter.fillRect(option.rect, background_color) |
| 218 | | |
| 219 | | #if checked: |
| 220 | | #check_option.state = option.state | QtGui.QStyle.State_On |
| 221 | | #check_option.state = QtGui.QStyle.State_On |
| 222 | | #else: |
| 223 | | #check_option.state = option.state | QtGui.QStyle.State_Off |
| 224 | | #check_option.state = QtGui.QStyle.State_On |
| 225 | | |
| 226 | | check_option.state = QtGui.QStyle.State_DownArrow |
| 227 | | |
| 228 | | QtGui.QApplication.style().drawControl(QtGui.QStyle.CE_MenuBarItem, |
| 229 | | check_option, |
| 230 | | painter) |
| 231 | | |
| 232 | | QtGui.QApplication.style().drawItemText(painter, rect, 0, option.palette, True, QString(value)) |
| 233 | | painter.restore() |
| 234 | | |
| 235 | | |
| 236 | | class TestComboBoxDelegate(QItemDelegate): |
| 237 | | |
| 238 | | |
| 239 | | def __init__(self, choices, parent = None): |
| 240 | | |
| 241 | | QItemDelegate.__init__(self, parent) |
| 242 | | self.choices = choices |
| 243 | | |
| 244 | | |
| 245 | | def createEditor(self, parent, option, index): |
| 246 | | editor = QComboBox( parent ) |
| 247 | | i = 0 |
| 248 | | for choice in self.choices: |
| 249 | | editor.insertItem(i, unicode(choice), QVariant(QString(choice))) |
| 250 | | i = i + 1 |
| 251 | | return editor |
| 252 | | |
| 253 | | def setEditorData( self, comboBox, index ): |
| 254 | | value = index.model().data(index, Qt.DisplayRole).toInt() |
| 255 | | comboBox.setCurrentIndex(value[0]) |
| 256 | | |
| 257 | | def setModelData(self, editor, model, index): |
| 258 | | value = editor.currentIndex() |
| 259 | | model.setData( index, editor.itemData( value, Qt.DisplayRole ) ) |
| 260 | | |
| 261 | | def updateEditorGeometry( self, editor, option, index ): |
| 262 | | |
| 263 | | editor.setGeometry(option.rect) |
| 264 | | rect = QtCore.QRect(option.rect.left(), |
| 265 | | option.rect.top(), |
| 266 | | option.rect.width(), |
| 267 | | option.rect.height()) |
| 268 | | |
| 269 | | #check_option.rect = rect |
| 270 | | check_option.palette = option.palette |
| 271 | | |
| 272 | | #if (option.state & QtGui.QStyle.State_Selected): |
| 273 | | # painter.fillRect(option.rect, option.palette.highlight()) |
| 274 | | #elif not self.editable: |
| 275 | | # painter.fillRect(option.rect, option.palette.window()) |
| 276 | | #else: |
| 277 | | # painter.fillRect(option.rect, background_color) |
| 278 | | |
| 279 | | #if checked: |
| 280 | | # check_option.state = option.state | QtGui.QStyle.State_On |
| 281 | | #else: |
| 282 | | # check_option.state = option.state | QtGui.QStyle.State_Off |
| 283 | | check_option.state = QtGui.QStyle.State_DownArrow |
| 284 | | |
| 285 | | QtGui.QApplication.style().drawControl(QtGui.QStyle.CE_MenuBarItem, |
| 286 | | check_option, |
| 287 | | painter) |
| 288 | | |
| 289 | | QtGui.QApplication.style().drawItemText(painter, |
| 290 | | rect, |
| 291 | | 0, |
| 292 | | option.palette, |
| 293 | | True, |
| 294 | | QString('choice')) |
| 295 | | |
| 296 | | painter.restore() |
| 297 | | |
| 298 | | |
| 299 | | class TestComboBoxDelegate(QItemDelegate): |
| 300 | | |
| 301 | | def __init__(self, choices, parent=None): |
| 302 | | QItemDelegate.__init__(self, parent) |
| 303 | | self.choices = choices |
| 304 | | |
| 305 | | def createEditor(self, parent, option, index): |
| 306 | | editor = QComboBox(parent) |
| 307 | | i = 0 |
| 308 | | for choice in self.choices: |
| 309 | | editor.insertItem(i, unicode(choice), QVariant(QString(choice))) |
| 310 | | i = i + 1 |
| 311 | | return editor |
| 312 | | |
| 313 | | def setEditorData(self, comboBox, index): |
| 314 | | value = index.model().data(index, Qt.DisplayRole).toInt() |
| 315 | | comboBox.setCurrentIndex(value[0]) |
| 316 | | |
| 317 | | def setModelData(self, editor, model, index): |
| 318 | | value = editor.currentIndex() |
| 319 | | model.setData(index, editor.itemData(value, Qt.DisplayRole)) |
| 320 | | |
| 321 | | def updateEditorGeometry(self, editor, option, index): |
| 322 | | editor.setGeometry(option.rect) |