Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_preset_chooser.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2002 Patrick Julien <freak@codepimps.org>
3 * SPDX-FileCopyrightText: 2009 Sven Langkamp <sven.langkamp@gmail.com>
4 * SPDX-FileCopyrightText: 2011 Silvio Heinrich <plassy@web.de>
5 * SPDX-FileCopyrightText: 2011 Srikanth Tiyyagura <srikanth.tulasiram@gmail.com>
6 * SPDX-FileCopyrightText: 2011 José Luis Vergara <pentalis@gmail.com>
7 *
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
10
11#include "kis_preset_chooser.h"
12
13#include <QEvent>
14#include <QVBoxLayout>
15#include <QPainter>
16#include <QAbstractItemDelegate>
17#include <QAbstractItemView>
18#include <QStyleOptionViewItem>
19#include <QPalette>
20#include <QTimer>
21#include <KisResourceModel.h>
22
23#include <kis_config.h>
24#include <klocalizedstring.h>
25#include <KisKineticScroller.h>
26
27#include <KoIcon.h>
31#include <KisResourceLocator.h>
32#include <KisResourceTypes.h>
33
37#include "kis_config_notifier.h"
38#include <kis_icon.h>
43
45class KisPresetDelegate : public QAbstractItemDelegate
46{
47public:
48 // Stroke preview ratio range: 1:3 to 1:4.
49 static constexpr int SLIDER_MIN = 30;
50 static constexpr int SLIDER_MAX = 80;
51 static constexpr double ASPECT_RATIO_MIN = 3.0;
52 static constexpr double ASPECT_RATIO_MAX = 4.0;
53
54 KisPresetDelegate(QObject * parent = nullptr)
55 : QAbstractItemDelegate(parent)
56 , m_viewMode(KisPresetChooser::THUMBNAIL)
57 , m_useDirtyPresets(false)
58 , m_iconSize(50) {}
59
60 ~KisPresetDelegate() override {}
61
63 void paint(QPainter *, const QStyleOptionViewItem &, const QModelIndex &) const override;
64
65 QSize sizeHint(const QStyleOptionViewItem & option, const QModelIndex &) const override {
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 }
74
78
82
83 void setIconSize(int size) {
84 m_iconSize = qBound(SLIDER_MIN, size, SLIDER_MAX);
85 }
86
88 static double calculateAspectRatio(int iconSize) {
89 int clampedSize = qBound(SLIDER_MIN, iconSize, SLIDER_MAX);
90 double t = static_cast<double>(clampedSize - SLIDER_MIN) / (SLIDER_MAX - SLIDER_MIN);
92 }
93
94 static QSize calculateStrokeThumbnailSize(int iconSize, bool withIcon) {
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 }
99
100private:
101 void paintThumbnail(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
102 void paintStrokePreview(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index, bool paint_thumbnail) const;
106};
107
108void KisPresetDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
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}
129
130void KisPresetDelegate::paintThumbnail(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
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}
177
178void KisPresetDelegate::paintStrokePreview(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index, bool paintThumbnail) const
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}
293
295 : QWidget(parent)
296{
297 setObjectName("KisPresetChooser");
298
299 QVBoxLayout * layout = new QVBoxLayout(this);
300 layout->setContentsMargins(0, 0, 0, 0);
301
304 m_delegate = new KisPresetDelegate(this);
306 m_chooser->setSynced(true);
308 layout->addWidget(m_chooser);
309
310 connect(KisResourceItemChooserSync::instance(), SIGNAL(baseLengthChanged(int)),
311 this, SLOT(slotBaseLengthChanged(int)));
312 connect(m_chooser, SIGNAL(resourceSelected(KoResourceSP )),
313 this, SIGNAL(resourceSelected(KoResourceSP )));
314 connect(m_chooser, SIGNAL(resourceClicked(KoResourceSP )),
315 this, SIGNAL(resourceClicked(KoResourceSP )));
316
317 connect(m_chooser, SIGNAL(listViewModeChanged(ListViewMode)),
318 this, SLOT(showHideBrushNames(ListViewMode)));
319
320 connect(KisBrushStrokePreviewCache::instance(), SIGNAL(sigPreviewReady(int)),
321 this, SLOT(slotStrokePreviewReady(int)));
322
324
325 connect(KisConfigNotifier::instance(), SIGNAL(configChanged()),
326 SLOT(notifyConfigChanged()));
327
329}
330
334
340
345
350
355
357{
358 KisConfig cfg(true);
360 int newIconSize = cfg.presetIconSize();
361 m_delegate->setIconSize(newIconSize);
362 setIconSize(newIconSize);
363}
364
366{
367 switch (m_mode) {
368 case ViewMode::THUMBNAIL: {
371 break;
372 }
373
377
378 int currentIconSize = iconSize();
379 m_delegate->setIconSize(currentIconSize);
380 QSize thumbnailSize = KisPresetDelegate::calculateStrokeThumbnailSize(currentIconSize, true);
381
382 m_chooser->itemView()->setGridSize(thumbnailSize);
383 m_chooser->itemView()->setIconSize(thumbnailSize);
384 m_chooser->itemView()->doItemsLayout();
385 break;
386 }
387 case ViewMode::STROKE: {
390
391 int currentIconSize = iconSize();
392 m_delegate->setIconSize(currentIconSize);
393 QSize thumbnailSize = KisPresetDelegate::calculateStrokeThumbnailSize(currentIconSize, false);
394
395 m_chooser->itemView()->setGridSize(thumbnailSize);
396 m_chooser->itemView()->setIconSize(thumbnailSize);
397 m_chooser->itemView()->doItemsLayout();
398 break;
399 }
400 }
401}
402
407
412
414{
416}
417
422
423void KisPresetChooser::setPresetFilter(const QString& paintOpId)
424{
425 QMap<QString, QVariant> metaDataFilter;
426 if (!paintOpId.isEmpty()) { // empty means "all"
427 metaDataFilter["paintopid"] = paintOpId;
428 }
429 m_chooser->tagFilterModel()->setMetaDataFilter(metaDataFilter);
431}
432
434{
436 chooserSync->setBaseLength(newSize);
437
439 m_delegate->setIconSize(newSize);
441
442 m_chooser->itemView()->setGridSize(thumbnailSize);
443 m_chooser->itemView()->setIconSize(thumbnailSize);
444 m_chooser->itemView()->doItemsLayout();
445 }
446}
447
449{
451 return chooserSync->baseLength();
452}
453
455{
456 if (KisConfig(true).presetIconSize() != iconSize()) {
458 }
459}
460
462{
463 QWidget::changeEvent(event);
464 if (event->type() == QEvent::PaletteChange) {
466 m_chooser->itemView()->viewport()->update();
467 }
468 }
469}
470
472{
473 Q_UNUSED(newViewMode);
474
476 // Restore non-square grid size.
477 int currentIconSize = iconSize();
479 m_chooser->itemView()->setGridSize(thumbnailSize);
480 m_chooser->itemView()->setIconSize(thumbnailSize);
481 m_chooser->itemView()->doItemsLayout();
482 return;
483 }
484}
485
487{
488 Q_UNUSED(presetId);
489
491 return;
492 }
493
494 m_chooser->itemView()->viewport()->update();
495}
496
498{
500 return;
501 }
502
505
506 m_chooser->itemView()->setGridSize(thumbnailSize);
507 m_chooser->itemView()->setIconSize(thumbnailSize);
508 m_chooser->itemView()->doItemsLayout();
509}
qreal length(const QPointF &vec)
Definition Ellipse.cc:82
float value(const T *src, size_t ch)
int iconSize(qreal width, qreal height)
@ 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.
static KisConfigNotifier * instance()
bool useDirtyPresets(bool defaultValue=false) const
void setPresetIconSize(const int value) const
int presetIconSize(bool defaultValue=false) const
@ STROKE_THUMBNAIL
Shows thumbnails.
@ STROKE
Shows stroke previews with text and thumbnail.
int iconSize()
get the base size for the icons. Used by the slider in the view options
void resourceSelected(KoResourceSP resource)
void setPresetFilter(const QString &paintOpId)
void setCurrentResource(KoResourceSP resource)
KisResourceItemChooser * m_chooser
void saveIconSize()
saves the icon size for the presets. called by the horizontal slider release event.
void setIconSize(int newSize)
sets the icon size. Used by slider in view menu
KisPresetDelegate * m_delegate
void showHideBrushNames(ListViewMode newViewMode)
void slotBaseLengthChanged(int length)
void setViewMode(ViewMode mode)
Sets a list of resources in the paintop list, when ever user press enter in the lineedit of paintop_p...
KisPresetChooser(QWidget *parent=0)
void showTaggingBar(bool show)
Sets the visibility of tagging klineEdits.
void resourceClicked(KoResourceSP resource)
KisResourceItemChooser * itemChooser()
KoResourceSP currentResource() const
void changeEvent(QEvent *event) override
void slotStrokePreviewReady(int presetId)
Resource item delegate for rendering brush stroke previews.
void setUseDirtyPresets(bool value)
void paint(QPainter *, const QStyleOptionViewItem &, const QModelIndex &) const override
reimplemented
static QSize calculateStrokeThumbnailSize(int iconSize, bool withIcon)
static constexpr int SLIDER_MAX
static constexpr double ASPECT_RATIO_MIN
void setIconSize(int size)
static constexpr double ASPECT_RATIO_MAX
KisPresetDelegate(QObject *parent=nullptr)
KisPresetChooser::ViewMode m_viewMode
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &) const override
void paintThumbnail(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
static constexpr int SLIDER_MIN
static double calculateAspectRatio(int iconSize)
Interpolate aspect ratio from slider.
void paintStrokePreview(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index, bool paint_thumbnail) const
void setViewMode(KisPresetChooser::ViewMode mode)
void setCurrentResource(KoResourceSP resource)
Sets the item representing the resource as selected.
void setListViewMode(ListViewMode viewMode)
KisResourceItemListView * itemView() const
void setRowHeight(int rowHeight)
Sets the height of the view rows.
KisTagFilterResourceProxyModel * tagFilterModel() const
void setItemDelegate(QAbstractItemDelegate *delegate)
Sets a custom delegate for the view.
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)
void setMetaDataFilter(QMap< QString, QVariant > metaDataMap)
setMetaDataFilter provides a set of metadata to filter on, for instance by paintop id category.
QIcon loadIcon(const QString &name)
const QString PaintOpPresets
static KisResourceItemChooserSync * instance()