~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to krita/plugins/tools/defaulttools/kis_tool_ellipse.cc

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-09-21 15:36:35 UTC
  • mfrom: (1.4.1 upstream) (60.2.11 maverick)
  • Revision ID: james.westby@ubuntu.com-20100921153635-6tejqkiro2u21ydi
Tags: 1:2.2.2-0ubuntu3
Add kubuntu_03_fix-crash-on-closing-sqlite-connection-2.2.2.diff and
kubuntu_04_support-large-memo-values-for-msaccess-2.2.2.diff as
recommended by upstream http://kexi-
project.org/wiki/wikiview/index.php@Kexi2.2_Patches.html#sqlite_stab
ility

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
 *  Copyright (c) 2004 Boudewijn Rempt <boud@valdyas.org>
7
7
 *  Copyright (c) 2004 Clarence Dang <dang@kde.org>
8
8
 *  Copyright (c) 2009 Lukáš Tvrdý <lukast.dev@gmail.com>
 
9
 *  Copyright (c) 2010 Cyrille Berger <cberger@cberger.net>
9
10
 *
10
11
 *  This program is free software; you can redistribute it and/or modify
11
12
 *  it under the terms of the GNU General Public License as published by
23
24
 */
24
25
 
25
26
#include "kis_tool_ellipse.h"
26
 
 
27
 
 
28
 
#include <QPainter>
29
 
 
30
 
#include <kis_debug.h>
31
 
#include <klocale.h>
32
 
 
33
 
#include <KoPointerEvent.h>
34
27
#include <KoCanvasBase.h>
35
 
#include <KoCanvasController.h>
 
28
#include <KoShapeController.h>
36
29
 
37
30
#include <kis_selection.h>
38
 
#include <kis_painter.h>
39
 
#include <kis_paintop_registry.h>
40
 
#include <kis_cursor.h>
41
 
#include <kis_layer.h>
42
 
 
43
 
#include <config-opengl.h>
44
 
 
45
 
#ifdef HAVE_OPENGL
46
 
#include <GL/gl.h>
47
 
#endif
48
 
 
49
 
 
 
31
#include <kis_shape_tool_helper.h>
 
32
#include <kis_paint_device.h>
 
33
#include <kis_paintop_preset.h>
 
34
 
 
35
#include <recorder/kis_action_recorder.h>
 
36
#include <recorder/kis_recorded_shape_paint_action.h>
 
37
#include <recorder/kis_node_query_path.h>
50
38
 
51
39
KisToolEllipse::KisToolEllipse(KoCanvasBase * canvas)
52
 
        : KisToolShape(canvas, KisCursor::load("tool_ellipse_cursor.png", 6, 6)),
53
 
        m_dragging(false)
 
40
        : KisToolEllipseBase(canvas, KisCursor::load("tool_ellipse_cursor.png", 6, 6))
54
41
{
55
42
    setObjectName("tool_ellipse");
56
 
 
57
 
    m_painter = 0;
58
 
    currentImage() = 0;
59
 
    m_dragStart = QPointF(0, 0);
60
 
    m_dragEnd = QPointF(0, 0);
61
43
}
62
44
 
63
45
KisToolEllipse::~KisToolEllipse()
64
46
{
65
47
}
66
48
 
67
 
void KisToolEllipse::paint(QPainter& gc, const KoViewConverter &converter)
68
 
{
69
 
    Q_ASSERT(currentImage());
70
 
    if (m_dragging)
71
 
        paintEllipse(gc, QRect());
72
 
}
73
 
 
74
 
 
75
 
void KisToolEllipse::mousePressEvent(KoPointerEvent *event)
76
 
{
77
 
    if (!m_canvas || !currentImage()) return;
78
 
 
79
 
    if (event->button() == Qt::LeftButton) {
80
 
        QPointF pos = convertToPixelCoord(event);
81
 
        m_dragging = true;
82
 
        m_dragStart = m_dragCenter = m_dragEnd = pos;
83
 
        //draw(m_dragStart, m_dragEnd);
84
 
    }
85
 
}
86
 
 
87
 
void KisToolEllipse::mouseMoveEvent(KoPointerEvent *event)
88
 
