# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function
from wbia.guitool.__PYQT__ import QtCore, QtGui
from wbia.guitool.__PYQT__ import QtWidgets
from wbia.guitool.__PYQT__ import GUITOOL_PYQT_VERSION
import utool as ut
ut.noinject(__name__, '[guitool.delegates]', DEBUG=False)
[docs]class APIDelegate(QtWidgets.QItemDelegate):
is_persistant_editable = True
def __init__(self, parent):
QtWidgets.QItemDelegate.__init__(self, parent)
[docs] def sizeHint(option, qindex):
# QStyleOptionViewItem option
return QtCore.QSize(50, 50)
[docs]class ImageDelegate(QtWidgets.QStyledItemDelegate):
def __init__(self, parent):
print(dir(self))
QtWidgets.QStyledItemDelegate.__init__(self, parent)
[docs] def paint(self, painter, option, index):
painter.fillRect(option.rect, QtGui.QColor(191, 222, 185))
# path = "path\to\my\image.jpg"
self.path = 'image.bmp'
image = QtGui.QImage(str(self.path))
pixmap = QtGui.QPixmap.fromImage(image)
pixmap.scaled(50, 40, QtCore.Qt.KeepAspectRatio)
painter.drawPixmap(option.rect, pixmap)
[docs]class ComboDelegate(APIDelegate):
"""
A delegate that places a fully functioning QComboBox in every
cell of the column to which it's applied
"""
def __init__(self, parent):
APIDelegate.__init__(self, parent)
[docs] def createEditor(self, parent, option, index):
combo = QtWidgets.QComboBox(parent)
combo.addItems(['option1', 'option2', 'option3'])
# FIXME: Change to newstyle signal slot
if GUITOOL_PYQT_VERSION == 5:
self.connect(combo.currentIndexChanged, self.currentIndexChanged)
else:
# I believe this particular option is broken in pyqt4
self.connect(
combo,
QtCore.SIGNAL('currentIndexChanged(int)'),
self,
QtCore.SLOT('currentIndexChanged()'),
)
return combo
[docs] def setEditorData(self, editor, index):
editor.blockSignals(True)
editor.setCurrentIndex(int(index.model().data(index).toString()))
editor.blockSignals(False)
[docs] def setModelData(self, editor, model, index):
model.setData(index, editor.currentIndex())
[docs] @QtCore.pyqtSlot()
def currentIndexChanged(self):
self.commitData.emit(self.sender())
# DELEGATE_MAP = {
# 'BUTTON': ButtonDelegate,
# 'COMBO': ComboDelegate,
# }