~ubuntu-branches/ubuntu/oneiric/kig/oneiric

« back to all changes in this revision

Viewing changes to filters/cabri-utils.cc

  • Committer: Bazaar Package Importer
  • Author(s): Harald Sitter
  • Date: 2011-07-10 11:57:38 UTC
  • Revision ID: james.westby@ubuntu.com-20110710115738-gdjnn1kctr49lmy9
Tags: upstream-4.6.90+repack
ImportĀ upstreamĀ versionĀ 4.6.90+repack

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// This file is part of Kig, a KDE program for Interactive Geometry...
 
2
// Copyright (C)  2002  Dominique Devriese <devriese@kde.org>
 
3
// Copyright (C)  2006  Pino Toscano       <toscano.pino@tiscali.it>
 
4
 
 
5
// This program is free software; you can redistribute it and/or
 
6
// modify it under the terms of the GNU General Public License
 
7
// as published by the Free Software Foundation; either version 2
 
8
// of the License, or (at your option) any later version.
 
9
 
 
10
// This program is distributed in the hope that it will be useful,
 
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
// GNU General Public License for more details.
 
14
 
 
15
// You should have received a copy of the GNU General Public License
 
16
// along with this program; if not, write to the Free Software
 
17
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
18
// 02110-1301, USA.
 
19
 
 
20
#include "cabri-utils.h"
 
21
 
 
22
#include "cabri-filter.h"
 
23
 
 
24
#include <qfile.h>
 
25
#include <qregexp.h>
 
26
#include <qstring.h>
 
27
 
 
28
#include <kdebug.h>
 
29
#include <klocale.h>
 
30
 
 
31
#define KIG_CABRI_FILTER_PARSE_ERROR \
 
32
{ \
 
33
  QString locs = i18n( "An error was encountered at line %1 in file %2.", \
 
34
      __LINE__, __FILE__ ); \
 
35
  m_filter->publicParseError( file, locs ); \
 
36
  return 0; \
 
37
}
 
38
 
 
39
static std::map<QString, QColor> colormap;
 
40
static std::map<QString, QColor> colormap_v12;
 
41
typedef std::map<QString, QColor>::iterator cmit;
 
42
 
 
43
/**
 
44
 * Gives the default color for an object, in case of an object come
 
45
 * with no color specified; it's exactly what Cabri 1.2 does.
 
46
 */
 
47
static QColor defaultColorForObject( const QByteArray& type )
 
48
{
 
49
  if ( type == "Line" || type == "Ray" || type == "Seg" )
 
50
    return Qt::green;
 
51
  if ( type == "Pt" || type == "Pt/" || type == "Locus" )
 
52
    return Qt::red;
 
53
  if ( type == "Cir" )
 
54
    return Qt::blue;
 
55
  if ( type == "Pol" )
 
56
    return QColor( 254, 0, 255 );
 
57
  if ( type == "Arc" )
 
58
    return Qt::cyan;
 
59
  if ( type == "Vec" )
 
60
    return QColor( 0, 0, 128 );
 
61
  if ( type == "Text" || type == "Angle" )
 
62
    return Qt::black;
 
63
 
 
64
  return Qt::blue;
 
65
}
 
66
 
 
67
static bool extractValuesFromString( const QString& str, std::vector<int>& vec )
 
68
{
 
69
  if ( !str.isEmpty() )
 
70
  {
 
71
    QString tmp = str;
 
72
    bool ok = true;
 
73
    // extracting the digits
 
74
    QRegExp ids( "\\d+" );
 
75
    int pos = -1;
 
76
    while ( ( pos = ids.indexIn( tmp ) ) > -1 )
 
77
    {
 
78
      vec.push_back( ids.cap( 0 ).toInt( &ok ) );
 
79
      if ( !ok ) return false;
 
80
      tmp.remove( pos, ids.matchedLength() );
 
81
    }
 
82
  }
 
83
  return true;
 
84
}
 
85
 
 
86
 
 
87
namespace CabriNS
 
88
{
 
89
QString readLine( QFile& file )
 
90
{
 
91
  QString ret = file.readLine( 10000L );
 
92
  if ( !ret.isEmpty() && ret[ret.length() - 1] == '\n' )
 
93
    ret.truncate( ret.length() - 1 );
 
94
  if ( !ret.isEmpty() && ret[ret.length() - 1] == '\r' )
 
95
    ret.truncate( ret.length() - 1 );
 
96
  return ret;
 
97
}
 
98
 
 
99
QString readText( QFile& f, const QString& s, const QString& sep )
 
100
{
 
101
  QString line = s;
 
102
  if ( !line.startsWith( '\"' ) || f.atEnd() )
 
103
    // don't blame on failing here
 
104
    return QString();
 
105
 
 
106
  QString tmp = s;
 
107
  QString text = tmp;
 
108
  while ( tmp.at( tmp.length() - 1 ) != '"' )
 
109
  {
 
110
    tmp = readLine( f );
 
111
    text += sep + tmp;
 
112
  }
 
113
  QString ret = text.mid( 1, text.length() - 2 );
 
114
 
 
115
kDebug() << "+++++++++ text: \"" << ret << "\"";
 
116
 
 
117
  return ret;
 
118
}
 
119
 
 
120
} // CabriNS
 
