pages/ was a flat row of eight screens, so a change to one meant scanning the whole folder to see what else was in it. The screens now sit in packages/ (browse, cleanup), overview/ (dashboard, history), system/ (logs, settings) and extensions/ (plugins), with the scaffolding they all build on — the Page base and the shared widgets — in base/.
174 lines
5.7 KiB
C++
174 lines
5.7 KiB
C++
#include "ui-kit.h"
|
|
|
|
#include <QAbstractItemView>
|
|
#include <QCoreApplication>
|
|
#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));
|
|
}
|
|
|
|
// A pill, like the website's .paquet-since / .nav-dropdown-version tags: a low
|
|
// tint of a colour as background, the colour itself as text, fully rounded.
|
|
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: 999px;"
|
|
"padding: 2px 10px; font-size: 11px; font-weight: 700;")
|
|
.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: #64748b; 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);
|
|
}
|
|
|
|
void setEmptyStateText(QStackedWidget* stack, const QString& hint) {
|
|
if (!stack) return;
|
|
// withEmptyState() puts the hint label second in the placeholder's layout:
|
|
// the only label of the stack, so this stays a safe lookup.
|
|
if (QLabel* label = stack->findChild<QLabel*>()) label->setText(hint);
|
|
}
|
|
|
|
QLabel* countLabel() {
|
|
QLabel* label = new QLabel();
|
|
label->setStyleSheet("font-size: 12px; color: #64748b; 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 QCoreApplication::translate("ui", "MAJ dispo");
|
|
if (pkg.installed) return QCoreApplication::translate("ui", "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
|