226 lines
10 KiB
C++
226 lines
10 KiB
C++
|
|
#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);
|
|||
|
|
}
|