Krita Source Code Documentation
Loading...
Searching...
No Matches
KoShapeRubberSelectStrategy.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2
3 SPDX-FileCopyrightText: 2006 Thorsten Zachmann <zachmann@kde.org>
4 SPDX-FileCopyrightText: 2006 Thomas Zander <zander@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
11#include "KoViewConverter.h"
12
13#include <QPainter>
14
15#include "KoShapeManager.h"
16#include "KoSelection.h"
17#include "KoCanvasBase.h"
18
19KoShapeRubberSelectStrategy::KoShapeRubberSelectStrategy(KoToolBase *tool, const QPointF &clicked, bool useSnapToGrid)
21{
23 d->snapGuide->enableSnapStrategies(KoSnapGuide::GridSnapping);
24 d->snapGuide->enableSnapping(useSnapToGrid);
25
26 d->selectRect = QRectF(d->snapGuide->snap(clicked, QFlags<Qt::KeyboardModifier>()), QSizeF(0, 0));
27}
28
29void KoShapeRubberSelectStrategy::paint(QPainter &painter, const KoViewConverter &converter)
30{
32 painter.setRenderHint(QPainter::Antialiasing, false);
33
34 const QColor crossingColor(80,130,8);
35 const QColor coveringColor(8,60,167);
36
37 QColor selectColor(
39 crossingColor : coveringColor);
40
41 selectColor.setAlphaF(0.8);
42 QPen select(selectColor, decorationThickness());
43 select.setCosmetic(true);
44 painter.setPen(select);
45
46 selectColor.setAlphaF(0.4);
47 const QBrush fillBrush(selectColor);
48 painter.setBrush(fillBrush);
49
50 QRectF paintRect = converter.documentToView(d->selectedRect());
51 paintRect = paintRect.normalized();
52
53 painter.drawRect(paintRect);
54}
55
56void KoShapeRubberSelectStrategy::handleMouseMove(const QPointF &p, Qt::KeyboardModifiers modifiers)
57{
59 QPointF point = d->snapGuide->snap(p, modifiers);
60 if (modifiers & Qt::ControlModifier) {
61 const QRectF oldDirtyRect = d->selectedRect();
62 d->selectRect.moveTopLeft(d->selectRect.topLeft() - (d->lastPos - point));
63 d->lastPos = point;
64 d->tool->canvas()->updateCanvas(oldDirtyRect | d->selectedRect());
65 return;
66 }
67 d->lastPos = point;
68 QPointF old = d->selectRect.bottomRight();
69 d->selectRect.setBottomRight(point);
70 /*
71 +---------------|--+
72 | | | We need to figure out rects A and B based on the two points. BUT
73 | old | A| we need to do that even if the points are switched places
74 | \ | | (i.e. the rect got smaller) and even if the rect is mirrored
75 +---------------+ | in either the horizontal or vertical axis.
76 | B |
77 +------------------+
78 `- point
79 */
80 QPointF x1 = old;
81 x1.setY(d->selectRect.topLeft().y());
82 qreal h1 = point.y() - x1.y();
83 qreal h2 = old.y() - x1.y();
84 QRectF A(x1, QSizeF(point.x() - x1.x(), point.y() < d->selectRect.top() ? qMin(h1, h2) : qMax(h1, h2)));
85 A = A.normalized();
86 d->tool->canvas()->updateCanvas(A);
87
88 QPointF x2 = old;
89 x2.setX(d->selectRect.topLeft().x());
90 qreal w1 = point.x() - x2.x();
91 qreal w2 = old.x() - x2.x();
92 QRectF B(x2, QSizeF(point.x() < d->selectRect.left() ? qMin(w1, w2) : qMax(w1, w2), point.y() - x2.y()));
93 B = B.normalized();
94 d->tool->canvas()->updateCanvas(B);
95}
96
98{
100 return d->selectRect.left() < d->selectRect.right() ? CoveringSelection : CrossingSelection;
101}
102
107
110 return d->selectedRect();
111}
const Params2D p
virtual SelectionMode currentMode() const
void handleMouseMove(const QPointF &mouseLocation, Qt::KeyboardModifiers modifiers) override
void paint(QPainter &painter, const KoViewConverter &converter) override
KoShapeRubberSelectStrategy(KoToolBase *tool, const QPointF &clicked, bool useSnapToGrid=false)
virtual QPointF documentToView(const QPointF &documentPoint) const