Skip to content
Snippets Groups Projects
Commit 22842735 authored by Lan Dam's avatar Lan Dam
Browse files

Implement widget to display valueColors

parent 727a136c
No related branches found
No related tags found
1 merge request!213Implement widget to display valueColors
sohstationviewer/images/edit_icon_black_background.png

1.14 KiB

sohstationviewer/images/edit_icon_white_background.png

7.12 KiB

import sys
import platform
import os
from pathlib import Path
from typing import Optional
from PySide2 import QtWidgets, QtGui, QtCore
from PySide2.QtCore import Qt
from PySide2.QtWidgets import QWidget, QDialog, QTextEdit
from sohstationviewer.view.db_config.value_color_helper.functions import \
convert_value_color_str, prepare_value_color_html
class ValueColorWidget(QTextEdit):
def __init__(self, parent: Optional[QWidget], background: str):
"""
Widget to display valueColors and call a dialog to edit tha value
:param parent: the parent widget
:param background: 'B'/'W': flag indicating background color
"""
QtWidgets.QTextEdit.__init__(self, parent)
self.set_background(background)
# string for value color to be saved in DB
self.value_color_str: str = ''
# dialog that pop up when clicking on edit_button to help edit color
# and value
self.edit_value_color_dialog: Optional[QWidget] = None
# type of channel's plot
self.plot_type: str = ''
self.setReadOnly(True)
# change cursor to Arrow so user know they can't edit directly
self.viewport().setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
# to see all info
self.setFixedHeight(28)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.verticalScrollBar().setDisabled(True)
self.edit_button = QtWidgets.QToolButton(self)
self.edit_button.setCursor(Qt.PointingHandCursor)
current_file_path = os.path.abspath(__file__)
root_path = Path(current_file_path).parent.parent.parent.parent
if background == 'B':
img_file = f"{root_path}/images/edit_icon_black_background.png"
else:
img_file = f"{root_path}/images/edit_icon_white_background.png"
self.edit_button.setIcon(QtGui.QIcon(img_file))
self.edit_button.setStyleSheet(
"background: transparent; border: none;")
self.edit_button.clicked.connect(self.on_edit)
layout = QtWidgets.QHBoxLayout(self)
layout.addWidget(self.edit_button, 0, Qt.AlignRight)
layout.setSpacing(0)
layout.setMargin(5)
def set_background(self, background: str):
"""
Set black background for user to have better feeling how the colors
displayed on black background. Text and PlaceholderText's colors
have to be changed to be readable on the black background too.
:param background: 'B'/'W': sign for background color
"""
palette = self.palette()
if background == 'B':
palette.setColor(QtGui.QPalette.Text, Qt.white)
palette.setColor(QtGui.QPalette.Base, Qt.black)
palette.setColor(QtGui.QPalette.PlaceholderText, Qt.lightGray)
self.setPalette(palette)
def set_value_color(self, plot_type: str, value_color_str: str) \
-> None:
"""
Set value_color_str, value_color_edit_dialog and display value color
string in html to show color for user to have the feeling.
:param plot_type: type of channel's plot
:param value_color_str: string for value color to be saved in DB
"""
self.plot_type = plot_type
# Won't need to convert after database's valueColors are changed
self.value_color_str = convert_value_color_str(
plot_type, value_color_str)
value_color_html = prepare_value_color_html(self.value_color_str)
self.setHtml(value_color_html)
def on_edit(self):
print('edit value color')
class TestDialog(QDialog):
def __init__(self):
super(TestDialog, self).__init__(None)
main_layout = QtWidgets.QVBoxLayout()
self.setLayout(main_layout)
self.value_colorb_widget = ValueColorWidget(self, 'B')
main_layout.addWidget(self.value_colorb_widget)
self.value_colorw_widget = ValueColorWidget(self, 'W')
main_layout.addWidget(self.value_colorw_widget)
# linesDots
self.value_colorb_widget.set_value_color('linesDots', 'L:R|D:G')
self.value_colorw_widget.set_value_color('linesDots', 'L:R|D:G')
# triColorLines
# self.value_colorb_widget.set_value_color(
# 'triColorLines', '-1:M|0:R|1:G')
# self.value_colorw_widget.set_value_color(
# 'triColorLines', '-1:M|0:R|1:G')
# multiColorDotsEqualOnUpperBound
# self.value_colorb_widget.set_value_color(
# 'multiColorDotsEqualOnUpperBound', '0:R|1:Y|2:G|+2:M')
# self.value_colorw_widget.set_value_color(
# 'multiColorDotsEqualOnUpperBound', '0:R|1:Y|2:G|+2:M')
# multiColorDotsEqualOnLowerBound
# self.value_colorb_widget.set_value_color('multiColorDotsEqualOnLowerBound',
# '3:R|3.3:Y|=3.3:G')
# self.value_colorw_widget.set_value_color('multiColorDotsEqualOnLowerBound',
# '3:R|3.3:Y|=3.3:G')
if __name__ == '__main__':
os_name, version, *_ = platform.platform().split('-')
if os_name == 'macOS':
os.environ['QT_MAC_WANTS_LAYER'] = '1'
app = QtWidgets.QApplication(sys.argv)
test = TestDialog()
test.exec_()
sys.exit(app.exec_())
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment