start: fix filecard text visibility on classic theme also fixes hover and pressed states

This commit is contained in:
Alfredo Monclus
2025-02-26 15:17:46 -03:00
parent e4a14871f5
commit eae8aec93d
4 changed files with 71 additions and 127 deletions

View File

@@ -33,6 +33,7 @@
#include <QModelIndex>
#include <QVBoxLayout>
#include <QApplication>
#include <QPushButton>
#endif
#include "FileCardDelegate.h"
@@ -44,152 +45,98 @@
using namespace Start;
FileCardDelegate::FileCardDelegate(QObject* parent)
: QAbstractItemDelegate(parent)
: QStyledItemDelegate(parent)
{
_parameterGroup = App::GetApplication().GetParameterGroupByPath(
"User parameter:BaseApp/Preferences/Mod/Start");
_widget = std::make_unique<QWidget>();
_widget = std::make_unique<QPushButton>();
_widget->setObjectName(QLatin1String("thumbnailWidget"));
auto layout = gsl::owner<QVBoxLayout*>(new QVBoxLayout());
layout->setSpacing(0);
_widget->setLayout(layout);
}
QColor FileCardDelegate::getBorderColor() const
{
QColor color(98, 160, 234); // NOLINT
uint32_t packed = Base::Color::asPackedRGB<QColor>(color);
packed = _parameterGroup->GetUnsigned("FileThumbnailBorderColor", packed);
color = Base::Color::fromPackedRGB<QColor>(packed);
return color;
}
QColor FileCardDelegate::getBackgroundColor() const
{
QColor color(221, 221, 221); // NOLINT
uint32_t packed = Base::Color::asPackedRGB<QColor>(color);
packed = _parameterGroup->GetUnsigned("FileThumbnailBackgroundColor", packed);
color = Base::Color::fromPackedRGB<QColor>(packed);
return color;
}
QColor FileCardDelegate::getSelectionColor() const
{
QColor color(38, 162, 105); // NOLINT
uint32_t packed = Base::Color::asPackedRGB<QColor>(color);
packed = _parameterGroup->GetUnsigned("FileThumbnailSelectionColor", packed);
color = Base::Color::fromPackedRGB<QColor>(packed);
return color;
}
void FileCardDelegate::paint(QPainter* painter,
const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
painter->save();
// Step 1: Styling
QStyleOptionButton buttonOption;
buttonOption.initFrom(_widget.get());
buttonOption.rect = option.rect;
buttonOption.state = QStyle::State_Enabled;
if ((option.state & QStyle::State_MouseOver) != 0) {
buttonOption.state |= QStyle::State_MouseOver;
}
if ((option.state & QStyle::State_Selected) != 0) {
buttonOption.state |= QStyle::State_On;
}
if ((option.state & QStyle::State_Sunken) != 0) {
buttonOption.state |= QStyle::State_Sunken;
}
QApplication::style()->drawControl(QStyle::CE_PushButton, &buttonOption, painter);
// Step 2: Fetch required data
auto thumbnailSize =
static_cast<int>(_parameterGroup->GetInt("FileThumbnailIconsSize", 128)); // NOLINT
auto cardWidth = thumbnailSize;
auto baseName = index.data(static_cast<int>(DisplayedFilesModelRoles::baseName)).toString();
auto elidedName = painter->fontMetrics().elidedText(baseName, Qt::ElideRight, thumbnailSize);
auto size = index.data(static_cast<int>(DisplayedFilesModelRoles::size)).toString();
auto image = index.data(static_cast<int>(DisplayedFilesModelRoles::image)).toByteArray();
auto path = index.data(static_cast<int>(DisplayedFilesModelRoles::path)).toString();
painter->save();
auto thumbnail = std::make_unique<QLabel>();
auto pixmap = std::make_unique<QPixmap>();
auto layout = qobject_cast<QVBoxLayout*>(_widget->layout());
QPixmap pixmap;
if (!image.isEmpty()) {
pixmap->loadFromData(image);
if (!pixmap->isNull()) {
auto scaled = pixmap->scaled(QSize(thumbnailSize, thumbnailSize),
Qt::AspectRatioMode::KeepAspectRatio,
Qt::TransformationMode::SmoothTransformation);
thumbnail->setPixmap(scaled);
}
pixmap.loadFromData(image);
}
else {
thumbnail->setPixmap(generateThumbnail(path));
}
thumbnail->setFixedSize(thumbnailSize, thumbnailSize);
thumbnail->setSizePolicy(QSizePolicy::Policy::Fixed, QSizePolicy::Policy::Fixed);
QString style = QStringLiteral("");
_widget->setProperty("state", QStringLiteral(""));
if (option.state & QStyle::State_Selected) {
_widget->setProperty("state", QStringLiteral("pressed"));
if (qApp->styleSheet().isEmpty()) {
QColor color = getSelectionColor();
style = QStringLiteral("QWidget#thumbnailWidget {"
" border: 2px solid rgb(%1, %2, %3);"
" border-radius: 4px;"
" padding: 2px;"
"}")
.arg(color.red())
.arg(color.green())
.arg(color.blue());
}
}
else if (option.state & QStyle::State_MouseOver) {
_widget->setProperty("state", QStringLiteral("hovered"));
if (qApp->styleSheet().isEmpty()) {
QColor color = getBorderColor();
style = QStringLiteral("QWidget#thumbnailWidget {"
" border: 2px solid rgb(%1, %2, %3);"
" border-radius: 4px;"
" padding: 2px;"
"}")
.arg(color.red())
.arg(color.green())
.arg(color.blue());
}
}
else if (qApp->styleSheet().isEmpty()) {
QColor color = getBackgroundColor();
style = QStringLiteral("QWidget#thumbnailWidget {"
" background-color: rgb(%1, %2, %3);"
" border-radius: 8px;"
"}")
.arg(color.red())
.arg(color.green())
.arg(color.blue());
pixmap = generateThumbnail(path);
}
_widget->setStyleSheet(style);
QPixmap scaledPixmap = pixmap.scaled(QSize(thumbnailSize, thumbnailSize),
Qt::KeepAspectRatio,
Qt::SmoothTransformation);
auto elided =
painter->fontMetrics().elidedText(baseName, Qt::TextElideMode::ElideRight, cardWidth);
auto name = std::make_unique<QLabel>(elided);
layout->addWidget(thumbnail.get()); // Temp. ownership transfer
layout->addWidget(name.get()); // Temp. ownership transfer
auto sizeLabel = std::make_unique<QLabel>(size);
layout->addWidget(sizeLabel.get()); // Temp. ownership transfer
layout->addStretch();
_widget->resize(option.rect.size());
painter->translate(option.rect.topLeft());
_widget->render(painter, QPoint(), QRegion(), QWidget::DrawChildren);
// Step 4: Positioning
QRect thumbnailRect(option.rect.x() + margin,
option.rect.y() + margin,
thumbnailSize,
thumbnailSize);
QRect textRect(option.rect.x() + margin,
thumbnailRect.bottom() + margin,
thumbnailSize,
painter->fontMetrics().lineSpacing());
QRect sizeRect(option.rect.x() + margin,
textRect.bottom() + textspacing,
thumbnailSize,
painter->fontMetrics().lineSpacing() + margin);
// Step 5: Draw
painter->drawPixmap(thumbnailRect, scaledPixmap);
painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, elidedName);
painter->drawText(sizeRect, Qt::AlignLeft | Qt::AlignTop, size);
painter->restore();
layout->removeWidget(sizeLabel.get());
layout->removeWidget(thumbnail.get());
layout->removeWidget(name.get());
}
QSize FileCardDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
Q_UNUSED(option)
Q_UNUSED(index)
Q_UNUSED(option);
Q_UNUSED(index);
auto thumbnailSize = _parameterGroup->GetInt("FileThumbnailIconsSize", 128); // NOLINT
auto cardMargin = _widget->layout()->contentsMargins();
auto cardWidth = thumbnailSize + cardMargin.left() + cardMargin.right();
auto spacing = _widget->layout()->spacing();
auto font = QGuiApplication::font();
auto qfm = QFontMetrics(font);
auto textHeight = 2 * qfm.lineSpacing();
auto cardHeight =
thumbnailSize + textHeight + 2 * spacing + cardMargin.top() + cardMargin.bottom();
QFontMetrics qfm(QGuiApplication::font());
int textHeight = textspacing + qfm.lineSpacing() * 2; // name + size
int cardWidth = static_cast<int>(thumbnailSize) + 2 * margin;
int cardHeight = static_cast<int>(thumbnailSize) + textHeight + 3 * margin;
return {static_cast<int>(cardWidth), static_cast<int>(cardHeight)};
return {cardWidth, cardHeight};
}
namespace