9#include <QXmlStreamReader>
21#include <QGlobalStatic>
24#include <QPixmapCache>
26#include <QDomDocument>
27#include <QPainterPath>
51 dbgUI <<
"KisPaintingAssistantHandle ctor";
107 if (!assistant->
handles().contains(
this)) {
159 return m11 == b.m11 &&
m12 == b.m12 &&
m21 == b.m21 &&
m22 == b.m22;
205 dbgUI <<
"handle not found in the map, creating a new one...";
209 handleMap.insert(origHandle, mappedHandle);
211 dbgUI <<
"orig handle is null, not doing anything";
215 if (mappedHandle && registerAssistant) {
223 return d->
s->useCustomColor;
233 d->
s->assistantCustomColor = color;
238 return d->
s->assistantCustomColor;
243 d->
s->assistantGlobalColorCache = color;
248 return d->
s->useCustomColor ?
d->
s->assistantCustomColor :
d->
s->assistantGlobalColorCache;
255 d->
s->isSnappingActive =
true;
256 d->
s->outlineVisible =
true;
261 QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap)
262 : m_hasBeenInsideLocalRect(rhs.m_hasBeenInsideLocalRect)
265 dbgUI <<
"creating handles...";
272#define _REUSE_H(name) d->name = d->reuseOrCreateHandle(handleMap, rhs.d->name, this, false)
287 return d->
s->isSnappingActive;
292 d->
s->isSnappingActive = set;
297 return d->
s->brushPriority;
302 d->
s->brushPriority = priority;
307 d->
s->adjustedPositionValid =
false;
308 d->
s->followBrushPosition =
false;
314 d->
s->adjustedBrushPosition = position;
315 d->
s->adjustedPositionValid =
true;
320 d->
s->followBrushPosition = follow;
335 return d->
s->isLocal;
345 return d->
s->isLocked;
360 return d->
s->isDuplicating;
365 return d->
s->editorWidgetOffset;
370 d->
s->editorWidgetOffset = offset;
380 paintingColor.setAlpha(0.2 * paintingColor.alpha());
384 QPen pen_a = startingPen;
385 pen_a.setColor(paintingColor);
387 pen_a.setCosmetic(
true);
388 painter.setPen(pen_a);
389 painter.drawPath(path);
397 pen_a.setStyle(Qt::SolidLine);
398 pen_a.setCosmetic(
true);
399 painter.setPen(pen_a);
400 painter.drawPath(path);
409 pen_a.setCosmetic(
true);
410 painter.setPen(pen_a);
411 painter.drawPath(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);
457 Q_ASSERT(
d->
handles.contains(_handle));
459 Q_ASSERT(!
d->
handles.contains(_handle));
466 Q_ASSERT(!
d->
handles.contains(handle));
482 const int padding = 16;
484 editorWidgetPos.rx() = qBound(0.0,
486 canvasSize.width() - (editorSize.width() + padding));
487 editorWidgetPos.ry() = qBound(0.0,
489 canvasSize.height() - (editorSize.height() + padding));
496 Q_UNUSED(updateRect);
498 Q_UNUSED(previewVisible);
504 drawCache(gc, converter, displayRenderInterface, assistantVisible);
510 if (bound.isEmpty()) {
515 const QRect widgetBound =
transform.mapRect(bound);
517 const QRect paintRect =
transform.mapRect(bound).intersected(gc.viewport());
518 if (paintRect.isEmpty())
return;
521 bool found = QPixmapCache::find(
d->
s->cached, &cached);
525 d->
s->cachedRect.translated(widgetBound.topLeft()).contains(paintRect))) {
527 const QRect cacheRect = gc.viewport().adjusted(-100, -100, 100, 100).intersected(widgetBound);
528 Q_ASSERT(!cacheRect.isEmpty());
530 if (cached.isNull() || cached.size() != cacheRect.size()) {
531 cached = QPixmap(cacheRect.size());
534 cached.fill(Qt::transparent);
535 QPainter painter(&cached);
536 painter.setRenderHint(QPainter::Antialiasing);
537 painter.setWindow(cacheRect);
538 drawCache(painter, converter, displayRenderInterface, assistantVisible);
541 d->
s->cachedRect = cacheRect.translated(-widgetBound.topLeft());
542 d->
s->cached = QPixmapCache::insert(cached);
545 gc.drawPixmap(paintRect, cached, paintRect.translated(-widgetBound.topLeft() -
d->
s->cachedRect.topLeft()));
549 d->
s->m_canvas = canvas;
555 d->
s->cached = QPixmapCache::Key();
562 r = r.united(QRectF(*h, QSizeF(1,1)));
564 return r.adjusted(-2, -2, 2, 2).toAlignedRect();
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));
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));
609 xml.writeStartElement(
"handles");
611 int id = handleMap.size();
612 if (!handleMap.contains(handle)){
613 handleMap.insert(handle,
id);
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();
622 xml.writeEndElement();
624 xml.writeStartElement(
"sidehandles");
625 QMap<KisPaintingAssistantHandleSP, int> sideHandleMap;
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();
637 xml.writeEndElement();
638 xml.writeEndDocument();
650 double x = 0.0, y = 0.0;
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") {
660 auto active = xml.attributes().value(
"active");
663 if (xml.attributes().hasAttribute(
"brushPriority")) {
668 if ( xml.attributes().hasAttribute(
"useCustomColor")) {
671 bool usingColor =
false;
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()));
684 if ( xml.attributes().hasAttribute(
"customColor")) {
685 auto customColor = xml.attributes().value(
"customColor");
690 if ( xml.attributes().hasAttribute(
"locked")) {
691 auto locked = xml.attributes().value(
"locked");
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()) {
707 if (!handleMap.contains(
id)) {
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()) {
720 if (!sideHandleMap.contains(
id)) {
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
820 uint vHole = 0,hHole = 0;
822 if (
d->
handles.size() == 4 &&
d->
s->id ==
"perspective") {
827 hHandlesList.append(handle);
828 hHole = hHandlesList.size() - 1;
829 vHandlesList.append(handle);
830 vHole = vHandlesList.size() - 1;
834 while(hHole > 0 && hHandlesList.at(hHole -1).data()->x() > handle.
data()->x()) {
835 hHandlesList.swapItemsAt(hHole - 1, hHole);
841 while(vHole > 0 && vHandlesList.at(vHole -1).data()->y() > handle.
data()->y()) {
842 vHandlesList.swapItemsAt(vHole-1, vHole);
850 if(vHandlesList.at(0).data()->x() > vHandlesList.at(1).data()->x()) {
858 if(vHandlesList.at(2).data()->x() > vHandlesList.at(3).data()->x()) {
876 if(hHandlesList.at(0).data()->y() > hHandlesList.at(1).data()->y()) {
884 if(hHandlesList.at(2).data()->y() > hHandlesList.at(3).data()->y()) {
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))
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))
1061 int m_handleSize = 16;
1063 QRectF handlerect(pointTwo - QPointF(m_handleSize * 0.5, m_handleSize * 0.5), QSizeF(m_handleSize, m_handleSize));
1064 return handlerect.contains(pointOne);
1069 if (!
d->
s->m_canvas) {
1089 QPointF documentCoord =
d->
s->m_canvas->image()->pixelToDocument(pixelCoords);
1090 return d->
s->m_canvas->viewConverter()->documentToView(documentCoord);
1097 if (
d->
s->followBrushPosition &&
d->
s->adjustedPositionValid) {
1099 }
else if (canvas) {
1101 mousePos= canvas->
canvasWidget()->mapFromGlobal(QCursor::pos());
1104 mousePos = QCursor::pos();
1105 dbgUI <<
"no canvas given for assistant, you may have passed arguments incorrectly:";
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()));
1138 return p.x() *
p.x() +
p.y() *
p.y();
1148 QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> handleMap;
1150 for (
auto i = list.begin(); i != list.end(); ++i) {
1151 clonedList << (*i)->clone(handleMap);
1176 Q_FOREACH (
const QString &
id,
keys()) {
1179 dbgRegistry <<
"deleting KisPaintingAssistantFactoryRegistry ";
float value(const T *src, size_t ch)
Q_GLOBAL_STATIC(KisStoragePluginRegistry, s_instance)
KisAbstractCanvasWidget * canvasWidget
QColor defaultAssistantsColor(bool defaultValue=false) const
_Private::Traits< T >::Result widgetToDocument(const T &obj) const
QTransform documentToWidgetTransform() const
_Private::Traits< T >::Result documentToWidget(const T &obj) const
QSizeF getCanvasWidgetSize() const
~KisPaintingAssistantFactoryRegistry() override
KisPaintingAssistantFactoryRegistry()
static KisPaintingAssistantFactoryRegistry * instance()
virtual ~KisPaintingAssistantFactory()
KisPaintingAssistantFactory()
void mergeWith(KisPaintingAssistantHandleSP)
void registerAssistant(KisPaintingAssistant *)
void unregisterAssistant(KisPaintingAssistant *)
KisPaintingAssistant * chiefAssistant() const
KisPaintingAssistantHandle(double x, double y)
~KisPaintingAssistantHandle()
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)
bool isSnappingActive() const
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)
int brushPriority() const
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()
QColor effectiveAssistantColor() const
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...
bool isLocal() const
isLocal
QColor assistantCustomColor()
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())
void findPerspectiveAssistantHandleLocation()
virtual ~KisPaintingAssistant()
const KisPaintingAssistantHandleSP bottomLeft() const
void setAssistantCustomColor(QColor color)
QPointF pixelToView(const QPoint pixelCoords) const
bool m_hasBeenInsideLocalRect
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
QPointF editorWidgetOffset()
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)
void setSnappingActive(bool set)
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
QList< QString > keys() const
bool open(const QString &name)
QByteArray read(qint64 max)
KisSharedPtr< KisPaintingAssistantHandle > KisPaintingAssistantHandleSP
QString qColorToQString(QColor color)
QColor qStringToQColor(QString colorString)
QList< KisPaintingAssistant * > assistants
QColor assistantCustomColor
QPointF adjustedBrushPosition
QPointF editorWidgetOffset
struct KisPaintingAssistant::Private::SharedData::TranslationInvariantTransform cachedTransform
bool adjustedPositionValid
QColor assistantGlobalColorCache
KisPaintingAssistantHandleSP reuseOrCreateHandle(QMap< KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP > &handleMap, KisPaintingAssistantHandleSP origHandle, KisPaintingAssistant *q, bool registerAssistant=true)
QList< KisPaintingAssistantHandleSP > handles
KisPaintingAssistantHandleSP bottomLeft
const int previewLineWidth
QList< KisPaintingAssistantHandleSP > sideHandles
KisPaintingAssistantHandleSP topRight
KisPaintingAssistantHandleSP topMiddle
QSharedPointer< SharedData > s
KisPaintingAssistantHandleSP rightMiddle
KisPaintingAssistantHandleSP leftMiddle
KisPaintingAssistantHandleSP topLeft
KisPaintingAssistantHandleSP bottomRight
KisPaintingAssistantHandleSP bottomMiddle
static KoColorSpaceRegistry * instance()