/home/enzo/treballs/fib/pfc/nanocomp/src/ruleManager.cpp

Go to the documentation of this file.
00001 /*
00002  *                                                                         *
00003  *   This program is free software; you can redistribute it and/or modify  *
00004  *   it under the terms of the GNU General Public License as published by  *
00005  *   the Free Software Foundation; either version 2 of the License, or     *
00006  *   (at your option) any later version.                                   *
00007  *                                                                         *
00008  *   This program is distributed in the hope that it will be useful,       *
00009  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00010  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00011  *   GNU General Public License for more details.                          *
00012  *                                                                         *
00013  *   You should have received a copy of the GNU General Public License     *
00014  *   along with this program; if not, write to the                         *
00015  *   Free Software Foundation, Inc.,                                       *
00016  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00017                                                                           */
00018 
00019 // $Revision: 1.21 $
00020 
00021 #include "ruleManager.hpp"
00022 #include <wx/filename.h>
00023 
00024 
00026 
00029 RuleManager::RuleManager(MainController *controller)
00030 {
00031     nextId = 1;
00032     currentRule = 0; //No current rule
00033     modified = false;
00034     needNewFile = false;
00035     fileName = wxEmptyString;
00036     diskManager = new RuleDiskManager(this);
00037     this->controller = controller;
00038 }
00039 
00040 
00042 RuleManager::~RuleManager()
00043 {
00044     delete diskManager;
00045 }
00046 
00047 
00049 
00053 void RuleManager::newRule(int width, int height)
00054 {
00055     Rule *newRule = new Rule(width, height);
00056     ruleList[nextId] = newRule;
00057     currentRule = nextId;
00058     nextId++;
00059     view->updateGrids(newRule->getInitialGrid(), newRule->getFinalGrid(), true);
00060     
00061     updateList();
00062     view->selectRule(currentRule);
00063     ruleSelected(currentRule);
00064     modified = true;
00065     if (fileName == wxEmptyString)
00066     {
00067         needNewFile = true;
00068     }
00069     controller->rulesChanged();
00070     controller->elementChanged();
00071 }
00072 
00073 
00075 
00078 void RuleManager::setView(RuleView *view)
00079 {
00080     this->view = view;
00081 }
00082 
00083 
00085 
00088 void RuleManager::ruleSelected(unsigned int ruleId)
00089 {
00090     currentRule = ruleId;
00091     updateGrids(true);
00092 }
00093 
00094 
00096 
00101 void RuleManager::cellChanged(GridType grid, int x, int y)
00102 {
00103     switch (grid)
00104     {
00105         case nINITIAL:
00106         {
00107             ruleList[currentRule]->cellChanged(x,  y, grid);
00108             break;
00109         }
00110         case nFINAL:
00111         {
00112             ruleList[currentRule]->cellChanged(x,  y, grid);
00113             break;
00114         }
00115     }
00116 
00117     updateGrids(false);
00118     modified = true;
00119     controller->elementChanged();
00120 }
00121 
00122 
00124 
00128 void RuleManager::updateGrids(bool changed)
00129 {
00130     Grid initial;
00131     Grid final;
00132     if (currentRule > 0)
00133     {
00134         initial = ruleList[currentRule]->getInitialGrid();
00135         final = ruleList[currentRule]->getFinalGrid();
00136     }
00137     view->updateGrids(initial, final, changed);
00138 }
00139 
00140 
00142 void RuleManager::removeRule()
00143 {
00144     delete ruleList[currentRule];
00145     map<unsigned int, Rule*>::iterator i = ruleList.find(currentRule);
00146     bool first = (i == ruleList.begin());
00147     map<unsigned int, Rule*>::iterator j = i;
00148     map<unsigned int, Rule*>::iterator k = i;
00149     j++;
00150     k--;
00151     ruleList.erase(i);
00152     
00153     if (j != ruleList.end())
00154     {
00155         currentRule = (*j).first;
00156     }
00157     else
00158     {
00159         if (first)
00160         {
00161             currentRule = 0;//No rules
00162         }
00163         else
00164         {
00165             currentRule = (*k).first;
00166         }
00167     } 
00168     
00169     updateList();
00170     view->selectRule(currentRule);
00171     ruleSelected(currentRule);
00172     modified = true;
00173     controller->rulesChanged();
00174     controller->elementChanged();
00175 }
00176 
00177 
00179 void RuleManager::updateList()
00180 {
00181     wxArrayString stringList;
00182     
00183     for (map<unsigned int, Rule*>::iterator i = ruleList.begin(); i != ruleList.end(); i++)
00184     {
00185         stringList.Add(wxString::Format(_("Rule %d"), (*i).first));
00186     }
00187     
00188     view->updateList(stringList);
00189 }
00190 
00191 
00193 
00194 void RuleManager::copyGrids()
00195 {
00196     if (currentRule > 0)
00197     {
00198         ruleList[currentRule]->setFinalGrid(ruleList[currentRule]->getInitialGrid());
00199         updateGrids(false);
00200     }
00201 }
00202 
00203 
00205 
00210 bool RuleManager::saveFile()
00211 {
00212     if (modified)
00213     {
00214         if (needNewFile)
00215         {
00216             wxString name = fileView.saveFileChoose(view, _("Select a rule file to save"), _("*.rule"));
00217     
00218             if (name != wxEmptyString)
00219             {
00220                 fileName = name;
00221             }
00222             else
00223             {
00224                 return false;
00225             }
00226         }
00227     
00228         list<Rule*> rules;
00229     
00230         for(map<unsigned int, Rule*>::iterator i = ruleList.begin(); i != ruleList.end(); i++)
00231         {
00232             rules.push_back((*i).second);
00233         }
00234     
00235         //Set the extension for the filename
00236         wxFileName extension(fileName);
00237         extension.SetExt(_("rule"));
00238         fileName = extension.GetFullPath();
00239         if (diskManager->saveRules(rules, fileName))
00240         {
00241             modified = false;
00242             needNewFile = false;
00243         }
00244         else
00245         {
00246             view->errorMsg(_("Error saving rule file!"));
00247             return false;
00248         }
00249     }
00250     return true;
00251 }
00252 
00253 
00255 
00258 bool RuleManager::saveFileAs()
00259 {
00260     wxString name = fileView.saveFileChoose(view, _("Select a rule file to save"), _("*.rule"));
00261     
00262     if (name != wxEmptyString)
00263     {
00264         fileName = name;
00265     }
00266     else
00267     {
00268         return false;
00269     }
00270     
00271     list<Rule*> rules;
00272   
00273     for(map<unsigned int, Rule*>::iterator i = ruleList.begin(); i != ruleList.end(); i++)
00274     {
00275         rules.push_back((*i).second);
00276     }
00277     
00278     //Set the extension for the filename
00279     wxFileName extension(fileName);
00280     extension.SetExt(_("rule"));
00281     fileName = extension.GetFullPath();
00282     if (diskManager->saveRules(rules, fileName))
00283     {
00284         modified = false;
00285         needNewFile = false;
00286     }
00287     else
00288     {
00289         view->errorMsg(_("Error saving rule file!"));
00290         return false;
00291     }
00292     return true;
00293 }
00294 
00295 
00297 
00301 void RuleManager::openFile()
00302 {
00303     bool deleteRules = false;;
00304     //We should ask if the user wants to replace
00305     //the rules or if he wants to append them to
00306     //existing
00307     if (ruleList.size() > 0)
00308     {
00309         if (!view->msgYesNo(_("Do you want to append the new rules into the existing ones? (Selecting no will delete them)")))
00310         {
00311             deleteRules = true;
00312             //We have to delete existing rules
00313             //Check if the user wants to save modified rules
00314             if (modified)
00315             {
00316                 if (view->msgYesNo(_("Do you want to save modified rules?")))
00317                 {
00318                     saveFile();
00319                 }
00320             }
00321         }
00322         else
00323         {
00324             deleteRules = false;
00325         }
00326         
00327         if (!appendRules(deleteRules, false))
00328         {
00329             view->errorMsg(_("Error opening rule file"));
00330             clean();
00331         }
00332     }
00333     else
00334     {
00335         if (!appendRules(deleteRules, true))
00336         {
00337             view->errorMsg(_("Error opening rule file"));
00338             clean();
00339         }
00340     }
00341     controller->elementChanged();
00342 }
00343 
00344 
00346 
00350 bool RuleManager::openPFile(wxString file)
00351 {
00352     bool result;
00353     deleteRules();
00354     fileName = file;
00355     result = diskManager->openRules(file);
00356     updateList();
00357     view->selectRule(currentRule);
00358     ruleSelected(currentRule);
00359     controller->elementChanged();
00360     return result;
00361 }
00362 
00363 
00365 void RuleManager::deleteRules()
00366 {
00367     ruleList.clear();
00368     nextId = 1;
00369     currentRule = 0; //No current rule
00370     controller->elementChanged();
00371 }
00372 
00373 
00375 
00381 bool RuleManager::appendRules(bool mustDeleteRules, bool newFile)
00382 {
00383     bool result = true;
00384     wxString name =fileView.openFileChoose(view, _("Select a rule file to open"), _("*.rule"));
00385     
00386     if (name != wxEmptyString)
00387     {
00388         if (mustDeleteRules)
00389         {
00390             deleteRules();
00391         }
00392         //Set te new filename if necessary
00393         if (newFile)
00394         {
00395             fileName = name;
00396         }
00397         
00398         result = diskManager->openRules(name);
00399     }
00400     
00401     updateList();
00402     view->selectRule(currentRule);
00403     ruleSelected(currentRule);
00404     modified = true;
00405     
00406     controller->elementChanged();
00407     return result;    
00408 }
00409 
00410 
00412 
00415 void RuleManager::newRule(Rule *rule)
00416 {
00417     ruleList[nextId] = rule;
00418     currentRule = nextId;
00419     nextId++;
00420     modified = true;
00421     controller->rulesChanged();
00422     controller->elementChanged();
00423 }
00424 
00425 
00427 
00430 wxArrayString RuleManager::getRuleList()
00431 {
00432     wxArrayString result;
00433     
00434     for (map<unsigned int, Rule*>::iterator i = ruleList.begin(); i != ruleList.end(); i++)
00435     {
00436         result.Add(wxString::Format(_("Rule %d"), (*i).first));
00437     }
00438     
00439     return result;  
00440 }
00441 
00442 
00444 
00447 bool RuleManager::isModified()
00448 {
00449     return modified;
00450 }
00451 
00452 
00454 
00457 wxString RuleManager::getFileName()
00458 {
00459     return fileName;
00460 }
00461 
00462 
00464 
00468 bool RuleManager::saveFilename(wxString file)
00469 {
00470     list<Rule*> rules;
00471     
00472     for(map<unsigned int, Rule*>::iterator i = ruleList.begin(); i != ruleList.end(); i++)
00473     {
00474         rules.push_back((*i).second);
00475     }
00476     
00477     if (diskManager->saveRules(rules, file))
00478     {
00479         fileName = file;
00480         return true;
00481     }
00482     else
00483     {
00484         return false;
00485     }
00486 }
00487 
00488 
00491 void RuleManager::clean()
00492 {
00493     fileName = wxEmptyString;
00494     deleteRules();
00495     updateList();
00496     view->selectRule(currentRule);
00497     ruleSelected(currentRule);
00498     controller->rulesChanged();
00499     controller->elementChanged();
00500     modified = false;
00501 }
00502 
00503 
00505 
00508 bool RuleManager::checkRules()
00509 {
00510     vector<bool> errors;
00511     errors.resize(ruleList.size());
00512     for (unsigned int i = 0; i < errors.size(); i++)
00513     {
00514         errors[i] = false;
00515     }
00516     
00517     for (map<unsigned int, Rule*>::iterator i = ruleList.begin(); i != ruleList.end(); i++)
00518     {
00519         if ((*i).second->sameInitialFinal())
00520         {
00521             view->setListMessage((*i).first, _(" - Initial equals to final"));
00522             errors[(*i).first - 1] = true;
00523         }
00524         else if (!(*i).second->valid())
00525         {
00526             view->setListMessage((*i).first, _(" - Invalid"));
00527             errors[(*i).first - 1] = true;
00528         }
00529         map<unsigned int, Rule*>::iterator j = i;
00530         j++;
00531         while (j != ruleList.end())
00532         {
00533             if ((*i).second->equals((*j).second))
00534             {
00535                 view->setListMessage((*j).first, wxString::Format(_(" - Repeated (%d)"), (*i).first));
00536                 errors[(*j).first - 1] = true;
00537             }
00538             j++;
00539         }
00540     }
00541     
00542     bool result = true;
00543     for (unsigned int i = 0; i < errors.size(); i++)
00544     {
00545         if (!errors[i])
00546         {
00547             view->resetListItem(i + 1);
00548         }
00549         else
00550         {
00551             result = false;
00552         }
00553     }
00554     return result;
00555 }
00556 
00557 
00559 
00563 Rule * RuleManager::getRule(unsigned int id)
00564 {
00565     if (ruleList.find(id) != ruleList.end())
00566     {
00567         return ruleList[id];
00568     }
00569     else
00570     {
00571         return NULL;
00572     }
00573 }

Generated on Fri Sep 1 23:55:14 2006 for NanoComp by  doxygen 1.4.6