All the business logic now lives in AppCore (providers, workers, history, plugins, sudo and pacman-lock handling), freed from any widget so it runs headless and is testable on its own. The Qt window becomes a thin frame around one file per screen under pages/, wired through a common Page contract and a shared ui-kit. CMake gains the app-core-test target, which links Qt Core only to enforce the headless guarantee. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
164 lines
5.1 KiB
C++
164 lines
5.1 KiB
C++
#include "ui-kit.h"
|
|
|
|
#include <QAbstractItemView>
|
|
#include <QHeaderView>
|
|
#include <QLabel>
|
|
#include <QSignalBlocker>
|
|
#include <QStackedWidget>
|
|
#include <QTableWidget>
|
|
#include <QVBoxLayout>
|
|
|
|
namespace ui {
|
|
|
|
QIcon icon(const QString& name) {
|
|
return QIcon(QString(":/icons/%1.svg").arg(name));
|
|
}
|
|
|
|
QPixmap iconPixmap(const QString& name, int size) {
|
|
return icon(name).pixmap(QSize(size, size));
|
|
}
|
|
|
|
QLabel* badge(const QString& text, const QString& background, const QString& foreground) {
|
|
QLabel* label = new QLabel(text);
|
|
label->setStyleSheet(QString(
|
|
"background: %1; color: %2; border-radius: 9px;"
|
|
"padding: 2px 9px; font-size: 11px; font-weight: 600;")
|
|
.arg(background, foreground));
|
|
return label;
|
|
}
|
|
|
|
QFrame* card() {
|
|
QFrame* frame = new QFrame();
|
|
frame->setObjectName("card");
|
|
return frame;
|
|
}
|
|
|
|
QFrame* statCard(const QString& iconName, const QString& title, const QString& accent,
|
|
QLabel** valueOut) {
|
|
QFrame* frame = card();
|
|
// Read by the stylesheet rule that draws the coloured left edge.
|
|
frame->setProperty("accent", accent);
|
|
QVBoxLayout* layout = new QVBoxLayout(frame);
|
|
layout->setContentsMargins(18, 16, 18, 16);
|
|
layout->setSpacing(6);
|
|
|
|
QLabel* iconLabel = new QLabel();
|
|
iconLabel->setPixmap(iconPixmap(iconName, 24));
|
|
iconLabel->setStyleSheet("background: transparent;");
|
|
layout->addWidget(iconLabel);
|
|
|
|
QLabel* value = new QLabel("…");
|
|
value->setObjectName("statValue");
|
|
layout->addWidget(value);
|
|
|
|
QLabel* titleLabel = new QLabel(title);
|
|
titleLabel->setObjectName("statTitle");
|
|
layout->addWidget(titleLabel);
|
|
|
|
if (valueOut) *valueOut = value;
|
|
return frame;
|
|
}
|
|
|
|
QTableWidget* table(const QStringList& headers, bool allStretch) {
|
|
QTableWidget* widget = new QTableWidget(0, headers.size());
|
|
widget->setHorizontalHeaderLabels(headers);
|
|
widget->verticalHeader()->setVisible(false);
|
|
widget->verticalHeader()->setDefaultSectionSize(36);
|
|
widget->setSelectionBehavior(QAbstractItemView::SelectRows);
|
|
widget->setSelectionMode(QAbstractItemView::SingleSelection);
|
|
widget->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
|
widget->setShowGrid(false);
|
|
widget->setAlternatingRowColors(true);
|
|
widget->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
|
|
|
|
for (int column = 0; column < headers.size(); ++column) {
|
|
widget->horizontalHeader()->setSectionResizeMode(
|
|
column,
|
|
allStretch ? QHeaderView::Stretch
|
|
: (column == 0 ? QHeaderView::Stretch : QHeaderView::ResizeToContents));
|
|
}
|
|
return widget;
|
|
}
|
|
|
|
QStackedWidget* withEmptyState(QTableWidget* widget, const QString& hint) {
|
|
QStackedWidget* stack = new QStackedWidget();
|
|
stack->addWidget(widget);
|
|
|
|
QWidget* placeholder = new QWidget();
|
|
QVBoxLayout* layout = new QVBoxLayout(placeholder);
|
|
layout->addStretch();
|
|
QLabel* label = new QLabel(hint);
|
|
label->setAlignment(Qt::AlignCenter);
|
|
label->setWordWrap(true);
|
|
label->setStyleSheet("background: transparent; color: #565f73; font-size: 13px;");
|
|
layout->addWidget(label);
|
|
layout->addStretch();
|
|
stack->addWidget(placeholder);
|
|
return stack;
|
|
}
|
|
|
|
void showEmptyState(QStackedWidget* stack, bool empty) {
|
|
if (stack) stack->setCurrentIndex(empty ? 1 : 0);
|
|
}
|
|
|
|
QLabel* countLabel() {
|
|
QLabel* label = new QLabel();
|
|
label->setStyleSheet("font-size: 12px; color: #7a8299; font-weight: 600;");
|
|
return label;
|
|
}
|
|
|
|
QString providersLabel(const PluginManager* plugins, const PackageInfo& pkg) {
|
|
auto labelFor = [plugins](const std::string& id) {
|
|
for (const auto& entry : plugins->plugins()) {
|
|
if (entry.info.id == id) return QString::fromStdString(entry.info.name);
|
|
}
|
|
return QString::fromStdString(id);
|
|
};
|
|
|
|
QStringList names;
|
|
for (const auto& id : pkg.providers) names << labelFor(id);
|
|
if (names.isEmpty() && !pkg.provider.empty()) names << labelFor(pkg.provider);
|
|
return names.join(", ");
|
|
}
|
|
|
|
QString packageStatusText(const PackageInfo& pkg) {
|
|
if (pkg.installed && pkg.updateAvailable) return QStringLiteral("MAJ dispo");
|
|
if (pkg.installed) return QStringLiteral("Installé");
|
|
if (!pkg.repository.empty()) return QStringLiteral("Disponible");
|
|
return QStringLiteral("—");
|
|
}
|
|
|
|
QString csvField(const QString& value) {
|
|
if (!value.contains(';') && !value.contains('"') && !value.contains('\n')) {
|
|
return value;
|
|
}
|
|
QString escaped = value;
|
|
escaped.replace(QLatin1String("\""), QLatin1String("\"\""));
|
|
return QLatin1Char('"') + escaped + QLatin1Char('"');
|
|
}
|
|
|
|
struct TableFillBatch::Impl {
|
|
QTableWidget* table = nullptr;
|
|
std::unique_ptr<QSignalBlocker> blocker;
|
|
};
|
|
|
|
TableFillBatch::TableFillBatch(QTableWidget* widget)
|
|
: m_impl(std::make_unique<Impl>()) {
|
|
m_impl->table = widget;
|
|
if (!widget) return;
|
|
widget->setUpdatesEnabled(false);
|
|
m_impl->blocker = std::make_unique<QSignalBlocker>(widget);
|
|
}
|
|
|
|
void TableFillBatch::release() {
|
|
if (!m_impl->table || !m_impl->blocker) return;
|
|
m_impl->blocker.reset();
|
|
m_impl->table->setUpdatesEnabled(true);
|
|
}
|
|
|
|
TableFillBatch::~TableFillBatch() {
|
|
release();
|
|
}
|
|
|
|
} // namespace ui
|