karbon
roundcornersplugin.cc00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "roundcornersplugin.h"
00021 #include <karbon_view.h>
00022 #include <karbon_part.h>
00023 #include <core/vpath.h>
00024 #include <core/vsegment.h>
00025 #include <kgenericfactory.h>
00026 #include <kdebug.h>
00027 #include <qgroupbox.h>
00028 #include <qlabel.h>
00029
00030 #include <knuminput.h>
00031
00032 typedef KGenericFactory<VRoundCornersPlugin, KarbonView> VRoundCornersPluginFactory;
00033 K_EXPORT_COMPONENT_FACTORY( karbon_roundcornersplugin, VRoundCornersPluginFactory( "karbonroundcornersplugin" ) )
00034
00035 VRoundCornersPlugin::VRoundCornersPlugin( KarbonView *parent, const char* name, const QStringList & ) : Plugin( parent, name )
00036 {
00037 new KAction(
00038 i18n( "&Round Corners..." ), "14_roundcorners", 0, this,
00039 SLOT( slotRoundCorners() ), actionCollection(), "path_round_corners" );
00040
00041 m_roundCornersDlg = new VRoundCornersDlg();
00042 m_roundCornersDlg->setRadius( 10.0 );
00043 }
00044
00045 VRoundCornersPlugin::~VRoundCornersPlugin()
00046 {
00047 }
00048
00049 void
00050 VRoundCornersPlugin::slotRoundCorners()
00051 {
00052 KarbonPart *part = ((KarbonView *)parent())->part();
00053 if( part && m_roundCornersDlg->exec() )
00054 part->addCommand( new VRoundCornersCmd( &part->document(), m_roundCornersDlg->radius() ), true );
00055 }
00056
00057
00058 VRoundCornersDlg::VRoundCornersDlg( QWidget* parent, const char* name )
00059 : KDialogBase( parent, name, true, i18n( "Polygonize" ), Ok | Cancel )
00060 {
00061
00062 QGroupBox* group = new QGroupBox( 2, Qt::Horizontal, i18n( "Properties" ), this );
00063
00064 new QLabel( i18n( "Round corners:" ), group );
00065 m_radius = new KDoubleNumInput( group );
00066 group->setMinimumWidth( 300 );
00067
00068
00069 connect( this, SIGNAL( okClicked() ), this, SLOT( accept() ) );
00070 connect( this, SIGNAL( cancelClicked() ), this, SLOT( reject() ) );
00071
00072 setMainWidget( group );
00073 setFixedSize( baseSize() );
00074 }
00075
00076 double
00077 VRoundCornersDlg::radius() const
00078 {
00079 return m_radius->value();
00080 }
00081
00082 void
00083 VRoundCornersDlg::setRadius( double value )
00084 {
00085 m_radius->setValue(value);
00086 }
00087
00088
00089
00090 VRoundCornersCmd::VRoundCornersCmd( VDocument* doc, double radius )
00091 : VReplacingCmd( doc, i18n( "Round Corners" ) )
00092 {
00093
00094 m_radius = radius > 0.0 ? radius : 1.0;
00095 }
00096
00097 void
00098 VRoundCornersCmd::visitVSubpath( VSubpath& path )
00099 {
00100
00101 if( path.isEmpty() )
00102 return;
00103
00104
00105
00106
00107 VSubpath newPath( 0L );
00108
00109 path.first();
00110
00111 path.next();
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225 double length;
00226 double param;
00227
00228
00229
00230
00231
00232 if( path.current()->isFlat() )
00233 path.current()->setDegree( 1 );
00234
00235 if( path.getLast()->isFlat() )
00236 path.getLast()->setDegree( 1 );
00237
00238 if(
00239 path.isClosed() &&
00240
00241 !path.getLast()->isSmooth( *path.current() ) )
00242 {
00243 length = path.current()->length();
00244
00245 param = length > 2 * m_radius
00246 ? path.current()->lengthParam( m_radius )
00247 : param = 0.5;
00248
00249
00250 path.insert(
00251 path.current()->splitAt( param ) );
00252
00253 newPath.moveTo(
00254 path.current()->knot() );
00255
00256 path.next();
00257
00258
00259 if( !success() )
00260 setSuccess();
00261 }
00262 else
00263 {
00264 newPath.moveTo(
00265 path.current()->prev()->knot() );
00266 }
00267
00268
00269
00270
00271
00272 while(
00273 path.current() &&
00274 path.current()->next() )
00275 {
00276
00277 if( path.current()->isFlat() )
00278 path.current()->setDegree( 1 );
00279
00280 if( path.current()->next()->isFlat() )
00281 path.current()->next()->setDegree( 1 );
00282
00283
00284
00285 if( path.current()->isSmooth() )
00286 {
00287 newPath.append( path.current()->clone() );
00288 path.next();
00289 continue;
00290 }
00291
00292
00293
00294
00295 length = path.current()->length();
00296
00297
00298
00299 if( length > m_radius )
00300 {
00301 param = path.current()->lengthParam( length - m_radius );
00302
00303 path.insert(
00304 path.current()->splitAt( param ) );
00305 newPath.append(
00306 path.current()->clone() );
00307
00308 path.next();
00309 }
00310
00311
00312
00313 path.next();
00314
00315
00316
00317 length = path.current()->length();
00318
00319 param = length > 2 * m_radius
00320 ? path.current()->lengthParam( m_radius )
00321 : 0.5;
00322
00323 path.insert(
00324 path.current()->splitAt( param ) );
00325
00326
00327
00328 newPath.curveTo(
00329 path.current()->prev()->pointAt( 0.5 ),
00330 path.current()->pointAt( 0.5 ),
00331 path.current()->knot() );
00332
00333
00334 if( !success() )
00335 setSuccess();
00336
00337 path.next();
00338 }
00339
00340
00341
00342
00343
00344 if( path.isClosed() )
00345 {
00346
00347 if( path.current()->isFlat() )
00348 path.current()->setDegree( 1 );
00349
00350 if( path.getFirst()->next()->isFlat() )
00351 path.getFirst()->next()->setDegree( 1 );
00352
00353
00354 if( !path.current()->isSmooth( *path.getFirst()->next() ) )
00355 {
00356 length = path.current()->length();
00357
00358
00359
00360 if( length > m_radius )
00361 {
00362 param = path.current()->lengthParam( length - m_radius );
00363
00364 path.insert(
00365 path.current()->splitAt( param ) );
00366 newPath.append(
00367 path.current()->clone() );
00368
00369 path.next();
00370 }
00371
00372
00373 path.first();
00374 path.next();
00375
00376
00377 newPath.curveTo(
00378 path.getLast()->pointAt( 0.5 ),
00379 path.current()->pointAt( 0.5 ),
00380 path.current()->knot() );
00381
00382
00383 if( !success() )
00384 setSuccess();
00385 }
00386 else
00387 newPath.append( path.current()->clone() );
00388
00389 newPath.close();
00390 }
00391 else
00392 newPath.append( path.current()->clone() );
00393
00394
00395 path = newPath;
00396
00397
00398 path.invalidateBoundingBox();
00399 }
00400
00401 #include "roundcornersplugin.moc"
00402
|