Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start: Correct card size calculation #13673

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
94 changes: 49 additions & 45 deletions src/Mod/Start/Gui/FileCardDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
{
_parameterGroup = App::GetApplication().GetParameterGroupByPath(
"User parameter:BaseApp/Preferences/Mod/Start");
_widget = std::make_unique<QWidget>();
_widget->setObjectName(QLatin1String("thumbnailWidget"));
auto layout = gsl::owner<QVBoxLayout*>(new QVBoxLayout());
layout->setSpacing(0);
_widget->setLayout(layout);
}

QColor FileCardDelegate::getBorderColor() const
Expand Down Expand Up @@ -89,12 +94,9 @@
auto image = index.data(static_cast<int>(DisplayedFilesModelRoles::image)).toByteArray();
auto path = index.data(static_cast<int>(DisplayedFilesModelRoles::path)).toString();
painter->save();
auto widget = gsl::owner<QWidget*>(new QWidget());
widget->setObjectName(QLatin1String("thumbnailWidget"));
auto layout = gsl::owner<QVBoxLayout*>(new QVBoxLayout());
widget->setLayout(layout);
auto thumbnail = gsl::owner<QLabel*>(new QLabel());
auto pixmap = gsl::owner<QPixmap*>(new QPixmap());
auto thumbnail = std::make_unique<QLabel>();
auto pixmap = std::make_unique<QPixmap>();
auto layout = qobject_cast<QVBoxLayout*>(_widget->layout());
if (!image.isEmpty()) {
pixmap->loadFromData(image);
if (!pixmap->isNull()) {
Expand All @@ -110,61 +112,61 @@
thumbnail->setFixedSize(thumbnailSize, thumbnailSize);
thumbnail->setSizePolicy(QSizePolicy::Policy::Fixed, QSizePolicy::Policy::Fixed);

widget->setProperty("state", QStringLiteral(""));
_widget->setProperty("state", QStringLiteral(""));

Check warning on line 115 in src/Mod/Start/Gui/FileCardDelegate.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

Use an empty QLatin1String instead of an empty QStringLiteral [-Wclazy-empty-qstringliteral]
if (option.state & QStyle::State_Selected) {

Check warning on line 116 in src/Mod/Start/Gui/FileCardDelegate.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

implicit conversion 'QFlags<QStyle::StateFlag>::Int' (aka 'unsigned int') -> bool [readability-implicit-bool-conversion]
widget->setProperty("state", QStringLiteral("pressed"));
_widget->setProperty("state", QStringLiteral("pressed"));
if (qApp->styleSheet().isEmpty()) {
QColor color = getSelectionColor();
widget->setStyleSheet(QString::fromLatin1("QWidget#thumbnailWidget {"
" border: 2px solid rgb(%1, %2, %3);"
" border-radius: 4px;"
" padding: 2px;"
"}")
.arg(color.red())
.arg(color.green())
.arg(color.blue()));
_widget->setStyleSheet(QString::fromLatin1("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) {

Check warning on line 130 in src/Mod/Start/Gui/FileCardDelegate.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

implicit conversion 'QFlags<QStyle::StateFlag>::Int' (aka 'unsigned int') -> bool [readability-implicit-bool-conversion]
widget->setProperty("state", QStringLiteral("hovered"));
_widget->setProperty("state", QStringLiteral("hovered"));
if (qApp->styleSheet().isEmpty()) {
QColor color = getBorderColor();
widget->setStyleSheet(QString::fromLatin1("QWidget#thumbnailWidget {"
" border: 2px solid rgb(%1, %2, %3);"
" border-radius: 4px;"
" padding: 2px;"
"}")
.arg(color.red())
.arg(color.green())
.arg(color.blue()));
_widget->setStyleSheet(QString::fromLatin1("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();
widget->setStyleSheet(QString::fromLatin1("QWidget#thumbnailWidget {"
" background-color: rgb(%1, %2, %3);"
" border-radius: 8px;"
"}")
.arg(color.red())
.arg(color.green())
.arg(color.blue()));
_widget->setStyleSheet(QString::fromLatin1("QWidget#thumbnailWidget {"
" background-color: rgb(%1, %2, %3);"
" border-radius: 8px;"
"}")
.arg(color.red())
.arg(color.green())
.arg(color.blue()));
}

auto elided =
painter->fontMetrics().elidedText(baseName, Qt::TextElideMode::ElideRight, cardWidth);
auto name = gsl::owner<QLabel*>(new QLabel(elided));
layout->addWidget(thumbnail);
layout->addWidget(name);
auto sizeLabel = gsl::owner<QLabel*>(new QLabel(size));
layout->addWidget(sizeLabel);
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();
layout->setSpacing(0);
widget->resize(option.rect.size());
_widget->resize(option.rect.size());
painter->translate(option.rect.topLeft());
widget->render(painter, QPoint(), QRegion(), QWidget::DrawChildren);
_widget->render(painter, QPoint(), QRegion(), QWidget::DrawChildren);
painter->restore();
delete pixmap;
delete widget;
layout->removeWidget(sizeLabel.get());
layout->removeWidget(thumbnail.get());
layout->removeWidget(name.get());
}


Expand All @@ -173,13 +175,15 @@
Q_UNUSED(option)
Q_UNUSED(index)
auto thumbnailSize = _parameterGroup->GetInt("FileThumbnailIconsSize", 128); // NOLINT
auto cardSpacing = _parameterGroup->GetInt("FileCardSpacing", 30); // NOLINT
auto cardWidth = thumbnailSize + cardSpacing;
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 + cardSpacing;
auto cardHeight =
thumbnailSize + textHeight + 2 * spacing + cardMargin.top() + cardMargin.bottom();

Check warning on line 186 in src/Mod/Start/Gui/FileCardDelegate.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

performing an implicit widening conversion to type 'long' of a multiplication performed in type 'int' [bugprone-implicit-widening-of-multiplication-result]

return {static_cast<int>(cardWidth), static_cast<int>(cardHeight)};
}
Expand Down
1 change: 1 addition & 0 deletions src/Mod/Start/Gui/FileCardDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class FileCardDelegate: public QAbstractItemDelegate

private:
Base::Reference<ParameterGrp> _parameterGroup;
std::unique_ptr<QWidget> _widget;
};


Expand Down