mirror of
https://github.com/bec-project/bec_widgets.git
synced 2025-07-14 03:31:50 +02:00
feat: add textbox widget
This commit is contained in:
@ -17,6 +17,7 @@ class Widgets(str, enum.Enum):
|
||||
BECDockArea = "BECDockArea"
|
||||
BECFigure = "BECFigure"
|
||||
SpiralProgressBar = "SpiralProgressBar"
|
||||
TextBox = "TextBox"
|
||||
WebsiteWidget = "WebsiteWidget"
|
||||
|
||||
|
||||
@ -1897,6 +1898,48 @@ class SpiralProgressBar(RPCBase):
|
||||
"""
|
||||
|
||||
|
||||
class StopButton(RPCBase):
|
||||
@property
|
||||
@rpc_call
|
||||
def config_dict(self) -> "dict":
|
||||
"""
|
||||
Get the configuration of the widget.
|
||||
|
||||
Returns:
|
||||
dict: The configuration of the widget.
|
||||
"""
|
||||
|
||||
@rpc_call
|
||||
def get_all_rpc(self) -> "dict":
|
||||
"""
|
||||
Get all registered RPC objects.
|
||||
"""
|
||||
|
||||
|
||||
class TextBox(RPCBase):
|
||||
@rpc_call
|
||||
def set_color(self, background_color: str, font_color: str) -> None:
|
||||
"""
|
||||
Set the background color of the Widget.
|
||||
|
||||
Args:
|
||||
background_color (str): The color to set the background in HEX.
|
||||
font_color (str): The color to set the font in HEX.
|
||||
"""
|
||||
|
||||
@rpc_call
|
||||
def set_text(self, text: str) -> None:
|
||||
"""
|
||||
Set the text of the Widget
|
||||
"""
|
||||
|
||||
@rpc_call
|
||||
def set_font_size(self, size: int) -> None:
|
||||
"""
|
||||
Set the font size of the text in the Widget.
|
||||
"""
|
||||
|
||||
|
||||
class WebsiteWidget(RPCBase):
|
||||
@rpc_call
|
||||
def set_url(self, url: str) -> None:
|
||||
|
@ -1,6 +1,7 @@
|
||||
from bec_widgets.utils import BECConnector
|
||||
from bec_widgets.widgets.figure import BECFigure
|
||||
from bec_widgets.widgets.spiral_progress_bar.spiral_progress_bar import SpiralProgressBar
|
||||
from bec_widgets.widgets.text_box.text_box import TextBox
|
||||
from bec_widgets.widgets.website.website import WebsiteWidget
|
||||
|
||||
|
||||
@ -11,6 +12,7 @@ class RPCWidgetHandler:
|
||||
"BECFigure": BECFigure,
|
||||
"SpiralProgressBar": SpiralProgressBar,
|
||||
"Website": WebsiteWidget,
|
||||
"TextBox": TextBox,
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
|
0
bec_widgets/widgets/text_box/__init__.py
Normal file
0
bec_widgets/widgets/text_box/__init__.py
Normal file
77
bec_widgets/widgets/text_box/text_box.py
Normal file
77
bec_widgets/widgets/text_box/text_box.py
Normal file
@ -0,0 +1,77 @@
|
||||
import re
|
||||
|
||||
from qtpy.QtWidgets import QTextEdit
|
||||
|
||||
from bec_widgets.utils import BECConnector
|
||||
|
||||
|
||||
class TextBox(BECConnector, QTextEdit):
|
||||
|
||||
USER_ACCESS = ["set_color", "set_text", "set_font_size"]
|
||||
|
||||
def __init__(self, text: str = "", parent=None, client=None, config=None, gui_id=None):
|
||||
super().__init__(client=client, config=config, gui_id=gui_id)
|
||||
QTextEdit.__init__(self, parent=parent)
|
||||
|
||||
self.setReadOnly(True)
|
||||
self._background_color = "#FFF"
|
||||
self._font_color = "#000"
|
||||
self._font_size = 12
|
||||
self.set_color(self._background_color, self._font_color)
|
||||
self.setGeometry(self.rect())
|
||||
self._text = text
|
||||
self.set_text(text)
|
||||
|
||||
def set_color(self, background_color: str, font_color: str) -> None:
|
||||
"""Set the background color of the Widget.
|
||||
|
||||
Args:
|
||||
background_color (str): The color to set the background in HEX.
|
||||
font_color (str): The color to set the font in HEX.
|
||||
|
||||
"""
|
||||
self._background_color = background_color
|
||||
self._font_color = font_color
|
||||
self._update_stylesheet()
|
||||
|
||||
def set_font_size(self, size: int) -> None:
|
||||
"""Set the font size of the text in the Widget."""
|
||||
self._font_size = size
|
||||
self._update_stylesheet()
|
||||
|
||||
def _update_stylesheet(self):
|
||||
"""Update the stylesheet of the widget."""
|
||||
self.setStyleSheet(
|
||||
f"background-color: {self._background_color}; color: {self._font_color}; font-size: {self._font_size}px"
|
||||
)
|
||||
|
||||
def set_text(self, text: str) -> None:
|
||||
"""Set the text of the Widget"""
|
||||
if self.is_html(text):
|
||||
self.setHtml(text)
|
||||
else:
|
||||
self.setPlainText(text)
|
||||
self._text = text
|
||||
|
||||
def is_html(self, text: str) -> bool:
|
||||
"""Check if the text contains HTML tags.
|
||||
|
||||
Args:
|
||||
text (str): The text to check.
|
||||
|
||||
Returns:
|
||||
bool: True if the text contains HTML tags, False otherwise.
|
||||
"""
|
||||
return bool(re.search(r"<[a-zA-Z/][^>]*>", text))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import sys
|
||||
|
||||
from qtpy.QtWidgets import QApplication
|
||||
|
||||
app = QApplication(sys.argv)
|
||||
|
||||
widget = TextBox()
|
||||
widget.show()
|
||||
sys.exit(app.exec())
|
Reference in New Issue
Block a user