qt test
This commit is contained in:
parent
69f2fd810d
commit
de92e54a6c
|
@ -0,0 +1,180 @@
|
||||||
|
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")
|
218
gui_test.py
218
gui_test.py
|
@ -1,180 +1,92 @@
|
||||||
from juce_init import START_JUCE_COMPONENT
|
import sys
|
||||||
import popsicle as juce
|
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout
|
||||||
import time
|
from PyQt5.QtCore import Qt
|
||||||
|
from PyQt5.QtGui import QColor, QPalette
|
||||||
|
|
||||||
|
class ChangeRobot(QWidget):
|
||||||
class ChangeRobot(juce.Component, juce.Timer):
|
def __init__(self, **kwargs):
|
||||||
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__()
|
super().__init__()
|
||||||
juce.Timer.__init__(self)
|
self.initUI()
|
||||||
|
|
||||||
self.robots = robots or []
|
def initUI(self):
|
||||||
self.updateRobot = updateRobot
|
palette = self.palette()
|
||||||
self.status = status
|
palette.setColor(QPalette.Window, QColor(173, 216, 230)) # LightBlue
|
||||||
|
self.setAutoFillBackground(True)
|
||||||
|
self.setPalette(palette)
|
||||||
|
|
||||||
for r in self.robots:
|
class Status(QWidget):
|
||||||
self.robotsRadio.append(
|
def __init__(self, **kwargs):
|
||||||
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__()
|
super().__init__()
|
||||||
juce.Timer.__init__(self)
|
self.initUI()
|
||||||
|
|
||||||
self.status = status
|
def initUI(self):
|
||||||
|
palette = self.palette()
|
||||||
|
palette.setColor(QPalette.Window, QColor(144, 238, 144)) # LightGreen
|
||||||
|
self.setAutoFillBackground(True)
|
||||||
|
self.setPalette(palette)
|
||||||
|
|
||||||
self.statusLabel = juce.Label(self.status())
|
class RightPanel(QWidget):
|
||||||
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):
|
def __init__(self, panels):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.panels = panels
|
layout = QVBoxLayout()
|
||||||
|
for panel in panels:
|
||||||
|
# panel.setFixedHeight(200)
|
||||||
|
layout.addWidget(panel)
|
||||||
|
self.setLayout(layout)
|
||||||
|
self.initUI()
|
||||||
|
|
||||||
for p in self.panels:
|
def initUI(self):
|
||||||
self.addAndMakeVisible(p)
|
palette = self.palette()
|
||||||
|
palette.setColor(QPalette.Window, QColor(169, 169, 169)) # DarkGray
|
||||||
|
self.setAutoFillBackground(True)
|
||||||
|
self.setPalette(palette)
|
||||||
|
|
||||||
def paint(self, g):
|
class MainPanel(QWidget):
|
||||||
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):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
self.initUI()
|
||||||
|
|
||||||
def paint(self, g):
|
def initUI(self):
|
||||||
g.fillAll(juce.Colours.hotpink)
|
palette = self.palette()
|
||||||
|
palette.setColor(QPalette.Window, QColor(255, 255, 255)) # White
|
||||||
|
self.setAutoFillBackground(True)
|
||||||
|
self.setPalette(palette)
|
||||||
|
|
||||||
|
class MainContentComponent(QWidget):
|
||||||
class MainContentComponent(juce.Component):
|
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
robotArgs = kwargs.get("robotPanel")
|
robotArgs = kwargs.get("robotPanel", {})
|
||||||
robotPanel = ChangeRobot(**robotArgs)
|
robotPanel = ChangeRobot(**robotArgs)
|
||||||
statusArgs = kwargs.get("statusPanel")
|
statusArgs = kwargs.get("statusPanel", {})
|
||||||
statusPanel = Status(**statusArgs)
|
statusPanel = Status(**statusArgs)
|
||||||
self.rightPanel = RightPanel(panels=[robotPanel, statusPanel])
|
self.rightPanel = RightPanel(panels=[robotPanel, statusPanel])
|
||||||
self.mainPanel = MainPanel()
|
self.mainPanel = MainPanel()
|
||||||
|
|
||||||
self.addAndMakeVisible(self.rightPanel)
|
layout = QHBoxLayout()
|
||||||
self.addAndMakeVisible(self.mainPanel)
|
layout.addWidget(self.mainPanel)
|
||||||
|
layout.addWidget(self.rightPanel)
|
||||||
|
|
||||||
self.setSize(800, 600)
|
self.setLayout(layout)
|
||||||
# self.setResizable(False, False)
|
self.setFixedSize(800, 600)
|
||||||
|
self.initUI()
|
||||||
|
|
||||||
def paint(self, g):
|
def initUI(self):
|
||||||
g.fillAll(
|
palette = QPalette()
|
||||||
self.getLookAndFeel().findColour(juce.ResizableWindow.backgroundColourId)
|
palette.setColor(QPalette.Background, QColor(240, 240, 240))
|
||||||
)
|
self.setPalette(palette)
|
||||||
|
|
||||||
def resized(self):
|
def resizeEvent(self, event):
|
||||||
bounds = self.getLocalBounds()
|
width = self.width()
|
||||||
self.rightPanel.setBounds(
|
rightPanelWidth = int(width * 0.25)
|
||||||
bounds.removeFromRight(
|
self.rightPanel.setFixedWidth(rightPanelWidth)
|
||||||
self.proportionOfWidth(0.25),
|
super().resizeEvent(event)
|
||||||
),
|
|
||||||
)
|
|
||||||
self.mainPanel.setBounds(bounds)
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
if __name__ == "__main__":
|
app = QApplication(sys.argv)
|
||||||
START_JUCE_COMPONENT(MainContentComponent, name="ROBOT GUI")
|
mainWindow = MainContentComponent(
|
||||||
|
robotPanel={'arg1': 'value1'},
|
||||||
|
statusPanel={'arg1': 'value1'}
|
||||||
|
)
|
||||||
|
mainWindow.show()
|
||||||
|
sys.exit(app.exec_())
|
||||||
|
|
25
main.py
25
main.py
|
@ -1,5 +1,6 @@
|
||||||
import json
|
import json
|
||||||
from juce_init import START_JUCE_COMPONENT
|
import sys
|
||||||
|
from PyQt5.QtWidgets import QApplication
|
||||||
from gui_test import MainContentComponent
|
from gui_test import MainContentComponent
|
||||||
from client_socket import SocketRobotArm
|
from client_socket import SocketRobotArm
|
||||||
import time
|
import time
|
||||||
|
@ -13,19 +14,31 @@ class MyApp:
|
||||||
self.startRobot()
|
self.startRobot()
|
||||||
self.startGui()
|
self.startGui()
|
||||||
|
|
||||||
|
def get_status(self):
|
||||||
|
if self.robot_app:
|
||||||
|
return self.robot_app.get_status
|
||||||
|
else:
|
||||||
|
return self.def_robot_status
|
||||||
|
|
||||||
|
def def_robot_status(self):
|
||||||
|
return SocketRobotArm.Status[1]
|
||||||
|
|
||||||
def startGui(self):
|
def startGui(self):
|
||||||
self.gui_app = START_JUCE_COMPONENT(
|
app = QApplication(sys.argv)
|
||||||
MainContentComponent,
|
mainWindow = MainContentComponent(
|
||||||
name="ROBOT GUI",
|
|
||||||
robotPanel={
|
robotPanel={
|
||||||
"robots": self.robots,
|
"robots": self.robots,
|
||||||
"updateRobot": self.updateRobot,
|
"updateRobot": self.updateRobot,
|
||||||
"status": self.robot_app.get_status,
|
"status": self.get_status,
|
||||||
},
|
},
|
||||||
statusPanel={
|
statusPanel={
|
||||||
"status": self.robot_app.get_status,
|
"status": self.get_status,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
mainWindow.setWindowTitle("ROBOT GUI")
|
||||||
|
mainWindow.show()
|
||||||
|
|
||||||
|
sys.exit(app.exec_())
|
||||||
|
|
||||||
def startRobot(self):
|
def startRobot(self):
|
||||||
self.robot_app = SocketRobotArm()
|
self.robot_app = SocketRobotArm()
|
||||||
|
|
Loading…
Reference in New Issue