XPlor/app/definitionviewer.cpp

118 lines
3.8 KiB
C++
Raw Normal View History

#include "definitionviewer.h"
#include <QFileInfo>
#include <QDir>
#include <QCoreApplication>
DefinitionViewer::DefinitionViewer(const QVector<DefinitionLoadResult>& results, QWidget *parent)
: QDialog(parent)
, mResults(results)
{
setWindowTitle("XPlor - Definition Viewer");
setMinimumSize(600, 400);
resize(700, 500);
auto* layout = new QVBoxLayout(this);
mSummaryLabel = new QLabel(this);
layout->addWidget(mSummaryLabel);
mTreeWidget = new QTreeWidget(this);
mTreeWidget->setHeaderLabels({"Definition", "Status", "Error"});
mTreeWidget->header()->setSectionResizeMode(0, QHeaderView::Stretch);
mTreeWidget->header()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
mTreeWidget->header()->setSectionResizeMode(2, QHeaderView::Stretch);
mTreeWidget->setAlternatingRowColors(true);
layout->addWidget(mTreeWidget);
auto* closeButton = new QPushButton("Close", this);
connect(closeButton, &QPushButton::clicked, this, &QDialog::accept);
layout->addWidget(closeButton);
populateTree();
}
void DefinitionViewer::setResults(const QVector<DefinitionLoadResult>& results)
{
mResults = results;
populateTree();
}
void DefinitionViewer::populateTree()
{
mTreeWidget->clear();
// Group by subdirectory
QMap<QString, QVector<const DefinitionLoadResult*>> byFolder;
QString basePath = QCoreApplication::applicationDirPath() + "/definitions/";
for (const auto& result : mResults) {
QString relativePath = result.filePath;
if (relativePath.startsWith(basePath)) {
relativePath = relativePath.mid(basePath.length());
}
QFileInfo fi(relativePath);
QString folder = fi.path();
if (folder == ".") folder = "(root)";
byFolder[folder].append(&result);
}
int successCount = 0;
int failCount = 0;
for (auto it = byFolder.begin(); it != byFolder.end(); ++it) {
const QString& folder = it.key();
const auto& items = it.value();
auto* folderItem = new QTreeWidgetItem(mTreeWidget);
folderItem->setText(0, folder);
folderItem->setExpanded(true);
int folderSuccess = 0;
int folderFail = 0;
for (const auto* result : items) {
auto* item = new QTreeWidgetItem(folderItem);
item->setText(0, result->fileName);
if (result->success) {
item->setText(1, "OK");
item->setForeground(0, QColor(0, 128, 0));
item->setForeground(1, QColor(0, 128, 0));
folderSuccess++;
successCount++;
} else {
item->setText(1, "FAILED");
item->setText(2, result->errorMessage);
item->setForeground(0, QColor(192, 0, 0));
item->setForeground(1, QColor(192, 0, 0));
item->setForeground(2, QColor(128, 128, 128));
folderFail++;
failCount++;
}
}
// Set folder color based on contents
if (folderFail == 0) {
folderItem->setForeground(0, QColor(0, 128, 0));
} else if (folderSuccess == 0) {
folderItem->setForeground(0, QColor(192, 0, 0));
} else {
folderItem->setForeground(0, QColor(200, 140, 0));
}
folderItem->setText(1, QString("%1/%2").arg(folderSuccess).arg(folderSuccess + folderFail));
}
mSummaryLabel->setText(QString("Loaded: %1 successful, %2 failed (%3 total)")
.arg(successCount)
.arg(failCount)
.arg(successCount + failCount));
if (failCount > 0) {
mSummaryLabel->setStyleSheet("color: #c00000; font-weight: bold;");
} else {
mSummaryLabel->setStyleSheet("color: #008000; font-weight: bold;");
}
}