karbon

vtool.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 <tqevent.h>
21#include <tqlabel.h>
22
23#include <kdebug.h>
24#include <kiconloader.h>
25
26#include "karbon_view.h"
27#include "karbon_part.h"
28#include "vtoolcontroller.h"
29#include "KoContextCelp.h"
30#include "vtool.h"
31#include "vtool.moc"
32
33
34VTool::VTool( KarbonView *view, const char *name ) : TQObject( 0, name ), m_view( view )
35{
36 m_mouseButtonIsDown = false;
37 m_isDragging = false;
38 m_shiftPressed = false;
39 m_ctrlPressed = false;
40 m_altPressed = false;
41 m_action = 0;
42}
43
45{
46 if (toolController())
47 toolController()->unregisterTool( this );
48
49 delete m_action;
50 //kdDebug(38000) << "Deleting : " << name().latin1() << endl;
51}
52
53void
55{
56 toolController()->registerTool( tool );
57}
58
59VToolController *
61{
62 return m_view->toolController();
63}
64
65KarbonView *
67{
68 return m_view;
69}
70
71bool
72VTool::mouseEvent( TQMouseEvent* mouseEvent, const KoPoint &canvasCoordinate )
73{
74 if( !view() || !view()->part() || !view()->part()->isReadWrite() )
75 return false;
76
77 m_lastPoint.setX( canvasCoordinate.x() );
78 m_lastPoint.setY( canvasCoordinate.y() );
79
80 setCursor();
81
82 m_altPressed = mouseEvent->state() & TQt::AltButton;
83 m_shiftPressed = mouseEvent->state() & TQt::ShiftButton;
84 m_ctrlPressed = mouseEvent->state() & TQt::ControlButton;
85
86 // Mouse events:
87 if( mouseEvent->type() == TQEvent::MouseButtonDblClick )
88 {
90
91 return true;
92 }
93
94 if( mouseEvent->type() == TQEvent::MouseButtonPress )
95 {
96 m_firstPoint.setX( canvasCoordinate.x() );
97 m_firstPoint.setY( canvasCoordinate.y() );
98
99 if( mouseEvent->button() == TQt::RightButton )
101 else
103
104 m_mouseButtonIsDown = true;
105
106 return true;
107 }
108
109 if( mouseEvent->type() == TQEvent::MouseMove )
110 {
111 //setCursor();
112
113 if( m_mouseButtonIsDown )
114 {
115 mouseDrag();
116
117 m_isDragging = true;
118 }
119 else
120 mouseMove();
121
122 return true;
123 }
124
125 if( mouseEvent->type() == TQEvent::MouseButtonRelease )
126 {
127 if( m_isDragging )
128 {
130
131 m_isDragging = false;
132 }
133 else if( m_mouseButtonIsDown ) // False if canceled.
134 if( mouseEvent->button() == TQt::RightButton )
136 else
138
139 m_mouseButtonIsDown = false;
140
141 return true;
142 }
143
144 return false;
145}
146
147bool
148VTool::keyEvent( TQEvent* event )
149{
150 // Key press events.
151 if( event->type() == TQEvent::KeyPress )
152 {
153 TQKeyEvent* keyEvent = static_cast<TQKeyEvent*>( event );
154
155 // Terminate the current drawing with the Enter-key:
156 if(
157 ( keyEvent->key() == TQt::Key_Enter ||
158 keyEvent->key() == TQt::Key_Return )
159 && !m_isDragging )
160 {
161 accept();
162
163 return true;
164 }
165
166 // Terminate the current drawing with the Enter-key:
167 if( keyEvent->key() == TQt::Key_Backspace && !m_isDragging )
168 {
169 cancelStep();
170
171 return true;
172 }
173
174 // Cancel dragging with ESC-key:
175 if( keyEvent->key() == TQt::Key_Escape )
176 {
177 cancel();
178
179 m_isDragging = false;
180 m_mouseButtonIsDown = false;
181
182 return true;
183 }
184
185 // If SHIFT is pressed, some tools create a "square" object while dragging:
186 if( keyEvent->key() == TQt::Key_Shift )
187 {
188 m_shiftPressed = true;
189 if( m_isDragging )
190 {
192
193 return true;
194 }
195 }
196
197 // If Ctrl is pressed, some tools create a "centered" object while dragging:
198 if( keyEvent->key() == TQt::Key_Control )
199 {
200 m_ctrlPressed = true;
201 if( m_isDragging )
202 {
204
205 return true;
206 }
207 }
208
209 }
210
211 // Key release events:
212 if( event->type() == TQEvent::KeyRelease )
213 {
214 TQKeyEvent* keyEvent = static_cast<TQKeyEvent*>( event );
215
216 TQt::Key key = (TQt::Key)keyEvent->key();
217 if( key == TQt::Key_Shift )
218 {
219 m_shiftPressed = false;
220 if( m_isDragging )
221 {
223
224 return true;
225 }
226 }
227
228 if( key == TQt::Key_Control )
229 {
230 m_ctrlPressed = false;
231 if( m_isDragging )
232 {
234
235 return true;
236 }
237 }
238
239 if( key == TQt::Key_Left || key == TQt::Key_Right || key == TQt::Key_Up || key == TQt::Key_Down )
240 {
241 arrowKeyReleased( key );
242 return true;
243 }
244
245 return keyReleased( key );
246 }
247
248 return false;
249}
250
251void
253{
254 kdDebug() << k_funcinfo << endl;
255 refreshUnit();
256 TQPixmap Icon = BarIcon( icon() );
257 view()->contextHelpAction()->updateHelp( uiname(), contextHelp(), &Icon );
258 view()->statusMessage()->setText( statusText() );
259 toolController()->setCurrentTool( this );
260#if 0
261 if( toolController()->activeTool() )
262 {
263 toolController()->activeTool()->action()->setChecked( false );
264 toolController()->activeTool()->deactivate();
265 }
266
267 if( toolController()->activeTool() == this )
268 showDialog();
269 else
270 {
271 refreshUnit();
272 TQPixmap Icon = BarIcon( icon() );
273 view()->contextHelpAction()->updateHelp( uiname(), contextHelp(), &Icon );
274 view()->statusMessage()->setText( statusText() );
275 toolController()->activeTool()->action()->setChecked( true );
276 }
277#endif
278}
279
virtual void mouseDragRelease()
Mouse button release.
Definition vtool.h:220
virtual void mouseButtonPress()
Left mouse button press.
Definition vtool.h:185
virtual bool keyReleased(TQt::Key)
Specified key released.
Definition vtool.h:250
virtual void rightMouseButtonRelease()
Right mouse button release.
Definition vtool.h:200
virtual void mouseButtonDblClick()
Mouse button double click.
Definition vtool.h:205
virtual void mouseButtonRelease()
Left mouse button release.
Definition vtool.h:195
virtual ~VTool()
Destroys the tool and unregisters it from the connected part.
Definition vtool.cpp:44
virtual void arrowKeyReleased(TQt::Key)
"Arrow" key released up, down, left, right
Definition vtool.h:245
virtual void mouseDragCtrlReleased()
"Ctrl" key released while mouse drag.
Definition vtool.h:240
VToolController * toolController() const
Helper function.
Definition vtool.cpp:60
virtual TQString uiname()
The name of the tool.
Definition vtool.h:93
virtual void accept()
Terminates the current tool drawing (if any).
Definition vtool.h:265
virtual void cancel()
Cancels all tool operations.
Definition vtool.h:255
virtual TQString statusText()
The tool status text.
Definition vtool.h:108
virtual void registerTool(VTool *tool)
Registers the specified tool at the connected part.
Definition vtool.cpp:54
virtual void activate()
Called during the tool activation.
Definition vtool.cpp:252
virtual void mouseDragCtrlPressed()
Mouse drag with "Ctrl" key pressed at the same time.
Definition vtool.h:230
TQString icon()
The tool icon name.
Definition vtool.h:119
TDERadioAction * m_action
The tool's action object.
Definition vtool.h:309
virtual void mouseDragShiftPressed()
Mouse drag with "Shift" key pressed at the same time.
Definition vtool.h:225
virtual void refreshUnit()
This function is called when the documents unit setting were changed.
Definition vtool.h:138
VTool(KarbonView *view, const char *name)
Constructs a new tool connected to the specified karbon view.
Definition vtool.cpp:34
bool keyEvent(TQEvent *event)
This function processes every important key event.
Definition vtool.cpp:148
virtual void mouseDragShiftReleased()
"Shift" key released while mouse drag.
Definition vtool.h:235
bool mouseEvent(TQMouseEvent *event, const KoPoint &)
This function processes every important mouse event.
Definition vtool.cpp:72
virtual TQString contextHelp()
The context help of the tool.
Definition vtool.h:98
virtual void setCursor() const
This function is called on each mouse event the tool receives.
Definition vtool.h:180
virtual void mouseMove()
Mouse move.
Definition vtool.h:210
virtual void rightMouseButtonPress()
Right mouse button press.
Definition vtool.h:190
virtual void mouseDrag()
Mouse drag.
Definition vtool.h:215
KarbonView * view() const
Returns the connected karbon part.
Definition vtool.cpp:66
virtual bool showDialog() const
Shows the tools option dialog.
Definition vtool.h:88
virtual void cancelStep()
Cancels the last tool step (if any).
Definition vtool.h:260