File: kis_tool_select_brush.cc

package info (click to toggle)
calligra 1:2.8.5+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 397,516 kB
  • ctags: 185,064
  • sloc: cpp: 1,505,875; python: 45,987; ansic: 28,421; xml: 26,472; sh: 23,986; java: 18,214; objc: 3,965; makefile: 2,816; perl: 2,792; yacc: 2,601; lex: 1,410; sql: 903; ruby: 737; asm: 236; lisp: 121
file content (263 lines) | stat: -rw-r--r-- 7,796 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
 *  kis_tool_select_brush.cc -- part of Krita
 *
 *  Copyright (C) 2010 Celarek Adam <kdedev at xibo dot at>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "kis_tool_select_brush.h"

#include <QLabel>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPainter>
#include <QPainterPath>

#include "knuminput.h"

#include <KoCanvasBase.h>
#include <KoViewConverter.h>
#include <KoPointerEvent.h>
#include <KoColorSpace.h>
#include <KoCompositeOp.h>

#include <Eigen/Core>
using namespace Eigen;

#include "kis_cursor.h"
#include "kis_canvas2.h"
#include "kis_painter.h"
#include "kis_pixel_selection.h"
#include "kis_image.h"
#include "kis_selection_options.h"
#include "kis_selection_tool_helper.h"
#include "kis_paintop_preset.h"


KisToolSelectBrush::KisToolSelectBrush(KoCanvasBase * canvas)
        : KisToolSelectBase(canvas,
                            KisCursor::load("tool_brush_selection_cursor.png", 6, 6),
                            i18n("Brush Selection")),
        m_brushRadius(15),
        m_lastMousePosition(-1, -1)
{
    resetSelection();
}

KisToolSelectBrush::~KisToolSelectBrush()
{
}

QWidget* KisToolSelectBrush::createOptionWidget()
{
    KisToolSelectBase::createOptionWidget();
    KisSelectionOptions *selectionWidget = selectionOptionWidget();

    QHBoxLayout* fl = new QHBoxLayout();
    QLabel * lbl = new QLabel(i18n("Brush size:"), selectionWidget);
    fl->addWidget(lbl);

    KIntNumInput * input = new KIntNumInput(selectionWidget);
    input->setRange(0, 500, 5);
    input->setValue(m_brushRadius*2);
    fl->addWidget(input);
    connect(input, SIGNAL(valueChanged(int)), this, SLOT(slotSetBrushSize(int)));

    QVBoxLayout* l = dynamic_cast<QVBoxLayout*>(selectionWidget->layout());
    Q_ASSERT(l);
    l->insertLayout(1, fl);

    selectionWidget->disableSelectionModeOption();

    return selectionWidget;
}

void KisToolSelectBrush::paint(QPainter& gc, const KoViewConverter &converter)
{
    Q_UNUSED(converter);
    if(mode() == KisTool::PAINT_MODE) {
        paintToolOutline(&gc, pixelToView(m_selection));
    }
    else  if(m_lastMousePosition!=QPoint(-1, -1)) {
        QPainterPath brushOutline;
        brushOutline.addEllipse(m_lastMousePosition, m_brushRadius, m_brushRadius);
        paintToolOutline(&gc, pixelToView(brushOutline));
    }
}

void KisToolSelectBrush::beginPrimaryAction(KoPointerEvent *event)
{
    if (!selectionEditable()) {
        event->ignore();
        return;
    }

    setMode(KisTool::PAINT_MODE);

    m_lastPoint = convertToPixelCoord(event->point);
    addPoint(m_lastPoint);
}

void KisToolSelectBrush::continuePrimaryAction(KoPointerEvent *event)
{
    CHECK_MODE_SANITY_OR_RETURN(KisTool::PAINT_MODE);

    // this gives better performance
    if(Vector2f((m_lastPoint-convertToPixelCoord(event->point)).x(), (m_lastPoint-convertToPixelCoord(event->point)).y()).norm()<m_brushRadius/6)
        return;

    //randomise the point to workaround a bug in QPainterPath::operator|=()
    //FIXME: http://bugreports.qt.nokia.com/browse/QTBUG-8035
    //will be fixed in version 4.7.0
    qreal randomX=rand()%100;
    randomX/=1000.;
    qreal randomY=rand()%100;
    randomY/=1000.;
    QPointF smallRandomPoint(randomX, randomY);
    addPoint(convertToPixelCoord(event->point)+smallRandomPoint);
}

