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
|
/**
This file is part of Kig, a KDE program for Interactive Geometry...
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 "cabri-filter.h"
#include "../kig/kig_document.h"
#include "../kig/kig_part.h"
#include "../misc/coordinate.h"
#include "../objects/arc_type.h"
#include "../objects/bogus_imp.h"
#include "../objects/circle_type.h"
#include "../objects/conic_types.h"
#include "../objects/curve_imp.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_type.h"
#include "../objects/polygon_type.h"
#include "../objects/transform_types.h"
#include "../objects/vector_type.h"
#include <tqcolor.h>
#include <tqfile.h>
#include <tqregexp.h>
#include <kdebug.h>
#include <klocale.h>
/**
* a line:
* "Nr": Type, Unknown, "CN:"NumberOfParents, "VN:"Unknown
* Color, FillType, Thickness, "DS":Dots, "GT":SpecialAppearance, Visible, Fixed
* ["Const": Parents] ["Val": Constants]
*
* Nr: Simple sequential numbering of the objects in a file.
* Type: seen so far: Pt, Axes, Line, Cir
* NumberOfParents: The number of parents that will be specified in
* Parents
* Color:
* R -> red
* O -> purple
* Y -> yellow
* P -> dark purple
* V -> dark blue
* Bl -> blue
* lBl -> bright blue
* G -> bright green
* dG -> dark green
* Br -> brown
* dBr -> beige
* lGr -> light grey
* Gr -> grey
* dGr -> dark grey
* B -> black
* FillType:
* W -> not filled ( white ? )
* all values of the Color item are valid here too..
* Thickness:
* t -> thin
* tT -> medium
* T -> Thick
* Dots:
* a specification for how a line should be drawn ( with many
* dots, with short lines, it's a pretty generic format: the first
* number signifies the number of sequential dots to draw first, and
* the next is the sum of this first number with the number of
* spaces to leave before starting a new segment.
* SpecialAppearance:
* a number indicating some way of specially drawing an object. This
* can be modified using the "Modify Appearance" button in
* Cabri. For example, for a segment, the number indicates the
* amount of ticks to put on the segment, to indicate
* correspondances between segments..
* Visible:
* V means visible, I means invisible
* Fixed:
* St means fix this object ( if you move one of its parents, it
* won't move ), nSt ( the default ) means don't fix this object.
* Parents:
* The numbers of the objects this object depends on
* Constants:
* Constants whose meaning depends on the type of object. E.g. for
* a point, this means first x, then y component.
*/
struct CabriObject
{
uint id;
TQCString type;
uint numberOfParents;
TQColor color;
TQColor fillColor;
int thick;
int lineSegLength;
int lineSegSplit;
int specialAppearanceSwitch;
bool visible;
bool fixed;
std::vector<int> parents;
std::vector<double> data;
};
KigFilterCabri::KigFilterCabri()
{
}
KigFilterCabri::~KigFilterCabri()
{
}
bool KigFilterCabri::supportMime( const TQString& mime )
{
// ugly hack to avoid duplicate extension ( XFig and Cabri files
// have the same .fig extension ).
return ( mime == "image/x-xfig" ) ||
( mime == "application/x-cabri" );
}
static TQString readLine( TQFile& file )
{
TQString ret;
file.readLine( ret, 10000L );
if ( ret[ret.length() - 1] == '\n' )
ret.truncate( ret.length() - 1 );
if ( ret[ret.length() - 1] == '\r' )
ret.truncate( ret.length() - 1 );
return ret;
}
static TQColor translatecolor( const TQString& s )
{
if ( s == "R" ) return Qt::red;
if ( s == "O" ) return Qt::magenta;
if ( s == "Y" ) return Qt::yellow;
if ( s == "P" ) return Qt::darkMagenta;
if ( s == "V" ) return Qt::darkBlue;
if ( s == "Bl" ) return Qt::blue;
if ( s == "lBl" ) return Qt::cyan; // TODO: bright blue
if ( s == "G" ) return Qt::green;
if ( s == "dG" ) return Qt::darkGreen;
if ( s == "Br" ) return TQColor( 165, 42, 42 );
if ( s == "dBr" ) return TQColor( 128, 128, 0 );
if ( s == "lGr" ) return Qt::lightGray;
if ( s == "Gr" ) return Qt::gray;
if ( s == "dGr" ) return Qt::darkGray;
if ( s == "B" ) return Qt::black;
if ( s == "W" ) return Qt::white;
kdDebug() << k_funcinfo << "unknown color: " << s << endl;
return Qt::black;
}
bool KigFilterCabri::readObject( TQFile& f, CabriObject& myobj )
{
// there are 4 lines per object in the file, so we read them all
// four now.
TQString line1, line2, line3, s;
TQString file = f.name();
line1 = readLine( f );
line2 = readLine( f );
line3 = readLine( f );
// ignore line 4, it is empty..
s = readLine( f );
TQRegExp firstlinere( "^([^:]+): ([^,]+), ([^,]+), CN:([^,]*), VN:(.*)$" );
if ( ! firstlinere.exactMatch( line1 ) )
KIG_FILTER_PARSE_ERROR;
bool ok;
TQString tmp;
tmp = firstlinere.cap( 1 );
myobj.id = tmp.toInt( &ok );
if ( !ok ) KIG_FILTER_PARSE_ERROR;
tmp = firstlinere.cap( 2 );
myobj.type = tmp.latin1();
tmp = firstlinere.cap( 3 );
// i have no idea what this number means..
tmp = firstlinere.cap( 4 );
myobj.numberOfParents = tmp.toInt( &ok );
if ( ! ok ) KIG_FILTER_PARSE_ERROR;
tmp = firstlinere.cap( 5 );
// i have no idea what this number means..
TQRegExp secondlinere( "^([^,]+), ([^,]+), ([^,]+), DS:([^ ]+) ([^,]+), GT:([^,]+), ([^,]+), (.*)$" );
if ( ! secondlinere.exactMatch( line2 ) )
KIG_FILTER_PARSE_ERROR;
tmp = secondlinere.cap( 1 );
myobj.color = translatecolor( tmp );
// if ( ! color.isValid() ) KIG_FILTER_PARSE_ERROR;
tmp = secondlinere.cap( 2 );
myobj.fillColor = translatecolor( tmp );
// if ( ! fillcolor.isValid() ) KIG_FILTER_PARSE_ERROR;
tmp = secondlinere.cap( 3 );
myobj.thick = tmp == "t" ? 1 : tmp == "tT" ? 2 : 3;
tmp = secondlinere.cap( 4 );
myobj.lineSegLength = tmp.toInt( &ok );
if ( ! ok ) KIG_FILTER_PARSE_ERROR;
tmp = secondlinere.cap( 5 );
myobj.lineSegSplit = tmp.toInt( &ok );
if ( ! ok ) KIG_FILTER_PARSE_ERROR;
tmp = secondlinere.cap( 6 );
myobj.specialAppearanceSwitch = tmp.toInt( &ok );
if ( ! ok ) KIG_FILTER_PARSE_ERROR;
tmp = secondlinere.cap( 7 );
myobj.visible = tmp == "V";
tmp = secondlinere.cap( 8 );
myobj.fixed = tmp == "St";
TQRegExp thirdlinere( "^(Const: ([^,]*),? ?)?(Val: (.*))?$" );
if ( ! thirdlinere.exactMatch( line3 ) )
KIG_FILTER_PARSE_ERROR;
tmp = thirdlinere.cap( 2 );
TQStringList parentsids = TQStringList::split( ' ', tmp );
for ( TQStringList::iterator i = parentsids.begin();
i != parentsids.end(); ++i )
{
myobj.parents.push_back( ( *i ).toInt( &ok ) );
if ( ! ok ) KIG_FILTER_PARSE_ERROR;
}
if ( myobj.parents.size() != myobj.numberOfParents )
KIG_FILTER_PARSE_ERROR;
tmp = thirdlinere.cap( 4 );
TQStringList valIds = TQStringList::split( ' ', tmp );
for ( TQStringList::iterator i = valIds.begin();
i != valIds.end(); ++i )
{
myobj.data.push_back( ( *i ).toDouble( &ok ) );
if ( ! ok ) KIG_FILTER_PARSE_ERROR;
}
// kdDebug()
// << k_funcinfo << endl
// << "id = " << myobj.id << endl
// << "type = " << myobj.type << endl
// << "numberOfParents = " << myobj.numberOfParents << endl
// << "color = " << myobj.color.name() << endl
// << "fillcolor = " << myobj.fillColor.name() << endl
// << "thick = " << myobj.thick << endl
// << "lineseglength = " << myobj.lineSegLength << endl
// << "linesegsplit = " << myobj.lineSegSplit << endl
// << "specialAppearanceSwitch = " << myobj.specialAppearanceSwitch << endl
// << "visible = " << visible << endl
// << "fixed = " << myobj.fixed << endl
// << "parents =" << endl;
// for ( std::vector<int>::iterator i = myobj.parents.begin(); i != myobj.parents.end(); ++i )
// kdDebug() << " " << *i << endl;
// kdDebug() << "vals = " << endl;
// for ( std::vector<double>::iterator i = myobj.data.begin(); i != myobj.data.end(); ++i )
// kdDebug() << " " << *i << endl;
return true;
}
KigDocument* KigFilterCabri::load( const TQString& file )
{
TQFile f( file );
if ( ! f.open( IO_ReadOnly ) )
{
fileNotFound( file );
return 0;
}
KigDocument* ret = new KigDocument();
TQString s = readLine( f );
TQString a = s.left( 21 );
TQString b = s.mid( 21 );
if( a != "FIGURE CabriII vers. " ||
( b != "DOS 1.0" && b != "MS-Windows 1.0" ) )
{
if ( s.left( 5 ) == "#FIG " )
{
notSupported( file, i18n( "This is an XFig file, not a Cabri figure." ) );
return 0;
}
else
KIG_FILTER_PARSE_ERROR;
}
// next we have:
// line 2: empty line
// line 3: window dimensions -> we don't need/use that...
// line 4: empty line
// line 5 through 8: center point
// line 9 through 12: axes
// so we skip 11 lines...
for( int i = 0; i != 11; ++i)
s = readLine( f );
// all Cabri files seem to at least have these center and axes...
if ( f.atEnd() )
KIG_FILTER_PARSE_ERROR;
std::vector<ObjectHolder*> holders;
std::vector<ObjectCalcer*> calcers;
const ObjectFactory* fact = ObjectFactory::instance();
std::vector<ObjectCalcer*> args;
ObjectCalcer* oc = 0;
while ( ! f.atEnd() )
{
CabriObject obj;
// we do one object each iteration..
if ( !readObject( f, obj ) )
return 0;
// reading linestyle
Qt::PenStyle ls = Qt::SolidLine;
if ( ( obj.lineSegLength > 1 ) && ( obj.lineSegLength < 6 ) &&
( obj.lineSegSplit > 1 ) && ( obj.lineSegSplit <= 10 ) )
ls = Qt::DotLine;
else if ( ( obj.lineSegLength >= 6 ) && ( obj.lineSegSplit > 10 ) )
ls = Qt::DashLine;
int ps = 0;
args.clear();
for ( std::vector<int>::iterator i = obj.parents.begin();
i != obj.parents.end(); ++i )
args.push_back( calcers[*i-3] );
// two fake objects at the start ( origin and axes.. )
if ( obj.id != calcers.size() + 3 ) KIG_FILTER_PARSE_ERROR;
oc = 0;
if ( obj.type == "Pt" )
{
if ( ! args.empty() ) KIG_FILTER_PARSE_ERROR;
if ( obj.data.size() != 2 ) KIG_FILTER_PARSE_ERROR;
switch ( obj.specialAppearanceSwitch )
{
case 0:
{
obj.thick -= 1;
break;
}
case 2:
{
obj.thick += 1;
break;
}
case 3:
{
obj.thick += 1;
ps = 1;
break;
}
case 4:
{
obj.thick += 2;
ps = 4;
break;
}
}
// different sizes for points..
obj.thick *= 2;
oc = fact->fixedPointCalcer( Coordinate( obj.data[0], obj.data[1] ) );
}
else if ( obj.type == "Cir" )
{
if ( args.size() == 1 )
{
if ( obj.data.size() != 1 ) KIG_FILTER_PARSE_ERROR;
ObjectConstCalcer* radc =
new ObjectConstCalcer( new DoubleImp( obj.data[0] ) );
args.push_back( radc );
oc = new ObjectTypeCalcer( CircleBPRType::instance(), args );
}
else if ( args.size() == 2 )
{
if ( ! obj.data.empty() ) KIG_FILTER_PARSE_ERROR;
oc = new ObjectTypeCalcer( CircleBCPType::instance(), args );
}
else KIG_FILTER_PARSE_ERROR;
}
else if ( obj.type == "Line" || obj.type == "Ray" || obj.type == "Seg" ||
obj.type == "Vec" )
{
if ( args.size() == 1 )
{
if ( obj.data.size() != 2 ) KIG_FILTER_PARSE_ERROR;
Coordinate vect( obj.data[0], obj.data[1] );
ObjectConstCalcer* vectorcalcer =
new ObjectConstCalcer( new VectorImp( Coordinate( 0, 0 ), vect ) );
args.push_back( vectorcalcer );
ObjectTypeCalcer* secondpoint =
new ObjectTypeCalcer( TranslatedType::instance(), args );
secondpoint->calc( *ret );
args[1] = secondpoint;
}
if ( args.size() != 2 ) KIG_FILTER_PARSE_ERROR;
const ObjectType* t = 0;
if ( obj.type == "Line" ) t = LineABType::instance();
else if ( obj.type == "Ray" ) t = RayABType::instance();
else if ( obj.type == "Seg" ) t = SegmentABType::instance();
else if ( obj.type == "Vec" ) t = VectorType::instance();
else assert( t );
oc = new ObjectTypeCalcer( t, args );
}
else if ( obj.type == "Pt/" )
{
// different sizes for points..
obj.thick *= 2;
if ( args.size() != 1 || obj.data.size() != 2 )
KIG_FILTER_PARSE_ERROR;
ObjectCalcer* parent = args[0];
if ( !parent->imp()->inherits( CurveImp::stype() ) )
KIG_FILTER_PARSE_ERROR;
const CurveImp* curve = static_cast<const CurveImp*>( parent->imp() );
Coordinate pt = Coordinate( obj.data[0], obj.data[1] );
double param = curve->getParam( pt, *ret );
args.push_back( new ObjectConstCalcer( new DoubleImp( param ) ) );
oc = new ObjectTypeCalcer( ConstrainedPointType::instance(), args );
}
else if ( obj.type == "Perp" || obj.type == "Par" )
{
if ( args.size() != 2 || obj.data.size() != 0 )
KIG_FILTER_PARSE_ERROR;
const ObjectType* t = 0;
if ( obj.type == "Perp" ) t = LinePerpendLPType::instance();
else if ( obj.type == "Par" ) t = LineParallelLPType::instance();
else assert( false );
oc = new ObjectTypeCalcer( t, args );
}
else if ( obj.type == "Arc" )
{
if ( args.size() != 3 || ! obj.data.empty() )
KIG_FILTER_PARSE_ERROR;
oc = new ObjectTypeCalcer( ArcBTPType::instance(), args );
}
else if ( obj.type == "Con" )
{
if ( args.size() != 5 || !obj.data.empty() )
KIG_FILTER_PARSE_ERROR;
oc = new ObjectTypeCalcer( ConicB5PType::instance(), args );
}
else if ( obj.type == "Mid" )
{
if ( args.size() == 2 )
{
ObjectCalcer* c =
new ObjectTypeCalcer( SegmentABType::instance(), args );
c->calc( *ret );
args.clear();
args.push_back( c );
}
// midpoint -> this can be the midpoint of a segment, two
// points, or a vector...
if ( args.size() != 1 || !obj.data.empty() )
KIG_FILTER_PARSE_ERROR;
ObjectCalcer* parent = args[0];
if ( parent->imp()->inherits( SegmentImp::stype() ) )
oc = fact->propertyObjectCalcer( parent, "mid-point" ) ;
else if ( parent->imp()->inherits( VectorImp::stype() ) )
oc = fact->propertyObjectCalcer( parent, "vect-mid-point" );
else KIG_FILTER_PARSE_ERROR;
}
else if ( obj.type == "PBiss" )
{
if ( args.size() == 2 )
{
ObjectCalcer* c =
new ObjectTypeCalcer( SegmentABType::instance(), args );
c->calc( *ret );
args.clear();
args.push_back( c );
}
if ( args.size() != 1 || !obj.data.empty() )
KIG_FILTER_PARSE_ERROR;
ObjectCalcer* parent = args[0];
ObjectCalcer* midpoint = 0;
if ( parent->imp()->inherits( SegmentImp::stype() ) )
midpoint = fact->propertyObjectCalcer( parent, "mid-point" ) ;
// else if ( parent->imp()->inherits( VectorImp::stype() ) )
// midpoint = fact->propertyObjectCalcer( parent, "vect-mid-point" );
else KIG_FILTER_PARSE_ERROR;
midpoint->calc( *ret );
args.push_back( midpoint );
oc = new ObjectTypeCalcer( LinePerpendLPType::instance(), args );
}
else if ( obj.type == "Pol" )
{
if ( args.size() < 3 || !obj.data.empty() )
KIG_FILTER_PARSE_ERROR;
oc = new ObjectTypeCalcer( PolygonBNPType::instance(), args );
}
else if ( obj.type == "Locus" )
{
if ( args.size() != 2 || !obj.data.empty() )
KIG_FILTER_PARSE_ERROR;
oc = fact->locusCalcer( args[0], args[1] );
}
else if ( obj.type == "Refl" )
{
if ( args.size() != 2 || !obj.data.empty() )
KIG_FILTER_PARSE_ERROR;
oc = new ObjectTypeCalcer( LineReflectionType::instance(), args );
}
else if ( obj.type == "Sym" )
{
if ( args.size() != 2 || !obj.data.empty() )
KIG_FILTER_PARSE_ERROR;
oc = new ObjectTypeCalcer( PointReflectionType::instance(), args );
}
else if ( obj.type == "Tran" )
{
if ( args.size() != 2 || !obj.data.empty() )
KIG_FILTER_PARSE_ERROR;
oc = new ObjectTypeCalcer( TranslatedType::instance(), args );
}
else
{
notSupported( file, i18n( "This Cabri file contains a \"%1\" object, "
"which Kig does not currently support." ).arg( obj.type ) );
return 0;
}
if ( oc == 0 ) KIG_FILTER_PARSE_ERROR;
oc->calc( *ret );
calcers.push_back( oc );
ObjectDrawer* d = new ObjectDrawer( obj.color, obj.thick, obj.visible, ls, ps );
ObjectHolder* oh = new ObjectHolder( oc, d );
holders.push_back( oh );
oc = 0;
}
ret->addObjects( holders );
ret->setGrid( false );
ret->setAxes( false );
return ret;
}
KigFilterCabri* KigFilterCabri::instance()
{
static KigFilterCabri t;
return &t;
}
|