121
 
 
122
CabriObject::CabriObject()
 
123
  : id( 0 ), thick( 1 ), lineSegLength( 0 ), lineSegSplit( 0 ), visible( true ),
 
124
    intersectionId( 0 ), ticks( 0 ), side( 0 ), textRect( Rect::invalidRect() ),
 
125
    gonio( CabriNS::CG_Deg )
 
126
{
 
127
}
 
128
 
 
129
CabriObject_v10::CabriObject_v10()
 
130
  : CabriObject(), specialAppearanceSwitch( 0 ), fixed( false )
 
131
{
 
132
}
 
133
 
 
134
CabriObject_v12::CabriObject_v12()
 
135
  : CabriObject(), pointStyle( 1 )
 
136
{
 
137
}
 
138
 
 
139
CabriReader::CabriReader( const KigFilterCabri* filter )
 
140
  : m_filter( filter )
 
141
{
 
142
  initColorMap();
 
143
}
 
144
 
 
145
CabriReader::~CabriReader()
 
146
{
 
147
}
 
148
 
 
149
void CabriReader::initColorMap()
 
150
{
 
151
  static bool colors_initialized = false;
 
152
  if ( !colors_initialized )
 
153
  {
 
154
    colors_initialized = true;
 
155
    colormap[ "R" ] = Qt::red;
 
156
    colormap[ "O" ] = Qt::magenta;
 
157
    colormap[ "Y" ] = Qt::yellow;
 
158
    colormap[ "P" ] = Qt::darkMagenta;
 
159
    colormap[ "V" ] = Qt::darkBlue;
 
160
    colormap[ "Bl" ] = Qt::blue;
 
161
    colormap[ "lBl" ] = Qt::cyan;
 
162
    colormap[ "G" ] = Qt::green;
 
163
    colormap[ "dG" ] = Qt::darkGreen;
 
164
    colormap[ "Br" ] = QColor( 165, 42, 42 );
 
165
    colormap[ "dBr" ] = QColor( 128, 128, 0 );
 
166
    colormap[ "lGr" ] = Qt::lightGray;
 
167
    colormap[ "Gr" ] = Qt::gray;
 
168
    colormap[ "dGr" ] = Qt::darkGray;
 
169
    colormap[ "B" ] = Qt::black;
 
170
    colormap[ "W" ] = Qt::white;
 
171
  }
 
172
}
 
173
 
 
174
QColor CabriReader::translateColor( const QString& s )
 
175
{
 
176
  initColorMap();
 
177
  cmit it = colormap.find( s );
 
178
  if ( it != colormap.end() ) return (*it).second;
 
179
 
 
180
  kDebug() << "unknown color: " << s;
 
181
  return Qt::black;
 
182
}
 
183
 
 
184
 
 
185
CabriReader_v10::CabriReader_v10( const KigFilterCabri* filter )
 
186
  : CabriReader( filter )
 
187
{
 
188
}
 
189
 
 
190
CabriReader_v10::~CabriReader_v10()
 
191
{
 
192
}
 
193
 
 
194
bool CabriReader_v10::readWindowMetrics( QFile& f )
 
195
{
 
196
  QString file = f.fileName();
 
197
 
 
198
  QString line = CabriNS::readLine( f );
 
199
  QRegExp first( "^Window center x: (.+) y: (.+) Window size x: (.+) y: (.+)$" );
 
200
  if ( !first.exactMatch( line ) )
 
201
    KIG_CABRI_FILTER_PARSE_ERROR;
 
202
 
 
203
  line = CabriNS::readLine( f );
 
204
 
 
205
  return true;
 
206
}
 
207
 
 
208
CabriObject* CabriReader_v10::readObject( QFile& f )
 
