diff --git a/src/Gui/Stylesheets/FreeCAD Dark.qss b/src/Gui/Stylesheets/FreeCAD Dark.qss index 0326a321b7..0b10fa8325 100644 --- a/src/Gui/Stylesheets/FreeCAD Dark.qss +++ b/src/Gui/Stylesheets/FreeCAD Dark.qss @@ -2860,17 +2860,17 @@ QTreeView::branch#groupsTreeView:has-siblings:!adjoins-item { /*================================================================================================== Start page ==================================================================================================*/ -QWidget#thumbnailWidget { - background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #333333, stop:1 #252525); +#thumbnailWidget { + background-color: #2f2f2f; border-radius: 8px; border: 1px solid #020202; } -QWidget#thumbnailWidget[state="hovered"] { +#thumbnailWidget:hover { border: 1px solid @ThemeAccentColor1; } -QWidget#thumbnailWidget[state="pressed"] { +#thumbnailWidget:pressed { border: 1px solid @ThemeAccentColor1; } diff --git a/src/Gui/Stylesheets/FreeCAD Light.qss b/src/Gui/Stylesheets/FreeCAD Light.qss index 9cafa1cb2a..77bc028e28 100644 --- a/src/Gui/Stylesheets/FreeCAD Light.qss +++ b/src/Gui/Stylesheets/FreeCAD Light.qss @@ -2865,17 +2865,17 @@ QTreeView::branch#groupsTreeView:has-siblings:!adjoins-item { /*================================================================================================== Start page ==================================================================================================*/ -QWidget#thumbnailWidget { - background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #f0f0f0, stop:1 #fdfdfd); +#thumbnailWidget { + background-color: #ededed; border-radius: 8px; border: 1px solid #ababab; } -QWidget#thumbnailWidget[state="hovered"] { +#thumbnailWidget:hover { border: 1px solid @ThemeAccentColor1; } -QWidget#thumbnailWidget[state="pressed"] { +#thumbnailWidget:pressed { border: 1px solid @ThemeAccentColor1; } diff --git a/src/Mod/Start/Gui/FileCardDelegate.cpp b/src/Mod/Start/Gui/FileCardDelegate.cpp index 9fef0b800d..a7cd323842 100644 --- a/src/Mod/Start/Gui/FileCardDelegate.cpp +++ b/src/Mod/Start/Gui/FileCardDelegate.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #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(); + _widget = std::make_unique(); _widget->setObjectName(QLatin1String("thumbnailWidget")); auto layout = gsl::owner(new QVBoxLayout()); layout->setSpacing(0); _widget->setLayout(layout); } -QColor FileCardDelegate::getBorderColor() const -{ - QColor color(98, 160, 234); // NOLINT - uint32_t packed = Base::Color::asPackedRGB(color); - packed = _parameterGroup->GetUnsigned("FileThumbnailBorderColor", packed); - color = Base::Color::fromPackedRGB(packed); - return color; -} - -QColor FileCardDelegate::getBackgroundColor() const -{ - QColor color(221, 221, 221); // NOLINT - uint32_t packed = Base::Color::asPackedRGB(color); - packed = _parameterGroup->GetUnsigned("FileThumbnailBackgroundColor", packed); - color = Base::Color::fromPackedRGB(packed); - return color; -} - -QColor FileCardDelegate::getSelectionColor() const -{ - QColor color(38, 162, 105); // NOLINT - uint32_t packed = Base::Color::asPackedRGB(color); - packed = _parameterGroup->GetUnsigned("FileThumbnailSelectionColor", packed); - color = Base::Color::fromPackedRGB(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(_parameterGroup->GetInt("FileThumbnailIconsSize", 128)); // NOLINT - auto cardWidth = thumbnailSize; auto baseName = index.data(static_cast(DisplayedFilesModelRoles::baseName)).toString(); + auto elidedName = painter->fontMetrics().elidedText(baseName, Qt::ElideRight, thumbnailSize); auto size = index.data(static_cast(DisplayedFilesModelRoles::size)).toString(); auto image = index.data(static_cast(DisplayedFilesModelRoles::image)).toByteArray(); auto path = index.data(static_cast(DisplayedFilesModelRoles::path)).toString(); - painter->save(); - auto thumbnail = std::make_unique(); - auto pixmap = std::make_unique(); - auto layout = qobject_cast(_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(elided); - layout->addWidget(thumbnail.get()); // Temp. ownership transfer - layout->addWidget(name.get()); // Temp. ownership transfer - auto sizeLabel = std::make_unique(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(thumbnailSize) + 2 * margin; + int cardHeight = static_cast(thumbnailSize) + textHeight + 3 * margin; - return {static_cast(cardWidth), static_cast(cardHeight)}; + return {cardWidth, cardHeight}; } namespace diff --git a/src/Mod/Start/Gui/FileCardDelegate.h b/src/Mod/Start/Gui/FileCardDelegate.h index 35e74c79ba..78ba3f80b4 100644 --- a/src/Mod/Start/Gui/FileCardDelegate.h +++ b/src/Mod/Start/Gui/FileCardDelegate.h @@ -27,9 +27,9 @@ #include #include -#include +#include -class FileCardDelegate: public QAbstractItemDelegate +class FileCardDelegate: public QStyledItemDelegate { public: @@ -44,14 +44,11 @@ public: protected: QPixmap generateThumbnail(const QString& path) const; -private: - QColor getBorderColor() const; - QColor getBackgroundColor() const; - QColor getSelectionColor() const; - private: Base::Reference _parameterGroup; std::unique_ptr _widget; + const int margin = 11; + const int textspacing = 2; };