void KisToolSelectBrush::endPrimaryAction(KoPointerEvent *event)
{
    Q_UNUSED(event;)
    CHECK_MODE_SANITY_OR_RETURN(KisTool::PAINT_MODE);

    setMode(KisTool::HOVER_MODE);
    applyToSelection(m_selection);
}

void KisToolSelectBrush::mouseMoveEvent(KoPointerEvent *event)
{
    /**
     * Update outline
     */
    QRect brushRect(-m_brushRadius, -m_brushRadius, 2*m_brushRadius, 2*m_brushRadius);
    brushRect.adjust(-2, -2, 2, 2);     //width of tool outline

    brushRect.moveCenter(m_lastMousePosition);
    updateCanvasPixelRect(brushRect);

    m_lastMousePosition = convertToPixelCoord(event).toPoint();

    brushRect.moveCenter(m_lastMousePosition);
    updateCanvasPixelRect(brushRect);
}

void KisToolSelectBrush::deactivate()
{
    resetSelection();
    KisToolSelectBase::deactivate();
}

void KisToolSelectBrush::slotSetBrushSize(int size)
{
    m_brushRadius = ((qreal) size)/2.0;
}

void KisToolSelectBrush::applyToSelection(QPainterPath selection) {
    KisCanvas2 * kisCanvas = dynamic_cast<KisCanvas2*>(canvas());
    Q_ASSERT(kisCanvas);
    if (!kisCanvas)
        return;

    KisSelectionToolHelper helper(kisCanvas, i18n("Brush Selection"));

    if (selectionMode() == PIXEL_SELECTION) {

        KisPixelSelectionSP tmpSel = new KisPixelSelection();

        KisPainter painter(tmpSel);
        painter.setPaintColor(KoColor(Qt::black, tmpSel->colorSpace()));
        painter.setPaintOpPreset(currentPaintOpPreset(), currentImage());
        painter.setAntiAliasPolygonFill(selectionOptionWidget()->antiAliasSelection());
        painter.setFillStyle(KisPainter::FillStyleForegroundColor);
        painter.setStrokeStyle(KisPainter::StrokeStyleNone);

        selection.closeSubpath();
        painter.fillPainterPath(selection);
        tmpSel->setOutlineCache(selection);

        helper.selectPixelSelection(tmpSel, selectionAction());

        resetSelection();
    }
}

void KisToolSelectBrush::resetSelection()
{
    updateCanvasPixelRect(m_selection.boundingRect());
    m_selection = QPainterPath();
}

void KisToolSelectBrush::addPoint(const QPointF& point)
{
    QPainterPath ellipse;
    ellipse.addEllipse(point, m_brushRadius, m_brushRadius);

    m_selection |= (ellipse);
    addGap(m_lastPoint, point);

    updateCanvasPixelRect(QRectF(m_lastPoint, point).normalized().adjusted(-m_brushRadius, -m_brushRadius, m_brushRadius, m_brushRadius));

    m_lastPoint = point;
}


void KisToolSelectBrush::addGap(const QPointF& start, const QPointF& end)
{
    Vector2f way((end-start).x(), (end-start).y());
    if(way.norm() < m_brushRadius/3.)
        return;

    Vector2f direction(way.normalized());

    //rotate 90 degrees clockwise
    Vector2f rotatedPlus(direction.y(), -direction.x());

    //rotate 90 degrees counter clockwise
    Vector2f rotatedMinus(-direction.y(), direction.x());

    Vector2f p1(rotatedPlus * m_brushRadius);
    Vector2f p2(way+p1);
    Vector2f p4(rotatedMinus * m_brushRadius);
    Vector2f p3(way+p4);

    //we need to convert floating point vectors to int ones because of a bug in QPainterPath::operator|=()
    //FIXME: http://bugreports.qt.nokia.com/browse/QTBUG-8035
    //converting int to float should be done with rounding, so don't use eigen casting.
    //parameter start contains important decimal places, these shouldn't be lost.
    QPointF pp1 = QPointF(p1.x(), p1.y()).toPoint();
    QPointF pp2 = QPointF(p2.x(), p2.y()).toPoint();
    QPointF pp3 = QPointF(p3.x(), p3.y()).toPoint();
    QPointF pp4 = QPointF(p4.x(), p4.y()).toPoint();

    pp1+=start;
    pp2+=start;
    pp3+=start;
    pp4+=start;

    QPainterPath gap;
    gap.moveTo(pp1);
    gap.lineTo(pp2);
    gap.lineTo(pp3);
    gap.lineTo(pp4);
    gap.closeSubpath();

    m_selection |= (gap);
}


#include "kis_tool_select_brush.moc"