209
{
 
210
  CabriObject_v10* myobj = new CabriObject_v10();
 
211
 
 
212
  QString file = f.fileName();
 
213
 
 
214
  bool ok;
 
215
  QString tmp;
 
216
 
 
217
  QString line1 = CabriNS::readLine( f );
 
218
  QRegExp namelinere( "^\"([^\"]+)\", NP: ([\\d-]+), ([\\d-]+), NS: (\\d+), (\\d+)$" );
 
219
  if ( namelinere.exactMatch( line1 ) )
 
220
  {
 
221
    tmp = namelinere.cap( 1 );
 
222
    myobj->name = tmp;
 
223
 
 
224
    line1 = CabriNS::readLine( f );
 
225
  }
 
226
 
 
227
  QRegExp firstlinere( "^([^:]+): ([^,]+), ([^,]+), CN:([^,]*), VN:(.*)$" );
 
228
  if ( ! firstlinere.exactMatch( line1 ) )
 
229
    KIG_CABRI_FILTER_PARSE_ERROR;
 
230
 
 
231
  tmp = firstlinere.cap( 1 );
 
232
  myobj->id = tmp.toUInt( &ok );
 
233
  if ( !ok ) KIG_CABRI_FILTER_PARSE_ERROR;
 
234
 
 
235
  tmp = firstlinere.cap( 2 );
 
236
  myobj->type = tmp.toLatin1();
 
237
 
 
238
  tmp = firstlinere.cap( 3 );
 
239
  myobj->specification = tmp.toInt( &ok );
 
240
  if ( !ok ) KIG_CABRI_FILTER_PARSE_ERROR;
 
241
  // for "Eq/Co" this is presumably what we want:
 
242
  //  0: x coordinate (of point)
 
243
  //  1: y coordinate (of point)
 
244
 
 
245
  tmp = firstlinere.cap( 4 );
 
246
  uint numberOfParents = tmp.toUInt( &ok );
 
247
  if ( !ok ) KIG_CABRI_FILTER_PARSE_ERROR;
 
248
 
 
249
  tmp = firstlinere.cap( 5 );
 
250
  // i have no idea what this number means..
 
251
 
 
252
  QString line2 = CabriNS::readLine( f );
 
253
  QRegExp secondlinere( "^([^,]+), ([^,]+), ([^,]+), DS:([^ ]+) ([^,]+), GT:([^,]+), ([^,]+), (.*)$" );
 
254
  QRegExp secondlinere2( "^([^,]+), ([^,]+), NbD:([^,]+), ([^,]+), ([^,]+), GT:([^,]+), ([^,]+), (.*)$" );
 
255
  if ( secondlinere.exactMatch( line2 ) )
 
256
  {
 
257
    tmp = secondlinere.cap( 1 );
 
258
    myobj->color = translateColor( tmp );
 
259
 
 
260
    tmp = secondlinere.cap( 2 );
 
261
    myobj->fillColor = translateColor( tmp );
 
262
 
 
263
    tmp = secondlinere.cap( 3 );
 
264
    myobj->thick = tmp == "t" ? 1 : tmp == "tT" ? 2 : 3;
 
265
 
 
266
    tmp = secondlinere.cap( 4 );
 
267
    myobj->lineSegLength = tmp.toInt( &ok );
 
268
    if ( !ok ) KIG_CABRI_FILTER_PARSE_ERROR;
 
269
 
 
270
    tmp = secondlinere.cap( 5 );
 
271
    myobj->lineSegSplit = tmp.toInt( &ok );
 
272
    if ( !ok ) KIG_CABRI_FILTER_PARSE_ERROR;
 
273
 
 
274
    tmp = secondlinere.cap( 6 );
 
275
    myobj->specialAppearanceSwitch = tmp.toInt( &ok );
 
276
    if ( !ok ) KIG_CABRI_FILTER_PARSE_ERROR;
 
277
 
 
278
    tmp = secondlinere.cap( 7 );
 
279
    myobj->visible = tmp == "V";
 
280
 
 
281
    tmp = secondlinere.cap( 8 );
 
282
    myobj->fixed = tmp == "St";
 
283
  }
 
284
  else if ( secondlinere2.exactMatch( line2 ) )  // e.g. for AngVal
 
285
  {
 
286
    tmp = secondlinere2.cap( 1 );
 
287
    myobj->color = translateColor( tmp );
 
288
 
 
289
    tmp = secondlinere2.cap( 2 );
 
290
    myobj->fillColor = translateColor( tmp );
 
291
 
 
292
    // 3: e.g. "NbD:129" what is the meaning?
 
293
    // 4: e.g. "FD"
 
294
 
 
295
    tmp = secondlinere2.cap( 5 );
 
296
    if ( tmp == "deg" )
 
297
      myobj->gonio = CabriNS::CG_Deg;
 
298
 
 
299
    tmp = secondlinere2.cap( 6 );
 
300
    myobj->specialAppearanceSwitch = tmp.toInt( &ok );
 
301
    if ( !ok ) KIG_CABRI_FILTER_PARSE_ERROR;
 
302
 
 
303
    tmp = secondlinere2.cap( 7 );
 
304
    myobj->visible = tmp == "V";
 
305
 
 
306
    tmp = secondlinere2.cap( 8 );
 
307
    myobj->fixed = tmp == "St";
 
308
 
 
309
  }
 
310
  else
 
311
    KIG_CABRI_FILTER_PARSE_ERROR;
 
312
 
 
313
  QString line3 = CabriNS::readLine( f );
 
314
  QRegExp thirdlinere( "^(Const: ([^,]*),? ?)?(Val: ([^,]*)(,(.*))?)?$" );
 
315
  if ( ! thirdlinere.exactMatch( line3 ) )
 
316
    KIG_CABRI_FILTER_PARSE_ERROR;
 
317
 
 
318
  tmp = thirdlinere.cap( 2 );
 
319
  const QStringList parentsids = tmp.split( ' ', QString::SkipEmptyParts );
 
320
  for ( QStringList::const_iterator i = parentsids.begin();
 
321
        i != parentsids.end(); ++i )
 
322
  {
 
323
    myobj->parents.push_back( ( *i ).toInt( &ok ) );
 
324
    if ( !ok ) KIG_CABRI_FILTER_PARSE_ERROR;
 
325
  }
 
326
  if ( myobj->parents.size() != numberOfParents )
 
327
    KIG_CABRI_FILTER_PARSE_ERROR;
 
328
 
 
329
  tmp = thirdlinere.cap( 4 );
 
330
  const QStringList valIds = tmp.split( ' ', QString::SkipEmptyParts );
 
331
  for ( QStringList::const_iterator i = valIds.begin();
 
332
        i != valIds.end(); ++i )
 
333
  {
 
334
    myobj->data.push_back( ( *i ).toDouble( &ok ) );
 
335
    if ( !ok ) KIG_CABRI_FILTER_PARSE_ERROR;
 
336
  }
 
337
 
 
338
  QString thirdlineextra = thirdlinere.cap( 6 );
 
339
  if ( myobj->type == "Text" || myobj->type == "Formula" )
 
340
  {
 
341
    QRegExp textMetrics( "TP: *[\\s]*([^,]+), *[\\s]*([^,]+), *TS:[\\s]*([^,]+), *[\\s]*([^,]+)" );
 
342
    if ( textMetrics.indexIn( thirdlineextra ) != -1 )
 
343
    {
 
344
      double xa = textMetrics.cap( 1 ).toDouble( &ok );
 
345
      if ( !ok ) KIG_CABRI_FILTER_PARSE_ERROR;
 
346
      double ya = textMetrics.cap( 2 ).toDouble( &ok );
 
347
      if ( !ok ) KIG_CABRI_FILTER_PARSE_ERROR;
 
348
      double tw = textMetrics.cap( 3 ).toDouble( &ok );
 
349
      if ( !ok ) KIG_CABRI_FILTER_PARSE_ERROR;
 
350
      double th = textMetrics.cap( 4 ).toDouble( &ok );
 
351
      if ( !ok ) KIG_CABRI_FILTER_PARSE_ERROR;
 
352
      myobj->textRect = Rect( xa, ya, tw, th );
 
353
    }
 
354
    QString textline = CabriNS::readLine( f );
 
355
    if ( textline.isEmpty() )
 
356
      KIG_CABRI_FILTER_PARSE_ERROR;
 
357
    if ( myobj->type == "Formula" )
 
358
    {
 
359
      // Just hope there is no escaping!
 
360
      myobj->text = textline;
 
361
    }
 
362
    else
 
363
      myobj->text = CabriNS::readText( f, textline, "\n" );
 
364
 
 
365
    // Hack: extracting substitution arguments from the parents list
 
366
    // and inserting them into the "incs" vector
 
367
    // under the assumption that the required parents are the last
 
368
    // in the arguments list (in forward order)
 
369
    // note that we do not check for special escaping of the symbols
 
370
    // '"' and '#'
 
371
    int count = myobj->text.count( "\"#" );
 
372
    int parentsnum = myobj->parents.size();
 
373
    for ( int i = parentsnum - count; i < parentsnum; ++i )
 
374
      myobj->incs.push_back( myobj->parents[i] );
 
375
  }
 
376
 
 
377
  QString line4 = CabriNS::readLine( f );
 
378
  if ( !line4.isEmpty() )
 
379
  {
 
380
    QRegExp fontlinere( "^p: (\\d+), ([^,]+), S: (\\d+) C: (\\d+) Fa: (\\d+)$" );
 
381
    if ( !fontlinere.exactMatch( line4 ) )
 
382
      KIG_CABRI_FILTER_PARSE_ERROR;
 
383
 
 
384
    line4 = CabriNS::readLine( f );
 
385
  }
 
386
 
 
387
//     kDebug()
 
388
//       << endl
 
389
//       << "id = " << myobj->id << endl
 
390
//       << "type = " << myobj->type << endl
 
391
//       << "numberOfParents = " << numberOfParents << endl
 
392
//       << "color = " << myobj->color.name() << endl
 
393
//       << "fillcolor = " << myobj->fillColor.name() << endl
 
394
//       << "thick = " << myobj->thick << endl
 
395
//       << "lineseglength = " << myobj->lineSegLength << endl
 
396
//       << "linesegsplit = " << myobj->lineSegSplit << endl
 
397
//       << "specialAppearanceSwitch = " << myobj->specialAppearanceSwitch << endl
 
398
//       << "visible = " << myobj->visible << endl
 
399
//       << "fixed = " << myobj->fixed << endl
 
400
//       << "parents =" << endl;
 
401
//     for ( std::vector<int>::iterator i = myobj->parents.begin(); i != myobj->parents.end(); ++i )
 
402
//       kDebug() << "  " << *i;
 
403
//     kDebug() << "vals = ";
 
404
//     for ( std::vector<double>::iterator i = myobj->data.begin(); i != myobj->data.end(); ++i )
 
405
//       kDebug() << "  " << *i;
 
406
 
 
407
  return myobj;
 
408
}
 
