211 lines
5.1 KiB
C++
211 lines
5.1 KiB
C++
#include "ui/Menu/SimuRunMenu.h"
|
||
#include "ui/Layout/AddParamSetting.h"
|
||
#include "ui/DockWidget.h"
|
||
#include "ui/DockTitleBar.h"
|
||
#include "ui/MainWindow.h"
|
||
|
||
#include <QHBoxLayout>
|
||
#include <QLabel>
|
||
#include <QGroupBox>
|
||
|
||
#include "workspace/WorkSpaceManager.h"
|
||
#include "workspace/WorkSpace.h"
|
||
#include "workspace/CommandManager.h"
|
||
|
||
SimuRunMenu::SimuRunMenu(QWidget *parent)
|
||
: QWidget(parent)
|
||
{
|
||
m_pParam = NULL;
|
||
m_mainWindow = NULL;
|
||
|
||
QHBoxLayout* layout = new QHBoxLayout(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
|
||
ClearAllBtn();
|
||
|
||
CreateMatlabParam();
|
||
CreateLine();
|
||
|
||
auto* ws = WorkSpaceManager::Get().GetCurrent();
|
||
if (!ws)
|
||
{
|
||
// Show hint when no workspace
|
||
if (auto* layout = qobject_cast<QHBoxLayout*>(this->layout())) {
|
||
auto* hint = new QLabel(tr("no workspace"), this);
|
||
layout->addWidget(hint);
|
||
}
|
||
|
||
CreateSpacer();
|
||
|
||
return;
|
||
}
|
||
|
||
CommandManager mgr;
|
||
const auto items = mgr.ListCommands(ws);
|
||
if (items.empty())
|
||
{
|
||
if (auto* layout = qobject_cast<QHBoxLayout*>(this->layout())) {
|
||
auto* hint = new QLabel(tr("no commands"), this);
|
||
layout->addWidget(hint);
|
||
}
|
||
|
||
CreateSpacer();
|
||
|
||
return;
|
||
}
|
||
|
||
// Render all commands in a single group without trigger distinction
|
||
CreateGroup(tr("Commands"), items);
|
||
|
||
CreateSpacer();
|
||
}
|
||
|
||
void SimuRunMenu::CreateGroup(const QString& title,
|
||
const std::vector<Command>& items) {
|
||
auto* root = qobject_cast<QHBoxLayout*>(this->layout());
|
||
if (!root) return;
|
||
|
||
QGroupBox * groupBox = new QGroupBox;
|
||
QHBoxLayout * hGroupLayout = new QHBoxLayout;
|
||
hGroupLayout->setContentsMargins(10, 0, 10, 0);
|
||
groupBox->setTitle(title);
|
||
groupBox->setAlignment(Qt::AlignHCenter);
|
||
|
||
for (const auto& item : items) {
|
||
auto* btn = new QToolButton(this);
|
||
btn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
|
||
btn->setText(item.name.isEmpty() ? tr("unnamed") : item.name);
|
||
btn->setIcon(QIcon(":/res/default/Command.png"));
|
||
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);
|
||
hGroupLayout->addWidget(btn);
|
||
|
||
connect(btn, &QToolButton::clicked, this, [name = item.name]() {
|
||
auto* wsCur = WorkSpaceManager::Get().GetCurrent();
|
||
if (!wsCur) return;
|
||
CommandManager execMgr;
|
||
execMgr.ExecuteByName(wsCur, name);
|
||
});
|
||
}
|
||
|
||
groupBox->setLayout(hGroupLayout);
|
||
root->addWidget(groupBox);
|
||
}
|
||
|
||
void SimuRunMenu::CreateMatlabParam()
|
||
{
|
||
QHBoxLayout* layout = qobject_cast<QHBoxLayout*>(this->layout());
|
||
if (!layout)
|
||
return;
|
||
|
||
QToolButton *pBtn = new QToolButton();
|
||
pBtn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
|
||
pBtn->setText(QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"));
|
||
pBtn->setIcon(QIcon(":/res/default/menu_setting_restore.png"));
|
||
pBtn->setToolTip(QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Matlab<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"));
|
||
|
||
connect(pBtn, SIGNAL(clicked()), this, SLOT(slotHandleMatlabParam()));
|
||
|
||
layout->addWidget(pBtn);
|
||
}
|
||
|
||
void SimuRunMenu::CreateLine()
|
||
{
|
||
QHBoxLayout* layout = qobject_cast<QHBoxLayout*>(this->layout());
|
||
if (!layout)
|
||
return;
|
||
|
||
QFrame *line = new QFrame(this);
|
||
line->setObjectName(QString::fromUtf8("line"));
|
||
line->setFrameShadow(QFrame::Plain);
|
||
line->setFrameShape(QFrame::VLine);
|
||
|
||
layout->addWidget(line);
|
||
}
|
||
|
||
void SimuRunMenu::CreateSpacer()
|
||
{
|
||
QHBoxLayout* layout = qobject_cast<QHBoxLayout*>(this->layout());
|
||
if (!layout)
|
||
return;
|
||
|
||
QSpacerItem *horizontalSpacer = new QSpacerItem(441, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
|
||
layout->addItem(horizontalSpacer);
|
||
}
|
||
|
||
void SimuRunMenu::ClearAllBtn()
|
||
{
|
||
if (auto* layout = qobject_cast<QHBoxLayout*>(this->layout()))
|
||
{
|
||
while (layout->count() > 0)
|
||
{
|
||
QLayoutItem* item = layout->takeAt(0);
|
||
if (item)
|
||
{
|
||
if (auto* w = item->widget())
|
||
{
|
||
w->deleteLater();
|
||
}
|
||
delete item;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
void SimuRunMenu::slotHandleMatlabParam()
|
||
{
|
||
if (WorkSpaceManager::Get().GetCurrent())
|
||
{
|
||
if (!m_pParam)
|
||
{
|
||
m_pParam = new DockWidget(m_mainWindow);
|
||
m_pParam->setAllowedAreas(Qt::NoDockWidgetArea);
|
||
|
||
DockTitleBar* titleBar = new DockTitleBar(m_pParam);
|
||
titleBar->SetFloatVisible(false);
|
||
titleBar->SetMaxVisible(false);
|
||
m_pParam->SetDockWidgetTitleBar(titleBar);
|
||
m_pParam->setWindowTitle(QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"));
|
||
|
||
QString strDir = WorkSpaceManager::Get().GetCurrent()->GetDir();
|
||
AddParamSetting *pParam = new AddParamSetting(strDir);
|
||
m_pParam->setWidget(pParam);
|
||
|
||
m_pParam->resize(1000, 600);
|
||
}
|
||
|
||
m_pParam->setFloating(true);
|
||
m_pParam->show();
|
||
}
|
||
}
|
||
|
||
void SimuRunMenu::SetMainWindow(MainWindow* mainWindow)
|
||
{
|
||
m_mainWindow = mainWindow;
|
||
} |