conan/uiwidget/widgetobject.py

82 lines
2.9 KiB
Python
Raw Normal View History

2021-10-17 12:49:49 +02:00
import numpy as np
import pyqtgraph
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QTextEdit
import pyqtgraph as pg
class WidgetObject(QtWidgets.QWidget):
def __init__(self, parent=None):
super(WidgetObject, self).__init__(parent)
self.frame = 0
self.tagFields = QtWidgets.QWidget()
self.tagLayout = QtWidgets.QVBoxLayout()
# Setup Graph Plot Widget
self.tagGraph = pg.PlotWidget()
self.tagGraph.setBackground('w')
self.tagGraph.setYRange(0, 400, padding=0)
self.tagGraph.getPlotItem().getAxis('bottom').setTickSpacing(minor=50, major=100)
self.tagGraph.getPlotItem().setTitle(title='Movement of Object Tags')
self.tagPlots = dict()
self.tagTextFields = dict()
self.plotText = dict()
layout = QtWidgets.QGridLayout()
layout.addWidget(self.tagGraph, 0, 0)
layout.addWidget(self.tagFields, 0, 1)
self.setLayout(layout)
@QtCore.pyqtSlot(list, tuple, dict, list)
def setInit(self, tags, frameSize, tracked, colors):
for i, tag in enumerate(tags):
label = QtWidgets.QLabel('Object tag #%i:' % tag)
label.setStyleSheet('color: black; background-color: rgb(%i,%i,%i)' % colors[i])
label.setFixedHeight(20)
field = QtWidgets.QTextEdit()
field.setFixedHeight(20)
field.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
field.textChanged.connect(self.tagTextChanged)
self.tagTextFields[tag] = field
trackedLabel = QtWidgets.QLabel('Tracked: {:.0%}'.format(tracked[tag]))
oneTagLayout = QtWidgets.QHBoxLayout()
oneTagLayout.addWidget(label)
oneTagLayout.addWidget(field)
oneTagLayout.addWidget(trackedLabel)
self.tagLayout.insertLayout(-1, oneTagLayout)
x = list(range(-200, 0)) # 200 time points
y = [0 for _ in range(200)] # 200 data points
dataLine = self.tagGraph.plot(x, y, pen=pg.mkPen(color=colors[i]))
self.tagPlots[tag] = dataLine
text = pg.TextItem(text='', color=colors[i])
text.setAnchor((1, i + 1))
self.plotText[tag] = text
self.tagGraph.addItem(text)
self.tagFields.setLayout(self.tagLayout)
@QtCore.pyqtSlot(dict)
def updateTagGraph(self, tagData):
for tag, values in tagData.items():
self.tagPlots[tag].setData(x=values[1], y=values[0]) # Update the data.
if tagData:
self.tagGraph.setXRange(np.min(values[1]), np.max(values[1]))
def tagTextChanged(self):
for tag, field in self.tagTextFields.items():
self.plotText[tag].setText(field.toPlainText())
x, y = self.tagPlots[tag].getData()
if len(x) > 0 and len(y) > 0:
#print(tag, x[-1], y[-1])
self.plotText[tag].setPos(x[-1], y[-1])