409
 
 
410
void CabriReader_v10::decodeStyle( CabriObject* obj, Qt::PenStyle& ps, int& pointType )
 
411
{
 
412
  CabriObject_v10* myobj = (CabriObject_v10*)obj;
 
413
 
 
414
  if ( ( myobj->type == "Pt" ) || ( myobj->type == "Pt/" ) )
 
415
  {
 
416
    switch ( myobj->specialAppearanceSwitch )
 
417
    {
 
418
      case 0:
 
419
      {
 
420
        myobj->thick -= 1;
 
421
        break;
 
422
      }
 
423
      case 2:
 
424
      {
 
425
        myobj->thick += 1;
 
426
        break;
 
427
      }
 
428
      case 3:
 
429
      {
 
430
        myobj->thick += 1;
 
431
        pointType = 1;
 
432
        break;
 
433
      }
 
434
      case 4:
 
435
      {
 
436
        myobj->thick += 2;
 
437
        pointType = 4;
 
438
        break;
 
439
      }
 
440
    }
 
441
    myobj->thick *= 2;
 
442
  }
 
443
  else
 
444
  {
 
445
    if ( ( myobj->lineSegLength > 1 ) && ( myobj->lineSegLength < 6 ) &&
 
446
         ( myobj->lineSegSplit > 1 ) && ( myobj->lineSegSplit <= 10 ) )
 
447
      ps = Qt::DotLine;
 
448
    else if ( ( myobj->lineSegLength >= 6 ) && ( myobj->lineSegSplit > 10 ) )
 
449
      ps = Qt::DashLine;
 
450
  }
 
451
}
 
