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

kig

  • sources
  • kde-4.14
  • kdeedu
  • kig
  • filters
pgfexporterimpvisitor.cc
Go to the documentation of this file.
1 // Copyright (C) 2004 Pino Toscano <toscano.pino@tiscali.it>
2 // Copyright (C) 2012 Raoul Bourquin
3 
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 // 02110-1301, USA.
18 
19 #include "pgfexporterimpvisitor.h"
20 
21 #include "../misc/goniometry.h"
22 #include "../objects/circle_imp.h"
23 #include "../objects/cubic_imp.h"
24 #include "../objects/bezier_imp.h"
25 #include "../objects/curve_imp.h"
26 #include "../objects/line_imp.h"
27 #include "../objects/locus_imp.h"
28 #include "../objects/object_drawer.h"
29 #include "../objects/object_holder.h"
30 #include "../objects/object_imp.h"
31 #include "../objects/other_imp.h"
32 #include "../objects/point_imp.h"
33 #include "../objects/polygon_imp.h"
34 #include "../objects/text_imp.h"
35 
36 
37 void PGFExporterImpVisitor::newLine()
38 {
39  mstream << ";\n";
40 }
41 
42 
43 QString PGFExporterImpVisitor::emitPenColor( const QColor& c )
44 {
45  QString pencolor("");
46  pencolor = "color={rgb,255:red," + QString::number(c.red()) + ";green," + QString::number(c.green()) + ";blue," + QString::number(c.blue()) + "}";
47  return pencolor;
48 }
49 
50 
51 QString PGFExporterImpVisitor::emitPenStyle( const Qt::PenStyle& style )
52 {
53  QString penstyle("line style=");
54  // PGF/TikZ definition of pen (line) style
55  // TODO: finetune styles
56  if ( style == Qt::SolidLine ) {
57  penstyle = "solid";
58  } else if ( style == Qt::DashLine ) {
59  penstyle = "dashed";
60  } else if ( style == Qt::DotLine ) {
61  penstyle = "dotted,dotsep=2pt";
62  } else if ( style == Qt::DashDotLine ) {
63  penstyle = "solid";
64  } else if ( style == Qt::DashDotDotLine ) {
65  penstyle = "solid";
66  }
67  return penstyle;
68 }
69 
70 
71 QString PGFExporterImpVisitor::emitPenSize( const int width )
72 {
73  // In this function we map the logical (integer) linewidth of Kig
74  // to real line widths that can be used in PGF/TikZ.
75  QString pensize("");
76  if ( width < 0 )
77  {
78  // Nothing specified, use TikZ default
79  pensize = "line width=1pt";
80  }
81  else
82  {
83  // TikZ definition of pen size
84  pensize = "line width=" + QString::number(width / 2.0) + "pt";
85  }
86  return pensize;
87 }
88 
89 
90 QString PGFExporterImpVisitor::emitPen( const QColor& c, const int width, const Qt::PenStyle& style )
91 {
92  QString pen("");
93  // TikZ definition of a pen
94  pen = emitPenColor(c) + ", " + emitPenSize(width) + ", " + emitPenStyle(style);
95  return pen;
96 }
97 
98 
99 QString PGFExporterImpVisitor::emitStyle( const ObjectDrawer* od )
100 {
101  int width = od->width();
102  if ( width == -1 ) width = 1;
103 
104  // TODO: finetune line width
105 
106  QString style("");
107  style = emitPen( od->color(), width, od->style() );
108  return style;
109 }
110 
111 
112 QString PGFExporterImpVisitor::emitCoord( const Coordinate& c )
113 {
114  QString ret("");
115  ret = "(" + QString::number(c.x) + "," + QString::number(c.y) + ")";
116  return ret;
117 }
118 
119 
120 void PGFExporterImpVisitor::emitLine( const Coordinate& a, const Coordinate& b,
121  const ObjectDrawer* od, bool vector )
122 {
123  if (vector)
124  {
125  mstream << "\\draw[" << emitStyle(od) << ", ->]";
126  }
127  else
128  {
129  mstream << "\\draw[" << emitStyle(od) << "]";
130  }
131  mstream << " " << emitCoord(a)
132  << " -- "
133  << emitCoord(b);
134  newLine();
135 }
136 
137 
138 void PGFExporterImpVisitor::plotGenericCurve( const CurveImp* imp )
139 {
140  std::vector< std::vector< Coordinate > > coordlist;
141  coordlist.push_back( std::vector< Coordinate >() );
142  uint curid = 0;
143 
144  Coordinate c;
145  Coordinate prev = Coordinate::invalidCoord();
146  for ( double i = 0.0; i <= 1.0; i += 0.0001 )
147  {
148  c = imp->getPoint( i, mw.document() );
149  if ( !c.valid() )
150  {
151  if ( coordlist[curid].size() > 0 )
152  {
153  coordlist.push_back( std::vector< Coordinate >() );
154  ++curid;
155  prev = Coordinate::invalidCoord();
156  }
157  continue;
158  }
159  if ( ! ( ( fabs( c.x ) <= 10000 ) && ( fabs( c.y ) <= 10000 ) ) )
160  continue;
161  // if there's too much distance between this coordinate and the previous
162  // one, then it's another piece of curve not joined with the rest
163  if ( prev.valid() && ( c.distance( prev ) > 50.0 ) )
164  {
165  coordlist.push_back( std::vector< Coordinate >() );
166  ++curid;
167  }
168  coordlist[curid].push_back( c );
169  prev = c;
170  }
171 
172  for ( uint i = 0; i < coordlist.size(); ++i )
173  {
174  uint s = coordlist[i].size();
175  // there's no point in draw curves empty or with only one point
176  if ( s <= 1 )
177  continue;
178 
179  QString tmp = "\\draw [" + emitStyle( mcurobj->drawer() ) + ", /pgf/fpu,/pgf/fpu/output format=fixed ] ";
180  mstream << tmp;
181 
182  uint linelength = tmp.length();
183 
184  for ( uint j = 0; j < s; ++j )
185  {
186  tmp = emitCoord( coordlist[i][j] );
187  // Avoid too long lines in the output file
188  if(linelength + tmp.length() > maxlinelength)
189  {
190  linelength = tmp.length();
191  mstream << "\n";
192  }
193  else
194  {
195  linelength += tmp.length();
196  }
197  mstream << tmp;
198 
199  if ( j < s-1 )
200  {
201  linelength += 4;
202  mstream << " -- ";
203  }
204  else
205  {
206  newLine();
207  linelength = 0;
208  }
209  }
210  newLine();
211  }
212 }
213 
214 
215 void PGFExporterImpVisitor::visit( ObjectHolder* obj )
216 {
217  mstream << "%% " << obj->imp()->type()->translatedName();
218  newLine();
219  if ( ! obj->drawer()->shown() )
220  return;
221  mcurobj = obj;
222  obj->imp()->visit( this );
223 }
224 
225 
226 void PGFExporterImpVisitor::visit( const LineImp* imp )
227 {
228  Coordinate a = imp->data().a;
229  Coordinate b = imp->data().b;
230  calcBorderPoints( a, b, msr );
231 
232  emitLine( a, b, mcurobj->drawer(), false );
233 }
234 
235 
236 void PGFExporterImpVisitor::visit( const PointImp* imp )
237 {
238  float width = mcurobj->drawer()->width();
239  // Some magic numbers that result in good point sizes
240  if ( width == -1) { width = 2.5; }
241  else { width /= 2.5; }
242 
243  mstream << "\\filldraw [" << emitPenColor(mcurobj->drawer()->color()) << "] "
244  << emitCoord (imp->coordinate())
245  << " circle (" << width << "pt )";
246  newLine();
247 }
248 
249 
250 void PGFExporterImpVisitor::visit( const TextImp* imp )
251 {
252  mstream << "\\node ";
253  if (imp->hasFrame())
254  {
255  mstream << "[rectangle,draw] ";
256  }
257  mstream << "at "
258  << emitCoord(imp->coordinate())
259  << " {" << imp->text() << "}";
260  newLine();
261 }
262 
263 
264 void PGFExporterImpVisitor::visit( const AngleImp* imp )
265 {
266  double start = Goniometry::convert( imp->startAngle(), Goniometry::Rad, Goniometry::Deg );
267  double end = Goniometry::convert( imp->startAngle() + imp->angle(), Goniometry::Rad, Goniometry::Deg );
268  double radius = 0.75;
269  mstream << "\\draw [" << emitStyle( mcurobj->drawer() ) << ",->] "
270  << emitCoord(imp->point())
271  << " +(" << start << ":" << radius << ")"
272  << " arc (" << start << ":" << end << ":" << radius << ")";
273  newLine();
274 }
275 
276 
277 void PGFExporterImpVisitor::visit( const VectorImp* imp )
278 {
279  Coordinate a = imp->data().a;
280  Coordinate b = imp->data().b;
281 
282  emitLine( a, b, mcurobj->drawer(), true );
283 }
284 
285 
286 void PGFExporterImpVisitor::visit( const LocusImp* imp )
287 {
288  plotGenericCurve( imp );
289 }
290 
291 
292 void PGFExporterImpVisitor::visit( const CircleImp* imp )
293 {
294  mstream << "\\draw [" << emitStyle( mcurobj->drawer() ) << "] "
295  << emitCoord( imp->center() )
296  << " circle (" << imp->radius() << ")";
297  newLine();
298 }
299 
300 
301 void PGFExporterImpVisitor::visit( const ConicImp* imp )
302 {
303  plotGenericCurve(imp);
304 }
305 
306 
307 void PGFExporterImpVisitor::visit( const CubicImp* imp )
308 {
309  plotGenericCurve(imp);
310 }
311 
312 
313 void PGFExporterImpVisitor::visit( const SegmentImp* imp )
314 {
315  Coordinate a = imp->data().a;
316  Coordinate b = imp->data().b;
317 
318  emitLine( a, b, mcurobj->drawer(), false );
319 }
320 
321 
322 void PGFExporterImpVisitor::visit( const RayImp* imp )
323 {
324  Coordinate a = imp->data().a;
325  Coordinate b = imp->data().b;
326  calcRayBorderPoints( a, b, msr );
327 
328  emitLine( a, b, mcurobj->drawer(), false );
329 }
330 
331 
332 void PGFExporterImpVisitor::visit( const ArcImp* imp )
333 {
334  double start = Goniometry::convert( imp->startAngle(), Goniometry::Rad, Goniometry::Deg );
335  double end = Goniometry::convert( imp->startAngle() + imp->angle(), Goniometry::Rad, Goniometry::Deg );
336  double radius = imp->radius();
337  mstream << "\\draw [" << emitStyle( mcurobj->drawer() ) << "] "
338  << emitCoord(imp->center())
339  << " +(" << start << ":" << radius << ")"
340  << " arc (" << start << ":" << end << ":" << radius << ")";
341  newLine();
342 }
343 
344 
345 void PGFExporterImpVisitor::visit( const FilledPolygonImp* imp )
346 {
347  mstream << "\\filldraw [" << emitStyle( mcurobj->drawer() ) << "] ";
348 
349  std::vector<Coordinate> pts = imp->points();
350  for ( uint i = 0; i < pts.size(); i++ )
351  {
352  mstream << emitCoord( pts[i] );
353  mstream << " -- ";
354  }
355  mstream << "cycle";
356  newLine();
357 }
358 
359 
360 void PGFExporterImpVisitor::visit ( const ClosedPolygonalImp* imp )
361 {
362  mstream << "\\draw [" << emitStyle( mcurobj->drawer() ) << "] ";
363 
364  std::vector<Coordinate> pts = imp->points();
365  for ( uint i = 0; i < pts.size(); i++ )
366  {
367  mstream << emitCoord( pts[i] );
368  mstream << " -- ";
369  }
370  mstream << "cycle";
371  newLine();
372 }
373 
374 
375 void PGFExporterImpVisitor::visit ( const OpenPolygonalImp* imp )
376 {
377  mstream << "\\draw [" << emitStyle( mcurobj->drawer() ) << "] ";
378 
379  std::vector<Coordinate> pts = imp->points();
380  for ( uint i = 0; i < pts.size(); i++ )
381  {
382  mstream << emitCoord( pts[i] );
383  if (i < pts.size() - 1)
384  {
385  mstream << " -- ";
386  }
387  }
388  newLine();
389 }
390 
391 
392 void PGFExporterImpVisitor::visit(const BezierImp* imp)
393 {
394  std::vector<Coordinate> pts = imp->points();
395  switch (pts.size())
396  {
397  case 3:
398  // Formula for cubic control points
399  // CP1 = QP0 + 2/3 *(QP1-QP0)
400  // CP2 = CP1 + 1/3 *(QP2-QP0)
401  mstream << "\\draw [" << emitStyle( mcurobj->drawer() ) << "] "
402  << emitCoord(pts.at(0))
403  << ".. controls ($"
404  << emitCoord(pts.at(0))
405  << "+2/3*"
406  << emitCoord(pts.at(1))
407  << "-2/3*"
408  << emitCoord(pts.at(0))
409  << "$) and ($"
410  << emitCoord(pts.at(0))
411  << "+2/3*"
412  << emitCoord(pts.at(1))
413  << "-2/3*"
414  << emitCoord(pts.at(0))
415  << "+1/3*"
416  << emitCoord(pts.at(2))
417  << "-1/3*"
418  << emitCoord(pts.at(0))
419  << "$) .. "
420  << emitCoord(pts.at(2));
421  newLine();
422  break;
423  case 4:
424  mstream << "\\draw [" << emitStyle( mcurobj->drawer() ) << "] "
425  << emitCoord(pts.front())
426  << ".. controls "
427  << emitCoord(pts.at(1))
428  << " and "
429  << emitCoord(pts.at(2))
430  << " .. "
431  << emitCoord(pts.back());
432  newLine();
433  break;
434  default:
435  plotGenericCurve(imp);
436  break;
437  }
438 }
439 
440 
441 void PGFExporterImpVisitor::visit(const RationalBezierImp* imp)
442 {
443  plotGenericCurve(imp);
444 }
calcRayBorderPoints
void calcRayBorderPoints(const Coordinate &a, Coordinate &b, const Rect &r)
this does the same as the above function, but only for b.
Definition: common.cpp:131
ClosedPolygonalImp
An ObjectImp representing a closed polygonal.
Definition: polygon_imp.h:130
BezierImp
An ObjectImp representing polynomial Bézier Curve.
Definition: bezier_imp.h:31
CubicImp
An ObjectImp representing a cubic.
Definition: cubic_imp.h:29
LocusImp
LocusImp is an imp that consists of a copy of the curveimp that the moving point moves over...
Definition: locus_imp.h:57
AbstractLineImp::data
LineData data() const
Get the LineData for this AbstractLineImp.
Definition: line_imp.cc:359
VectorImp::data
LineData data() const
Get the LineData for this vector.
Definition: other_imp.cc:771
TextImp::coordinate
const Coordinate coordinate() const
Definition: text_imp.cc:125
OpenPolygonalImp
An ObjectImp representing an open polygonal.
Definition: polygon_imp.h:157
ObjectDrawer::shown
bool shown() const
returns whether the object this ObjectDrawer is responsible for will be drawn or not.
Definition: object_drawer.cc:52
AngleImp::point
const Coordinate point() const
Return the center of this angle.
Definition: other_imp.h:79
LineData::b
Coordinate b
Another point on the line.
Definition: misc/common.h:68
Goniometry::Deg
Definition: goniometry.h:31
CircleImp
An ObjectImp representing a circle.
Definition: circle_imp.h:27
ObjectImp::visit
virtual void visit(ObjectImpVisitor *vtor) const =0
TextImp
Definition: text_imp.h:26
RayImp
An ObjectImp representing a ray.
Definition: line_imp.h:136
RationalBezierImp
An ObjectImp representing a rational Bézier curve.
Definition: bezier_imp.h:100
CircleImp::center
const Coordinate center() const
Return the center of this circle.
Definition: circle_imp.cc:210
ObjectHolder::drawer
const ObjectDrawer * drawer() const
Definition: object_holder.cc:58
PointImp::coordinate
const Coordinate & coordinate() const
Get the coordinate of this PointImp.
Definition: point_imp.h:50
AbstractPolygonImp::points
const std::vector< Coordinate > points() const
Returns the vector with polygon points.
Definition: polygon_imp.cc:571
ArcImp::radius
double radius() const
Return the radius of this arc.
Definition: other_imp.cc:537
Coordinate
The Coordinate class is the basic class representing a 2D location by its x and y components...
Definition: coordinate.h:33
CurveImp::getPoint
virtual const Coordinate getPoint(double param, const KigDocument &) const =0
VectorImp
An ObjectImp representing a vector.
Definition: other_imp.h:99
QString::number
QString number(int n, int base)
ObjectImp::type
virtual const ObjectImpType * type() const =0
Returns the lowermost ObjectImpType that this object is an instantiation of.
ObjectImpType::translatedName
QString translatedName() const
The name of this type, translated to the currently used language.
Definition: object_imp.cc:240
QColor::red
int red() const
TextImp::text
QString text() const
Definition: text_imp.cc:115
ArcImp::angle
double angle() const
Return the dimension in radians of this arc.
Definition: other_imp.cc:547
ObjectHolder
An ObjectHolder represents an object as it is known to the document.
Definition: object_holder.h:40
PointImp
An ObjectImp representing a point.
Definition: point_imp.h:27
Coordinate::distance
double distance(const Coordinate &p) const
Distance to another Coordinate.
Definition: coordinate.cpp:139
pgfexporterimpvisitor.h
AngleImp::angle
double angle() const
Return the dimension in radians of this angle.
Definition: other_imp.h:87
LineData::a
Coordinate a
One point on the line.
Definition: misc/common.h:64
QString
QColor
KigWidget::document
const KigDocument & document() const
Definition: kig_view.cpp:460
ConicImp
An ObjectImp representing a conic.
Definition: conic_imp.h:39
Goniometry::convert
static double convert(const double angle, const Goniometry::System from, const Goniometry::System to)
The most useful method of this class: convert the specified angle from the system from to the system ...
Definition: goniometry.cc:87
QColor::green
int green() const
ObjectDrawer::width
int width() const
return the width of the object
Definition: object_drawer.cc:134
ArcImp::center
const Coordinate center() const
Return the center of this arc.
Definition: other_imp.cc:532
ObjectHolder::imp
const ObjectImp * imp() const
Definition: object_holder.cc:48
ArcImp::startAngle
double startAngle() const
Return the start angle in radians of this arc.
Definition: other_imp.cc:542
AngleImp::startAngle
double startAngle() const
Return the start angle in radians of this angle.
Definition: other_imp.h:83
Coordinate::invalidCoord
static Coordinate invalidCoord()
Create an invalid Coordinate.
Definition: coordinate.cpp:171
ObjectDrawer
A class holding some information about how a certain object is drawn on the window.
Definition: object_drawer.h:47
Goniometry::Rad
Definition: goniometry.h:31
QColor::blue
int blue() const
CircleImp::radius
double radius() const
Return the radius of this circle.
Definition: circle_imp.cc:215
calcBorderPoints
void calcBorderPoints(Coordinate &p1, Coordinate &p2, const Rect &r)
this sets p1 and p2 to p1' and p2' so that p1'p2' is the same line as p1p2, and so that p1' and p2' a...
Definition: common.cpp:82
PGFExporterImpVisitor::visit
void visit(ObjectHolder *obj)
Definition: pgfexporterimpvisitor.cc:215
LineImp
An ObjectImp representing a line.
Definition: line_imp.h:184
QString::length
int length() const
BezierImp::points
const std::vector< Coordinate > points() const
Returns the vector with control points.
Definition: bezier_imp.cc:189
ArcImp
An ObjectImp representing an arc.
Definition: other_imp.h:169
Coordinate::x
double x
X Component.
Definition: coordinate.h:126
ObjectDrawer::color
QColor color() const
returns the color that the object will be drawn in
Definition: object_drawer.cc:57
Coordinate::y
double y
Y Component.
Definition: coordinate.h:129
ObjectDrawer::style
Qt::PenStyle style() const
return PenStyle for all objects except points
Definition: object_drawer.cc:139
FilledPolygonImp
An ObjectImp representing a filled polygon.
Definition: polygon_imp.h:101
TextImp::hasFrame
bool hasFrame() const
Definition: text_imp.cc:138
uint
unsigned int uint
Definition: object_imp.h:87
Coordinate::valid
bool valid() const
Return whether this is a valid Coordinate.
Definition: coordinate.cpp:176
AngleImp
An ObjectImp representing an angle.
Definition: other_imp.h:28
CurveImp
This class represents a curve: something which is composed of points, like a line, a circle, a locus.
Definition: curve_imp.h:27
SegmentImp
An ObjectImp representing a segment.
Definition: line_imp.h:81
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:12:05 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
  • 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