File: G_ref.cpp

package info (click to toggle)
kseg 0.4.0.3-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 3,160 kB
  • ctags: 2,052
  • sloc: cpp: 14,632; makefile: 10
file content (460 lines) | stat: -rw-r--r-- 11,627 bytes parent folder | download | duplicates (2)
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
/*
 *  KSeg
 *  Copyright (C) 1999-2006 Ilya Baran
 *
 *  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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Send comments and/or bug reports to:
 *                 ibaran@mit.edu
 */


#include "G_ref.H"
#include "KSegDocument.H"
#include "KSegConstruction.H"
#include "G_object.H"
#include <qptrdict.h>
#include "G_drawstyle.H"
#include <qpainter.h>
#include "G_undo.H"
#include "my_hash_set.H"
#include <qapplication.h>
#include "KSegSelectionGroup.H"

G_ref::G_ref(KSegDocument *inDoc)
  : label(this)
{
  doc = inDoc;
  where = NULL;
  exists = visible = true;
  labelVisible = given = final = deleted = selected = false;
  drawstyle = G_drawstyle::match();
}

G_ref::~G_ref()
{
  drawstyle->deleteReference();
  if(where != NULL && !deleted) delete where;
  drawstyle = NULL; //so it definitely crashes if deleted twice
  
  set<KSegSelectionGroup *>::iterator it;
  for(it = selectionGroups.begin(); it != selectionGroups.end(); ++it) {
    (*it)->deleted(this);
  }
}

void G_ref::create(G_Type inType, int inDescendType, const G_refs &inParents, KSegDocument *inDoc, bool inAutoLabel)
{

  type = inType;
  descendType = inDescendType;

  parents = inParents;

  doc = inDoc;

  drawstyle->deleteReference();
  drawstyle = inDoc->getDefaultDrawstyle();
  drawstyle->addReference();

  if(type == G_LOOP) visible = false;

  createObject();

  update();

  labelVisible = false;
  if(inAutoLabel)
    label.setString(doc->autoLabel(this));
}


void G_ref::createObject()
{
  deleted = false;
  selected = false;

  if(doc) {
    doc->addRef(this);
    doc->addUndo(new G_undoCreate(this));
  }

  int i;
  for(i = 0; i < (int)parents.count(); i++) {
    parents[i]->children.append(this);
  }

  //create new object
  switch(type) {
  case G_POINT:
    where = new G_pointObject(this);
    break;
  case G_LINE:
    where = new G_lineObject(this);
    break;
  case G_SEGMENT:
    where = new G_segmentObject(this);
    break;
  case G_RAY:
    where = new G_rayObject(this);
    break;
  case G_CIRCLE:
    where = new G_circleObject(this);
    break;
  case G_ARC:
    where = new G_arcObject(this);
    break;
  case G_ARCSECTOR:
    where = new G_arcSectorObject(this);
    break;
  case G_ARCSEGMENT:
    where = new G_arcSegmentObject(this);
    break;
  case G_CIRCLEINTERIOR:
    where = new G_circleInteriorObject(this);
    break;
  case G_POLYGON:
    where = new G_polygonObject(this);
    break;
  case G_LOCUS:
    where = new G_locusObject(this);
    break;
  case G_MEASURE:
    where = new G_measureObject(this);
    break;
  case G_CALCULATE:
    where = new G_calculateObject(this);
    break;
  case G_LOOP:
    where = NULL;
    break;
  default:
    qFatal("Unknown type %d!\n", type);
    where = NULL;
    break;
  }

  exists = true;
}


void G_ref::remove()
{
  int i;

  ASSERT(children.count() == 0);

  //undo stuff:
  doc->addUndo(new G_undoDelete(this));

  doc->delRef(this);

  for(i = 0; i < (int)parents.count(); i++) {
    parents[i]->children.removeRef(this);
  }

  if(where) delete where;

  deleted = true;
}


void G_ref::reconstrain(int inDescendType, const G_refs &inParents, bool topSortAfter)
{
  //topSortAfter is true by default--pasing false is useful if you are doing
  //several reconstraints one after another.
  int i;

  if(inDescendType == descendType && inParents == parents) return;

  for(i = 0; i < (int)parents.count(); i++) {
    parents[i]->children.removeRef(this);
  }

  parents = inParents;

  for(i = 0; i < (int)parents.count(); i++) {
    parents[i]->children.append(this);
  }

  descendType = inDescendType;

  if(topSortAfter) doc->topSortAllRefs();
}


