Add biohead controls and align task flow interactions with point planning
This commit is contained in:
parent
270afd0fb3
commit
4f8d153ac3
@ -209,7 +209,8 @@ list(APPEND CMVR_SOURCES
|
||||
src/touchplanner.cpp src/touchplanner.h
|
||||
src/touchsequencecontroller.cpp src/touchsequencecontroller.h
|
||||
src/touchvideowidget.cpp src/touchvideowidget.h
|
||||
src/touchplanestore.cpp src/touchplanestore.h src/cameracapabilities.h
|
||||
src/touchplanestore.cpp src/touchplanestore.h src/cameracapabilities.h src/bioheadoperations.h
|
||||
src/bioheadwidget.cpp src/bioheadwidget.h
|
||||
src/touchoperationwidget.cpp src/touchoperationwidget.h
|
||||
src/handeyemotionwidget.cpp src/handeyemotionwidget.h
|
||||
src/tcpcalibration.cpp src/tcpcalibration.h
|
||||
|
||||
@ -366,4 +366,6 @@ win32:RC_ICONS = icons/cmvr-app.ico
|
||||
HEADERS += src/calibrationfile.h
|
||||
|
||||
SOURCES += src/touchplanestore.cpp
|
||||
HEADERS += src/touchplanestore.h src/cameracapabilities.h
|
||||
HEADERS += src/touchplanestore.h src/cameracapabilities.h src/bioheadoperations.h
|
||||
SOURCES += src/bioheadwidget.cpp
|
||||
HEADERS += src/bioheadwidget.h
|
||||
|
||||
@ -297,6 +297,12 @@ Proto 约定和目录说明见 [protos/README.md](protos/README.md)。
|
||||
|
||||
## 软件设置与界面主题
|
||||
|
||||
仿生头页面默认显示「常用操作」:从已连接设备中选择仿生头后,可直接调用预设表情、说话动作、停止和检查设备,并查看调用反馈;原有 JSON 调试界面保留在「全部接口」。断开连接或无可用设备时,快捷操作不可用。
|
||||
|
||||
任务编排的画布操作与机械臂点位规划一致:选中节点后在同层后方插入;点击「添加到选中循环」后向循环末尾添加,点击「切换到根流程末尾」返回根流程。右侧标签实时显示添加位置。拖动仅调整同层顺序,右键拖动画布,双击编辑,循环右上角按钮折叠或展开。实线连接循环与子节点,蓝色虚线连接同层的先后节点。执行中禁止编辑节点;空循环在执行前报错,已有 JSON 数据格式保持兼容。
|
||||
|
||||
任务编排可根据已连接设备列表选择仿生头,支持高兴、惊讶、疲惫、愤怒、悲伤、打哈欠、开始/停止说话、紧急停止及检查设备。动作使用服务端预设,不要求手写 JSON。节点检查 gRPC 结果和反馈头中的业务成功标志,失败停止编排;成功后等待设定时间再继续。开始说话仅启动持续动作,不播放音频;需用停止说话节点结束。服务端急停不保证停止说话线程,检查设备也不代表已获得实际运动状态。自定义姿态及流式表情因服务端字段实现不完整,暂不提供快捷编排入口。
|
||||
|
||||
侧栏底部的「软件设置」用于调整界面字体、基础字号、启动页面、窗口大小与位置记忆、启动最大化、日志保留行数和自动滚动。字体与日志设置保存后立即生效,启动选项下次启动生效。
|
||||
|
||||
「启动与机械臂」中的模型预览开关默认关闭,保存后重启程序生效。关闭时不初始化 Qt3D 场景,机械臂连接、遥测和位置控制照常使用。该开关允许用户选择是否加载模型,并不修复特定 Qt3D/显卡组合的渲染兼容性问题。
|
||||
|
||||
66
src/bioheadoperations.h
Normal file
66
src/bioheadoperations.h
Normal file
@ -0,0 +1,66 @@
|
||||
#pragma once
|
||||
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonParseError>
|
||||
#include <QStringList>
|
||||
|
||||
// Only operations implemented by cmvr-es's BioHead driver are advertised.
|
||||
// SetExpression is partial and StreamExpression has different field mappings;
|
||||
// neither is presented as a complete custom-pose API.
|
||||
namespace BioHeadOperations {
|
||||
inline QStringList actions()
|
||||
{
|
||||
return {QStringLiteral("高兴"), QStringLiteral("惊讶"),
|
||||
QStringLiteral("疲惫"), QStringLiteral("愤怒"),
|
||||
QStringLiteral("悲伤"), QStringLiteral("打哈欠"),
|
||||
QStringLiteral("开始说话"), QStringLiteral("停止说话"),
|
||||
QStringLiteral("紧急停止"), QStringLiteral("检查设备")};
|
||||
}
|
||||
|
||||
inline QString method(const QString &action)
|
||||
{
|
||||
const QStringList methods = {QStringLiteral("Happy"), QStringLiteral("Surprise"),
|
||||
QStringLiteral("ExpressionTired"), QStringLiteral("ExpressionAngry"),
|
||||
QStringLiteral("ExpressionSadness"), QStringLiteral("ExpressionYawn"),
|
||||
QStringLiteral("SpeakStart"), QStringLiteral("SpeakStop"),
|
||||
QStringLiteral("EmergencyStop"), QStringLiteral("GetSystemStatus")};
|
||||
const int index = actions().indexOf(action);
|
||||
return index >= 0 ? methods.at(index) : QString();
|
||||
}
|
||||
|
||||
inline QString hint(const QString &action)
|
||||
{
|
||||
if (action == QStringLiteral("开始说话"))
|
||||
return QStringLiteral("启动服务端持续说话动作,不播放音频。接口确认后开始计时,可添加“停止说话”节点结束动作。");
|
||||
if (action == QStringLiteral("紧急停止"))
|
||||
return QStringLiteral("调用服务端急停并保持当前姿态;服务端未保证此操作会结束持续说话线程,需要结束说话时请使用“停止说话”。");
|
||||
if (action == QStringLiteral("检查设备"))
|
||||
return QStringLiteral("仅检查设备是否能被服务端找到;当前服务端没有返回真实运动状态或舵机位置。");
|
||||
return QStringLiteral("使用服务端预设动作,无需填写舵机参数。接口返回成功仅代表调用完成,不代表物理动作已到位;请按需要设置完成后等待时间。");
|
||||
}
|
||||
|
||||
inline bool responseSucceeded(bool transportSuccess, const QString &json, QString *detail)
|
||||
{
|
||||
if (!transportSuccess) {
|
||||
*detail = json;
|
||||
return false;
|
||||
}
|
||||
QJsonParseError error;
|
||||
const QJsonDocument document = QJsonDocument::fromJson(json.toUtf8(), &error);
|
||||
if (error.error != QJsonParseError::NoError || !document.isObject()) {
|
||||
*detail = QStringLiteral("仿生头接口返回了无效反馈");
|
||||
return false;
|
||||
}
|
||||
const QJsonObject header = document.object().value(QStringLiteral("header")).toObject();
|
||||
// A proto3 false value can be omitted from JSON; missing is not success.
|
||||
if (!header.value(QStringLiteral("success")).toBool(false)) {
|
||||
*detail = header.value(QStringLiteral("errorMessage")).toString();
|
||||
if (detail->isEmpty()) *detail = header.value(QStringLiteral("error_message")).toString();
|
||||
if (detail->isEmpty()) *detail = QStringLiteral("仿生头接口未返回业务成功标志");
|
||||
return false;
|
||||
}
|
||||
*detail = QStringLiteral("服务端已确认调用");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
225
src/bioheadwidget.cpp
Normal file
225
src/bioheadwidget.cpp
Normal file
@ -0,0 +1,225 @@
|
||||
#include "bioheadwidget.h"
|
||||
#include "bioheadoperations.h"
|
||||
#include "applicationstyle.h"
|
||||
#include "robotclient.h"
|
||||
#include "rpcservicewidget.h"
|
||||
|
||||
#include <QComboBox>
|
||||
#include <QDateTime>
|
||||
#include <QFrame>
|
||||
#include <QGridLayout>
|
||||
#include <QHBoxLayout>
|
||||
#include <QJsonDocument>
|
||||
#include <QLabel>
|
||||
#include <QListWidget>
|
||||
#include <QPushButton>
|
||||
#include <QSignalBlocker>
|
||||
#include <QTabWidget>
|
||||
#include <QToolButton>
|
||||
#include <QVBoxLayout>
|
||||
|
||||
BioHeadWidget::BioHeadWidget(RobotClient *client, QWidget *parent)
|
||||
: QWidget(parent), m_client(client)
|
||||
{
|
||||
setObjectName(QStringLiteral("bioheadPage"));
|
||||
auto *layout = new QVBoxLayout(this);
|
||||
layout->setContentsMargins(16, 12, 16, 14);
|
||||
auto *tabs = new QTabWidget(this);
|
||||
tabs->setObjectName(QStringLiteral("serviceTabs"));
|
||||
auto *controls = new QWidget(tabs);
|
||||
auto *content = new QVBoxLayout(controls);
|
||||
content->setContentsMargins(0, 12, 0, 0);
|
||||
content->setSpacing(12);
|
||||
auto *header = new QFrame(controls);
|
||||
header->setObjectName(QStringLiteral("panel"));
|
||||
auto *headerLayout = new QHBoxLayout(header);
|
||||
auto *title = new QLabel(QStringLiteral("仿生头控制"), header);
|
||||
title->setObjectName(QStringLiteral("sectionTitle"));
|
||||
headerLayout->addWidget(title);
|
||||
headerLayout->addStretch();
|
||||
headerLayout->addWidget(new QLabel(QStringLiteral("设备"), header));
|
||||
m_device = new QComboBox(header);
|
||||
m_device->setObjectName(QStringLiteral("bioheadDeviceCombo"));
|
||||
m_device->setMinimumWidth(220);
|
||||
ApplicationStyle::styleComboBox(m_device);
|
||||
headerLayout->addWidget(m_device);
|
||||
content->addWidget(header);
|
||||
|
||||
auto *actionPanels = new QHBoxLayout;
|
||||
actionPanels->setSpacing(12);
|
||||
auto addPanel = [&](const QString &heading, const QString &description,
|
||||
const QStringList &actions, int columns) {
|
||||
auto *panel = new QFrame(controls);
|
||||
panel->setObjectName(QStringLiteral("panel"));
|
||||
auto *box = new QVBoxLayout(panel);
|
||||
box->setContentsMargins(18, 16, 18, 18);
|
||||
box->setSpacing(16);
|
||||
auto *headingRow = new QHBoxLayout;
|
||||
auto *label = new QLabel(heading, panel);
|
||||
label->setObjectName(QStringLiteral("sectionTitle"));
|
||||
headingRow->addWidget(label);
|
||||
headingRow->addStretch();
|
||||
auto *help = new QToolButton(panel);
|
||||
help->setObjectName(QStringLiteral("bioheadHelp"));
|
||||
help->setText(QStringLiteral("?"));
|
||||
help->setAccessibleName(heading + QStringLiteral("使用说明"));
|
||||
help->setToolTip(description);
|
||||
headingRow->addWidget(help);
|
||||
box->addLayout(headingRow);
|
||||
auto *grid = new QGridLayout;
|
||||
grid->setSpacing(12);
|
||||
for (int i = 0; i < actions.size(); ++i) {
|
||||
const QString action = actions.at(i);
|
||||
auto *button = new QPushButton(action, panel);
|
||||
button->setObjectName(action == QStringLiteral("紧急停止")
|
||||
? QStringLiteral("dangerButton") : action == QStringLiteral("开始说话")
|
||||
? QStringLiteral("primaryButton") : QStringLiteral("secondaryButton"));
|
||||
button->setProperty("bioheadAction", action);
|
||||
button->setProperty("bioheadPreset", actions.size() == 6);
|
||||
button->setMinimumHeight(56);
|
||||
button->setToolTip(BioHeadOperations::hint(action));
|
||||
connect(button, &QPushButton::clicked, this, [this, action]() { invoke(action); });
|
||||
grid->addWidget(button, i / columns, i % columns);
|
||||
m_buttons.append(button);
|
||||
}
|
||||
box->addLayout(grid);
|
||||
actionPanels->addWidget(panel, actions.size() == 6 ? 3 : 2);
|
||||
};
|
||||
addPanel(QStringLiteral("预设表情"),
|
||||
QStringLiteral("直接调用服务端预设动作。接口确认不代表舵机已到位,当前不提供实际姿态遥测。"),
|
||||
BioHeadOperations::actions().mid(0, 6), 3);
|
||||
addPanel(QStringLiteral("动作控制"),
|
||||
QStringLiteral("开始说话会持续执行嘴部动作,不播放音频。请使用“停止说话”结束;急停保持姿态,但服务端未保证终止说话线程。"),
|
||||
{QStringLiteral("开始说话"), QStringLiteral("停止说话"),
|
||||
QStringLiteral("紧急停止"), QStringLiteral("检查设备")}, 2);
|
||||
content->addLayout(actionPanels);
|
||||
|
||||
auto *feedback = new QFrame(controls);
|
||||
feedback->setObjectName(QStringLiteral("panel"));
|
||||
auto *feedbackLayout = new QVBoxLayout(feedback);
|
||||
auto *feedbackHeader = new QHBoxLayout;
|
||||
auto *feedbackTitle = new QLabel(QStringLiteral("操作记录"), feedback);
|
||||
feedbackTitle->setObjectName(QStringLiteral("sectionTitle"));
|
||||
feedbackHeader->addWidget(feedbackTitle);
|
||||
feedbackHeader->addStretch();
|
||||
auto *clear = new QPushButton(QStringLiteral("清空记录"), feedback);
|
||||
clear->setObjectName(QStringLiteral("secondaryButton"));
|
||||
feedbackHeader->addWidget(clear);
|
||||
feedbackLayout->addLayout(feedbackHeader);
|
||||
m_status = new QLabel(QStringLiteral("未连接"), feedback);
|
||||
m_status->setToolTip(QStringLiteral("请连接服务并刷新设备列表。调用反馈仅代表接口结果,不代表实际运动状态。"));
|
||||
m_status->setObjectName(QStringLiteral("bioheadStatus"));
|
||||
m_status->setWordWrap(true);
|
||||
m_status->setTextInteractionFlags(Qt::TextSelectableByMouse);
|
||||
feedbackLayout->addWidget(m_status);
|
||||
m_history = new QListWidget(feedback);
|
||||
m_history->setObjectName(QStringLiteral("bioheadHistory"));
|
||||
m_history->setSelectionMode(QAbstractItemView::ExtendedSelection);
|
||||
feedbackLayout->addWidget(m_history, 1);
|
||||
connect(clear, &QPushButton::clicked, m_history, &QListWidget::clear);
|
||||
content->addWidget(feedback, 1);
|
||||
|
||||
tabs->addTab(controls, QStringLiteral("常用操作"));
|
||||
m_rpc = new RpcServiceWidget(QStringLiteral("cmvr.api.BioHeadService"), client, tabs);
|
||||
tabs->addTab(m_rpc, QStringLiteral("全部接口"));
|
||||
layout->addWidget(tabs);
|
||||
connect(m_device, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [this](int) {
|
||||
m_rpc->setDeviceId(m_device->currentData().toString());
|
||||
refreshControls();
|
||||
});
|
||||
connect(client, &RobotClient::systemStatusReceived, this,
|
||||
[this](double, double, double, const QVariantList &devices) { setAvailableDevices(devices); });
|
||||
connect(client, &RobotClient::connectionChanged, this, [this](bool connected, int, const QString &) {
|
||||
m_connected = connected;
|
||||
if (!connected) {
|
||||
m_pendingMethod.clear();
|
||||
m_pendingDevice.clear();
|
||||
setAvailableDevices({});
|
||||
m_status->setText(QStringLiteral("连接已断开"));
|
||||
}
|
||||
refreshControls();
|
||||
});
|
||||
connect(client, &RobotClient::rpcFinished, this,
|
||||
[this](const QString &service, const QString &method, bool success, const QString &json) {
|
||||
if (service != QStringLiteral("cmvr.api.BioHeadService")
|
||||
|| m_pendingMethod.isEmpty() || method != m_pendingMethod) return;
|
||||
QString detail;
|
||||
const bool accepted = BioHeadOperations::responseSucceeded(success, json, &detail);
|
||||
const QString message = QStringLiteral("%1 · %2:%3")
|
||||
.arg(m_pendingDevice, m_pendingAction, detail);
|
||||
m_pendingMethod.clear();
|
||||
m_pendingDevice.clear();
|
||||
m_status->setText(message);
|
||||
addFeedback(accepted, message);
|
||||
refreshControls();
|
||||
});
|
||||
refreshControls();
|
||||
}
|
||||
|
||||
void BioHeadWidget::setAvailableDevices(const QVariantList &devices)
|
||||
{
|
||||
QStringList ids;
|
||||
for (const auto &value : devices) {
|
||||
const QVariantMap device = value.toMap();
|
||||
const QString id = device.value(QStringLiteral("deviceId")).toString();
|
||||
if (device.value(QStringLiteral("type"), -1).toInt() == 8 && !id.isEmpty()
|
||||
&& !ids.contains(id)) ids.append(id);
|
||||
}
|
||||
QStringList existing;
|
||||
for (int i = 0; i < m_device->count(); ++i)
|
||||
existing.append(m_device->itemData(i).toString());
|
||||
// Periodic system refreshes must not erase the most recent action result.
|
||||
if (ids == existing) return;
|
||||
const QString previous = m_device->currentData().toString();
|
||||
QSignalBlocker blocker(m_device);
|
||||
m_device->clear();
|
||||
for (const auto &id : ids) m_device->addItem(id, id);
|
||||
const int index = m_device->findData(previous);
|
||||
if (index >= 0) m_device->setCurrentIndex(index);
|
||||
m_rpc->setDeviceId(m_device->currentData().toString());
|
||||
if (m_pendingMethod.isEmpty())
|
||||
m_status->setText(m_device->count() ? QStringLiteral("待操作")
|
||||
: QStringLiteral("未发现仿生头设备"));
|
||||
refreshControls();
|
||||
}
|
||||
|
||||
void BioHeadWidget::setDeviceId(const QString &deviceId)
|
||||
{
|
||||
m_device->setCurrentIndex(m_device->findData(deviceId));
|
||||
m_rpc->setDeviceId(deviceId);
|
||||
}
|
||||
|
||||
void BioHeadWidget::refreshControls()
|
||||
{
|
||||
const bool enabled = m_connected && !m_device->currentData().toString().isEmpty()
|
||||
&& m_pendingMethod.isEmpty();
|
||||
for (auto *button : m_buttons) button->setEnabled(enabled);
|
||||
m_device->setEnabled(m_pendingMethod.isEmpty());
|
||||
}
|
||||
|
||||
void BioHeadWidget::invoke(const QString &action)
|
||||
{
|
||||
if (!m_connected || !m_pendingMethod.isEmpty()) return;
|
||||
const QString device = m_device->currentData().toString();
|
||||
const QString method = BioHeadOperations::method(action);
|
||||
if (device.isEmpty() || method.isEmpty()) return;
|
||||
m_pendingMethod = method;
|
||||
m_pendingAction = action;
|
||||
m_pendingDevice = device;
|
||||
m_status->setText(QStringLiteral("正在请求 %1 · %2…").arg(device, action));
|
||||
refreshControls();
|
||||
const QJsonObject request{{QStringLiteral("header"),
|
||||
QJsonObject{{QStringLiteral("deviceId"), device}}}};
|
||||
m_client->requestRpc(QStringLiteral("cmvr.api.BioHeadService"), method,
|
||||
QString::fromUtf8(QJsonDocument(request).toJson(QJsonDocument::Compact)), device);
|
||||
}
|
||||
|
||||
void BioHeadWidget::addFeedback(bool success, const QString &message)
|
||||
{
|
||||
m_history->addItem(QStringLiteral("%1 %2 %3")
|
||||
.arg(QDateTime::currentDateTime().toString(QStringLiteral("HH:mm:ss")),
|
||||
success ? QStringLiteral("✓") : QStringLiteral("✕"), message));
|
||||
while (m_history->count() > 200) delete m_history->takeItem(0);
|
||||
m_history->scrollToBottom();
|
||||
emit statusMessage(success ? QStringLiteral("OK") : QStringLiteral("ERROR"), message);
|
||||
}
|
||||
40
src/bioheadwidget.h
Normal file
40
src/bioheadwidget.h
Normal file
@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
|
||||
#include <QWidget>
|
||||
#include <QVariantList>
|
||||
#include <QList>
|
||||
|
||||
class RobotClient;
|
||||
class RpcServiceWidget;
|
||||
class QComboBox;
|
||||
class QLabel;
|
||||
class QListWidget;
|
||||
class QPushButton;
|
||||
|
||||
class BioHeadWidget final : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit BioHeadWidget(RobotClient *client, QWidget *parent = nullptr);
|
||||
void setDeviceId(const QString &deviceId);
|
||||
void setAvailableDevices(const QVariantList &devices);
|
||||
|
||||
signals:
|
||||
void statusMessage(const QString &level, const QString &message);
|
||||
|
||||
private:
|
||||
void invoke(const QString &action);
|
||||
void refreshControls();
|
||||
void addFeedback(bool success, const QString &message);
|
||||
|
||||
RobotClient *m_client;
|
||||
RpcServiceWidget *m_rpc;
|
||||
QComboBox *m_device;
|
||||
QLabel *m_status;
|
||||
QListWidget *m_history;
|
||||
QList<QPushButton *> m_buttons;
|
||||
bool m_connected = false;
|
||||
QString m_pendingMethod;
|
||||
QString m_pendingAction;
|
||||
QString m_pendingDevice;
|
||||
};
|
||||
@ -4,6 +4,8 @@
|
||||
#include "tactileheatmapwidget.h"
|
||||
#include "jointfloweditor.h"
|
||||
#include "taskorchestrationwidget.h"
|
||||
#include "bioheadoperations.h"
|
||||
#include "bioheadwidget.h"
|
||||
#include "videorenderwidget.h"
|
||||
|
||||
#include "agvmapviewwidget.h"
|
||||
@ -515,7 +517,9 @@ MainWindow::MainWindow(QWidget *parent)
|
||||
m_pages->addWidget(createAudioPage(true));
|
||||
m_pages->addWidget(createAudioPage(false));
|
||||
m_pages->addWidget(createDexHandPage());
|
||||
m_pages->addWidget(createRpcPage(QStringLiteral("cmvr.api.BioHeadService")));
|
||||
auto *bioheadPage = new BioHeadWidget(m_client, m_pages);
|
||||
connect(bioheadPage, &BioHeadWidget::statusMessage, this, &MainWindow::appendLog);
|
||||
m_pages->addWidget(bioheadPage);
|
||||
m_pages->addWidget(createRpcPage(QStringLiteral("cmvr.api.HlcService")));
|
||||
m_pages->addWidget(createRpcPage(QStringLiteral("cmvr.api.TestService")));
|
||||
m_pages->addWidget(createQuicPage());
|
||||
@ -582,6 +586,8 @@ MainWindow::MainWindow(QWidget *parent)
|
||||
return;
|
||||
if (auto *rpcPage = qobject_cast<RpcServiceWidget *>(m_pages->widget(pageIndex)))
|
||||
rpcPage->setDeviceId(deviceId);
|
||||
if (auto *headPage = qobject_cast<BioHeadWidget *>(m_pages->widget(pageIndex)))
|
||||
headPage->setDeviceId(deviceId);
|
||||
selectPage(pageIndex);
|
||||
});
|
||||
m_pages->addWidget(createDiagnosticsPage());
|
||||
@ -812,6 +818,16 @@ MainWindow::MainWindow(QWidget *parent)
|
||||
dispatched = true;
|
||||
m_client->stopMicrophoneAudioStream();
|
||||
}
|
||||
} else if (deviceType == QStringLiteral("仿生头")) {
|
||||
const QString method = BioHeadOperations::method(action);
|
||||
if (!method.isEmpty()) {
|
||||
dispatched = true;
|
||||
const QJsonObject request{{QStringLiteral("header"),
|
||||
QJsonObject{{QStringLiteral("deviceId"), deviceId}}}};
|
||||
m_client->requestRpc(QStringLiteral("cmvr.api.BioHeadService"), method,
|
||||
QString::fromUtf8(QJsonDocument(request).toJson(QJsonDocument::Compact)),
|
||||
deviceId);
|
||||
}
|
||||
} else if (deviceType == QStringLiteral("通用RPC")) {
|
||||
dispatched = true;
|
||||
m_client->requestRpc(parameters.value(QStringLiteral("service")).toString(),
|
||||
|
||||
@ -2695,6 +2695,7 @@ public:
|
||||
if (m_demoMode) {
|
||||
QJsonObject result;
|
||||
result.insert(QStringLiteral("demo"), true);
|
||||
result.insert(QStringLiteral("header"), QJsonObject{{QStringLiteral("success"), true}});
|
||||
result.insert(QStringLiteral("service"), serviceName);
|
||||
result.insert(QStringLiteral("method"), methodName);
|
||||
result.insert(QStringLiteral("deviceId"), deviceId);
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
#include "audiostreammanager.h"
|
||||
#include "applicationstyle.h"
|
||||
#include "cameracapabilities.h"
|
||||
#include "bioheadoperations.h"
|
||||
#include "uiicons.h"
|
||||
|
||||
#include <QApplication>
|
||||
@ -36,15 +37,30 @@
|
||||
#include <QSaveFile>
|
||||
#include <QScrollBar>
|
||||
#include <QSpinBox>
|
||||
#include <QSignalBlocker>
|
||||
#include <QStackedWidget>
|
||||
#include <QTimer>
|
||||
#include <QTreeWidget>
|
||||
#include <QVBoxLayout>
|
||||
#include <functional>
|
||||
#include <algorithm>
|
||||
|
||||
namespace {
|
||||
enum Roles { KindRole = Qt::UserRole, DeviceTypeRole, DeviceIdRole, ActionRole,
|
||||
ParametersRole, WaitRole, WaitUnitRole, LoopCountRole, RunStateRole };
|
||||
ParametersRole, WaitRole, WaitUnitRole, LoopCountRole, RunStateRole, CollapsedRole };
|
||||
|
||||
void restoreTaskExpansion(QTreeWidgetItem *item)
|
||||
{
|
||||
if (item->data(0, KindRole).toString() == QStringLiteral("loop"))
|
||||
item->setExpanded(!item->data(0, CollapsedRole).toBool());
|
||||
for (int i = 0; i < item->childCount(); ++i) restoreTaskExpansion(item->child(i));
|
||||
}
|
||||
|
||||
void captureTaskExpansion(QTreeWidgetItem *item)
|
||||
{
|
||||
item->setData(0, CollapsedRole, !item->isExpanded());
|
||||
for (int i = 0; i < item->childCount(); ++i) captureTaskExpansion(item->child(i));
|
||||
}
|
||||
|
||||
constexpr qreal kTaskCardWidth = 270.0;
|
||||
constexpr qreal kTaskCardHeight = 94.0;
|
||||
@ -95,7 +111,9 @@ public:
|
||||
: m_source(source), m_title(title), m_subtitle(subtitle), m_loop(loop),
|
||||
m_moved(std::move(moved)), m_edited(std::move(edited)), m_toggled(std::move(toggled))
|
||||
{
|
||||
setFlags(ItemIsMovable | ItemIsSelectable);
|
||||
setFlags(ItemIsMovable | ItemIsSelectable | ItemSendsGeometryChanges);
|
||||
setToolTip(title + QStringLiteral("\n") + subtitle
|
||||
+ (loop ? QStringLiteral("\n右上角折叠;选中后点击“添加到选中循环”可添加子节点") : QString()));
|
||||
setAcceptHoverEvents(true);
|
||||
setCursor(Qt::OpenHandCursor);
|
||||
setZValue(2);
|
||||
@ -121,9 +139,11 @@ public:
|
||||
QFont titleFont = painter->font(); titleFont.setBold(true);
|
||||
painter->setFont(titleFont); painter->setPen(QColor("#25313d"));
|
||||
painter->drawText(QRectF(14, 9, kTaskCardWidth - (m_loop ? 52 : 28), 27),
|
||||
Qt::AlignLeft | Qt::AlignVCenter, m_title);
|
||||
Qt::AlignLeft | Qt::AlignVCenter,
|
||||
painter->fontMetrics().elidedText(m_title, Qt::ElideRight,
|
||||
int(kTaskCardWidth - (m_loop ? 52 : 28))));
|
||||
painter->setFont(QFont()); painter->setPen(QColor("#687684"));
|
||||
painter->drawText(QRectF(14, 38, kTaskCardWidth - 28, 45),
|
||||
painter->drawText(QRectF(14, 38, kTaskCardWidth - (m_loop ? 52 : 28), 45),
|
||||
Qt::AlignLeft | Qt::AlignVCenter | Qt::TextWordWrap, m_subtitle);
|
||||
if (m_loop) {
|
||||
painter->setPen(QPen(QColor("#b8c7d8"), 1.2)); painter->setBrush(Qt::white);
|
||||
@ -140,21 +160,41 @@ public:
|
||||
protected:
|
||||
void mousePressEvent(QGraphicsSceneMouseEvent *event) override
|
||||
{
|
||||
m_startPos = pos();
|
||||
m_startMouse = event->screenPos();
|
||||
m_buttonPress = false;
|
||||
if (m_loop && toggleRect().contains(event->pos())) {
|
||||
m_buttonPress = true;
|
||||
if (m_toggled) m_toggled(m_source);
|
||||
event->accept(); return;
|
||||
}
|
||||
setCursor(Qt::ClosedHandCursor);
|
||||
m_dragging = true;
|
||||
QGraphicsItem::mousePressEvent(event);
|
||||
}
|
||||
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override
|
||||
{
|
||||
setCursor(Qt::OpenHandCursor);
|
||||
m_dragging = false;
|
||||
if (m_buttonPress) { m_buttonPress = false; event->accept(); return; }
|
||||
QGraphicsItem::mouseReleaseEvent(event);
|
||||
if (m_moved) m_moved(m_source, pos());
|
||||
if ((event->screenPos() - m_startMouse).manhattanLength() >= QApplication::startDragDistance()) {
|
||||
if (m_moved) m_moved(m_source, pos());
|
||||
} else setPos(m_startPos);
|
||||
}
|
||||
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override
|
||||
{ if (m_edited) m_edited(m_source); event->accept(); }
|
||||
{
|
||||
if (!(m_loop && toggleRect().contains(event->pos())))
|
||||
if (m_edited) m_edited(m_source);
|
||||
event->accept();
|
||||
}
|
||||
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override
|
||||
{
|
||||
if (change == ItemPositionChange && m_dragging) {
|
||||
QPointF position = value.toPointF(); position.setY(m_startPos.y()); return position;
|
||||
}
|
||||
return QGraphicsItem::itemChange(change, value);
|
||||
}
|
||||
|
||||
private:
|
||||
QRectF toggleRect() const { return QRectF(kTaskCardWidth - 35, 10, 22, 22); }
|
||||
@ -164,8 +204,13 @@ private:
|
||||
std::function<void(QTreeWidgetItem *, const QPointF &)> m_moved;
|
||||
std::function<void(QTreeWidgetItem *)> m_edited;
|
||||
std::function<void(QTreeWidgetItem *)> m_toggled;
|
||||
QPointF m_startPos;
|
||||
QPoint m_startMouse;
|
||||
bool m_buttonPress = false;
|
||||
bool m_dragging = false;
|
||||
};
|
||||
|
||||
|
||||
class TaskNodeDialog final : public QDialog
|
||||
{
|
||||
public:
|
||||
@ -270,6 +315,8 @@ void showTaskNotice(QWidget *parent, const QString &title, const QString &messag
|
||||
|
||||
QStringList actionsForType(const QString &type)
|
||||
{
|
||||
if (type == QStringLiteral("仿生头"))
|
||||
return BioHeadOperations::actions();
|
||||
if (type == QStringLiteral("机械臂"))
|
||||
return {QStringLiteral("MoveJ"), QStringLiteral("使能"), QStringLiteral("去使能"),
|
||||
QStringLiteral("停止"), QStringLiteral("清除故障")};
|
||||
@ -302,6 +349,7 @@ QString deviceTypeName(int type)
|
||||
case 3: return QStringLiteral("灵巧手");
|
||||
case 7: return QStringLiteral("扬声器");
|
||||
case 5: return QStringLiteral("麦克风");
|
||||
case 8: return QStringLiteral("仿生头");
|
||||
default: return QStringLiteral("通用RPC");
|
||||
}
|
||||
}
|
||||
@ -585,6 +633,10 @@ bool editTaskDialog(QWidget *parent, QString &name, QString &type, QString &devi
|
||||
form.addRow(QStringLiteral("选择设备"), &deviceCombo);
|
||||
form.addRow(QStringLiteral("设备类型"), &typeValue);
|
||||
form.addRow(QStringLiteral("执行操作"), &actionCombo);
|
||||
QLabel bioheadHint(&dialog);
|
||||
bioheadHint.setWordWrap(true);
|
||||
bioheadHint.setObjectName(QStringLiteral("mutedLabel"));
|
||||
form.addRow(&bioheadHint);
|
||||
form.addRow(¶meterLabel, ¶meterPages);
|
||||
form.addRow(QStringLiteral("完成后等待"), &waitEditor);
|
||||
layout->addLayout(&form);
|
||||
@ -592,6 +644,8 @@ bool editTaskDialog(QWidget *parent, QString &name, QString &type, QString &devi
|
||||
auto refreshParameterPage = [&]() {
|
||||
const QString selectedType = typeValue.text();
|
||||
const QString selectedAction = actionCombo.currentText();
|
||||
bioheadHint.setVisible(selectedType == QStringLiteral("仿生头"));
|
||||
bioheadHint.setText(BioHeadOperations::hint(selectedAction));
|
||||
int page = 0;
|
||||
if (selectedType == QStringLiteral("机械臂") && selectedAction == QStringLiteral("MoveJ")) page = 1;
|
||||
else if (selectedType == QStringLiteral("AGV") && selectedAction == QStringLiteral("导航站点")) page = 3;
|
||||
@ -755,7 +809,7 @@ TaskOrchestrationWidget::TaskOrchestrationWidget(QWidget *parent) : QWidget(pare
|
||||
groups->addWidget(title); groups->addStretch(); groups->addWidget(m_groupCombo);
|
||||
groups->addWidget(newGroup); groups->addWidget(renameGroup);
|
||||
layout->addLayout(groups);
|
||||
QLabel *hint = new QLabel(QStringLiteral("任务节点可来自不同设备;循环节点可嵌套任务。拖动节点可调整顺序或放入循环。"), panel);
|
||||
QLabel *hint = new QLabel(QStringLiteral("选中节点后可在其后插入,拖动可调整同级执行顺序。右键拖动画布,双击编辑节点。"), panel);
|
||||
hint->setObjectName(QStringLiteral("mutedLabel"));
|
||||
layout->addWidget(hint);
|
||||
QHBoxLayout *toolbar = new QHBoxLayout;
|
||||
@ -771,6 +825,28 @@ TaskOrchestrationWidget::TaskOrchestrationWidget(QWidget *parent) : QWidget(pare
|
||||
toolbar->addWidget(removeButton); toolbar->addSpacing(12); toolbar->addWidget(upButton);
|
||||
toolbar->addWidget(downButton); toolbar->addStretch();
|
||||
layout->addLayout(toolbar);
|
||||
auto *insertionToolbar = new QHBoxLayout;
|
||||
auto *rootInsertion = new QPushButton(QStringLiteral("切换到根流程末尾"), panel);
|
||||
auto *loopInsertion = new QPushButton(QStringLiteral("添加到选中循环"), panel);
|
||||
rootInsertion->setObjectName(QStringLiteral("secondaryButton"));
|
||||
loopInsertion->setObjectName(QStringLiteral("secondaryButton"));
|
||||
insertionToolbar->addWidget(rootInsertion);
|
||||
insertionToolbar->addWidget(loopInsertion);
|
||||
insertionToolbar->addStretch();
|
||||
m_insertionHint = new QLabel(panel);
|
||||
m_insertionHint->setObjectName(QStringLiteral("jointFlowContextChip"));
|
||||
m_insertionHint->setWordWrap(false);
|
||||
m_insertionHint->setMaximumWidth(560);
|
||||
m_insertionHint->setToolTip(QStringLiteral("与点位规划相同:选中节点后在同层后方插入,进入循环需点击“添加到选中循环”。"));
|
||||
insertionToolbar->addWidget(m_insertionHint);
|
||||
m_editControls = {newGroup, renameGroup, m_groupCombo, addTaskButton, addLoopButton,
|
||||
editButton, removeButton, upButton, downButton, rootInsertion, loopInsertion};
|
||||
layout->addLayout(insertionToolbar);
|
||||
connect(rootInsertion, &QPushButton::clicked, this, &TaskOrchestrationWidget::setInsertionAtRoot);
|
||||
connect(loopInsertion, &QPushButton::clicked, this, [this]() {
|
||||
if (!useSelectedLoopAsInsertionParent())
|
||||
showTaskNotice(this, QStringLiteral("添加到循环"), QStringLiteral("请先选中一个循环节点。"));
|
||||
});
|
||||
m_tree = new QTreeWidget(panel);
|
||||
m_tree->setColumnCount(6);
|
||||
m_tree->setHeaderLabels({QStringLiteral("节点名称"), QStringLiteral("类型"),
|
||||
@ -840,7 +916,12 @@ TaskOrchestrationWidget::TaskOrchestrationWidget(QWidget *parent) : QWidget(pare
|
||||
if (!selected.isEmpty()) {
|
||||
if (TaskFlowCard *card = dynamic_cast<TaskFlowCard *>(selected.first()))
|
||||
m_tree->setCurrentItem(card->source());
|
||||
}
|
||||
} else m_tree->setCurrentItem(nullptr);
|
||||
QTreeWidgetItem *item = m_tree->currentItem();
|
||||
if (item) m_insertionParent = item->parent();
|
||||
m_insertionIndex = item ? (item->parent() ? item->parent()->indexOfChild(item)
|
||||
: m_tree->indexOfTopLevelItem(item)) + 1 : -1;
|
||||
updateInsertionHint();
|
||||
});
|
||||
connect(m_runButton, &QPushButton::clicked, this, &TaskOrchestrationWidget::startRun);
|
||||
connect(m_stopButton, &QPushButton::clicked, this, [this]() { stopRun(QStringLiteral("已手动停止")); });
|
||||
@ -848,8 +929,15 @@ TaskOrchestrationWidget::TaskOrchestrationWidget(QWidget *parent) : QWidget(pare
|
||||
load();
|
||||
}
|
||||
|
||||
TaskOrchestrationWidget::~TaskOrchestrationWidget()
|
||||
{
|
||||
// Match JointFlowEditor: scene destruction must not call into destroyed members.
|
||||
disconnect(m_flowScene, nullptr, this, nullptr);
|
||||
}
|
||||
|
||||
void TaskOrchestrationWidget::addTask(QTreeWidgetItem *)
|
||||
{
|
||||
if (!m_runSteps.isEmpty()) return;
|
||||
QString name = QStringLiteral("新任务"); QString type = QStringLiteral("机械臂");
|
||||
QString deviceId; QString action = QStringLiteral("MoveJ"); QJsonObject params; int waitMs = 0;
|
||||
QString waitUnit = QStringLiteral("ms");
|
||||
@ -863,34 +951,78 @@ void TaskOrchestrationWidget::addTask(QTreeWidgetItem *)
|
||||
item->setData(0, WaitRole, waitMs); item->setData(0, WaitUnitRole, waitUnit);
|
||||
item->setText(0, name); item->setText(1, type); item->setText(2, type);
|
||||
item->setText(3, deviceId); item->setText(4, action); item->setText(5, waitDisplayText(waitMs, waitUnit));
|
||||
QTreeWidgetItem *selected = m_tree->currentItem();
|
||||
QTreeWidgetItem *parent = selected && selected->data(0, KindRole).toString() == QStringLiteral("loop") ? selected : (selected ? selected->parent() : nullptr);
|
||||
if (selected && selected->data(0, KindRole).toString() != QStringLiteral("loop")) {
|
||||
const int index = parent ? parent->indexOfChild(selected) : m_tree->indexOfTopLevelItem(selected);
|
||||
if (parent) parent->insertChild(index + 1, item); else m_tree->insertTopLevelItem(index + 1, item);
|
||||
} else if (parent) parent->addChild(item); else m_tree->addTopLevelItem(item);
|
||||
m_tree->setCurrentItem(item); save();
|
||||
insertNode(item);
|
||||
}
|
||||
|
||||
void TaskOrchestrationWidget::addLoop(QTreeWidgetItem *)
|
||||
{
|
||||
if (!m_runSteps.isEmpty()) return;
|
||||
QString name = QStringLiteral("循环");
|
||||
int count = 2;
|
||||
if (!editLoopDialog(this, QStringLiteral("新增循环节点"), name, count)) return;
|
||||
QTreeWidgetItem *item = new QTreeWidgetItem;
|
||||
item->setData(0, KindRole, QStringLiteral("loop")); item->setData(0, LoopCountRole, count);
|
||||
item->setText(0, name); item->setText(1, QStringLiteral("循环 × %1").arg(count));
|
||||
item->setExpanded(true); insertNode(item);
|
||||
}
|
||||
|
||||
void TaskOrchestrationWidget::insertNode(QTreeWidgetItem *item)
|
||||
{
|
||||
const int count = m_insertionParent ? m_insertionParent->childCount() : m_tree->topLevelItemCount();
|
||||
const int index = m_insertionIndex < 0 ? count : qBound(0, m_insertionIndex, count);
|
||||
if (m_insertionParent) {
|
||||
m_insertionParent->insertChild(index, item);
|
||||
m_insertionParent->setExpanded(true);
|
||||
} else m_tree->insertTopLevelItem(index, item);
|
||||
if (item->data(0, KindRole).toString() == QStringLiteral("loop")) item->setExpanded(true);
|
||||
m_tree->setCurrentItem(item);
|
||||
m_insertionParent = item->parent();
|
||||
m_insertionIndex = index + 1;
|
||||
save();
|
||||
}
|
||||
|
||||
void TaskOrchestrationWidget::updateInsertionHint()
|
||||
{
|
||||
if (!m_insertionHint) return;
|
||||
QStringList path;
|
||||
for (auto *parent = m_insertionParent; parent; parent = parent->parent()) path.prepend(parent->text(0));
|
||||
path.prepend(QStringLiteral("根流程"));
|
||||
const int count = m_insertionParent ? m_insertionParent->childCount() : m_tree->topLevelItemCount();
|
||||
const int index = m_insertionIndex < 0 ? count : qBound(0, m_insertionIndex, count);
|
||||
QTreeWidgetItem *previous = index > 0 ? (m_insertionParent ? m_insertionParent->child(index - 1)
|
||||
: m_tree->topLevelItem(index - 1)) : nullptr;
|
||||
const QString description = QStringLiteral("添加到:%1%2")
|
||||
.arg(path.join(QStringLiteral(" / ")),
|
||||
m_insertionIndex >= 0 && previous ? QStringLiteral("(插入到“%1”之后)").arg(previous->text(0))
|
||||
: QStringLiteral("(末尾)"));
|
||||
m_insertionHint->setText(m_insertionHint->fontMetrics().elidedText(description, Qt::ElideMiddle, 520));
|
||||
m_insertionHint->setToolTip(description);
|
||||
}
|
||||
|
||||
void TaskOrchestrationWidget::setInsertionAtRoot()
|
||||
{
|
||||
if (!m_runSteps.isEmpty()) return;
|
||||
const QSignalBlocker blocker(m_flowScene);
|
||||
m_flowScene->clearSelection(); m_tree->setCurrentItem(nullptr);
|
||||
m_insertionParent = nullptr; m_insertionIndex = -1;
|
||||
updateInsertionHint();
|
||||
}
|
||||
|
||||
bool TaskOrchestrationWidget::useSelectedLoopAsInsertionParent()
|
||||
{
|
||||
if (!m_runSteps.isEmpty()) return false;
|
||||
QTreeWidgetItem *selected = m_tree->currentItem();
|
||||
QTreeWidgetItem *parent = selected && selected->data(0, KindRole).toString() == QStringLiteral("loop") ? selected : (selected ? selected->parent() : nullptr);
|
||||
if (selected && selected->data(0, KindRole).toString() != QStringLiteral("loop")) {
|
||||
const int index = parent ? parent->indexOfChild(selected) : m_tree->indexOfTopLevelItem(selected);
|
||||
if (parent) parent->insertChild(index + 1, item); else m_tree->insertTopLevelItem(index + 1, item);
|
||||
} else if (parent) parent->addChild(item); else m_tree->addTopLevelItem(item);
|
||||
item->setExpanded(true); m_tree->setCurrentItem(item); save();
|
||||
if (!selected || selected->data(0, KindRole).toString() != QStringLiteral("loop")) return false;
|
||||
const QSignalBlocker blocker(m_flowScene);
|
||||
m_flowScene->clearSelection(); m_tree->setCurrentItem(nullptr);
|
||||
m_insertionParent = selected; m_insertionIndex = -1;
|
||||
updateInsertionHint();
|
||||
return true;
|
||||
}
|
||||
|
||||
void TaskOrchestrationWidget::editSelected()
|
||||
{
|
||||
if (!m_runSteps.isEmpty()) return;
|
||||
QTreeWidgetItem *item = m_tree->currentItem(); if (!item) return;
|
||||
if (item->data(0, KindRole).toString() == QStringLiteral("loop")) {
|
||||
QString name = item->text(0);
|
||||
@ -913,25 +1045,44 @@ void TaskOrchestrationWidget::editSelected()
|
||||
}
|
||||
|
||||
void TaskOrchestrationWidget::removeSelected()
|
||||
{ QTreeWidgetItem *item = m_tree->currentItem(); if (!item) return; delete item; save(); }
|
||||
{
|
||||
if (!m_runSteps.isEmpty()) return;
|
||||
QTreeWidgetItem *item = m_tree->currentItem(); if (!item) return;
|
||||
m_insertionParent = item->parent(); m_insertionIndex = -1;
|
||||
delete item; save();
|
||||
}
|
||||
|
||||
void TaskOrchestrationWidget::moveSelected(int offset)
|
||||
{
|
||||
if (!m_runSteps.isEmpty()) return;
|
||||
QTreeWidgetItem *item = m_tree->currentItem(); if (!item) return;
|
||||
QTreeWidgetItem *parent = item->parent(); int index = parent ? parent->indexOfChild(item) : m_tree->indexOfTopLevelItem(item);
|
||||
int count = parent ? parent->childCount() : m_tree->topLevelItemCount(); int target = qBound(0, index + offset, count - 1);
|
||||
if (target == index) return;
|
||||
captureTaskExpansion(item);
|
||||
if (parent) { parent->takeChild(index); parent->insertChild(target, item); }
|
||||
else { m_tree->takeTopLevelItem(index); m_tree->insertTopLevelItem(target, item); }
|
||||
restoreTaskExpansion(item);
|
||||
m_insertionParent = parent; m_insertionIndex = target + 1;
|
||||
m_tree->setCurrentItem(item); save();
|
||||
}
|
||||
|
||||
void TaskOrchestrationWidget::appendRunSteps(QTreeWidgetItem *item, QList<RunStep> &steps) const
|
||||
bool TaskOrchestrationWidget::appendRunSteps(QTreeWidgetItem *item, QList<RunStep> &steps,
|
||||
QString *error, int depth) const
|
||||
{
|
||||
if (depth > 32 || steps.size() >= 100000) {
|
||||
*error = QStringLiteral("任务展开超过限制(最多 32 层、100000 步),请减少循环次数或嵌套层数");
|
||||
return false;
|
||||
}
|
||||
if (item->data(0, KindRole).toString() == QStringLiteral("loop")) {
|
||||
if (!item->childCount()) {
|
||||
*error = QStringLiteral("循环“%1”没有子节点,请先添加任务").arg(item->text(0));
|
||||
return false;
|
||||
}
|
||||
for (int repeat = 0; repeat < item->data(0, LoopCountRole).toInt(); ++repeat)
|
||||
for (int i = 0; i < item->childCount(); ++i) appendRunSteps(item->child(i), steps);
|
||||
return;
|
||||
for (int i = 0; i < item->childCount(); ++i)
|
||||
if (!appendRunSteps(item->child(i), steps, error, depth + 1)) return false;
|
||||
return true;
|
||||
}
|
||||
RunStep step; step.name = item->text(0); step.deviceType = item->data(0, DeviceTypeRole).toString();
|
||||
step.deviceId = item->data(0, DeviceIdRole).toString(); step.action = item->data(0, ActionRole).toString();
|
||||
@ -939,11 +1090,20 @@ void TaskOrchestrationWidget::appendRunSteps(QTreeWidgetItem *item, QList<RunSte
|
||||
step.waitMs = item->data(0, WaitRole).toInt();
|
||||
step.waitUnit = normalizedWaitUnit(item->data(0, WaitUnitRole).toString());
|
||||
step.source = item; steps.append(step);
|
||||
return true;
|
||||
}
|
||||
|
||||
void TaskOrchestrationWidget::startRun()
|
||||
{
|
||||
m_runSteps.clear(); for (int i = 0; i < m_tree->topLevelItemCount(); ++i) appendRunSteps(m_tree->topLevelItem(i), m_runSteps);
|
||||
if (!m_runSteps.isEmpty()) return;
|
||||
m_runSteps.clear();
|
||||
QString error;
|
||||
for (int i = 0; i < m_tree->topLevelItemCount(); ++i)
|
||||
if (!appendRunSteps(m_tree->topLevelItem(i), m_runSteps, &error)) {
|
||||
m_runSteps.clear();
|
||||
showTaskNotice(this, QStringLiteral("无法执行任务"), error);
|
||||
return;
|
||||
}
|
||||
if (m_runSteps.isEmpty()) {
|
||||
showTaskNotice(this, QStringLiteral("任务编排"),
|
||||
QStringLiteral("当前方案没有可执行任务。"));
|
||||
@ -955,6 +1115,8 @@ void TaskOrchestrationWidget::startRun()
|
||||
};
|
||||
for (int i = 0; i < m_tree->topLevelItemCount(); ++i) clearState(m_tree->topLevelItem(i));
|
||||
m_history->clear();
|
||||
for (auto *control : m_editControls) control->setEnabled(false);
|
||||
m_flowView->setInteractive(false);
|
||||
m_runIndex = 0; m_runButton->setEnabled(false); m_stopButton->setEnabled(true); runNext();
|
||||
}
|
||||
|
||||
@ -1080,6 +1242,9 @@ void TaskOrchestrationWidget::completeRpcTask(const QString &service,
|
||||
if (step.deviceType == QStringLiteral("通用RPC")) {
|
||||
expectedService = step.parameters.value(QStringLiteral("service")).toString();
|
||||
expectedMethod = step.parameters.value(QStringLiteral("method")).toString();
|
||||
} else if (step.deviceType == QStringLiteral("仿生头")) {
|
||||
expectedService = QStringLiteral("cmvr.api.BioHeadService");
|
||||
expectedMethod = BioHeadOperations::method(step.action);
|
||||
} else if (step.deviceType == QStringLiteral("扬声器")) {
|
||||
expectedService = QStringLiteral("cmvr.api.SpeakerService");
|
||||
if (step.action == QStringLiteral("播放服务端文件"))
|
||||
@ -1095,6 +1260,14 @@ void TaskOrchestrationWidget::completeRpcTask(const QString &service,
|
||||
}
|
||||
if (service != expectedService || method != expectedMethod)
|
||||
return;
|
||||
if (step.deviceType == QStringLiteral("仿生头")) {
|
||||
QString feedback;
|
||||
const bool accepted = BioHeadOperations::responseSucceeded(success, detail, &feedback);
|
||||
if (accepted && step.action == QStringLiteral("检查设备"))
|
||||
feedback += QStringLiteral("(设备存在;服务端未提供真实运动状态)");
|
||||
completeCurrentTask(accepted, feedback);
|
||||
return;
|
||||
}
|
||||
completeCurrentTask(success, detail);
|
||||
}
|
||||
|
||||
@ -1158,6 +1331,8 @@ void TaskOrchestrationWidget::completeActionTask(const QString &deviceType,
|
||||
void TaskOrchestrationWidget::stopRun(const QString &reason)
|
||||
{
|
||||
m_runTimer->stop(); m_runSteps.clear(); m_runIndex = 0; m_waitingForResult = false;
|
||||
for (auto *control : m_editControls) control->setEnabled(true);
|
||||
m_flowView->setInteractive(true);
|
||||
m_runButton->setEnabled(true); m_stopButton->setEnabled(false);
|
||||
m_status->setText(reason.isEmpty() ? QStringLiteral("未运行") : reason);
|
||||
}
|
||||
@ -1170,6 +1345,7 @@ QJsonObject TaskOrchestrationWidget::itemToJson(QTreeWidgetItem *item) const
|
||||
QJsonObject object; const QString kind = item->data(0, KindRole).toString();
|
||||
object.insert(QStringLiteral("kind"), kind); object.insert(QStringLiteral("name"), item->text(0));
|
||||
if (kind == QStringLiteral("loop")) {
|
||||
object.insert(QStringLiteral("collapsed"), !item->isExpanded());
|
||||
object.insert(QStringLiteral("count"), item->data(0, LoopCountRole).toInt()); QJsonArray children;
|
||||
for (int i = 0; i < item->childCount(); ++i) children.append(itemToJson(item->child(i)));
|
||||
object.insert(QStringLiteral("children"), children);
|
||||
@ -1192,7 +1368,7 @@ QTreeWidgetItem *TaskOrchestrationWidget::itemFromJson(const QJsonObject &object
|
||||
const int count = qMax(1, object.value(QStringLiteral("count")).toInt(1)); item->setData(0, LoopCountRole, count);
|
||||
item->setText(1, QStringLiteral("循环 × %1").arg(count));
|
||||
for (const QJsonValue &child : object.value(QStringLiteral("children")).toArray()) item->addChild(itemFromJson(child.toObject()));
|
||||
item->setExpanded(true);
|
||||
item->setData(0, CollapsedRole, object.value(QStringLiteral("collapsed")).toBool(false));
|
||||
} else {
|
||||
const QString type = object.value(QStringLiteral("deviceType")).toString(); const QString id = object.value(QStringLiteral("deviceId")).toString();
|
||||
const QString action = object.value(QStringLiteral("action")).toString(); const int wait = object.value(QStringLiteral("waitMs")).toInt();
|
||||
@ -1208,110 +1384,116 @@ QTreeWidgetItem *TaskOrchestrationWidget::itemFromJson(const QJsonObject &object
|
||||
void TaskOrchestrationWidget::rebuildFlowScene()
|
||||
{
|
||||
if (!m_flowScene) return;
|
||||
const QSignalBlocker blocker(m_flowScene);
|
||||
const quint64 revision = ++m_sceneRevision;
|
||||
QTreeWidgetItem *selected = m_tree->currentItem();
|
||||
m_flowScene->clear();
|
||||
QHash<QTreeWidgetItem *, QPointF> positions;
|
||||
QHash<QTreeWidgetItem *, qreal> widths;
|
||||
std::function<qreal(QTreeWidgetItem *)> measure = [&](QTreeWidgetItem *item) -> qreal {
|
||||
if (!item || item->childCount() == 0 || !item->isExpanded()) {
|
||||
widths.insert(item, kTaskCardWidth);
|
||||
return kTaskCardWidth;
|
||||
// Same layout rule as JointFlowEditor: child rows start to the right of
|
||||
// their loop, and each sibling reserves the full visible subtree.
|
||||
std::function<qreal(QTreeWidgetItem *)> measure = [&](QTreeWidgetItem *item) {
|
||||
qreal width = kTaskCardWidth;
|
||||
if (item->isExpanded() && item->childCount()) {
|
||||
qreal childWidth = 0;
|
||||
for (int i = 0; i < item->childCount(); ++i)
|
||||
childWidth += measure(item->child(i)) + (i ? 30 : 0);
|
||||
width = qMax(width, 40 + childWidth);
|
||||
}
|
||||
qreal total = 0;
|
||||
for (int i = 0; i < item->childCount(); ++i)
|
||||
total += measure(item->child(i)) + (i ? 28.0 : 0.0);
|
||||
const qreal width = qMax(kTaskCardWidth, total);
|
||||
widths.insert(item, width);
|
||||
return width;
|
||||
};
|
||||
std::function<void(QTreeWidgetItem *, qreal, qreal)> place =
|
||||
[&](QTreeWidgetItem *item, qreal left, qreal y) {
|
||||
const qreal width = widths.value(item, kTaskCardWidth);
|
||||
positions.insert(item, QPointF(left + (width - kTaskCardWidth) / 2.0, y));
|
||||
if (item->childCount() == 0 || !item->isExpanded()) return;
|
||||
qreal childLeft = left;
|
||||
for (int i = 0; i < item->childCount(); ++i) {
|
||||
QTreeWidgetItem *child = item->child(i);
|
||||
place(child, childLeft, y + 126.0);
|
||||
childLeft += widths.value(child, kTaskCardWidth) + 28.0;
|
||||
}
|
||||
};
|
||||
qreal left = 36.0;
|
||||
[&](QTreeWidgetItem *item, qreal x, qreal y) {
|
||||
positions.insert(item, QPointF(x, y));
|
||||
if (!item->isExpanded()) return;
|
||||
qreal childX = x + 40;
|
||||
for (int i = 0; i < item->childCount(); ++i) {
|
||||
QTreeWidgetItem *child = item->child(i);
|
||||
place(child, childX, y + kTaskCardHeight + 36);
|
||||
childX += widths.value(child) + 30;
|
||||
}
|
||||
};
|
||||
qreal left = 40;
|
||||
for (int i = 0; i < m_tree->topLevelItemCount(); ++i) {
|
||||
QTreeWidgetItem *item = m_tree->topLevelItem(i);
|
||||
const qreal width = measure(item);
|
||||
place(item, left, 38.0);
|
||||
left += width + 42.0;
|
||||
}
|
||||
for (auto it = positions.constBegin(); it != positions.constEnd(); ++it) {
|
||||
QTreeWidgetItem *item = it.key();
|
||||
if (!item->parent()) continue;
|
||||
const QPointF parent = positions.value(item->parent());
|
||||
QPainterPath path;
|
||||
path.moveTo(parent.x() + kTaskCardWidth / 2.0, parent.y() + kTaskCardHeight);
|
||||
path.cubicTo(parent.x() + kTaskCardWidth / 2.0, parent.y() + 100.0,
|
||||
it.value().x() + kTaskCardWidth / 2.0, it.value().y() - 26.0,
|
||||
it.value().x() + kTaskCardWidth / 2.0, it.value().y());
|
||||
QGraphicsPathItem *edge = m_flowScene->addPath(path, QPen(QColor("#9eb1c5"), 1.8));
|
||||
edge->setZValue(0);
|
||||
measure(item); place(item, left, 40);
|
||||
left += widths.value(item) + 30;
|
||||
}
|
||||
for (auto it = positions.constBegin(); it != positions.constEnd(); ++it) {
|
||||
QTreeWidgetItem *item = it.key();
|
||||
const bool loop = item->data(0, KindRole).toString() == QStringLiteral("loop");
|
||||
const QString subtitle = loop
|
||||
? QStringLiteral("循环 × %1").arg(item->data(0, LoopCountRole).toInt())
|
||||
? QStringLiteral("循环 × %1 · %2 个子节点").arg(item->data(0, LoopCountRole).toInt()).arg(item->childCount())
|
||||
: QStringLiteral("%1 · %2\n等待 %3")
|
||||
.arg(item->data(0, DeviceIdRole).toString(), item->data(0, ActionRole).toString())
|
||||
.arg(waitDisplayText(item->data(0, WaitRole).toInt(),
|
||||
item->data(0, WaitUnitRole).toString()));
|
||||
.arg(waitDisplayText(item->data(0, WaitRole).toInt(), item->data(0, WaitUnitRole).toString()));
|
||||
TaskFlowCard *card = new TaskFlowCard(item, item->text(0), subtitle, loop,
|
||||
[this](QTreeWidgetItem *source, const QPointF &position) {
|
||||
QTimer::singleShot(0, this, [this, source, position]() { moveFlowItem(source, position); });
|
||||
[this, revision](QTreeWidgetItem *source, const QPointF &position) {
|
||||
QTimer::singleShot(0, this, [this, source, position, revision]() {
|
||||
if (revision == m_sceneRevision) moveFlowItem(source, position);
|
||||
});
|
||||
},
|
||||
[this](QTreeWidgetItem *source) {
|
||||
QTimer::singleShot(0, this, [this, source]() { m_tree->setCurrentItem(source); editSelected(); });
|
||||
[this, revision](QTreeWidgetItem *source) {
|
||||
QTimer::singleShot(0, this, [this, source, revision]() {
|
||||
if (revision != m_sceneRevision || !m_runSteps.isEmpty()) return;
|
||||
m_tree->setCurrentItem(source); editSelected();
|
||||
});
|
||||
},
|
||||
[this](QTreeWidgetItem *source) {
|
||||
QTimer::singleShot(0, this, [this, source]() {
|
||||
source->setExpanded(!source->isExpanded()); rebuildFlowScene();
|
||||
[this, revision](QTreeWidgetItem *source) {
|
||||
QTimer::singleShot(0, this, [this, source, revision]() {
|
||||
if (revision != m_sceneRevision || !m_runSteps.isEmpty()) return;
|
||||
source->setExpanded(!source->isExpanded()); save();
|
||||
});
|
||||
});
|
||||
card->setPos(it.value());
|
||||
card->setData(0, QStringLiteral("taskNode")); card->setData(1, item->text(0));
|
||||
m_flowScene->addItem(card);
|
||||
if (item == selected) card->setSelected(true);
|
||||
}
|
||||
const QRectF bounds = m_flowScene->itemsBoundingRect().adjusted(-30, -30, 30, 50);
|
||||
m_flowScene->setSceneRect(bounds.isValid() ? bounds : QRectF(0, 0, 800, 360));
|
||||
auto link = [&](QTreeWidgetItem *from, QTreeWidgetItem *to, bool membership) {
|
||||
if (!positions.contains(from) || !positions.contains(to)) return;
|
||||
const QPointF offset(kTaskCardWidth / 2, kTaskCardHeight / 2);
|
||||
auto *edge = m_flowScene->addLine(QLineF(positions.value(from) + offset, positions.value(to) + offset),
|
||||
QPen(QColor(membership ? "#9aabc0" : "#5a91cf"), membership ? 2.0 : 1.5,
|
||||
membership ? Qt::SolidLine : Qt::DashLine));
|
||||
edge->setZValue(0);
|
||||
edge->setData(0, membership ? QStringLiteral("membership") : QStringLiteral("sequence"));
|
||||
};
|
||||
for (auto it = positions.constBegin(); it != positions.constEnd(); ++it) {
|
||||
QTreeWidgetItem *item = it.key();
|
||||
if (item->parent()) link(item->parent(), item, true);
|
||||
QTreeWidgetItem *parent = item->parent();
|
||||
const int index = parent ? parent->indexOfChild(item) : m_tree->indexOfTopLevelItem(item);
|
||||
if (index > 0) link(parent ? parent->child(index - 1) : m_tree->topLevelItem(index - 1), item, false);
|
||||
}
|
||||
m_flowScene->setSceneRect(m_flowScene->itemsBoundingRect().adjusted(-60, -60, 60, 60));
|
||||
updateInsertionHint();
|
||||
}
|
||||
|
||||
void TaskOrchestrationWidget::moveFlowItem(QTreeWidgetItem *item, const QPointF &position)
|
||||
{
|
||||
if (!item) return;
|
||||
if (!item || !m_runSteps.isEmpty()) return;
|
||||
QTreeWidgetItem *parent = item->parent();
|
||||
QList<QPair<qreal, QTreeWidgetItem *>> siblings;
|
||||
for (QGraphicsItem *graphicsItem : m_flowScene->items()) {
|
||||
TaskFlowCard *card = dynamic_cast<TaskFlowCard *>(graphicsItem);
|
||||
auto *card = dynamic_cast<TaskFlowCard *>(graphicsItem);
|
||||
if (card && card->source() != item && card->source()->parent() == parent)
|
||||
siblings.append(qMakePair(card->scenePos().x(), card->source()));
|
||||
}
|
||||
std::sort(siblings.begin(), siblings.end(), [](const QPair<qreal, QTreeWidgetItem *> &a,
|
||||
const QPair<qreal, QTreeWidgetItem *> &b) {
|
||||
return a.first < b.first;
|
||||
});
|
||||
std::sort(siblings.begin(), siblings.end(), [](const auto &a, const auto &b) { return a.first < b.first; });
|
||||
int target = 0;
|
||||
const qreal center = position.x() + kTaskCardWidth / 2.0;
|
||||
while (target < siblings.size() && center > siblings.at(target).first + kTaskCardWidth / 2.0) ++target;
|
||||
if (parent) {
|
||||
const int old = parent->indexOfChild(item);
|
||||
parent->takeChild(old);
|
||||
parent->insertChild(qBound(0, target, parent->childCount()), item);
|
||||
} else {
|
||||
const int old = m_tree->indexOfTopLevelItem(item);
|
||||
m_tree->takeTopLevelItem(old);
|
||||
m_tree->insertTopLevelItem(qBound(0, target, m_tree->topLevelItemCount()), item);
|
||||
while (target < siblings.size() && position.x() > siblings.at(target).first) ++target;
|
||||
const int old = parent ? parent->indexOfChild(item) : m_tree->indexOfTopLevelItem(item);
|
||||
if (old != target) {
|
||||
captureTaskExpansion(item);
|
||||
if (parent) { parent->takeChild(old); parent->insertChild(target, item); }
|
||||
else { m_tree->takeTopLevelItem(old); m_tree->insertTopLevelItem(target, item); }
|
||||
restoreTaskExpansion(item);
|
||||
}
|
||||
m_tree->setCurrentItem(item);
|
||||
save();
|
||||
m_insertionParent = parent; m_insertionIndex = target + 1;
|
||||
if (old != target) save(); else rebuildFlowScene();
|
||||
}
|
||||
|
||||
void TaskOrchestrationWidget::storeCurrentGroup()
|
||||
@ -1344,9 +1526,12 @@ void TaskOrchestrationWidget::setCameraCapabilities(const QString &deviceId,
|
||||
|
||||
void TaskOrchestrationWidget::activateGroup(int index)
|
||||
{
|
||||
if (!m_runSteps.isEmpty()) return;
|
||||
if (m_switchingGroup || index < 0 || index >= m_groups.size()) return;
|
||||
m_insertionParent = nullptr; m_insertionIndex = -1;
|
||||
storeCurrentGroup(); m_activeGroup = index; m_tree->clear();
|
||||
for (const QJsonValue &node : m_groups.at(index).value(QStringLiteral("nodes")).toArray()) m_tree->addTopLevelItem(itemFromJson(node.toObject()));
|
||||
for (int i = 0; i < m_tree->topLevelItemCount(); ++i) restoreTaskExpansion(m_tree->topLevelItem(i));
|
||||
save();
|
||||
}
|
||||
|
||||
@ -1360,6 +1545,7 @@ void TaskOrchestrationWidget::load()
|
||||
m_switchingGroup = true; for (const QJsonObject &group : m_groups) m_groupCombo->addItem(group.value(QStringLiteral("name")).toString());
|
||||
m_groupCombo->setCurrentIndex(0); m_switchingGroup = false; m_activeGroup = 0;
|
||||
for (const QJsonValue &node : m_groups.first().value(QStringLiteral("nodes")).toArray()) m_tree->addTopLevelItem(itemFromJson(node.toObject()));
|
||||
for (int i = 0; i < m_tree->topLevelItemCount(); ++i) restoreTaskExpansion(m_tree->topLevelItem(i));
|
||||
rebuildFlowScene();
|
||||
}
|
||||
|
||||
|
||||
@ -21,6 +21,7 @@ class TaskOrchestrationWidget final : public QWidget
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit TaskOrchestrationWidget(QWidget *parent = nullptr);
|
||||
~TaskOrchestrationWidget() override;
|
||||
void setAvailableDevices(const QVariantList &devices);
|
||||
void setCameraCapabilities(const QString &deviceId, const QVariantMap &capabilities);
|
||||
void completeCurrentTask(bool success, const QString &detail);
|
||||
@ -44,6 +45,7 @@ signals:
|
||||
void statusMessage(const QString &level, const QString &message);
|
||||
|
||||
private:
|
||||
friend struct TaskFlowTestAccess;
|
||||
struct RunStep {
|
||||
QString name;
|
||||
QString deviceType;
|
||||
@ -63,7 +65,7 @@ private:
|
||||
void startRun();
|
||||
void stopRun(const QString &reason = QString());
|
||||
void runNext();
|
||||
void appendRunSteps(QTreeWidgetItem *item, QList<RunStep> &steps) const;
|
||||
bool appendRunSteps(QTreeWidgetItem *item, QList<RunStep> &steps, QString *error, int depth = 0) const;
|
||||
void load();
|
||||
void save();
|
||||
void storeCurrentGroup();
|
||||
@ -73,6 +75,10 @@ private:
|
||||
QTreeWidgetItem *itemFromJson(const QJsonObject &object) const;
|
||||
void rebuildFlowScene();
|
||||
void moveFlowItem(QTreeWidgetItem *item, const QPointF &position);
|
||||
void setInsertionAtRoot();
|
||||
bool useSelectedLoopAsInsertionParent();
|
||||
void insertNode(QTreeWidgetItem *item);
|
||||
void updateInsertionHint();
|
||||
|
||||
QComboBox *m_groupCombo = nullptr;
|
||||
QTreeWidget *m_tree = nullptr;
|
||||
@ -82,6 +88,11 @@ private:
|
||||
QPushButton *m_stopButton = nullptr;
|
||||
QLabel *m_status = nullptr;
|
||||
QListWidget *m_history = nullptr;
|
||||
QLabel *m_insertionHint = nullptr;
|
||||
QTreeWidgetItem *m_insertionParent = nullptr;
|
||||
int m_insertionIndex = -1;
|
||||
quint64 m_sceneRevision = 0;
|
||||
QList<QWidget *> m_editControls;
|
||||
QTimer *m_runTimer = nullptr;
|
||||
QList<QJsonObject> m_groups;
|
||||
QList<RunStep> m_runSteps;
|
||||
|
||||
42
styles.qss
42
styles.qss
@ -1970,3 +1970,45 @@ QScrollBar::add-line, QScrollBar::sub-line { width: 0; height: 0; border: none;
|
||||
QScrollBar::add-page, QScrollBar::sub-page { background: transparent; }
|
||||
QToolTip { color: #ffffff; background: #253246; border: none; border-radius: 4px; padding: 7px 10px; }
|
||||
QSizeGrip { width: 14px; height: 14px; background: transparent; }
|
||||
|
||||
/* Biohead quick controls: restrained cards, details available on hover. */
|
||||
QWidget#bioheadPage QPushButton[bioheadPreset="true"] {
|
||||
color: #334155;
|
||||
background: #f5f8fd;
|
||||
border: 1px solid #e2e9f3;
|
||||
border-radius: 8px;
|
||||
padding: 10px 16px;
|
||||
}
|
||||
QWidget#bioheadPage QPushButton[bioheadPreset="true"]:hover {
|
||||
color: #165dcc;
|
||||
background: #eaf2ff;
|
||||
border-color: #9fc1f5;
|
||||
}
|
||||
QWidget#bioheadPage QPushButton[bioheadPreset="true"]:pressed {
|
||||
background: #dceaff;
|
||||
border-color: #1b6ef3;
|
||||
}
|
||||
QWidget#bioheadPage QPushButton[bioheadPreset="true"]:disabled {
|
||||
color: #9aa7b8;
|
||||
background: #f7f9fc;
|
||||
border-color: #e8edf4;
|
||||
}
|
||||
QWidget#bioheadPage QToolButton#bioheadHelp {
|
||||
color: #8795a8;
|
||||
background: transparent;
|
||||
border: 1px solid #dce4ef;
|
||||
border-radius: 10px;
|
||||
padding: 0;
|
||||
min-width: 20px;
|
||||
max-width: 20px;
|
||||
min-height: 20px;
|
||||
max-height: 20px;
|
||||
}
|
||||
QWidget#bioheadPage QToolButton#bioheadHelp:hover {
|
||||
color: #165dcc;
|
||||
background: #eaf2ff;
|
||||
border-color: #9fc1f5;
|
||||
}
|
||||
QListWidget#bioheadHistory { border: none; background: transparent; outline: none; }
|
||||
QListWidget#bioheadHistory::item { padding: 9px 10px; border-bottom: 1px solid #edf1f6; }
|
||||
QLabel#bioheadStatus { color: #64748b; padding: 4px 0; }
|
||||
|
||||
@ -2,6 +2,9 @@
|
||||
#include "applicationstyle.h"
|
||||
#include "urdfviewerwidget.h"
|
||||
#include "taskorchestrationwidget.h"
|
||||
#include "bioheadoperations.h"
|
||||
#include "bioheadwidget.h"
|
||||
#include "robotclient.h"
|
||||
#include <QApplication>
|
||||
#include <QCheckBox>
|
||||
#include <QComboBox>
|
||||
@ -13,6 +16,219 @@
|
||||
#include <QFile>
|
||||
#include <QLabel>
|
||||
#include <QTextStream>
|
||||
#include <QTreeWidget>
|
||||
#include <QListWidget>
|
||||
#include <QEventLoop>
|
||||
#include <QTabWidget>
|
||||
#include <QGraphicsScene>
|
||||
#include <QGraphicsItem>
|
||||
#include <QGraphicsView>
|
||||
#include <QMouseEvent>
|
||||
|
||||
struct TaskFlowTestAccess
|
||||
{
|
||||
static bool verify()
|
||||
{
|
||||
TaskOrchestrationWidget w;
|
||||
w.m_tree->clear(); w.m_insertionParent = nullptr; w.m_insertionIndex = -1;
|
||||
auto task = [&](const QString &name) {
|
||||
return w.itemFromJson(QJsonObject{{"kind", "task"}, {"name", name},
|
||||
{"deviceType", QStringLiteral("仿生头")}, {"deviceId", "bio_head"},
|
||||
{"action", QStringLiteral("高兴")}, {"waitMs", 0}});
|
||||
};
|
||||
auto *a = task("A"); auto *b = task("B"); auto *d = task("D"); auto *c = task("C");
|
||||
auto *loop = w.itemFromJson(QJsonObject{{"kind", "loop"}, {"name", "Outer"}, {"count", 2}});
|
||||
auto *nested = w.itemFromJson(QJsonObject{{"kind", "loop"}, {"name", "Inner"}, {"count", 2}});
|
||||
w.m_tree->addTopLevelItems({a, loop, c});
|
||||
loop->addChildren({b, nested}); nested->addChild(d);
|
||||
loop->setExpanded(true); nested->setExpanded(true);
|
||||
w.rebuildFlowScene();
|
||||
int edges = 0, memberships = 0;
|
||||
QList<QRectF> cards;
|
||||
for (auto *g : w.m_flowScene->items()) {
|
||||
edges += g->data(0).toString() == "sequence";
|
||||
memberships += g->data(0).toString() == "membership";
|
||||
if (g->data(0).toString() == "taskNode") cards.append(g->sceneBoundingRect());
|
||||
}
|
||||
if (edges != 3 || memberships != 3) return false;
|
||||
for (int i = 0; i < cards.size(); ++i) for (int j = i + 1; j < cards.size(); ++j)
|
||||
if (cards[i].intersects(cards[j])) return false;
|
||||
auto select = [&](const QString &name) {
|
||||
w.m_flowScene->clearSelection();
|
||||
for (auto *g : w.m_flowScene->items())
|
||||
if (g->data(0).toString() == "taskNode" && g->data(1).toString() == name) {
|
||||
g->setSelected(true); return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
if (!select("Outer") || w.m_insertionParent || w.m_insertionIndex != 2) return false;
|
||||
auto *x = task("X"); w.insertNode(x);
|
||||
if (w.m_tree->topLevelItem(2) != x || loop->childCount() != 2) return false;
|
||||
if (!select("B") || w.m_insertionParent != loop || w.m_insertionIndex != 1) return false;
|
||||
if (!w.m_insertionHint->text().contains("Outer")) return false;
|
||||
auto *y = task("Y"); w.insertNode(y);
|
||||
if (loop->child(1) != y || nested->childCount() != 1) return false;
|
||||
if (!select("Outer") || !w.useSelectedLoopAsInsertionParent()) return false;
|
||||
if (w.m_insertionParent != loop || w.m_tree->currentItem()) return false;
|
||||
auto *z = task("Z"); w.insertNode(z);
|
||||
if (loop->child(3) != z) return false;
|
||||
w.moveFlowItem(z, QPointF(-100000, 0));
|
||||
if (z->parent() != loop || loop->child(0) != z) return false;
|
||||
w.moveFlowItem(z, QPointF(100000, 0));
|
||||
if (z->parent() != loop || loop->child(3) != z) return false;
|
||||
w.setInsertionAtRoot();
|
||||
if (w.m_insertionParent || w.m_tree->currentItem()
|
||||
|| !w.m_insertionHint->text().contains(QStringLiteral("根流程(末尾)"))) return false;
|
||||
// A click does not pass through the drag code; moving preserves nesting.
|
||||
w.m_tree->setCurrentItem(loop); w.moveSelected(1);
|
||||
if (!loop->isExpanded() || !nested->isExpanded() || d->parent() != nested) return false;
|
||||
QList<TaskOrchestrationWidget::RunStep> steps; QString error;
|
||||
if (!w.appendRunSteps(loop, steps, &error) || steps.size() != 10) return false;
|
||||
const QStringList expected{"B", "Y", "D", "D", "Z", "B", "Y", "D", "D", "Z"};
|
||||
for (int i = 0; i < steps.size(); ++i) if (steps[i].name != expected[i]) return false;
|
||||
nested->setExpanded(false); w.save();
|
||||
TaskOrchestrationWidget loaded;
|
||||
QTreeWidgetItem *loadedLoop = nullptr;
|
||||
for (int i = 0; i < loaded.m_tree->topLevelItemCount(); ++i)
|
||||
if (loaded.m_tree->topLevelItem(i)->text(0) == "Outer") loadedLoop = loaded.m_tree->topLevelItem(i);
|
||||
if (!loadedLoop || loadedLoop->child(2)->isExpanded()) return false;
|
||||
auto *empty = w.itemFromJson(QJsonObject{{"kind", "loop"}, {"name", "Empty"}, {"count", 2}});
|
||||
steps.clear(); bool rejected = !w.appendRunSteps(empty, steps, &error); delete empty;
|
||||
if (!rejected || error.isEmpty()) return false;
|
||||
w.resize(1500, 900); w.show(); qApp->processEvents();
|
||||
QGraphicsItem *cardA = nullptr;
|
||||
for (auto *g : w.m_flowScene->items())
|
||||
if (g->data(0).toString() == "taskNode" && g->data(1).toString() == "A") cardA = g;
|
||||
if (!cardA) return false;
|
||||
w.m_flowView->ensureVisible(cardA);
|
||||
const QPoint point = w.m_flowView->mapFromScene(cardA->sceneBoundingRect().center());
|
||||
const QPoint global = w.m_flowView->viewport()->mapToGlobal(point);
|
||||
const quint64 revision = w.m_sceneRevision;
|
||||
QMouseEvent press(QEvent::MouseButtonPress, point, global, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
|
||||
QMouseEvent release(QEvent::MouseButtonRelease, point, global, Qt::LeftButton, Qt::NoButton, Qt::NoModifier);
|
||||
QApplication::sendEvent(w.m_flowView->viewport(), &press);
|
||||
QApplication::sendEvent(w.m_flowView->viewport(), &release);
|
||||
qApp->processEvents();
|
||||
if (w.m_sceneRevision != revision || w.m_tree->currentItem() != a
|
||||
|| w.m_insertionParent || w.m_insertionIndex != 1) return false;
|
||||
const QJsonObject beforeFold = w.itemToJson(loop);
|
||||
QGraphicsItem *loopCard = nullptr;
|
||||
for (auto *g : w.m_flowScene->items())
|
||||
if (g->data(0).toString() == "taskNode" && g->data(1).toString() == "Outer") loopCard = g;
|
||||
if (!loopCard) return false;
|
||||
w.m_flowView->ensureVisible(loopCard);
|
||||
const QPoint toggle = w.m_flowView->mapFromScene(loopCard->scenePos() + QPointF(246, 21));
|
||||
const QPoint toggleGlobal = w.m_flowView->viewport()->mapToGlobal(toggle);
|
||||
QMouseEvent foldPress(QEvent::MouseButtonPress, toggle, toggleGlobal, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
|
||||
QMouseEvent foldRelease(QEvent::MouseButtonRelease, toggle, toggleGlobal, Qt::LeftButton, Qt::NoButton, Qt::NoModifier);
|
||||
QApplication::sendEvent(w.m_flowView->viewport(), &foldPress);
|
||||
QApplication::sendEvent(w.m_flowView->viewport(), &foldRelease);
|
||||
qApp->processEvents();
|
||||
QJsonObject afterFold = w.itemToJson(loop);
|
||||
afterFold.insert("collapsed", beforeFold.value("collapsed"));
|
||||
if (loop->isExpanded() || afterFold != beforeFold) return false;
|
||||
loop->setExpanded(true); w.rebuildFlowScene();
|
||||
w.grab().save(QStringLiteral("task-flow-interactions.png"));
|
||||
qInfo() << "Task flow: insertion contexts, nested order, edges, drag scope and persistence PASS";
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
static bool verifyBioheadPage()
|
||||
{
|
||||
RobotClient client;
|
||||
// Prevent any dispatch to the real worker; inspect requests and inject replies.
|
||||
QObject::disconnect(&client, &RobotClient::rpcRequested, nullptr, nullptr);
|
||||
BioHeadWidget page(&client);
|
||||
page.resize(1250, 850);
|
||||
page.show();
|
||||
auto *devices = page.findChild<QComboBox *>(QStringLiteral("bioheadDeviceCombo"));
|
||||
auto *status = page.findChild<QLabel *>(QStringLiteral("bioheadStatus"));
|
||||
auto *tabs = page.findChild<QTabWidget *>();
|
||||
if (!devices || !status || !tabs || tabs->count() != 2) return false;
|
||||
QPushButton *happy = nullptr;
|
||||
int count = 0;
|
||||
for (auto *button : page.findChildren<QPushButton *>()) {
|
||||
if (button->property("bioheadAction").isValid()) {
|
||||
++count;
|
||||
if (button->isEnabled()) return false;
|
||||
if (button->property("bioheadAction") == QStringLiteral("高兴")) happy = button;
|
||||
}
|
||||
}
|
||||
if (count != 10 || !happy) return false;
|
||||
const QVariantList list{QVariantMap{{"deviceId", "bio_head"}, {"type", 8}},
|
||||
QVariantMap{{"deviceId", "usb_cam1"}, {"type", 2}}};
|
||||
page.setAvailableDevices(list);
|
||||
emit client.connectionChanged(true, 0, QString());
|
||||
if (devices->count() != 1 || !happy->isEnabled()) return false;
|
||||
bool dispatched = false;
|
||||
QObject::connect(&client, &RobotClient::rpcRequested, &page,
|
||||
[&](const QString &service, const QString &method, const QString &json, const QString &id) {
|
||||
dispatched = service == QStringLiteral("cmvr.api.BioHeadService") && method == QStringLiteral("Happy")
|
||||
&& id == QStringLiteral("bio_head") && QJsonDocument::fromJson(json.toUtf8()).object()
|
||||
.value(QStringLiteral("header")).toObject().value(QStringLiteral("deviceId")).toString() == id;
|
||||
});
|
||||
happy->click();
|
||||
if (!dispatched || happy->isEnabled()) return false;
|
||||
emit client.rpcFinished(QStringLiteral("cmvr.api.BioHeadService"), QStringLiteral("Happy"), true,
|
||||
QStringLiteral("{\"header\":{\"errorMessage\":\"device rejected\"}}"));
|
||||
if (!happy->isEnabled() || !status->text().contains(QStringLiteral("device rejected"))) return false;
|
||||
page.setAvailableDevices(list);
|
||||
if (!status->text().contains(QStringLiteral("device rejected"))) return false;
|
||||
page.grab().save(QStringLiteral("biohead-page.png"));
|
||||
emit client.connectionChanged(false, 0, QString());
|
||||
if (happy->isEnabled()) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool verifyBioheadTaskRun()
|
||||
{
|
||||
// Isolated from MainWindow/RobotClient: no real connection or hardware calls.
|
||||
TaskOrchestrationWidget tasks;
|
||||
auto *tree = tasks.findChild<QTreeWidget *>();
|
||||
if (!tree) return false;
|
||||
tree->clear();
|
||||
for (const QString &action : {QStringLiteral("开始说话"), QStringLiteral("停止说话"),
|
||||
QStringLiteral("高兴")}) {
|
||||
auto *item = new QTreeWidgetItem(tree);
|
||||
item->setText(0, action);
|
||||
item->setData(0, Qt::UserRole, QStringLiteral("task"));
|
||||
item->setData(0, Qt::UserRole + 1, QStringLiteral("仿生头"));
|
||||
item->setData(0, Qt::UserRole + 2, QStringLiteral("bio_head"));
|
||||
item->setData(0, Qt::UserRole + 3, action);
|
||||
item->setData(0, Qt::UserRole + 4, QStringLiteral("{}"));
|
||||
item->setData(0, Qt::UserRole + 5, 40);
|
||||
}
|
||||
QPushButton *run = nullptr;
|
||||
for (auto *button : tasks.findChildren<QPushButton *>())
|
||||
if (button->text() == QStringLiteral("执行任务编排")) run = button;
|
||||
if (!run) return false;
|
||||
int requests = 0;
|
||||
bool valid = true;
|
||||
QEventLoop loop;
|
||||
QObject::connect(&tasks, &TaskOrchestrationWidget::taskRequested, &tasks,
|
||||
[&](const QString &type, const QString &id, const QString &action, const QJsonObject &) {
|
||||
++requests;
|
||||
valid = valid && type == QStringLiteral("仿生头") && id == QStringLiteral("bio_head");
|
||||
const QString method = BioHeadOperations::method(action);
|
||||
tasks.completeRpcTask(QStringLiteral("wrong.service"), method, true,
|
||||
QStringLiteral("{\"header\":{\"success\":true}}"));
|
||||
QTimer::singleShot(10, &tasks, [&, method]() {
|
||||
const bool first = requests == 1;
|
||||
tasks.completeRpcTask(QStringLiteral("cmvr.api.BioHeadService"), method, true,
|
||||
first ? QStringLiteral("{\"header\":{\"success\":true}}")
|
||||
: QStringLiteral("{\"header\":{\"errorMessage\":\"test rejection\"}}"));
|
||||
});
|
||||
});
|
||||
run->click();
|
||||
QTimer::singleShot(300, &loop, &QEventLoop::quit);
|
||||
loop.exec();
|
||||
auto *history = tasks.findChild<QListWidget *>();
|
||||
bool failureShown = false;
|
||||
if (history) for (int i = 0; i < history->count(); ++i)
|
||||
failureShown |= history->item(i)->text().contains(QStringLiteral("test rejection"));
|
||||
return valid && requests == 2 && run->isEnabled() && failureShown;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
@ -20,6 +236,9 @@ int main(int argc, char **argv)
|
||||
application.setApplicationName(QStringLiteral("CMVR Theme Verification"));
|
||||
application.setOrganizationName(QStringLiteral("CMVR Tests"));
|
||||
ApplicationStyle::install(application);
|
||||
if (!TaskFlowTestAccess::verify()) return 11;
|
||||
if (!verifyBioheadPage()) return 10;
|
||||
if (!verifyBioheadTaskRun()) return 9;
|
||||
if (application.windowIcon().pixmap(32, 32).isNull()) return 1;
|
||||
MainWindow window;
|
||||
if (auto *demo = window.findChild<QCheckBox *>(QStringLiteral("demoCheck")))
|
||||
@ -55,7 +274,8 @@ int main(int argc, char **argv)
|
||||
window.grab().save(QStringLiteral("application-tasks.png"));
|
||||
if (auto *tasks = window.findChild<TaskOrchestrationWidget *>()) {
|
||||
tasks->setAvailableDevices({QVariantMap{{"deviceId", "usb_cam1"}, {"type", 2}},
|
||||
QVariantMap{{"deviceId", "mic1"}, {"type", 5}}});
|
||||
QVariantMap{{"deviceId", "mic1"}, {"type", 5}},
|
||||
QVariantMap{{"deviceId", "bio_head"}, {"type", 8}}});
|
||||
QPushButton *add = nullptr;
|
||||
for (auto *button : tasks->findChildren<QPushButton *>())
|
||||
if (button->text().contains(QStringLiteral("任务节点"))) { add = button; break; }
|
||||
@ -72,6 +292,21 @@ int main(int argc, char **argv)
|
||||
taskInspected = taskInspected && combo->view()->styleSheet().contains(QStringLiteral("#e8f1ff"));
|
||||
++count;
|
||||
}
|
||||
QComboBox *devices = nullptr;
|
||||
for (auto *combo : dialog->findChildren<QComboBox *>())
|
||||
for (int i = 0; i < combo->count(); ++i)
|
||||
if (combo->itemData(i).toMap().value(QStringLiteral("deviceId"))
|
||||
== QStringLiteral("bio_head")) {
|
||||
devices = combo;
|
||||
combo->setCurrentIndex(i);
|
||||
break;
|
||||
}
|
||||
bool bioheadActions = false;
|
||||
for (auto *combo : dialog->findChildren<QComboBox *>())
|
||||
if (combo->findText(QStringLiteral("开始说话")) >= 0)
|
||||
bioheadActions = combo->count() == BioHeadOperations::actions().size()
|
||||
&& combo->findText(QStringLiteral("紧急停止")) >= 0;
|
||||
taskInspected = taskInspected && devices && bioheadActions;
|
||||
if (report.open(QIODevice::Append | QIODevice::Text)) {
|
||||
QTextStream stream(&report);
|
||||
stream << "Task-node dialog combos: " << count << " / " << (taskInspected ? "PASS" : "FAIL") << '\n';
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
#include "applicationsettings.h"
|
||||
#include "applicationstyle.h"
|
||||
#include "appmessagebox.h"
|
||||
#include "bioheadoperations.h"
|
||||
|
||||
#include <QtTest>
|
||||
#include <QApplication>
|
||||
@ -29,6 +30,29 @@ class ApplicationThemeTest : public QObject
|
||||
private slots:
|
||||
void initTestCase() { ApplicationStyle::install(*qApp); }
|
||||
|
||||
void bioheadOperationContract()
|
||||
{
|
||||
QCOMPARE(BioHeadOperations::actions().size(), 10);
|
||||
QCOMPARE(BioHeadOperations::method(QStringLiteral("高兴")), QStringLiteral("Happy"));
|
||||
QCOMPARE(BioHeadOperations::method(QStringLiteral("疲惫")), QStringLiteral("ExpressionTired"));
|
||||
QCOMPARE(BioHeadOperations::method(QStringLiteral("开始说话")), QStringLiteral("SpeakStart"));
|
||||
QCOMPARE(BioHeadOperations::method(QStringLiteral("停止说话")), QStringLiteral("SpeakStop"));
|
||||
QCOMPARE(BioHeadOperations::method(QStringLiteral("检查设备")), QStringLiteral("GetSystemStatus"));
|
||||
QVERIFY(BioHeadOperations::method(QStringLiteral("不存在的操作")).isEmpty());
|
||||
QString detail;
|
||||
QVERIFY(BioHeadOperations::responseSucceeded(true, "{\"header\":{\"success\":true}}", &detail));
|
||||
QVERIFY(!BioHeadOperations::responseSucceeded(true,
|
||||
"{\"header\":{\"success\":false,\"errorMessage\":\"device missing\"}}", &detail));
|
||||
QCOMPARE(detail, QStringLiteral("device missing"));
|
||||
QVERIFY(!BioHeadOperations::responseSucceeded(true,
|
||||
"{\"header\":{\"error_message\":\"control locked\"}}", &detail));
|
||||
QCOMPARE(detail, QStringLiteral("control locked"));
|
||||
QVERIFY(!BioHeadOperations::responseSucceeded(true, "{}", &detail));
|
||||
QVERIFY(!BioHeadOperations::responseSucceeded(true, "invalid JSON", &detail));
|
||||
QVERIFY(!BioHeadOperations::responseSucceeded(false, "disconnected", &detail));
|
||||
QCOMPARE(detail, QStringLiteral("disconnected"));
|
||||
}
|
||||
|
||||
void settingsPreserveGeometryAndUnknownFields()
|
||||
{
|
||||
QTemporaryDir directory;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user