karbon

vshapetool.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2001, 2002, 2003 The Karbon Developers
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include <tqcursor.h>
21#include <tqevent.h>
22#include <tqlabel.h>
23
24#include "karbon_part.h"
25#include "karbon_view.h"
26#include "vcanvas.h"
27#include "vcomposite.h"
28#include "vglobal.h"
29#include "vpainter.h"
30#include "vpainterfactory.h"
31#include "vshapecmd.h"
32#include "vshapetool.h"
33#include "vselection.h"
34#include "vcursor.h"
35
36VShapeTool::VShapeTool( KarbonView *view, const char *name, bool polar )
37 : VTool( view, name )
38{
39 m_cursor = new TQCursor( VCursor::createCursor( VCursor::CrossHair ) );
40
41 m_isPolar = polar;
42 m_isSquare = false;
43 m_isCentered = false;
44}
45
46VShapeTool::~VShapeTool()
47{
48 delete m_cursor;
49}
50
51TQString
52VShapeTool::contextHelp()
53{
54 TQString s = i18n( "<qt><b>Shape tool</b><br>" );
55 s += i18n( "<i>Click and drag</i> to place your own shape.<br>" );
56 s += i18n( "<i>Click</i> to place a shape using the tool properties values.</qt>" );
57 return s;
58}
59
60void
61VShapeTool::activate()
62{
64 view()->setCursor( *m_cursor );
65 view()->part()->document().selection()->showHandle( true );
66}
67
68TQString
69VShapeTool::statusText()
70{
71 return uiname();
72}
73
74void
76{
77 VPainter* painter = view()->painterFactory()->editpainter();
78 painter->setRasterOp( TQt::NotROP );
79
80 VPath* composite = shape();
81 composite->setState( VPath::edit );
82 composite->draw( painter, &composite->boundingBox() );
83 delete( composite );
84}
85
86void
87VShapeTool::mouseButtonPress()
88{
89 recalc();
90
91 // Draw new object:
92 draw();
93}
94
95void
96VShapeTool::mouseButtonRelease()
97{
98 draw();
99
100 recalc();
101
102 if( showDialog() )
103 {
104 VPath* composite = shape( true );
105
106 if( composite )
107 {
108 VShapeCmd* cmd = new VShapeCmd(
109 &view()->part()->document(),
110 uiname(), composite, icon() );
111
112 view()->part()->addCommand( cmd, true );
113 }
114 }
115
116 m_isSquare = false;
117 m_isCentered = false;
118}
119
120void
121VShapeTool::mouseDrag()
122{
123 // Erase old object:
124 draw();
125
126 recalc();
127
128 // Draw new object:
129 draw();
130}
131
132void
133VShapeTool::mouseDragRelease()
134{
135 //recalc();
136
137 VShapeCmd* cmd = new VShapeCmd(
138 &view()->part()->document(),
139 uiname(), shape(), icon() );
140
141 view()->part()->addCommand( cmd, true );
142
143 m_isSquare = false;
144 m_isCentered = false;
145}
146
147void
148VShapeTool::mouseDragShiftPressed()
149{
150 // Erase old object:
151 draw();
152
153 m_isSquare = true;
154 recalc();
155
156 // Draw new object:
157 draw();
158}
159
160void
161VShapeTool::mouseDragCtrlPressed()
162{
163 // Erase old object:
164 draw();
165
166 m_isCentered = true;
167 recalc();
168
169 // Draw new object:
170 draw();
171}
172
173void
174VShapeTool::mouseDragShiftReleased()
175{
176 // Erase old object:
177 draw();
178
179 m_isSquare = false;
180 recalc();
181
182 // Draw new object:
183 draw();
184}
185
186void
187VShapeTool::mouseDragCtrlReleased()
188{
189 // Erase old object:
190 draw();
191
192 m_isCentered = false;
193 recalc();
194
195 // Draw new object:
196 draw();
197}
198
199void
200VShapeTool::cancel()
201{
202 // Erase old object:
203 if ( isDragging() )
204 {
205 draw();
206 m_isSquare = false;
207 m_isCentered = false;
208 }
209}
210
211void
212VShapeTool::recalc()
213{
214 m_isSquare = shiftPressed();
215 m_isCentered = ctrlPressed();
216
217 KoPoint _first = view()->canvasWidget()->snapToGrid( first() );
218 KoPoint _last = view()->canvasWidget()->snapToGrid( last() );
219
220 // Calculate radius and angle:
221 if( m_isPolar )
222 {
223 // Radius:
224 m_d1 = sqrt(
225 ( _last.x() - _first.x() ) * ( _last.x() - _first.x() ) +
226 ( _last.y() - _first.y() ) * ( _last.y() - _first.y() ) );
227
228 // Angle:
229 m_d2 = atan2( _last.y() - _first.y(), _last.x() - _first.x() );
230
231 // Define pi/2 as "0.0":
232 m_d2 -= VGlobal::pi_2;
233
234 m_p = _first;
235 }
236 else
237 // Calculate width and height:
238 {
239 m_d1 = _last.x() - _first.x();
240 m_d2 = _last.y() - _first.y();
241
242 const int m_sign1 = VGlobal::sign( m_d1 );
243// TODO: revert when we introduce y-mirroring:
244 const int m_sign2 = VGlobal::sign( -m_d2 );
245
246 // Make unsigned:
247 if( m_d1 < 0.0 )
248 m_d1 = -m_d1;
249
250 if( m_d2 < 0.0 )
251 m_d2 = -m_d2;
252
253 if ( m_isSquare )
254 {
255 if ( m_d1 > m_d2 )
256 m_d2 = m_d1;
257 else
258 m_d1 = m_d2;
259 }
260
261 m_p.setX(
262 _first.x() - ( m_sign1 == -1 ? m_d1 : 0.0 ) );
263// TODO: revert when we introduce y-mirroring:
264 m_p.setY(
265 _first.y() + ( m_sign2 == -1 ? m_d2 : 0.0 ) );
266
267 if ( m_isCentered )
268 {
269 m_p.setX( m_p.x() - m_sign1 * tqRound( m_d1 * 0.5 ) );
270 m_p.setY( m_p.y() + m_sign2 * tqRound( m_d2 * 0.5 ) );
271 }
272 }
273}
274
@ CrossHair
unthemed crosshair cursor
Definition vcursor.h:38
static TQCursor createCursor(CursorType type)
Creates a predefined cursor of the specified type.
Definition vcursor.cpp:75
virtual void setState(const VState state)
Sets the state to a specified new state.
Definition vobject.h:163
@ edit
visible, active and is currently manipulated by a tool
Definition vobject.h:62
A composite path consists of one or many subpaths.
Definition vcomposite.h:48
virtual void draw(VPainter *painter, const KoRect *rect=0L) const
Draw the object to a painting device.
virtual const KoRect & boundingBox() const
Calculates the tightest bounding box around the object.
The base class for all karbon tools.
Definition vtool.h:52
virtual void draw(VPainter *painter)
Draws the actual tool state.
Definition vtool.h:158
virtual void activate()
Called during the tool activation.
Definition vtool.cpp:252