452
 
 
453
CabriReader_v12::CabriReader_v12( const KigFilterCabri* filter )
 
454
  : CabriReader( filter )
 
455
{
 
456
  initColorMap();
 
457
}
 
458
 
 
459
CabriReader_v12::~CabriReader_v12()
 
460
{
 
461
}
 
462
 
 
463
void CabriReader_v12::initColorMap()
 
464
{
 
465
  static bool colors_initialized = false;
 
466
  if ( !colors_initialized )
 
467
  {
 
468
    colors_initialized = true;
 
469
    CabriReader::initColorMap();
 
470
    colormap_v12 = colormap;
 
471
    colormap_v12[ "dkg" ] = QColor( 0, 100, 0 );
 
472
    colormap_v12[ "old" ] = QColor( 107, 142, 35 );
 
473
    colormap_v12[ "olv" ] = QColor( 128, 128, 0 );
 
474
    colormap_v12[ "lig" ] = QColor( 50, 205, 50 );
 
475
    colormap_v12[ "gry" ] = QColor( 173, 255, 47 );
 
476
    colormap_v12[ "gor" ] = QColor( 218, 165, 32 );
 
477
    colormap_v12[ "msg" ] = QColor( 0, 250, 154 );
 
478
    colormap_v12[ "spg" ] = QColor( 0, 255, 127 );
 
479
    colormap_v12[ "pag" ] = QColor( 152, 251, 152 );
 
480
    colormap_v12[ "kki" ] = QColor( 240, 230, 140 );
 
481
    colormap_v12[ "O" ] = QColor( 255, 140, 0 );
 
482
    colormap_v12[ "Br" ] = QColor( 165, 42, 42 );
 
483
    colormap_v12[ "tea" ] = QColor( 0, 128, 128 );
 
484
    colormap_v12[ "pat" ] = QColor( 175, 238, 238 );
 
485
    colormap_v12[ "ltp" ] = QColor( 255, 182, 193 );
 
486
    colormap_v12[ "dBr" ] = QColor( 128, 0, 0 );
 
487
    colormap_v12[ "lsg" ] = QColor( 32, 178, 170 );
 
488
    colormap_v12[ "dob" ] = QColor( 30, 144, 255 );
 
489
    colormap_v12[ "skb" ] = QColor( 135, 206, 235 );
 
490
    colormap_v12[ "plm" ] = QColor( 221, 160, 221 );
 
491
    colormap_v12[ "dep" ] = QColor( 255, 20, 147 );
 
492
    colormap_v12[ "crs" ] = QColor( 220, 20, 60 );
 
493
    colormap_v12[ "rob" ] = QColor( 65, 105, 225 );
 
494
    colormap_v12[ "blv" ] = QColor( 138, 43, 226 );
 
495
    colormap_v12[ "ma" ] = QColor( 254, 0, 255 );
 
496
    colormap_v12[ "mvr" ] = QColor( 199, 21, 133 );
 
497
    colormap_v12[ "ind" ] = QColor( 75, 0, 130 );
 
498
    colormap_v12[ "meo" ] = QColor( 186, 85, 211 );
 
499
    colormap_v12[ "Gr" ] = QColor( 128, 128, 128 );
 
500
    colormap_v12[ "dGr" ] = QColor( 64, 64, 64 );
 
501
  }
 
502
}
 
