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

#include <KoResourcePopupAction.h>

+ Inheritance diagram for KoResourcePopupAction:

Classes

class  Private
 

Public Slots

void updateIcon ()
 

Signals

void resourceSelected (QSharedPointer< KoShapeBackground > background)
 Emitted when a resource was selected.
 

Public Member Functions

QSharedPointer< KoShapeBackgroundcurrentBackground () const
 
KoResourceSP currentResource () const
 
 KoResourcePopupAction (const QString &resourceType, KoCanvasResourcesInterfaceSP canvasResourcesInterface, QObject *parent=0)
 
void setCanvasResourcesInterface (KoCanvasResourcesInterfaceSP canvasResourcesInterface)
 
void setCurrentBackground (QSharedPointer< KoShapeBackground > background)
 
void setCurrentResource (KoResourceSP resource)
 
 ~KoResourcePopupAction () override
 

Private Slots

void indexChanged (const QModelIndex &modelIndex)
 

Private Attributes

Private *const d
 

Detailed Description

Definition at line 26 of file KoResourcePopupAction.h.

Constructor & Destructor Documentation

◆ KoResourcePopupAction()

KoResourcePopupAction::KoResourcePopupAction ( const QString & resourceType,
KoCanvasResourcesInterfaceSP canvasResourcesInterface,
QObject * parent = 0 )
explicit

Constructs a KoResourcePopupAction (gradient or pattern) with the specified parent.

Parameters
gradientResourceAdapterpointer to the gradient or pattern
parentThe parent for this action.

Definition at line 43 of file KoResourcePopupAction.cpp.

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}
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
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...
KoCanvasResourcesInterfaceSP canvasResourcesInterface
KisResourceItemListView * resourceList
void indexChanged(const QModelIndex &modelIndex)
const QString Gradients
const QString PaintOpPresets
void setFixedToolTipThumbnailSize(const QSize &size)

References KoResourcePopupAction::Private::canvasResourcesInterface, connect(), d, ResourceType::Gradients, indexChanged(), KoResourcePopupAction::Private::menu, KoResourcePopupAction::Private::model, ResourceType::PaintOpPresets, KoResourcePopupAction::Private::resourceList, KisResourceItemListView::setFixedToolTipThumbnailSize(), and updateIcon().

◆ ~KoResourcePopupAction()

KoResourcePopupAction::~KoResourcePopupAction ( )
override

Destructor

Definition at line 81 of file KoResourcePopupAction.cpp.

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}

References d, and KoResourcePopupAction::Private::menu.

Member Function Documentation

◆ currentBackground()

QSharedPointer< KoShapeBackground > KoResourcePopupAction::currentBackground ( ) const

Definition at line 96 of file KoResourcePopupAction.cpp.

97{
98 return d->background;
99}
QSharedPointer< KoShapeBackground > background

References KoResourcePopupAction::Private::background, and d.

◆ currentResource()

KoResourceSP KoResourcePopupAction::currentResource ( ) const

Definition at line 117 of file KoResourcePopupAction.cpp.

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}
KoResourceSP resourceForIndex(QModelIndex index=QModelIndex()) const override
resourceForIndex returns a properly versioned and id'ed resource object

References d, KoResourcePopupAction::Private::model, KisResourceModel::resourceForIndex(), and KoResourcePopupAction::Private::resourceList.

◆ indexChanged

void KoResourcePopupAction::indexChanged ( const QModelIndex & modelIndex)
privateslot

Definition at line 131 of file KoResourcePopupAction.cpp.

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}
A gradient shape background.
A pattern shape background.
Write API docs here.
Definition KoPattern.h:21
void resourceSelected(QSharedPointer< KoShapeBackground > background)
Emitted when a resource was selected.

References KoResourcePopupAction::Private::background, KoResourcePopupAction::Private::canvasResourcesInterface, d, KoResourcePopupAction::Private::menu, KoResourcePopupAction::Private::model, KisResourceModel::resourceForIndex(), resourceSelected(), and updateIcon().

◆ resourceSelected

void KoResourcePopupAction::resourceSelected ( QSharedPointer< KoShapeBackground > background)
signal

Emitted when a resource was selected.

◆ setCanvasResourcesInterface()

void KoResourcePopupAction::setCanvasResourcesInterface ( KoCanvasResourcesInterfaceSP canvasResourcesInterface)

Definition at line 126 of file KoResourcePopupAction.cpp.

127{
128 d->canvasResourcesInterface = canvasResourcesInterface;
129}

References KoResourcePopupAction::Private::canvasResourcesInterface, and d.

◆ setCurrentBackground()

void KoResourcePopupAction::setCurrentBackground ( QSharedPointer< KoShapeBackground > background)

Definition at line 101 of file KoResourcePopupAction.cpp.

102{
103 d->background = background;
104
105 updateIcon();
106}

References KoResourcePopupAction::Private::background, d, and updateIcon().

◆ setCurrentResource()

void KoResourcePopupAction::setCurrentResource ( KoResourceSP resource)

Definition at line 108 of file KoResourcePopupAction.cpp.

109{
110 QModelIndex index = d->model->indexForResource(resource);
111 if (index.isValid()) {
112 d->resourceList->setCurrentIndex(index);
113 indexChanged(index);
114 }
115}
QModelIndex indexForResource(KoResourceSP resource) const override
indexFromResource

References d, indexChanged(), KisResourceModel::indexForResource(), KoResourcePopupAction::Private::model, and KoResourcePopupAction::Private::resourceList.

◆ updateIcon

void KoResourcePopupAction::updateIcon ( )
slot

Definition at line 161 of file KoResourcePopupAction.cpp.

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
int iconSize(qreal width, qreal height)
void paint(QPainter &painter, const QRectF &rect, const QPointF &patternOrigin) const

References KoResourcePopupAction::Private::background, KoResourcePopupAction::Private::checkerPainter, d, iconSize(), p, and KoCheckerBoardPainter::paint().

Member Data Documentation

◆ d

Private* const KoResourcePopupAction::d
private

Definition at line 64 of file KoResourcePopupAction.h.


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