1 // SPDX-FileCopyrightText: 2002 Dominique Devriese <devriese@kde.org>
2 
3 // SPDX-License-Identifier: GPL-2.0-or-later
4 
5 #include "line_type.h"
6 
7 #include "bogus_imp.h"
8 #include "line_imp.h"
9 #include "object_holder.h"
10 #include "other_imp.h"
11 #include "point_imp.h"
12 
13 #include "../kig/kig_view.h"
14 #include "../kig/kig_part.h"
15 #include "../kig/kig_commands.h"
16 #include "../misc/common.h"
17 #include "../misc/calcpaths.h"
18 
19 #include <QStringList>
20 
21 static const ArgsParser::spec argsspecSegmentAB[] =
22 {
23   { PointImp::stype(), I18N_NOOP( "Construct a segment starting at this point" ),
24     I18N_NOOP( "Select the start point of the new segment..." ), true },
25   { PointImp::stype(), I18N_NOOP( "Construct a segment ending at this point" ),
26     I18N_NOOP( "Select the end point of the new segment..." ), true }
27 };
28 
KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE(SegmentABType)29 KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( SegmentABType )
30 
31 SegmentABType::SegmentABType()
32   : ObjectABType( "SegmentAB", argsspecSegmentAB, 2 )
33 {
34 }
35 
~SegmentABType()36 SegmentABType::~SegmentABType()
37 {
38 }
39 
instance()40 const SegmentABType* SegmentABType::instance()
41 {
42   static const SegmentABType s;
43   return &s;
44 }
45 
calcx(const Coordinate & a,const Coordinate & b) const46 ObjectImp* SegmentABType::calcx( const Coordinate& a, const Coordinate& b ) const
47 {
48   return new SegmentImp( a, b );
49 }
50 
51 static const char constructlineabstat[] = I18N_NOOP( "Construct a line through this point" );
52 
53 static const ArgsParser::spec argsspecLineAB[] =
54 {
55   { PointImp::stype(), constructlineabstat,
56     I18N_NOOP( "Select a point for the line to go through..." ), true },
57   { PointImp::stype(), constructlineabstat,
58     I18N_NOOP( "Select another point for the line to go through..." ), true }
59 };
60 
KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE(LineABType)61 KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( LineABType )
62 
63 LineABType::LineABType()
64   : ObjectABType( "LineAB", argsspecLineAB, 2 )
65 {
66 }
67 
~LineABType()68 LineABType::~LineABType()
69 {
70 }
71 
instance()72 const LineABType* LineABType::instance()
73 {
74   static const LineABType s;
75   return &s;
76 }
77 
calcx(const Coordinate & a,const Coordinate & b) const78 ObjectImp* LineABType::calcx( const Coordinate& a, const Coordinate& b ) const
79 {
80   return new LineImp( a, b );
81 }
82 
83 static const char constructhalflinestartingstat[] = I18N_NOOP( "Construct a half-line starting at this point" );
84 
85 static const ArgsParser::spec argsspecRayAB[] =
86 {
87   { PointImp::stype(), constructhalflinestartingstat,
88     I18N_NOOP( "Select the start point of the new half-line..." ), true },
89   { PointImp::stype(), I18N_NOOP( "Construct a half-line through this point" ),
90     I18N_NOOP( "Select a point for the half-line to go through..." ), true }
91 };
92 
KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE(RayABType)93 KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( RayABType )
94 
95 RayABType::RayABType()
96   : ObjectABType( "RayAB", argsspecRayAB, 2 )
97 {
98 }
99 
~RayABType()100 RayABType::~RayABType()
101 {
102 }
103 
instance()104 const RayABType* RayABType::instance()
105 {
106   static const RayABType s;
107   return &s;
108 }
109 
calcx(const Coordinate & a,const Coordinate & b) const110 ObjectImp* RayABType::calcx( const Coordinate& a, const Coordinate& b ) const
111 {
112   return new RayImp( a, b );
113 }
114 
115 static const ArgsParser::spec argspecSegmentAxisABType[] =
116 {
117   { SegmentImp::stype(), I18N_NOOP( "Construct the axis of this segment" ),
118     I18N_NOOP( "Select the segment of which you want to draw the axis..." ), true }
119 };
120 
121 KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( SegmentAxisType );
122 
SegmentAxisType()123 SegmentAxisType::SegmentAxisType()
124   : ArgsParserObjectType( "Segment Axis", argspecSegmentAxisABType, 1 )
125 {
126 }
127 
~SegmentAxisType()128 SegmentAxisType::~SegmentAxisType()
129 {
130 }
131 
instance()132 const SegmentAxisType* SegmentAxisType::instance()
133 {
134   static const SegmentAxisType s;
135   return &s;
136 }
137 
calc(const Args & args,const KigDocument &) const138 ObjectImp* SegmentAxisType::calc( const Args& args, const KigDocument& ) const
139 {
140   if ( ! margsparser.checkArgs( args ) ) return new InvalidImp;
141 
142   const SegmentImp * l = static_cast< const SegmentImp * >( args[0] );
143   const Coordinate a = l->data().a;
144   const Coordinate b = l->data().b;
145   const Coordinate mp = ( a + b ) / 2;
146   const Coordinate dir( b - a );
147   const Coordinate perpPoint = calcPointOnPerpend( dir, mp );
148   return new LineImp( mp, perpPoint  );
149 }
150 
resultId() const151 const ObjectImpType* SegmentAxisType::resultId() const
152 {
153   return LineImp::stype();
154 }
155 
instance()156 LinePerpendLPType* LinePerpendLPType::instance()
157 {
158   static LinePerpendLPType l;
159   return &l;
160 }
161 
calc(const LineData & a,const Coordinate & b) const162 ObjectImp* LinePerpendLPType::calc(
163   const LineData& a,
164   const Coordinate& b ) const
165 {
166   Coordinate p = calcPointOnPerpend( a, b );
167   return new LineImp( b, p );
168 }
169 
170 static const ArgsParser::spec argsspecLineParallel[] =
171 {
172   { AbstractLineImp::stype(), I18N_NOOP( "Construct a line parallel to this line" ),
173     I18N_NOOP( "Select a line parallel to the new line..." ), false },
174   { PointImp::stype(), I18N_NOOP( "Construct the parallel line through this point" ),
175     I18N_NOOP( "Select a point for the new line to go through..." ), true }
176 };
177 
KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE(LineParallelLPType)178 KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( LineParallelLPType )
179 
180 LineParallelLPType::LineParallelLPType()
181   : ObjectLPType( "LineParallel", argsspecLineParallel, 2 )
182 {
183 }
184 
~LineParallelLPType()185 LineParallelLPType::~LineParallelLPType()
186 {
187 }
188 
instance()189 LineParallelLPType* LineParallelLPType::instance()
190 {
191   static LineParallelLPType l;
192   return &l;
193 }
194 
calc(const LineData & a,const Coordinate & b) const195 ObjectImp* LineParallelLPType::calc(
196   const LineData& a,
197   const Coordinate& b ) const
198 {
199   Coordinate r = calcPointOnParallel( a, b );
200   return new LineImp( r, b );
201 }
202 
203 static const ArgsParser::spec argsspecLinePerpend[] =
204 {
205   { AbstractLineImp::stype(), I18N_NOOP( "Construct a line perpendicular to this line" ),
206     I18N_NOOP( "Select a line perpendicular to the new line..." ), false },
207   { PointImp::stype(), I18N_NOOP( "Construct a perpendicular line through this point" ),
208     I18N_NOOP( "Select a point for the new line to go through..." ), true }
209 };
210 
KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE(LinePerpendLPType)211 KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( LinePerpendLPType )
212 
213 LinePerpendLPType::LinePerpendLPType()
214   : ObjectLPType( "LinePerpend", argsspecLinePerpend, 2 )
215 {
216 }
217 
~LinePerpendLPType()218 LinePerpendLPType::~LinePerpendLPType()
219 {
220 }
221 
resultId() const222 const ObjectImpType* SegmentABType::resultId() const
223 {
224   return SegmentImp::stype();
225 }
226 
resultId() const227 const ObjectImpType* LineABType::resultId() const
228 {
229   return LineImp::stype();
230 }
231 
resultId() const232 const ObjectImpType* RayABType::resultId() const
233 {
234   return RayImp::stype();
235 }
236 
resultId() const237 const ObjectImpType* LinePerpendLPType::resultId() const
238 {
239   return LineImp::stype();
240 }
241 
resultId() const242 const ObjectImpType* LineParallelLPType::resultId() const
243 {
244   return LineImp::stype();
245 }
246 
specialActions() const247 QStringList SegmentABType::specialActions() const
248 {
249   QStringList ret;
250   ret << i18n( "Set &Length..." );
251   return ret;
252 }
253 
executeAction(int i,ObjectHolder &,ObjectTypeCalcer & c,KigPart & d,KigWidget & w,NormalMode &) const254 void SegmentABType::executeAction( int i, ObjectHolder&, ObjectTypeCalcer& c,
255                                    KigPart& d, KigWidget& w, NormalMode& ) const
256 {
257   assert( i == 0 );
258   // pretend to use this var..
259   (void) i;
260 
261   std::vector<ObjectCalcer*> parents = c.parents();
262   assert( margsparser.checkArgs( parents ) );
263 
264   Coordinate a = static_cast<const PointImp*>( parents[0]->imp() )->coordinate();
265   Coordinate b = static_cast<const PointImp*>( parents[1]->imp() )->coordinate();
266 
267   bool ok = true;
268   double length = getDoubleFromUser(
269     i18n( "Set Segment Length" ), i18n( "Choose the new length: " ),
270     (b-a).length(), &w, &ok, -2147483647, 2147483647, 3 );
271   if ( ! ok ) return;
272 
273   Coordinate nb = a + ( b - a ).normalize( length );
274 
275   MonitorDataObjects mon( getAllParents( parents ) );
276   parents[1]->move( nb, d.document() );
277   KigCommand* cd = new KigCommand( d, i18n( "Resize Segment" ) );
278   mon.finish( cd );
279   d.history()->push( cd );
280 }
281 
282 static const ArgsParser::spec argsspecLineByVector[] =
283 {
284   { VectorImp::stype(), I18N_NOOP( "Construct a line by this vector" ),
285     I18N_NOOP( "Select a vector in the direction of the new line..." ), true },
286   { PointImp::stype(), constructlineabstat,
287     I18N_NOOP( "Select a point for the new line to go through..." ), true }
288 };
289 
KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE(LineByVectorType)290 KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( LineByVectorType )
291 
292 LineByVectorType::LineByVectorType()
293   : ArgsParserObjectType( "LineByVector", argsspecLineByVector, 2 )
294 {
295 }
296 
~LineByVectorType()297 LineByVectorType::~LineByVectorType()
298 {
299 }
300 
instance()301 const LineByVectorType* LineByVectorType::instance()
302 {
303   static const LineByVectorType s;
304   return &s;
305 }
306 
calc(const Args & args,const KigDocument &) const307 ObjectImp* LineByVectorType::calc( const Args& args, const KigDocument& ) const
308 {
309   if ( ! margsparser.checkArgs( args ) ) return new InvalidImp;
310 
311   const VectorImp& a = *static_cast<const VectorImp*>( args[0] );
312   const PointImp& b = *static_cast<const PointImp*>( args[1] );
313 
314   return new LineImp(  b.coordinate(), b.coordinate() + a.dir() );
315 }
316 
resultId() const317 const ObjectImpType* LineByVectorType::resultId() const
318 {
319   return LineImp::stype();
320 }
321 
322 static const ArgsParser::spec argsspecHalflineByVector[] =
323 {
324   { VectorImp::stype(), I18N_NOOP( "Construct a half-line by this vector" ),
325     I18N_NOOP( "Select a vector in the direction of the new half-line..." ), true },
326   { PointImp::stype(), constructhalflinestartingstat,
327     I18N_NOOP( "Select the start point of the new half-line..." ), true }
328 };
329 
KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE(HalflineByVectorType)330 KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( HalflineByVectorType )
331 
332 HalflineByVectorType::HalflineByVectorType()
333   : ArgsParserObjectType( "HalflineByVector", argsspecHalflineByVector, 2 )
334 {
335 }
336 
~HalflineByVectorType()337 HalflineByVectorType::~HalflineByVectorType()
338 {
339 }
340 
instance()341 const HalflineByVectorType* HalflineByVectorType::instance()
342 {
343   static const HalflineByVectorType s;
344   return &s;
345 }
346 
calc(const Args & args,const KigDocument &) const347 ObjectImp* HalflineByVectorType::calc( const Args& args, const KigDocument& ) const
348 {
349   if ( ! margsparser.checkArgs( args ) ) return new InvalidImp;
350 
351   const VectorImp& a = *static_cast<const VectorImp*>( args[0] );
352   const PointImp& b = *static_cast<const PointImp*>( args[1] );
353 
354   return new RayImp(  b.coordinate(), b.coordinate() + a.dir() );
355 }
356 
resultId() const357 const ObjectImpType* HalflineByVectorType::resultId() const
358 {
359   return RayImp::stype();
360 }
361