karbon

vrotatetool.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 <math.h>
00021 
00022 #include <qcursor.h>
00023 #include <qlabel.h>
00024 
00025 #include <klocale.h>
00026 #include <KoRect.h>
00027 
00028 #include <karbon_part.h>
00029 #include <karbon_view.h>
00030 #include <core/vglobal.h>
00031 #include "vrotatetool.h"
00032 #include <render/vpainter.h>
00033 #include <render/vpainterfactory.h>
00034 #include <commands/vtransformcmd.h>
00035 
00036 #include <kdebug.h>
00037 
00038 VRotateTool::VRotateTool( KarbonView *view )
00039     : VTool( view, "tool_rotate" )
00040 {
00041     m_objects.setAutoDelete( true );
00042     registerTool( this );
00043 }
00044 
00045 VRotateTool::~VRotateTool()
00046 {
00047     m_objects.clear();
00048 }
00049 
00050 void
00051 VRotateTool::activate()
00052 {
00053     view()->setCursor( QCursor( Qt::arrowCursor ) );
00054     view()->part()->document().selection()->setState( VObject::selected );
00055     view()->part()->document().selection()->showHandle( false );
00056     VTool::activate();
00057 }
00058 
00059 QString
00060 VRotateTool::statusText()
00061 {
00062     return i18n( "Rotate" );
00063 }
00064 
00065 void
00066 VRotateTool::draw()
00067 {
00068     VPainter* painter = view()->painterFactory()->editpainter();
00069     //painter->setZoomFactor( view()->zoom() );
00070     painter->setRasterOp( Qt::NotROP );
00071 
00072     VObjectListIterator itr = m_objects;
00073     for( ; itr.current(); ++itr )
00074     {
00075         itr.current()->draw( painter, &itr.current()->boundingBox() );
00076     }
00077 }
00078 
00079 void
00080 VRotateTool::mouseButtonPress()
00081 {
00082     //view()->painterFactory()->painter()->end();
00083 
00084     recalc();
00085 
00086     // Draw new object:
00087     draw();
00088 }
00089 
00090 void
00091 VRotateTool::mouseDrag()
00092 {
00093     // Erase old object:
00094     draw();
00095 
00096     recalc();
00097 
00098     // Draw new object:
00099     draw();
00100 }
00101 
00102 void
00103 VRotateTool::mouseDragRelease()
00104 {
00105     view()->part()->addCommand(
00106         new VRotateCmd(
00107             &view()->part()->document(),
00108             m_center,
00109             m_angle, altPressed() ),
00110         true );
00111 }
00112 
00113 void
00114 VRotateTool::cancel()
00115 {
00116     // Erase old object:
00117     if ( isDragging() )
00118     {
00119         draw();
00120         view()->repaintAll( view()->part()->document().selection()->boundingBox() );
00121     }
00122 }
00123 
00124 void
00125 VRotateTool::recalc()
00126 {
00127     // Get center:
00128     m_center = view()->part()->document().selection()->boundingBox().center();
00129 
00130     // Calculate angle between vector (last - center) and (first - center):
00131     m_angle = VGlobal::one_pi_180 * (
00132             atan2(
00133                 last().y() - m_center.y(),
00134                 last().x() - m_center.x() )
00135         -
00136             atan2(
00137                 first().y() - m_center.y(),
00138                 first().x() - m_center.x() ) );
00139 
00140     VRotateCmd cmd( 0L, m_center, m_angle );
00141 
00142     // Copy selected objects and transform:
00143     m_objects.clear();
00144     VObject* copy;
00145 
00146     VObjectListIterator itr = view()->part()->document().selection()->objects();
00147     for ( ; itr.current() ; ++itr )
00148     {
00149         if( itr.current()->state() != VObject::deleted )
00150         {
00151             copy = itr.current()->clone();
00152 
00153             cmd.visit( *copy );
00154 
00155             copy->setState( VObject::edit );
00156 
00157             m_objects.append( copy );
00158         }
00159     }
00160 }
00161 
00162 void
00163 VRotateTool::setup( KActionCollection *collection )
00164 {
00165     m_action = static_cast<KRadioAction *>(collection -> action( name() ) );
00166 
00167     if( m_action == 0 )
00168     {
00169         m_action = new KRadioAction( i18n( "Rotate Tool" ), "14_rotate", Qt::SHIFT+Qt::Key_H, this, SLOT( activate() ), collection, name() );
00170         m_action->setToolTip( i18n( "Rotate" ) );
00171         m_action->setExclusiveGroup( "manipulation" );
00172         //m_ownAction = true;
00173     }
00174 }
00175 
KDE Home | KDE Accessibility Home | Description of Access Keys