• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • krita API Reference
  • KDE Home
  • Contact Us
 

KritaWidgets

  • sources
  • kfour-appscomplete
  • krita
  • libs
  • widgets
KoResourcePopupAction.cpp
Go to the documentation of this file.
1 /*
2  * Made by Tomislav Lukman ([email protected])
3  * SPDX-FileCopyrightText: 2012 Jean-Nicolas Artaud <[email protected]>
4  * SPDX-FileCopyrightText: 2019 Boudewijn Rempt <[email protected]>
5  *
6  * SPDX-License-Identifier: LGPL-2.0-or-later
7  */
8 
9 #include "KoResourcePopupAction.h"
10 
11 #include <KisResourceItemListView.h>
12 #include <KisResourceModel.h>
13 #include <KisResourceItemDelegate.h>
14 #include <KoResource.h>
15 
16 #include <KoCheckerBoardPainter.h>
17 #include <KoShapeBackground.h>
18 #include <resources/KoAbstractGradient.h>
19 #include <resources/KoPattern.h>
20 #include <KoGradientBackground.h>
21 #include <KoPatternBackground.h>
22 #include <KoImageCollection.h>
23 
24 #include <QMenu>
25 #include <QHBoxLayout>
26 #include <QHeaderView>
27 #include <QPainter>
28 #include <QGradient>
29 #include <QToolButton>
30 #include <QRect>
31 #include <QWidgetAction>
32 
33 class KoResourcePopupAction::Private
34 {
35 public:
36  QMenu *menu = 0;
37  KisResourceModel *model = 0;
38  KisResourceItemListView *resourceList = 0;
39  QSharedPointer<KoShapeBackground> background;
40  KoImageCollection *imageCollection = 0;
41  KoCheckerBoardPainter checkerPainter {4};
42  KoCanvasResourcesInterfaceSP canvasResourcesInterface;
43 };
44 
45 KoResourcePopupAction::KoResourcePopupAction(const QString &resourceType, KoCanvasResourcesInterfaceSP canvasResourcesInterface, QObject *parent)
46  : QAction(parent)
47  , d(new Private())
48 {
49  d->canvasResourcesInterface = canvasResourcesInterface;
50 
51  d->menu = new QMenu();
52  QWidget *widget = new QWidget();
53  QWidgetAction *wdgAction = new QWidgetAction(this);
54 
55  d->resourceList = new KisResourceItemListView(widget);
56 
57  d->model = new KisResourceModel(resourceType, this);
58  d->resourceList->setModel(d->model);
59  d->resourceList->setItemDelegate(new KisResourceItemDelegate(widget));
60  d->resourceList->setCurrentIndex(d->model->index(0, 0));
61  if (resourceType==ResourceType::Gradients) {
62  d->resourceList->setViewMode(QListView::ListMode);
63  }
64  indexChanged(d->resourceList->currentIndex());
65  QHBoxLayout *layout = new QHBoxLayout(widget);
66  layout->addWidget(d->resourceList);
67 
68  wdgAction->setDefaultWidget(widget);
69  d->menu->addAction(wdgAction);
70  setMenu(d->menu);
71  new QHBoxLayout(d->menu);
72  d->menu->layout()->addWidget(widget);
73  d->menu->layout()->setMargin(0);
74 
75  connect(d->resourceList, SIGNAL(clicked(QModelIndex)), this, SLOT(indexChanged(QModelIndex)));
76 
77  updateIcon();
78 }
79 
80 KoResourcePopupAction::~KoResourcePopupAction()
81 {
82  /* Removing the actions here make them be deleted together with their default widget.
83  * This happens only if the actions are QWidgetAction, and we know they are since
84  * the only ones added are in KoResourcePopupAction constructor. */
85  int i = 0;
86  while(d->menu->actions().size() > 0) {
87  d->menu->removeAction(d->menu->actions()[i]);
88  ++i;
89  }
90 
91  delete d->menu;
92  delete d->imageCollection;
93  delete d;
94 }
95 
96 QSharedPointer<KoShapeBackground> KoResourcePopupAction::currentBackground() const
97 {
98  return d->background;
99 }
100 
101 void KoResourcePopupAction::setCurrentBackground(QSharedPointer<KoShapeBackground> background)
102 {
103  d->background = background;
104 
105  updateIcon();
106 }
107 
108 void KoResourcePopupAction::setCurrentResource(KoResourceSP resource)
109 {
110  QModelIndex index = d->model->indexForResource(resource);
111  if (index.isValid()) {
112  d->resourceList->setCurrentIndex(index);
113  indexChanged(index);
114  }
115 }
116 
117 KoResourceSP KoResourcePopupAction::currentResource() const
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 
126 void KoResourcePopupAction::setCanvasResourcesInterface(KoCanvasResourcesInterfaceSP canvasResourcesInterface)
127 {
128  d->canvasResourcesInterface = canvasResourcesInterface;
129 }
130 
131 void 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  qg->setCoordinateMode(QGradient::ObjectBoundingMode);
147  d->background = QSharedPointer<KoShapeBackground>(new KoGradientBackground(qg));
148  } else if (pattern) {
149  KoImageCollection *collection = new KoImageCollection();
150  d->background = QSharedPointer<KoShapeBackground>(new KoPatternBackground(collection));
151  qSharedPointerDynamicCast<KoPatternBackground>(d->background)->setPattern(pattern->pattern());
152  }
153 
154  emit resourceSelected(d->background);
155 
156  updateIcon();
157  }
158 }
159 
160 void KoResourcePopupAction::updateIcon()
161 {
162  QSize iconSize;
163  QToolButton *toolButton = dynamic_cast<QToolButton*>(parentWidget());
164  if (toolButton) {
165  iconSize = QSize(toolButton->iconSize());
166  } else {
167  iconSize = QSize(16, 16);
168  }
169 
170  // This must be a QImage, as drawing to a QPixmap outside the
171  // UI thread will cause sporadic crashes.
172  QImage pm = QImage(iconSize, QImage::Format_ARGB32_Premultiplied);
173 
174  pm.fill(Qt::transparent);
175 
176  QPainter p(&pm);
177  QSharedPointer<KoGradientBackground> gradientBackground = qSharedPointerDynamicCast<KoGradientBackground>(d->background);
178  QSharedPointer<KoPatternBackground> patternBackground = qSharedPointerDynamicCast<KoPatternBackground>(d->background);
179 
180  if (gradientBackground) {
181  QRect innerRect(0, 0, iconSize.width(), iconSize.height());
182  QLinearGradient paintGradient;
183  paintGradient.setStops(gradientBackground->gradient()->stops());
184  paintGradient.setStart(innerRect.topLeft());
185  paintGradient.setFinalStop(innerRect.topRight());
186 
187  d->checkerPainter.paint(p, innerRect);
188  p.fillRect(innerRect, QBrush(paintGradient));
189  }
190  else if (patternBackground) {
191  d->checkerPainter.paint(p, QRect(QPoint(),iconSize));
192  p.fillRect(0, 0, iconSize.width(), iconSize.height(), patternBackground->pattern());
193  }
194 
195  p.end();
196 
197  setIcon(QIcon(QPixmap::fromImage(pm)));
198 }
KoResourcePopupAction::setCurrentBackground
void setCurrentBackground(QSharedPointer< KoShapeBackground > background)
Definition: KoResourcePopupAction.cpp:101
QPixmap::fromImage
QPixmap fromImage(const QImage &image, QFlags< Qt::ImageConversionFlag > flags)
QImage::fill
void fill(uint pixelValue)
QRect
QAction::setMenu
void setMenu(QMenu *menu)
QAction::parentWidget
QWidget * parentWidget() const
QWidget
QMenu
QSize
KoResourcePopupAction::currentResource
KoResourceSP currentResource() const
Definition: KoResourcePopupAction.cpp:117
KoResourcePopupAction::currentBackground
QSharedPointer< KoShapeBackground > currentBackground() const
Definition: KoResourcePopupAction.cpp:96
QSize::width
int width() const
QSharedPointer< KoShapeBackground >
QWidgetAction
QPainter
QGradient
QPainter::fillRect
void fillRect(const QRectF &rectangle, const QBrush &brush)
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
KoResourcePopupAction::setCurrentResource
void setCurrentResource(KoResourceSP resource)
Definition: KoResourcePopupAction.cpp:108
QWidgetAction::setDefaultWidget
void setDefaultWidget(QWidget *widget)
QGradient::setStops
void setStops(const QGradientStops &stopPoints)
KoResourcePopupAction.h
QAction::menu
QMenu * menu() const
QAction::setIcon
void setIcon(const QIcon &icon)
KoResourcePopupAction::resourceSelected
void resourceSelected(QSharedPointer< KoShapeBackground > background)
Emitted when a resource was selected.
QPainter::end
bool end()
QGradient::setCoordinateMode
void setCoordinateMode(CoordinateMode mode)
QObject
QSize::height
int height() const
QString
QToolButton
KoResourcePopupAction::KoResourcePopupAction
KoResourcePopupAction(const QString &resourceType, KoCanvasResourcesInterfaceSP canvasResourcesInterface, QObject *parent=0)
Constructs a KoResourcePopupAction (gradient or pattern) with the specified parent.
Definition: KoResourcePopupAction.cpp:45
QIcon
QModelIndex::isValid
bool isValid() const
QBrush
QAbstractButton::iconSize
iconSize
KoResourcePopupAction::~KoResourcePopupAction
~KoResourcePopupAction() override
Destructor.
Definition: KoResourcePopupAction.cpp:80
QLinearGradient
KoResourcePopupAction::setCanvasResourcesInterface
void setCanvasResourcesInterface(KoCanvasResourcesInterfaceSP canvasResourcesInterface)
Definition: KoResourcePopupAction.cpp:126
QHBoxLayout
QAction
KoResourcePopupAction::updateIcon
void updateIcon()
Definition: KoResourcePopupAction.cpp:160
QModelIndex
QImage
QPoint
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Jan 19 2021 23:44:00 by doxygen 1.8.16 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KritaWidgets

Skip menu "KritaWidgets"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

krita API Reference

Skip menu "krita API Reference"
  • libs
  •   KritaBasicFlakes
  •   brush
  •   KritaUndo2
  •   KritaFlake
  •   image
  •   KritaPlugin
  •   Krita
  •   KritaPigment
  •   KritaResources
  •   KritaStore
  •   ui
  •   KritaWidgets
  •   KritaWidgetUtils
  • plugins
  •   Assitants
  •   Extensions
  •   Filters
  •   Generators
  •   Formats
  •           src
  •   PaintOps
  •     libpaintop

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal