karbon

vstartool.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 <qlabel.h>
00021 #include <qgroupbox.h>
00022 
00023 #include <klocale.h>
00024 #include <knuminput.h>
00025 #include <kcombobox.h>
00026 
00027 #include <karbon_view.h>
00028 #include <karbon_part.h>
00029 #include <shapes/vstar.h>
00030 #include "vstartool.h"
00031 #include "KoUnitWidgets.h"
00032 
00033 
00034 VStarOptionsWidget::VStarOptionsWidget( KarbonPart *part, QWidget* parent, const char* name )
00035     : KDialogBase( parent, name, true, i18n( "Insert Star" ), Ok | Cancel ), m_part( part )
00036 {
00037     QGroupBox *group = new QGroupBox( 2, Qt::Horizontal, i18n( "Properties" ), this );
00038     new QLabel( i18n( "Type:" ), group );
00039     m_type = new KComboBox( false, group );
00040     m_type->insertItem( i18n( "Star Outline" ), VStar::star_outline );
00041     m_type->insertItem( i18n( "Spoke" ), VStar::spoke );
00042     m_type->insertItem( i18n( "Wheel" ), VStar::wheel );
00043     m_type->insertItem( i18n( "Polygon" ), VStar::polygon );
00044     m_type->insertItem( i18n( "Framed Star" ), VStar::framed_star);
00045     m_type->insertItem( i18n( "Star" ), VStar::star );
00046     m_type->insertItem( i18n( "Gear" ), VStar::gear );
00047     connect( m_type, SIGNAL( activated( int ) ), this, SLOT( typeChanged( int ) ) );
00048 
00049     // add width/height-input:
00050     m_outerRLabel = new QLabel( i18n( "Outer radius:" ), group );
00051     m_outerR = new KoUnitDoubleSpinBox( group, 0.0, 1000.0, 0.5, 50.0, KoUnit::U_MM );
00052     connect( m_outerR, SIGNAL( valueChanged( double ) ), this, SLOT( setOuterRadius( double ) ) );
00053 
00054     m_innerRLabel = new QLabel( i18n( "Inner radius:" ), group );
00055     m_innerR = new KoUnitDoubleSpinBox( group, 0.0, 1000.0, 0.5, 25.0, KoUnit::U_MM );
00056 
00057     refreshUnit();
00058 
00059     new QLabel( i18n( "Edges:" ), group );
00060     m_edges = new KIntSpinBox( group );
00061     m_edges->setMinValue( 3 );
00062     connect( m_edges, SIGNAL( valueChanged( int ) ), this, SLOT( setEdges( int ) ) );
00063 
00064     new QLabel( i18n( "Inner angle:" ), group );
00065     m_innerAngle = new KIntSpinBox( group );
00066     m_innerAngle->setMinValue( 0 );
00067     m_innerAngle->setMaxValue( 360 );
00068 
00069     new QLabel( i18n( "Roundness:" ), group );
00070     m_roundness = new KDoubleNumInput( group );
00071     m_roundness->setRange( 0.0, 1.0, 0.05 );
00072 
00073     typeChanged( VStar::star_outline );
00074 
00075     group->setInsideMargin( 4 );
00076     group->setInsideSpacing( 2 );
00077 
00078     setMainWidget( group );
00079     setFixedSize( baseSize() );
00080 }
00081 
00082 void
00083 VStarOptionsWidget::refreshUnit()
00084 {
00085     m_outerR->setUnit( m_part->unit() );
00086     m_innerR->setUnit( m_part->unit() );
00087 }
00088 
00089 void
00090 VStarOptionsWidget::setEdges( int v )
00091 {
00092     m_edges->setValue( v );
00093 
00094     // set optimal inner radius
00095     if( type() == VStar::star )
00096         m_innerR->setValue( VStar::getOptimalInnerRadius( edges(), outerRadius(), innerAngle() ) );
00097 }
00098 
00099 void
00100 VStarOptionsWidget::setInnerRadius( double v )
00101 {
00102     m_innerR->changeValue( v );
00103 }
00104 
00105 void
00106 VStarOptionsWidget::setOuterRadius( double v )
00107 {
00108     m_outerR->setValue( v );
00109 
00110     // set optimal inner radius
00111     if( type() == VStar::star )
00112         m_innerR->setValue( VStar::getOptimalInnerRadius( edges(), outerRadius(), innerAngle() ) );
00113 }
00114 
00115 double
00116 VStarOptionsWidget::roundness() const
00117 {
00118     return m_roundness->value();
00119 }
00120 
00121 int
00122 VStarOptionsWidget::edges() const
00123 {
00124     return m_edges->value();
00125 }
00126 
00127 double
00128 VStarOptionsWidget::innerRadius() const
00129 {
00130     return m_innerR->value();
00131 }
00132 
00133 double
00134 VStarOptionsWidget::outerRadius() const
00135 {
00136     return m_outerR->value();
00137 }
00138 
00139 uint
00140 VStarOptionsWidget::type() const
00141 {
00142     return m_type->currentItem();
00143 }
00144 
00145 uint
00146 VStarOptionsWidget::innerAngle() const
00147 {
00148     return m_innerAngle->value();
00149 }
00150 
00151 void
00152 VStarOptionsWidget::typeChanged( int type )
00153 {
00154     m_innerR->setEnabled( type == VStar::star || type == VStar::star_outline || type == VStar::framed_star || type == VStar::gear );
00155     m_innerAngle->setEnabled( type == VStar::star || type == VStar::star_outline || type == VStar::framed_star || type == VStar::gear );
00156 
00157     // set optimal inner radius
00158     if( type == VStar::star )
00159         m_innerR->changeValue( VStar::getOptimalInnerRadius( edges(), outerRadius(), innerAngle() ) );
00160 }
00161 
00162 VStarTool::VStarTool( KarbonView *view )
00163     : VShapeTool( view, "tool_star", true )
00164 {
00165     // create config dialog:
00166     m_optionsWidget = new VStarOptionsWidget( view->part() );
00167     m_optionsWidget->setEdges( 5 );
00168     registerTool( this );
00169 }
00170 
00171 void VStarTool::refreshUnit()
00172 {
00173     m_optionsWidget->refreshUnit();
00174 }
00175 
00176 VStarTool::~VStarTool()
00177 {
00178     delete( m_optionsWidget );
00179 }
00180 
00181 void
00182 VStarTool::arrowKeyReleased( Qt::Key key )
00183 {
00184     int change = 0;
00185     if( key == Qt::Key_Up )
00186         change = 1;
00187     else if( key == Qt::Key_Down )
00188         change = -1;
00189 
00190     if( change != 0 )
00191     {
00192         draw();
00193 
00194         m_optionsWidget->setEdges( m_optionsWidget->edges() + change );
00195 
00196         draw();
00197     }
00198 }
00199 
00200 VPath*
00201 VStarTool::shape( bool interactive ) const
00202 {
00203     if( interactive )
00204     {
00205         return
00206             new VStar(
00207                 0L,
00208                 m_p,
00209                 m_optionsWidget->outerRadius(),
00210                 m_optionsWidget->innerRadius(),
00211                 m_optionsWidget->edges(), 0.0, m_optionsWidget->innerAngle(),
00212                 m_optionsWidget->roundness(), (VStar::VStarType)m_optionsWidget->type() );
00213     }
00214     else
00215         return
00216             new VStar(
00217                 0L,
00218                 m_p,
00219                 m_d1,
00220                 m_optionsWidget->innerRadius() * m_d1 /
00221                 m_optionsWidget->outerRadius(),
00222                 m_optionsWidget->edges(),
00223                 m_d2, m_optionsWidget->innerAngle(),
00224                 m_optionsWidget->roundness(), (VStar::VStarType)m_optionsWidget->type() );
00225 }
00226 
00227 bool
00228 VStarTool::showDialog() const
00229 {
00230     return m_optionsWidget->exec() == QDialog::Accepted;
00231 }
00232 
00233 void
00234 VStarTool::setup( KActionCollection *collection )
00235 {
00236     m_action = static_cast<KRadioAction *>(collection -> action( name() ) );
00237 
00238     if( m_action == 0 )
00239     {
00240         KShortcut shortcut( Qt::Key_Plus );
00241         shortcut.append(KShortcut( Qt::Key_F9 ) );
00242         m_action = new KRadioAction( i18n( "Star Tool" ), "14_star", shortcut, this, SLOT( activate() ), collection, name() );
00243         m_action->setToolTip( i18n( "Draw a star" ) );
00244         m_action->setExclusiveGroup( "shapes" );
00245         //m_ownAction = true;
00246     }
00247 }
00248 
00249 #include "vstartool.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys