• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

kig

  • sources
  • kde-4.12
  • kdeedu
  • kig
  • kig
kig_document.cc
Go to the documentation of this file.
1 // Copyright (C) 2004 Dominique Devriese <devriese@kde.org>
2 
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.
7 
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.
12 
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., 51 Franklin Street, Fifth Floor, Boston, MA
16 // 02110-1301, USA.
17 
18 #include "kig_document.h"
19 
20 #include "../misc/common.h"
21 #include "../objects/object_calcer.h"
22 #include "../objects/object_holder.h"
23 #include "../objects/point_imp.h"
24 #include "../objects/polygon_imp.h"
25 #include "../misc/coordinate_system.h"
26 #include "../misc/rect.h"
27 #include "../misc/calcpaths.h"
28 
29 #include <assert.h>
30 #include <iterator>
31 #include <cmath>
32 
33 KigDocument::KigDocument( std::set<ObjectHolder*> objects, CoordinateSystem* coordsystem,
34  bool showgrid, bool showaxes, bool nv )
35  : mobjects( objects ), mcoordsystem( coordsystem ), mshowgrid( showgrid ),
36  mshowaxes( showaxes ), mnightvision( nv ), mcoordinatePrecision( -1 ), mcachedparam( 0.0 )
37 {
38 }
39 
40 const CoordinateSystem& KigDocument::coordinateSystem() const
41 {
42  assert( mcoordsystem );
43  return *mcoordsystem;
44 }
45 
46 const std::vector<ObjectHolder*> KigDocument::objects() const
47 {
48  return std::vector<ObjectHolder*>( mobjects.begin(), mobjects.end() );
49 }
50 
51 const std::set<ObjectHolder*>& KigDocument::objectsSet() const
52 {
53  return mobjects;
54 }
55 
56 void KigDocument::setCoordinateSystem( CoordinateSystem* s )
57 {
58  delete switchCoordinateSystem( s );
59 }
60 
61 CoordinateSystem* KigDocument::switchCoordinateSystem( CoordinateSystem* s )
62 {
63  CoordinateSystem* ret = mcoordsystem;
64  mcoordsystem = s;
65  return ret;
66 }
67 
68 std::vector<ObjectHolder*> KigDocument::whatAmIOn( const Coordinate& p, const KigWidget& w ) const
69 {
70  std::vector<ObjectHolder*> ret;
71  std::vector<ObjectHolder*> curves;
72  std::vector<ObjectHolder*> fatobjects;
73  for ( std::set<ObjectHolder*>::const_iterator i = mobjects.begin();
74  i != mobjects.end(); ++i )
75  {
76  if(!(*i)->contains(p, w, mnightvision)) continue;
77  const ObjectImp* oimp = (*i)->imp();
78  if ( oimp->inherits( PointImp::stype() ) ) ret.push_back( *i );
79  else
80  if ( !oimp->inherits( FilledPolygonImp::stype() ) ) curves.push_back( *i );
81  else
82  {
83  fatobjects.push_back( *i );
84  }
85  };
86  std::copy( curves.begin(), curves.end(), std::back_inserter( ret ) );
87  std::copy( fatobjects.begin(), fatobjects.end(), std::back_inserter( ret ) );
88  return ret;
89 }
90 
91 std::vector<ObjectHolder*> KigDocument::whatIsInHere( const Rect& p, const KigWidget& w )
92 {
93  std::vector<ObjectHolder*> ret;
94  std::vector<ObjectHolder*> nonpoints;
95  for ( std::set<ObjectHolder*>::const_iterator i = mobjects.begin();
96  i != mobjects.end(); ++i )
97  {
98  if(! (*i)->inRect( p, w ) ) continue;
99  if ( (*i)->imp()->inherits( PointImp::stype() ) ) ret.push_back( *i );
100  else nonpoints.push_back( *i );
101  };
102  std::copy( nonpoints.begin(), nonpoints.end(), std::back_inserter( ret ) );
103  return ret;
104 }
105 
106 Rect KigDocument::suggestedRect() const
107 {
108  bool rectInited = false;
109  Rect r(0.,0.,0.,0.);
110  for ( std::set<ObjectHolder*>::const_iterator i = mobjects.begin();
111  i != mobjects.end(); ++i )
112  {
113  if ( (*i)->shown() )
114  {
115  Rect cr = (*i)->imp()->surroundingRect();
116  if ( ! cr.valid() ) continue;
117  if( !rectInited )
118  {
119  r = cr;
120  rectInited = true;
121  }
122  else
123  r.eat( cr );
124  };
125  };
126 
127  if ( ! rectInited )
128  return Rect( -5.5, -5.5, 11., 11. );
129  r.setContains( Coordinate( 0, 0 ) );
130  if( r.width() == 0 ) r.setWidth( 1 );
131  if( r.height() == 0 ) r.setHeight( 1 );
132  Coordinate center = r.center();
133  r *= 2;
134  r.setCenter(center);
135  return r;
136 }
137 
138 void KigDocument::addObject( ObjectHolder* o )
139 {
140  mobjects.insert( o );
141 }
142 
143 void KigDocument::addObjects( const std::vector<ObjectHolder*>& os )
144 {
145  for ( std::vector<ObjectHolder*>::const_iterator i = os.begin();
146  i != os.end(); ++i )
147  ( *i )->calc( *this );
148  std::copy( os.begin(), os.end(), std::inserter( mobjects, mobjects.begin() ) );
149 }
150 
151 void KigDocument::delObject( ObjectHolder* o )
152 {
153  mobjects.erase( o );
154 }
155 
156 void KigDocument::delObjects( const std::vector<ObjectHolder*>& os )
157 {
158  for ( std::vector<ObjectHolder*>::const_iterator i = os.begin();
159  i != os.end(); ++i )
160  mobjects.erase( *i );
161 }
162 
163 KigDocument::KigDocument()
164  : mcoordsystem( new EuclideanCoords )
165 {
166  mshowgrid = true;
167  mshowaxes = true;
168  mnightvision = false;
169  mcoordinatePrecision = -1;
170 }
171 
172 KigDocument::~KigDocument()
173 {
174  typedef std::set<ObjectHolder*> s;
175  for ( s::iterator i = mobjects.begin(); i != mobjects.end(); ++i ) {
176  delete *i;
177  }
178  delete mcoordsystem;
179 }
180 
181 void KigDocument::setGrid( bool showgrid )
182 {
183  mshowgrid = showgrid;
184 }
185 
186 bool KigDocument::grid() const
187 {
188  return mshowgrid;
189 }
190 
191 void KigDocument::setAxes( bool showaxes )
192 {
193  mshowaxes = showaxes;
194 }
195 
196 void KigDocument::setNightVision( bool nv )
197 {
198  mnightvision = nv;
199 }
200 
201 void KigDocument::setCoordinatePrecision( int precision )
202 {
203  mcoordinatePrecision = precision;
204 }
205 
206 bool KigDocument::axes() const
207 {
208  return mshowaxes;
209 }
210 
211 bool KigDocument::getNightVision() const
212 {
213  return mnightvision;
214 }
215 
216 bool KigDocument::isUserSpecifiedCoordinatePrecision() const
217 {
218  return mcoordinatePrecision != -1;
219 }
220 
221 int KigDocument::getCoordinatePrecision() const
222 {
223  if( mcoordinatePrecision == -1 )
224  {
225  // we use default coordinate precision calculation
226  Rect sr = suggestedRect();
227  double m = kigMax( sr.width(), sr.height() );
228 
229  return kigMax( 0, (int) ( 3 - log10( m ) ) );
230  }
231 
232  return mcoordinatePrecision;
233 }
234 
235 /*
236  * scan all points in the document and find those that simultaneously
237  * belong to two curves c1 and c2. This is required when the user
238  * asks for intersections between a conic and a line (or two circles)
239  * and one of the intersections is already present. In this case
240  * we construct the "other" intersection instead of the two standard
241  * intersections. Note that this is a completely different construction
242  * which takes into account the known intersection in order to reduce
243  * the resolvant equation to first degree, thus ensuring that the newly
244  * constructed point is *always* the other intersection
245  */
246 
247 std::vector<ObjectCalcer*>
248 KigDocument::findIntersectionPoints( const ObjectCalcer* c1,
249  const ObjectCalcer* c2) const
250 {
251  std::vector<ObjectCalcer*> ret;
252  for ( std::set<ObjectHolder*>::const_iterator i = mobjects.begin();
253  i != mobjects.end(); ++i )
254  {
255  if ( !(*i)->imp()->inherits( PointImp::stype() ) ) continue;
256  ObjectCalcer* o = (*i)->calcer();
257  if ( isPointOnCurve( o, c1 ) &&
258  isPointOnCurve( o, c2 ) )
259  {
260  ret.push_back( o );
261  }
262  };
263 
264  return ret;
265 }
ObjectImp::inherits
bool inherits(const ObjectImpType *t) const
Returns true if this ObjectImp inherits the ObjectImp type represented by t.
Definition: object_imp.cc:279
CoordinateSystem
a CoordinateSystem is what the user sees: it is kept by KigPart to show the user a grid...
Definition: coordinate_system.h:60
KigDocument::whatIsInHere
std::vector< ObjectHolder * > whatIsInHere(const Rect &p, const KigWidget &)
Return a vector of objects that are in the given Rect.
Definition: kig_document.cc:91
KigDocument::~KigDocument
~KigDocument()
Definition: kig_document.cc:172
Rect::width
double width() const
Definition: rect.cc:204
KigDocument::delObject
void delObject(ObjectHolder *o)
Remove the object o from the document.
Definition: kig_document.cc:151
Rect::valid
bool valid()
Definition: rect.cc:300
KigDocument::grid
bool grid() const
Definition: kig_document.cc:186
KigDocument::objectsSet
const std::set< ObjectHolder * > & objectsSet() const
Definition: kig_document.cc:51
Rect
This file is part of Kig, a KDE program for Interactive Geometry...
Definition: rect.h:34
Rect::eat
void eat(const Rect &r)
This expands the rect so that it contains r.
Definition: rect.cc:274
KigDocument::getNightVision
bool getNightVision() const
Definition: kig_document.cc:211
KigDocument::addObjects
void addObjects(const std::vector< ObjectHolder * > &os)
Add the objects os to the document.
Definition: kig_document.cc:143
Rect::setHeight
void setHeight(const double h)
Definition: rect.cc:125
Coordinate
The Coordinate class is the basic class representing a 2D location by its x and y components...
Definition: coordinate.h:33
KigDocument::getCoordinatePrecision
int getCoordinatePrecision() const
Definition: kig_document.cc:221
KigDocument::setCoordinateSystem
void setCoordinateSystem(CoordinateSystem *s)
sets the coordinate system to s , and deletes the old one.
Definition: kig_document.cc:56
KigDocument::setGrid
void setGrid(bool showgrid)
set to show/hide the grid.
Definition: kig_document.cc:181
EuclideanCoords
Definition: coordinate_system.h:91
ObjectHolder
An ObjectHolder represents an object as it is known to the document.
Definition: object_holder.h:40
ObjectCalcer
An ObjectCalcer is an object that represents an algorithm for calculating an ObjectImp from other Obj...
Definition: object_calcer.h:66
isPointOnCurve
bool isPointOnCurve(const ObjectCalcer *point, const ObjectCalcer *curve)
Return true if the given point is ( by construction ) on the given curve.
Definition: calcpaths.cc:309
kigMax
T kigMax(const T &a, const T &b)
Definition: misc/common.h:261
Rect::setCenter
void setCenter(const Coordinate p)
Definition: rect.cc:91
KigDocument::whatAmIOn
std::vector< ObjectHolder * > whatAmIOn(const Coordinate &p, const KigWidget &w) const
Return a vector of objects that contain the given point.
Definition: kig_document.cc:68
Rect::center
Coordinate center() const
Definition: rect.cc:181
KigWidget
This class is the real widget showing the document.
Definition: kig_view.h:50
FilledPolygonImp::stype
static const ObjectImpType * stype()
Definition: polygon_imp.cc:660
KigDocument::objects
const std::vector< ObjectHolder * > objects() const
Get a hold of the objects of this KigDocument.
Definition: kig_document.cc:46
Rect::setContains
void setContains(Coordinate p)
this makes sure p is in the rect, extending it if necessary...
Definition: rect.cc:240
Rect::setWidth
void setWidth(const double w)
Definition: rect.cc:120
kig_document.h
KigDocument::KigDocument
KigDocument()
Definition: kig_document.cc:163
KigDocument::setAxes
void setAxes(bool showaxes)
set to show/hide the grid.
Definition: kig_document.cc:191
KigDocument::setCoordinatePrecision
void setCoordinatePrecision(int precision)
Definition: kig_document.cc:201
PointImp::stype
static const ObjectImpType * stype()
Returns the ObjectImpType representing PointImp's.
Definition: point_imp.cc:159
Rect::height
double height() const
Definition: rect.cc:209
KigDocument::findIntersectionPoints
std::vector< ObjectCalcer * > findIntersectionPoints(const ObjectCalcer *c1, const ObjectCalcer *c2) const
Return all the points that belong (by construction) on both the given curves.
Definition: kig_document.cc:248
KigDocument::switchCoordinateSystem
CoordinateSystem * switchCoordinateSystem(CoordinateSystem *s)
sets the coordinate system to s , and returns the old one.
Definition: kig_document.cc:61
KigDocument::addObject
void addObject(ObjectHolder *oObject)
Add the objects o to the document.
Definition: kig_document.cc:138
KigDocument::setNightVision
void setNightVision(bool nv)
set to enable/disable night-vision (visibility of hidden objects)
Definition: kig_document.cc:196
ObjectImp
The ObjectImp class represents the behaviour of an object after it is calculated. ...
Definition: object_imp.h:226
KigDocument::axes
bool axes() const
Definition: kig_document.cc:206
KigDocument::delObjects
void delObjects(const std::vector< ObjectHolder * > &os)
Remove the objects os from the document.
Definition: kig_document.cc:156
KigDocument::coordinateSystem
const CoordinateSystem & coordinateSystem() const
Definition: kig_document.cc:40
KigDocument::isUserSpecifiedCoordinatePrecision
bool isUserSpecifiedCoordinatePrecision() const
Definition: kig_document.cc:216
KigDocument::suggestedRect
Rect suggestedRect() const
Return a rect containing most of the objects, which would be a fine suggestion to map to the widget...
Definition: kig_document.cc:106
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:35:39 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kig

Skip menu "kig"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal