-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview.cpp
More file actions
271 lines (249 loc) · 10.8 KB
/
view.cpp
File metadata and controls
271 lines (249 loc) · 10.8 KB
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
#include "view.h"
#include "observable.h"
#include "observer.h"
#include "queries.h"
#include "utility.h"
#include <memory>
#include <sstream>
#include <QLayout>
#include <QPushButton>
#include <QString>
#include <QtWidgets>
namespace DSVisualization {
namespace {
Qt::GlobalColor FromStatusToQTColor(DSVisualization::Status status) {
switch (status) {
case DSVisualization::Status::to_delete:
return Qt::GlobalColor::darkBlue;
case DSVisualization::Status::touched:
return Qt::GlobalColor::green;
case DSVisualization::Status::current:
return Qt::GlobalColor::yellow;
case DSVisualization::Status::rotate:
return Qt::GlobalColor::magenta;
case DSVisualization::Status::found:
return Qt::GlobalColor::cyan;
default:
return Qt::GlobalColor::transparent;
}
}
std::string GetTextAndClear(QLineEdit* line_edit) {
PRINT_WHERE_AM_I();
assert(line_edit != nullptr);
std::string result = line_edit->text().toStdString();
line_edit->clear();
return result;
}
}// namespace
View::View()
: main_window_(), observer_model_view_(
[this](const RedBlackTree<int>::Data& x) {
OnNotifyFromModel(x);
},
[this](const RedBlackTree<int>::Data& x) {
OnNotifyFromModel(x);
},
[this](const RedBlackTree<int>::Data& x) {
OnNotifyFromModel(x);
}),
observable_view_controller_([this]() {
return this->query_;
}) {
PRINT_WHERE_AM_I();
QObject::connect(main_window_.insert_button_, &QPushButton::clicked, this,
&View::OnInsertButtonPushed);
QObject::connect(main_window_.erase_button_, &QPushButton::clicked, this,
&View::OnEraseButtonPushed);
QObject::connect(main_window_.find_button_, &QPushButton::clicked, this,
&View::OnFindButtonPushed);
}
[[nodiscard]] Observer<RedBlackTree<int>::Data>* View::GetObserver() {
PRINT_WHERE_AM_I();
return &observer_model_view_;
}
template<typename T>
static float IntegralToFloat(T value) {
static_assert(std::is_integral_v<T>);
return static_cast<float>(value);
}
static void Delay(int milliseconds) {
QEventLoop loop;
QTimer t;
QTimer::connect(&t, &QTimer::timeout, &loop, &QEventLoop::quit);
t.start(milliseconds);
loop.exec();
}
void View::OnNotifyFromModel(const RedBlackTree<int>::Data& value) {
PRINT_WHERE_AM_I();
float counter = 0;
tree_width_ = IntegralToFloat(value.tree_size) *
(horizontal_space_between_nodes + default_node_diameter);
std::unique_ptr<DrawableTree> result = std::make_unique<DrawableTree>(
DrawableTree{GetDrawableNode(value, value.root, 0, counter)});
this->DrawTree(result);
Delay(draw_delay_in_ms);
}
void View::SubscribeToQuery(Observer<TreeQuery>* observer_view_controller) {
PRINT_WHERE_AM_I();
observable_view_controller_.Subscribe(observer_view_controller);
}
void View::OnInsertButtonPushed() {
PRINT_WHERE_AM_I();
std::string str = GetTextAndClear(main_window_.insert_line_edit_);
HandlePushButton(TreeQueryType::insert, std::ref(str));
}
void View::OnEraseButtonPushed() {
PRINT_WHERE_AM_I();
std::string str = GetTextAndClear(main_window_.erase_line_edit_);
HandlePushButton(TreeQueryType::erase, str);
}
void View::OnFindButtonPushed() {
PRINT_WHERE_AM_I();
std::string str = GetTextAndClear(main_window_.find_line_edit_);
HandlePushButton(TreeQueryType::find, str);
}
namespace {
const int MIN_VALUE = -(1 << 15);
const int MAX_VALUE = (1 << 15) - 1;
std::variant<int, std::string> string_to_int(const std::string& text) {
if (text.empty()) {
return "Empty query";
}
int result = 0;
bool is_negative = false;
size_t first_pos = 0;
if (text[0] == '+') {
first_pos = 1;
} else if (text[0] == '-') {
first_pos = 1;
is_negative = true;
}
if (first_pos == text.size()) {
return "Empty query";
}
for (size_t i = first_pos; i < text.size(); ++i) {
if (!('0' <= text[i] && text[i] <= '9')) {
return "Value must be a number";
}
result = result * 10 + (text[i] - '0');
if (result > MAX_VALUE * 10) {
return "Value must be in range from " + std::to_string(MIN_VALUE) + " to " +
std::to_string(MAX_VALUE);
}
}
if (is_negative) {
result *= -1;
}
if (!(MIN_VALUE <= result && result <= MAX_VALUE)) {
return "Value must be in range from " + std::to_string(MIN_VALUE) + " to " +
std::to_string(MAX_VALUE);
}
return result;
}
}// namespace
void View::HandlePushButton(TreeQueryType query_type, const std::string& text) {
PRINT_WHERE_AM_I();
main_window_.DisableButtons();
std::variant<int, std::string> value = string_to_int(text);
if (value.index() == 0) {
query_ = {query_type, std::get<int>(value)};
observable_view_controller_.Notify();
} else {
QMessageBox messageBox;
QMessageBox::critical(nullptr, "Error", std::get<std::string>(value).c_str());
}
main_window_.EnableButtons();
}
std::unique_ptr<DrawableNode> View::GetDrawableNode(const TreeInfo<int>& tree_info,
const RedBlackTree<int>::Node* node,
float depth, float& counter) {
if (!node) {
return nullptr;
}
std::unique_ptr<DrawableNode> result = std::make_unique<DrawableNode>(DrawableNode{
0, 0, 0, Qt::black, FromStatusToQTColor(Status::initial), nullptr, nullptr});
result->left = GetDrawableNode(tree_info, node->left.get(), depth + 1, counter);
result->x = counter * (horizontal_space_between_nodes + default_node_diameter);
result->y = depth * (default_node_diameter + vertical_space_between_nodes);
assert(result);
assert(node);
result->key = node->value;
result->inside_color = (node->color == Color::red ? Qt::red : Qt::black);
{
auto it = tree_info.node_to_status.find(node);
auto status =
(it == tree_info.node_to_status.end() ? Status::initial
: tree_info.node_to_status.at(node));
result->outside_color = FromStatusToQTColor(status);
}
counter++;
result->right = GetDrawableNode(tree_info, node->right.get(), depth + 1, counter);
return result;
}
void View::DrawTree(const std::unique_ptr<DrawableTree>& tree) {
PRINT_WHERE_AM_I();
main_window_.tree_view_->scene()->clear();
current_node_diameter_ = default_node_diameter;
main_window_.current_width_ = IntegralToFloat(size().width());
if (tree_width_ + default_node_diameter + MainWindow::margin >=
main_window_.current_width_) {
current_node_diameter_ = (default_node_diameter * main_window_.current_width_) /
(tree_width_ + default_node_diameter + MainWindow::margin);
}
RecursiveDraw(tree->root);
main_window_.tree_view_->show();
}
void View::DrawNode(const std::unique_ptr<DrawableNode>& node) {
PRINT_WHERE_AM_I();
QPen pen;
QBrush brush;
pen.setWidth(5);
brush.setColor(node->inside_color);
brush.setStyle(Qt::SolidPattern);
pen.setColor(node->outside_color);
main_window_.tree_view_->scene()->addEllipse(node->x, node->y, current_node_diameter_,
current_node_diameter_, pen, brush);
auto* text = new QGraphicsTextItem(std::to_string(node->key).c_str());
auto rect = text->boundingRect();
text->setPos(node->x - rect.width() / 2 + current_node_diameter_ / 2,
node->y - rect.height() / 2 + current_node_diameter_ / 2);
text->setDefaultTextColor(Qt::white);
main_window_.tree_view_->scene()->addItem(text);
}
void View::DrawEdgeBetweenNodes(const std::unique_ptr<DrawableNode>& parent,
bool is_child_left) {
PRINT_WHERE_AM_I();
std::cerr << "radius = " << current_node_diameter_ << std::endl;
float x1 = parent->x;
float y1 = parent->y;
float x2 = is_child_left ? parent->left->x : parent->right->x;
float y2 = is_child_left ? parent->left->y : parent->right->y;
auto* horizontal_line = new QGraphicsLineItem(
x1 + (is_child_left ? 0 : current_node_diameter_), y1 + current_node_diameter_ / 2,
x2 + current_node_diameter_ / 2, y1 + current_node_diameter_ / 2);
auto* vertical_line = new QGraphicsLineItem(x2 + current_node_diameter_ / 2,
y1 + current_node_diameter_ / 2,
x2 + current_node_diameter_ / 2, y2);
main_window_.tree_view_->scene()->addItem(horizontal_line);
main_window_.tree_view_->scene()->addItem(vertical_line);
}
void View::RecursiveDraw(const std::unique_ptr<DrawableNode>& node) {
if (!node) {
return;
}
if (tree_width_ + default_node_diameter + MainWindow::margin >=
main_window_.current_width_) {
node->x = node->x / (tree_width_ + default_node_diameter + MainWindow::margin) *
main_window_.current_width_;
}
RecursiveDraw(node->left);
if (node->left) {
DrawEdgeBetweenNodes(node, true);
}
DrawNode(node);
RecursiveDraw(node->right);
if (node->right) {
DrawEdgeBetweenNodes(node, false);
}
}
}// namespace DSVisualization