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"
15#include <KoIcon.h>
16
18 : QAbstractItemDelegate(parent)
19 , m_checkerPainter(4)
20 , m_showText(false)
21 , m_isWidget(false)
22{
23}
24
26{
27 m_showText = showText;
28}
29
31{
32 m_isWidget = isWidget;
33}
34
35void KisResourceItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem & option, const QModelIndex &index) const
36{
37 if (!index.isValid()) return;
38 painter->save();
39
40 if (m_showText) {
41 QRect paintRect = QRect(option.rect.x(), option.rect.y(), option.rect.height(), option.rect.height());
42 QString resourceDisplayName = index.data(Qt::UserRole + KisAbstractResourceModel::Name).toString().replace("_", " "); // don't need underscores that might be part of the file name
43 painter->drawText(paintRect.width() + 10, option.rect.y() + option.rect.height() - 10, resourceDisplayName);
44
45 if (m_isWidget) { // Bundle Creator selected list...
46 QIcon icon = qvariant_cast<QIcon>(index.data(Qt::DecorationRole));
47
48 QImage preview;
49 if (!icon.isNull()) {
50 QSize iconSize = option.decorationSize; // or specify a desired size
51 QPixmap pixmap = icon.pixmap(iconSize);
52 preview = pixmap.toImage();
53 }
54
55 painter->drawImage(paintRect.x(), paintRect.y(), preview);
56 } else {
57 m_thumbnailPainter.paint(painter, index, paintRect, option.palette, false, true);
58 }
59
60 if (option.state & QStyle::State_Selected) {
61 painter->setCompositionMode(QPainter::CompositionMode_HardLight);
62 painter->setOpacity(1.0);
63 painter->fillRect(option.rect, option.palette.highlight());
64
65 // highlight is not strong enough to pick out preset. draw border around it.
66 painter->setCompositionMode(QPainter::CompositionMode_SourceOver);
67 painter->setPen(QPen(option.palette.highlight(), 4, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin));
68 QRect selectedBorder = option.rect.adjusted(2 , 2, -2, -2); // constrict the rectangle so it doesn't bleed into other presets
69 painter->drawRect(selectedBorder);
70 }
71 } else {
72 m_thumbnailPainter.paint(painter, index, option.rect, option.palette, option.state & QStyle::State_Selected, true);
73 }
74 painter->restore();
75}
76
77
78QSize KisResourceItemDelegate::sizeHint(const QStyleOptionViewItem &optionItem, const QModelIndex &index) const
79{
80 return optionItem.decorationSize;
81}
int iconSize(qreal width, qreal height)
QSize sizeHint(const QStyleOptionViewItem &, const QModelIndex &) const override
KisResourceThumbnailPainter m_thumbnailPainter
void paint(QPainter *, const QStyleOptionViewItem &, const QModelIndex &) const override
KisResourceItemDelegate(QObject *parent=0)
void paint(QPainter *painter, const QModelIndex &index, QRect rect, const QPalette &palette, bool selected, bool addMargin) const