Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_preset_live_preview_view.cpp
Go to the documentation of this file.
1
2/*
3 * SPDX-FileCopyrightText: 2017 Scott Petrovic <scottpetrovic@gmail.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8#include <QEvent>
9#include <QGraphicsPixmapItem>
10#include <QGraphicsScene>
11#include <QPalette>
12#include <QSet>
13
15
17#include <kis_image.h>
18#include <kis_paint_layer.h>
23#include <kis_brush.h>
25#include "kis_transaction.h"
29
31 : QGraphicsView(parent),
32 m_updateCompressor(DefaultUpdateDelayMs, KisSignalCompressor::FIRST_ACTIVE)
33{
34 connect(&m_updateCompressor, SIGNAL(timeout()), SLOT(updateStroke()));
35}
36
42
44{
46
47 // initializing to nullptr helps check later if they actually have something in them
48 m_noPreviewText = nullptr;
49 m_sceneImageItem = nullptr;
50
51 setHorizontalScrollBarPolicy ( Qt::ScrollBarAlwaysOff );
52 setVerticalScrollBarPolicy ( Qt::ScrollBarAlwaysOff );
53
54
55 // layer image needs to be big enough to get an entire stroke of data
56 m_canvasSize.setWidth(this->width());
57 m_canvasSize.setHeight(this->height());
58
59 m_canvasCenterPoint.setX(m_canvasSize.width()*0.5);
60 m_canvasCenterPoint.setY(m_canvasSize.height()*0.5);
61
63 m_image = new KisImage(0, m_canvasSize.width(), m_canvasSize.height(), m_colorSpace, "stroke sample image");
64
65 m_layer = new KisPaintLayer(m_image, "livePreviewStrokeSample", OPACITY_OPAQUE_U8, m_colorSpace);
66
67 // set scene for the view
68 m_brushPreviewScene = new QGraphicsScene();
69 setScene(m_brushPreviewScene);
70
71}
72
77
82
91
96
101
103{
104 if (!m_currentPreset) {
105 return;
106 }
107
110 return;
111 }
112
113 m_previewPresetId = m_currentPreset->resourceId();
114
115 // do not paint a stroke if we are any of these engines (they have some issue currently)
119 return;
120 }
121
124}
125
127{
129
130 if (m_currentPreset->settings()->paintOpCompositeOp() == COMPOSITE_ERASE) {
132 KisPainter painter = KisPainter(dev);
133
134 // TODO: These are configurable for the canvas, figure out what to do with that
135 KoColor checker1(QColor("#dcdcdc"), m_image->colorSpace());
136 KoColor checker2(QColor("#ffffff"), m_image->colorSpace());
137
138 int checkerSize = 12;
139
140 // Just get the upper one, and draw into the unused space
141 int numCheckersV = std::ceil(m_image->height() / (float)checkerSize);
142 int numCheckersH = std::ceil(m_image->width() / (float)checkerSize);
143
144 painter.fill(m_image->bounds().x(), m_image->bounds().y(), m_image->bounds().width(), m_image->bounds().height(), checker2);
145
146 bool altColor = true;
147 for (int y = 0; y < numCheckersV; y++) {
148 for (int x = 0; x < numCheckersH; x++) {
149 altColor = !altColor;
150 if (altColor) {
151 continue;
152 }
153 painter.fill(x * checkerSize, y * checkerSize, checkerSize, checkerSize, checker1);
154 }
155
156 //Only need to do it if there's an even number of squares
157 if (numCheckersH % 2 == 0) {
158 altColor = !altColor;
159 }
160 }
161
165 }
166 painter.bitBlt(m_image->bounds().topLeft(), m_layer->paintDevice(), m_image->bounds());
167
169 } else {
171 }
172
173 if (!m_cachePreviewMode) {
174 // only add the object once...then just update the pixmap so we can move the preview around
175 if (!m_sceneImageItem) {
176 m_sceneImageItem = m_brushPreviewScene->addPixmap(QPixmap::fromImage(m_lastPreviewImage));
177 } else {
178 m_sceneImageItem->setPixmap(QPixmap::fromImage(m_lastPreviewImage));
179 }
180 }
181
182 // Emit preview for caching.
183 const int presetId =
185 if (presetId >= 0 && !m_lastPreviewImage.isNull()) {
187 }
188
190}
191
193{
194 const int grayStrips = 20;
195 const float sectionPercent = 1.0f / grayStrips;
196 const int imageWidth = m_layer->image()->width();
197 const int imageHeight = m_layer->image()->height();
198
199 for (int i = 0; i < grayStrips; i++) {
200 KoColor fillColor(m_layer->paintDevice()->colorSpace());
201 fillColor.fromQColor((i % 2) ? QColor(80, 80, 80) : QColor(140, 140, 140));
202
203 const QRect fillRect(imageWidth * sectionPercent * i,
204 0,
205 imageWidth * (sectionPercent * i + sectionPercent),
206 imageHeight);
207
209 m_layer->paintDevice()->fill(fillRect, fillColor);
210 t.end();
211 }
212}
213
215{
216 // clean up "no preview" text object if it exists. we will add it later if we need it
217 if (m_noPreviewText) {
218 this->scene()->removeItem(m_noPreviewText);
219 m_noPreviewText = nullptr;
220 }
221
222 const QString paintOpId = m_currentPreset->paintOp().id();
223
224 const QColor backgroundColor = palette().color(QPalette::Window);
225
226 if (cacheMode) {
229 m_paintColor = KoColor(QColor(Qt::white), m_colorSpace);
230 return;
231 }
232
235 }
236 else if (m_currentPreset->settings()->paintOpCompositeOp() == COMPOSITE_ERASE) {
237 // Doesn't really matter what the color is, just has to be something for the eraser to erase, we're just
238 // gonna use the alpha of the result
240 }
241 else {
243 }
244
245 m_paintColor = KoColor(QColor(Qt::white), m_colorSpace);
246 return;
247 }
248
249 const QColor textColor = palette().color(QPalette::Text);
250
253 m_paintColor = KoColor(textColor, m_colorSpace);
254 }
257 m_layer->paintDevice()->fill(m_image->bounds(), KoColor(backgroundColor, m_colorSpace));
258 t.end();
259
260 m_paintColor = KoColor(textColor, m_colorSpace);
261
262 QFont font;
263 font.setPixelSize(14);
264 font.setBold(false);
265
266 m_noPreviewText = this->scene()->addText(i18n("No Preview for this engine"),font);
267 m_noPreviewText->setDefaultTextColor(textColor);
268 m_noPreviewText->setPos(50, this->height()/4);
269
270 return;
271 }
272 else {
274 m_layer->paintDevice()->fill(m_image->bounds(), KoColor(backgroundColor, m_colorSpace));
275 t.end();
276
277 m_paintColor = KoColor(textColor, m_colorSpace);
278 }
279}
280
281class NotificationStroke : public QObject, public KisSimpleStrokeStrategy
282{
283 Q_OBJECT
284public:
286 : KisSimpleStrokeStrategy(QLatin1String("NotificationStroke"))
287 {
291 }
292
293 void initStrokeCallback() override {
294 Q_EMIT timeout();
295 }
296
297 void cancelStrokeCallback() override {
298 Q_EMIT cancelled();
299 }
300
301Q_SIGNALS:
302 void timeout();
303 void cancelled();
304};
305
307{
308 // limit the brush stroke size. larger brush strokes just don't look good and are CPU intensive
309 // we are making a proxy preset and setting it to the painter...otherwise setting the brush size of the original preset
310 // will fire off signals that make this run in an infinite loop
311 qreal previewSize = qBound(3.0, m_currentPreset->settings()->paintOpSize(), 25.0 ); // constrain live preview brush size
312 //Except for the sketchbrush where it determines the history.
313 if (m_currentPreset->paintOp().id() == "sketchbrush" ||
314 m_currentPreset->paintOp().id() == "spraybrush") {
315 previewSize = qMax(3.0, m_currentPreset->settings()->paintOpSize());
316 }
317
318
319 KisPaintOpPresetSP proxy_preset = m_currentPreset->clone().dynamicCast<KisPaintOpPreset>();
320 KisPaintOpSettingsSP settings = proxy_preset->settings();
321 settings->setPaintOpSize(previewSize);
322
323 if (settings->paintOpCompositeOp() != COMPOSITE_ERASE) {
324 settings->setPaintOpCompositeOp(COMPOSITE_OVER);
325 }
326
327 int maxTextureSize = 200;
328 int textureOffsetX = settings->getInt("Texture/Pattern/MaximumOffsetX")*2;
329 int textureOffsetY = settings->getInt("Texture/Pattern/MaximumOffsetY")*2;
330 double textureScale = settings->getDouble("Texture/Pattern/Scale");
331 if ( textureOffsetX*textureScale> maxTextureSize || textureOffsetY*textureScale > maxTextureSize) {
332 int maxSize = qMax(textureOffsetX, textureOffsetY);
333 double result = qreal(maxTextureSize) / maxSize;
334 settings->setProperty("Texture/Pattern/Scale", result);
335 }
336 if (proxy_preset->paintOp().id() == "spraybrush") {
337
338 QDomElement element;
339 QDomDocument d;
340 QString brushDefinition = settings->getString("brush_definition");
341 if (!brushDefinition.isEmpty()) {
342 d.setContent(brushDefinition);
343 element = d.firstChildElement("Brush");
344
346
347 qreal width = brush->image().width();
348 qreal scale = brush->scale();
349 qreal diameterToBrushRatio = 1.0;
350 qreal diameter = settings->getInt("Spray/diameter");
351 //hack, 1000 being the maximum possible brushsize.
352 if (brush->filename().endsWith(".svg", Qt::CaseInsensitive)) {
353 diameterToBrushRatio = diameter/(1000.0*scale);
354 scale = 25.0 / 1000.0;
355 } else {
356 if (width * scale > 25.0) {
357 diameterToBrushRatio = diameter / (width * scale);
358 scale = 25.0 / width;
359
360 if (!settings->getBool("SprayShape/proportional")) {
361 settings->setProperty("SprayShape/width", qRound(scale * settings->getInt("SprayShape/width")));
362 settings->setProperty("SprayShape/height", qRound(scale * settings->getInt("SprayShape/height")));
363 }
364 }
365 }
366 settings->setProperty("Spray/diameter", int(25.0 * diameterToBrushRatio));
367
368 brush->setScale(scale);
369 d.clear();
370 element = d.createElement("Brush");
371 brush->toXML(d, element);
372 d.appendChild(element);
373 settings->setProperty("brush_definition", d.toString());
374 }
375 }
376
377 proxy_preset->setSettings(settings);
378
379 KisResourcesSnapshotSP resources =
382 0, {},
383 proxy_preset);
384 resources->setOpacity(settings->paintOpOpacity());
385 resources->setMirroring(false, false); // ignore mirroring in toolbar
386
387 if (settings->paintOpCompositeOp() != COMPOSITE_ERASE) {
389 } else {
391 }
392
395
396 KisStrokeStrategy *stroke =
397 new FreehandStrokeStrategy(resources, strokeInfo, kundo2_noi18n("temp_stroke"));
398
399 KisStrokeId strokeId = m_image->startStroke(stroke);
400
401 if (proxy_preset->paintOp().id() == "mypaintbrush") {
402
405 }
406
407 // paint the stroke. The sketchbrush gets a different shape than the others to show how it works
408 if (proxy_preset->paintOp().id() == "sketchbrush"
409 || proxy_preset->paintOp().id() == "curvebrush"
410 || proxy_preset->paintOp().id() == "particlebrush") {
411 qreal startX = m_canvasCenterPoint.x() - (this->width()*0.4);
412 qreal endX = m_canvasCenterPoint.x() + (this->width()*0.4);
413 qreal middle = m_canvasCenterPoint.y();
414 KisPaintInformation pointOne;
415 pointOne.setPressure(0.0);
416 pointOne.setPos(QPointF(startX, middle));
417 KisPaintInformation pointTwo;
418 pointTwo.setPressure(0.0);
419 pointTwo.setPos(QPointF(startX, middle));
420 int repeats = 8;
421
422 for (int i = 0; i < repeats; i++) {
423 pointOne.setPos(pointTwo.pos());
424 pointOne.setPressure(pointTwo.pressure());
425
426 pointTwo.setPressure((1.0/repeats)*(i+1));
427 qreal xPos = ((1.0/repeats) * (i+1) * (endX-startX) )+startX;
428 pointTwo.setPos(QPointF(xPos, middle));
429
430 qreal offset = (this->height()/(repeats*1.5))*(i+1);
431 qreal handleY = middle + offset;
432 if (i%2 == 0) {
433 handleY = middle - offset;
434 }
435
436 m_image->addJob(strokeId,
438 pointOne,
439 QPointF(pointOne.pos().x(),
440 handleY),
441 QPointF(pointTwo.pos().x(),
442 handleY),
443 pointTwo));
445 }
446
447 } else {
448
449 // paint an S curve
450 m_curvePointPI1.setPos(QPointF(m_canvasCenterPoint.x() - (this->width()*0.45),
451 m_canvasCenterPoint.y() + (this->height()*0.2)));
453
454
455 m_curvePointPI2.setPos(QPointF(m_canvasCenterPoint.x() + (this->width()*0.4),
456 m_canvasCenterPoint.y() - (this->height()*0.2) ));
457
459
460 m_image->addJob(strokeId,
463 QPointF(m_canvasCenterPoint.x(),
464 m_canvasCenterPoint.y()-this->height()),
465 QPointF(m_canvasCenterPoint.x(),
466 m_canvasCenterPoint.y()+this->height()),
469 }
470 m_image->endStroke(strokeId);
471
473
474 NotificationStroke *notificationStroke = new NotificationStroke();
475 connect(notificationStroke, SIGNAL(timeout()), SLOT(slotPreviewGenerationCompleted()));
476 KisStrokeId notificationId = m_image->startStroke(notificationStroke);
477 m_image->endStroke(notificationId);
478
479
480 // TODO: if we don't have any regressions because of it until 4.2.8, then
481 // just remove this code.
482 // even though the brush is cloned, the proxy_preset still has some connection to the original preset which will mess brush sizing
483 // we need to return brush size to normal.The normal brush sends out a lot of extra signals, so keeping the proxy for now
484 //proxy_preset->settings()->setPaintOpSize(originalPresetSize);
485
486}
487
489{
490 QWidget::changeEvent(event);
491 if (event->type() == QEvent::PaletteChange && !m_cachePreviewMode) {
492 if (m_currentPreset) {
494 }
495 }
496}
497
498#include "kis_preset_live_preview_view.moc"
const quint8 OPACITY_OPAQUE_U8
const QString COMPOSITE_OVER
const QString COMPOSITE_ERASE
static bool needsStripedBackground(const QString &paintOpId)
True if paint engine needs striped background (colorsmudge, deform, filter).
static bool isNoPreviewEngine(const QString &paintOpId)
True if paint engine has no preview support.
static KisBrushSP fromXML(const QDomElement &element, KisResourcesInterfaceSP resourcesInterface)
static KisResourcesInterfaceSP instance()
const KoColorSpace * colorSpace() const
qint32 width() const
void addJob(KisStrokeId id, KisStrokeJobData *data) override
KisStrokeId startStroke(KisStrokeStrategy *strokeStrategy) override
qint32 height() const
QRect bounds() const override
void endStroke(KisStrokeId id) override
virtual void clear()
void fill(const QRect &rc, const KoColor &color)
const KoColorSpace * colorSpace() const
QImage convertToQImage(const KoColorProfile *dstProfile, qint32 x, qint32 y, qint32 w, qint32 h, KoColorConversionTransformation::Intent renderingIntent=KoColorConversionTransformation::internalRenderingIntent(), KoColorConversionTransformation::ConversionFlags conversionFlags=KoColorConversionTransformation::internalConversionFlags()) const
void setPos(const QPointF &p)
const QPointF & pos() const
void setCurrentTime(qreal time) const
void setPressure(qreal p)
Set the pressure.
qreal pressure() const
The pressure of the value (from 0.0 to 1.0)
void fill(qint32 x, qint32 y, qint32 width, qint32 height, const KoColor &color)
void bitBlt(qint32 dstX, qint32 dstY, const KisPaintDeviceSP srcDev, qint32 srcX, qint32 srcY, qint32 srcWidth, qint32 srcHeight)
void setCompositeOpId(const KoCompositeOp *op)
int m_previewPresetId
presetId (resource id) associated with the in-flight preview generation.
QGraphicsScene * m_brushPreviewScene
the scene that can add items like text and the brush stroke image
void sigPreviewImageReady(int presetId, QImage previewImage)
Emitted when a preview image is generated.
const KoColorSpace * m_colorSpace
internally sets the color space for brush preview
void setupAndPaintStroke()
creates and performs the actual stroke that goes on top of the background this is internally and shou...
KoCanvasResourceProvider * m_resourceManager
internally sets the Resource Provider for brush preview (allowing gradients in preview)
void setCurrentPreset(KisPaintOpPresetSP preset)
set the current preset from resource manager for the live preview to use. Good to call this every str...
QGraphicsTextItem * m_noPreviewText
holds the 'no preview available' text object
KisImageSP m_image
internally sets the image area for brush preview
void paintBackground(bool cacheMode)
works as both clearing the previous stroke, providing striped backgrounds for smudging brushes,...
QGraphicsPixmapItem * m_sceneImageItem
holds the preview brush stroke data
QImage m_lastPreviewImage
Last preview image.
KisLayerSP m_layer
internally sets the layer area for brush preview
void setup(KoCanvasResourceProvider *resourceManager)
one time setup for initialization of many variables. This live preview might be in a UI file,...
KoCanvasResourceProvider * resourceManager() const
KoColor m_paintColor
the color which is used for rendering the stroke
void paintStripedBackground()
Paint alternating gray stripes on the paint device.
The KisResourcesSnapshot class takes a snapshot of the various resources like colors and settings use...
void setFGColorOverride(const KoColor &color)
void setMirroring(bool horizontal, bool vertical)
void setCompositeOpId(const QString &compositeOpId)
Override the composite op for the snapshot.
void enableJob(JobType type, bool enable=true, KisStrokeJobData::Sequentiality sequentiality=KisStrokeJobData::SEQUENTIAL, KisStrokeJobData::Exclusivity exclusivity=KisStrokeJobData::NORMAL)
void setClearsRedoOnStart(bool value)
void fromQColor(const QColor &c)
Convenient function for converting from a QColor.
Definition KoColor.cpp:213
KUndo2MagicString kundo2_noi18n(const QString &text)
KisImageWSP image
virtual KisPaintDeviceSP paintDevice() const =0
static KoColorSpaceRegistry * instance()
const KoColorSpace * rgb8(const QString &profileName=QString())