DYTSrouce/src/ui/Menu/SimuRunMenu.cpp
2025-10-14 00:15:18 +08:00

108 lines
3.0 KiB
C++

#include "ui/Menu/SimuRunMenu.h"
#include <QToolButton>
#include <QHBoxLayout>
#include <QLabel>
#include "workspace/WorkSpaceManager.h"
#include "workspace/WorkSpace.h"
#include "workspace/CommandManager.h"
SimuRunMenu::SimuRunMenu(QWidget *parent)
: QWidget(parent) {
ui.setupUi(this);
// Create a vertical layout to host grouped command rows
auto* layout = new QVBoxLayout(this);
layout->setContentsMargins(9, 0, 0, 0);
layout->setSpacing(6);
setLayout(layout);
// Refresh when workspace changes
connect(&WorkSpaceManager::Get(), &WorkSpaceManager::WorkSpaceChanged,
this, &SimuRunMenu::OnWorkspaceChanged);
// Initial population
RefreshButtons();
}
SimuRunMenu::~SimuRunMenu() {
}
void SimuRunMenu::OnWorkspaceChanged(WorkSpace* ws) {
Q_UNUSED(ws);
RefreshButtons();
}
void SimuRunMenu::RefreshButtons() {
// Clear existing buttons
if (auto* layout = qobject_cast<QVBoxLayout*>(this->layout())) {
while (layout->count() > 0) {
QLayoutItem* item = layout->takeAt(0);
if (item) {
if (auto* w = item->widget()) {
w->deleteLater();
}
delete item;
}
}
}
auto* ws = WorkSpaceManager::Get().GetCurrent();
if (!ws) {
// Show hint when no workspace
if (auto* layout = qobject_cast<QVBoxLayout*>(this->layout())) {
auto* hint = new QLabel(tr("no workspace"), this);
layout->addWidget(hint);
}
return;
}
CommandManager mgr;
const auto items = mgr.ListCommands(ws);
if (items.empty()) {
if (auto* layout = qobject_cast<QVBoxLayout*>(this->layout())) {
auto* hint = new QLabel(tr("no commands"), this);
layout->addWidget(hint);
}
return;
}
// Render all commands in a single group without trigger distinction
CreateGroup(tr("Commands"), items);
}
void SimuRunMenu::CreateGroup(const QString& title,
const std::vector<Command>& items) {
auto* root = qobject_cast<QVBoxLayout*>(this->layout());
if (!root) return;
auto* label = new QLabel(title, this);
root->addWidget(label);
auto* row = new QHBoxLayout();
row->setSpacing(6);
for (const auto& item : items) {
auto* btn = new QToolButton(this);
btn->setText(item.name.isEmpty() ? tr("unnamed") : item.name);
QString tip = item.descript;
if (!item.program.isEmpty()) {
tip += QStringLiteral("\nprog: ") + item.program;
}
if (!item.path.isEmpty()) {
tip += QStringLiteral("\npath: ") + item.path;
}
btn->setToolTip(tip);
row->addWidget(btn);
connect(btn, &QToolButton::clicked, this, [name = item.name]() {
auto* wsCur = WorkSpaceManager::Get().GetCurrent();
if (!wsCur) return;
CommandManager execMgr;
execMgr.ExecuteByName(wsCur, name);
});
}
row->addStretch(1);
root->addLayout(row);
}