Krita Source Code Documentation
Loading...
Searching...
No Matches
KisResourceItemDelegate.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 * SPDX-FileCopyrightText: 2008 Jan Hambrecht <jaham@gmx.net>
3 * SPDX-FileCopyrightText: 2018 Boudewijn Rempt <boud@valdyas.org>
4 *
5 * SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7
8
9#include <QPainter>
10#include <QDebug>
11
13#include "KisResourceModel.h"
16#include <KoIcon.h>
17
19 : QAbstractItemDelegate(parent)
20 , m_checkerPainter(4)
21 , m_showText(false)
22 , m_isWidget(false)
23{
24}
25
27{
28 m_showText = showText;
29}
30
32{
33 m_isWidget = isWidget;
34}
35
36QModelIndex KisResourceItemDelegate::convertToGlobalModelIndexIfNeeded(const QModelIndex &localIndex) const
37{
38 if (!m_isWidget) return localIndex;
39
40 const int resourceId = localIndex.data(Qt::UserRole + KisAllResourcesModel::Id).toInt();
41 const QString resourceType = localIndex.data(Qt::UserRole + KisAllResourcesModel::ResourceType).toString();
42
43 auto *model = KisResourceModelProvider::resourceModel(resourceType);
44 QModelIndex globalIndex = model->indexForResourceId(resourceId);
45 return globalIndex;
46}
47
48void KisResourceItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem & option, const QModelIndex &index) const
49{
50 if (!index.isValid()) return;
51 painter->save();
52
53 if (m_showText) {
54 QRect paintRect = QRect(option.rect.x(), option.rect.y(), option.rect.height(), option.rect.height());
55 QString resourceDisplayName = index.data(Qt::UserRole + KisAbstractResourceModel::Name).toString().replace("_", " "); // don't need underscores that might be part of the file name
56 painter->drawText(paintRect.width() + 10, option.rect.y() + option.rect.height() - 10, resourceDisplayName);
57
58 const QModelIndex globalIndex = convertToGlobalModelIndexIfNeeded(index);
59 m_thumbnailPainter.paint(painter, globalIndex, paintRect, option.palette, false, true);
60
61 if (option.state & QStyle::State_Selected) {
62 painter->setCompositionMode(QPainter::CompositionMode_HardLight);
63 painter->setOpacity(1.0);
64 painter->fillRect(option.rect, option.palette.highlight());
65
66 // highlight is not strong enough to pick out preset. draw border around it.
67 painter->setCompositionMode(QPainter::CompositionMode_SourceOver);
68 painter->setPen(QPen(option.palette.highlight(), 4, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin));
69 QRect selectedBorder = option.rect.adjusted(2 , 2, -2, -2); // constrict the rectangle so it doesn't bleed into other presets
70 painter->drawRect(selectedBorder);
71 }
72 } else {
73 const QModelIndex globalIndex = convertToGlobalModelIndexIfNeeded(index);
74 m_thumbnailPainter.paint(painter, globalIndex, option.rect, option.palette, option.state & QStyle::State_Selected, true);
75 }
76 painter->restore();
77}
78
79
80QSize KisResourceItemDelegate::sizeHint(const QStyleOptionViewItem &optionItem, const QModelIndex &index) const
81{
82 Q_UNUSED(index);
83 return optionItem.decorationSize;
84}
QModelIndex convertToGlobalModelIndexIfNeeded(const QModelIndex &localIndex) const
QSize sizeHint(const QStyleOptionViewItem &, const QModelIndex &) const override
KisResourceThumbnailPainter m_thumbnailPainter
void paint(QPainter *, const QStyleOptionViewItem &, const QModelIndex &) const override
KisResourceItemDelegate(QObject *parent=0)
static KisAllResourcesModel * resourceModel(const QString &resourceType)
void paint(QPainter *painter, const QModelIndex &index, QRect rect, const QPalette &palette, bool selected, bool addMargin) const