-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main_Window.cpp
241 lines (194 loc) · 10.1 KB
/
Main_Window.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
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
//****************************************************************************
//
// MAIN_WINDOW CPP
//
//****************************************************************************
// ===========================================================================
// Libraries
// ===========================================================================
// ===========================================================================
// Project Files
// ===========================================================================
#include "DefVal.h"
#include "Main_Window.h"
#include "Environment.h"
#include <string>
//############################################################################
// #
// Class Main_Window #
// #
//############################################################################
// ===========================================================================
// Constructors
// ===========================================================================
Main_Window::Main_Window(void) {
env.saveGraphics();
// Set the title
set_title(DefVal::APP_NAME);
// Set icon
set_icon_from_file(DefVal::ICON);
// Set window size
resize(DefVal::WINDOW_WIDTH, DefVal::WINDOW_HEIGHT);
// Window position
set_position(Gtk::WIN_POS_CENTER);
Main_table.resize(5,5); // Resize table
add(Main_table); // Add table container
// Attach host_label
Main_table.attach(host_label,0,1,0,1, Gtk::FILL, Gtk::FILL,DefVal::WIDGETS_MARGIN_SIZE,DefVal::WIDGETS_MARGIN_SIZE);
host_label.set_label(DefVal::HOST_LABEL_TEXT);
// Attach host_combobox
Main_table.attach(host_combobox,1,2,0,1, Gtk::FILL, Gtk::FILL,DefVal::WIDGETS_MARGIN_SIZE,DefVal::WIDGETS_MARGIN_SIZE);
host_combobox.set_wrap_width(DefVal::HOST_COMBOBOX_WRAP_WIDTH);
host_combobox.set_title(DefVal::HOST_COMBOBOX_TITLE);
Fill_host_combobox();
// Link the option chosen in the combobox to the display of a profile
host_combobox.signal_changed().connect(sigc::mem_fun(*this, &Main_Window::change_central_pic));
// Attach generation_number_label
Main_table.attach(generation_number_label,2,3,0,1, Gtk::FILL, Gtk::FILL,DefVal::WIDGETS_MARGIN_SIZE,DefVal::WIDGETS_MARGIN_SIZE);
generation_number_label.set_label(DefVal::GENERATION_NUMBER_LABEL_TEXT);
// Attach generation_number_spinbutton
Main_table.attach(generation_number_spinbutton,3,4,0,1, Gtk::FILL, Gtk::FILL,DefVal::WIDGETS_MARGIN_SIZE,DefVal::WIDGETS_MARGIN_SIZE);
generation_number_spinbutton.set_numeric(TRUE);
generation_number_spinbutton.set_wrap(TRUE);
generation_number_spinbutton.set_range(0, DefVal::GENERATION_NUMBER_MAX);
generation_number_spinbutton.set_increments(1,10);
generation_number_spinbutton.set_value(DefVal::GENERATION_NUMBER_DEFAULT);
// Attach start Button
Main_table.attach(start_button,4,5,0,2, Gtk::FILL, Gtk::FILL,DefVal::WIDGETS_MARGIN_SIZE,DefVal::WIDGETS_MARGIN_SIZE);
start_button.set_label(DefVal::START_BUTTON_LABEL);
start_button.signal_clicked().connect(sigc::mem_fun(*this, &Main_Window::Start_button_clicked));
// // Attach host_number_label
// Main_table.attach(host_number_label,0,1,1,2, Gtk::FILL, Gtk::FILL,DefVal::WIDGETS_MARGIN_SIZE,DefVal::WIDGETS_MARGIN_SIZE);
// host_number_label.set_label(DefVal::HOST_NUMBER_LABEL_TEXT);
// // Attach host_number_spinbutton
// Main_table.attach(host_number_spinbutton,1,2,1,2, Gtk::FILL, Gtk::FILL,DefVal::WIDGETS_MARGIN_SIZE,DefVal::WIDGETS_MARGIN_SIZE);
// host_number_spinbutton.set_numeric(TRUE);
// host_number_spinbutton.set_wrap(FALSE);
// host_number_spinbutton.set_range(1, DefVal::HOST_NUMBER_MAX);
// host_number_spinbutton.set_value(DefVal::HOST_NUMBER_DEFAULT);
// host_number_spinbutton.set_increments(1,10);
// host_number_spinbutton.signal_value_changed().connect(sigc::mem_fun(*this, &Main_Window::host_number_spinbutton_changed));
// // Attach parasite_number_label
// Main_table.attach(parasite_number_label,2,3,1,2, Gtk::FILL, Gtk::FILL,DefVal::WIDGETS_MARGIN_SIZE,DefVal::WIDGETS_MARGIN_SIZE);
// parasite_number_label.set_label(DefVal::PARASITE_NUMBER_LABEL_TEXT);
// // Attach parasite_number_spinbutton
// Main_table.attach(parasite_number_spinbutton,3,4,1,2, Gtk::FILL, Gtk::FILL,DefVal::WIDGETS_MARGIN_SIZE,DefVal::WIDGETS_MARGIN_SIZE);
// parasite_number_spinbutton.set_numeric(TRUE);
// parasite_number_spinbutton.set_wrap(FALSE);
// parasite_number_spinbutton.set_range(0, DefVal::PARASITE_NUMBER_MAX);
// parasite_number_spinbutton.set_value(DefVal::PARASITE_NUMBER_DEFAULT);
// parasite_number_spinbutton.set_increments(1,10);
// parasite_number_spinbutton.signal_value_changed().connect(sigc::mem_fun(*this, &Main_Window::parasite_number_spinbutton_changed));
// Attach display_image
Main_table.attach(display_image,0,5,2,3, Gtk::FILL | Gtk::EXPAND, Gtk::FILL | Gtk::EXPAND,DefVal::WIDGETS_MARGIN_SIZE,DefVal::WIDGETS_MARGIN_SIZE);
display_image.set(DefVal::DISPLAY_IMAGE_FILE_NAME);
// Attach statistics_label
Main_table.attach(statistics_label,0,5,4,5, Gtk::FILL, Gtk::FILL,DefVal::WIDGETS_MARGIN_SIZE,DefVal::WIDGETS_MARGIN_SIZE);
statistics_label.set_label(DefVal::STATISTICS_LABEL_TEXT);
display_stats_on_label();
// Attach legend_table
Main_table.attach(legend_table,0,5,3,4, Gtk::FILL, Gtk::FILL,DefVal::LEGEND_WIDGETS_MARGIN_SIZE,DefVal::LEGEND_WIDGETS_MARGIN_SIZE);
legend_table.resize(1,12); // Resize table
add(legend_table);
// Attach legend_1_image
legend_table.attach(legend_1_image,0,1,0,1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL | Gtk::EXPAND,DefVal::LEGEND_WIDGETS_MARGIN_SIZE,DefVal::LEGEND_WIDGETS_MARGIN_SIZE);
legend_1_image.set(DefVal::LEGEND_1_IMAGE_FILE_NAME);
// Attach legend_1_label
legend_table.attach(legend_1_label,1,2,0,1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL | Gtk::EXPAND,DefVal::LEGEND_WIDGETS_MARGIN_SIZE,DefVal::LEGEND_WIDGETS_MARGIN_SIZE);
legend_1_label.set_label(DefVal::LEGEND_1_LABEL);
// Attach legend_2_image
legend_table.attach(legend_2_image,2,3,0,1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL | Gtk::EXPAND,DefVal::LEGEND_WIDGETS_MARGIN_SIZE,DefVal::LEGEND_WIDGETS_MARGIN_SIZE);
legend_2_image.set(DefVal::LEGEND_2_IMAGE_FILE_NAME);
// Attach legend_2_label
legend_table.attach(legend_2_label,3,4,0,1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL | Gtk::EXPAND,DefVal::LEGEND_WIDGETS_MARGIN_SIZE,DefVal::LEGEND_WIDGETS_MARGIN_SIZE);
legend_2_label.set_label(DefVal::LEGEND_2_LABEL);
// Attach legend_3_image
legend_table.attach(legend_3_image,4,5,0,1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL | Gtk::EXPAND,DefVal::LEGEND_WIDGETS_MARGIN_SIZE,DefVal::LEGEND_WIDGETS_MARGIN_SIZE);
legend_3_image.set(DefVal::LEGEND_3_IMAGE_FILE_NAME);
// Attach legend_3_label
legend_table.attach(legend_3_label,5,6,0,1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL | Gtk::EXPAND,DefVal::LEGEND_WIDGETS_MARGIN_SIZE,DefVal::LEGEND_WIDGETS_MARGIN_SIZE);
legend_3_label.set_label(DefVal::LEGEND_3_LABEL);
// Attach legend_4_image
legend_table.attach(legend_4_image,6,7,0,1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL | Gtk::EXPAND,DefVal::LEGEND_WIDGETS_MARGIN_SIZE,DefVal::LEGEND_WIDGETS_MARGIN_SIZE);
legend_4_image.set(DefVal::LEGEND_4_IMAGE_FILE_NAME);
// Attach legend_4_label
legend_table.attach(legend_4_label,7,8,0,1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL | Gtk::EXPAND,DefVal::LEGEND_WIDGETS_MARGIN_SIZE,DefVal::LEGEND_WIDGETS_MARGIN_SIZE);
legend_4_label.set_label(DefVal::LEGEND_4_LABEL);
// Attach legend_5_image
legend_table.attach(legend_5_image,8,9,0,1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL | Gtk::EXPAND,DefVal::LEGEND_WIDGETS_MARGIN_SIZE,DefVal::LEGEND_WIDGETS_MARGIN_SIZE);
legend_5_image.set(DefVal::LEGEND_5_IMAGE_FILE_NAME);
// Attach legend_5_label
legend_table.attach(legend_5_label,9,10,0,1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL | Gtk::EXPAND,DefVal::LEGEND_WIDGETS_MARGIN_SIZE,DefVal::LEGEND_WIDGETS_MARGIN_SIZE);
legend_5_label.set_label(DefVal::LEGEND_5_LABEL);
// Attach legend_3_image
legend_table.attach(legend_6_image,10,11,0,1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL | Gtk::EXPAND,DefVal::LEGEND_WIDGETS_MARGIN_SIZE,DefVal::LEGEND_WIDGETS_MARGIN_SIZE);
legend_6_image.set(DefVal::LEGEND_6_IMAGE_FILE_NAME);
// Attach legend_6_label
legend_table.attach(legend_6_label,11,12,0,1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL | Gtk::EXPAND,DefVal::LEGEND_WIDGETS_MARGIN_SIZE,DefVal::LEGEND_WIDGETS_MARGIN_SIZE);
legend_6_label.set_label(DefVal::LEGEND_6_LABEL);
// Display all widgets
show_all();
}
// ===========================================================================
// Destructor
// ===========================================================================
// ===========================================================================
// Methods definition
// ===========================================================================
void Main_Window::Fill_host_combobox(void){
// Add entries to the combobox
char name[100];
for (int i = 0; i < DefVal::NB_HOSTS; ++i)
{
sprintf(name,"host_%d", i);
host_combobox.append(name);
}
host_combobox.set_active_text("host_0");
}
void Main_Window::change_central_pic(void)
{
string text2 = host_combobox.get_active_text();
string f_name = text2 + DefVal::PIC_FORMAT;
display_image.set(f_name);
display_stats_on_label();
}
void Main_Window::Start_button_clicked(void)
{
int n;
n = generation_number_spinbutton.get_value();
printf("Uuuuuuuh: %d generations\n",n);
if(n > 0)
{
for(int i = 0; i < n; i++)
{
env.newGeneration();
env.mutation();
}
env.saveGraphics();
change_central_pic();
}
}
// void Main_Window::host_number_spinbutton_changed(void)
// {
// int n;
// n = host_number_spinbutton.get_value();
// printf("Uuuuuuuh: %d hosts\n",n);
// }
// void Main_Window::parasite_number_spinbutton_changed(void)
// {
// int n;
// n = parasite_number_spinbutton.get_value();
// printf("Uuuuuuuh: %d parasites\n",n);
// }
void Main_Window::display_stats_on_label()
{
string host_name = host_combobox.get_active_text();
char host_file [100];
sprintf(host_file, "%s.data", host_name.c_str());
FILE * file = fopen(host_file, "r");
char stats [100];
fread(stats,sizeof(char),100,file);//size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );
//printf("%s\n",stats );
statistics_label.set_label(stats);
fclose(file);
}