NewMapWin has become Dialog instead of Window. Therefore it is created dynamically, when there is need for it, instead of keeping one instance in memory. This solution is slower, but more correct than before.
1 #include <new_map_win.h>
3 bool NewMapWin::closeIfEscapeIsPressed(GdkEventKey* e)
5 if(e->keyval==GDK_Escape)
12 NewMapWin::NewMapWin(const std::string& title, GraphDisplayerCanvas & grdispc, bool itisedge, bool edgenode):Gtk::Dialog(title, true, true),gdc(grdispc),node("Create NodeMap"),edge("Create EdgeMap")
14 set_default_size(200, 50);
16 signal_key_press_event().connect(sigc::mem_fun(*this, &NewMapWin::closeIfEscapeIsPressed));
18 Gtk::VBox * vbox=get_vbox();
21 table=new Gtk::Table(3, 2, false);
24 label->set_text("Name of new map:");
27 (*table).attach(*label,0,1,0,1,Gtk::SHRINK,Gtk::SHRINK,10,3);
28 (*table).attach(name,1,2,0,1,Gtk::SHRINK,Gtk::SHRINK,10,3);
31 label->set_text("Default value in the map:");
32 default_value.set_text("0");
34 (*table).attach(*label,0,1,1,2,Gtk::SHRINK,Gtk::SHRINK,10,3);
35 (*table).attach(default_value,1,2,1,2,Gtk::SHRINK,Gtk::SHRINK,10,3);
37 //node vs. edge map selector
38 Gtk::RadioButton::Group group = node.get_group();
39 edge.set_group(group);
43 (*table).attach(node,0,1,2,3,Gtk::SHRINK,Gtk::SHRINK,10,3);
44 (*table).attach(edge,1,2,2,3,Gtk::SHRINK,Gtk::SHRINK,10,3);
58 vbox->pack_start(*table);
61 add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
67 void NewMapWin::on_response(int response_id)
69 if(response_id==Gtk::RESPONSE_OK)
73 //get and formulate text
74 std::string def_val_str=default_value.get_text();
75 std::string polishform=string2Polishform(def_val_str,edge.get_active());
78 std::string mapname=name.get_text();
80 if(!mapname.empty()&&!polishform.empty())
86 Graph::EdgeMap<double> * emptr=new Graph::EdgeMap<double> (gdc.mapstorage.graph);
88 std::stack<double> polishstack;
90 for(EdgeIt k(gdc.mapstorage.graph); k!=INVALID; ++k)
92 for(int i=0;i<(int)polishform.size();i++)
102 op1=polishstack.top();
104 op2=polishstack.top();
108 //substitute variable
109 std::map< std::string,Graph::EdgeMap<double> * > ems=gdc.mapstorage.edgemap_storage;
110 bool itisvar=(ems.find(ch2var[ polishform[i] ])!=ems.end());
113 polishstack.push( (*(gdc.mapstorage.edgemap_storage[ ch2var[ polishform[i] ] ]))[k]);
117 char * def_val_ch=new char [(int)(ch2var[ polishform[i] ].length())];
118 for(int j=0;j<(int)(ch2var[ polishform[i] ].length());j++)
120 def_val_ch[j]=ch2var[ polishform[i] ][j];
122 polishstack.push(atof(def_val_ch));
130 switch(polishform[i])
145 std::cout << "How could we get here?" << std::endl;
148 polishstack.push(res);
151 (*emptr)[k]=polishstack.top();
154 //if addition was not successful addEdgeMap returns one.
155 //cause can be that there is already a map named like the new one
156 if(gdc.mapstorage.addEdgeMap(mapname, emptr, def_val))
161 //add it to the list of the displayable maps
162 gdc.mapwin.registerNewEdgeMap(mapname);
165 gdc.changeEdgeText(mapname);
167 else //!edge.get_active()
170 Graph::NodeMap<double> * emptr=new Graph::NodeMap<double> (gdc.mapstorage.graph);
172 std::stack<double> polishstack;
174 for(NodeIt k(gdc.mapstorage.graph); k!=INVALID; ++k)
176 for(int i=0;i<(int)polishform.size();i++)
180 switch(polishform[i])
186 op1=polishstack.top();
188 op2=polishstack.top();
192 std::map< std::string,Graph::NodeMap<double> * > nms=gdc.mapstorage.nodemap_storage;
193 bool itisvar=(nms.find(ch2var[ polishform[i] ])!=nms.end());
196 polishstack.push( (*(gdc.mapstorage.nodemap_storage[ ch2var[ polishform[i] ] ]))[k]);
200 char * def_val_ch=new char [(int)(ch2var[ polishform[i] ].length())];
201 for(int j=0;j<(int)(ch2var[ polishform[i] ].length());j++)
203 def_val_ch[j]=ch2var[ polishform[i] ][j];
205 polishstack.push(atof(def_val_ch));
213 switch(polishform[i])
228 std::cout << "How could we get here?" << std::endl;
231 polishstack.push(res);
234 (*emptr)[k]=polishstack.top();
237 //if addition was not successful addNodeMap returns one.
238 //cause can be that there is already a map named like the new one
239 if(gdc.mapstorage.addNodeMap(mapname,emptr, def_val))
244 //add it to the list of the displayable maps
245 gdc.mapwin.registerNewNodeMap(mapname);
248 //gdc.changeNodeText(mapname);
253 default_value.set_text("0");
263 std::string NewMapWin::string2Polishform(std::string rawcommand, bool itisedge)
265 bool valid_entry=true;
267 std::map<std::string, int> str2i;
271 std::string variable;
275 for(int i=0;(valid_entry&&(i<(int)rawcommand.size()));i++)
277 switch(rawcommand[i])
285 if(!variable.empty())
287 valid_entry=validVariable(variable, itisedge);
288 ch2var[index]=variable;
291 variable.erase(0,variable.size());
293 command+=rawcommand[i];
296 variable+=rawcommand[i];
301 if(!variable.empty()&&valid_entry)
303 valid_entry=validVariable(variable, itisedge);
304 ch2var[index]=variable;
307 variable.erase(0,variable.size());
312 unsigned int pr=10000;
314 unsigned int prev_change=pr;
315 unsigned int prev_br=pr;
317 std::string comm_nobr="";
318 std::vector<unsigned int> p;
322 //6 brackets embedded
323 //100 operation in a row from the same priority
325 for(int i=0;i<(int)command.size();i++)
327 bool put_in_string=true;
373 comm_nobr=comm_nobr+command[i];
377 tree_node * root=weightedString2Tree(comm_nobr, p, 0);
379 std::string polishform=postOrder(root);
386 NewMapWin::tree_node * NewMapWin::weightedString2Tree(std::string to_tree, std::vector<unsigned int> & p, int offset)
388 unsigned int min=p[offset];
390 for(int i=0;i<(int)to_tree.size();i++)
398 tree_node * act_node=new tree_node;
399 act_node->ch=to_tree[minplace];
400 if(to_tree.size()>=3)
402 act_node->left_child=weightedString2Tree(to_tree.substr(0,minplace), p, offset);
403 act_node->right_child=weightedString2Tree(to_tree.substr(minplace+1,to_tree.size()-minplace-1), p, offset+minplace+1);
407 act_node->left_child=NULL;
408 act_node->right_child=NULL;
413 std::string NewMapWin::postOrder(tree_node * subtree)
415 std::string subtree_to_string;
416 if(subtree->left_child)
418 subtree_to_string=postOrder(subtree->left_child);
420 if(subtree->right_child)
422 subtree_to_string=subtree_to_string+postOrder(subtree->right_child);
424 subtree_to_string=subtree_to_string+subtree->ch;
425 return subtree_to_string;
428 bool NewMapWin::validVariable(std::string variable, bool itisedge)
434 cancel=(gdc.mapstorage.edgemap_storage.find(variable)==gdc.mapstorage.edgemap_storage.end());
438 cancel=(gdc.mapstorage.nodemap_storage.find(variable)==gdc.mapstorage.nodemap_storage.end());
445 for(int j=0;(!cancel)&&(j<(int)variable.size());j++)
447 if(((variable[j]<'0')||(variable[j]>'9'))&&(variable[j]!='.'))