Krita Source Code Documentation
Loading...
Searching...
No Matches
KisPresetDelegate Class Reference

Resource item delegate for rendering brush stroke previews. More...

+ Inheritance diagram for KisPresetDelegate:

Public Member Functions

 KisPresetDelegate (QObject *parent=nullptr)
 
void paint (QPainter *, const QStyleOptionViewItem &, const QModelIndex &) const override
 reimplemented
 
void setIconSize (int size)
 
void setUseDirtyPresets (bool value)
 
void setViewMode (KisPresetChooser::ViewMode mode)
 
QSize sizeHint (const QStyleOptionViewItem &option, const QModelIndex &) const override
 
 ~KisPresetDelegate () override
 

Static Public Member Functions

static double calculateAspectRatio (int iconSize)
 Interpolate aspect ratio from slider.
 
static QSize calculateStrokeThumbnailSize (int iconSize, bool withIcon)
 

Static Public Attributes

static constexpr double ASPECT_RATIO_MAX = 4.0
 
static constexpr double ASPECT_RATIO_MIN = 3.0
 
static constexpr int SLIDER_MAX = 80
 
static constexpr int SLIDER_MIN = 30
 

Private Member Functions

void paintStrokePreview (QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index, bool paint_thumbnail) const
 
void paintThumbnail (QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
 

Private Attributes

int m_iconSize
 
bool m_useDirtyPresets
 
KisPresetChooser::ViewMode m_viewMode
 

Detailed Description

Resource item delegate for rendering brush stroke previews.

Definition at line 45 of file kis_preset_chooser.cpp.

Constructor & Destructor Documentation

◆ KisPresetDelegate()

KisPresetDelegate::KisPresetDelegate ( QObject * parent = nullptr)
inline

Definition at line 54 of file kis_preset_chooser.cpp.

55 : QAbstractItemDelegate(parent)
57 , m_useDirtyPresets(false)
58 , m_iconSize(50) {}
KisPresetChooser::ViewMode m_viewMode

◆ ~KisPresetDelegate()

KisPresetDelegate::~KisPresetDelegate ( )
inlineoverride

Definition at line 60 of file kis_preset_chooser.cpp.

60{}

Member Function Documentation

◆ calculateAspectRatio()

static double KisPresetDelegate::calculateAspectRatio ( int iconSize)
inlinestatic

Interpolate aspect ratio from slider.

Definition at line 88 of file kis_preset_chooser.cpp.

88 {
89 int clampedSize = qBound(SLIDER_MIN, iconSize, SLIDER_MAX);
90 double t = static_cast<double>(clampedSize - SLIDER_MIN) / (SLIDER_MAX - SLIDER_MIN);
92 }
int iconSize(qreal width, qreal height)
static constexpr int SLIDER_MAX
static constexpr double ASPECT_RATIO_MIN
static constexpr double ASPECT_RATIO_MAX
static constexpr int SLIDER_MIN

References ASPECT_RATIO_MAX, ASPECT_RATIO_MIN, iconSize(), SLIDER_MAX, and SLIDER_MIN.

◆ calculateStrokeThumbnailSize()

static QSize KisPresetDelegate::calculateStrokeThumbnailSize ( int iconSize,
bool withIcon )
inlinestatic

Definition at line 94 of file kis_preset_chooser.cpp.

94 {
95 int clampedSize = qBound(SLIDER_MIN, iconSize, SLIDER_MAX);
96 double aspectRatio = calculateAspectRatio(clampedSize);
97 return QSize(static_cast<int>(clampedSize * aspectRatio) + (withIcon ? clampedSize : 0), clampedSize);
98 }
static double calculateAspectRatio(int iconSize)
Interpolate aspect ratio from slider.

References calculateAspectRatio(), iconSize(), SLIDER_MAX, and SLIDER_MIN.

◆ paint()

void KisPresetDelegate::paint ( QPainter * painter,
const QStyleOptionViewItem & option,
const QModelIndex & index ) const
override

reimplemented

Definition at line 108 of file kis_preset_chooser.cpp.

109{
110 painter->save();
111 painter->setRenderHint(QPainter::SmoothPixmapTransform, true);
112 if (!(option.state & QStyle::State_Enabled)) {
113 painter->setOpacity(0.2);
114 }
115
116 if (!index.isValid()) {
117 painter->restore();
118 return;
119 }
120
123 } else {
124 paintThumbnail(painter, option, index);
125 }
126
127 painter->restore();
128}
@ STROKE_THUMBNAIL
Shows thumbnails.
@ STROKE
Shows stroke previews with text and thumbnail.
void paintThumbnail(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
void paintStrokePreview(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index, bool paint_thumbnail) const

References m_viewMode, paintStrokePreview(), paintThumbnail(), KisPresetChooser::STROKE, and KisPresetChooser::STROKE_THUMBNAIL.

◆ paintStrokePreview()

void KisPresetDelegate::paintStrokePreview ( QPainter * painter,
const QStyleOptionViewItem & option,
const QModelIndex & index,
bool paint_thumbnail ) const
private

Definition at line 178 of file kis_preset_chooser.cpp.

179{
180 bool dirty = index.data(Qt::UserRole + KisAbstractResourceModel::Dirty).toBool();
181 int presetId = index.data(Qt::UserRole + KisAbstractResourceModel::Id).toInt();
182
183 qreal devicePixelRatioF = painter->device()->devicePixelRatioF();
184 const bool isSelected = option.state & QStyle::State_Selected;
185
186 const QColor backgroundColor = option.palette.color(QPalette::Window);
187 const QColor borderColor = option.palette.color(QPalette::Mid);
188 QColor textColor = option.palette.color(QPalette::Text);
189 if (isSelected) {
190 textColor = textColor.lightnessF() < 0.5 ? QColor(Qt::black) : QColor(Qt::white);
191 }
192
193 painter->fillRect(option.rect, borderColor);
194 QRect innerRect = option.rect.adjusted(2, 2, -2, -2);
195 painter->fillRect(innerRect, backgroundColor);
196
197 if (isSelected) {
198 const QColor accentColor = option.palette.highlight().color();
199 const qreal bgLightness = backgroundColor.lightnessF();
200 const qreal accentLightness = accentColor.lightnessF();
201 const bool sameSide =
202 (bgLightness >= 0.5 && accentLightness >= 0.5) ||
203 (bgLightness < 0.5 && accentLightness < 0.5);
204 if (sameSide) {
205 painter->setCompositionMode(QPainter::CompositionMode_HardLight);
206 painter->setOpacity(1.0);
207 painter->fillRect(innerRect, accentColor);
208 painter->setCompositionMode(QPainter::CompositionMode_SourceOver);
209 }
210 }
211
212 if (paintThumbnail) {
213 QImage preview = KisResourceThumbnailCache::instance()->getImage(index);
214
215 QRect imageRect = innerRect.adjusted(2, 2, -2, -2);
216
217 if (preview.isNull()) {
218 preview = QImage(imageRect.height() * devicePixelRatioF,
219 imageRect.height() * devicePixelRatioF,
220 QImage::Format_RGB32);
221 preview.fill(Qt::red);
222 }
223
224
225 QImage previewHighDpi = KisResourceThumbnailCache::instance()->getImage(
226 index,
227 QSize(imageRect.height(), imageRect.height()) * devicePixelRatioF,
228 Qt::IgnoreAspectRatio,
229 Qt::SmoothTransformation);
230
231 previewHighDpi.setDevicePixelRatio(devicePixelRatioF);
232
233 painter->drawImage(imageRect.topLeft(), previewHighDpi);
234 innerRect = innerRect.adjusted(innerRect.height(), 2, -2, 0);
235 }
236
237 // Keep text height fixed; stroke area shrinks.
238 QFontMetrics fm(painter->font());
239 const int bottomPadding = 1;
240 const int topPadding = 1;
241 const int textAreaHeight = fm.height() + 4 + bottomPadding +topPadding;
242 const int textAreaTopTrim = 3;
243
244 QRect strokeRect = innerRect;
245 strokeRect.setHeight(qMax(10, innerRect.height() - (textAreaHeight - textAreaTopTrim)));
246
247 QSize previewSize = strokeRect.size() * devicePixelRatioF;
248 const QString paintOpId =
249 KisResourceModelProvider::resourceMetadataModel()->metaDataValue(presetId, "paintopid").toString();
250 const bool useStripedBackground = KisBrushStrokePreviewCache::needsStripedBackground(paintOpId);
251
252 QImage strokePreview = KisBrushStrokePreviewCache::instance()->getPreview(presetId, previewSize, painter->device()->devicePixelRatio());
253 if (strokePreview.isNull()) {
254 strokePreview = QImage(previewSize, QImage::Format_ARGB32_Premultiplied);
255 strokePreview.fill(Qt::transparent);
256 }
257
258 strokePreview.setDevicePixelRatio(devicePixelRatioF);
259 painter->drawImage(strokeRect.topLeft(), strokePreview);
260
261 QString presetDisplayName = index.data(Qt::UserRole + KisAbstractResourceModel::Name)
262 .toString()
263 .replace(QLatin1Char('_'), QLatin1Char(' '));
264 if (dirty) {
265 presetDisplayName.append(QLatin1Char('*'));
266 }
267 QString elidedName = fm.elidedText(presetDisplayName, Qt::ElideRight, innerRect.width() - 14);
268
269 QRect textRect = innerRect;
270 textRect.setTop(innerRect.bottom() - textAreaHeight + topPadding);
271 textRect.setLeft(innerRect.left() + 7);
272 textRect.setBottom(innerRect.bottom() - bottomPadding);
273 painter->setPen(textColor);
274 painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, elidedName);
275
276 if (dirty) {
277 const QIcon icon = KisIconUtils::loadIcon(QStringLiteral("dirty-preset"));
278 QPixmap pixmap = icon.pixmap(QSize(16, 16));
279 painter->drawPixmap(innerRect.x() + 3, innerRect.y() + 3, pixmap);
280 }
281
282 if (index.data(Qt::UserRole + KisAbstractResourceModel::BrokenStatus).toBool()) {
283 const QIcon icon = KisIconUtils::loadIcon(QStringLiteral("broken-preset"));
284 icon.paint(painter, QRect(innerRect.right() - 25, innerRect.y() + 3, 22, 22));
285 }
286
287 if (isSelected) {
288 painter->setPen(QPen(option.palette.highlight(), 4, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin));
289 QRect selectedBorder = option.rect.adjusted(2, 2, -2, -2);
290 painter->drawRect(selectedBorder);
291 }
292}
@ Dirty
A dirty resource is one that has been modified locally but not saved.
@ BrokenStatus
Whether the resource is broken (bool)
static KisBrushStrokePreviewCache * instance()
static bool needsStripedBackground(const QString &paintOpId)
True if paint engine needs striped background (colorsmudge, deform, filter).
QImage getPreview(int presetId, const QSize &size, qreal pixelRatio)
Get cached preview scaled to size, or placeholder if not cached.
QVariant metaDataValue(int resourceId, const QString &key)
static KisResourceMetaDataModel * resourceMetadataModel()
static KisResourceThumbnailCache * instance()
QImage getImage(const QModelIndex &index, const QSize size=QSize(-1, -1), Qt::AspectRatioMode aspectMode=Qt::IgnoreAspectRatio, Qt::TransformationMode transformMode=Qt::FastTransformation)
QIcon loadIcon(const QString &name)

