1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
// Copyright (C) 2002 Dominique Devriese <[email protected]>
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
// 02110-1301, USA.
#include "object_factory.h"
#include "bogus_imp.h"
#include "curve_imp.h"
#include "intersection_types.h"
#include "line_imp.h"
#include "object_drawer.h"
#include "object_holder.h"
#include "other_type.h"
#include "point_imp.h"
#include "point_type.h"
#include "text_type.h"
#include "../kig/kig_document.h"
#include "../kig/kig_view.h"
#include "../misc/calcpaths.h"
#include "../misc/coordinate.h"
#include "../misc/object_hierarchy.h"
#include <algorithm>
#include <functional>
ObjectHolder* ObjectFactory::fixedPoint( const Coordinate& c ) const
{
ObjectHolder* o = new ObjectHolder( fixedPointCalcer( c ) );
return o;
}
ObjectTypeCalcer* ObjectFactory::fixedPointCalcer( const Coordinate& c ) const
{
std::vector<ObjectCalcer*> args;
args.push_back( new ObjectConstCalcer( new DoubleImp( c.x ) ) );
args.push_back( new ObjectConstCalcer( new DoubleImp( c.y ) ) );
ObjectTypeCalcer* oc = new ObjectTypeCalcer( FixedPointType::instance(), args );
return oc;
}
ObjectTypeCalcer* ObjectFactory::cursorPointCalcer( const Coordinate& c ) const
{
std::vector<ObjectCalcer*> args;
args.push_back( new ObjectConstCalcer( new DoubleImp( c.x ) ) );
args.push_back( new ObjectConstCalcer( new DoubleImp( c.y ) ) );
ObjectTypeCalcer* oc = new ObjectTypeCalcer( CursorPointType::instance(), args );
return oc;
}
const ObjectFactory* ObjectFactory::instance()
{
static ObjectFactory f;
return &f;
}
ObjectTypeCalcer* ObjectFactory::sensiblePointCalcer(
const Coordinate& c, const KigDocument& d, const KigWidget& w ) const
{
std::vector<ObjectHolder*> os = d.whatAmIOn( c, w );
if ( os.size() == 2 )
{
// we can calc intersection point *olny* between two objects...
std::vector<ObjectCalcer*> args;
args.push_back( os[0]->calcer() );
args.push_back( os[1]->calcer() );
// the simpliest case: two lines...
if ( ( os[0]->imp()->inherits( AbstractLineImp::stype() ) ) &&
( os[1]->imp()->inherits( AbstractLineImp::stype() ) ) )
return new ObjectTypeCalcer( LineLineIntersectionType::instance(), args );
// other cases will follow...
}
for ( std::vector<ObjectHolder*>::iterator i = os.begin(); i != os.end(); ++i )
if ( (*i)->imp()->inherits( CurveImp::stype() ) )
return constrainedPointCalcer( (*i)->calcer(), c, d );
return fixedPointCalcer( c );
}
ObjectHolder* ObjectFactory::sensiblePoint(
const Coordinate& c, const KigDocument& d, const KigWidget& w ) const
{
return new ObjectHolder( sensiblePointCalcer( c, d, w ) );
}
ObjectTypeCalcer* ObjectFactory::relativePointCalcer(
ObjectCalcer* o, const Coordinate& loc ) const
{
Coordinate reference =
static_cast<const ObjectImp*>( o->imp() )->attachPoint();
assert( reference.valid() );
double x = 0.0;
double y = 0.0;
if ( loc.valid() )
{
x = loc.x - reference.x;
y = loc.y - reference.y;
}
std::vector<ObjectCalcer*> parents;
parents.push_back( new ObjectConstCalcer( new DoubleImp( x ) ) );
parents.push_back( new ObjectConstCalcer( new DoubleImp( y ) ) );
parents.push_back( o );
return new ObjectTypeCalcer( RelativePointType::instance(), parents );
}
ObjectTypeCalcer* ObjectFactory::constrainedPointCalcer(
ObjectCalcer* curve, double param ) const
{
assert( curve->imp()->inherits( CurveImp::stype() ) );
std::vector<ObjectCalcer*> parents;
parents.push_back( new ObjectConstCalcer( new DoubleImp( param ) ) );
parents.push_back( curve );
return new ObjectTypeCalcer( ConstrainedPointType::instance(), parents );
}
ObjectHolder* ObjectFactory::constrainedPoint(
ObjectCalcer* curve, double param ) const
{
return new ObjectHolder( constrainedPointCalcer( curve, param ) );
}
ObjectTypeCalcer* ObjectFactory::constrainedPointCalcer(
ObjectCalcer* curve, const Coordinate& c, const KigDocument& d ) const
{
assert( curve->imp()->inherits( CurveImp::stype() ) );
double param = static_cast<const CurveImp*>( curve->imp() )->getParam( c, d );
return constrainedPointCalcer( curve, param );
}
ObjectHolder* ObjectFactory::constrainedPoint(
ObjectCalcer* curve, const Coordinate& c, const KigDocument& d ) const
{
return new ObjectHolder( constrainedPointCalcer( curve, c, d ) );
}
ObjectTypeCalcer* ObjectFactory::locusCalcer(
ObjectCalcer* a, ObjectCalcer* b ) const
{
assert( dynamic_cast<const ObjectTypeCalcer*>( a ) );
ObjectTypeCalcer* constrained = static_cast<ObjectTypeCalcer*>( a );
assert( constrained->type()->inherits( ObjectType::ID_ConstrainedPointType ) );
assert( constrained->parents().size() == 2 );
ObjectCalcer* curve = a->parents().back();
const ObjectCalcer* moving = b;
std::vector<ObjectCalcer*> hierparents;
hierparents.push_back( constrained );
std::vector<ObjectCalcer*> sideOfTree = sideOfTreePath( hierparents, moving );
std::copy( sideOfTree.begin(), sideOfTree.end(), std::back_inserter( hierparents ) );
ObjectHierarchy hier( hierparents, moving );
std::vector<ObjectCalcer*> realparents( 2 + sideOfTree.size(), 0 );
realparents[0] = new ObjectConstCalcer( new HierarchyImp( hier ) );
realparents[1] = curve;
copy( sideOfTree.begin(), sideOfTree.end(), realparents.begin() + 2 );
return new ObjectTypeCalcer( LocusType::instance(), realparents );
}
ObjectHolder* ObjectFactory::locus( ObjectCalcer* a, ObjectCalcer* b ) const
{
return new ObjectHolder( locusCalcer( a, b ) );
}
ObjectHolder* ObjectFactory::label(
const TQString& s, const Coordinate& loc,
bool needframe, const std::vector<ObjectCalcer*>& parents,
const KigDocument& doc ) const
{
return new ObjectHolder( labelCalcer( s, loc, needframe, parents, doc ) );
}
ObjectTypeCalcer* ObjectFactory::labelCalcer(
const TQString& s, const Coordinate& loc,
bool needframe, const std::vector<ObjectCalcer*>& parents,
const KigDocument& doc ) const
{
return attachedLabelCalcer( s, 0, loc, needframe, parents, doc );
}
ObjectTypeCalcer* ObjectFactory::attachedLabelCalcer(
const TQString& s, ObjectCalcer* p,
const Coordinate& loc, bool needframe,
const std::vector<ObjectCalcer*>& nparents,
const KigDocument& doc ) const
{
std::vector<ObjectCalcer*> parents;
parents.reserve( nparents.size() + 3 );
parents.push_back( new ObjectConstCalcer( new IntImp( needframe ? 1 : 0 ) ) );
parents.push_back( getAttachPoint( p, loc, doc ) );
parents.push_back( new ObjectConstCalcer( new StringImp( s ) ) );
std::copy( nparents.begin(), nparents.end(), std::back_inserter( parents ) );
ObjectTypeCalcer* ret = new ObjectTypeCalcer( TextType::instance(), parents );
ret->calc( doc );
return ret;
}
ObjectCalcer* ObjectFactory::getAttachPoint(
ObjectCalcer* p,
const Coordinate& loc,
const KigDocument& doc ) const
{
/*
* mp: (changes to add relative-attachment). Now an object is tested
* as follows:
* - if attachPoint() returns a valid coordinate, then we use the new method
* - if it is a point: 'old-style' treatment (we can change this shortly)
* - if it is a curve: 'old-style' treatment (we could use the new approach,
* which can be better/worse depending on personal taste, I think)
*
* the first condition that matches determines the behaviour.
* the new method works similarly to the curve case, but we generate a new
* RelativePointType instead of a ConstrainedPointType; this will in turn make use
* of the new attachPoint() method for objects.
*
* changed the preference order 2005/01/21 (now attachPoint has preference over points)
*
* NOTE: changes in the tests performed should be matched also in
* modes/popup.cpp (addNameLabel) and in label.cpp (TextLabelModeBase::mouseMoved)
*/
if ( p && p->imp()->attachPoint().valid() )
{
ObjectCalcer* o = relativePointCalcer( p, loc );
o->calc( doc );
return o;
}
else if ( p && p->imp()->inherits( PointImp::stype() ) )
{
return p;
}
else if ( p && p->imp()->inherits( CurveImp::stype() ) )
{
double param = 0.5;
if ( loc.valid() )
param = static_cast<const CurveImp*>( p->imp() )->getParam( loc, doc );
ObjectCalcer* o = constrainedPointCalcer( p, param );
o->calc( doc );
return o;
}
else
{
if ( loc.valid() )
return new ObjectConstCalcer( new PointImp( loc ) );
else
return new ObjectConstCalcer( new PointImp( Coordinate( 0, 0 ) ) );
}
}
ObjectHolder* ObjectFactory::attachedLabel(
const TQString& s, ObjectCalcer* locationparent,
const Coordinate& loc, bool needframe,
const std::vector<ObjectCalcer*>& parents,
const KigDocument& doc ) const
{
return new ObjectHolder( attachedLabelCalcer( s, locationparent, loc, needframe, parents, doc ) );
}
ObjectPropertyCalcer* ObjectFactory::propertyObjectCalcer(
ObjectCalcer* o, const char* p ) const
{
int wp = o->imp()->propertiesInternalNames().findIndex( p );
if ( wp == -1 ) return 0;
return new ObjectPropertyCalcer( o, wp );
}
ObjectHolder* ObjectFactory::propertyObject(
ObjectCalcer* o, const char* p ) const
{
return new ObjectHolder( propertyObjectCalcer( o, p ) );
}
void ObjectFactory::redefinePoint(
ObjectTypeCalcer* point, const Coordinate& c,
KigDocument& doc, const KigWidget& w ) const
{
std::vector<ObjectHolder*> hos = doc.whatAmIOn( c, w );
std::vector<ObjectCalcer*> os;
ObjectCalcer* (ObjectHolder::*calcmeth)() = &ObjectHolder::calcer;
std::transform( hos.begin(), hos.end(), std::back_inserter( os ),
std::mem_fn( calcmeth ) );
ObjectCalcer* v = 0;
// we don't want one of our children as a parent...
std::set<ObjectCalcer*> children = getAllChildren( point );
for ( std::vector<ObjectCalcer*>::iterator i = os.begin();
i != os.end(); ++i )
if ( (*i)->imp()->inherits( CurveImp::stype() ) &&
children.find( *i ) == children.end() )
{
v = *i;
break;
};
if ( v )
{
// we want a constrained point...
const CurveImp* curveimp = static_cast<const CurveImp*>( v->imp() );
double newparam = curveimp->getParam( c, doc );
if ( point->type()->inherits( ObjectType::ID_ConstrainedPointType ) )
{
// point already was constrained -> simply update the param
// DataObject and make sure point is on the right curve...
ObjectCalcer* dataobj = 0;
std::vector<ObjectCalcer*> parents = point->parents();
assert( parents.size() == 2 );
assert ( parents[0]->imp()->inherits( DoubleImp::stype() ) );
dataobj = parents[0];
parents.clear();
parents.push_back( dataobj );
parents.push_back( v );
point->setParents( parents );
assert( dynamic_cast<ObjectConstCalcer*>( dataobj ) );
static_cast<ObjectConstCalcer*>( dataobj )->setImp(
new DoubleImp( newparam ) );
}
else
{
// point used to be fixed -> add a new DataObject etc.
std::vector<ObjectCalcer*> args;
args.push_back( new ObjectConstCalcer( new DoubleImp( newparam ) ) );
args.push_back( v );
point->setType( ConstrainedPointType::instance() );
point->setParents( args );
}
}
else
{
// a fixed point...
if ( point->type()->inherits( ObjectType::ID_ConstrainedPointType ) )
{
// point used to be constrained..
std::vector<ObjectCalcer*> a;
a.push_back( new ObjectConstCalcer( new DoubleImp( c.x ) ) );
a.push_back( new ObjectConstCalcer( new DoubleImp( c.y ) ) );
point->setType( FixedPointType::instance() );
point->setParents( a );
}
else
{
// point used to be fixed -> simply update the DataObject's
// we can use the point's move function for that..
point->move( c, doc );
};
}
}
|