24
#include "PreCompiled.h"
28
#include <QFileIconProvider>
29
#include <QImageReader>
31
#include <QStyleOptionViewItem>
35
#include <QApplication>
38
#include "FileCardDelegate.h"
39
#include "../App/DisplayedFilesModel.h"
40
#include "App/Application.h"
42
#include <gsl/pointers>
46
FileCardDelegate::FileCardDelegate(QObject* parent)
47
: QAbstractItemDelegate(parent)
49
_parameterGroup = App::GetApplication().GetParameterGroupByPath(
50
"User parameter:BaseApp/Preferences/Mod/Start");
51
_widget = std::make_unique<QWidget>();
52
_widget->setObjectName(QLatin1String("thumbnailWidget"));
53
auto layout = gsl::owner<QVBoxLayout*>(new QVBoxLayout());
54
layout->setSpacing(0);
55
_widget->setLayout(layout);
58
QColor FileCardDelegate::getBorderColor() const
60
QColor color(98, 160, 234);
61
uint32_t packed = App::Color::asPackedRGB<QColor>(color);
62
packed = _parameterGroup->GetUnsigned("FileThumbnailBorderColor", packed);
63
color = App::Color::fromPackedRGB<QColor>(packed);
67
QColor FileCardDelegate::getBackgroundColor() const
69
QColor color(221, 221, 221);
70
uint32_t packed = App::Color::asPackedRGB<QColor>(color);
71
packed = _parameterGroup->GetUnsigned("FileThumbnailBackgroundColor", packed);
72
color = App::Color::fromPackedRGB<QColor>(packed);
76
QColor FileCardDelegate::getSelectionColor() const
78
QColor color(38, 162, 105);
79
uint32_t packed = App::Color::asPackedRGB<QColor>(color);
80
packed = _parameterGroup->GetUnsigned("FileThumbnailSelectionColor", packed);
81
color = App::Color::fromPackedRGB<QColor>(packed);
85
void FileCardDelegate::paint(QPainter* painter,
86
const QStyleOptionViewItem& option,
87
const QModelIndex& index) const
90
static_cast<int>(_parameterGroup->GetInt("FileThumbnailIconsSize", 128));
91
auto cardWidth = thumbnailSize;
92
auto baseName = index.data(static_cast<int>(DisplayedFilesModelRoles::baseName)).toString();
93
auto size = index.data(static_cast<int>(DisplayedFilesModelRoles::size)).toString();
94
auto image = index.data(static_cast<int>(DisplayedFilesModelRoles::image)).toByteArray();
95
auto path = index.data(static_cast<int>(DisplayedFilesModelRoles::path)).toString();
97
auto thumbnail = std::make_unique<QLabel>();
98
auto pixmap = std::make_unique<QPixmap>();
99
auto layout = qobject_cast<QVBoxLayout*>(_widget->layout());
100
if (!image.isEmpty()) {
101
pixmap->loadFromData(image);
102
if (!pixmap->isNull()) {
103
auto scaled = pixmap->scaled(QSize(thumbnailSize, thumbnailSize),
104
Qt::AspectRatioMode::KeepAspectRatio,
105
Qt::TransformationMode::SmoothTransformation);
106
thumbnail->setPixmap(scaled);
110
thumbnail->setPixmap(generateThumbnail(path));
112
thumbnail->setFixedSize(thumbnailSize, thumbnailSize);
113
thumbnail->setSizePolicy(QSizePolicy::Policy::Fixed, QSizePolicy::Policy::Fixed);
115
_widget->setProperty("state", QStringLiteral(""));
116
if (option.state & QStyle::State_Selected) {
117
_widget->setProperty("state", QStringLiteral("pressed"));
118
if (qApp->styleSheet().isEmpty()) {
119
QColor color = getSelectionColor();
120
_widget->setStyleSheet(QString::fromLatin1("QWidget#thumbnailWidget {"
121
" border: 2px solid rgb(%1, %2, %3);"
122
" border-radius: 4px;"
130
else if (option.state & QStyle::State_MouseOver) {
131
_widget->setProperty("state", QStringLiteral("hovered"));
132
if (qApp->styleSheet().isEmpty()) {
133
QColor color = getBorderColor();
134
_widget->setStyleSheet(QString::fromLatin1("QWidget#thumbnailWidget {"
135
" border: 2px solid rgb(%1, %2, %3);"
136
" border-radius: 4px;"
144
else if (qApp->styleSheet().isEmpty()) {
145
QColor color = getBackgroundColor();
146
_widget->setStyleSheet(QString::fromLatin1("QWidget#thumbnailWidget {"
147
" background-color: rgb(%1, %2, %3);"
148
" border-radius: 8px;"
156
painter->fontMetrics().elidedText(baseName, Qt::TextElideMode::ElideRight, cardWidth);
157
auto name = std::make_unique<QLabel>(elided);
158
layout->addWidget(thumbnail.get());
159
layout->addWidget(name.get());
160
auto sizeLabel = std::make_unique<QLabel>(size);
161
layout->addWidget(sizeLabel.get());
162
layout->addStretch();
163
_widget->resize(option.rect.size());
164
painter->translate(option.rect.topLeft());
165
_widget->render(painter, QPoint(), QRegion(), QWidget::DrawChildren);
167
layout->removeWidget(sizeLabel.get());
168
layout->removeWidget(thumbnail.get());
169
layout->removeWidget(name.get());
173
QSize FileCardDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
177
auto thumbnailSize = _parameterGroup->GetInt("FileThumbnailIconsSize", 128);
178
auto cardMargin = _widget->layout()->contentsMargins();
179
auto cardWidth = thumbnailSize + cardMargin.left() + cardMargin.right();
180
auto spacing = _widget->layout()->spacing();
182
auto font = QGuiApplication::font();
183
auto qfm = QFontMetrics(font);
184
auto textHeight = 2 * qfm.lineSpacing();
186
thumbnailSize + textHeight + 2 * spacing + cardMargin.top() + cardMargin.bottom();
188
return {static_cast<int>(cardWidth), static_cast<int>(cardHeight)};
193
QPixmap pixmapToSizedQImage(const QImage& pixmap, int size)
195
return QPixmap::fromImage(pixmap).scaled(size,
197
Qt::AspectRatioMode::KeepAspectRatio,
198
Qt::TransformationMode::SmoothTransformation);
202
QPixmap FileCardDelegate::generateThumbnail(const QString& path) const
205
static_cast<int>(_parameterGroup->GetInt("FileThumbnailIconsSize", 128));
206
if (path.endsWith(QLatin1String(".fcstd"), Qt::CaseSensitivity::CaseInsensitive)) {
207
QImageReader reader(QLatin1String(":/icons/freecad-doc.svg"));
208
reader.setScaledSize({thumbnailSize, thumbnailSize});
209
return QPixmap::fromImage(reader.read());
211
if (path.endsWith(QLatin1String(".fcmacro"), Qt::CaseSensitivity::CaseInsensitive)) {
212
QImageReader reader(QLatin1String(":/icons/MacroEditor.svg"));
213
reader.setScaledSize({thumbnailSize, thumbnailSize});
214
return QPixmap::fromImage(reader.read());
216
if (!QImageReader::imageFormat(path).isEmpty()) {
218
QImageReader reader(path);
219
auto image = reader.read();
220
if (!image.isNull()) {
221
return pixmapToSizedQImage(image, thumbnailSize);
224
QIcon icon = QFileIconProvider().icon(QFileInfo(path));
225
if (!icon.isNull()) {
226
QPixmap pixmap = icon.pixmap(thumbnailSize);
227
if (!pixmap.isNull()) {
231
QPixmap pixmap = QPixmap(thumbnailSize, thumbnailSize);