References KisAbstractResourceModel::BrokenStatus, KisAbstractResourceModel::Dirty, KisResourceThumbnailCache::getImage(), KisBrushStrokePreviewCache::getPreview(), KisAbstractResourceModel::Id, KisResourceThumbnailCache::instance(), KisBrushStrokePreviewCache::instance(), KisIconUtils::loadIcon(), KisResourceMetaDataModel::metaDataValue(), KisAbstractResourceModel::Name, KisBrushStrokePreviewCache::needsStripedBackground(), paintThumbnail(), and KisResourceModelProvider::resourceMetadataModel().

◆ paintThumbnail()

void KisPresetDelegate::paintThumbnail ( QPainter * painter,
const QStyleOptionViewItem & option,
const QModelIndex & index ) const
private

Definition at line 130 of file kis_preset_chooser.cpp.

131{
132
133 bool dirty = index.data(Qt::UserRole + KisAbstractResourceModel::Dirty).toBool();
134
135 QImage preview = KisResourceThumbnailCache::instance()->getImage(index);
136
137 if (preview.isNull()) {
138 preview = QImage(512, 512, QImage::Format_RGB32);
139 preview.fill(Qt::red);
140 }
141
142 qreal devicePixelRatioF = painter->device()->devicePixelRatioF();
143
144 QRect paintRect = option.rect.adjusted(1, 1, -1, -1);
145
146 QImage previewHighDpi =
148 paintRect.size() * devicePixelRatioF,
149 Qt::IgnoreAspectRatio,
150 Qt::SmoothTransformation);
151 previewHighDpi.setDevicePixelRatio(devicePixelRatioF);
152 painter->drawImage(paintRect.x(), paintRect.y(), previewHighDpi);
153
154 if (m_useDirtyPresets && dirty) {
155 const QIcon icon = KisIconUtils::loadIcon("dirty-preset");
156 QPixmap pixmap = icon.pixmap(QSize(16,16));
157 painter->drawPixmap(paintRect.x() + 3, paintRect.y() + 3, pixmap);
158 }
159
160 if (index.data(Qt::UserRole + KisAbstractResourceModel::BrokenStatus).toBool()) {
161 const QIcon icon = KisIconUtils::loadIcon("broken-preset");
162 icon.paint(painter, QRect(paintRect.x() + paintRect.height() - 25, paintRect.y() + paintRect.height() - 25, 25, 25));
163 }
164
165 if (option.state & QStyle::State_Selected) {
166 painter->setCompositionMode(QPainter::CompositionMode_HardLight);
167 painter->setOpacity(1.0);
168 painter->fillRect(option.rect, option.palette.highlight());
169
170 // highlight is not strong enough to pick out preset. draw border around it.
171 painter->setCompositionMode(QPainter::CompositionMode_SourceOver);
172 painter->setPen(QPen(option.palette.highlight(), 4, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin));
173 QRect selectedBorder = option.rect.adjusted(2 , 2, -2, -2); // constrict the rectangle so it doesn't bleed into other presets
174 painter->drawRect(selectedBorder);
175 }
176}