{
89
 
    if (m_dragging) {
90
 
        QRectF bound;
91
 
 
92
 
        bound.setTopLeft(m_dragStart);
93
 
        bound.setBottomRight(m_dragEnd);
94
 
        m_canvas->updateCanvas(convertToPt(bound.normalized()));
95
 
 
96
 
        QPointF pos = convertToPixelCoord(event);
97
 
        // erase old lines on canvas
98
 
        //draw(m_dragStart, m_dragEnd);
99
 
        // move (alt) or resize ellipse
100
 
        if (event->modifiers() & Qt::AltModifier) {
101
 
            QPointF trans = pos - m_dragEnd;
102
 
            m_dragStart += trans;
103
 
            m_dragEnd += trans;
104
 
        } else {
105
 
            QPointF diag = pos - (event->modifiers() & Qt::ControlModifier
106
 
                                  ? m_dragCenter : m_dragStart);
107
 
            // circle?
108
 
            if (event->modifiers() & Qt::ShiftModifier) {
109
 
                double size = qMax(fabs(diag.x()), fabs(diag.y()));
110
 
                double w = diag.x() < 0 ? -size : size;
111
 
                double h = diag.y() < 0 ? -size : size;
112
 
                diag = QPointF(w, h);
113
 
            }
114
 
 
115
 
            // resize around center point?
116
 
            if (event->modifiers() & Qt::ControlModifier) {
117
 
                m_dragStart = m_dragCenter - diag;
118
 
                m_dragEnd = m_dragCenter + diag;
119
 
            } else {
120
 
                m_dragEnd = m_dragStart + diag;
121
 
            }
122
 
        }
123
 
        // draw new lines on canvas
124
 
        //draw(m_dragStart, m_dragEnd);
125
 
        bound.setTopLeft(m_dragStart);
126
 
        bound.setBottomRight(m_dragEnd);
127
 
 
128
 
        m_canvas->updateCanvas(convertToPt(bound.normalized()));
129
 
 
130
 
        m_dragCenter = QPointF((m_dragStart.x() + m_dragEnd.x()) / 2,
131
 
                               (m_dragStart.y() + m_dragEnd.y()) / 2);
132
 
    }
133
 
}
134
 
 
135
 
void KisToolEllipse::mouseReleaseEvent(KoPointerEvent *event)
136
 
