karbon

vshapetool.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 <qcursor.h>
00021 #include <qevent.h>
00022 #include <qlabel.h>
00023 
00024 #include "karbon_part.h"
00025 #include "karbon_view.h"
00026 #include "vcanvas.h"
00027 #include "vcomposite.h"
00028 #include "vglobal.h"
00029 #include "vpainter.h"
00030 #include "vpainterfactory.h"
00031 #include "vshapecmd.h"
00032 #include "vshapetool.h"
00033 #include "vselection.h"
00034 #include "vcursor.h"
00035 
00036 VShapeTool::VShapeTool( KarbonView *view, const char *name, bool polar )
00037     : VTool( view, name )
00038 {
00039     m_cursor = new QCursor( VCursor::createCursor( VCursor::CrossHair ) );
00040 
00041     m_isPolar = polar;
00042     m_isSquare   = false;
00043     m_isCentered = false;
00044 }
00045 
00046 VShapeTool::~VShapeTool()
00047 {
00048     delete m_cursor;
00049 }
00050 
00051 QString
00052 VShapeTool::contextHelp()
00053 {
00054     QString s = i18n( "<qt><b>Shape tool</b><br>" );
00055     s += i18n( "<i>Click and drag</i> to place your own shape.<br>" );
00056     s += i18n( "<i>Click</i> to place a shape using the tool properties values.</qt>" );
00057     return s;
00058 } 
00059 
00060 void
00061 VShapeTool::activate()
00062 {
00063     VTool::activate();
00064     view()->setCursor( *m_cursor );
00065     view()->part()->document().selection()->showHandle( true );
00066 }
00067 
00068 QString
00069 VShapeTool::statusText()
00070 {
00071     return uiname();
00072 }
00073 
00074 void
00075 VShapeTool::draw()
00076 {
00077     VPainter* painter = view()->painterFactory()->editpainter();
00078     painter->setRasterOp( Qt::NotROP );
00079 
00080     VPath* composite = shape();
00081     composite->setState( VPath::edit );
00082     composite->draw( painter, &composite->boundingBox() );
00083     delete( composite );
00084 }
00085 
00086 void
00087 VShapeTool::mouseButtonPress()
00088 {
00089     recalc();
00090 
00091     // Draw new object:
00092     draw();
00093 }
00094 
00095 void
00096 VShapeTool::mouseButtonRelease()
00097 {
00098     draw();
00099 
00100     recalc();
00101 
00102     if( showDialog() )
00103     {
00104         VPath* composite = shape( true );
00105 
00106         if( composite )
00107         {
00108             VShapeCmd* cmd = new VShapeCmd(
00109                 &view()->part()->document(),
00110                 uiname(), composite, icon() );
00111 
00112             view()->part()->addCommand( cmd, true );
00113         }
00114     }
00115 
00116     m_isSquare = false;
00117     m_isCentered = false;
00118 }
00119 
00120 void
00121 VShapeTool::mouseDrag()
00122 {
00123     // Erase old object:
00124     draw();
00125 
00126     recalc();
00127 
00128     // Draw new object:
00129     draw();
00130 }
00131 
00132 void
00133 VShapeTool::mouseDragRelease()
00134 {
00135     //recalc();
00136 
00137     VShapeCmd* cmd = new VShapeCmd(
00138         &view()->part()->document(),
00139         uiname(), shape(), icon() );
00140 
00141     view()->part()->addCommand( cmd, true );
00142 
00143     m_isSquare = false;
00144     m_isCentered = false;
00145 }
00146 
00147 void
00148 VShapeTool::mouseDragShiftPressed()
00149 {
00150     // Erase old object:
00151     draw();
00152 
00153     m_isSquare = true;
00154     recalc();
00155 
00156     // Draw new object:
00157     draw();
00158 }
00159 
00160 void
00161 VShapeTool::mouseDragCtrlPressed()
00162 {
00163     // Erase old object:
00164     draw();
00165 
00166     m_isCentered = true;
00167     recalc();
00168 
00169     // Draw new object:
00170     draw();
00171 }
00172 
00173 void
00174 VShapeTool::mouseDragShiftReleased()
00175 {
00176     // Erase old object:
00177     draw();
00178 
00179     m_isSquare = false;
00180     recalc();
00181 
00182     // Draw new object:
00183     draw();
00184 }
00185 
00186 void
00187 VShapeTool::mouseDragCtrlReleased()
00188 {
00189     // Erase old object:
00190     draw();
00191 
00192     m_isCentered = false;
00193     recalc();
00194 
00195     // Draw new object:
00196     draw();
00197 }
00198 
00199 void
00200 VShapeTool::cancel()
00201 {
00202     // Erase old object:
00203     if ( isDragging() )
00204     {
00205         draw();
00206         m_isSquare = false;
00207         m_isCentered = false;
00208     }
00209 }
00210 
00211 void
00212 VShapeTool::recalc()
00213 {
00214     m_isSquare = shiftPressed();
00215     m_isCentered = ctrlPressed();
00216 
00217     KoPoint _first = view()->canvasWidget()->snapToGrid( first() );
00218     KoPoint _last = view()->canvasWidget()->snapToGrid( last() );
00219 
00220     // Calculate radius and angle:
00221     if( m_isPolar )
00222     {
00223         // Radius:
00224         m_d1 = sqrt(
00225             ( _last.x() - _first.x() ) * ( _last.x() - _first.x() ) +
00226             ( _last.y() - _first.y() ) * ( _last.y() - _first.y() ) );
00227 
00228         // Angle:
00229         m_d2 = atan2( _last.y() - _first.y(), _last.x() - _first.x() );
00230 
00231         // Define pi/2 as "0.0":
00232         m_d2 -= VGlobal::pi_2;
00233 
00234         m_p = _first;
00235     }
00236     else
00237     // Calculate width and height:
00238     {
00239         m_d1 = _last.x() - _first.x();
00240         m_d2 = _last.y() - _first.y();
00241 
00242         const int m_sign1 = VGlobal::sign( m_d1 );
00243 // TODO: revert when we introduce y-mirroring:
00244         const int m_sign2 = VGlobal::sign( -m_d2 );
00245 
00246         // Make unsigned:
00247         if( m_d1 < 0.0 )
00248             m_d1 = -m_d1;
00249 
00250         if( m_d2 < 0.0 )
00251             m_d2 = -m_d2;
00252 
00253         if ( m_isSquare )
00254         {
00255             if ( m_d1 > m_d2 )
00256                 m_d2 = m_d1;
00257             else
00258                 m_d1 = m_d2;
00259         }
00260 
00261         m_p.setX(
00262             _first.x() - ( m_sign1 == -1 ? m_d1 : 0.0 ) );
00263 // TODO: revert when we introduce y-mirroring:
00264         m_p.setY(
00265             _first.y() + ( m_sign2 == -1 ? m_d2 : 0.0 ) );
00266 
00267         if ( m_isCentered )
00268         {
00269             m_p.setX( m_p.x() - m_sign1 * qRound( m_d1 * 0.5 ) );
00270             m_p.setY( m_p.y() + m_sign2 * qRound( m_d2 * 0.5 ) );
00271         }
00272     }
00273 }
00274 
KDE Home | KDE Accessibility Home | Description of Access Keys