Skip to content

Instantly share code, notes, and snippets.

@wkentaro
Created February 14, 2020 00:16
Show Gist options
  • Save wkentaro/738daf8678d4d0e34e2c31dde4784e89 to your computer and use it in GitHub Desktop.
Save wkentaro/738daf8678d4d0e34e2c31dde4784e89 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import sys
import imgviz
from qtpy import QtCore
from qtpy.QtCore import Qt
from qtpy import QtWidgets
from qtpy import QtGui
from qtpy.QtCore import *
from qtpy.QtWidgets import *
from qtpy.QtGui import *
class HTMLDelegate(QStyledItemDelegate):
def __init__(self, parent=None):
super().__init__()
self.doc = QTextDocument(self)
def paint(self, painter, option, index):
painter.save()
options = QStyleOptionViewItem(option)
self.initStyleOption(options, index)
self.doc.setHtml(options.text)
options.text = ""
style = QApplication.style() if options.widget is None \
else options.widget.style()
style.drawControl(QStyle.CE_ItemViewItem, options, painter)
ctx = QAbstractTextDocumentLayout.PaintContext()
if option.state & QStyle.State_Selected:
ctx.palette.setColor(QPalette.Text, option.palette.color(
QPalette.Active, QPalette.HighlightedText))
else:
ctx.palette.setColor(QPalette.Text, option.palette.color(
QPalette.Active, QPalette.Text))
textRect = style.subElementRect(
QStyle.SE_ItemViewItemText, options)
if index.column() != 0:
textRect.adjust(5, 0, 0, 0)
thefuckyourshitup_constant = 4
margin = (option.rect.height() - options.fontMetrics.height()) // 2
margin = margin - thefuckyourshitup_constant
textRect.setTop(textRect.top() + margin)
painter.translate(textRect.topLeft())
painter.setClipRect(textRect.translated(-textRect.topLeft()))
self.doc.documentLayout().draw(painter, ctx)
painter.restore()
def sizeHint(self, option, index):
return QSize(self.doc.idealWidth(), self.doc.size().height())
def main():
app = QtWidgets.QApplication(sys.argv)
view = QtWidgets.QListView()
view.setWindowFlags(Qt.Window)
view.setModel(QtGui.QStandardItemModel())
colormap = imgviz.label_colormap(value=200)
colormap = {
"person": colormap[15],
"dog": colormap[6],
}
for text in ["person", "dog"]:
item = QtGui.QStandardItem()
item.setText('{} <font color="#{:02x}{:02x}{:02x}">●</font>'.format(text, *colormap[text]))
item.setCheckable(True)
item.setCheckState(Qt.Checked)
view.model().setItem(view.model().rowCount(), 0, item)
def itemChangedCallback(item):
print("itemChangedCallback:", item, item.text())
def itemSelectionChangedCallback(selected, deselected):
# print(selected.indexes(), deselected.indexes())
for index in selected.indexes():
item = view.model().item(index.row(), index.column())
print("itemSelectionChangedCallback:", item, item.text())
view.model().itemChanged.connect(itemChangedCallback)
view.selectionModel().selectionChanged.connect(itemSelectionChangedCallback)
view.setItemDelegate(HTMLDelegate())
view.show()
view.raise_()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment