moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kig / kig / kig_document.cc
blob16082397b40a41fba62e280c066d8291fd9ad3bb
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
16 // 02111-1307, USA.
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"
27 #include <assert.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 );
39 return *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
49 return mobjects;
52 void KigDocument::setCoordinateSystem( CoordinateSystem* s )
54 delete switchCoordinateSystem( s );
57 CoordinateSystem* KigDocument::switchCoordinateSystem( CoordinateSystem* s )
59 CoordinateSystem* ret = mcoordsystem;
60 mcoordsystem = s;
61 return ret;
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 );
74 else
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 ) );
80 return 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 ) );
95 return ret;
98 Rect KigDocument::suggestedRect() const
100 bool rectInited = false;
101 Rect r(0.,0.,0.,0.);
102 for ( std::set<ObjectHolder*>::const_iterator i = mobjects.begin();
103 i != mobjects.end(); ++i )
105 if ( (*i)->shown() )
107 Rect cr = (*i)->imp()->surroundingRect();
108 if ( ! cr.valid() ) continue;
109 if( !rectInited )
111 r = cr;
112 rectInited = true;
114 else
115 r.eat( cr );
119 if ( ! rectInited )
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();
125 r *= 2;
126 r.setCenter(center);
127 return r;
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();
138 i != os.end(); ++i )
139 ( *i )->calc( *this );
140 std::copy( os.begin(), os.end(), std::inserter( mobjects, mobjects.begin() ) );
143 void KigDocument::delObject( ObjectHolder* o )
145 mobjects.erase( o );
148 void KigDocument::delObjects( const std::vector<ObjectHolder*>& os )
150 for ( std::vector<ObjectHolder*>::const_iterator i = os.begin();
151 i != os.end(); ++i )
152 mobjects.erase( *i );
155 KigDocument::KigDocument()
156 : mcoordsystem( new EuclideanCoords )
158 mshowgrid = true;
159 mshowaxes = true;
160 mnightvision = false;
163 KigDocument::~KigDocument()
165 typedef std::set<ObjectHolder*> s;
166 for ( s::iterator i = mobjects.begin(); i != mobjects.end(); ++i ) {
167 delete *i;
169 delete mcoordsystem;
172 void KigDocument::setGrid( bool showgrid )
174 mshowgrid = showgrid;
177 const bool KigDocument::grid() const
179 return mshowgrid;
182 void KigDocument::setAxes( bool showaxes )
184 mshowaxes = showaxes;
187 void KigDocument::setNightVision( bool nv )
189 mnightvision = nv;
192 const bool KigDocument::axes() const
194 return mshowaxes;
197 const bool KigDocument::getNightVision() const
199 return mnightvision;