karbon

vellipsetool.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 
00021 #include <qgroupbox.h>
00022 #include <qlabel.h>
00023 
00024 #include <klocale.h>
00025 #include "KoUnitWidgets.h"
00026 #include <KoUnit.h>
00027 #include <kcombobox.h>
00028 #include <knuminput.h>
00029 
00030 #include <karbon_view.h>
00031 #include <karbon_part.h>
00032 #include <shapes/vellipse.h>
00033 #include "vellipsetool.h"
00034 #include "vglobal.h"
00035 
00036 
00037 VEllipseOptionsWidget::VEllipseOptionsWidget( KarbonPart *part, QWidget *parent, const char *name )
00038     : KDialogBase( parent, name, true, i18n( "Insert Ellipse" ), Ok | Cancel ), m_part( part )
00039 {
00040     QGroupBox *group = new QGroupBox( 2, Qt::Horizontal, i18n( "Properties" ), this );
00041     new QLabel( i18n( "Type:" ), group );
00042     m_type = new KComboBox( false, group );
00043     m_type->insertItem( i18n( "Full" ), VEllipse::full );
00044     m_type->insertItem( i18n( "Section" ), VEllipse::section );
00045     m_type->insertItem( i18n( "Pie" ), VEllipse::cut );
00046     m_type->insertItem( i18n( "Arc" ), VEllipse::arc );
00047     connect( m_type, SIGNAL( activated( int ) ), this, SLOT( typeChanged( int ) ) );
00048 
00049     // add width/height-input:
00050     m_widthLabel = new QLabel( i18n( "object width", "Width:" ), group );
00051     m_width = new KoUnitDoubleSpinBox( group, 0.0, 1000.0, 0.5, 100.0, KoUnit::U_MM );
00052     m_heightLabel = new QLabel( i18n( "Height:" ), group );
00053     m_height = new KoUnitDoubleSpinBox( group, 0.0, 1000.0, 0.5, 100.0, KoUnit::U_MM );
00054 
00055     new QLabel( i18n( "Start angle:" ), group );
00056     m_startAngle = new KIntSpinBox( group );
00057     m_startAngle->setMinValue( 0 );
00058     m_startAngle->setMaxValue( 360 );
00059 
00060     new QLabel( i18n( "End angle:" ), group );
00061     m_endAngle = new KIntSpinBox( group );
00062     m_endAngle->setMinValue( 0 );
00063     m_endAngle->setMaxValue( 360 );
00064 
00065     typeChanged( VEllipse::full );
00066 
00067     refreshUnit();
00068 
00069     group->setInsideMargin( 4 );
00070     group->setInsideSpacing( 2 );
00071 
00072     setMainWidget( group );
00073     setFixedSize( baseSize() );
00074 }
00075 
00076 void
00077 VEllipseOptionsWidget::typeChanged( int type )
00078 {
00079     m_startAngle->setEnabled( type != VEllipse::full );
00080     m_endAngle->setEnabled( type != VEllipse::full );
00081 }
00082 
00083 uint
00084 VEllipseOptionsWidget::type() const
00085 {
00086     return m_type->currentItem();
00087 }
00088 
00089 uint
00090 VEllipseOptionsWidget::startAngle() const
00091 {
00092     return m_startAngle->value();
00093 }
00094 
00095 uint
00096 VEllipseOptionsWidget::endAngle() const
00097 {
00098     return m_endAngle->value();
00099 }
00100 
00101 double
00102 VEllipseOptionsWidget::width() const
00103 {
00104     return m_width->value();
00105 }
00106 
00107 double
00108 VEllipseOptionsWidget::height() const
00109 {
00110     return m_height->value();
00111 }
00112 
00113 void
00114 VEllipseOptionsWidget::setWidth( double value )
00115 {
00116     m_width->changeValue( value );
00117 }
00118 
00119 void
00120 VEllipseOptionsWidget::setHeight( double value )
00121 {
00122     m_height->changeValue( value );
00123 }
00124 
00125 void
00126 VEllipseOptionsWidget::refreshUnit ()
00127 {
00128     m_width->setUnit( m_part->unit() );
00129     m_height->setUnit( m_part->unit() );
00130 }
00131 
00132 VEllipseTool::VEllipseTool( KarbonView *view )
00133     : VShapeTool( view, "tool_ellipse" )
00134 {
00135     // create config dialog:
00136     m_optionsWidget = new VEllipseOptionsWidget( view->part() );
00137     registerTool( this );
00138 
00139     m_startAngle = m_endAngle = 0;
00140     m_state = normal;
00141 }
00142 
00143 VEllipseTool::~VEllipseTool()
00144 {
00145     delete( m_optionsWidget );
00146 }
00147 
00148 void
00149 VEllipseTool::refreshUnit()
00150 {
00151     m_optionsWidget->refreshUnit();
00152 }
00153 
00154 VPath*
00155 VEllipseTool::shape( bool interactive ) const
00156 {
00157     if( interactive )
00158     {
00159         double d1 = m_optionsWidget->width() / 2.0;
00160         double d2 = m_optionsWidget->height() / 2.0;
00161         return
00162             new VEllipse(
00163                 0L,
00164                 KoPoint( m_center.x() - d1, m_center.y() - d2 ),
00165                 d1 * 2.0, d2 * 2.0,
00166                 (VEllipse::VEllipseType)m_optionsWidget->type(),
00167                 m_optionsWidget->startAngle(),
00168                 m_optionsWidget->endAngle() );
00169     }
00170     else
00171         return
00172             new VEllipse(
00173                 0L,
00174                 KoPoint( m_center.x() - m_d1, m_center.y() - m_d2 ),
00175                 m_d1 * 2.0,
00176                 m_d2 * 2.0,
00177                 (VEllipse::VEllipseType)m_optionsWidget->type(),
00178                 m_startAngle, m_endAngle );
00179 }
00180 
00181 void
00182 VEllipseTool::mouseMove()
00183 {
00184     if( m_state == normal )
00185         return;
00186 
00187     draw();
00188 
00189     //recalc();
00190 
00191     if( m_state == startangle )
00192     {
00193         m_startAngle = atan2( last().y() - m_center.y(), last().x() - m_center.x() );
00194         m_startAngle = ( m_startAngle / VGlobal::pi_2 ) * 90.0;
00195         if( m_startAngle < 0 )
00196             m_startAngle += 360.0;
00197     }
00198     else
00199     {
00200         m_endAngle = atan2( last().y() - m_center.y(), last().x() - m_center.x() );
00201         m_endAngle = ( m_endAngle / VGlobal::pi_2 ) * 90.0;
00202         if( m_endAngle < 0 )
00203             m_endAngle += 360.0;
00204     }
00205 
00206     draw();
00207 }
00208 
00209 void
00210 VEllipseTool::mouseDragRelease()
00211 {
00212     if( m_optionsWidget->type() == VEllipse::full )
00213         VShapeTool::mouseDragRelease();
00214 
00215     if( m_state == normal )
00216         if( m_optionsWidget->type() != VEllipse::full )
00217             m_state = startangle;
00218 }
00219 
00220 void
00221 VEllipseTool::mouseButtonPress()
00222 {
00223     if( m_state == normal )
00224     {
00225         VShapeTool::mouseButtonPress();
00226         m_center = first();
00227     }
00228 }
00229 
00230 void
00231 VEllipseTool::mouseButtonRelease()
00232 {
00233     if( m_optionsWidget->type() == VEllipse::full || m_state == normal )
00234         VShapeTool::mouseButtonRelease();
00235 
00236     if( m_state == startangle )
00237         m_state = endangle;
00238     else if( m_state == endangle )
00239     {
00240         VShapeTool::mouseDragRelease();
00241         m_startAngle = m_endAngle = 0;
00242         m_state = normal;
00243     }
00244 }
00245 
00246 void
00247 VEllipseTool::cancel()
00248 {
00249     if( isDragging() )
00250         VShapeTool::cancel();
00251     else
00252         draw();
00253 
00254     m_startAngle = m_endAngle = 0;
00255     m_state = normal;
00256 }
00257 
00258 bool
00259 VEllipseTool::showDialog() const
00260 {
00261     return m_optionsWidget->exec() == QDialog::Accepted;
00262 }
00263 
00264 void
00265 VEllipseTool::setup( KActionCollection *collection )
00266 {
00267     m_action = static_cast<KRadioAction *>(collection -> action( name() ) );
00268 
00269     if( m_action == 0 )
00270     {
00271         m_action = new KRadioAction( i18n( "Ellipse Tool" ), "14_ellipse", Qt::SHIFT+Qt::Key_H, this, SLOT( activate() ), collection, name() );
00272         m_action->setToolTip( i18n( "Ellipse" ) );
00273         m_action->setExclusiveGroup( "shapes" );
00274         //m_ownAction = true;
00275     }
00276 }
00277 
00278 #include "vellipsetool.moc"
00279 
KDE Home | KDE Accessibility Home | Description of Access Keys