void G_ref::update(bool fromLocus)
{
  if(type == G_LOOP) { visible = false; return; }

  int i;
  for(i = 0; i < (int)parents.count(); i++) {
    if(!parents[i]->getExists()) {
      if(type != G_LOCUS || descendType != G_OBJECT_LOCUS || i != 1) {
	exists = false;
	return;
      }
    }
  }
  exists = true;

  if(fromLocus && (type & G_VALUE)) {
    //if we are coming from a locus, don't parse the formula
    ((G_valueObject *)where)->calculate();
  }
  else where->update();
}


void G_ref::drawLabel(QPainter &p)
{
  if(labelVisible) {
    ASSERT( (type & (G_POINT | G_CURVE)) );

    label.draw(p, *drawstyle, selected);
  }
}

void G_ref::changeDrawstyle(G_drawstyle *d)
{
  if(drawstyle != d) {
    bool sameFont = (drawstyle->getFont() == d->getFont());

    doc->addUndo(new G_undoChangeDrawstyle(this));
    setDrawstyle(d);

    if(!sameFont || !labelVisible) {
      QPixmap tmp(1, 1);
      QPainter tmpp(&tmp);
      label.draw(tmpp, *d, false);
      label.setPos(label.getPos());
    }
    
    if(type & G_TEXT && !isDrawn()) {
      QPixmap tmp(1, 1);
      QPainter tmpp(&tmp);
      where->draw(tmpp);
    }

  }
  else {
    d->deleteReference();
  }
}


void G_ref::setDrawstyle(G_drawstyle *d)
{
  drawstyle->deleteReference();
  drawstyle = d;
}


//which types may be substituted for this type if this is a given
int G_ref::whatCanItBe()
{
  static hash_set<G_ref *> refsConsidered; // prevents infinite recursion

  //if it's a measurement or calculation, it can be either
  if(type & G_VALUE) return G_VALUE;

  //if it's a filled, it can be any filled:
  if(type & G_FILLED) return G_FILLED;
  
  //otherwise, if it's not some curve, it can only be itself
  if(!(type & G_CURVE)) return type;

  if(refsConsidered.count(this)) return G_CURVE; //if it's already been considered

  int soFar = G_CURVE; //stores what we haven't eliminated yet
  //now go through the children and eliminate possibilities
  int i;
  for(i = 0; i < (int)children.size(); ++i) {
    if(soFar == type) { //if we eliminated all possibilities
      return type;
    }

    //only a segment can be used for scaling
    if(type == G_SEGMENT && (children[i]->type & G_GEOMETRIC) &&
       children[i]->descendType == G_SCALED && children[i]->parents[0] != this) {
      soFar &= G_SEGMENT;
      continue;
    }

    //for transforms, determine possibilities recursively
    if(children[i]->type == type && IS_TRANSFORM(children[i]->descendType)) {
      refsConsidered.insert(this);
      soFar &= children[i]->whatCanItBe();
      refsConsidered.erase(this);
      continue;
    }

    //check point children
    if(children[i]->type == G_POINT) {
      //only segments have midpoints
      if(children[i]->descendType == G_MID_POINT) return G_SEGMENT;
      if(children[i]->descendType == G_INTERSECTION2_POINT) {
	if((type & G_STRAIGHT) == 0) { //if it's a curve, it must stay a curve
	  soFar &= (G_CURVE - G_STRAIGHT);
	}
      }
      if(children[i]->descendType == G_END_POINT) soFar &= (G_SEGMENT | G_RAY | G_ARC);
      if(children[i]->descendType == G_END2_POINT) soFar &= (G_SEGMENT | G_ARC);
      if(children[i]->descendType == G_CENTER_POINT) soFar &= (G_CIRCLE | G_ARC);
      continue;
    }

    //check line children
    if(children[i]->type == G_LINE) {
      if(children[i]->descendType == G_PERPENDICULAR_LINE || 
	 children[i]->descendType == G_PARALLEL_LINE) {
	soFar &= G_STRAIGHT;
	continue;
      }
    }

    if(children[i]->type == G_CIRCLE && //only a segment can be a radius of a circle
       children[i]->descendType == G_CENTERRADIUS_CIRCLE) return G_SEGMENT;

    if(children[i]->type == G_MEASURE) {
      if(children[i]->descendType == G_LENGTH_MEASURE) {
	soFar &= (G_ARC | G_CIRCLE | G_SEGMENT);
	continue;
      }
      if(children[i]->descendType == G_RADIUS_MEASURE) {
	soFar &= (G_ARC | G_CIRCLE);
	continue;
      }
      if(children[i]->descendType == G_ANGLE_MEASURE) return G_ARC;
      if(children[i]->descendType == G_RATIO_MEASURE) return G_SEGMENT;
      if(children[i]->descendType == G_SLOPE_MEASURE) {
	soFar &= G_STRAIGHT;
	continue;
      }
    }

    //arc sectors, arc segments and circle interiors are specific:
    if(children[i]->type & G_FILLED) return type; 

    //finally check loops recursively
    if(children[i]->type == G_LOOP) {
      ASSERT(doc->isConstruction());
      KSegConstruction *d = (KSegConstruction *)doc;

      int j;
      for(j = 0; j < int(children[i]->parents.size()); ++j) {
	if(children[i]->parents[j] != this) continue;

	//now the current object corresponds to the jth given in the loop that is
	//the ith child.  Check if it can do that.
	refsConsidered.insert(this);
	soFar &= d->getGiven()[j]->whatCanItBe();
	refsConsidered.erase(this);
      }
    }
  }

  return soFar;
}




