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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
|
// Copyright (C) 2003 Dominique Devriese <devriese@kde.org>
// 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 "kseg-filter.h"
#include "kseg-defs.h"
#include "../kig/kig_document.h"
#include "../kig/kig_part.h"
#include "../misc/coordinate.h"
#include "../objects/angle_type.h"
#include "../objects/arc_type.h"
#include "../objects/bogus_imp.h"
#include "../objects/circle_type.h"
#include "../objects/conic_imp.h"
#include "../objects/conic_types.h"
#include "../objects/intersection_types.h"
#include "../objects/line_imp.h"
#include "../objects/line_type.h"
#include "../objects/object_calcer.h"
#include "../objects/object_drawer.h"
#include "../objects/object_factory.h"
#include "../objects/object_holder.h"
#include "../objects/other_imp.h"
#include "../objects/other_type.h"
#include "../objects/point_imp.h"
#include "../objects/point_type.h"
#include "../objects/polygon_type.h"
#include "../objects/transform_types.h"
#include "../objects/vector_type.h"
#include <tqfont.h>
#include <tqpen.h>
#include <tqbrush.h>
#include <tqfile.h>
#include <tqdatastream.h>
#include <tqbuffer.h>
#include <klocale.h>
KigFilterKSeg::KigFilterKSeg()
{
}
KigFilterKSeg::~KigFilterKSeg()
{
}
bool KigFilterKSeg::supportMime( const TQString& mime )
{
return mime == "application/x-kseg";
}
struct drawstyle
{
TQ_INT8 pointstyle;
TQFont font;
TQPen pen;
TQBrush brush;
};
static Coordinate readKSegCoordinate( TQDataStream& stream )
{
// read the coord..
float inx, iny;
stream >> inx >> iny;
// KSeg uses a coordinate system, where the topleft is (0,0), and
// the bottom right is the widget coordinate in the window: if the
// KSeg window had a width of 600 pixels and a height of 600, then
// the bottom right will be at (600,600). We assume a window of
// such a height here, and transform it into Kig Coordinates. This
// function is quite similar to ScreenInfo::fromScreen, and it's
// basically a simple modification of that code to floats..
// invert the y-axis: 0 is at the bottom !
Coordinate t( inx, 600 - iny );
t *= 14;
t /= 600;
return t + Coordinate( -7, -7 );
}
static ObjectTypeCalcer* intersectionPoint( const std::vector<ObjectCalcer*>& parents, int which )
{
if ( parents.size() != 2 ) return 0;
int nlines = 0;
int nconics = 0;
int narcs = 0;
for ( int i = 0; i < 2; ++i )
{
if ( parents[i]->imp()->inherits( AbstractLineImp::stype() ) ) ++nlines;
else if ( parents[i]->imp()->inherits( ConicImp::stype() ) ) ++nconics;
else if ( parents[i]->imp()->inherits( ArcImp::stype() ) ) ++narcs;
else return 0;
};
if ( nlines == 2 )
return which == -1 ? new ObjectTypeCalcer( LineLineIntersectionType::instance(), parents ) : 0;
else if ( nlines == 1 && nconics == 1 )
{
std::vector<ObjectCalcer*> intparents( parents );
intparents.push_back( new ObjectConstCalcer( new IntImp( which ) ) );
return new ObjectTypeCalcer( ConicLineIntersectionType::instance(), intparents );
}
else if ( nlines == 0 && nconics == 2 )
{
std::vector<ObjectCalcer*> rparents( parents );
rparents.push_back( new ObjectConstCalcer( new IntImp( 1 ) ) );
rparents.push_back( new ObjectConstCalcer( new IntImp( 1 ) ) );
rparents.push_back( new ObjectTypeCalcer( ConicRadicalType::instance(), rparents ) );
std::vector<ObjectCalcer*> iparents;
iparents.push_back( parents[0] );
iparents.push_back( rparents.back() );
iparents.push_back( new ObjectConstCalcer( new IntImp( which ) ) );
return new ObjectTypeCalcer( ConicLineIntersectionType::instance(), iparents );
}
else if ( nlines == 1 && narcs == 1 )
{
std::vector<ObjectCalcer*> intparents( parents );
intparents.push_back( new ObjectConstCalcer( new IntImp( which ) ) );
return new ObjectTypeCalcer( ArcLineIntersectionType::instance(), intparents );
}
else return 0;
}
ObjectCalcer* KigFilterKSeg::transformObject( const TQString& file, KigDocument& kigdoc,
std::vector<ObjectCalcer*>& parents,
int subtype, bool& ok )
{
ok = true;
ObjectCalcer* retobj = 0;
switch( subtype )
{
case G_TRANSLATED:
{
std::vector<ObjectCalcer*> vectorparents( parents.begin() + 1, parents.end() );
ObjectTypeCalcer* vector = new ObjectTypeCalcer( VectorType::instance(), vectorparents );
vector->calc( kigdoc );
std::vector<ObjectCalcer*> transparents;
transparents.push_back( parents[0] );
transparents.push_back( vector );
retobj = new ObjectTypeCalcer( TranslatedType::instance(), transparents );
break;
}
case G_ROTATED:
{
std::vector<ObjectCalcer*> angleparents( parents.begin() + 2, parents.end() );
ObjectTypeCalcer* angle = new ObjectTypeCalcer( AngleType::instance(), angleparents );
angle->calc( kigdoc );
std::vector<ObjectCalcer*> rotparents;
rotparents.push_back( parents[0] );
rotparents.push_back( parents[1] );
rotparents.push_back( angle );
retobj = new ObjectTypeCalcer( RotationType::instance(), rotparents );
break;
}
case G_SCALED:
{
if ( parents.size() == 4 )
{
retobj = new ObjectTypeCalcer( ScalingOverCenter2Type::instance(), parents );
}
else
{
// TODO
notSupported( file, i18n( "This KSeg document uses a scaling "
"transformation, which Kig currently "
"cannot import." ) );
ok = false;
return 0;
}
break;
}
case G_REFLECTED:
{
std::vector<ObjectCalcer*> mirparents( parents.begin(), parents.end() );
retobj = new ObjectTypeCalcer( LineReflectionType::instance(), mirparents );
break;
}
}
return retobj;
}
KigDocument* KigFilterKSeg::load( const TQString& file )
{
TQFile ffile( file );
if ( ! ffile.open( IO_ReadOnly ) )
{
fileNotFound( file );
return false;
};
KigDocument* retdoc = new KigDocument();
TQDataStream fstream( &ffile );
TQString versionstring;
fstream >> versionstring;
if ( !versionstring.startsWith( "KSeg Document Version " ) )
KIG_FILTER_PARSE_ERROR;
TQByteArray array;
fstream >> array;
TQBuffer buf( array );
buf.open( IO_ReadOnly );
TQDataStream stream( &buf );
stream.setVersion( 3 );
// G_drawstyles:
short numstyles;
stream >> numstyles;
std::vector<drawstyle> drawstyles( numstyles );
for ( short i = 0; i < numstyles; ++i )
{
stream >> drawstyles[i].pointstyle;
stream >> drawstyles[i].font;
stream >> drawstyles[i].pen;
stream >> drawstyles[i].brush;
};
std::vector<ObjectHolder*> ret;
std::vector<ObjectHolder*> ret2;
// G_refs
unsigned int count;
stream >> count;
ret.resize( count, 0 );
const ObjectFactory* fact = ObjectFactory::instance();
// KSeg topologically sorts the objects before saving, that means we
// can read the entire file in one iteration..
for ( uint i = 0; i < count; ++i )
{
short styleid;
stream >> styleid;
short nparents;
stream >> nparents;
std::vector<ObjectCalcer*> parents( nparents, 0 );
for ( short j = 0; j < nparents; ++j )
{
int parent;
stream >> parent;
parents[j] = ret[parent]->calcer();
};
// read the object..
short info;
stream >> info;
int type = 1 << (info & 31);
info >>= 5;
int descendtype = (info & 15);
info >>= 4;
bool visible = info & 1;
bool labelVisible = info & 2;
bool given = info & 4;
bool final = info & 8;
// avoid g++ warnings about unused vars..
// this doesn't really do anything..
(void) given;
(void) final;
drawstyle style = drawstyles[styleid];
if ( type == G_LOOP ) continue;
// read the label..
TQString labeltext;
stream >> labeltext;
Coordinate relcoord = readKSegCoordinate( stream );
// shut up gcc
(void) relcoord;
if ( type & G_CURVE )
{
Coordinate relcurvecoord = readKSegCoordinate( stream );
// shut up gcc
(void) relcurvecoord;
};
// now load the object data..
ObjectHolder* object = 0;
ObjectCalcer* o = 0;
bool ok = true;
TQColor color = style.pen.color();
int width = style.pen.width();
/*
kdDebug() << "type: " << type << endl
<< "descendtype: " << descendtype << endl
<< "label: " << labeltext << endl;
//*/
switch ( type )
{
case G_POINT:
{
switch( descendtype )
{
case G_TRANSLATED:
case G_ROTATED:
case G_SCALED:
case G_REFLECTED:
{
o = transformObject( file, *retdoc, parents, descendtype, ok );
break;
}
case G_FREE_POINT:
{
// fixed point
if ( nparents != 0 ) KIG_FILTER_PARSE_ERROR;
Coordinate c = readKSegCoordinate( stream );
o = fact->fixedPointCalcer( c );
break;
}
case G_CONSTRAINED_POINT:
{
// constrained point
double p;
stream >> p;
if ( nparents != 1 ) KIG_FILTER_PARSE_ERROR;
ObjectCalcer* parent = parents[0];
assert( parent );
o = fact->constrainedPointCalcer( parent, p );
break;
}
case G_INTERSECTION_POINT:
{
// KSeg has somewhat weird intersection objects..
// for all objects G_INTERSECTION_POINT gets the
// first intersection of its parents, G_INTERSECTION2_POINT
// represents the second, if present.
o = intersectionPoint( parents, -1 );
if ( ! o ) KIG_FILTER_PARSE_ERROR;
break;
}
case G_INTERSECTION2_POINT:
{
o = intersectionPoint( parents, 1 );
if ( ! o ) KIG_FILTER_PARSE_ERROR;
break;
}
case G_MID_POINT:
{
// midpoint of a segment..
if ( parents.size() != 1 ) KIG_FILTER_PARSE_ERROR;
if ( !parents[0]->imp()->inherits( SegmentImp::stype() ) )
KIG_FILTER_PARSE_ERROR;
int index = parents[0]->imp()->propertiesInternalNames().findIndex( "mid-point" );
assert( index != -1 );
o = new ObjectPropertyCalcer( parents[0], index );
break;
}
default:
KIG_FILTER_PARSE_ERROR;
};
width = style.pointstyle == SMALL_CIRCLE ? 2 : style.pointstyle == MEDIUM_CIRCLE ? 3 : 5;
color = style.brush.color();
break;
};
case G_SEGMENT:
{
switch( descendtype )
{
case G_TRANSLATED:
case G_ROTATED:
case G_SCALED:
case G_REFLECTED:
{
o = transformObject( file, *retdoc, parents, descendtype, ok );
break;
}
case G_ENDPOINTS_SEGMENT:
{
if ( nparents != 2 ) KIG_FILTER_PARSE_ERROR;
o = new ObjectTypeCalcer( SegmentABType::instance(), parents );
break;
}
default:
KIG_FILTER_PARSE_ERROR;
}
break;
};
case G_RAY:
{
switch( descendtype )
{
case G_TRANSLATED:
case G_ROTATED:
case G_SCALED:
case G_REFLECTED:
{
o = transformObject( file, *retdoc, parents, descendtype, ok );
break;
}
case G_TWOPOINTS_RAY:
{
if ( nparents != 2 ) KIG_FILTER_PARSE_ERROR;
o = new ObjectTypeCalcer( RayABType::instance(), parents );
break;
}
case G_BISECTOR_RAY:
{
ObjectTypeCalcer* angle = new ObjectTypeCalcer( HalfAngleType::instance(), parents );
angle->calc( *retdoc );
o = fact->propertyObjectCalcer( angle, "angle-bisector" );
break;
}
default:
KIG_FILTER_PARSE_ERROR;
};
break;
};
case G_LINE:
{
switch( descendtype )
{
case G_TRANSLATED:
case G_ROTATED:
case G_SCALED:
case G_REFLECTED:
{
o = transformObject( file, *retdoc, parents, descendtype, ok );
break;
}
case G_TWOPOINTS_LINE:
{
if ( nparents != 2 ) KIG_FILTER_PARSE_ERROR;
o = new ObjectTypeCalcer( LineABType::instance(), parents );
break;
}
case G_PARALLEL_LINE:
{
if ( nparents != 2 ) KIG_FILTER_PARSE_ERROR;
o = new ObjectTypeCalcer( LineParallelLPType::instance(), parents );
break;
}
case G_PERPENDICULAR_LINE:
{
if ( nparents != 2 ) KIG_FILTER_PARSE_ERROR;
o = new ObjectTypeCalcer( LinePerpendLPType::instance(), parents );
break;
}
default:
KIG_FILTER_PARSE_ERROR;
};
break;
};
case G_CIRCLE:
{
switch( descendtype )
{
case G_TRANSLATED:
case G_ROTATED:
case G_SCALED:
case G_REFLECTED:
{
o = transformObject( file, *retdoc, parents, descendtype, ok );
break;
}
case G_CENTERPOINT_CIRCLE:
{
if ( nparents != 2 ) KIG_FILTER_PARSE_ERROR;
o = new ObjectTypeCalcer( CircleBCPType::instance(), parents );
break;
}
case G_CENTERRADIUS_CIRCLE:
{
ObjectCalcer* point;
ObjectCalcer* segment;
if ( parents[0]->imp()->inherits( PointImp::stype() ) )
{
point = parents[0];
segment = parents[1];
}
else
{
point = parents[1];
segment = parents[0];
};
int index = segment->imp()->propertiesInternalNames().findIndex( "length" );
if ( index == -1 ) KIG_FILTER_PARSE_ERROR;
ObjectPropertyCalcer* length = new ObjectPropertyCalcer( segment, index );
length->calc( *retdoc );
std::vector<ObjectCalcer*> cparents;
cparents.push_back( point );
cparents.push_back( length );
o = new ObjectTypeCalcer( CircleBPRType::instance(), cparents );
break;
}
default:
KIG_FILTER_PARSE_ERROR;
};
break;
};
case G_ARC:
{
switch( descendtype )
{
case G_TRANSLATED:
case G_ROTATED:
case G_SCALED:
case G_REFLECTED:
{
o = transformObject( file, *retdoc, parents, descendtype, ok );
break;
}
case G_THREEPOINTS_ARC:
{
if ( nparents != 3 ) KIG_FILTER_PARSE_ERROR;
o = new ObjectTypeCalcer( ArcBTPType::instance(), parents );
break;
}
default:
KIG_FILTER_PARSE_ERROR;
}
break;
};
case G_POLYGON:
{
switch( descendtype )
{
case G_TRANSLATED:
case G_ROTATED:
case G_SCALED:
case G_REFLECTED:
{
o = transformObject( file, *retdoc, parents, descendtype, ok );
break;
}
default:
{
if ( nparents < 3 ) KIG_FILTER_PARSE_ERROR;
o = new ObjectTypeCalcer( PolygonBNPType::instance(), parents );
}
}
// default:
// KIG_FILTER_PARSE_ERROR;
break;
};
case G_CIRCLEINTERIOR:
{
notSupported( file, i18n( "This KSeg file contains a filled circle, "
"which Kig does not currently support." ) );
return false;
};
case G_ARCSECTOR:
{
notSupported( file, i18n( "This KSeg file contains an arc sector, "
"which Kig does not currently support." ) );
return false;
};
case G_ARCSEGMENT:
{
notSupported( file, i18n( "This KSeg file contains an arc segment, "
"which Kig does not currently support." ) );
return false;
};
case G_LOCUS:
{
switch( descendtype )
{
case G_TRANSLATED:
case G_ROTATED:
case G_SCALED:
case G_REFLECTED:
{
o = transformObject( file, *retdoc, parents, descendtype, ok );
break;
}
case G_OBJECT_LOCUS:
{
if ( nparents != 2 ) KIG_FILTER_PARSE_ERROR;
o = fact->locusCalcer( parents[0], parents[1] );
break;
}
default:
KIG_FILTER_PARSE_ERROR;
}
break;
};
case G_MEASURE:
KIG_FILTER_PARSE_ERROR;
case G_CALCULATE:
KIG_FILTER_PARSE_ERROR;
case G_ANNOTATION:
KIG_FILTER_PARSE_ERROR;
case G_LOOP:
KIG_FILTER_PARSE_ERROR;
default:
KIG_FILTER_PARSE_ERROR;
}
// checking if the object was correctly created
if ( ! o )
{
if ( ok )
KIG_FILTER_PARSE_ERROR
else
return 0;
}
ObjectDrawer* d = new ObjectDrawer( color, width, visible, style.pen.style() );
if ( !labeltext.isEmpty() )
{
ObjectConstCalcer* name = new ObjectConstCalcer( new StringImp( labeltext ) );
object = new ObjectHolder( o, d, name );
}
else
{
object = new ObjectHolder( o, d );
}
assert( object );
ret[i] = object;
object->calc( *retdoc );
if ( !labeltext.isEmpty() && labelVisible )
{
std::vector<ObjectCalcer*> args2;
args2.push_back( object->nameCalcer() );
ObjectCalcer* oc2 = fact->attachedLabelCalcer(
TQString::fromLatin1( "%1" ), object->calcer(),
static_cast<const PointImp*>( object->imp() )->coordinate(),
false, args2, *retdoc );
oc2->calc( *retdoc );
ObjectDrawer* d2 = new ObjectDrawer( style.pen.color() );
ObjectHolder* o2 = new ObjectHolder( oc2, d2 );
ret2.push_back( o2 );
}
};
// selection groups ( we ignore them, but we pretend to read them
// out anyway, so we can find what comes after them.. )
int selgroupcount;
stream >> selgroupcount;
for ( int i = 0; i < selgroupcount; ++i )
{
TQString name;
stream >> name;
int size;
stream >> size;
for ( int i = 0; i < size; ++i )
{
short object;
stream >> object;
(void) object;
};
};
// no more data in the file..
retdoc->addObjects( ret );
retdoc->addObjects( ret2 );
retdoc->setAxes( false );
retdoc->setGrid( false );
return retdoc;
}
KigFilterKSeg* KigFilterKSeg::instance()
{
static KigFilterKSeg f;
return &f;
}
|