Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_selection_decoration.cc
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2008 Sven Langkamp <sven.langkamp@gmail.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
9
10#include <QPainter>
11#include <QVarLengthArray>
12#include <QApplication>
13#include <QMainWindow>
14#include <QWindow>
15#include <QScreen>
16
17#include <kis_debug.h>
18#include <klocalizedstring.h>
19
20#include <KisMainWindow.h>
21#include "kis_types.h"
22#include "KisViewManager.h"
23#include "kis_selection.h"
24#include "kis_image.h"
26#include "kis_pixel_selection.h"
29#include "canvas/kis_canvas2.h"
32#include "kis_config.h"
33#include "kis_config_notifier.h"
34#include "kis_image_config.h"
36#include "kis_painting_tweaks.h"
37#include "KisView.h"
38#include "kis_selection_mask.h"
39#include <KisPart.h>
42
43static const unsigned int ANT_LENGTH = 4;
44static const unsigned int ANT_SPACE = 4;
45static const unsigned int ANT_ADVANCE_WIDTH = ANT_LENGTH + ANT_SPACE;
46
48 : KisCanvasDecoration("selection", _view),
49 m_signalCompressor(50 /*ms*/, KisSignalCompressor::FIRST_ACTIVE),
50 m_offset(0),
51 m_mode(Ants)
52{
54 connect(this->view()->canvasBase()->resourceManager(), SIGNAL(canvasResourceChanged(int, const QVariant&)), this, SLOT(slotCanvasResourcesChanged(int, const QVariant&)));
55
56 connect(KisConfigNotifier::instance(), SIGNAL(configChanged()), SLOT(slotConfigChanged()));
57 connect(KisImageConfigNotifier::instance(), SIGNAL(configChanged()), SLOT(slotConfigChanged()));
58
59 m_antsTimer = new QTimer(this);
60 m_antsTimer->setInterval(150);
61 m_antsTimer->setSingleShot(false);
62 connect(m_antsTimer, SIGNAL(timeout()), SLOT(antsAttackEvent()));
63
64 connect(&m_signalCompressor, SIGNAL(timeout()), SLOT(slotStartUpdateSelection()));
65
66 // selections should be at the top of the stack
67 setPriority(100);
68
69 m_selectionActionsPanel = new KisSelectionActionsPanel(this->view()->viewManager());
70
72}
73
77
82
88
90{
91 KisSelectionSP selection = view()->selection();
92 return visible() && selection &&
93 (selection->hasNonEmptyPixelSelection() || selection->hasNonEmptyShapeSelection()) &&
94 selection->isVisible();
95}
96
98{
99 QColor white(Qt::white);
100 QColor black(Qt::black);
101
104 black, white);
105
106 m_antsPen.setWidth(decorationThickness());
108}
109
111{
112 KisSelectionMaskSP mask = qobject_cast<KisSelectionMask*>(view()->currentNode().data());
113 if (!mask || !mask->active() || !mask->visible(true)) {
114 mask = 0;
115 }
116
117 if (!view()->isCurrent() ||
118 view()->viewManager()->mainWindow() == KisPart::instance()->currentMainwindow()) {
119
120 view()->image()->setOverlaySelectionMask(mask);
121 }
122
123 KisSelectionSP selection = view()->selection();
124
125 if (!mask && selectionIsActive()) {
126 if ((m_mode == Ants && selection->outlineCacheValid()) ||
127 (m_mode == Mask && selection->thumbnailImageValid())) {
128
130
131 if (m_mode == Ants) {
132 m_outlinePath = selection->outlineCache();
133 m_antsTimer->start();
134 } else {
135 m_thumbnailImage = selection->thumbnailImage();
137 m_antsTimer->stop();
138 }
139
140 if (view() && view()->canvasBase()) {
141 view()->canvasBase()->updateCanvas();
142 }
143
144
146 } else {
148 }
149 } else {
151 m_outlinePath = QPainterPath();
152 m_thumbnailImage = QImage();
153 m_thumbnailImageTransform = QTransform();
154 view()->canvasBase()->updateCanvas();
155 m_antsTimer->stop();
156 }
157
158 if (!selection && !selectionIsActive()) {
160 }
161}
162
164{
165 KisSelectionSP selection = view()->selection();
166 if (!selection) return;
167
168 view()->image()->addSpontaneousJob(new KisUpdateOutlineJob(selection, m_mode == Mask, m_maskColor));
169}
170
181
183{
184 Q_UNUSED(v);
187 }
188}
189
191{
192 KisSelectionSP selection = view()->selection();
193 if (!selection) return;
194
195 if (selectionIsActive()) {
197 m_antsPen.setDashOffset(m_offset);
198 view()->canvasBase()->updateCanvas();
199 }
200}
201
205
206void KisSelectionDecoration::drawDecoration(QPainter& gc, const QRectF& updateRect, const KisCoordinatesConverter *converter, KisCanvas2 *canvas)
207{
208 Q_UNUSED(updateRect);
209 Q_UNUSED(canvas);
210
211 // render Selection Action Bar first, so that it doesn't blink when making a new selection
212
213 if ((m_mode == Ants && m_outlinePath.isEmpty()) || (m_mode == Mask && m_thumbnailImage.isNull())
215 //The SAP needs to be drawn on top of the decoration, but to avoid the panel flashing when making a new selection, we also need to call draw here
217
218 return;
219 }
220
222 return;
223 }
224
225 QTransform transform = converter->imageToWidgetTransform();
226
227 gc.save();
228 gc.setTransform(transform, false);
229
230 if (m_mode == Mask) {
231 gc.setRenderHints(QPainter::SmoothPixmapTransform |
232 QPainter::Antialiasing, false);
233
234 gc.setTransform(m_thumbnailImageTransform, true);
235 gc.drawImage(QPoint(), m_thumbnailImage);
236
237 QRect r1 = m_thumbnailImageTransform.inverted().mapRect(view()->image()->bounds());
238 QRect r2 = m_thumbnailImage.rect();
239
240 QPainterPath p1;
241 p1.addRect(r1);
242
243 QPainterPath p2;
244 p2.addRect(r2);
245
246 KoColor c;
249 gc.setPen(Qt::NoPen);
250 gc.drawPath(p1 - p2);
251
252 } else /* if (m_mode == Ants) */ {
253
254 KoColor c;
255 c.fromQColor(Qt::white);
257 c.fromQColor(Qt::black);
259
260 gc.setRenderHints(QPainter::Antialiasing | QPainter::Antialiasing, m_antialiasSelectionOutline);
261
262 gc.setOpacity(m_opacity);
263
264 // render selection outline in white
265 gc.setPen(m_outlinePen);
266 gc.drawPath(m_outlinePath);
267
268 // render marching ants in black (above the white outline)
269 gc.setPen(m_antsPen);
270 gc.drawPath(m_outlinePath);
271 }
272
273 gc.restore();
275}
276
281
289
291{
292 if(minimized) {
293 m_antsTimer->stop();
294 } else {
296 }
297}
QPointF r2
qreal v
QPointF r1
QPointF p2
QPointF p1
KoColorDisplayRendererInterface * displayRendererInterface() const override
displayRendererInterface The display renderer interface has a number of color conversion functions wh...
KisDisplayColorConverter displayColorConverter
QPointer< KisView > view() const
virtual void setVisible(bool v)
static KisConfigNotifier * instance()
bool antialiasSelectionOutline(bool defaultValue=false) const
bool selectionActionBar(bool defaultValue=false) const
QColor convertColorToDisplayColorSpace(const KoColor color, bool applyOcio=false) const
convertColorToDisplayColorSpace This applies displayfiltering to the given KoColor,...
static KisImageConfigNotifier * instance()
qreal selectionOutlineOpacity(bool defaultValue=false) const
QColor selectionOverlayMaskColor(bool defaultValue=false) const
static KisPart * instance()
Definition KisPart.cpp:131
void canvasWidgetChanged(KisCanvasWidgetBase *canvas)
void draw(QPainter &painter, const KoColorDisplayRendererInterface *displayRendererInterface)
void notifyWindowMinimized(bool minimized) override
void setCanvasWidget(KisCanvasWidgetBase *canvas) override
void slotCanvasResourcesChanged(int key, const QVariant &v)
void drawDecoration(QPainter &gc, const QRectF &updateRect, const KisCoordinatesConverter *converter, KisCanvas2 *canvas) override
KisSignalCompressor m_signalCompressor
KisSelectionDecoration(QPointer< KisView > view)
KisSelectionActionsPanel * m_selectionActionsPanel
void fromQColor(const QColor &c)
Convenient function for converting from a QColor.
Definition KoColor.cpp:213
#define bounds(x, a, b)
static const unsigned int ANT_LENGTH
static const unsigned int ANT_ADVANCE_WIDTH
static const unsigned int ANT_SPACE
void initAntsPen(QPen *antsPen, QPen *outlinePen, int antLength, int antSpace, QColor black, QColor white)
@ DecorationThickness
Integer, the thickness of single px decorations, will be adjusted by HiDPI settings....
virtual bool visible(bool recursive=false) const
bool hasNonEmptyPixelSelection() const
bool thumbnailImageValid() const
QTransform thumbnailImageTransform() const
bool outlineCacheValid() const
QImage thumbnailImage() const
bool hasNonEmptyShapeSelection() const
QPainterPath outlineCache() const