karbon

vtoolcontroller.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 "karbon_tool_registry.h"
00021 #include "karbon_part.h"
00022 #include <vselection.h>
00023 #include "vtoolcontroller.h"
00024 #include "vtool.h"
00025 #include "vtoolbox.h"
00026 
00027 VToolController::VToolController( KarbonView *view ) : m_view( view ), m_currentTool( 0L ), m_setup( false )
00028 {
00029     m_tools.setAutoDelete( true );
00030 }
00031 
00032 void
00033 VToolController::init()
00034 {
00035 }
00036 
00037 VToolController::~VToolController()
00038 {
00039 }
00040 
00041 void
00042 VToolController::setCurrentTool( VTool *tool )
00043 {
00044     if( m_currentTool )
00045     {
00046         m_currentTool->action()->setChecked( false );
00047         m_currentTool->deactivate();
00048     }
00049 
00050     if( m_currentTool && m_currentTool == tool )
00051         m_currentTool->showDialog();
00052     else
00053     {
00054         m_currentTool = tool;
00055 
00056         if( ! tool )
00057             return;
00058 
00059         m_currentTool->action()->setChecked( true );
00060         m_currentTool->action()->activate();
00061     }
00062     m_toolBox->slotSetTool( tool->name() );
00063 }
00064 
00065 void
00066 VToolController::registerTool( VTool *tool )
00067 {
00068     if( !m_tools.find( tool->name() ) )
00069         m_tools.insert( tool->name(), tool );
00070     //kdDebug(38000) << "active tool : " << m_currentTool->name() << endl;
00071 }
00072 
00073 void
00074 VToolController::unregisterTool( VTool *tool )
00075 {
00076     // tool->name() is not valid in VTool destructor
00077     QDictIterator<VTool> it( m_tools );
00078     for( ; it.current(); ++it )
00079         if (it.current() == tool)
00080         {
00081             m_tools.remove(it.currentKey());
00082             return;
00083         }
00084 }
00085 
00086 bool
00087 VToolController::mouseEvent( QMouseEvent* event, const KoPoint &p )
00088 {
00089     if( !m_currentTool ) {
00090         return false;
00091     }
00092 
00093     return m_currentTool->mouseEvent( event, p );
00094 }
00095 
00096 bool
00097 VToolController::keyEvent( QEvent* event )
00098 {
00099     if( !m_currentTool ) {
00100         return false;
00101     }
00102 
00103     return m_currentTool->keyEvent( event );
00104 }
00105 
00106 void
00107 VToolController::setUp( KActionCollection *ac, VToolBox * toolbox )
00108 {
00109     if( m_setup )
00110     {
00111         resetToolBox( toolbox);
00112         return;
00113     }
00114 
00115     KarbonToolRegistry::instance()->createTools( ac, m_view );
00116 
00117     m_toolBox = toolbox;
00118 
00119     QDictIterator<VTool> it( m_tools );
00120     for( ; it.current(); ++it )
00121         toolbox->registerTool( it.current() );
00122 
00123     toolbox->setupTools();
00124 
00125     VTool *t = findTool( "tool_select" );
00126     setCurrentTool(t);
00127 
00128     m_setup = true;
00129 }
00130 
00131 void
00132 VToolController::resetToolBox( VToolBox * toolbox )
00133 {
00134     m_toolBox = toolbox;
00135 
00136     QDictIterator<VTool> it( m_tools );
00137     for( ; it.current(); ++it )
00138         toolbox->registerTool( it.current() );
00139 
00140     toolbox->setupTools();
00141 
00142     if( m_currentTool )
00143     {
00144         // restore the old current tool
00145         setCurrentTool( m_currentTool );
00146         m_currentTool = 0;
00147     }
00148 }
00149 
00150 VTool *
00151 VToolController::findTool( const QString &toolName ) const
00152 {
00153     VTool *tool = 0;
00154     QDictIterator<VTool> it( m_tools );
00155     for( ; it.current(); ++it )
00156         if( it.current()->name() == toolName )
00157             return it.current();
00158     return tool;
00159 }
00160 
00161 void
00162 VToolController::youAintGotNoToolBox()
00163 {
00164     m_toolBox = 0;
00165     //m_currentTool = currentTool();
00166 }
00167 
KDE Home | KDE Accessibility Home | Description of Access Keys