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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
/*
* kis_tool_select_freehand.h - part of Krayon^WChalk
*
* Copyright (c) 2000 John Califf <jcaliff@compuzone.net>
* Copyright (c) 2002 Patrick Julien <freak@codepimps.org>
* Copyright (c) 2004 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 <tqapplication.h>
#include <tqpainter.h>
#include <tqregion.h>
#include <tqwidget.h>
#include <tqlayout.h>
#include <tdeaction.h>
#include <kdebug.h>
#include <kcommand.h>
#include <tdelocale.h>
#include <kis_layer.h>
#include <kis_selection_options.h>
#include <kis_canvas_controller.h>
#include <kis_canvas_subject.h>
#include <kis_cursor.h>
#include <kis_image.h>
#include <kis_tool_select_outline.h>
#include <kis_vec.h>
#include <kis_undo_adapter.h>
#include <kis_button_press_event.h>
#include <kis_button_release_event.h>
#include <kis_move_event.h>
#include "kis_selected_transaction.h"
#include "kis_painter.h"
#include "kis_paintop_registry.h"
#include "kis_canvas.h"
#include "kis_canvas_painter.h"
KisToolSelectOutline::KisToolSelectOutline()
: super(i18n("Select Outline"))
{
setName("tool_select_outline");
setCursor(KisCursor::load("tool_outline_selection_cursor.png", 5, 5));
m_subject = 0;
m_dragging = false;
m_optWidget = 0;
m_selectAction = SELECTION_ADD;
}
KisToolSelectOutline::~KisToolSelectOutline()
{
}
void KisToolSelectOutline::activate()
{
super::activate();
if (!m_optWidget)
return;
m_optWidget->slotActivated();
}
void KisToolSelectOutline::update (KisCanvasSubject *subject)
{
m_subject = subject;
super::update(m_subject);
}
void KisToolSelectOutline::buttonPress(KisButtonPressEvent *event)
{
if (event->button() == Qt::LeftButton) {
m_dragging = true;
m_dragStart = event->pos();
m_dragEnd = event->pos();
m_points.clear();
m_points.append(m_dragStart);
}
}
void KisToolSelectOutline::move(KisMoveEvent *event)
{
if (m_dragging) {
m_dragStart = m_dragEnd;
m_dragEnd = event->pos();
m_points.append (m_dragEnd);
// draw new lines on canvas
draw();
}
}
void KisToolSelectOutline::buttonRelease(KisButtonReleaseEvent *event)
{
if (!m_subject)
return;
if (m_dragging && event->button() == Qt::LeftButton) {
m_dragging = false;
deactivate();
KisImageSP img = m_subject->currentImg();
if (img && img->activeDevice()) {
TQApplication::setOverrideCursor(KisCursor::waitCursor());
KisPaintDeviceSP dev = img->activeDevice();
bool hasSelection = dev->hasSelection();
KisSelectedTransaction *t = 0;
if (img->undo()) t = new KisSelectedTransaction(i18n("Outline Selection"), dev);
KisSelectionSP selection = dev->selection();
if (!hasSelection) {
selection->clear();
}
KisPainter painter(selection.data());
painter.setPaintColor(KisColor(TQt::black, selection->colorSpace()));
painter.setFillStyle(KisPainter::FillStyleForegroundColor);
painter.setStrokeStyle(KisPainter::StrokeStyleNone);
painter.setBrush(m_subject->currentBrush());
painter.setOpacity(OPACITY_OPAQUE);
KisPaintOp * op = KisPaintOpRegistry::instance()->paintOp("paintbrush", 0, &painter);
painter.setPaintOp(op); // And now the painter owns the op and will destroy it.
switch (m_selectAction) {
case SELECTION_ADD:
painter.setCompositeOp(COMPOSITE_OVER);
break;
case SELECTION_SUBTRACT:
painter.setCompositeOp(COMPOSITE_SUBTRACT);
break;
default:
break;
}
painter.paintPolygon(m_points);
if(hasSelection) {
TQRect dirty(painter.dirtyRect());
dev->setDirty(dirty);
dev->emitSelectionChanged(dirty);
} else {
dev->setDirty();
dev->emitSelectionChanged();
}
if (img->undo())
img->undoAdapter()->addCommand(t);
TQApplication::restoreOverrideCursor();
}
m_points.clear();
}
}
void KisToolSelectOutline::paint(KisCanvasPainter& gc)
{
draw(gc);
}
void KisToolSelectOutline::paint(KisCanvasPainter& gc, const TQRect&)
{
draw(gc);
}
void KisToolSelectOutline::draw()
{
if (m_subject) {
KisCanvasController *controller = m_subject->canvasController();
KisCanvas *canvas = controller->kiscanvas();
KisCanvasPainter gc(canvas);
draw(gc);
}
}
void KisToolSelectOutline::draw(KisCanvasPainter& gc)
{
if (!m_subject)
return;
if (m_dragging && !m_points.empty()) {
TQPen pen(TQt::white, 0, TQt::DotLine);
gc.setPen(pen);
gc.setRasterOp(TQt::XorROP);
KisCanvasController *controller = m_subject->canvasController();
KisPoint start, end;
TQPoint startPos;
TQPoint endPos;
startPos = controller->windowToView(m_dragStart.floorTQPoint());
endPos = controller->windowToView(m_dragEnd.floorTQPoint());
gc.drawLine(startPos, endPos);
}
}
void KisToolSelectOutline::deactivate()
{
if (m_subject) {
KisCanvasController *controller = m_subject->canvasController();
KisCanvas *canvas = controller->kiscanvas();
KisCanvasPainter gc(canvas);
TQPen pen(TQt::white, 0, TQt::DotLine);
gc.setPen(pen);
gc.setRasterOp(TQt::XorROP);
KisPoint start, end;
TQPoint startPos;
TQPoint endPos;
for (KisPointVector::iterator it = m_points.begin(); it != m_points.end(); ++it) {
if (it == m_points.begin())
{
start = (*it);
} else {
end = (*it);
startPos = controller->windowToView(start.floorTQPoint());
endPos = controller->windowToView(end.floorTQPoint());
gc.drawLine(startPos, endPos);
start = end;
}
}
}
}
void KisToolSelectOutline::setup(TDEActionCollection *collection)
{
m_action = static_cast<TDERadioAction *>(collection->action(name()));
if (m_action == 0) {
m_action = new TDERadioAction(i18n("&Outline Selection"),
"tool_outline_selection",
0,
this,
TQT_SLOT(activate()),
collection,
name());
TQ_CHECK_PTR(m_action);
m_action->setExclusiveGroup("tools");
m_action->setToolTip(i18n("Select an outline"));
m_ownAction = true;
}
}
TQWidget* KisToolSelectOutline::createOptionWidget(TQWidget* parent)
{
m_optWidget = new KisSelectionOptions(parent, m_subject);
TQ_CHECK_PTR(m_optWidget);
m_optWidget->setCaption(i18n("Outline Selection"));
connect (m_optWidget, TQT_SIGNAL(actionChanged(int)), this, TQT_SLOT(slotSetAction(int)));
TQVBoxLayout * l = dynamic_cast<TQVBoxLayout*>(m_optWidget->layout());
l->addItem(new TQSpacerItem(1, 1, TQSizePolicy::Fixed, TQSizePolicy::Expanding));
return m_optWidget;
}
TQWidget* KisToolSelectOutline::optionWidget()
{
return m_optWidget;
}
void KisToolSelectOutline::slotSetAction(int action) {
if (action >= SELECTION_ADD && action <= SELECTION_SUBTRACT)
m_selectAction =(enumSelectionMode)action;
}
#include "kis_tool_select_outline.moc"
|