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#include <KoColor.h>
20
21KoShapeRubberSelectStrategy::KoShapeRubberSelectStrategy(KoToolBase *tool, const QPointF &clicked, bool useSnapToGrid)
23{
25 d->snapGuide->enableSnapStrategies(KoSnapGuide::GridSnapping);
26 d->snapGuide->enableSnapping(useSnapToGrid);
27
28 d->selectRect = QRectF(d->snapGuide->snap(clicked, QFlags<Qt::KeyboardModifier>()), QSizeF(0, 0));
29}
30
31void KoShapeRubberSelectStrategy::paint(QPainter &painter, const KoViewConverter &converter, const KoColorDisplayRendererInterface *displayRendererInterface)
32{
34 painter.setRenderHint(QPainter::Antialiasing, false);
35
36 KoColor c;
37 c.fromQColor(QColor(80,130,8));
38 const QColor crossingColor(displayRendererInterface->convertColorToDisplayColorSpace(c));
39 c.fromQColor(QColor(8,60,167));
40 const QColor coveringColor(displayRendererInterface->convertColorToDisplayColorSpace(c));
41
42 QColor selectColor(
44 crossingColor : coveringColor);
45
46 selectColor.setAlphaF(0.8);
47 QPen select(selectColor, decorationThickness());
48 select.setCosmetic(true);
49 painter.setPen(select);
50
51 selectColor.setAlphaF(0.4);
52 const QBrush fillBrush(selectColor);
53 painter.setBrush(fillBrush);
54
55 QRectF paintRect = converter.documentToView(d->selectedRect());
56 paintRect = paintRect.normalized();
57
58 painter.drawRect(paintRect);
59}
60
61void KoShapeRubberSelectStrategy::handleMouseMove(const QPointF &p, Qt::KeyboardModifiers modifiers)
62{
64 QPointF point = d->snapGuide->snap(p, modifiers);
65 if (modifiers & Qt::ControlModifier) {
66 const QRectF oldDirtyRect = d->selectedRect();
67 d->selectRect.moveTopLeft(d->selectRect.topLeft() - (d->lastPos - point));
68 d->lastPos = point;
69 d->tool->canvas()->updateCanvas(oldDirtyRect | d->selectedRect());
70 return;
71 }
72 d->lastPos = point;
73 QPointF old = d->selectRect.bottomRight();
74 d->selectRect.setBottomRight(point);
75 /*
76 +---------------|--+
77 | | | We need to figure out rects A and B based on the two points. BUT
78 | old | A| we need to do that even if the points are switched places
79 | \ | | (i.e. the rect got smaller) and even if the rect is mirrored
80 +---------------+ | in either the horizontal or vertical axis.
81 | B |
82 +------------------+
83 `- point
84 */
85 QPointF x1 = old;
86 x1.setY(d->selectRect.topLeft().y());
87 qreal h1 = point.y() - x1.y();
88 qreal h2 = old.y() - x1.y();
89 QRectF A(x1, QSizeF(point.x() - x1.x(), point.y() < d->selectRect.top() ? qMin(h1, h2) : qMax(h1, h2)));
90 A = A.normalized();
91 d->tool->canvas()->updateCanvas(A);
92
93 QPointF x2 = old;
94 x2.setX(d->selectRect.topLeft().x());
95 qreal w1 = point.x() - x2.x();
96 qreal w2 = old.x() - x2.x();
97 QRectF B(x2, QSizeF(point.x() < d->selectRect.left() ? qMin(w1, w2) : qMax(w1, w2), point.y() - x2.y()));
98 B = B.normalized();
99 d->tool->canvas()->updateCanvas(B);
100}
101
103{
105 return d->selectRect.left() < d->selectRect.right() ? CoveringSelection : CrossingSelection;
106}
107
112
115 return d->selectedRect();
116}
const Params2D p
virtual QColor convertColorToDisplayColorSpace(const KoColor color) const =0
convertColorToDisplayColorSpace
void fromQColor(const QColor &c)
Convenient function for converting from a QColor.
Definition KoColor.cpp:213
virtual SelectionMode currentMode() const
void handleMouseMove(const QPointF &mouseLocation, Qt::KeyboardModifiers modifiers) override
void paint(QPainter &painter, const KoViewConverter &converter, const KoColorDisplayRendererInterface *displayRendererInterface) override
KoShapeRubberSelectStrategy(KoToolBase *tool, const QPointF &clicked, bool useSnapToGrid=false)
virtual QPointF documentToView(const QPointF &documentPoint) const