karbon

vtool.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2001, 2002, 2003 The Karbon Developers
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include <qevent.h>
00021 #include <qlabel.h>
00022 
00023 #include <kdebug.h>
00024 #include <kiconloader.h>
00025 
00026 #include "karbon_view.h"
00027 #include "karbon_part.h"
00028 #include "vtoolcontroller.h"
00029 #include "KoContextCelp.h"
00030 #include "vtool.h"
00031 #include "vtool.moc"
00032 
00033 
00034 VTool::VTool( KarbonView *view, const char *name ) : QObject( 0, name ), m_view( view )
00035 {
00036     m_mouseButtonIsDown = false;
00037     m_isDragging        = false;
00038     m_shiftPressed      = false;
00039     m_ctrlPressed       = false;
00040     m_altPressed        = false;
00041     m_action = 0;
00042 }
00043 
00044 VTool::~VTool()
00045 {
00046     if (toolController())
00047         toolController()->unregisterTool( this );
00048 
00049     delete m_action;
00050     //kdDebug(38000) << "Deleting : " << name().latin1() << endl;
00051 }
00052 
00053 void
00054 VTool::registerTool( VTool *tool )
00055 {
00056     toolController()->registerTool( tool );
00057 }
00058 
00059 VToolController *
00060 VTool::toolController() const
00061 {
00062     return m_view->toolController();
00063 }
00064 
00065 KarbonView *
00066 VTool::view() const
00067 {
00068     return m_view;
00069 }
00070 
00071 bool
00072 VTool::mouseEvent( QMouseEvent* mouseEvent, const KoPoint &canvasCoordinate )
00073 {
00074     if( !view() || !view()->part() || !view()->part()->isReadWrite() )
00075         return false;
00076 
00077     m_lastPoint.setX( canvasCoordinate.x() );
00078     m_lastPoint.setY( canvasCoordinate.y() );
00079 
00080     setCursor();
00081 
00082     m_altPressed = mouseEvent->state() & Qt::AltButton;
00083     m_shiftPressed = mouseEvent->state() & Qt::ShiftButton;
00084     m_ctrlPressed = mouseEvent->state() & Qt::ControlButton;
00085 
00086     // Mouse events:
00087     if( mouseEvent->type() == QEvent::MouseButtonDblClick )
00088     {
00089         mouseButtonDblClick();
00090 
00091         return true;
00092     }
00093 
00094     if( mouseEvent->type() == QEvent::MouseButtonPress )
00095     {
00096         m_firstPoint.setX( canvasCoordinate.x() );
00097         m_firstPoint.setY( canvasCoordinate.y() );
00098 
00099         if( mouseEvent->button() == QEvent::RightButton )
00100             rightMouseButtonPress();
00101         else
00102             mouseButtonPress();
00103 
00104         m_mouseButtonIsDown = true;
00105 
00106         return true;
00107     }
00108 
00109     if( mouseEvent->type() == QEvent::MouseMove )
00110     {
00111         //setCursor();
00112 
00113         if( m_mouseButtonIsDown )
00114         {
00115             mouseDrag();
00116 
00117             m_isDragging = true;
00118         }
00119         else
00120             mouseMove();
00121 
00122         return true;
00123     }
00124 
00125     if( mouseEvent->type() == QEvent::MouseButtonRelease )
00126     {
00127         if( m_isDragging )
00128         {
00129             mouseDragRelease();
00130 
00131             m_isDragging = false;
00132         }
00133         else if( m_mouseButtonIsDown )  // False if canceled.
00134             if( mouseEvent->button() == QEvent::RightButton )
00135                 rightMouseButtonRelease();
00136             else
00137                 mouseButtonRelease();
00138 
00139         m_mouseButtonIsDown = false;
00140 
00141         return true;
00142     }
00143 
00144     return false;
00145 }
00146 
00147 bool
00148 VTool::keyEvent( QEvent* event )
00149 {
00150     // Key press events.
00151     if( event->type() == QEvent::KeyPress )
00152     {
00153         QKeyEvent* keyEvent = static_cast<QKeyEvent*>( event );
00154 
00155         // Terminate the current drawing with the Enter-key:
00156         if(
00157             ( keyEvent->key() == Qt::Key_Enter ||
00158               keyEvent->key() == Qt::Key_Return )
00159             && !m_isDragging )
00160         {
00161             accept();
00162 
00163             return true;
00164         }
00165 
00166         // Terminate the current drawing with the Enter-key:
00167         if( keyEvent->key() == Qt::Key_Backspace && !m_isDragging )
00168         {
00169             cancelStep();
00170 
00171             return true;
00172         }
00173 
00174         // Cancel dragging with ESC-key:
00175         if( keyEvent->key() == Qt::Key_Escape )
00176         {
00177             cancel();
00178 
00179             m_isDragging = false;
00180             m_mouseButtonIsDown = false;
00181 
00182             return true;
00183         }
00184 
00185         // If SHIFT is pressed, some tools create a "square" object while dragging:
00186         if( keyEvent->key() == Qt::Key_Shift )
00187         {
00188             m_shiftPressed = true;
00189             if( m_isDragging )
00190             {
00191                 mouseDragShiftPressed();
00192 
00193                 return true;
00194             }
00195         }
00196 
00197         // If Ctrl is pressed, some tools create a "centered" object while dragging:
00198         if( keyEvent->key() == Qt::Key_Control )
00199         {
00200             m_ctrlPressed = true;
00201             if( m_isDragging )
00202             {
00203                 mouseDragCtrlPressed();
00204 
00205                 return true;
00206             }
00207         }
00208 
00209     }
00210 
00211     // Key release events:
00212     if( event->type() == QEvent::KeyRelease )
00213     {
00214         QKeyEvent* keyEvent = static_cast<QKeyEvent*>( event );
00215 
00216         Qt::Key key = (Qt::Key)keyEvent->key();
00217         if( key == Qt::Key_Shift )
00218         {
00219             m_shiftPressed = false;
00220             if( m_isDragging )
00221             {
00222                 mouseDragShiftReleased();
00223 
00224                 return true;
00225             }
00226         }
00227 
00228         if( key == Qt::Key_Control )
00229         {
00230             m_ctrlPressed = false;
00231             if( m_isDragging )
00232             {
00233                 mouseDragCtrlReleased();
00234 
00235                 return true;
00236             }
00237         }
00238 
00239         if( key == Qt::Key_Left || key == Qt::Key_Right || key == Qt::Key_Up || key == Qt::Key_Down )
00240         {
00241             arrowKeyReleased( key );
00242             return true;
00243         }
00244 
00245         return keyReleased( key );
00246     }
00247 
00248     return false;
00249 }
00250 
00251 void
00252 VTool::activate()
00253 {
00254     kdDebug() << k_funcinfo << endl;
00255     refreshUnit();
00256     QPixmap Icon = BarIcon( icon() );
00257     view()->contextHelpAction()->updateHelp( uiname(), contextHelp(), &Icon );
00258     view()->statusMessage()->setText( statusText() );
00259     toolController()->setCurrentTool( this );
00260 #if 0
00261     if( toolController()->activeTool() )
00262     {
00263         toolController()->activeTool()->action()->setChecked( false );
00264         toolController()->activeTool()->deactivate();
00265     }
00266 
00267     if( toolController()->activeTool() == this )
00268         showDialog();
00269     else
00270     {
00271         refreshUnit();
00272         QPixmap Icon = BarIcon( icon() );
00273         view()->contextHelpAction()->updateHelp( uiname(), contextHelp(), &Icon );
00274         view()->statusMessage()->setText( statusText() );
00275         toolController()->activeTool()->action()->setChecked( true );
00276     }
00277 #endif
00278 }
00279 
KDE Home | KDE Accessibility Home | Description of Access Keys