{
137
 
    QPointF pos = convertToPixelCoord(event);
138
 
 
139
 
    if (!m_canvas || !currentImage())
 
49
void KisToolEllipse::finishEllipse(const QRectF& rect)
 
50
{
 
51
    if (rect.isEmpty())
140
52
        return;
141
53
 
142
 
    if (m_dragging && event->button() == Qt::LeftButton) {
143
 
        // erase old lines on canvas
144
 
        //draw(m_dragStart, m_dragEnd);
145
 
        m_dragging = false;
146
 
 
147
 
        if (m_dragStart == m_dragEnd)
148
 
            return;
149
 
 
150
 
        if (!currentImage())
151
 
            return;
152
 
 
153
 
        if (!currentNode())
154
 
            return;
155
 
 
 
54
    if (image()) {
 
55
        KisRecordedShapePaintAction linePaintAction(KisNodeQueryPath::absolutePath(currentNode()), currentPaintOpPreset(), KisRecordedShapePaintAction::Ellipse, rect);
 
56
        setupPaintAction(&linePaintAction);
 
57
        image()->actionRecorder()->addAction(linePaintAction);
 
58
    }
 
59
 
 
60
    if (!currentNode()->inherits("KisShapeLayer")) {
156
61
        if (!currentNode()->paintDevice())
157
62
            return;
158
63
 
159
64
        KisPaintDeviceSP device = currentNode()->paintDevice();
160
 
        delete m_painter;
161
 
        m_painter = new KisPainter(device, currentSelection());
162
 
        Q_CHECK_PTR(m_painter);
163
 
 
164
 
        m_painter->beginTransaction(i18n("Ellipse"));
165
 
        setupPainter(m_painter);
166
 
        m_painter->setOpacity(m_opacity);
167
 
        m_painter->setCompositeOp(m_compositeOp);
168
 
 
169
 
        m_painter->paintEllipse(QRectF(m_dragStart, m_dragEnd));
170
 
        QRegion bound = m_painter->dirtyRegion();
 
65
 
 
66
        KisPainter painter(device, currentSelection());
 
67
 
 
68
        painter.beginTransaction(i18n("Ellipse"));
 
69
        setupPainter(&painter);
 
70
        painter.setOpacity(m_opacity);
 
71
        painter.setCompositeOp(m_compositeOp);
 
72
 
 
73
        painter.paintEllipse(rect);
 
74
        QRegion bound = painter.dirtyRegion();
171
75
        device->setDirty(bound);
172
76
        notifyModified();
173
77
 
174
 
        m_canvas->addCommand(m_painter->endTransaction());
175
 
 
176
 
        delete m_painter;
177
 
        m_painter = 0;
 
78
        canvas()->addCommand(painter.endTransaction());
178
79
    } else {
179
 
        KisToolPaint::mouseReleaseEvent(event);
 
80
        QRectF r = convertToPt(rect);
 
81
        KoShape* shape = KisShapeToolHelper::createEllipseShape(r);
 
82
 
 
83
        QUndoCommand * cmd = canvas()->shapeController()->addShape(shape);
 
84
        canvas()->addCommand(cmd);
180
85
    }
181
86
}
182
87
 
183
 
 
184
 
void KisToolEllipse::paintEllipse(QPainter& gc, const QRect&)
185
 
{
186
 
    QPointF viewDragStart = pixelToView(m_dragStart);
187
 
    QPointF viewDragEnd = pixelToView(m_dragEnd);
188
 
 
189
 
#if defined(HAVE_OPENGL)
190
 
    if (m_canvas->canvasController()->isCanvasOpenGL()) {
191
 
        glEnable(GL_LINE_SMOOTH);
192
 
        glEnable(GL_COLOR_LOGIC_OP);
193
 
        glLogicOp(GL_XOR);
194
 
        glColor3f(0.501961, 1.0, 0.501961);
195
 
 
196
 
        int steps = 72;
197
 
        qreal x = (viewDragEnd.x() - viewDragStart.x()) * 0.5;
198
 
        qreal a = qAbs(x);
199
 
        qreal y = (viewDragEnd.y() - viewDragStart.y()) * 0.5;
200
 
        qreal b = qAbs(y);
201
 
 
202
 
        x += viewDragStart.x();
203
 
        y += viewDragStart.y();
204
 
 
205
 
// useful for debugging
206
 
#if 0
207
 
        glPointSize(20);
208
 
        glBegin(GL_POINTS);
209
 
        glVertex2d(x, y);
210
 
        glEnd();
211
 
 
212
 
        glBegin(GL_LINES);
213
 
        glVertex2d(viewDragStart.x(), viewDragStart.y());
214
 
        glVertex2d(viewDragEnd.x(), viewDragEnd.y());
215
 
 
216
 
        glVertex2d(x, y);
217
 
        glVertex2d(x + a, y);
218
 
 
219
 
        glVertex2d(x, y);
220
 
        glVertex2d(x, y + b);
221
 
 
222
 
        glEnd();
223
 
#endif
224
 
 
225
 
        qreal angle = 0;
226
 
        qreal beta = -angle;
227
 
        qreal sinbeta = sin(beta);
228
 
        qreal cosbeta = cos(beta);
229
 
 
230
 
        glBegin(GL_LINE_LOOP);
231
 
        for (int i = 0; i < 360; i += 360.0 / steps) {
232
 
            qreal alpha = i * (M_PI / 180) ;
233
 
            qreal sinalpha = sin(alpha);
234
 
            qreal cosalpha = cos(alpha);
235
 
 
236
 
            qreal X = x + (a * cosalpha * cosbeta - b * sinalpha * sinbeta);
237
 
            qreal Y = y + (a * cosalpha * sinbeta + b * sinalpha * cosbeta);
238
 
 
239
 
            glVertex2d(X, Y);
240
 
        }
241
 
        glEnd();
242
 
 
243
 
 
244
 
        glDisable(GL_COLOR_LOGIC_OP);
245
 
        glDisable(GL_LINE_SMOOTH);
246
 
 
247
 
    } else
248
 
#endif
249
 
 
250
 
        if (m_canvas) {
251
 
#ifdef INDEPENDENT_CANVAS
252
 
            QPainterPath path;
253
 
            path.addEllipse(QRectF(viewDragStart, viewDragEnd));
254
 
            paintToolOutline(&gc, path);
255
 
#else
256
 
            QPen old = gc.pen();
257
 
            QPen pen(Qt::SolidLine);
258
 
            gc.setPen(pen);
259
 
            gc.drawEllipse(QRectF(viewDragStart, viewDragEnd));
260
 
            gc.setPen(old);
261
 
#endif
262
 
        }
263
 
}
264
 
 
265
88
#include "kis_tool_ellipse.moc"