karbon

vrotatetool.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 <math.h>
21
22#include <tqcursor.h>
23#include <tqlabel.h>
24
25#include <tdelocale.h>
26#include <KoRect.h>
27
28#include <karbon_part.h>
29#include <karbon_view.h>
30#include <core/vglobal.h>
31#include "vrotatetool.h"
32#include <render/vpainter.h>
33#include <render/vpainterfactory.h>
34#include <commands/vtransformcmd.h>
35
36#include <kdebug.h>
37
38VRotateTool::VRotateTool( KarbonView *view )
39 : VTool( view, "tool_rotate" )
40{
41 m_objects.setAutoDelete( true );
42 registerTool( this );
43}
44
45VRotateTool::~VRotateTool()
46{
47 m_objects.clear();
48}
49
50void
52{
53 view()->setCursor( TQCursor( TQt::arrowCursor ) );
54 view()->part()->document().selection()->setState( VObject::selected );
55 view()->part()->document().selection()->showHandle( false );
57}
58
59TQString
61{
62 return i18n( "Rotate" );
63}
64
65void
66VRotateTool::draw()
67{
68 VPainter* painter = view()->painterFactory()->editpainter();
69 //painter->setZoomFactor( view()->zoom() );
70 painter->setRasterOp( TQt::NotROP );
71
72 VObjectListIterator itr = m_objects;
73 for( ; itr.current(); ++itr )
74 {
75 itr.current()->draw( painter, &itr.current()->boundingBox() );
76 }
77}
78
79void
81{
82 //view()->painterFactory()->painter()->end();
83
84 recalc();
85
86 // Draw new object:
87 draw();
88}
89
90void
92{
93 // Erase old object:
94 draw();
95
96 recalc();
97
98 // Draw new object:
99 draw();
100}
101
102void
104{
105 view()->part()->addCommand(
106 new VRotateCmd(
107 &view()->part()->document(),
108 m_center,
109 m_angle, altPressed() ),
110 true );
111}
112
113void
115{
116 // Erase old object:
117 if ( isDragging() )
118 {
119 draw();
120 view()->repaintAll( view()->part()->document().selection()->boundingBox() );
121 }
122}
123
124void
125VRotateTool::recalc()
126{
127 // Get center:
128 m_center = view()->part()->document().selection()->boundingBox().center();
129
130 // Calculate angle between vector (last - center) and (first - center):
131 m_angle = VGlobal::one_pi_180 * (
132 atan2(
133 last().y() - m_center.y(),
134 last().x() - m_center.x() )
135 -
136 atan2(
137 first().y() - m_center.y(),
138 first().x() - m_center.x() ) );
139
140 VRotateCmd cmd( 0L, m_center, m_angle );
141
142 // Copy selected objects and transform:
143 m_objects.clear();
144 VObject* copy;
145
146 VObjectListIterator itr = view()->part()->document().selection()->objects();
147 for ( ; itr.current() ; ++itr )
148 {
149 if( itr.current()->state() != VObject::deleted )
150 {
151 copy = itr.current()->clone();
152
153 cmd.visit( *copy );
154
155 copy->setState( VObject::edit );
156
157 m_objects.append( copy );
158 }
159 }
160}
161
162void
163VRotateTool::setup( TDEActionCollection *collection )
164{
165 m_action = static_cast<TDERadioAction *>(collection -> action( name() ) );
166
167 if( m_action == 0 )
168 {
169 m_action = new TDERadioAction( i18n( "Rotate Tool" ), "14_rotate", TQt::SHIFT+TQt::Key_H, this, TQ_SLOT( activate() ), collection, name() );
170 m_action->setToolTip( i18n( "Rotate" ) );
171 m_action->setExclusiveGroup( "manipulation" );
172 //m_ownAction = true;
173 }
174}
175
void addCommand(VCommand *cmd, bool repaint=false)
insert a command into the undo/redo-history:
VSelection * selection() const
Returns a pointer to the selection.
Definition vdocument.h:238
virtual VObject * clone() const =0
Create an exact copy of this object.
virtual void setState(const VState state)
Sets the state to a specified new state.
Definition vobject.h:163
@ selected
visible, active and can be manipulated by tools
Definition vobject.h:61
@ deleted
deleted, nearly dead
Definition vobject.h:58
@ edit
visible, active and is currently manipulated by a tool
Definition vobject.h:62
const VObjectList & objects() const
Read only access to the selected objects.
Definition vselection.h:136
void showHandle(bool handle=true)
Toggle selection handles on/off.
Definition vselection.h:166
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 mouseDragRelease()
Mouse button release.
Definition vtool.h:220
virtual void mouseButtonPress()
Left mouse button press.
Definition vtool.h:185
const KoPoint & last() const
The last mouse coordinate.
Definition vtool.h:285
const KoPoint & first() const
Most tools need to know the first mouse coordinate.
Definition vtool.h:280
bool altPressed() const
The status of the alt key.
Definition vtool.h:306
TDERadioAction * action() const
Returns the tool's associated action object.
Definition vtool.h:151
virtual void cancel()
Cancels all tool operations.
Definition vtool.h:255
virtual TQString statusText()
The tool status text.
Definition vtool.h:108
virtual void activate()
Called during the tool activation.
Definition vtool.cpp:252
TDERadioAction * m_action
The tool's action object.
Definition vtool.h:309
bool isDragging() const
Returns the current dagging state.
Definition vtool.h:173
virtual void mouseDrag()
Mouse drag.
Definition vtool.h:215
KarbonView * view() const
Returns the connected karbon part.
Definition vtool.cpp:66