QDataStream &operator<<(QDataStream &stream, G_ref &ref)
{
  //parents and draw style have already been saved.

  short info = 0; //saves space

  //the first five bits is the object type
  int tmp = qRound((log(double(ref.type)) / log(2.)));

  info |= (tmp & 31); //bits 0-4

  //the next four bits is the descend type
  info |= ((ref.descendType & 15) << 5); //bits 5-8

  //the next four bits are the visible, label visible, given, and final flags
  info |= (ref.visible << 9); //bit 9
  info |= (ref.labelVisible << 10); //bit 10
  info |= (ref.given << 11); //bit 11
  info |= (ref.final << 12); //bit 12

  stream << info;

  if(ref.type == G_LOOP) return stream;

  stream << ref.label;

  ref.where->save(stream);

  return stream;
}


QDataStream &operator>>(QDataStream &stream, G_ref &ref)
{
  //parents, draw style, and the document have already been loaded.

  short info = 0; //saves space

  stream >> info;

  //the first five bits is the object type
  ref.type = (G_Type)(1 << (info & 31));
  info >>= 5;
  
  //the next four bits is the descend type
  ref.descendType = (info & 15);
  info >>= 4;

  //the next four bits are the visible, label visible, given, and final flags
  ref.visible = info & 1;
  ref.labelVisible = info & 2;
  ref.given = info & 4;
  ref.final = info & 8;

  ref.createObject();

  if(ref.type == G_LOOP) return stream;

  stream >> ref.label;

  ref.where->load(stream);

  return stream;
}



QString G_ref::getNameFromType(G_Type type)
{
  if(type == G_POINT) return qApp->translate("G_ref", "Point", "");
  if(type == G_SEGMENT) return qApp->translate("G_ref", "Segment", "");
  if(type == G_RAY) return qApp->translate("G_ref", "Ray", "");
  if(type == G_LINE) return qApp->translate("G_ref", "Line", "");
  if(type == G_CIRCLE) return qApp->translate("G_ref", "Circle", "");
  if(type == G_ARC) return qApp->translate("G_ref", "Arc", "");
  if(type == G_POLYGON) return qApp->translate("G_ref", "Polygon", "");
  if(type == G_CIRCLEINTERIOR) return qApp->translate("G_ref", "Circle Interior", "");
  if(type == G_ARCSECTOR) return qApp->translate("G_ref", "Arc Sector", "");
  if(type == G_ARCSEGMENT) return qApp->translate("G_ref", "Arc Segment", "");
  if(type == G_LOCUS) return qApp->translate("G_ref", "Locus", "");
  if(type == G_MEASURE) return qApp->translate("G_ref", "Measurement", "");
  if(type == G_CALCULATE) return qApp->translate("G_ref", "Calculation", "");
  if(type == G_ANNOTATION) return qApp->translate("G_ref", "Annotation", "");

  return qApp->translate("G_ref", "Unknown", "");
}