503
 
 
504
bool CabriReader_v12::readWindowMetrics( QFile& f )
 
505
{
 
506
  QString file = f.fileName();
 
507
 
 
508
  QString line = CabriNS::readLine( f );
 
509
  QRegExp first( "^Window center x: (.+) y: (.+) Window size x: (.+) y: (.+)$" );
 
510
  if ( !first.exactMatch( line ) )
 
511
    KIG_CABRI_FILTER_PARSE_ERROR;
 
512
 
 
513
  QString line2 = CabriNS::readLine( f );
 
514
  QRegExp second( "^Resolution: (\\d+) ppc$" );
 
515
  if ( !second.exactMatch( line2 ) )
 
516
    KIG_CABRI_FILTER_PARSE_ERROR;
 
517
 
 
518
  line = CabriNS::readLine( f );
 
519
 
 
520
  return true;
 
521
}
 
522
 
 
523
CabriObject* CabriReader_v12::readObject( QFile& f )
 
524
{
 
525
  CabriObject_v12* myobj = new CabriObject_v12();
 
526
 
 
527
  QString file = f.fileName();
 
528
  QString line = CabriNS::readLine( f );
 
529
 
 
530
#ifdef CABRI_DEBUG
 
531
  kDebug() << "+++++++++ line: \"" << line << "\"";
 
532
#endif
 
533
  QRegExp firstlinere( "^([^:]+): ([^,]+), (Const: [,0-9\\s]+)?(int ind:([^,]+),\\s)?(cart, )?(side:(\\d+), )?(inc\\.elmts: ([,0-9\\s]+))?(axis:(x|y), )?(on mark, )?(Val: ([^,]+))?(.*)$" );
 
534
  if ( !firstlinere.exactMatch( line ) )
 
535
    KIG_CABRI_FILTER_PARSE_ERROR;
 
536
 
 
537
  bool ok = true;
 
538
  QString tmp;
 
539
 
 
540
  // id
 
541
  tmp = firstlinere.cap( 1 );
 
542
  myobj->id = tmp.toUInt( &ok );
 
543
  if ( !ok ) KIG_CABRI_FILTER_PARSE_ERROR;
 
544
 
 
545
  // type
 
546
  tmp = firstlinere.cap( 2 );
 
547
  myobj->type = tmp.toLatin1();
 
548
 
 
549
  // parents
 
550
  tmp = firstlinere.cap( 3 );
 
551
  if ( !extractValuesFromString( tmp, myobj->parents ) )
 
552
    KIG_CABRI_FILTER_PARSE_ERROR;
 
553
 
 
554
  // data
 
555
  tmp = firstlinere.cap( 15 );
 
556
  const QStringList valIds = tmp.split( ' ', QString::SkipEmptyParts );
 
557
  for ( QStringList::const_iterator i = valIds.begin(); i != valIds.end(); ++i )
 
558
  {
 
559
    myobj->data.push_back( ( *i ).toDouble( &ok ) );
 
560
    if ( !ok ) KIG_CABRI_FILTER_PARSE_ERROR;
 
561
  }
 
562
 
 
563
  // intersection id (if present)
 
564
  tmp = firstlinere.cap( 5 );
 
565
  if ( !tmp.isEmpty() )
 
566
  {
 
567
    long intId = tmp.toLong( &ok, 16 );
 
568
    if ( !ok ) KIG_CABRI_FILTER_PARSE_ERROR;
 
569
#ifdef CABRI_DEBUG
 
570
    kDebug() << "+++++++++ intId: " << intId;
 
571
#endif
 
572
    if ( intId == 0 ) myobj->intersectionId = -1;
 
573
    else if ( intId == 0x10000 ) myobj->intersectionId = 1;
 
574
    else KIG_CABRI_FILTER_PARSE_ERROR;
 
575
  }
 
576
 
 
577
  // side of a polygon (if present)
 
578
  tmp = firstlinere.cap( 8 );
 
579
  if ( !tmp.isEmpty() )
 
580
  {
 
581
    myobj->side = tmp.toInt( &ok );
 
582
    if ( !ok ) KIG_CABRI_FILTER_PARSE_ERROR;
 
583
  }
 
584
 
 
585
  // inc.elements (if present)
 
586
  tmp = firstlinere.cap( 10 );
 
587
  if ( !extractValuesFromString( tmp, myobj->incs ) )
 
588
    KIG_CABRI_FILTER_PARSE_ERROR;
 
589
 
 
590
  line = CabriNS::readLine( f );
 
591
 
 
592
  QRegExp textMetrics( "^TP:[\\s]*([^,]+),[\\s]*([^,]+), TS:[\\s]*([^,]+),[\\s]*([^,]+)$" );
 
593
  bool freeText = false;
 
594
  while ( !line.isEmpty() )
 
595
  {
 
596
    if ( line.startsWith( '\"' ) )
 
597
    {
 
598
      QString txt = CabriNS::readText( f, line );
 
599
      if ( myobj->type != "Text" )
 
600
        myobj->name = txt;
 
601
      else
 
602
        myobj->text = txt;
 
603
    }
 
604
    else if ( line.startsWith( "NbD:" ) )
 
605
    {
 
606
      // TODO
 
607
    }
 
608
    else if ( textMetrics.exactMatch( line ) )
 
609
    {
 
610
      double xa = textMetrics.cap( 1 ).toDouble( &ok );
 
611
      if ( !ok ) KIG_CABRI_FILTER_PARSE_ERROR;
 
612
      double ya = textMetrics.cap( 2 ).toDouble( &ok );
 
613
      if ( !ok ) KIG_CABRI_FILTER_PARSE_ERROR;
 
614
      double tw = textMetrics.cap( 3 ).toDouble( &ok );
 
615
      if ( !ok ) KIG_CABRI_FILTER_PARSE_ERROR;
 
616
      double th = textMetrics.cap( 4 ).toDouble( &ok );
 
617
      if ( !ok ) KIG_CABRI_FILTER_PARSE_ERROR;
 
618
      myobj->textRect = Rect( xa, ya, tw, th );
 
619
    }
 
620
    else
 
621
    {
 
622
      if ( !freeText && myobj->type == "Formula" )
 
623
      {
 
624
        myobj->text = line;
 
625
        freeText = true;
 
626
      }
 
627
      else
 
628
        readStyles( file, line, myobj );
 
629
    }
 
630
 
 
631
    line = CabriNS::readLine( f );
 
632
  }
 
633
 
 
634
  // default color
 
635
  if ( !myobj->color.isValid() )
 
636
    myobj->color = defaultColorForObject( myobj->type );
 
637
 
 
638
  return myobj;
 
639
}
 
