-
Notifications
You must be signed in to change notification settings - Fork 0
/
Document.cpp
117 lines (108 loc) · 1.93 KB
/
Document.cpp
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
#include "Document.h"
Document::Document(wxSize size):_size(size)
{
_lastHitKey = NULL;
_calculator = new SimpleCalculator();
CreateKeys();
}
Document::~Document(void)
{
ClearKeys();
delete _calculator;
}
void Document::ClearExpression()
{
_expression.ClearExpression();
UpdateObservers();
}
void Document::BackExpression()
{
_expression.BackExpression();
UpdateObservers();
}
std::string const& Document::GetExpressionAsString()
{
return _expression.GetExpressionAsString();
}
void Document::Evaluate()
{
_expression.Evaluate();
UpdateObservers();
}
void Document::AddObserver(DocumentObserver *observer)
{
_listeners.push_back(observer);
}
void Document::RemoveObserver(DocumentObserver *observer)
{
_listeners.remove(observer);
}
void Document::UpdateObservers()
{
observer_iter it = _listeners.begin();
for(;it!=_listeners.end();it++)
{
(*it)->Observe();
}
}
void Document::Draw(wxPaintDC *dc)
{
key_iter it= _keys.begin();
for(;it!=_keys.end();it++)
{
(*it)->Draw(dc);
}
}
void Document::HitKey(Key *key)
{
key->ReverseColors();
_lastHitKey = key;
_expression.Update(key->GetSymbol());
}
void Document::CreateKeys()
{
IteratorPtr<Symbol<double>*> iter = _calculator->GetSymbols();
float min = MIN(_size.GetWidth(),_size.GetHeight());
float edgeLength = (int)((float)min/((float)COL));
float xStart = 0,yStart = 0;
wxPoint pos(xStart,yStart);
int colCount = 1;
for(iter->First();!iter->IsDone();iter->Next())
{
Key *key = new Key(pos,edgeLength,iter->Current());
_keys.push_back(key);
if(colCount>=COL)
{
pos.x = xStart;
pos.y += edgeLength;
colCount = 1;
}
else
{
colCount++;
pos.x += edgeLength;
}
}
}
void Document::ReleaseKey()
{
if(_lastHitKey!=NULL)
{
_lastHitKey->ReverseColors();
}
}
void Document::ClearKeys()
{
if(!_keys.empty())
{
key_iter it = _keys.begin();
for(;it!=_keys.end();)
{
Key* temp = *it;
it++;
delete temp;
temp = NULL;
}
_keys.clear();
}
}