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
|
/*
* kis_tool_line.cc - part of Krayon
*
* Copyright (c) 2000 John Califf
* Copyright (c) 2002 Patrick Julien <freak@codepimps.org>
* Copyright (c) 2003 Boudewijn Rempt <boud@valdyas.org>
*
* 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 <tqpainter.h>
#include <tqlayout.h>
#include <tqwidget.h>
#include <kdebug.h>
#include <tdeaction.h>
#include <kcommand.h>
#include <tdelocale.h>
#include "kis_cursor.h"
#include "kis_painter.h"
#include "kis_tool_line.h"
#include "kis_button_press_event.h"
#include "kis_button_release_event.h"
#include "kis_move_event.h"
#include "kis_paintop_registry.h"
#include "kis_canvas_subject.h"
#include "kis_undo_adapter.h"
#include "kis_canvas.h"
#include "kis_canvas_painter.h"
#include "kis_cursor.h"
#include "kis_layer.h"
KisToolLine::KisToolLine()
: super(i18n("Line")),
m_dragging( false )
{
setName("tool_line");
setCursor(KisCursor::load("tool_line_cursor.png", 6, 6));
m_painter = 0;
m_currentImage = 0;
m_startPos = KisPoint(0, 0);
m_endPos = KisPoint(0, 0);
}
KisToolLine::~KisToolLine()
{
}
void KisToolLine::update(KisCanvasSubject *subject)
{
m_subject = subject;
m_currentImage = subject->currentImg();
super::update(m_subject);
}
void KisToolLine::paint(KisCanvasPainter& gc)
{
if (m_dragging)
paintLine(gc, TQRect());
}
void KisToolLine::paint(KisCanvasPainter& gc, const TQRect& rc)
{
if (m_dragging)
paintLine(gc, rc);
}
void KisToolLine::buttonPress(KisButtonPressEvent *e)
{
if (!m_subject || !m_currentImage) return;
if (!m_subject->currentBrush()) return;
if (e->button() == Qt::LeftButton) {
m_dragging = true;
//KisCanvasController *controller = m_subject->canvasController();
m_startPos = e->pos(); //controller->windowToView(e->pos());
m_endPos = e->pos(); //controller->windowToView(e->pos());
}
}
void KisToolLine::move(KisMoveEvent *e)
{
if (m_dragging) {
if (m_startPos != m_endPos)
paintLine();
//KisCanvasController *controller = m_subject->canvasController();
if (e->state() & TQt::AltButton) {
KisPoint trans = e->pos() - m_endPos;
m_startPos += trans;
m_endPos += trans;
} else if (e->state() & TQt::ShiftButton)
m_endPos = straightLine(e->pos());
else
m_endPos = e->pos();//controller->windowToView(e->pos());
paintLine();
}
}
void KisToolLine::buttonRelease(KisButtonReleaseEvent *e)
{
if (m_dragging && e->button() == Qt::LeftButton) {
m_dragging = false;
KisCanvasController *controller = m_subject->canvasController();
KisImageSP img = m_subject->currentImg();
if (m_startPos == m_endPos) {
controller->updateCanvas();
m_dragging = false;
return;
}
if ((e->state() & TQt::ShiftButton) == TQt::ShiftButton) {
m_endPos = straightLine(e->pos());
}
else {
m_endPos = e->pos();
}
KisPaintDeviceSP device;
if (m_currentImage &&
(device = m_currentImage->activeDevice()) &&
m_subject &&
m_subject->currentBrush())
{
delete m_painter;
m_painter = new KisPainter( device );
TQ_CHECK_PTR(m_painter);
if (m_currentImage->undo()) m_painter->beginTransaction(i18n("Line"));
m_painter->setPaintColor(m_subject->fgColor());
m_painter->setBrush(m_subject->currentBrush());
m_painter->setOpacity(m_opacity);
m_painter->setCompositeOp(m_compositeOp);
KisPaintOp * op = KisPaintOpRegistry::instance()->paintOp(m_subject->currentPaintop(), m_subject->currentPaintopSettings(), m_painter);
m_painter->setPaintOp(op); // Painter takes ownership
m_painter->paintLine(m_startPos, PRESSURE_DEFAULT, 0, 0, m_endPos, PRESSURE_DEFAULT, 0, 0);
device->setDirty( m_painter->dirtyRect() );
notifyModified();
/* remove remains of the line drawn while moving */
if (controller->kiscanvas()) {
controller->kiscanvas()->update();
}
if (m_currentImage->undo() && m_painter) {
m_currentImage->undoAdapter()->addCommand(m_painter->endTransaction());
}
delete m_painter;
m_painter = 0;
} else {
if (m_painter)
controller->updateCanvas(m_painter->dirtyRect()); // Removes the last remaining line.
}
}
}
KisPoint KisToolLine::straightLine(KisPoint point)
{
KisPoint comparison = point - m_startPos;
KisPoint result;
if ( fabs(comparison.x()) > fabs(comparison.y())) {
result.setX(point.x());
result.setY(m_startPos.y());
} else {
result.setX( m_startPos.x() );
result.setY( point.y() );
}
return result;
}
void KisToolLine::paintLine()
{
if (m_subject) {
KisCanvasController *controller = m_subject->canvasController();
KisCanvas *canvas = controller->kiscanvas();
KisCanvasPainter gc(canvas);
TQRect rc;
paintLine(gc, rc);
}
}
void KisToolLine::paintLine(KisCanvasPainter& gc, const TQRect&)
{
if (m_subject) {
KisCanvasController *controller = m_subject->canvasController();
RasterOp op = gc.rasterOp();
TQPen old = gc.pen();
TQPen pen(TQt::SolidLine);
KisPoint start;
KisPoint end;
// Q_ASSERT(controller);
start = controller->windowToView(m_startPos);
end = controller->windowToView(m_endPos);
// start.setX(start.x() - controller->horzValue());
// start.setY(start.y() - controller->vertValue());
// end.setX(end.x() - controller->horzValue());
// end.setY(end.y() - controller->vertValue());
// end.setX((end.x() - start.x()));
// end.setY((end.y() - start.y()));
// start *= m_subject->zoomFactor();
// end *= m_subject->zoomFactor();
gc.setRasterOp(TQt::NotROP);
gc.setPen(pen);
gc.drawLine(start.floorTQPoint(), end.floorTQPoint());
gc.setRasterOp(op);
gc.setPen(old);
}
}
void KisToolLine::setup(TDEActionCollection *collection)
{
m_action = static_cast<TDERadioAction *>(collection->action(name()));
if (m_action == 0) {
m_action = new TDERadioAction(i18n("&Line"),
"tool_line", TQt::Key_L, this,
TQT_SLOT(activate()), collection,
name());
m_action->setToolTip(i18n("Draw a line"));
m_action->setExclusiveGroup("tools");
m_ownAction = true;
}
}
TQString KisToolLine::quickHelp() const {
return i18n("Alt+Drag will move the origin of the currently displayed line around, Shift+Drag will force you to draw straight lines");
}
#include "kis_tool_line.moc"
|