Files
FWS-PacManage/pages/dashboard-page.cpp
T
BlackAngelTV 897c174925 refactor(arch): split the app into a logic core and per-screen pages
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>
2026-09-13 11:45:48 +02:00

206 lines
8.1 KiB
C++

#include "dashboard-page.h"
#include <QAbstractItemView>
#include <QFrame>
#include <QHBoxLayout>
#include <QLabel>
#include <QListWidget>
#include <QListWidgetItem>
#include <QPushButton>
#include <QVBoxLayout>
#include "ui-kit.h"
DashboardPage::DashboardPage(AppCore* core, QWidget* parent) : Page(core, parent) {
setObjectName("dashboardPage");
QVBoxLayout* layout = new QVBoxLayout(this);
layout->setContentsMargins(24, 20, 24, 20);
layout->setSpacing(16);
// Stat cards
QHBoxLayout* statsRow = new QHBoxLayout();
statsRow->setSpacing(12);
statsRow->addWidget(
ui::statCard("package", "Paquets installés", "blue", &m_statInstalled));
statsRow->addWidget(
ui::statCard("arrow-up", "Mises à jour dispo", "amber", &m_statUpdates));
statsRow->addWidget(
ui::statCard("history", "Actions enregistrées", "cyan", &m_statHistory));
statsRow->addWidget(
ui::statCard("tools", "Gestionnaire", "green", &m_statManager));
layout->addLayout(statsRow);
// Quick actions
QFrame* actionsCard = ui::card();
QVBoxLayout* actionsLayout = new QVBoxLayout(actionsCard);
actionsLayout->setContentsMargins(18, 16, 18, 16);
actionsLayout->setSpacing(10);
QLabel* actionsTitle = new QLabel("ACTIONS RAPIDES");
actionsTitle->setObjectName("cardTitle");
actionsLayout->addWidget(actionsTitle);
QHBoxLayout* actionsRow = new QHBoxLayout();
actionsRow->setSpacing(10);
QPushButton* btnSearch = new QPushButton("Rechercher un paquet");
btnSearch->setProperty("variant", "primary");
btnSearch->setIcon(ui::icon("search"));
connect(btnSearch, &QPushButton::clicked, this,
[this] { emit goToPage("search"); });
actionsRow->addWidget(btnSearch);
QPushButton* btnInstalled = new QPushButton("Paquets installés");
btnInstalled->setIcon(ui::icon("package"));
connect(btnInstalled, &QPushButton::clicked, this,
[this] { emit goToPage("installed"); });
actionsRow->addWidget(btnInstalled);
QPushButton* btnUpdateAll = new QPushButton("Tout mettre à jour");
btnUpdateAll->setProperty("variant", "warning");
btnUpdateAll->setIcon(ui::icon("arrow-up"));
connect(btnUpdateAll, &QPushButton::clicked, this,
[this, core] { start(core->updateAll(), tr("Mise à jour globale")); });
actionsRow->addWidget(btnUpdateAll);
QPushButton* btnCleanup = new QPushButton("Nettoyer le système");
btnCleanup->setIcon(ui::icon("broom"));
btnCleanup->setToolTip("Purger les paquets inutilisés et vider les caches");
connect(btnCleanup, &QPushButton::clicked, this,
[this] { emit goToPage("cleanup"); });
actionsRow->addWidget(btnCleanup);
QPushButton* btnRefresh = new QPushButton("Rafraîchir les sources");
btnRefresh->setProperty("variant", "info");
btnRefresh->setIcon(ui::icon("refresh"));
connect(btnRefresh, &QPushButton::clicked, this,
[this, core] { start(core->refresh(), tr("Rafraîchissement des sources")); });
actionsRow->addWidget(btnRefresh);
QPushButton* btnLogs = new QPushButton("Logs pacman");
btnLogs->setIcon(ui::icon("logs"));
connect(btnLogs, &QPushButton::clicked, this, [this] { emit goToPage("logs"); });
actionsRow->addWidget(btnLogs);
actionsRow->addStretch();
actionsLayout->addLayout(actionsRow);
layout->addWidget(actionsCard);
// Recent activity
QFrame* recentCard = ui::card();
QVBoxLayout* recentLayout = new QVBoxLayout(recentCard);
recentLayout->setContentsMargins(18, 16, 18, 12);
recentLayout->setSpacing(8);
QLabel* recentTitle = new QLabel("ACTIVITÉ RÉCENTE");
recentTitle->setObjectName("cardTitle");
recentLayout->addWidget(recentTitle);
m_recentList = new QListWidget();
m_recentList->setObjectName("recentList");
m_recentList->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
m_recentList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_recentList->setFocusPolicy(Qt::NoFocus);
connect(m_recentList, &QListWidget::itemClicked, this,
&DashboardPage::onActivityClicked);
recentLayout->addWidget(m_recentList, 1);
layout->addWidget(recentCard, 1);
// The figures and the activity list both come from the core.
connect(core, &AppCore::listFinished, this,
[this](AppCore::ListMode mode, std::vector<PackageInfo> results) {
if (mode == AppCore::ListMode::Installed) {
m_statInstalled->setText(QString::number(results.size()));
} else if (mode == AppCore::ListMode::Updates) {
m_statUpdates->setText(QString::number(results.size()));
m_statUpdates->setStyleSheet(results.empty()
? QString()
: QString("color: #e0af68;"));
}
});
connect(core, &AppCore::historyChanged, this, &DashboardPage::refresh);
}
void DashboardPage::refresh() {
m_statHistory->setText(QString::number(core()->historyCount()));
m_statManager->setText(core()->hasAvailableProvider()
? core()->availableProviderNames().join(", ")
: QStringLiteral("indisponible"));
refreshRecentActivity();
// The listing itself arrives through listFinished: the search/installed
// pages share it, so nothing is queried twice.
if (core()->hasAvailableProvider()) {
start(core()->loadCounts(), tr("Tableau de bord"));
}
}
void DashboardPage::refreshRecentActivity() {
m_recentList->clear();
const auto& history = core()->history()->getHistory();
if (history.empty()) {
QListWidgetItem* empty =
new QListWidgetItem("Aucune action enregistrée pour le moment");
empty->setFlags(Qt::NoItemFlags);
empty->setTextAlignment(Qt::AlignCenter);
m_recentList->addItem(empty);
return;
}
int start = std::max(0, static_cast<int>(history.size()) - 8);
for (int i = static_cast<int>(history.size()) - 1; i >= start; --i) {
const HistoryEntry& entry = history[i];
QListWidgetItem* item = new QListWidgetItem();
item->setSizeHint(QSize(0, 42));
// Keep the package name so a click can open its details.
item->setData(Qt::UserRole, QString::fromStdString(entry.package));
QWidget* row = new QWidget();
QHBoxLayout* rowLayout = new QHBoxLayout(row);
rowLayout->setContentsMargins(10, 4, 10, 4);
rowLayout->setSpacing(10);
QLabel* timestamp = new QLabel(QString::fromStdString(entry.timestamp));
timestamp->setStyleSheet(
"background: transparent; color: #5f6785;"
"font-family: monospace; font-size: 12px;");
rowLayout->addWidget(timestamp);
QString actionText, actionBg, actionFg;
if (entry.action == "install") {
actionText = "Installation"; actionBg = "#12301f"; actionFg = "#3fb950";
} else if (entry.action == "remove") {
actionText = "Suppression"; actionBg = "#3a1a1a"; actionFg = "#f7768e";
} else if (entry.action == "cleanup") {
actionText = "Nettoyage"; actionBg = "#12333a"; actionFg = "#56b6c2";
} else {
actionText = "Mise à jour"; actionBg = "#3d2f0f"; actionFg = "#e0af68";
}
rowLayout->addWidget(ui::badge(actionText, actionBg, actionFg));
QLabel* package = new QLabel(QString::fromStdString(entry.package));
package->setStyleSheet("background: transparent; font-weight: 600; color: #c8d3f5;");
rowLayout->addWidget(package);
rowLayout->addStretch();
QLabel* status = new QLabel();
status->setPixmap(ui::iconPixmap(entry.success ? "check" : "close", 16));
status->setStyleSheet("background: transparent;");
rowLayout->addWidget(status);
m_recentList->addItem(item);
m_recentList->setItemWidget(item, row);
}
}
void DashboardPage::onActivityClicked(QListWidgetItem* item) {
if (!item) return;
const QString name = item->data(Qt::UserRole).toString();
if (name.isEmpty()) return;
emit openPackage(name);
}