-
Notifications
You must be signed in to change notification settings - Fork 156
/
HighlightState.cpp
166 lines (145 loc) · 4.06 KB
/
HighlightState.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
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
/**
* GeoDa TM, Copyright (C) 2011-2015 by Luc Anselin - all rights reserved
*
* This file is part of GeoDa.
*
* GeoDa 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 3 of the License, or
* (at your option) any later version.
*
* GeoDa 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, see <http://www.gnu.org/licenses/>.
*/
#include <algorithm>
#include <functional>
#include "HighlightStateObserver.h"
#include "logger.h"
#include "HighlightState.h"
HighlightState::HighlightState()
{
delete_self_when_empty = false;
LOG_MSG("In HighlightState::HighlightState()");
}
HighlightState::~HighlightState()
{
LOG_MSG("In HighlightState::~HighlightState()");
}
void HighlightState::closeAndDeleteWhenEmpty()
{
LOG_MSG("Entering HighlightState::closeAndDeleteWhenEmpty");
delete_self_when_empty = true;
if (observers.size() == 0) {
delete this;
}
LOG_MSG("Exiting HighlightState::closeAndDeleteWhenEmpty");
}
std::map<wxString, wxString> HighlightState::GetMetaData()
{
return meta_data;
}
void HighlightState::SetMetaData(std::map<wxString, wxString>& _meta_data)
{
meta_data = _meta_data;
}
void HighlightState::SetSize(int n) {
total_highlighted = 0;
highlight.resize(n);
newly_highlighted.resize(n);
newly_unhighlighted.resize(n);
std::vector<bool>::iterator it;
for ( it=highlight.begin(); it != highlight.end(); it++ ) (*it) = false;
}
wxString HighlightState::GetEventTypeStr()
{
if (event_type == delta) return "delta";
if (event_type == unhighlight_all) return "unhighlight_all";
if (event_type == invert) return "invert";
return "empty";
}
/* Observable interface definitions */
void HighlightState::registerObserver(HighlightStateObserver* o)
{
observers.push_front(o);
}
void HighlightState::removeObserver(HighlightStateObserver* o)
{
LOG_MSG("Entering HighlightState::removeObserver");
observers.remove(o);
if (observers.size() == 0 && delete_self_when_empty) {
delete this;
}
LOG_MSG("Exiting HighlightState::removeObserver");
}
void HighlightState::notifyObservers()
{
ApplyChanges();
if (event_type == empty) return;
if (observers.empty()) return;
// See section 18.4.4.2 of Stroustrup
//std::for_each(observers.begin(), observers.end(),
// std::bind2nd(std::mem_fun(&HighlightStateObserver::update),this));
std::list<HighlightStateObserver*>::iterator it;
for (it=observers.begin(); it != observers.end(); ++it) {
HighlightStateObserver* obj = *it;
obj->update(this);
}
}
void HighlightState::notifyObservers(HighlightStateObserver* exclude)
{
ApplyChanges();
if (event_type == empty) return;
for (std::list<HighlightStateObserver*>::iterator i=observers.begin();
i != observers.end(); ++i)
{
if ((*i) == exclude) {
} else {
(*i)->update(this);
}
}
}
void HighlightState::ApplyChanges()
{
switch (event_type) {
case delta:
{
total_highlighted = 0;
for (int i=0; i<highlight.size(); i++) {
if (highlight[i] == true) {
total_highlighted += 1;
}
}
}
break;
case unhighlight_all:
{
if (total_highlighted == 0) {
//MMM: figure out why this short-cut isn't always working
//event_type = empty;
}
for (int i=0, iend=highlight.size(); i<iend; i++) {
highlight[i] = false;
}
total_highlighted = 0;
}
break;
case invert:
{
total_highlighted = 0;
for (int i=0; i<highlight.size(); i++) {
if (highlight[i] == false) {
total_highlighted += 1;
}
highlight[i] = !highlight[i];
}
}
break;
default:
break;
}
}