References KisAbstractResourceModel::BrokenStatus, KisAbstractResourceModel::Dirty, KisResourceThumbnailCache::getImage(), KisResourceThumbnailCache::instance(), KisIconUtils::loadIcon(), and m_useDirtyPresets.

◆ setIconSize()

void KisPresetDelegate::setIconSize ( int size)
inline

Definition at line 83 of file kis_preset_chooser.cpp.

83 {
84 m_iconSize = qBound(SLIDER_MIN, size, SLIDER_MAX);
85 }

References m_iconSize, SLIDER_MAX, and SLIDER_MIN.

◆ setUseDirtyPresets()

void KisPresetDelegate::setUseDirtyPresets ( bool value)
inline

Definition at line 79 of file kis_preset_chooser.cpp.

79 {
81 }
float value(const T *src, size_t ch)

References m_useDirtyPresets, and value().

◆ setViewMode()

void KisPresetDelegate::setViewMode ( KisPresetChooser::ViewMode mode)
inline

Definition at line 75 of file kis_preset_chooser.cpp.

75 {
76 m_viewMode = mode;
77 }

References m_viewMode.

◆ sizeHint()

QSize KisPresetDelegate::sizeHint ( const QStyleOptionViewItem & option,
const QModelIndex &  ) const
inlineoverride

