initial port for Cevo game in C++ (libs)

This commit is contained in:
Geoffray Levasseur committed 2026-10-22 17:20:45 +02:00
commit 8436242032
375 files changed
+46282

No files matched your search

+134
View File
@@ -0,0 +1,134 @@
#include "Cevo/FormTests.h"
#include "Cevo/FormTest.h"
#include "ui_FormTests.h"
#include <QMenu>
namespace Cevo {
namespace {
TestCase *selectedTestCase(QTreeWidget *tree)
{
QTreeWidgetItem *item = tree->currentItem();
if (!item)
return nullptr;
return static_cast<TestCase *>(item->data(0, Qt::UserRole).value<void *>());
}
} // namespace
FormTests::FormTests(QWidget *parent) : FormBase(parent), ui(new Ui::FormTests)
{
ui->setupUi(this);
connect(ui->buttonRun, &QPushButton::clicked, this, &FormTests::onRunClicked);
// Matches the original .lfm's ButtonClose OnClick=ButtonRunClick quirk.
connect(ui->buttonClose, &QPushButton::clicked, this, &FormTests::onRunClicked);
connect(ui->listViewTestCases, &QTreeWidget::itemSelectionChanged, this, &FormTests::onSelectionChanged);
connect(ui->listViewTestCases, &QTreeWidget::itemDoubleClicked, this, &FormTests::onShowSelected);
connect(ui->listViewTestCases, &QTreeWidget::customContextMenuRequested, this, [this](const QPoint &pos) {
QMenu menu(this);
QAction *show = menu.addAction(tr("Show"), this, &FormTests::onShowSelected);
QAction *run = menu.addAction(tr("Run"), this, &FormTests::onRunSelected);
const bool hasSelection = ui->listViewTestCases->currentItem() != nullptr;
show->setEnabled(hasSelection);
run->setEnabled(hasSelection);
menu.exec(ui->listViewTestCases->viewport()->mapToGlobal(pos));
});
}
FormTests::~FormTests()
{
delete ui;
}
void FormTests::setTestCases(TestCases *value)
{
if (m_testCases == value)
return;
m_testCases = value;
reloadList();
updateInterface();
}
void FormTests::showEvent(QShowEvent *event)
{
FormBase::showEvent(event);
reloadList();
updateInterface();
}
void FormTests::reloadList()
{
ui->listViewTestCases->clear();
if (!m_testCases)
return;
for (int i = 0; i < m_testCases->count(); ++i) {
TestCase *testCase = m_testCases->at(i);
auto *item = new QTreeWidgetItem(ui->listViewTestCases);
item->setText(0, testCase->name);
item->setText(1, testResultText(testCase->testResult));
item->setData(0, Qt::UserRole, QVariant::fromValue(static_cast<void *>(testCase)));
}
}
void FormTests::updateInterface()
{
const bool selected = ui->listViewTestCases->currentItem() != nullptr;
ui->buttonRun->setEnabled(m_testCases != nullptr);
Q_UNUSED(selected);
int passed = 0, failed = 0;
if (m_testCases) {
for (int i = 0; i < m_testCases->count(); ++i) {
switch (m_testCases->at(i)->testResult) {
case TestResult::Passed:
++passed;
break;
case TestResult::Failed:
++failed;
break;
default:
break;
}
}
}
ui->labelResult->setText(tr("Passed") + ' ' + QString::number(passed) + ", " + tr("Failed") + ' ' +
QString::number(failed));
}
void FormTests::onRunClicked()
{
if (!m_testCases)
return;
for (int i = 0; i < m_testCases->count(); ++i)
m_testCases->at(i)->run();
reloadList();
updateInterface();
}
void FormTests::onSelectionChanged()
{
updateInterface();
}
void FormTests::onShowSelected()
{
TestCase *testCase = selectedTestCase(ui->listViewTestCases);
if (!testCase)
return;
FormTest dialog(this);
dialog.setTestCase(testCase);
dialog.exec();
reloadList();
}
void FormTests::onRunSelected()
{
TestCase *testCase = selectedTestCase(ui->listViewTestCases);
if (!testCase)
return;
testCase->run();
reloadList();
updateInterface();
}
} // namespace Cevo