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

« back to all changes in this revision

Viewing changes to krita/plugins/tools/tool_polyline/kis_tool_polyline.cc

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2006-04-20 21:38:53 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060420213853-j5lxluqvymxt2zny
Tags: 1:1.5.0-0ubuntu2
UbuntuĀ uploadĀ 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  kis_tool_polyline.cc -- part of Krita
 
3
 *
 
4
 *  Copyright (c) 2004 Michael Thaler <michael.thaler@physik.tu-muenchen.de>
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; either version 2 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU General Public License
 
17
 *  along with this program; if not, write to the Free Software
 
18
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
19
 */
 
20
 
 
21
 
 
22
#include <math.h>
 
23
 
 
24
#include <qpainter.h>
 
25
#include <qspinbox.h>
 
26
 
 
27
#include <kaction.h>
 
28
#include <kdebug.h>
 
29
#include <klocale.h>
 
30
#include <kdebug.h>
 
31
#include <knuminput.h>
 
32
 
 
33
#include "kis_doc.h"
 
34
#include "kis_view.h"
 
35
#include "kis_painter.h"
 
36
#include "kis_canvas_subject.h"
 
37
#include "kis_canvas_controller.h"
 
38
#include "kis_button_press_event.h"
 
39
#include "kis_button_release_event.h"
 
40
#include "kis_move_event.h"
 
41
#include "kis_paintop_registry.h"
 
42
#include "kis_canvas.h"
 
43
#include "kis_canvas_painter.h"
 
44
#include "kis_cursor.h"
 
45
 
 
46
#include "kis_tool_polyline.h"
 
47
 
 
48
KisToolPolyline::KisToolPolyline()
 
49
        : super(i18n ("Polyline")),
 
50
          m_dragging (false),
 
51
          m_currentImage (0)
 
52
{
 
53
    setName("tool_polyline");
 
54
    setCursor(KisCursor::load("tool_polyline_cursor.png", 6, 6));
 
55
}
 
56
 
 
57
KisToolPolyline::~KisToolPolyline()
 
58
{
 
59
}
 
60
 
 
61
void KisToolPolyline::update (KisCanvasSubject *subject)
 
62
{
 
63
        super::update (subject);
 
64
        if (m_subject)
 
65
            m_currentImage = m_subject->currentImg ();
 
66
}
 
67
 
 
68
void KisToolPolyline::buttonPress(KisButtonPressEvent *event)
 
69
{
 
70
    if (m_currentImage) {
 
71
        if (event->button() == LeftButton && event->state() != Qt::ShiftButton ) {
 
72
 
 
73
            m_dragging = true;
 
74
 
 
75
            if (m_points.isEmpty())
 
76
            {
 
77
                m_dragStart = event->pos();
 
78
                m_dragEnd = event->pos();
 
79
                m_points.append(m_dragStart);
 
80
            } else {
 
81
                m_dragStart = m_dragEnd;
 
82
                m_dragEnd = event->pos();
 
83
                draw();
 
84
            }
 
85
        } else if (event->button() == LeftButton && event->state() == Qt::ShiftButton ) {
 
86
            finish();
 
87
        }
 
88
    }
 
89
}
 
90
 
 
91
void KisToolPolyline::deactivate()
 
92
{
 
93
    draw();
 
94
    m_points.clear();
 
95
    m_dragging = false;
 
96
}
 
97
 
 
98
void KisToolPolyline::finish()
 
99
{
 
100
    // erase old lines on canvas
 
101
    draw();
 
102
    m_dragging = false;
 
103
 
 
104
    KisPaintDeviceSP device = m_currentImage->activeDevice ();
 
105
    if (!device) return;
 
106
    
 
107
    KisPainter painter (device);
 
108
    if (m_currentImage->undo()) painter.beginTransaction (i18n ("Polyline"));
 
109
 
 
110
    painter.setPaintColor(m_subject->fgColor());
 
111
    painter.setBrush(m_subject->currentBrush());
 
112
    painter.setOpacity(m_opacity);
 
113
    painter.setCompositeOp(m_compositeOp);
 
114
    KisPaintOp * op = KisPaintOpRegistry::instance()->paintOp(m_subject->currentPaintop(), m_subject->currentPaintopSettings(), &painter);
 
115
    painter.setPaintOp(op); // Painter takes ownership
 
116
 
 
117
    KisPoint start,end;
 
118
    KisPointVector::iterator it;
 
119
    for( it = m_points.begin(); it != m_points.end(); ++it )
 
120
    {
 
121
        if( it == m_points.begin() )
 
122
        {
 
123
            start = (*it);
 
124
        } else {
 
125
            end = (*it);
 
126
            painter.paintLine(start, PRESSURE_DEFAULT, 0, 0, end, PRESSURE_DEFAULT, 0, 0);
 
127
            start = end;
 
128
        }
 
129
    }
 
130
    m_points.clear();
 
131
 
 
132
    device->setDirty( painter.dirtyRect() );
 
133
    notifyModified();
 
134
 
 
135
    if (m_currentImage->undo()) {
 
136
        m_currentImage->undoAdapter()->addCommand(painter.endTransaction());
 
137
    }
 
138
 
 
139
}
 
