karbon

vsegment.h

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 #ifndef __VSEGMENT_H__
00021 #define __VSEGMENT_H__
00022 
00023 #include <qptrlist.h>
00024 #include <qvaluelist.h>
00025 
00026 #include <KoPoint.h>
00027 #include <KoRect.h>
00028 
00029 #include "vglobal.h"
00030 #include <koffice_export.h>
00031 
00032 class QDomElement;
00033 class VPainter;
00034 
00042 class KARBONBASE_EXPORT VSegment
00043 {
00044     friend class VSubpath;
00045     friend class VSubpathIterator;
00046 
00047 public:
00053     enum VCtrlPointFixing
00054     {
00055         none = 0,
00056         first = 1,
00057         second = 2
00058     };
00059 
00060     enum VState
00061     {
00062         normal,
00063         deleted
00064     };
00065 
00066 
00067     VSegment( unsigned short deg = 3 );
00068 
00069     VSegment( const VSegment& segment );
00070 
00071     ~VSegment();
00072 
00077     unsigned short degree() const
00078     {
00079         return m_degree;
00080     }
00081 
00086     void setDegree( unsigned short deg );
00087 
00091     bool isBegin() const { return (degree() == 1) && !prev(); }
00092     bool isLine() const { return (degree() == 1) && prev(); }
00093     bool isCurve() const { return degree() > 1; }
00094 
00098     VState state() const
00099     {
00100         return m_state;
00101     }
00102 
00106     void setState( VState state )
00107     {
00108         m_state = state;
00109     }
00110 
00111 
00115     const KoPoint& point( int i ) const
00116     {
00117         return m_nodes[ i ].m_vector;
00118     }
00119 
00125     const KoPoint& p( int i ) const
00126     {
00127         return i == 0
00128                ? prev()->knot()
00129                : m_nodes[ --i ].m_vector;
00130     }
00131 
00135     const KoPoint& knot() const
00136     {
00137         return point( degree() - 1 );
00138     }
00139 
00143     void setPoint( int i, const KoPoint& p )
00144     {
00145         m_nodes[ i ].m_vector = p;
00146     }
00147 
00153     void setP( int i, const KoPoint& p )
00154     {
00155         if( i == 0 )
00156             prev()->setKnot( p );
00157         else
00158             m_nodes[ --i ].m_vector = p;
00159     }
00160 
00164     void setKnot( const KoPoint& p )
00165     {
00166         m_nodes[ degree() - 1 ].m_vector = p;
00167     }
00168 
00169 
00173     bool pointIsSelected( int i ) const
00174     {
00175         return m_nodes[ i ].m_isSelected;
00176     }
00177 
00181     bool knotIsSelected() const
00182     {
00183         return m_nodes[ degree() - 1 ].m_isSelected;
00184     }
00185 
00189     void selectPoint( int i, bool select = true )
00190     {
00191         m_nodes[ i ].m_isSelected = select;
00192     }
00193 
00197     void selectKnot( bool select = true )
00198     {
00199         m_nodes[ degree() - 1 ].m_isSelected = select;
00200     }
00201 
00202 
00207     // TODO: Move this function into "userland"
00208     uint nodeNear( const KoPoint& p,
00209                    double isNearRange = VGlobal::isNearRange ) const;
00210 
00211 
00216     VSegment* prev() const;
00217 
00222     VSegment* next() const;
00223 
00228     bool isFlat( double flatness = VGlobal::flatnessTolerance ) const;
00229 
00230 
00235     KoPoint pointAt( double t ) const;
00236 
00241     void pointDerivativesAt( double t, KoPoint* p = 0L,
00242                              KoPoint* d1 = 0L, KoPoint* d2 = 0L ) const;
00243 
00244 
00252     KoPoint tangentAt( double t ) const;
00253 
00259     void pointTangentNormalAt( double t, KoPoint* p = 0L,
00260                                KoPoint* tn = 0L, KoPoint* n = 0L ) const;
00261 
00262 
00267     double length( double t = 1.0 ) const;
00268 
00273     double chordLength() const;
00274 
00278     double polyLength() const;
00279 
00280 
00285     double lengthParam( double len ) const;
00286 
00287 
00292     double nearestPointParam( const KoPoint& p ) const;
00293 
00294 
00300     bool isSmooth( const VSegment& next ) const;
00301 
00302     bool isSmooth() const
00303     {
00304         return next()
00305                ? isSmooth( *next() )
00306                : false;
00307     }
00308 
00309 
00315     VSegment* revert() const;
00316 
00317 
00323     VSegment* splitAt( double t );
00324 
00325 
00329     static double height(
00330         const KoPoint& a,
00331         const KoPoint& p,
00332         const KoPoint& b );
00333 
00334 
00338     static bool linesIntersect(
00339         const KoPoint& a0,
00340         const KoPoint& a1,
00341         const KoPoint& b0,
00342         const KoPoint& b1 );
00343 
00347     bool intersects( const VSegment& segment ) const;
00348 
00349 
00355     double pointIsLeft( const KoPoint& p ) const
00356     {
00357         return
00358             ( knot().x() - prev()->knot().x() ) *
00359             ( p.y() - prev()->knot().y() )
00360             -
00361             ( p.x() - prev()->knot().x() ) *
00362             ( knot().y() - prev()->knot().y() );
00363     }
00364 
00368     KoRect boundingBox() const;
00369 
00370 
00371     void draw( VPainter* painter ) const;
00372 
00373     // TODO: remove this backward compatibility function after koffice 1.3.x.
00374     void load( const QDomElement& element );
00375 
00376 
00380     VSegment* clone() const;
00381 
00382 private:
00387     void rootParams( QValueList<double>& params ) const;
00388 
00392     int controlPolygonZeros() const;
00393 
00394 
00398     unsigned short m_degree : 6;
00399 
00403     VState m_state : 2;
00404 
00408     struct VNodeData
00409     {
00410         KoPoint m_vector;
00411         bool m_isSelected;
00412     };
00413 
00417     VNodeData* m_nodes;
00418 
00419 
00423     VSegment* m_prev;
00424 
00428     VSegment* m_next;
00429 };
00430 
00431 #endif
00432 
KDE Home | KDE Accessibility Home | Description of Access Keys