Krita Source Code Documentation
Loading...
Searching...
No Matches
KoResourcePopupAction.cpp
Go to the documentation of this file.
1/*
2 * Made by Tomislav Lukman (tomislav.lukman@ck.tel.hr)
3 * SPDX-FileCopyrightText: 2012 Jean-Nicolas Artaud <jeannicolasartaud@gmail.com>
4 * SPDX-FileCopyrightText: 2019 Boudewijn Rempt <boud@valdyas.org>
5 *
6 * SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8
10
12#include <KisResourceModel.h>
14#include <KoResource.h>
15
17#include <KoShapeBackground.h>
19#include <resources/KoPattern.h>
21#include <KoPatternBackground.h>
22
23#include <QMenu>
24#include <QHBoxLayout>
25#include <QHeaderView>
26#include <QPainter>
27#include <QGradient>
28#include <QToolButton>
29#include <QRect>
30#include <QWidgetAction>
31
42
43KoResourcePopupAction::KoResourcePopupAction(const QString &resourceType, KoCanvasResourcesInterfaceSP canvasResourcesInterface, QObject *parent)
44 : QAction(parent)
45 , d(new Private())
46{
47 d->canvasResourcesInterface = canvasResourcesInterface;
48
49 d->menu = new QMenu();
50 QWidget *widget = new QWidget();
51 QWidgetAction *wdgAction = new QWidgetAction(this);
52
54 if (resourceType == ResourceType::PaintOpPresets) {
56 }
57
58 d->model = new KisResourceModel(resourceType, this);
59 d->resourceList->setModel(d->model);
60 d->resourceList->setItemDelegate(new KisResourceItemDelegate(widget));
61 d->resourceList->setCurrentIndex(d->model->index(0, 0));
62 if (resourceType==ResourceType::Gradients) {
63 d->resourceList->setViewMode(QListView::ListMode);
64 }
65 indexChanged(d->resourceList->currentIndex());
66 QHBoxLayout *layout = new QHBoxLayout(widget);
67 layout->addWidget(d->resourceList);
68
69 wdgAction->setDefaultWidget(widget);
70 d->menu->addAction(wdgAction);
71 setMenu(d->menu);
72 new QHBoxLayout(d->menu);
73 d->menu->layout()->addWidget(widget);
74 d->menu->layout()->setContentsMargins(0, 0, 0, 0);
75
76 connect(d->resourceList, SIGNAL(clicked(QModelIndex)), this, SLOT(indexChanged(QModelIndex)));
77
78 updateIcon();
79}
80
82{
83 /* Removing the actions here make them be deleted together with their default widget.
84 * This happens only if the actions are QWidgetAction, and we know they are since
85 * the only ones added are in KoResourcePopupAction constructor. */
86 int i = 0;
87 while(d->menu->actions().size() > 0) {
88 d->menu->removeAction(d->menu->actions()[i]);
89 ++i;
90 }
91
92 delete d->menu;
93 delete d;
94}
95
100
107
109{
110 QModelIndex index = d->model->indexForResource(resource);
111 if (index.isValid()) {
112 d->resourceList->setCurrentIndex(index);
113 indexChanged(index);
114 }
115}
116
118{
119 QModelIndex index = d->resourceList->currentIndex();
120 if (!index.isValid()) return 0;
121
122 KoResourceSP resource = d->model->resourceForIndex(index);
123 return resource;
124}
125
127{
128 d->canvasResourcesInterface = canvasResourcesInterface;
129}
130
131void KoResourcePopupAction::indexChanged(const QModelIndex &modelIndex)
132{
133 if (! modelIndex.isValid()) {
134 return;
135 }
136
137 d->menu->hide();
138
139 KoResourceSP resource = d->model->resourceForIndex(modelIndex);
140
141 if (resource) {
142 KoAbstractGradientSP gradient = resource.dynamicCast<KoAbstractGradient>();
143 KoPatternSP pattern = resource.dynamicCast<KoPattern>();
144 if (gradient) {
145 QGradient *qg = gradient->cloneAndBakeVariableColors(d->canvasResourcesInterface)->toQGradient();
146 if (qg) {
147 qg->setCoordinateMode(QGradient::ObjectBoundingMode);
149 }
150 } else if (pattern) {
152 qSharedPointerDynamicCast<KoPatternBackground>(d->background)->setPattern(pattern->pattern());
153 }
154
156
157 updateIcon();
158 }
159}
160
162{
163 QSize iconSize;
164 QToolButton *toolButton = dynamic_cast<QToolButton*>(parentWidget());
165 if (toolButton) {
166 iconSize = QSize(toolButton->iconSize());
167 } else {
168 iconSize = QSize(16, 16);
169 }
170
171 // This must be a QImage, as drawing to a QPixmap outside the
172 // UI thread will cause sporadic crashes.
173 QImage pm = QImage(iconSize, QImage::Format_ARGB32_Premultiplied);
174
175 pm.fill(Qt::transparent);
176
177 QPainter p(&pm);
178 QSharedPointer<KoGradientBackground> gradientBackground = qSharedPointerDynamicCast<KoGradientBackground>(d->background);
179 QSharedPointer<KoPatternBackground> patternBackground = qSharedPointerDynamicCast<KoPatternBackground>(d->background);
180
181 if (gradientBackground) {
182 QRect innerRect(0, 0, iconSize.width(), iconSize.height());
183 QLinearGradient paintGradient;
184 paintGradient.setStops(gradientBackground->gradient()->stops());
185 paintGradient.setStart(innerRect.topLeft());
186 paintGradient.setFinalStop(innerRect.topRight());
187
188 d->checkerPainter.paint(p, innerRect);
189 p.fillRect(innerRect, QBrush(paintGradient));
190 }
191 else if (patternBackground) {
192 d->checkerPainter.paint(p, QRect(QPoint(),iconSize));
193 p.fillRect(0, 0, iconSize.width(), iconSize.height(), patternBackground->pattern());
194 }
195
196 p.end();
197
198 setIcon(QIcon(QPixmap::fromImage(pm)));
199}
const Params2D p
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
int iconSize(qreal width, qreal height)
The resource item delegate for rendering the resource preview.
The KisResourceModel class provides the main access to resources. It is possible to filter the resour...
QModelIndex indexForResource(KoResourceSP resource) const override
indexFromResource
KoResourceSP resourceForIndex(QModelIndex index=QModelIndex()) const override
resourceForIndex returns a properly versioned and id'ed resource object
void paint(QPainter &painter, const QRectF &rect, const QPointF &patternOrigin) const
A gradient shape background.
A pattern shape background.
Write API docs here.
Definition KoPattern.h:21
KoCanvasResourcesInterfaceSP canvasResourcesInterface
KisResourceItemListView * resourceList
QSharedPointer< KoShapeBackground > background
void setCurrentBackground(QSharedPointer< KoShapeBackground > background)
void setCanvasResourcesInterface(KoCanvasResourcesInterfaceSP canvasResourcesInterface)
void indexChanged(const QModelIndex &modelIndex)
QSharedPointer< KoShapeBackground > currentBackground() const
void setCurrentResource(KoResourceSP resource)
KoResourcePopupAction(const QString &resourceType, KoCanvasResourcesInterfaceSP canvasResourcesInterface, QObject *parent=0)
KoResourceSP currentResource() const
void resourceSelected(QSharedPointer< KoShapeBackground > background)
Emitted when a resource was selected.
const QString Gradients
const QString PaintOpPresets
void setFixedToolTipThumbnailSize(const QSize &size)