Changeset 201:879e47e5b731 in glemon-0.x for new_map_win.cc
- Timestamp:
- 01/02/08 22:03:09 (16 years ago)
- Branch:
- default
- Phase:
- public
- Convert:
- svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/glemon/trunk@3431
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
new_map_win.cc
r194 r201 30 30 } 31 31 32 NewMapWin::NewMapWin(const std::string& title, NoteBookTab & mw, bool itisedge, bool edgenode ):Gtk::Dialog(title, true, true),mytab(mw),node("Create NodeMap"),edge("Create EdgeMap")32 NewMapWin::NewMapWin(const std::string& title, NoteBookTab & mw, bool itisedge, bool edgenode, MapType type):Gtk::Dialog(title, true, true),mytab(mw),node("Create NodeMap"),edge("Create EdgeMap"),map_type(type) 33 33 { 34 34 set_default_size(200, 50); … … 39 39 40 40 //entries 41 table=new Gtk::Table( 3, 2, false);41 table=new Gtk::Table(5, 2, false); 42 42 43 43 label=new Gtk::Label; … … 48 48 (*table).attach(name,1,2,0,1,Gtk::SHRINK,Gtk::SHRINK,10,3); 49 49 50 lblType.set_label("Element type:"); 51 if (map_type & NUM) 52 cbType.append_text("Numeric"); 53 if (map_type & STR) 54 cbType.append_text("String"); 55 cbType.set_active(0); 56 57 (*table).attach(lblType,0,1,1,2,Gtk::SHRINK,Gtk::SHRINK,10,3); 58 (*table).attach(cbType, 1,2,1,2,Gtk::SHRINK,Gtk::SHRINK,10,3); 59 50 60 label=new Gtk::Label; 51 61 label->set_text("Default value in the map:"); 52 62 default_value.set_text("0"); 53 63 54 (*table).attach(*label,0,1, 1,2,Gtk::SHRINK,Gtk::SHRINK,10,3);55 (*table).attach(default_value,1,2, 1,2,Gtk::SHRINK,Gtk::SHRINK,10,3);64 (*table).attach(*label,0,1,2,3,Gtk::SHRINK,Gtk::SHRINK,10,3); 65 (*table).attach(default_value,1,2,2,3,Gtk::SHRINK,Gtk::SHRINK,10,3); 56 66 57 67 //node vs. edge map selector 58 68 Gtk::RadioButton::Group group = node.get_group(); 59 69 edge.set_group(group); 60 70 61 71 if(edgenode) 62 63 (*table).attach(node,0,1,2,3,Gtk::SHRINK,Gtk::SHRINK,10,3);64 (*table).attach(edge,1,2,2,3,Gtk::SHRINK,Gtk::SHRINK,10,3);65 72 { 73 (*table).attach(node,0,1,3,4,Gtk::SHRINK,Gtk::SHRINK,10,3); 74 (*table).attach(edge,1,2,3,4,Gtk::SHRINK,Gtk::SHRINK,10,3); 75 } 66 76 else 67 { 68 if(itisedge) 69 { 70 edge.set_active(); 71 } 72 else 73 { 74 node.set_active(); 75 } 76 } 77 { 78 if(itisedge) 79 { 80 edge.set_active(); 81 } 82 else 83 { 84 node.set_active(); 85 } 86 } 87 88 (*table).attach(lblErrorMsg,0,2,4,5,Gtk::SHRINK,Gtk::SHRINK,10,3); 77 89 78 90 vbox->pack_start(*table); … … 85 97 } 86 98 99 void NewMapWin::setErrorMsg(const Glib::ustring& msg) 100 { 101 lblErrorMsg.set_markup("<i><small>" + msg + "</small></i>"); 102 } 103 104 std::vector<double>* NewMapWin::evaluate_expr(const std::string polishform, bool itisedge) 105 { 106 MapStorage& ms = *mytab.mapstorage; 107 108 std::vector<double>* ret = new std::vector<double>; 109 std::stack<double> polishstack; 110 111 if (itisedge) 112 { 113 for(EdgeIt k(ms.graph); k!=INVALID; ++k) 114 { 115 for(int i=0;i<(int)polishform.size();i++) 116 { 117 double op1=0, op2=0; 118 bool operation=true; 119 switch(polishform[i]) 120 { 121 case '+': 122 case '-': 123 case '/': 124 case '*': 125 op1=polishstack.top(); 126 polishstack.pop(); 127 op2=polishstack.top(); 128 polishstack.pop(); 129 break; 130 default: 131 //substitute variable 132 std::vector<std::string> maps = ms.getEdgeMapList(NUM); 133 bool itisvar=(std::find(maps.begin(), maps.end(), ch2var[ polishform[i] ]) != maps.end()); 134 if(itisvar) 135 { 136 polishstack.push(ms.get(ch2var[ polishform[i] ], k)); 137 } 138 else 139 { 140 polishstack.push(atof(ch2var[ polishform[i] ].c_str())); 141 } 142 operation=false; 143 break; 144 } 145 if(operation) 146 { 147 double res; 148 switch(polishform[i]) 149 { 150 case '+': 151 res=op1+op2; 152 break; 153 case '-': 154 res=op2-op1; 155 break; 156 case '/': 157 res=op2/op1; 158 break; 159 case '*': 160 res=op1*op2; 161 break; 162 default: 163 std::cout << "How could we get here?" << std::endl; 164 break; 165 } 166 polishstack.push(res); 167 } 168 }//foreach letter in polishform 169 ret->push_back(polishstack.top()); 170 }//foreach edge 171 } 172 else 173 { 174 for(NodeIt k(ms.graph); k!=INVALID; ++k) 175 { 176 for(int i=0;i<(int)polishform.size();i++) 177 { 178 double op1=0, op2=0; 179 bool operation=true; 180 switch(polishform[i]) 181 { 182 case '+': 183 case '-': 184 case '/': 185 case '*': 186 op1=polishstack.top(); 187 polishstack.pop(); 188 op2=polishstack.top(); 189 polishstack.pop(); 190 break; 191 default: 192 //substitute variable 193 std::vector<std::string> maps = ms.getNodeMapList(NUM); 194 bool itisvar=(std::find(maps.begin(), maps.end(), ch2var[ polishform[i] ]) != maps.end()); 195 if(itisvar) 196 { 197 polishstack.push(ms.get(ch2var[ polishform[i] ], k)); 198 } 199 else 200 { 201 polishstack.push(atof(ch2var[ polishform[i] ].c_str())); 202 } 203 operation=false; 204 break; 205 } 206 if(operation) 207 { 208 double res; 209 switch(polishform[i]) 210 { 211 case '+': 212 res=op1+op2; 213 break; 214 case '-': 215 res=op2-op1; 216 break; 217 case '/': 218 res=op2/op1; 219 break; 220 case '*': 221 res=op1*op2; 222 break; 223 default: 224 std::cout << "How could we get here?" << std::endl; 225 break; 226 } 227 polishstack.push(res); 228 } 229 }//foreach letter in polishform 230 ret->push_back(polishstack.top()); 231 }//foreach edge 232 } 233 return ret; 234 } 235 87 236 void NewMapWin::on_response(int response_id) 88 237 { 238 MapStorage& ms = *mytab.mapstorage; 239 89 240 if(response_id==Gtk::RESPONSE_OK) 90 { 91 double def_val=0; 92 93 //get and formulate text 94 std::string def_val_str=default_value.get_text(); 95 96 bool only_nums=true; 97 for(int i=0;i<(int)def_val_str.size() && only_nums;i++) 98 { 99 if( def_val_str[i]<'0' || def_val_str[i]>'9' ) 100 { 101 only_nums=false; 102 } 103 } 104 std::string polishform; 105 106 if(only_nums) 107 { 108 def_val=atof(def_val_str.c_str()); 109 } 241 { 242 std::string map_name = name.get_text(); 243 std::string def_val = default_value.get_text(); 244 245 if (map_name.empty()) 246 { 247 setErrorMsg("No map name given."); 248 return; 249 } 250 251 // check whether the map already exists 252 if (edge.get_active()) 253 { 254 if (ms.edgeMapExists(map_name)) 255 { 256 setErrorMsg("Map '" + map_name + "' already exists."); 257 return; 258 } 259 } 260 else 261 { 262 if (ms.nodeMapExists(map_name)) 263 { 264 setErrorMsg("Map '" + map_name + "' already exists."); 265 return; 266 } 267 } 268 269 Glib::ustring text = cbType.get_active_text(); 270 if (text == "Numeric") 271 { 272 double d; 273 char *endptr; 274 d = strtod(def_val.c_str(), &endptr); 275 if (def_val.c_str() + def_val.length() == endptr) 276 { 277 // the full string was a number 278 if (edge.get_active()) 279 ms.createEdgeMap(map_name, MapValue::NUMERIC, 280 MapValue(d)); 281 else 282 ms.createNodeMap(map_name, MapValue::NUMERIC, 283 MapValue(d)); 284 } 110 285 else 111 { 112 polishform=string2Polishform(def_val_str,edge.get_active()); 113 } 114 115 //get name of text 116 std::string mapname=name.get_text(); 117 118 if(!mapname.empty()&&(!polishform.empty()||only_nums)) 119 { 120 int abortion=0; 121 if(edge.get_active()) 122 { 123 //create the new map 124 Graph::EdgeMap<double> * emptr=new Graph::EdgeMap<double> (mytab.mapstorage->graph, def_val); 125 126 if(!only_nums) 127 { 128 std::stack<double> polishstack; 129 130 for(EdgeIt k(mytab.mapstorage->graph); k!=INVALID; ++k) 131 { 132 for(int i=0;i<(int)polishform.size();i++) 133 { 134 double op1=0, op2=0; 135 bool operation=true; 136 switch(polishform[i]) 137 { 138 case '+': 139 case '-': 140 case '/': 141 case '*': 142 op1=polishstack.top(); 143 polishstack.pop(); 144 op2=polishstack.top(); 145 polishstack.pop(); 146 break; 147 default: 148 //substitute variable 149 std::map< std::string,Graph::EdgeMap<double> * > ems=mytab.mapstorage->edgemap_storage; 150 bool itisvar=(ems.find(ch2var[ polishform[i] ])!=ems.end()); 151 if(itisvar) 152 { 153 polishstack.push( (*(mytab.mapstorage->edgemap_storage[ ch2var[ polishform[i] ] ]))[k]); 154 } 155 else 156 { 157 polishstack.push(atof(ch2var[ polishform[i] ].c_str())); 158 } 159 operation=false; 160 break; 161 } 162 if(operation) 163 { 164 double res; 165 switch(polishform[i]) 166 { 167 case '+': 168 res=op1+op2; 169 break; 170 case '-': 171 res=op2-op1; 172 break; 173 case '/': 174 res=op2/op1; 175 break; 176 case '*': 177 res=op1*op2; 178 break; 179 default: 180 std::cout << "How could we get here?" << std::endl; 181 break; 182 } 183 polishstack.push(res); 184 } 185 }//foreach letter in polishform 186 (*emptr)[k]=polishstack.top(); 187 }//foreach edge 188 }//!only_nums 189 190 //if addition was not successful addEdgeMap returns one. 191 //cause can be that there is already a map named like the new one 192 if(mytab.mapstorage->addEdgeMap(mapname, emptr, def_val)) 193 { 194 abortion=1; 195 } 196 197 //add it to the list of the displayable maps 198 //furthermore it is done by signals 199 //mytab.registerNewEdgeMap(mapname); 200 201 //display it 202 //gdc.changeEdgeText(mapname); 203 } 204 else //!edge.get_active() 205 { 206 //create the new map 207 Graph::NodeMap<double> * emptr=new Graph::NodeMap<double> (mytab.mapstorage->graph, def_val); 208 209 if(!only_nums) 210 { 211 std::stack<double> polishstack; 212 213 for(NodeIt k(mytab.mapstorage->graph); k!=INVALID; ++k) 214 { 215 for(int i=0;i<(int)polishform.size();i++) 216 { 217 double op1=0, op2=0; 218 bool operation=true; 219 switch(polishform[i]) 220 { 221 case '+': 222 case '-': 223 case '/': 224 case '*': 225 op1=polishstack.top(); 226 polishstack.pop(); 227 op2=polishstack.top(); 228 polishstack.pop(); 229 break; 230 default: 231 std::map< std::string,Graph::NodeMap<double> * > nms=mytab.mapstorage->nodemap_storage; 232 bool itisvar=(nms.find(ch2var[ polishform[i] ])!=nms.end()); 233 if(itisvar) 234 { 235 polishstack.push( (*(mytab.mapstorage->nodemap_storage[ ch2var[ polishform[i] ] ]))[k]); 236 } 237 else 238 { 239 polishstack.push(atof(ch2var[ polishform[i] ].c_str())); 240 } 241 operation=false; 242 break; 243 } 244 if(operation) 245 { 246 double res; 247 switch(polishform[i]) 248 { 249 case '+': 250 res=op1+op2; 251 break; 252 case '-': 253 res=op2-op1; 254 break; 255 case '/': 256 res=op2/op1; 257 break; 258 case '*': 259 res=op1*op2; 260 break; 261 default: 262 std::cout << "How could we get here?" << std::endl; 263 break; 264 } 265 polishstack.push(res); 266 } 267 } 268 (*emptr)[k]=polishstack.top(); 269 } 270 } 271 //if addition was not successful addNodeMap returns one. 272 //cause can be that there is already a map named like the new one 273 if(mytab.mapstorage->addNodeMap(mapname,emptr, def_val)) 274 { 275 abortion=1; 276 } 277 278 //add it to the list of the displayable maps 279 //furthermore it is done by signals 280 //mytab.registerNewNodeMap(mapname); 281 282 //display it 283 //gdc.changeNodeText(mapname); 284 } 285 if(!abortion) 286 { 287 name.set_text(""); 288 default_value.set_text("0"); 289 edge.show(); 290 node.show(); 291 hide(); 292 } 293 } 294 } 286 { 287 // let't try to evaluate the string as an arithmetic expression 288 std::string polishform = 289 string2Polishform(def_val, edge.get_active()); 290 if (polishform.empty()) 291 return; 292 std::vector<double>* values = 293 evaluate_expr(polishform, edge.get_active()); 294 if (edge.get_active()) 295 { 296 ms.createEdgeMap(map_name, MapValue::NUMERIC, 297 MapValue(0.0)); 298 std::vector<double>::const_iterator vit = values->begin(); 299 for (EdgeIt it(ms.graph); it != INVALID; ++it) 300 { 301 ms.set(map_name, it, MapValue(*vit)); 302 ++vit; 303 } 304 } 305 else 306 { 307 ms.createNodeMap(map_name, MapValue::NUMERIC, 308 MapValue(0.0)); 309 std::vector<double>::const_iterator vit = values->begin(); 310 for (NodeIt it(ms.graph); it != INVALID; ++it) 311 { 312 ms.set(map_name, it, MapValue(*vit)); 313 ++vit; 314 } 315 } 316 delete values; 317 } 318 } 319 else if (text == "String") 320 { 321 if (edge.get_active()) 322 ms.createEdgeMap(map_name, MapValue::STRING, 323 MapValue(def_val)); 324 else 325 ms.createNodeMap(map_name, MapValue::STRING, 326 MapValue(def_val)); 327 } 328 329 name.set_text(""); 330 default_value.set_text("0"); 331 edge.show(); 332 node.show(); 333 hide(); 334 } 295 335 } 296 336 … … 309 349 310 350 for(int i=0;(valid_entry&&(i<(int)rawcommand.size()));i++) 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 351 { 352 switch(rawcommand[i]) 353 { 354 case '+': 355 case '-': 356 case '*': 357 case '/': 358 case ')': 359 case '(': 360 if(!variable.empty()) 361 { 362 valid_entry=validVariable(variable, itisedge); 363 ch2var[index]=variable; 364 command+=index; 365 index++; 366 variable.erase(0,variable.size()); 367 } 368 command+=rawcommand[i]; 369 break; 370 default: 371 variable+=rawcommand[i]; 372 break; 373 } 374 } 335 375 336 376 if(!variable.empty()&&valid_entry) 337 338 339 340 341 342 343 377 { 378 valid_entry=validVariable(variable, itisedge); 379 ch2var[index]=variable; 380 command+=index; 381 index++; 382 variable.erase(0,variable.size()); 383 } 344 384 345 385 if(valid_entry) 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 386 { 387 unsigned int pr=10000; 388 bool prevmult=false; 389 unsigned int prev_change=pr; 390 unsigned int prev_br=pr; 391 int counter=0; 392 std::string comm_nobr=""; 393 std::vector<unsigned int> p; 394 p.resize(counter+1); 395 396 //limits 397 //6 brackets embedded 398 //100 operation in a row from the same priority 399 400 for(int i=0;i<(int)command.size();i++) 401 { 402 bool put_in_string=true; 403 switch(command[i]) 404 { 405 case '(': 406 pr=prev_br+10000; 407 prev_br=pr; 408 prevmult=false; 409 put_in_string=false; 410 break; 411 case ')': 412 pr=prev_br-10000; 413 prev_br=pr; 414 prevmult=false; 415 put_in_string=false; 416 break; 417 case '+': 418 case '-': 419 if(prevmult) 420 { 421 pr=prev_change; 422 } 423 p[counter]=pr; 424 pr-=100; 425 426 prevmult=false; 427 break; 428 case '/': 429 case '*': 430 if(!prevmult) 431 { 432 prev_change=pr; 433 pr+=200; 434 pr-=1; 435 } 436 p[counter]=pr; 437 pr-=1; 438 prevmult=true; 439 break; 440 default: 441 p[counter]=65000; 442 break; 443 } 444 if(put_in_string) 445 { 446 counter++; 447 p.resize(counter+1); 448 comm_nobr=comm_nobr+command[i]; 449 } 450 } 451 452 tree_node * root=weightedString2Tree(comm_nobr, p, 0); 453 454 std::string polishform=postOrder(root); 455 456 deleteTree(root); 457 458 return polishform; 459 } 420 460 return ""; 421 461 } … … 424 464 { 425 465 if(node->left_child!=NULL) 426 427 428 466 { 467 deleteTree(node->left_child); 468 } 429 469 if(node->right_child!=NULL) 430 431 432 470 { 471 deleteTree(node->right_child); 472 } 433 473 delete node; 434 474 } … … 439 479 int minplace=0; 440 480 for(int i=0;i<(int)to_tree.size();i++) 441 442 443 444 445 446 447 481 { 482 if(min>p[offset+i]) 483 { 484 min=p[offset+i]; 485 minplace=i; 486 } 487 } 448 488 tree_node * act_node=new tree_node; 449 489 act_node->ch=to_tree[minplace]; 450 490 if(to_tree.size()>=3) 451 452 453 454 491 { 492 act_node->left_child=weightedString2Tree(to_tree.substr(0,minplace), p, offset); 493 act_node->right_child=weightedString2Tree(to_tree.substr(minplace+1,to_tree.size()-minplace-1), p, offset+minplace+1); 494 } 455 495 else 456 457 458 459 496 { 497 act_node->left_child=NULL; 498 act_node->right_child=NULL; 499 } 460 500 return act_node; 461 501 } … … 465 505 std::string subtree_to_string; 466 506 if(subtree->left_child) 467 468 469 507 { 508 subtree_to_string=postOrder(subtree->left_child); 509 } 470 510 if(subtree->right_child) 471 472 473 511 { 512 subtree_to_string=subtree_to_string+postOrder(subtree->right_child); 513 } 474 514 subtree_to_string=subtree_to_string+subtree->ch; 475 515 return subtree_to_string; … … 478 518 bool NewMapWin::validVariable(std::string variable, bool itisedge) 479 519 { 520 MapStorage& ms = *mytab.mapstorage; 521 480 522 bool cancel; 481 523 //is it mapname? 482 524 if(itisedge) 483 { 484 cancel=(mytab.mapstorage->edgemap_storage.find(variable)==mytab.mapstorage->edgemap_storage.end()); 485 } 525 { 526 std::vector<std::string> edge_maps = 527 ms.getEdgeMapList(NUM); 528 cancel=(std::find(edge_maps.begin(), edge_maps.end(), variable)==edge_maps.end()); 529 } 486 530 else 487 { 488 cancel=(mytab.mapstorage->nodemap_storage.find(variable)==mytab.mapstorage->nodemap_storage.end()); 489 } 531 { 532 std::vector<std::string> node_maps = 533 ms.getNodeMapList(NUM); 534 cancel=(std::find(node_maps.begin(), node_maps.end(), variable)==node_maps.end()); 535 } 490 536 //maybe it is number 491 537 int point_num=0; 492 538 if(cancel) 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 539 { 540 cancel=false; 541 for(int j=0;(!cancel)&&(j<(int)variable.size());j++) 542 { 543 if(((variable[j]<'0')||(variable[j]>'9'))&&(variable[j]!='.')) 544 { 545 cancel=true; 546 } 547 else 548 { 549 if(variable[j]=='.') 550 { 551 point_num++; 552 if(point_num>1) 553 { 554 cancel=true; 555 } 556 } 557 } 558 } 559 } 514 560 if(cancel) 515 516 517 561 { 562 return false; 563 } 518 564 return true; 519 565 }
Note: See TracChangeset
for help on using the changeset viewer.