From e32ed18dfb34da6d228efefb5f3390bc89a16c5a Mon Sep 17 00:00:00 2001 From: Kseninia Mikhaylova Date: Tue, 15 Oct 2024 10:17:39 +0300 Subject: [PATCH] qt gui --- gui_test.py => gui/gui_test.py | 12 +-- gui/robot.py | 74 ++++++++++++++ gui_test copy.py | 180 --------------------------------- main.py | 6 +- 4 files changed, 78 insertions(+), 194 deletions(-) rename gui_test.py => gui/gui_test.py (88%) create mode 100644 gui/robot.py delete mode 100644 gui_test copy.py diff --git a/gui_test.py b/gui/gui_test.py similarity index 88% rename from gui_test.py rename to gui/gui_test.py index ea9f313..c3e0905 100644 --- a/gui_test.py +++ b/gui/gui_test.py @@ -2,17 +2,7 @@ import sys from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout from PyQt5.QtCore import Qt from PyQt5.QtGui import QColor, QPalette - -class ChangeRobot(QWidget): - def __init__(self, **kwargs): - super().__init__() - self.initUI() - - def initUI(self): - palette = self.palette() - palette.setColor(QPalette.Window, QColor(173, 216, 230)) # LightBlue - self.setAutoFillBackground(True) - self.setPalette(palette) +from gui.robot import ChangeRobot class Status(QWidget): def __init__(self, **kwargs): diff --git a/gui/robot.py b/gui/robot.py new file mode 100644 index 0000000..aea9db9 --- /dev/null +++ b/gui/robot.py @@ -0,0 +1,74 @@ +import sys +import time +from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QRadioButton, QPushButton, QButtonGroup +from PyQt5.QtCore import Qt, QTimer + +class ChangeRobot(QWidget): + def __init__(self, robots, updateRobot, status): + super().__init__() + self.robots = robots or [] + self.updateRobot = updateRobot + self.status = status + + self.initUI() + self.old_status = None + self.counter = 0 + + self.timer = QTimer(self) + self.timer.timeout.connect(self.timerCallback) + + def initUI(self): + self.layout = QVBoxLayout() + + self.robotsLabel = QLabel("Выберите робота") + self.layout.addWidget(self.robotsLabel) + + self.buttonGroup = QButtonGroup(self) + self.robotsRadio = [] + + for i, r in enumerate(self.robots): + radioButton = QRadioButton(r["name"]) + self.buttonGroup.addButton(radioButton, i) + self.robotsRadio.append(radioButton) + self.layout.addWidget(radioButton) + + self.connectButton = QPushButton("Соединить") + self.connectButton.clicked.connect(self.connectRobot) + self.layout.addWidget(self.connectButton) + + self.setLayout(self.layout) + self.updateConnectButtonText() + + def updateConnectButtonText(self): + if self.status() == "not_connected": + self.connectButton.setText("Соединить") + elif self.status() == "connected": + self.connectButton.setText("Отключить") + + def timerCallback(self): + new_status = self.status() + self.counter += 1 + if new_status == self.old_status and self.counter <= 10: + self.connectButton.setText(self.connectButton.text() + ".") + else: + self.old_status = None + self.timer.stop() + self.updateConnectButtonText() + + def connectRobot(self): + self.updateConnectButtonText() + selectedId = self.buttonGroup.checkedId() + if selectedId == -1: + self.connectButton.setText("Выберите робота") + return + + self.old_status = self.status() + self.timer.start(500) + time.sleep(0.3) + self.updateRobot(self.robots[selectedId]) + + def paintEvent(self, event): + p = self.palette() + p.setColor(self.backgroundRole(), Qt.lightGray) + self.setPalette(p) + super().paintEvent(event) \ No newline at end of file diff --git a/gui_test copy.py b/gui_test copy.py deleted file mode 100644 index e27e9f8..0000000 --- a/gui_test copy.py +++ /dev/null @@ -1,180 +0,0 @@ -from juce_init import START_JUCE_COMPONENT -import popsicle as juce -import time - - -class ChangeRobot(juce.Component, juce.Timer): - backgroundColour = juce.Colours.lightblue - textColour = juce.Colours.black - - robots = [] - - robotsLabel = juce.Label("", "Выберите робота") - robotsRadio = [] - robotsControls = [robotsLabel] + robotsRadio - robotButtonsId = 1001 - - connectButton = juce.TextButton("Соединить") - - old_status = None - counter = 0 - - def __init__(self, robots, updateRobot, status): - super().__init__() - juce.Timer.__init__(self) - - self.robots = robots or [] - self.updateRobot = updateRobot - self.status = status - - for r in self.robots: - self.robotsRadio.append( - juce.ToggleButton(r["name"]), - ) - self.robotsControls = [self.robotsLabel] + self.robotsRadio - - for s in self.robotsControls: - self.addAndMakeVisible(s) - - for s in self.robotsRadio: - s.onClick = self.setConnectText - s.setRadioGroupId(self.robotButtonsId, juce.dontSendNotification) - s.setClickingTogglesState(True) - - self.addAndMakeVisible(self.connectButton) - self.connectButton.onClick = self.connectRobot - - def paint(self, g: juce.Graphics): - g.fillAll(self.backgroundColour) - - for i, s in enumerate(self.robotsControls): - s.setColour(s.textColourId, self.textColour) - if hasattr(s, "tickDisabledColourId"): - s.setColour(s.tickColourId, self.textColour) - s.setColour(s.tickDisabledColourId, self.textColour) - - s.setBounds(10 if i == 0 else 20, (i * 20), self.getWidth() - 20, 20) - - h = i + 2 - self.connectButton.setBounds(10, (h * 20), self.getWidth() - 20, 20) - - def setConnectText(self): - if self.status() == "not_connected": - self.connectButton.setButtonText("Connect") - if self.status() == "connected": - self.connectButton.setButtonText("Disconnect") - - def timerCallback(self): - new_status = self.status() - self.counter += 1 - if new_status == self.old_status and self.counter <= 10: - self.connectButton.setButtonText(self.connectButton.getButtonText() + ".") - else: - self.old_status = None - self.stopTimer() - self.setConnectText() - - def connectRobot(self): - self.setConnectText() - res = None - for i, s in enumerate(self.robotsRadio): - if s.getToggleState() == True: - res = i - break - - if res == None: - self.connectButton.setButtonText("Выберите робота") - return - - self.old_status = self.status() - self.startTimer(500) - time.sleep(0.3) - self.updateRobot(self.robots[i]) - - -class Status(juce.Component, juce.Timer): - backgroundColour = juce.Colours.lightgrey - textColour = juce.Colours.black - - def __init__(self, status): - super().__init__() - juce.Timer.__init__(self) - - self.status = status - - self.statusLabel = juce.Label(self.status()) - self.addAndMakeVisible(self.statusLabel) - - def paint(self, g: juce.Graphics): - g.fillAll(self.backgroundColour) - self.statusLabel.setColour(self.statusLabel.textColourId, self.textColour) - self.statusLabel.setBounds(10, 0, self.getWidth() - 10, 20) - - def timerCallback(self): - # self.setConnectText() - pass - - -class RightPanel(juce.Component): - def __init__(self, panels): - super().__init__() - self.panels = panels - - for p in self.panels: - self.addAndMakeVisible(p) - - def paint(self, g): - g.fillAll(juce.Colours.blue) - - def resized(self): - bounds = self.getLocalBounds() - - for p in self.panels: - p.setBounds( - bounds.removeFromTop(self.proportionOfHeight(1 / len(self.panels))) - ) - pass - - -class MainPanel(juce.Component): - def __init__(self): - super().__init__() - - def paint(self, g): - g.fillAll(juce.Colours.hotpink) - - -class MainContentComponent(juce.Component): - def __init__(self, **kwargs): - super().__init__() - - robotArgs = kwargs.get("robotPanel") - robotPanel = ChangeRobot(**robotArgs) - statusArgs = kwargs.get("statusPanel") - statusPanel = Status(**statusArgs) - self.rightPanel = RightPanel(panels=[robotPanel, statusPanel]) - self.mainPanel = MainPanel() - - self.addAndMakeVisible(self.rightPanel) - self.addAndMakeVisible(self.mainPanel) - - self.setSize(800, 600) - # self.setResizable(False, False) - - def paint(self, g): - g.fillAll( - self.getLookAndFeel().findColour(juce.ResizableWindow.backgroundColourId) - ) - - def resized(self): - bounds = self.getLocalBounds() - self.rightPanel.setBounds( - bounds.removeFromRight( - self.proportionOfWidth(0.25), - ), - ) - self.mainPanel.setBounds(bounds) - - -if __name__ == "__main__": - START_JUCE_COMPONENT(MainContentComponent, name="ROBOT GUI") diff --git a/main.py b/main.py index 4207163..433fcc9 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,7 @@ import json import sys from PyQt5.QtWidgets import QApplication -from gui_test import MainContentComponent +from gui.gui_test import MainContentComponent from client_socket import SocketRobotArm import time @@ -16,9 +16,9 @@ class MyApp: def get_status(self): if self.robot_app: - return self.robot_app.get_status + return self.robot_app.get_status() else: - return self.def_robot_status + return self.def_robot_status() def def_robot_status(self): return SocketRobotArm.Status[1]