Files
hfut-bishe/client/gui/props/HotspotPropertySection.cpp
2026-05-14 13:30:06 +08:00

152 lines
5.0 KiB
C++

#include "props/HotspotPropertySection.h"
#include <QComboBox>
#include <QFormLayout>
#include <QHash>
#include <QLabel>
#include <QListWidget>
#include <QMenu>
#include <QPushButton>
#include <QVBoxLayout>
namespace gui {
HotspotPropertySection::HotspotPropertySection(QWidget* parent) : PropertySectionWidget(parent) {
auto* root = new QVBoxLayout(this);
root->setContentsMargins(0, 0, 0, 0);
auto* title = new QLabel(QStringLiteral("已添加热点"), this);
root->addWidget(title);
m_hotspotList = new QListWidget(this);
m_hotspotList->setObjectName(QStringLiteral("PropertyPanelListWidget"));
m_hotspotList->setMinimumHeight(120);
m_hotspotList->setContextMenuPolicy(Qt::CustomContextMenu);
root->addWidget(m_hotspotList);
auto* form = new QFormLayout();
form->setSpacing(6);
root->addLayout(form);
m_targetAnim = new QComboBox(this);
m_targetAnim->setMinimumWidth(120);
form->addRow(QStringLiteral("目标"), m_targetAnim);
m_btnDelete = new QPushButton(QStringLiteral("删除"), this);
root->addWidget(m_btnDelete);
connect(m_targetAnim, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [this](int) {
if (m_hotspotId.isEmpty() || !m_targetAnim) {
return;
}
const QString aid = m_targetAnim->currentData().toString();
emit targetAnimationChanged(m_hotspotId, aid);
});
connect(m_hotspotList, &QListWidget::currentItemChanged, this, [this](QListWidgetItem* current, QListWidgetItem*) {
const QString hotspotId = current ? current->data(Qt::UserRole).toString() : QString();
if (hotspotId == m_hotspotId) {
return;
}
m_hotspotId = hotspotId;
emit hotspotSelected(hotspotId);
});
connect(m_hotspotList, &QListWidget::customContextMenuRequested, this, [this](const QPoint& pos) {
if (!m_hotspotList) {
return;
}
auto* item = m_hotspotList->itemAt(pos);
if (!item) {
return;
}
const QString hotspotId = item->data(Qt::UserRole).toString();
if (hotspotId.isEmpty()) {
return;
}
m_hotspotList->setCurrentItem(item);
QMenu menu(this);
auto* actDelete = menu.addAction(QStringLiteral("移除热点"));
if (menu.exec(m_hotspotList->viewport()->mapToGlobal(pos)) == actDelete) {
emit deleteRequested(hotspotId);
}
});
connect(m_btnDelete, &QPushButton::clicked, this, [this]() {
if (!m_hotspotId.isEmpty()) {
emit deleteRequested(m_hotspotId);
}
});
}
void HotspotPropertySection::clearDisconnected() {
m_hotspotId.clear();
}
void HotspotPropertySection::applyState(const QVector<core::Project::PresentationHotspot>& hotspots,
const QString& selectedHotspotId,
const QVector<QPair<QString, QString>>& animIdToLabelNonNone) {
m_hotspotId = selectedHotspotId;
if (!m_targetAnim || !m_hotspotList) {
return;
}
QString targetAnimationId;
QHash<QString, QString> animLabelById;
for (const auto& p : animIdToLabelNonNone) {
animLabelById.insert(p.first, p.second);
}
m_hotspotList->blockSignals(true);
m_hotspotList->clear();
QListWidgetItem* selectedItem = nullptr;
for (const auto& hotspot : hotspots) {
QString text = hotspot.id;
const QString animLabel = animLabelById.value(hotspot.targetAnimationId);
if (!animLabel.isEmpty()) {
text += QStringLiteral(" -> ") + animLabel;
} else if (!hotspot.targetAnimationId.isEmpty()) {
text += QStringLiteral(" -> ") + hotspot.targetAnimationId;
} else {
text += QStringLiteral(" -> 未设");
}
auto* item = new QListWidgetItem(text, m_hotspotList);
item->setData(Qt::UserRole, hotspot.id);
if (hotspot.id == selectedHotspotId) {
selectedItem = item;
targetAnimationId = hotspot.targetAnimationId;
}
}
if (selectedItem) {
m_hotspotList->setCurrentItem(selectedItem);
} else {
m_hotspotList->setCurrentRow(-1);
m_hotspotId.clear();
}
m_hotspotList->blockSignals(false);
m_targetAnim->blockSignals(true);
m_targetAnim->clear();
m_targetAnim->addItem(QStringLiteral("未设"), QString());
for (const auto& p : animIdToLabelNonNone) {
m_targetAnim->addItem(p.second, p.first);
}
int idx = -1;
for (int i = 0; i < m_targetAnim->count(); ++i) {
if (m_targetAnim->itemData(i).toString() == targetAnimationId) {
idx = i;
break;
}
}
if (idx >= 0) {
m_targetAnim->setCurrentIndex(idx);
} else {
m_targetAnim->setCurrentIndex(0);
}
m_targetAnim->blockSignals(false);
const bool ok = !m_hotspotId.isEmpty() && m_targetAnim->count() > 0;
m_targetAnim->setEnabled(ok);
if (m_btnDelete) {
m_btnDelete->setEnabled(!m_hotspotId.isEmpty());
}
}
} // namespace gui