640
 
 
641
void CabriReader_v12::decodeStyle( CabriObject* obj, Qt::PenStyle& ps, int& pointType )
 
642
{
 
643
  CabriObject_v12* myobj = (CabriObject_v12*)obj;
 
644
 
 
645
  if ( ( myobj->type == "Pt" ) || ( myobj->type == "Pt/" ) )
 
646
  {
 
647
      // different sizes for points..
 
648
      myobj->thick *= 2;
 
649
      switch ( myobj->pointStyle )
 
650
      {
 
651
        case 0:
 
652
        {
 
653
          myobj->thick /= 2;
 
654
          break;
 
655
        }
 
656
        case 1:
 
657
        {
 
658
          pointType = 2;
 
659
          break;
 
660
        }
 
661
        case 2:
 
662
        {
 
663
          pointType = 1;
 
664
          break;
 
665
        }
 
666
        case 3:
 
667
        {
 
668
          pointType = 4;
 
669
          break;
 
670
        }
 
671
        case 4:
 
672
        {
 
673
          break;
 
674
        }
 
675
      }
 
676
  }
 
677
  else
 
678
  {
 
679
    if ( ( myobj->lineSegLength > 1 ) && ( myobj->lineSegLength < 6 ) &&
 
680
         ( myobj->lineSegSplit > 1 ) && ( myobj->lineSegSplit <= 10 ) )
 
681
      ps = Qt::DotLine;
 
682
    else if ( ( myobj->lineSegLength >= 6 ) && ( myobj->lineSegSplit > 10 ) )
 
683
      ps = Qt::DashLine;
 
684
  }
 
685
}
 
