1 // Copyright (C) 2004 Dominique Devriese <devriese@kde.org>
3 // This program is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU General Public License
5 // as published by the Free Software Foundation; either version 2
6 // of the License, or (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18 #include "kig_document.h"
20 #include "../objects/object_calcer.h"
21 #include "../objects/object_holder.h"
22 #include "../objects/point_imp.h"
23 #include "../objects/polygon_imp.h"
24 #include "../misc/coordinate_system.h"
25 #include "../misc/rect.h"
29 KigDocument::KigDocument( std::set
<ObjectHolder
*> objects
, CoordinateSystem
* coordsystem
,
30 bool showgrid
, bool showaxes
, bool nv
)
31 : mobjects( objects
), mcoordsystem( coordsystem
), mshowgrid( showgrid
),
32 mshowaxes( showaxes
), mnightvision( nv
)
36 const CoordinateSystem
& KigDocument::coordinateSystem() const
38 assert( mcoordsystem
);
42 const std::vector
<ObjectHolder
*> KigDocument::objects() const
44 return std::vector
<ObjectHolder
*>( mobjects
.begin(), mobjects
.end() );
47 const std::set
<ObjectHolder
*>& KigDocument::objectsSet() const
52 void KigDocument::setCoordinateSystem( CoordinateSystem
* s
)
54 delete switchCoordinateSystem( s
);
57 CoordinateSystem
* KigDocument::switchCoordinateSystem( CoordinateSystem
* s
)
59 CoordinateSystem
* ret
= mcoordsystem
;
64 std::vector
<ObjectHolder
*> KigDocument::whatAmIOn( const Coordinate
& p
, const KigWidget
& w
) const
66 std::vector
<ObjectHolder
*> ret
;
67 std::vector
<ObjectHolder
*> curves
;
68 std::vector
<ObjectHolder
*> fatobjects
;
69 for ( std::set
<ObjectHolder
*>::const_iterator i
= mobjects
.begin();
70 i
!= mobjects
.end(); ++i
)
72 if(!(*i
)->contains(p
, w
, mnightvision
)) continue;
73 if ( (*i
)->imp()->inherits( PointImp::stype() ) ) ret
.push_back( *i
);
75 if ( !(*i
)->imp()->inherits( PolygonImp::stype() ) ) curves
.push_back( *i
);
76 else fatobjects
.push_back( *i
);
78 std::copy( curves
.begin(), curves
.end(), std::back_inserter( ret
) );
79 std::copy( fatobjects
.begin(), fatobjects
.end(), std::back_inserter( ret
) );
83 std::vector
<ObjectHolder
*> KigDocument::whatIsInHere( const Rect
& p
, const KigWidget
& w
)
85 std::vector
<ObjectHolder
*> ret
;
86 std::vector
<ObjectHolder
*> nonpoints
;
87 for ( std::set
<ObjectHolder
*>::const_iterator i
= mobjects
.begin();
88 i
!= mobjects
.end(); ++i
)
90 if(! (*i
)->inRect( p
, w
) ) continue;
91 if ( (*i
)->imp()->inherits( PointImp::stype() ) ) ret
.push_back( *i
);
92 else nonpoints
.push_back( *i
);
94 std::copy( nonpoints
.begin(), nonpoints
.end(), std::back_inserter( ret
) );
98 Rect
KigDocument::suggestedRect() const
100 bool rectInited
= false;
102 for ( std::set
<ObjectHolder
*>::const_iterator i
= mobjects
.begin();
103 i
!= mobjects
.end(); ++i
)
107 Rect cr
= (*i
)->imp()->surroundingRect();
108 if ( ! cr
.valid() ) continue;
120 return Rect( -5.5, -5.5, 11., 11. );
121 r
.setContains( Coordinate( 0, 0 ) );
122 if( r
.width() == 0 ) r
.setWidth( 1 );
123 if( r
.height() == 0 ) r
.setHeight( 1 );
124 Coordinate center
= r
.center();
130 void KigDocument::addObject( ObjectHolder
* o
)
132 mobjects
.insert( o
);
135 void KigDocument::addObjects( const std::vector
<ObjectHolder
*>& os
)
137 for ( std::vector
<ObjectHolder
*>::const_iterator i
= os
.begin();
139 ( *i
)->calc( *this );
140 std::copy( os
.begin(), os
.end(), std::inserter( mobjects
, mobjects
.begin() ) );
143 void KigDocument::delObject( ObjectHolder
* o
)
148 void KigDocument::delObjects( const std::vector
<ObjectHolder
*>& os
)
150 for ( std::vector
<ObjectHolder
*>::const_iterator i
= os
.begin();
152 mobjects
.erase( *i
);
155 KigDocument::KigDocument()
156 : mcoordsystem( new EuclideanCoords
)
160 mnightvision
= false;
163 KigDocument::~KigDocument()
165 typedef std::set
<ObjectHolder
*> s
;
166 for ( s::iterator i
= mobjects
.begin(); i
!= mobjects
.end(); ++i
) {
172 void KigDocument::setGrid( bool showgrid
)
174 mshowgrid
= showgrid
;
177 const bool KigDocument::grid() const
182 void KigDocument::setAxes( bool showaxes
)
184 mshowaxes
= showaxes
;
187 void KigDocument::setNightVision( bool nv
)
192 const bool KigDocument::axes() const
197 const bool KigDocument::getNightVision() const