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

snaps to the canvas grid More...

#include <KoSnapStrategy.h>

+ Inheritance diagram for GridSnapStrategy:

Public Member Functions

QPainterPath decoration (const KoViewConverter &converter) const override
 returns the current snap strategy decoration
 
 GridSnapStrategy ()
 
bool snap (const QPointF &mousePosition, KoSnapProxy *proxy, qreal maxSnapDistance) override
 
- Public Member Functions inherited from KoSnapStrategy
 KoSnapStrategy (KoSnapGuide::Strategy type)
 
QPointF snappedPosition () const
 returns the snapped position form the last call to snapToPoints
 
SnapType snappedType () const
 
KoSnapGuide::Strategy type () const
 returns the strategies type
 
virtual ~KoSnapStrategy ()
 

Additional Inherited Members

- Public Types inherited from KoSnapStrategy
enum  SnapType { ToPoint = 0 , ToLine }
 
- Static Public Member Functions inherited from KoSnapStrategy
static qreal scalarProduct (const QPointF &p1, const QPointF &p2)
 
static qreal squareDistance (const QPointF &p1, const QPointF &p2)
 
- Protected Member Functions inherited from KoSnapStrategy
void setSnappedPosition (const QPointF &position, SnapType snapType)
 sets the current snapped position
 

Detailed Description

snaps to the canvas grid

Definition at line 110 of file KoSnapStrategy.h.

Constructor & Destructor Documentation

◆ GridSnapStrategy()

GridSnapStrategy::GridSnapStrategy ( )

Definition at line 421 of file KoSnapStrategy.cpp.

423{
424}
KoSnapStrategy(KoSnapGuide::Strategy type)

Member Function Documentation

◆ decoration()

QPainterPath GridSnapStrategy::decoration ( const KoViewConverter & converter) const
overridevirtual

returns the current snap strategy decoration

Implements KoSnapStrategy.

Definition at line 488 of file KoSnapStrategy.cpp.

489{
490 QSizeF unzoomedSize = converter.viewToDocument(QSizeF(5, 5));
491 QPainterPath decoration;
492 decoration.moveTo(snappedPosition() - QPointF(unzoomedSize.width(), 0));
493 decoration.lineTo(snappedPosition() + QPointF(unzoomedSize.width(), 0));
494 decoration.moveTo(snappedPosition() - QPointF(0, unzoomedSize.height()));
495 decoration.lineTo(snappedPosition() + QPointF(0, unzoomedSize.height()));
496 return decoration;
497}
QPainterPath decoration(const KoViewConverter &converter) const override
returns the current snap strategy decoration
QPointF snappedPosition() const
returns the snapped position form the last call to snapToPoints
virtual QPointF viewToDocument(const QPointF &viewPoint) const

References decoration(), KoSnapStrategy::snappedPosition(), and KoViewConverter::viewToDocument().

◆ snap()

bool GridSnapStrategy::snap ( const QPointF & mousePosition,
KoSnapProxy * proxy,
qreal maxSnapDistance )
overridevirtual

Implements KoSnapStrategy.

Definition at line 426 of file KoSnapStrategy.cpp.

427{
428 Q_ASSERT(std::isfinite(maxSnapDistance));
429 if (! proxy->canvas()->snapToGrid())
430 return false;
431
432 // The 1e-10 here is a workaround for some weird division problem.
433 // 360.00062366 / 2.83465058 gives 127 'exactly' when shown as a qreal,
434 // but when casting into an int, we get 126. In fact it's 127 - 5.64e-15 !
435 QPointF offset;
436 QSizeF spacing;
437 proxy->canvas()->gridSize(&offset, &spacing);
438
439 // we want to snap to the nearest grid point, so calculate
440 // the grid rows/columns before and after the points position
441 int col = static_cast<int>((mousePosition.x() - offset.x()) / spacing.width() + 1e-10);
442 int nextCol = col + 1;
443 int row = static_cast<int>((mousePosition.y() - offset.y()) / spacing.height() + 1e-10);
444 int nextRow = row + 1;
445
446 // now check which grid line has less distance to the point
447 qreal distToCol = qAbs(offset.x() + col * spacing.width() - mousePosition.x());
448 qreal distToNextCol = qAbs(offset.x() + nextCol * spacing.width() - mousePosition.x());
449
450 if (distToCol > distToNextCol) {
451 col = nextCol;
452 distToCol = distToNextCol;
453 }
454
455 qreal distToRow = qAbs(offset.y() + row * spacing.height() - mousePosition.y());
456 qreal distToNextRow = qAbs(offset.y() + nextRow * spacing.height() - mousePosition.y());
457 if (distToRow > distToNextRow) {
458 row = nextRow;
459 distToRow = distToNextRow;
460 }
461
462 QPointF snappedPoint = mousePosition;
463 SnapType snapType = ToPoint;
464
465 bool pointIsSnapped = false;
466
467 const qreal sqDistance = distToCol * distToCol + distToRow * distToRow;
468 const qreal maxSqDistance = maxSnapDistance * maxSnapDistance;
469 // now check if we are inside the snap distance
470 if (sqDistance < maxSqDistance) {
471 snappedPoint = QPointF(offset.x() + col * spacing.width(), offset.y() + row * spacing.height());
472 pointIsSnapped = true;
473 } else if (distToRow < maxSnapDistance) {
474 snappedPoint.ry() = offset.y() + row * spacing.height();
475 snapType = ToLine;
476 pointIsSnapped = true;
477 } else if (distToCol < maxSnapDistance) {
478 snappedPoint.rx() = offset.x() + col * spacing.width();
479 snapType = ToLine;
480 pointIsSnapped = true;
481 }
482
483 setSnappedPosition(snappedPoint, snapType);
484
485 return pointIsSnapped;
486}
virtual void gridSize(QPointF *offset, QSizeF *spacing) const =0
virtual bool snapToGrid() const =0
KoCanvasBase * canvas()
returns canvas we are working on
void setSnappedPosition(const QPointF &position, SnapType snapType)
sets the current snapped position

References KoSnapProxy::canvas(), KoCanvasBase::gridSize(), KoSnapStrategy::setSnappedPosition(), KoCanvasBase::snapToGrid(), KoSnapStrategy::ToLine, and KoSnapStrategy::ToPoint.


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