File: G_calculateObject.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 (109 lines) | stat: -rw-r--r-- 2,991 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
/*
 *  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 <stdio.h>

#include "G_object.H"
#include "G_undo.H"
#include "KSegDocument.H"


class CalculationReferenceFetcher : public ReferenceFetcher
{
public:
  CalculationReferenceFetcher(G_ref *inRef) { ref = inRef; }

  double getValue(int reference_number)
  { return ref->getParents()[reference_number]->getObject()->getNumValue(); }

  KFormula *getFormula(int reference_number)
  { return ((G_valueObject *)(ref->getParents()[reference_number]->getObject()))->getLhs(); }

protected:
  G_ref * ref;
};


G_calculateObject::G_calculateObject(G_ref *inWhere)
  :G_valueObject(inWhere, true)
{
  lhs.setReferenceFetcher(new CalculationReferenceFetcher(where));
}


void G_calculateObject::changeFormula(const G_refs& newParents, const QString &newFormulaString)
{
  if(where->getParents() == newParents && newFormulaString == formulaString) return;

  where->getDocument()->addUndo(new G_undoChangeCalculation(where));

  where->reconstrain(G_REGULAR_CALCULATE, newParents);
  formulaString = newFormulaString;

  dirtylhs = true;

  update();
}


void G_calculateObject::calculate()
{
  //first check to make sure its parents are defined
  unsigned int i;

  for(i = 0; i < where->getParents().count(); ++i) {
    if(fabs(parent(i)->getNumValue()) >= BIG) {
      value = BIG * 10;
      return;
    }
  }

  lhs.setReferenceFetcher(new CalculationReferenceFetcher(where));

  int error;

  value = lhs.evaluate(QDict<double>(), &error);

  if(error != NO_ERROR) value = BIG * 10;
}

void G_calculateObject::convertFormulaStringOnLoad()
{
  int i;
  
  for(i = 0; i < (int)formulaString.length(); ++i) {
    if(formulaString[i].unicode() > OLD_UNUSED_OFFSET && formulaString[i].unicode() < OLD_UNUSED_OFFSET + 300)
      formulaString[i] = QChar(formulaString[i].unicode() + (UNUSED_OFFSET - OLD_UNUSED_OFFSET));
  }
}

QString G_calculateObject::convertFormulaStringOnSave(QString tmp) const
{
  for(int i = 0; i < (int)tmp.length(); ++i)
    if(tmp[i].unicode() > UNUSED_OFFSET &&
       tmp[i].unicode() < UNUSED_OFFSET + 300)
      tmp[i] = QChar((int)(tmp[i].unicode()) +
		     (OLD_UNUSED_OFFSET - UNUSED_OFFSET));

  return tmp;
}