]> git.llucax.com Git - z.facultad/75.52/treemulator.git/blob - viewer/main.cpp
Bugfix del test.sh para que no 'oculte' errores por SIGSEGV.
[z.facultad/75.52/treemulator.git] / viewer / main.cpp
1
2 #include <iostream>
3
4 #include <gtkmm.h>
5 #include <libgnomecanvasmm.h>
6
7 #include "random.h"
8 #include "view_btree.h"
9 #include "view_properties.h"
10 #include "new_tree_dialog.h"
11 #include "key_dialog.h"
12 #include "view_debug.h"
13
14 using namespace Gnome::Canvas;
15
16  Glib::ustring ui_info =
17 "<ui>"
18 "  <menubar name='MenuBar'>"
19 "    <menu action='MenuFile'>"
20 "      <menuitem action='Nuevo'/>"
21 "      <separator/>"
22 "      <menuitem action='Salir'/>"
23 "    </menu>"
24 "    <menu action='MenuKey'>"
25 "      <menuitem action='Agregar Clave'/>"
26 "      <menuitem action='Borrar Clave'/>"
27 "      <menuitem action='Buscar Clave'/>"
28 "    </menu>"
29 "    <menu action='MenuZoom'>"
30 "      <menuitem action='Alejar'/>"
31 "      <menuitem action='Acercar'/>"
32 "      <separator/>"
33 "      <menuitem action='100 %'/>"
34 "    </menu>"
35 "  </menubar>"
36 "</ui>";
37
38 void nuevo_arbol ();
39 void agregar_clave ();
40 void borrar_clave ();
41 void buscar_clave ();
42 void zoom_out ();
43 void zoom_in ();
44 void zoom_normal ();
45
46 Glib::RefPtr<ViewBTree> tree;
47 Gnome::Canvas::Canvas *real_canvas;
48 ViewProperties *real_frame;
49
50 int main(int argc, char *argv[])
51 {
52         Gtk::Main kit(argc, argv);
53
54         Gnome::Canvas::init ();
55
56         Gtk::Window window;
57         Gtk::VBox hbox;
58         Gtk::VBox vbox;
59
60         Gtk::ScrolledWindow area;
61         Gnome::Canvas::Canvas canvas;
62         ViewProperties frame;
63
64         real_canvas = &canvas;
65         real_frame = &frame;
66
67         canvas.set_scroll_region (0, 0, 100, 100);
68         area.add (canvas);
69
70         hbox.pack_start (area);
71         hbox.pack_start (frame, false, false, 10);
72         frame.set_size_request (200, 200);
73
74         Glib::RefPtr<Gtk::ActionGroup> actiongroup = Gtk::ActionGroup::create();
75
76         actiongroup->add( Gtk::Action::create("MenuFile", "_Arbol") );
77         actiongroup->add( Gtk::Action::create("Nuevo", Gtk::Stock::NEW), &nuevo_arbol);
78         actiongroup->add( Gtk::Action::create("Salir", Gtk::Stock::QUIT), Gtk::AccelKey ("<control>q"), &Gtk::Main::quit);
79         actiongroup->add( Gtk::Action::create("MenuKey", "_Clave") );
80         actiongroup->add( Gtk::Action::create("Agregar Clave", Gtk::Stock::ADD), Gtk::AccelKey ("<control>a"), &agregar_clave);
81         actiongroup->add( Gtk::Action::create("Borrar Clave", Gtk::Stock::REMOVE), Gtk::AccelKey ("<control>d"), &borrar_clave);
82         actiongroup->add( Gtk::Action::create("Buscar Clave", Gtk::Stock::FIND), Gtk::AccelKey ("<control>f"), &buscar_clave);
83         actiongroup->add( Gtk::Action::create("MenuZoom", "_Zoom"));
84         actiongroup->add( Gtk::Action::create("Alejar", Gtk::Stock::ZOOM_OUT), Gtk::AccelKey ("<control>z"), &zoom_out );
85         actiongroup->add( Gtk::Action::create("Acercar", Gtk::Stock::ZOOM_IN), Gtk::AccelKey ("<control>x"), &zoom_in);
86         actiongroup->add( Gtk::Action::create("100 %", Gtk::Stock::ZOOM_100), Gtk::AccelKey ("<control>1"), &zoom_normal);
87
88         Glib::RefPtr<Gtk::UIManager> m_refUIManager = Gtk::UIManager::create();
89         m_refUIManager->insert_action_group (actiongroup);
90
91         m_refUIManager->add_ui_from_string(ui_info);
92         Gtk::Widget* menubar = m_refUIManager->get_widget("/MenuBar");
93         menubar->show_all ();
94
95         vbox.pack_start (*menubar, false, true, 0);
96         vbox.pack_end (hbox, true, true, 5);
97
98         window.add_accel_group (m_refUIManager->get_accel_group ());
99         window.add (vbox);
100         window.set_size_request (640, 480);
101         window.show_all ();
102
103         /* Conecto el Canvas con el Frame */
104         Gtk::Main::run(window);
105                                                             
106         return 0;
107 }
108
109 void nuevo_arbol ()
110 {
111         real_canvas->set_scroll_region (0, 0, 5000, 5000);
112
113         NewTreeDialog d;
114         if (d.run () == Gtk::RESPONSE_OK) {
115                 uint altas = d.getAdds ();
116                 uint bajas = d.getDels ();
117
118                 double paltas = bajas / (double)altas;
119
120                 int type = d.getKeyType ();
121                 tree = Glib::RefPtr<ViewBTree>(new ViewBTree (real_canvas->root(), "test.idx", d.getBlockSize (), type));
122                 tree->signal_selected ().connect ( sigc::mem_fun (*real_frame, &ViewProperties::ShowItem) );
123                 if (type == BTree::KEY_FIXED) {
124                         std::list<int> lst;
125                         std::list<int>::iterator it;
126                         Random::Init ();
127                         Random::Ints (lst, altas);
128
129                         it = lst.begin ();
130                         uint i = 0;
131                         while (it != lst.end ()) {
132                                 ClaveFija c(*it);
133
134                                 double l = Random::Double (0.0f, 1.0f);
135                                 std::cout << l << " >= " << paltas << std::endl;
136                                 if (l >= paltas) {
137                                         try {
138                                                 tree->AddKey (c);
139                                         } catch (Exception *e) {
140                                                 std::cout << "====== " << (std::string)c << e->Message () << std::endl;
141                                         }
142                                         i++;
143                                 } else {
144                                         /* Tengo que borrar una clave entre 0 e "i" de la lista
145                                          * porque son las que ya agregue. */
146                                         int aborrar = (int)Random::Double (0, i);
147                                         std::list<int>::iterator otro = lst.begin ();
148                                         int j = 0;
149                                         while (j < aborrar) {
150                                                 otro++;
151                                                 j++;
152                                         }
153                                         ClaveFija c(*otro);
154
155                                         tree->DelKey (c);
156                                         std::string sss = c;
157                                         std::cout << "Clave Borrada " << sss << std::endl;
158                                 }
159
160                                 it++;
161                         }
162                 } else {
163                         std::list<std::string> lst;
164                         std::list<std::string>::iterator it;
165                         Random::Init ();
166                         Random::Strings (lst, altas);
167
168                         it = lst.begin ();
169                         while (it != lst.end ()) {
170                                 ClaveVariable c(*it);
171
172                                 try {
173                                         tree->AddKey (c);
174                                 } catch (Exception *e) {
175                                         std::cout << "====== " << (std::string)c << e->Message () << std::endl;
176                                 }
177                                 it++;
178                         }
179                 }
180                 tree->AddNode (0);
181                 double x1, x2, y1, y2;
182                 tree->get_bounds (x1, y1, x2, y2);
183                 real_canvas->scroll_to (0, 0);
184         }
185 }
186
187 void agregar_clave ()
188 {
189         if (!tree)
190         {
191                 Gtk::MessageDialog d("No hay un arbol creado, por favor primero cree un arbol!",
192                                 false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
193                 d.run();
194                 return;
195         }
196         KeyDialog d("Agregar");
197         if (d.run () == Gtk::RESPONSE_OK)
198         {
199                 Glib::ustring str_key = d.key();
200                 if (tree->type() == BTree::KEY_FIXED)
201                 {
202                         ClaveFija c(atoi(str_key.c_str()));
203                         tree->AddKey(c);
204                 }
205                 else
206                 {
207                         ClaveVariable c(str_key);
208                         tree->AddKey(c);
209                 }
210                 delete tree->last_selected;
211                 tree->AddNode (0);
212                 real_canvas->scroll_to (0, 0);
213         }
214 }
215
216 void borrar_clave ()
217 {
218         if (!tree)
219         {
220                 Gtk::MessageDialog d("No hay un arbol creado, por favor primero cree un arbol!",
221                                 false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
222                 d.run();
223                 return;
224         }
225         KeyDialog d("Borrar");
226         if (d.run () == Gtk::RESPONSE_OK)
227         {
228                 Glib::ustring str_key = d.key();
229                 if (tree->type() == BTree::KEY_FIXED)
230                 {
231                         ClaveFija c(atoi(str_key.c_str()));
232                         tree->DelKey(c);
233                 }
234                 else
235                 {
236                         ClaveVariable c(str_key);
237                         tree->DelKey(c);
238                 }
239                 delete tree->last_selected;
240                 tree->AddNode (0);
241                 real_canvas->scroll_to (0, 0);
242         }
243 }
244
245 void buscar_clave ()
246 {
247         if (!tree)
248         {
249                 Gtk::MessageDialog d("No hay un arbol creado, por favor primero cree un arbol!",
250                                 false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
251                 d.run();
252                 return;
253         }
254         KeyDialog d("Buscar");
255         while (true) // Repite hasta que se encuentre algo o se cancele
256         {
257                 if (d.run () == Gtk::RESPONSE_OK)
258                 {
259                         BTreeFindResult* result = 0;
260                         Clave *c = NULL;
261                         Glib::ustring str_key = d.key();
262                         if (tree->type() == BTree::KEY_FIXED)
263                         {
264                                 c = new ClaveFija (atoi(str_key.c_str()));
265                                 result = tree->FindKey(*c);
266                         }
267                         else
268                         {
269                                 c = new ClaveVariable (str_key);
270                                 result = tree->FindKey(*c);
271                         }
272                         if (result)
273                         {
274                                 tree->Clear ();
275                                 tree->AddNode(result->node);
276                                 tree->HighliteKey (*c);
277                                 delete result;
278                                 delete c;
279                                 real_canvas->scroll_to (0, 0);
280                                 return; // Encontramos, salimos
281                         }
282                         if (c) delete c;
283                         Gtk::MessageDialog msg("Clave no encontrada!", false,
284                                         Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
285                         msg.run();
286                         // Seguimos intentando
287                 }
288                 else return; // Cancelaron, salimos
289         }
290 }
291
292 void zoom_out ()
293 {
294         double r = real_canvas->get_pixels_per_unit ();
295         r *= 0.9f;
296         if (r < 0.0001)
297                 r = 0.0001;
298         real_canvas->set_pixels_per_unit (r);
299 }
300
301 void zoom_in ()
302 {
303         double r = real_canvas->get_pixels_per_unit ();
304         r *= 1.1f;
305         if (r > 10)
306                 r = 10;
307         real_canvas->set_pixels_per_unit (r);
308 }
309
310 void zoom_normal ()
311 {
312         real_canvas->set_pixels_per_unit (1.0f);
313 }
314