686
 
 
687
QColor CabriReader_v12::translateColor( const QString& s )
 
688
{
 
689
  initColorMap();
 
690
  cmit it = colormap_v12.find( s );
 
691
  if ( it != colormap_v12.end() ) return (*it).second;
 
692
 
 
693
  kDebug() << "unknown color: " << s;
 
694
  return CabriReader::translateColor( s );
 
695
}
 
696
 
 
697
bool CabriReader_v12::readStyles( const QString& file, const QString& line, CabriObject_v12* myobj )
 
698
{
 
699
#ifdef CABRI_DEBUG
 
700
  kDebug() << ">>>>>>>>> style line: \"" << line << "\"";
 
701
#endif
 
702
  QStringList styles = line.split( ", ", QString::SkipEmptyParts );
 
703
  bool ok = true;
 
704
  for ( QStringList::iterator it = styles.begin(); it != styles.end(); ++it )
 
705
  {
 
706
    if ( (*it) == "invisible" )
 
707
    {
 
708
      myobj->visible = false;
 
709
    }
 
710
    else if ( (*it).startsWith( "DS:" ) )
 
711
    {
 
712
      QRegExp ticks( "DS:(\\d+)\\s(\\d+)" );
 
713
      if ( ticks.exactMatch( (*it) ) )
 
714
      {
 
715
        myobj->lineSegLength = ticks.cap( 1 ).toInt( &ok );
 
716
        if ( !ok ) KIG_CABRI_FILTER_PARSE_ERROR;
 
717
        myobj->lineSegSplit = ticks.cap( 2 ).toInt( &ok );
 
718
        if ( !ok ) KIG_CABRI_FILTER_PARSE_ERROR;
 
719
      }
 
720
    }
 
721
    // colors
 
722
    else if ( (*it).startsWith( "color:" ) )
 
723
    {
 
724
      QString color = (*it).remove( 0, 6 );
 
725
      myobj->color = translateColor( color );
 
726
    }
 
727
    else if ( (*it).startsWith( "fill color:" ) )
 
728
    {
 
729
      QString color = (*it).remove( 0, 11 );
 
730
      myobj->fillColor = translateColor( color );
 
731
    }
 
732
    // styles
 
733
    else if ( (*it) == "thicker" )
 
734
    {
 
735
      myobj->thick = 2;
 
736
    }
 
737
    else if ( (*it) == "thick" )
 
738
    {
 
739
      myobj->thick = 3;
 
740
    }
 
741
    // point types
 
742
    else if ( (*it) == "1x1" )
 
743
    {
 
744
      myobj->pointStyle = 0;
 
745
    }
 
746
    else if ( (*it) == "CIRCLE" )
 
747
    {
 
748
      myobj->pointStyle = 2;
 
749
    }
 
750
    else if ( (*it) == "TIMES" )
 
751
    {
 
752
      myobj->pointStyle = 3;
 
753
    }
 
754
    else if ( (*it) == "PLUS" )
 
755
    {
 
756
      myobj->pointStyle = 4;
 
757
    }
 
758
    // goniometry systems
 
759
    else if ( (*it) == "deg" )
 
760
    {
 
761
      myobj->gonio = CabriNS::CG_Deg;
 
762
    }
 
763
    // object ticks
 
764
    else if ( (*it).startsWith( "marks nb:" ) )
 
765
    {
 
766
      QString strticks = (*it).remove( 0, 9 );
 
767
      myobj->ticks = strticks.toInt( &ok );
 
768
      if ( !ok ) KIG_CABRI_FILTER_PARSE_ERROR;
 
769
    }
 
770
#ifdef CABRI_DEBUG
 
771
    else
 
772
    {
 
773
      kDebug() << ">>>>>>>>> UNKNOWN STYLE STRING: \"" << *it << "\"";
 
774
    }
 
775
#endif
 
776
  }
 
777
  return true;
 
778
}