| 1 |
# -*- coding utf-8 -*- |
| 2 |
"""This is a test utility that consumes patches generated by |
| 3 |
ResolveLSPatternGeneratorServer or ResolveCMPatternGeneratorServer. |
| 4 |
""" |
| 5 |
import random |
| 6 |
import socket |
| 7 |
import struct |
| 8 |
import sys |
| 9 |
import time |
| 10 |
from xml.etree import ElementTree |
| 11 |
|
| 12 |
from qtpy import QtCore, QtGui, QtWidgets |
| 13 |
|
| 14 |
|
| 15 |
ADDRESS = ("localhost", 20002) |
| 16 |
|
| 17 |
# <?xml version="1.0" encoding="utf-8"?> |
| 18 |
# <calibration> |
| 19 |
# <color red="512" green="512" blue="512" bits="10"/> |
| 20 |
# <background red="168" green="168" blue="168" bits="10"/> |
| 21 |
# <geometry x="0.3519" y="0.2023" cx="0.3281" cy="0.5076"/> |
| 22 |
# </calibration> |
| 23 |
|
| 24 |
def main(): |
| 25 |
"""The main function that runs the dialog.""" |
| 26 |
app = QtWidgets.QApplication(sys.argv) |
| 27 |
dialog = PatchConsumerDialog() |
| 28 |
dialog.accepted.connect(sys.exit) |
| 29 |
dialog.show() |
| 30 |
dialog.run_client() |
| 31 |
app.exec() |
| 32 |
|
| 33 |
|
| 34 |
class PatchConsumerDialog(QtWidgets.QDialog): |
| 35 |
"""The main dialog.""" |
| 36 |
|
| 37 |
def __init__(self, *args, **kwargs): |
| 38 |
super(PatchConsumerDialog, self).__init__(*args, **kwargs) |
| 39 |
self.main_layout = None |
| 40 |
self.color_patch = None |
| 41 |
self.client = None |
| 42 |
self._setup() |
| 43 |
self.app = QtWidgets.QApplication.instance() |
| 44 |
|
| 45 |
def _setup(self): |
| 46 |
"""Create widgets.""" |
| 47 |
self.resize(500, 500) |
| 48 |
self.main_layout = QtWidgets.QVBoxLayout(self) |
| 49 |
self.setAttribute(QtCore.Qt.WA_StyledBackground, True) |
| 50 |
self.setWindowFlags( |
| 51 |
self.windowFlags() | QtCore.Qt.WindowStaysOnTopHint |
| 52 |
) |
| 53 |
|
| 54 |
def update_color(self, color:QtGui.QColor): |
| 55 |
"""Update the color. |
| 56 |
|
| 57 |
Args: |
| 58 |
color (QtGui.QColor): The color to update to. |
| 59 |
""" |
| 60 |
style_sheet = "background-color: rgb({}, {}, {});".format( |
| 61 |
color.red(), color.green(), color.blue() |
| 62 |
) |
| 63 |
print( |
| 64 |
"{:.04f} {:.04f} {:.04f}".format( |
| 65 |
color.redF(), |
| 66 |
color.greenF(), |
| 67 |
color.blueF(), |
| 68 |
) |
| 69 |
) |
| 70 |
self.setStyleSheet(style_sheet) |
| 71 |
|
| 72 |
def run_client(self): |
| 73 |
"""Run the client.""" |
| 74 |
self.client = Client() |
| 75 |
self.client.update_color.connect(self.update_color) |
| 76 |
self.client.start() |
| 77 |
|
| 78 |
class Client(QtCore.QThread): |
| 79 |
"""The client thread.""" |
| 80 |
|
| 81 |
update_color = QtCore.Signal(QtGui.QColor) |
| 82 |
|
| 83 |
def __init__(self): |
| 84 |
super(Client, self).__init__() |
| 85 |
self.buffer_size = 16 |
| 86 |
self.conn = None |
| 87 |
|
| 88 |
def run(self): |
| 89 |
"""Run the thread.""" |
| 90 |
self.conn = socket.create_connection(ADDRESS) |
| 91 |
while True: |
| 92 |
xml_data = b"" |
| 93 |
print(xml_data) |
| 94 |
while True: |
| 95 |
data_length_raw = self.conn.recv(4) |
| 96 |
data_length = struct.unpack(">I", data_length_raw) |
| 97 |
data = self.conn.recv(data_length[0]) |
| 98 |
color = self.parse_xml_data(data) |
| 99 |
self.update_color.emit(color) |
| 100 |
|
| 101 |
def parse_xml_data(self, xml_data_raw:bytes) -> QtGui.QColor: |
| 102 |
"""Parse the xml data and return the QColor.""" |
| 103 |
calibration = ElementTree.fromstring(xml_data_raw) |
| 104 |
color = calibration.find("color") |
| 105 |
red = int(color.attrib["red"]) |
| 106 |
green = int(color.attrib["green"]) |
| 107 |
blue = int(color.attrib["blue"]) |
| 108 |
bit_depth = int(color.attrib["bits"]) |
| 109 |
# scale the colors to 16bit |
| 110 |
bit_multiplier = 2**(16 - bit_depth) |
| 111 |
return QtGui.QColor( |
| 112 |
QtGui.QRgba64.fromRgba64( |
| 113 |
red * bit_multiplier, |
| 114 |
green * bit_multiplier, |
| 115 |
blue * bit_multiplier, |
| 116 |
65535, # alpha |
| 117 |
) |
| 118 |
) |
| 119 |
|
| 120 |
def quit(self): |
| 121 |
"""Quit the thread.""" |
| 122 |
self.socket.close() |
| 123 |
|
| 124 |
|
| 125 |
if __name__ == "__main__": |
| 126 |
main() |