140
void KisToolPolyline::move(KisMoveEvent *event)
 
141
{
 
142
    if (m_dragging) {
 
143
        // erase old lines on canvas
 
144
        draw();
 
145
        // get current mouse position
 
146
        m_dragEnd = event->pos();
 
147
        // draw new lines on canvas
 
148
        draw();
 
149
    }
 
150
}
 
151
 
 
152
void KisToolPolyline::buttonRelease(KisButtonReleaseEvent *event)
 
153
{
 
154
        if (!m_subject || !m_currentImage)
 
155
            return;
 
156
 
 
157
        if (m_dragging && event->button() == LeftButton)  {
 
158
                m_dragging = false;
 
159
                m_points.append (m_dragEnd);
 
160
    }
 
161
 
 
162
    if (m_dragging && event->button() == RightButton) {
 
163
 
 
164
        }
 
165
}
 
166
 
 
167
 
 
168
void KisToolPolyline::doubleClick(KisDoubleClickEvent *)
 
169
{
 
170
    finish();
 
171
}
 
172
 
 
173
 
 
174
void KisToolPolyline::paint(KisCanvasPainter& gc)
 
175
{
 
176
    draw(gc);
 
177
}
 
178
 
 
179
void KisToolPolyline::paint(KisCanvasPainter& gc, const QRect&)
 
180
{
 
181
    draw(gc);
 
182
}
 
183
 
 
184
void KisToolPolyline::draw()
 
185
{
 
186
    if (m_subject) {
 
187
        KisCanvasController *controller = m_subject->canvasController();
 
188
        KisCanvas *canvas = controller->kiscanvas();
 
189
        KisCanvasPainter gc(canvas);
 
190
 
 
191
        draw(gc);
 
192
    }
 
193
}
 
194
 
 
195
void KisToolPolyline::draw(KisCanvasPainter& gc)
 
196
{
 
197
        if (!m_subject || !m_currentImage)
 
198
            return;
 
199
 
 
200
        QPen pen(Qt::white, 0, Qt::SolidLine);
 
201
 
 
202
    gc.setPen(pen);
 
203
        gc.setRasterOp(Qt::XorROP);
 
204
 
 
205
    KisCanvasController *controller = m_subject->canvasController();
 
206
    KisPoint start, end;
 
207
    QPoint startPos;
 
208
    QPoint endPos;
 
209
 
 
210
    if (m_dragging) {
 
211
        startPos = controller->windowToView(m_dragStart.floorQPoint());
 
212
        endPos = controller->windowToView(m_dragEnd.floorQPoint());
 
213
        gc.drawLine(startPos, endPos);
 
214
    } else {
 
215
        for (KisPointVector::iterator it = m_points.begin(); it != m_points.end(); ++it) {
 
216
 
 
217
            if (it == m_points.begin())
 
218
            {
 
219
                start = (*it);
 
220
            } else {
 
221
                end = (*it);
 
222
 
 
223
                startPos = controller->windowToView(start.floorQPoint());
 
224
                endPos = controller->windowToView(end.floorQPoint());
 
225
 
 
226
                gc.drawLine(startPos, endPos);
 
227
 
 
228
                start = end;
 
229
            }
 
230
        }
 
231
    }
 
232
}
 
233
 
 
234
void KisToolPolyline::setup(KActionCollection *collection)
 
235
{
 
236
        m_action = static_cast<KRadioAction *>(collection->action(name()));
 
237
 
 
238
    if (m_action == 0) {
 
239
        KShortcut shortcut(Qt::Key_Plus);
 
240
        shortcut.append(KShortcut(Qt::Key_F9));
 
241
        m_action = new KRadioAction(i18n("&Polyline"),
 
242
                        "polyline",
 
243
                        shortcut,
 
244
                        this,
 
245
                        SLOT(activate()),
 
246
                        collection,
 
247
                        name());
 
248
        Q_CHECK_PTR(m_action);
 
249
 
 
250
        m_action->setToolTip(i18n("Draw a polyline. Shift-mouseclick ends the polyline."));
 
251
        m_action->setExclusiveGroup("tools");
 
252
        m_ownAction = true;
 
253
        }
 
254
}
 
255
 
 
256
QString KisToolPolyline::quickHelp() const
 
257
{
 
258
    return i18n("Press shift-mouseclick to end the polyline.");
 
259
}
 
260
 
 
261
#include "kis_tool_polyline.moc"