Definition at line 65 of file kis_preset_chooser.cpp.

65 {
67 int height = m_iconSize;
68 int width = static_cast<int>(height * calculateAspectRatio(m_iconSize)
70 return QSize(width, height);
71 }
72 return option.decorationSize;
73 }

References calculateAspectRatio(), m_iconSize, m_viewMode, KisPresetChooser::STROKE, and KisPresetChooser::STROKE_THUMBNAIL.

Member Data Documentation

◆ ASPECT_RATIO_MAX

constexpr double KisPresetDelegate::ASPECT_RATIO_MAX = 4.0
staticconstexpr

Definition at line 52 of file kis_preset_chooser.cpp.

◆ ASPECT_RATIO_MIN

constexpr double KisPresetDelegate::ASPECT_RATIO_MIN = 3.0
staticconstexpr

Definition at line 51 of file kis_preset_chooser.cpp.

◆ m_iconSize

int KisPresetDelegate::m_iconSize
private

Definition at line 105 of file kis_preset_chooser.cpp.

◆ m_useDirtyPresets

bool KisPresetDelegate::m_useDirtyPresets
private

Definition at line 104 of file kis_preset_chooser.cpp.

◆ m_viewMode

KisPresetChooser::ViewMode KisPresetDelegate::m_viewMode
private

Definition at line 103 of file kis_preset_chooser.cpp.

◆ SLIDER_MAX

constexpr int KisPresetDelegate::SLIDER_MAX = 80
staticconstexpr

Definition at line 50 of file kis_preset_chooser.cpp.

◆ SLIDER_MIN

constexpr int KisPresetDelegate::SLIDER_MIN = 30
staticconstexpr

Definition at line 49 of file kis_preset_chooser.cpp.


The documentation for this class was generated from the following file: