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
|
/*
* kis_tool_ellipse.cc - part of Krayon
*
* Copyright (c) 2000 John Califf <[email protected]>
* Copyright (c) 2002 Patrick Julien <[email protected]>
* Copyright (c) 2004 Boudewijn Rempt <[email protected]>
* Copyright (c) 2004 Clarence Dang <[email protected]>
*
* 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 <tdeaction.h>
#include <kdebug.h>
#include <tdelocale.h>
#include "kis_painter.h"
#include "kis_canvas_subject.h"
#include "kis_canvas_controller.h"
#include "kis_tool_ellipse.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_undo_adapter.h"
#include "kis_canvas.h"
#include "kis_canvas_painter.h"
#include "kis_cursor.h"
KisToolEllipse::KisToolEllipse()
: super(i18n ("Ellipse")),
m_dragging (false),
m_currentImage (0)
{
setName("tool_ellipse");
setCursor(KisCursor::load("tool_ellipse_cursor.png", 6, 6));
}
KisToolEllipse::~KisToolEllipse()
{
}
void KisToolEllipse::update (KisCanvasSubject *subject)
{
super::update (subject);
if (m_subject)
m_currentImage = m_subject->currentImg ();
}
void KisToolEllipse::buttonPress(KisButtonPressEvent *event)
{
if (m_currentImage && event->button() == Qt::LeftButton) {
m_dragging = true;
m_dragStart = m_dragCenter = m_dragEnd = event->pos();
draw(m_dragStart, m_dragEnd);
}
}
void KisToolEllipse::move(KisMoveEvent *event)
{
if (m_dragging) {
// erase old lines on canvas
draw(m_dragStart, m_dragEnd);
// move (alt) or resize ellipse
if (event->state() & TQt::AltButton) {
KisPoint trans = event->pos() - m_dragEnd;
m_dragStart += trans;
m_dragEnd += trans;
} else {
KisPoint diag = event->pos() - (event->state() & TQt::ControlButton
? m_dragCenter : m_dragStart);
// circle?
if (event->state() & TQt::ShiftButton) {
double size = TQMAX(fabs(diag.x()), fabs(diag.y()));
double w = diag.x() < 0 ? -size : size;
double h = diag.y() < 0 ? -size : size;
diag = KisPoint(w, h);
}
// resize around center point?
if (event->state() & TQt::ControlButton) {
m_dragStart = m_dragCenter - diag;
m_dragEnd = m_dragCenter + diag;
} else {
m_dragEnd = m_dragStart + diag;
}
}
// draw new lines on canvas
draw(m_dragStart, m_dragEnd);
m_dragCenter = KisPoint((m_dragStart.x() + m_dragEnd.x()) / 2,
(m_dragStart.y() + m_dragEnd.y()) / 2);
}
}
void KisToolEllipse::buttonRelease(KisButtonReleaseEvent *event)
{
if (!m_subject || !m_currentImage)
return;
if (m_dragging && event->button() == Qt::LeftButton) {
// erase old lines on canvas
draw(m_dragStart, m_dragEnd);
m_dragging = false;
if (m_dragStart == m_dragEnd)
return;
if (!m_currentImage)
return;
if (!m_currentImage->activeDevice())
return;
KisPaintDeviceSP device = m_currentImage->activeDevice ();
KisPainter painter (device);
if (m_currentImage->undo()) painter.beginTransaction (i18n ("Ellipse"));
painter.setPaintColor(m_subject->fgColor());
painter.setBackgroundColor(m_subject->bgColor());
painter.setFillStyle(fillStyle());
painter.setBrush(m_subject->currentBrush());
painter.setPattern(m_subject->currentPattern());
painter.setOpacity(m_opacity);
painter.setCompositeOp(m_compositeOp);
KisPaintOp * op = KisPaintOpRegistry::instance()->paintOp(m_subject->currentPaintop(), m_subject->currentPaintopSettings(), &painter);
painter.setPaintOp(op); // Painter takes ownership
painter.paintEllipse(m_dragStart, m_dragEnd, PRESSURE_DEFAULT/*event->pressure()*/, event->xTilt(), event->yTilt());
device->setDirty( painter.dirtyRect() );
notifyModified();
KisUndoAdapter *adapter = m_currentImage->undoAdapter();
if (adapter) {
adapter->addCommand(painter.endTransaction());
}
}
}
void KisToolEllipse::draw(const KisPoint& start, const KisPoint& end )
{
if (!m_subject || !m_currentImage)
return;
KisCanvasController *controller = m_subject->canvasController ();
KisCanvas *canvas = controller->kiscanvas();
KisCanvasPainter p (canvas);
p.setRasterOp (TQt::NotROP);
p.drawEllipse (TQRect (controller->windowToView (start).floorTQPoint(), controller->windowToView (end).floorTQPoint()));
p.end ();
}
void KisToolEllipse::setup(TDEActionCollection *collection)
{
m_action = static_cast<TDERadioAction *>(collection->action(name()));
if (m_action == 0) {
TDEShortcut shortcut(TQt::Key_Plus);
shortcut.append(TDEShortcut(TQt::Key_F7));
m_action = new TDERadioAction(i18n("&Ellipse"),
"tool_ellipse",
shortcut,
this,
TQT_SLOT(activate()),
collection,
name());
m_action->setToolTip(i18n("Draw an ellipse"));
m_action->setExclusiveGroup("tools");
m_ownAction = true;
}
}
#include "kis_tool_ellipse.moc"
|