Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_painting_assistant.cc
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2008, 2011 Cyrille Berger <cberger@cberger.net>
3 * SPDX-FileCopyrightText: 2010 Geoffry Song <goffrie@gmail.com>
4 * SPDX-FileCopyrightText: 2017 Scott Petrovic <scottpetrovic@gmail.com>
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9#include <QXmlStreamReader>
12#include "kis_debug.h"
13#include "kis_dom_utils.h"
14#include <kis_canvas2.h>
15#include "kis_tool.h"
16#include "kis_config.h"
17
18#include <KoStore.h>
20
21#include <QGlobalStatic>
22#include <QPen>
23#include <QPainter>
24#include <QPixmapCache>
25#include <QDomElement>
26#include <QDomDocument>
27#include <QPainterPath>
28#include <QDebug>
29#include <memory>
30
32
37
38KisPaintingAssistantHandle::KisPaintingAssistantHandle(double x, double y) : QPointF(x, y), d(new Private)
39{
40}
41
45
47 : QPointF(rhs)
48 , KisShared()
49 , d(new Private)
50{
51 dbgUI << "KisPaintingAssistantHandle ctor";
52}
53
55{
56 setX(pt.x());
57 setY(pt.y());
58 return *this;
59}
60
62{
63 d->handle_type = type;
64}
65
67{
68 return d->handle_type;
69}
70
72{
73 return !d->assistants.isEmpty() ? d->assistants.first() : 0;
74}
75
77{
78 Q_ASSERT(d->assistants.empty());
79 delete d;
80}
81
83{
84 Q_ASSERT(!d->assistants.contains(assistant));
85 d->assistants.append(assistant);
86}
87
89{
90 d->assistants.removeOne(assistant);
91 Q_ASSERT(!d->assistants.contains(assistant));
92}
93
95{
96 return d->assistants.contains(assistant);
97}
98
100{
101 if(this->handleType()== HandleType::NORMAL || handle.data()->handleType()== HandleType::SIDE) {
102 return;
103 }
104
105
106 Q_FOREACH (KisPaintingAssistant* assistant, handle->d->assistants) {
107 if (!assistant->handles().contains(this)) {
108 assistant->replaceHandle(handle, this);
109 }
110 }
111}
112
114{
115 Q_FOREACH (KisPaintingAssistant* assistant, d->assistants) {
116 assistant->uncache();
117 }
118}
119
121 Private();
122 explicit Private(const Private &rhs);
123 KisPaintingAssistantHandleSP reuseOrCreateHandle(QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap, KisPaintingAssistantHandleSP origHandle, KisPaintingAssistant *q, bool registerAssistant = true);
125
127
128 // share everything except handles between the clones
129 struct SharedData {
130 QString id;
131 QString name;
132 bool isSnappingActive {true};
134 bool outlineVisible {true};
135 bool isLocal {false};
136 bool isLocked {false};
137 //The isDuplicating flag only exists to draw the duplicate button depressed when pressed
138 bool isDuplicating {false};
142
144
145 QPointF editorWidgetOffset {QPointF(0, 0)};
146
147 QPixmapCache::Key cached;
148 QRect cachedRect; // relative to boundingRect().topLeft()
149
151 qreal m11 {0.0};
152 qreal m12 {0.0};
153 qreal m21 {0.0};
154 qreal m22 {0.0};
155
157 TranslationInvariantTransform(const QTransform& t) : m11(t.m11()), m12(t.m12()), m21(t.m21()), m22(t.m22()) { }
159 return m11 == b.m11 && m12 == b.m12 && m21 == b.m21 && m22 == b.m22;
160 }
162
163 QColor assistantGlobalColorCache = QColor(Qt::red); // color to paint with if a custom color is not set
164
165 bool useCustomColor {false};
167 };
168
170
171
172 const int previewLineWidth {1};
173 const int mainLineWidth {2}; // for "drawPath" etc.
174 const int errorLineWidth {2};
175
177
178};
179
184
186 : s(rhs.s)
187{
188}
189
191{
192 // Clones do not get a copy of the shared data, so this function is necessary to copy
193 // the SharedData struct from the old assistant to this one. The function returns a
194 // reference to a new SharedData object copied from the original
197 *this->d->s = *sd;
198}
199
200KisPaintingAssistantHandleSP KisPaintingAssistant::Private::reuseOrCreateHandle(QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap, KisPaintingAssistantHandleSP origHandle, KisPaintingAssistant *q, bool registerAssistant)
201{
202 KisPaintingAssistantHandleSP mappedHandle = handleMap.value(origHandle);
203 if (!mappedHandle) {
204 if (origHandle) {
205 dbgUI << "handle not found in the map, creating a new one...";
206 mappedHandle = KisPaintingAssistantHandleSP(new KisPaintingAssistantHandle(*origHandle));
207 dbgUI << "done";
208 mappedHandle->setType(origHandle->handleType());
209 handleMap.insert(origHandle, mappedHandle);
210 } else {
211 dbgUI << "orig handle is null, not doing anything";
212 mappedHandle = KisPaintingAssistantHandleSP();
213 }
214 }
215 if (mappedHandle && registerAssistant) {
216 mappedHandle->registerAssistant(q);
217 }
218 return mappedHandle;
219}
220
222{
223 return d->s->useCustomColor;
224}
225
227{
228 d->s->useCustomColor = useCustomColor;
229}
230
232{
233 d->s->assistantCustomColor = color;
234}
235
237{
238 return d->s->assistantCustomColor;
239}
240
242{
243 d->s->assistantGlobalColorCache = color;
244}
245
247{
248 return d->s->useCustomColor ? d->s->assistantCustomColor : d->s->assistantGlobalColorCache;
249}
250
251KisPaintingAssistant::KisPaintingAssistant(const QString& id, const QString& name) : d(new Private)
252{
253 d->s->id = id;
254 d->s->name = name;
255 d->s->isSnappingActive = true;
256 d->s->outlineVisible = true;
257}
258
260 const KisPaintingAssistant &rhs,
261 QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap)
262 : m_hasBeenInsideLocalRect(rhs.m_hasBeenInsideLocalRect)
263 , d(new Private(*(rhs.d)))
264{
265 dbgUI << "creating handles...";
266 Q_FOREACH (const KisPaintingAssistantHandleSP origHandle, rhs.d->handles) {
267 d->handles << d->reuseOrCreateHandle(handleMap, origHandle, this);
268 }
269 Q_FOREACH (const KisPaintingAssistantHandleSP origHandle, rhs.d->sideHandles) {
270 d->sideHandles << d->reuseOrCreateHandle(handleMap, origHandle, this);
271 }
272#define _REUSE_H(name) d->name = d->reuseOrCreateHandle(handleMap, rhs.d->name, this, /* registerAssistant = */ false)
281#undef _REUSE_H
282 dbgUI << "done";
283}
284
286{
287 return d->s->isSnappingActive;
288}
289
291{
292 d->s->isSnappingActive = set;
293}
294
296{
297 return d->s->brushPriority;
298}
299
301{
302 d->s->brushPriority = priority;
303}
304
306{
307 d->s->adjustedPositionValid = false;
308 d->s->followBrushPosition = false;
310}
311
313{
314 d->s->adjustedBrushPosition = position;
315 d->s->adjustedPositionValid = true;
316}
317
319{
320 d->s->followBrushPosition = follow;
321}
322
324{
325 return getDefaultEditorPosition() + d->s->editorWidgetOffset;
326}
327
329{
330 return false;
331}
332
334{
335 return d->s->isLocal;
336}
337
339{
340 d->s->isLocal = value;
341}
342
344{
345 return d->s->isLocked;
346}
347
349{
350 d->s->isLocked = value;
351}
352
354{
355 d->s->isDuplicating = value;
356}
357
359{
360 return d->s->isDuplicating;
361}
362
364{
365 return d->s->editorWidgetOffset;
366}
367
369{
370 d->s->editorWidgetOffset = offset;
371}
372
373
374void KisPaintingAssistant::drawPath(QPainter& painter, const QPainterPath &path, const KoColorDisplayRendererInterface *displayRenderInterface, bool isSnappingOn, QPen startingPen)
375{
376
377 QColor paintingColor = displayRenderInterface->convertColorToDisplayColorSpace(KoColor(effectiveAssistantColor(), KoColorSpaceRegistry::instance()->rgb8()));;
378
379 if (!isSnappingOn) {
380 paintingColor.setAlpha(0.2 * paintingColor.alpha());
381 }
382
383 painter.save();
384 QPen pen_a = startingPen;
385 pen_a.setColor(paintingColor);
386 pen_a.setWidth(d->mainLineWidth * d->decorationThickness);
387 pen_a.setCosmetic(true);
388 painter.setPen(pen_a);
389 painter.drawPath(path);
390 painter.restore();
391}
392
393void KisPaintingAssistant::drawPreview(QPainter& painter, const QPainterPath &path, const KoColorDisplayRendererInterface *displayRenderInterface)
394{
395 painter.save();
397 pen_a.setStyle(Qt::SolidLine);
398 pen_a.setCosmetic(true);
399 painter.setPen(pen_a);
400 painter.drawPath(path);
401 painter.restore();
402}
403
404void KisPaintingAssistant::drawError(QPainter &painter, const QPainterPath &path, const KoColorDisplayRendererInterface *displayRenderInterface)
405{
406 painter.save();
407 const QColor error = displayRenderInterface->convertColorToDisplayColorSpace(KoColor(QColor(255, 0, 0, 125), KoColorSpaceRegistry::instance()->rgb8()));
408 QPen pen_a(error, d->errorLineWidth * d->decorationThickness);
409 pen_a.setCosmetic(true);
410 painter.setPen(pen_a);
411 painter.drawPath(path);
412 painter.restore();
413}
414
415void KisPaintingAssistant::drawX(QPainter &painter, const QPointF &pt, const KoColorDisplayRendererInterface *displayRenderInterface)
416{
417 QPainterPath path;
418 path.moveTo(QPointF(pt.x() - 5.0, pt.y() - 5.0)); path.lineTo(QPointF(pt.x() + 5.0, pt.y() + 5.0));
419 path.moveTo(QPointF(pt.x() - 5.0, pt.y() + 5.0)); path.lineTo(QPointF(pt.x() + 5.0, pt.y() - 5.0));
420 drawPath(painter, path, displayRenderInterface);
421}
422
424{
425 Q_ASSERT(d->handles.isEmpty());
426 d->handles = _handles;
427 Q_FOREACH (KisPaintingAssistantHandleSP handle, _handles) {
428 handle->registerAssistant(this);
429 }
430}
431
433{
434 Q_FOREACH (KisPaintingAssistantHandleSP handle, d->handles) {
435 handle->unregisterAssistant(this);
436 }
437 if(!d->sideHandles.isEmpty()) {
438 Q_FOREACH (KisPaintingAssistantHandleSP handle, d->sideHandles) {
439 handle->unregisterAssistant(this);
440 }
441 }
442 delete d;
443}
444
445const QString& KisPaintingAssistant::id() const
446{
447 return d->s->id;
448}
449
450const QString& KisPaintingAssistant::name() const
451{
452 return d->s->name;
453}
454
456{
457 Q_ASSERT(d->handles.contains(_handle));
458 d->handles.replace(d->handles.indexOf(_handle), _with);
459 Q_ASSERT(!d->handles.contains(_handle));
460 _handle->unregisterAssistant(this);
461 _with->registerAssistant(this);
462}
463
465{
466 Q_ASSERT(!d->handles.contains(handle));
467 if (HandleType::SIDE == type) {
468 d->sideHandles.append(handle);
469 } else {
470 d->handles.append(handle);
471 }
472
473 handle->registerAssistant(this);
474 handle.data()->setType(type);
475}
476
478{
479 QPointF editorDocumentPos = getEditorPosition();
480 QPointF editorWidgetPos = converter->documentToWidgetTransform().map(editorDocumentPos);
481 QSizeF canvasSize = converter->getCanvasWidgetSize();
482 const int padding = 16;
483
484 editorWidgetPos.rx() = qBound(0.0,
485 editorWidgetPos.x(),
486 canvasSize.width() - (editorSize.width() + padding));
487 editorWidgetPos.ry() = qBound(0.0,
488 editorWidgetPos.y(),
489 canvasSize.height() - (editorSize.height() + padding));
490
491 return converter->widgetToDocument(editorWidgetPos);
492}
493
494void KisPaintingAssistant::drawAssistant(QPainter& gc, const QRectF& updateRect, const KisCoordinatesConverter* converter, const KoColorDisplayRendererInterface *displayRenderInterface, bool useCache, KisCanvas2* canvas, bool assistantVisible, bool previewVisible)
495{
496 Q_UNUSED(updateRect);
497
498 Q_UNUSED(previewVisible);
499
501
502 if (!useCache) {
503 gc.save();
504 drawCache(gc, converter, displayRenderInterface, assistantVisible);
505 gc.restore();
506 return;
507 }
508
509 const QRect bound = boundingRect();
510 if (bound.isEmpty()) {
511 return;
512 }
513
514 const QTransform transform = converter->documentToWidgetTransform();
515 const QRect widgetBound = transform.mapRect(bound);
516
517 const QRect paintRect = transform.mapRect(bound).intersected(gc.viewport());
518 if (paintRect.isEmpty()) return;
519
520 QPixmap cached;
521 bool found = QPixmapCache::find(d->s->cached, &cached);
522
523 if (!(found &&
524 d->s->cachedTransform == transform &&
525 d->s->cachedRect.translated(widgetBound.topLeft()).contains(paintRect))) {
526
527 const QRect cacheRect = gc.viewport().adjusted(-100, -100, 100, 100).intersected(widgetBound);
528 Q_ASSERT(!cacheRect.isEmpty());
529
530 if (cached.isNull() || cached.size() != cacheRect.size()) {
531 cached = QPixmap(cacheRect.size());
532 }
533
534 cached.fill(Qt::transparent);
535 QPainter painter(&cached);
536 painter.setRenderHint(QPainter::Antialiasing);
537 painter.setWindow(cacheRect);
538 drawCache(painter, converter, displayRenderInterface, assistantVisible);
539 painter.end();
540 d->s->cachedTransform = transform;
541 d->s->cachedRect = cacheRect.translated(-widgetBound.topLeft());
542 d->s->cached = QPixmapCache::insert(cached);
543 }
544
545 gc.drawPixmap(paintRect, cached, paintRect.translated(-widgetBound.topLeft() - d->s->cachedRect.topLeft()));
546
547
548 if (canvas) {
549 d->s->m_canvas = canvas;
550 }
551}
552
554{
555 d->s->cached = QPixmapCache::Key();
556}
557
559{
560 QRectF r;
561 Q_FOREACH (KisPaintingAssistantHandleSP h, handles()) {
562 r = r.united(QRectF(*h, QSizeF(1,1)));
563 }
564 return r.adjusted(-2, -2, 2, 2).toAlignedRect();
565}
566
568{
569 return true;
570}
571
572void KisPaintingAssistant::transform(const QTransform &transform)
573{
574 Q_FOREACH(KisPaintingAssistantHandleSP handle, handles()) {
575 if (handle->chiefAssistant() != this) continue;
576
577 *handle = transform.map(*handle);
578 }
579
580 Q_FOREACH(KisPaintingAssistantHandleSP handle, sideHandles()) {
581 if (handle->chiefAssistant() != this) continue;
582
583 *handle = transform.map(*handle);
584 }
585
586 uncache();
587}
588
589QByteArray KisPaintingAssistant::saveXml(QMap<KisPaintingAssistantHandleSP, int> &handleMap)
590{
591 QByteArray data;
592 QXmlStreamWriter xml(&data);
593 xml.writeStartDocument();
594 xml.writeStartElement("assistant");
595 xml.writeAttribute("type",d->s->id);
596 xml.writeAttribute("active", QString::number(d->s->isSnappingActive));
597 xml.writeAttribute("brushPriority", QString::number(d->s->brushPriority));
598 xml.writeAttribute("useCustomColor", QString::number(d->s->useCustomColor));
599 xml.writeAttribute("customColor", KisDomUtils::qColorToQString(d->s->assistantCustomColor));
600 xml.writeAttribute("locked", QString::number(d->s->isLocked));
601 xml.writeAttribute("editorWidgetOffset_X", QString::number((double)(d->s->editorWidgetOffset.x()), 'f', 3));
602 xml.writeAttribute("editorWidgetOffset_Y", QString::number((double)(d->s->editorWidgetOffset.y()), 'f', 3));
603
604
605
606 saveCustomXml(&xml); // if any specific assistants have custom XML data to save to
607
608 // write individual handle data
609 xml.writeStartElement("handles");
610 Q_FOREACH (const KisPaintingAssistantHandleSP handle, d->handles) {
611 int id = handleMap.size();
612 if (!handleMap.contains(handle)){
613 handleMap.insert(handle, id);
614 }
615 id = handleMap.value(handle);
616 xml.writeStartElement("handle");
617 xml.writeAttribute("id", QString::number(id));
618 xml.writeAttribute("x", QString::number(double(handle->x()), 'f', 3));
619 xml.writeAttribute("y", QString::number(double(handle->y()), 'f', 3));
620 xml.writeEndElement();
621 }
622 xml.writeEndElement();
623 if (!d->sideHandles.isEmpty()) { // for vanishing points only
624 xml.writeStartElement("sidehandles");
625 QMap<KisPaintingAssistantHandleSP, int> sideHandleMap;
626 Q_FOREACH (KisPaintingAssistantHandleSP handle, d->sideHandles) {
627 int id = sideHandleMap.size();
628 sideHandleMap.insert(handle, id);
629 xml.writeStartElement("sidehandle");
630 xml.writeAttribute("id", QString::number(id));
631 xml.writeAttribute("x", QString::number(double(handle->x()), 'f', 3));
632 xml.writeAttribute("y", QString::number(double(handle->y()), 'f', 3));
633 xml.writeEndElement();
634 }
635 }
636
637 xml.writeEndElement();
638 xml.writeEndDocument();
639 return data;
640}
641
642void KisPaintingAssistant::saveCustomXml(QXmlStreamWriter* xml)
643{
644 Q_UNUSED(xml);
645}
646
647void KisPaintingAssistant::loadXml(KoStore* store, QMap<int, KisPaintingAssistantHandleSP> &handleMap, QString path)
648{
649 int id = 0;
650 double x = 0.0, y = 0.0;
651 store->open(path);
652 QByteArray data = store->read(store->size());
653 QXmlStreamReader xml(data);
654 QMap<int, KisPaintingAssistantHandleSP> sideHandleMap;
655 while (!xml.atEnd()) {
656 switch (xml.readNext()) {
657 case QXmlStreamReader::StartElement:
658 if (xml.name() == "assistant") {
659
660 auto active = xml.attributes().value("active");
661 setSnappingActive( (active != "0") );
662
663 if (xml.attributes().hasAttribute("brushPriority")) {
664 setBrushPriority(xml.attributes().value("brushPriority").toInt());
665 }
666
667 // load custom shared assistant properties
668 if ( xml.attributes().hasAttribute("useCustomColor")) {
669 auto useCustomColor = xml.attributes().value("useCustomColor");
670
671 bool usingColor = false;
672 if (useCustomColor.toString() == "1") {
673 usingColor = true;
674 }
675
676
677 setUseCustomColor(usingColor);
678 }
679
680 if (xml.attributes().hasAttribute("editorWidgetOffset_X") && xml.attributes().hasAttribute("editorWidgetOffset_Y")) {
681 setEditorWidgetOffset(QPointF(xml.attributes().value("editorWidgetOffset_X").toDouble(), xml.attributes().value("editorWidgetOffset_Y").toDouble()));
682 }
683
684 if ( xml.attributes().hasAttribute("customColor")) {
685 auto customColor = xml.attributes().value("customColor");
686 setAssistantCustomColor( KisDomUtils::qStringToQColor(customColor.toString()) );
687
688 }
689
690 if ( xml.attributes().hasAttribute("locked")) {
691 auto locked = xml.attributes().value("locked");
692 setLocked(locked == "1");
693 }
694
695 }
696
697 loadCustomXml(&xml);
698
699 if (xml.name() == "handle") {
700 QString strId = xml.attributes().value("id").toString(),
701 strX = xml.attributes().value("x").toString(),
702 strY = xml.attributes().value("y").toString();
703 if (!strId.isEmpty() && !strX.isEmpty() && !strY.isEmpty()) {
704 id = strId.toInt();
705 x = strX.toDouble();
706 y = strY.toDouble();
707 if (!handleMap.contains(id)) {
708 handleMap.insert(id, new KisPaintingAssistantHandle(x, y));
709 }
710 }
711 addHandle(handleMap.value(id), HandleType::NORMAL);
712 } else if (xml.name() == "sidehandle") {
713 QString strId = xml.attributes().value("id").toString(),
714 strX = xml.attributes().value("x").toString(),
715 strY = xml.attributes().value("y").toString();
716 if (!strId.isEmpty() && !strX.isEmpty() && !strY.isEmpty()) {
717 id = strId.toInt();
718 x = strX.toDouble();
719 y = strY.toDouble();
720 if (!sideHandleMap.contains(id)) {
721 sideHandleMap.insert(id, new KisPaintingAssistantHandle(x, y));
722 }
723 }
724 addHandle(sideHandleMap.value(id), HandleType::SIDE);
725
726 }
727 break;
728 default:
729 break;
730 }
731 }
732 store->close();
733}
734
735bool KisPaintingAssistant::loadCustomXml(QXmlStreamReader* xml)
736{
737 Q_UNUSED(xml);
738 return true;
739}
740
741void KisPaintingAssistant::saveXmlList(QDomDocument& doc, QDomElement& assistantsElement,int count)
742{
743 if (d->s->id == "ellipse"){
744 QDomElement assistantElement = doc.createElement("assistant");
745 assistantElement.setAttribute("type", "ellipse");
746 assistantElement.setAttribute("filename", QString("ellipse%1.assistant").arg(count));
747 assistantsElement.appendChild(assistantElement);
748 }
749 else if (d->s->id == "spline"){
750 QDomElement assistantElement = doc.createElement("assistant");
751 assistantElement.setAttribute("type", "spline");
752 assistantElement.setAttribute("filename", QString("spline%1.assistant").arg(count));
753 assistantsElement.appendChild(assistantElement);
754 }
755 else if (d->s->id == "perspective"){
756 QDomElement assistantElement = doc.createElement("assistant");
757 assistantElement.setAttribute("type", "perspective");
758 assistantElement.setAttribute("filename", QString("perspective%1.assistant").arg(count));
759 assistantsElement.appendChild(assistantElement);
760 }
761 else if (d->s->id == "vanishing point"){
762 QDomElement assistantElement = doc.createElement("assistant");
763 assistantElement.setAttribute("type", "vanishing point");
764 assistantElement.setAttribute("filename", QString("vanishing point%1.assistant").arg(count));
765 assistantsElement.appendChild(assistantElement);
766 }
767 else if (d->s->id == "infinite ruler"){
768 QDomElement assistantElement = doc.createElement("assistant");
769 assistantElement.setAttribute("type", "infinite ruler");
770 assistantElement.setAttribute("filename", QString("infinite ruler%1.assistant").arg(count));
771 assistantsElement.appendChild(assistantElement);
772 }
773 else if (d->s->id == "parallel ruler"){
774 QDomElement assistantElement = doc.createElement("assistant");
775 assistantElement.setAttribute("type", "parallel ruler");
776 assistantElement.setAttribute("filename", QString("parallel ruler%1.assistant").arg(count));
777 assistantsElement.appendChild(assistantElement);
778 }
779 else if (d->s->id == "concentric ellipse"){
780 QDomElement assistantElement = doc.createElement("assistant");
781 assistantElement.setAttribute("type", "concentric ellipse");
782 assistantElement.setAttribute("filename", QString("concentric ellipse%1.assistant").arg(count));
783 assistantsElement.appendChild(assistantElement);
784 }
785 else if (d->s->id == "fisheye-point"){
786 QDomElement assistantElement = doc.createElement("assistant");
787 assistantElement.setAttribute("type", "fisheye-point");
788 assistantElement.setAttribute("filename", QString("fisheye-point%1.assistant").arg(count));
789 assistantsElement.appendChild(assistantElement);
790 }
791 else if (d->s->id == "ruler"){
792 QDomElement assistantElement = doc.createElement("assistant");
793 assistantElement.setAttribute("type", "ruler");
794 assistantElement.setAttribute("filename", QString("ruler%1.assistant").arg(count));
795 assistantsElement.appendChild(assistantElement);
796 }
797 else if (d->s->id == "two point"){
798 QDomElement assistantElement = doc.createElement("assistant");
799 assistantElement.setAttribute("type", "two point");
800 assistantElement.setAttribute("filename", QString("two point%1.assistant").arg(count));
801 assistantsElement.appendChild(assistantElement);
802 }
803 else if (d->s->id == "perspective ellipse"){
804 QDomElement assistantElement = doc.createElement("assistant");
805 assistantElement.setAttribute("type", "perspective ellipse");
806 assistantElement.setAttribute("filename", QString("perspective ellipse%1.assistant").arg(count));
807 assistantsElement.appendChild(assistantElement);
808 }
809 else if (d->s->id == "curvilinear-perspective"){
810 QDomElement assistantElement = doc.createElement("assistant");
811 assistantElement.setAttribute("type", "curvilinear-perspective");
812 assistantElement.setAttribute("filename", QString("curvilinear-perspective%1.assistant").arg(count));
813 assistantsElement.appendChild(assistantElement);
814 }
815}
816
820 uint vHole = 0,hHole = 0;
822 if (d->handles.size() == 4 && d->s->id == "perspective") {
823 //get the handle opposite to the first handle
824 oppHandle = oppHandleOne();
825 //Sorting handles into two list, X sorted and Y sorted into hHandlesList and vHandlesList respectively.
826 Q_FOREACH (const KisPaintingAssistantHandleSP handle,d->handles) {
827 hHandlesList.append(handle);
828 hHole = hHandlesList.size() - 1;
829 vHandlesList.append(handle);
830 vHole = vHandlesList.size() - 1;
831 /*
832 sort handles on the basis of X-coordinate
833 */
834 while(hHole > 0 && hHandlesList.at(hHole -1).data()->x() > handle.data()->x()) {
835 hHandlesList.swapItemsAt(hHole - 1, hHole);
836 hHole = hHole - 1;
837 }
838 /*
839 sort handles on the basis of Y-coordinate
840 */
841 while(vHole > 0 && vHandlesList.at(vHole -1).data()->y() > handle.data()->y()) {
842 vHandlesList.swapItemsAt(vHole-1, vHole);
843 vHole = vHole - 1;
844 }
845 }
846
847 /*
848 give the handles their respective positions
849 */
850 if(vHandlesList.at(0).data()->x() > vHandlesList.at(1).data()->x()) {
851 d->topLeft = vHandlesList.at(1);
852 d->topRight= vHandlesList.at(0);
853 }
854 else {
855 d->topLeft = vHandlesList.at(0);
856 d->topRight = vHandlesList.at(1);
857 }
858 if(vHandlesList.at(2).data()->x() > vHandlesList.at(3).data()->x()) {
859 d->bottomLeft = vHandlesList.at(3);
860 d->bottomRight = vHandlesList.at(2);
861 }
862 else {
863 d->bottomLeft= vHandlesList.at(2);
864 d->bottomRight = vHandlesList.at(3);
865 }
866
867 /*
868 find if the handles that should be opposite are actually oppositely positioned
869 */
870 if (( (d->topLeft == d->handles.at(0).data() && d->bottomRight == oppHandle) ||
871 (d->topLeft == oppHandle && d->bottomRight == d->handles.at(0).data()) ||
872 (d->topRight == d->handles.at(0).data() && d->bottomLeft == oppHandle) ||
873 (d->topRight == oppHandle && d->bottomLeft == d->handles.at(0).data()) ) )
874 {}
875 else {
876 if(hHandlesList.at(0).data()->y() > hHandlesList.at(1).data()->y()) {
877 d->topLeft = hHandlesList.at(1);
878 d->bottomLeft= hHandlesList.at(0);
879 }
880 else {
881 d->topLeft = hHandlesList.at(0);
882 d->bottomLeft = hHandlesList.at(1);
883 }
884 if(hHandlesList.at(2).data()->y() > hHandlesList.at(3).data()->y()) {
885 d->topRight = hHandlesList.at(3);
886 d->bottomRight = hHandlesList.at(2);
887 }
888 else {
889 d->topRight= hHandlesList.at(2);
890 d->bottomRight = hHandlesList.at(3);
891 }
892
893 }
894 /*
895 Setting the middle handles as needed
896 */
897 if(!d->bottomMiddle && !d->topMiddle && !d->leftMiddle && !d->rightMiddle) {
898
899 // Before re-adding the handles, clear old ones that have been
900 // potentially loaded from disk and not re-associated with the
901 // xxxMiddle pointers in d; otherwise those would stay in place.
902 if(!d->sideHandles.isEmpty()) {
903 Q_FOREACH (KisPaintingAssistantHandleSP handle, d->sideHandles) {
904 handle->unregisterAssistant(this);
905 }
906 d->sideHandles.clear();
907 }
908
910 (d->bottomLeft.data()->y() + d->bottomRight.data()->y())*0.5);
911 d->topMiddle = new KisPaintingAssistantHandle((d->topLeft.data()->x() + d->topRight.data()->x())*0.5,
912 (d->topLeft.data()->y() + d->topRight.data()->y())*0.5);
914 (d->topRight.data()->y() + d->bottomRight.data()->y())*0.5);
915 d->leftMiddle= new KisPaintingAssistantHandle((d->bottomLeft.data()->x() + d->topLeft.data()->x())*0.5,
916 (d->bottomLeft.data()->y() + d->topLeft.data()->y())*0.5);
917
922 }
923 else
924 {
925 d->bottomMiddle.data()->operator =(QPointF((d->bottomLeft.data()->x() + d->bottomRight.data()->x())*0.5,
926 (d->bottomLeft.data()->y() + d->bottomRight.data()->y())*0.5));
927 d->topMiddle.data()->operator =(QPointF((d->topLeft.data()->x() + d->topRight.data()->x())*0.5,
928 (d->topLeft.data()->y() + d->topRight.data()->y())*0.5));
929 d->rightMiddle.data()->operator =(QPointF((d->topRight.data()->x() + d->bottomRight.data()->x())*0.5,
930 (d->topRight.data()->y() + d->bottomRight.data()->y())*0.5));
931 d->leftMiddle.data()->operator =(QPointF((d->bottomLeft.data()->x() + d->topLeft.data()->x())*0.5,
932 (d->bottomLeft.data()->y() + d->topLeft.data()->y())*0.5));
933 }
934
935 }
936}
937
939{
940 QPointF intersection(0,0);
941 if((QLineF(d->handles.at(0).data()->toPoint(),d->handles.at(1).data()->toPoint()).intersects(QLineF(d->handles.at(2).data()->toPoint(),d->handles.at(3).data()->toPoint()), &intersection) != QLineF::NoIntersection)
942 && (QLineF(d->handles.at(0).data()->toPoint(),d->handles.at(1).data()->toPoint()).intersects(QLineF(d->handles.at(2).data()->toPoint(),d->handles.at(3).data()->toPoint()), &intersection) != QLineF::UnboundedIntersection))
943 {
944 return d->handles.at(1);
945 }
946 else if((QLineF(d->handles.at(0).data()->toPoint(),d->handles.at(2).data()->toPoint()).intersects(QLineF(d->handles.at(1).data()->toPoint(),d->handles.at(3).data()->toPoint()), &intersection) != QLineF::NoIntersection)
947 && (QLineF(d->handles.at(0).data()->toPoint(),d->handles.at(2).data()->toPoint()).intersects(QLineF(d->handles.at(1).data()->toPoint(),d->handles.at(3).data()->toPoint()), &intersection) != QLineF::UnboundedIntersection))
948 {
949 return d->handles.at(2);
950 }
951 else
952 {
953 return d->handles.at(3);
954 }
955}
956
961
966
971
976
981
986
991
996
1001
1006
1011
1016
1021
1026
1031
1036
1041
1046
1051
1056
1057
1058
1059bool KisPaintingAssistant::areTwoPointsClose(const QPointF& pointOne, const QPointF& pointTwo)
1060{
1061 int m_handleSize = 16;
1062
1063 QRectF handlerect(pointTwo - QPointF(m_handleSize * 0.5, m_handleSize * 0.5), QSizeF(m_handleSize, m_handleSize));
1064 return handlerect.contains(pointOne);
1065}
1066
1068{
1069 if (!d->s->m_canvas) {
1070 return 0;
1071 }
1072
1073
1074 if (areTwoPointsClose(point, pixelToView(topLeft()->toPoint()))) {
1075 return topLeft();
1076 } else if (areTwoPointsClose(point, pixelToView(topRight()->toPoint()))) {
1077 return topRight();
1078 } else if (areTwoPointsClose(point, pixelToView(bottomLeft()->toPoint()))) {
1079 return bottomLeft();
1080 } else if (areTwoPointsClose(point, pixelToView(bottomRight()->toPoint()))) {
1081 return bottomRight();
1082 }
1083 return 0;
1084}
1085
1086
1087QPointF KisPaintingAssistant::pixelToView(const QPoint pixelCoords) const
1088{
1089 QPointF documentCoord = d->s->m_canvas->image()->pixelToDocument(pixelCoords);
1090 return d->s->m_canvas->viewConverter()->documentToView(documentCoord);
1091}
1092
1094{
1095 QPointF mousePos;
1096
1097 if (d->s->followBrushPosition && d->s->adjustedPositionValid) {
1098 mousePos = converter->documentToWidget(d->s->adjustedBrushPosition);
1099 } else if (canvas) {
1100 // FIXME: this may be simple and cheap, but it's only integer precision!
1101 mousePos= canvas->canvasWidget()->mapFromGlobal(QCursor::pos());
1102 } else {
1103 //...of course, you need to have access to a canvas-widget for that.//
1104 mousePos = QCursor::pos(); //this'll give an offset//
1105 dbgUI << "no canvas given for assistant, you may have passed arguments incorrectly:";
1106 }
1107 return mousePos;
1108}
1109
1114
1119
1121{
1122 if (!isLocal() || !firstLocalHandle() || !secondLocalHandle()) {
1123 return QRectF();
1124 }
1125
1128
1129 QPointF topLeft = QPointF(qMin(first->x(), second->x()), qMin(first->y(), second->y()));
1130 QPointF bottomRight = QPointF(qMax(first->x(), second->x()), qMax(first->y(), second->y()));
1131
1132 QRectF rect(topLeft, bottomRight);
1133 return rect;
1134}
1135
1136double KisPaintingAssistant::norm2(const QPointF& p)
1137{
1138 return p.x() * p.x() + p.y() * p.y();
1139}
1140
1142{
1143 d->decorationThickness = thickness;
1144}
1145
1147{
1148 QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> handleMap;
1150 for (auto i = list.begin(); i != list.end(); ++i) {
1151 clonedList << (*i)->clone(handleMap);
1152 }
1153 return clonedList;
1154}
1155
1156
1157
1158/*
1159 * KisPaintingAssistantFactory classes
1160*/
1161
1165
1169
1173
1175{
1176 Q_FOREACH (const QString &id, keys()) {
1177 delete get(id);
1178 }
1179 dbgRegistry << "deleting KisPaintingAssistantFactoryRegistry ";
1180}
1181
float value(const T *src, size_t ch)
const Params2D p
Q_GLOBAL_STATIC(KisStoragePluginRegistry, s_instance)
unsigned int uint
KisAbstractCanvasWidget * canvasWidget
QColor defaultAssistantsColor(bool defaultValue=false) const
_Private::Traits< T >::Result widgetToDocument(const T &obj) const
_Private::Traits< T >::Result documentToWidget(const T &obj) const
static KisPaintingAssistantFactoryRegistry * instance()
void mergeWith(KisPaintingAssistantHandleSP)
void registerAssistant(KisPaintingAssistant *)
void unregisterAssistant(KisPaintingAssistant *)
KisPaintingAssistant * chiefAssistant() const
KisPaintingAssistantHandle(double x, double y)
bool containsAssistant(KisPaintingAssistant *) const
KisPaintingAssistantHandle & operator=(const QPointF &)
virtual QRect boundingRect() const
virtual KisPaintingAssistantHandleSP secondLocalHandle() const
secondLocalHandle Note: this doesn't guarantee it will be the bottomRight corner! For that,...
void drawError(QPainter &painter, const QPainterPath &path, const KoColorDisplayRendererInterface *displayRenderInterface)
const KisPaintingAssistantHandleSP topMiddle() const
static QList< KisPaintingAssistantSP > cloneAssistantList(const QList< KisPaintingAssistantSP > &list)
QPointF effectiveBrushPosition(const KisCoordinatesConverter *converter, KisCanvas2 *canvas) const
Query the effective brush position to be used for preview lines. This is intended to be used for pain...
bool isDuplicating()
isDuplicating
void setDecorationThickness(int thickness)
void setLocked(bool value)
setLocked
void setBrushPriority(int priority)
void setDuplicating(bool value)
setDuplicating
virtual bool isAssistantComplete() const
void addHandle(KisPaintingAssistantHandleSP handle, HandleType type)
virtual QPointF getDefaultEditorPosition() const =0
void drawPreview(QPainter &painter, const QPainterPath &path, const KoColorDisplayRendererInterface *displayRenderInterface)
void drawX(QPainter &painter, const QPointF &pt, const KoColorDisplayRendererInterface *displayRenderInterface)
virtual void transform(const QTransform &transform)
virtual void drawAssistant(QPainter &gc, const QRectF &updateRect, const KisCoordinatesConverter *converter, const KoColorDisplayRendererInterface *displayRenderInterface, bool cached, KisCanvas2 *canvas=0, bool assistantVisible=true, bool previewVisible=true)
const KisPaintingAssistantHandleSP bottomMiddle() const
virtual void saveCustomXml(QXmlStreamWriter *xml)
void setAssistantGlobalColorCache(const QColor &color)
KisPaintingAssistantHandleSP topLeft()
const QString & name() const
void copySharedData(KisPaintingAssistantSP assistant)
const KisPaintingAssistantHandleSP bottomRight() const
QRectF getLocalRect() const
getLocalRect The function deals with local handles not being topLeft and bottomRight gracefully and r...
const KisPaintingAssistantHandleSP topLeft() const
virtual void setAdjustedBrushPosition(const QPointF position)
const QList< KisPaintingAssistantHandleSP > & sideHandles() const
KisPaintingAssistantHandleSP closestCornerHandleFromPoint(QPointF point)
virtual bool canBeLocal() const
canBeLocal
virtual void drawCache(QPainter &gc, const KisCoordinatesConverter *converter, const KoColorDisplayRendererInterface *displayRenderInterface, bool assistantVisible=true)=0
performance layer where the graphics can be drawn from a cache instead of generated every render upda...
void saveXmlList(QDomDocument &doc, QDomElement &assistantsElement, int count)
static double norm2(const QPointF &p)
void setEditorWidgetOffset(QPointF offset)
void drawPath(QPainter &painter, const QPainterPath &path, const KoColorDisplayRendererInterface *displayRenderInterface, bool drawActive=true, QPen startingPen=QPen())
const KisPaintingAssistantHandleSP bottomLeft() const
void setAssistantCustomColor(QColor color)
QPointF pixelToView(const QPoint pixelCoords) const
virtual bool loadCustomXml(QXmlStreamReader *xml)
virtual QPointF getEditorPosition() const
virtual KisPaintingAssistantHandleSP firstLocalHandle() const
firstLocalHandle Note: this doesn't guarantee it will be the topleft corner! For that,...
const KisPaintingAssistantHandleSP rightMiddle() const
void setLocal(bool value)
setLocal
KisPaintingAssistant(const QString &id, const QString &name)
virtual void setFollowBrushPosition(bool follow)
const QString & id() const
void loadXml(KoStore *store, QMap< int, KisPaintingAssistantHandleSP > &handleMap, QString path)
QPointF viewportConstrainedEditorPosition(const KisCoordinatesConverter *converter, const QSize editorSize)
const QList< KisPaintingAssistantHandleSP > & handles() const
void setUseCustomColor(bool useCustomColor)
void replaceHandle(KisPaintingAssistantHandleSP _handle, KisPaintingAssistantHandleSP _with)
KisPaintingAssistantHandleSP oppHandleOne()
bool areTwoPointsClose(const QPointF &pointOne, const QPointF &pointTwo)
const KisPaintingAssistantHandleSP leftMiddle() const
QByteArray saveXml(QMap< KisPaintingAssistantHandleSP, int > &handleMap)
const KisPaintingAssistantHandleSP topRight() const
KisPaintingAssistantHandleSP bottomRight()
void initHandles(QList< KisPaintingAssistantHandleSP > _handles)
virtual QColor convertColorToDisplayColorSpace(const KoColor color) const =0
convertColorToDisplayColorSpace
KisPaintingAssistantFactory * get(const QString &id) const
bool close()
Definition KoStore.cpp:156
qint64 size() const
Definition KoStore.cpp:239
bool open(const QString &name)
Definition KoStore.cpp:109
QByteArray read(qint64 max)
Definition KoStore.cpp:181
#define dbgUI
Definition kis_debug.h:56
#define dbgRegistry
Definition kis_debug.h:51
#define _REUSE_H(name)
KisSharedPtr< KisPaintingAssistantHandle > KisPaintingAssistantHandleSP
QString qColorToQString(QColor color)
QColor qStringToQColor(QString colorString)
QList< KisPaintingAssistant * > assistants
struct KisPaintingAssistant::Private::SharedData::TranslationInvariantTransform cachedTransform
KisPaintingAssistantHandleSP reuseOrCreateHandle(QMap< KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP > &handleMap, KisPaintingAssistantHandleSP origHandle, KisPaintingAssistant *q, bool registerAssistant=true)
QList< KisPaintingAssistantHandleSP > handles
KisPaintingAssistantHandleSP bottomLeft
QList< KisPaintingAssistantHandleSP > sideHandles
KisPaintingAssistantHandleSP topRight
KisPaintingAssistantHandleSP topMiddle
QSharedPointer< SharedData > s
KisPaintingAssistantHandleSP rightMiddle
KisPaintingAssistantHandleSP leftMiddle
KisPaintingAssistantHandleSP topLeft
KisPaintingAssistantHandleSP bottomRight
KisPaintingAssistantHandleSP bottomMiddle
static KoColorSpaceRegistry * instance()