/home/enzo/treballs/fib/pfc/nanocomp/src/forbiddenPatternManager.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.19 $
00020 
00021 #include "forbiddenPatternManager.hpp"
00022 #include <wx/filename.h>
00023 
00024 
00026 
00029 ForbiddenPatternManager::ForbiddenPatternManager(MainController *controller)
00030 {
00031     nextId = 1;
00032     currentFP = 0; //No FP
00033     modified = false;
00034     needNewFile = false;
00035     fileName = wxEmptyString;
00036     diskManager = new ForbiddenPatternDiskManager(this);
00037     this->controller = controller;
00038 }
00039 
00040 
00042 ForbiddenPatternManager::~ForbiddenPatternManager()
00043 {
00044     delete diskManager;
00045 }
00046 
00047 
00049 
00052 void ForbiddenPatternManager::setView(ForbiddenPatternView *view)
00053 {
00054     this->view = view;
00055 }
00056 
00057 
00059 
00065 void ForbiddenPatternManager::cellChanged(int x, int y)
00066 {
00067     FPList[currentFP]->cellChanged(x,  y);
00068     updateGrids(false);
00069     modified = true;
00070     controller->elementChanged();
00071 }
00072 
00073 
00075 
00080 void ForbiddenPatternManager::updateGrids(bool changed)
00081 {
00082     Grid grid;
00083     if (currentFP > 0)
00084     {
00085         grid = FPList[currentFP]->getGrid();
00086     }
00087     view->updateGrids(grid, changed); 
00088 }
00089 
00090 
00092 
00096 void ForbiddenPatternManager::newFP(int width, int height)
00097 {
00098     ForbiddenPattern *newFP = new ForbiddenPattern(width, height);
00099     //Add the pattern to the collection
00100     FPList[nextId] = newFP;
00101     //Assign the new pattern as the selected one
00102     currentFP = nextId;
00103     nextId++;
00104     //Update the view because of the new selection
00105     view->updateGrids(newFP->getGrid(), true);
00106     updateList();
00107     view->selectFP(currentFP);
00108     FPSelected(currentFP);
00109     if (fileName == wxEmptyString)
00110     {
00111         needNewFile = true;
00112     }
00113     modified = true;
00114     //Notify the controller that the patterns have changed
00115     controller->FPsChanged();
00116     controller->elementChanged();
00117 }
00118 
00119 
00121 void ForbiddenPatternManager::updateList()
00122 {
00123     wxArrayString stringList;
00124     for (map<unsigned int, ForbiddenPattern*>::iterator i = FPList.begin(); i != FPList.end(); i++)
00125     {
00126         stringList.Add(wxString::Format(_("Pattern %d"), (*i).first));
00127     }
00128     view->updateList(stringList);
00129 }
00130 
00132 
00135 void ForbiddenPatternManager::FPSelected(unsigned int FPId)
00136 {
00137     currentFP = FPId;
00138     updateGrids(true);
00139 }
00140 
00141 
00143 void ForbiddenPatternManager::removeFP()
00144 {
00145     //Delete the pattern
00146     delete FPList[currentFP];
00147     //Here we choose the new selected pattern
00148     map<unsigned int, ForbiddenPattern*>::iterator i = FPList.find(currentFP);
00149     bool first = (i == FPList.begin());
00150     //Create two iterators because we have to delete an element
00151     //pointed by this one and then the iterator comes invalid
00152     map<unsigned int, ForbiddenPattern*>::iterator j = i;
00153     map<unsigned int, ForbiddenPattern*>::iterator k = i;
00154     j++;
00155     k--;
00156     FPList.erase(i);
00157     if (j != FPList.end())
00158     {
00159         currentFP = (*j).first;
00160     }
00161     else
00162     {
00163         //We deleted the only pattern left
00164         if (first)
00165         {
00166             currentFP = 0;//No FPs
00167         }
00168         else
00169         {
00170             currentFP = (*k).first;
00171         }
00172     } 
00173     //Update view
00174     updateList();
00175     view->selectFP(currentFP);
00176     FPSelected(currentFP);
00177     modified = true;
00178     //Notify main controller that patterns have changed
00179     controller->FPsChanged();
00180     controller->elementChanged();
00181 }
00182 
00183 
00185 
00191 bool ForbiddenPatternManager::saveFile()
00192 {
00193     if (modified)
00194     {
00195         if (needNewFile)
00196         {
00197             wxString name = fileView.saveFileChoose(view, _("Select a forbidden pattern file to save"), _("*.rule"));
00198     
00199             if (name != wxEmptyString)
00200             {
00201                 fileName = name;
00202             }
00203             else
00204             {
00205                 return false;
00206             }
00207         }
00208     
00209         list<ForbiddenPattern*> FPs;
00210     
00211         for(map<unsigned int, ForbiddenPattern*>::iterator i = FPList.begin(); i != FPList.end(); i++)
00212         {
00213             FPs.push_back((*i).second);
00214         }
00215         
00216         //Set the extension for the filename
00217         wxFileName extension(fileName);
00218         extension.SetExt(_("rule"));
00219         fileName = extension.GetFullPath();
00220         if (diskManager->saveFPs(FPs, fileName))
00221         {
00222             modified = false;
00223             needNewFile = false;
00224         }
00225         else
00226         {
00227             view->errorMsg(_("Error saving forbidden pattern file!"));
00228             return false;
00229         }
00230     }
00231     return true;
00232 }
00233 
00234 
00236 
00241 bool ForbiddenPatternManager::saveFileAs()
00242 {
00243     wxString name = fileView.saveFileChoose(view, _("Select a forbidden pattern file to save"), _("*.rule"));
00244     
00245     if (name != wxEmptyString)
00246     {
00247         fileName = name;
00248     }
00249     else
00250     {
00251         return false;
00252     }
00253     
00254     list<ForbiddenPattern*> FPs;
00255   
00256     for(map<unsigned int, ForbiddenPattern*>::iterator i = FPList.begin(); i != FPList.end(); i++)
00257     {
00258         FPs.push_back((*i).second);
00259     }
00260    
00261     //Set the extension for the filename
00262     wxFileName extension(fileName);
00263     extension.SetExt(_("rule"));
00264     fileName = extension.GetFullPath();
00265     if (diskManager->saveFPs(FPs, fileName))
00266     {
00267         modified = false;
00268         needNewFile = false;
00269     }
00270     else
00271     {
00272         view->errorMsg(_("Error saving forbidden pattern file!"));
00273         return false;
00274     }
00275     return true;
00276 }
00277 
00278 
00280 
00283 void ForbiddenPatternManager::openFile()
00284 {
00285     bool deleteFPs = false;
00286     //We should ask if the user wants to replace
00287     //the FPs or if he wants to append them to
00288     //existing
00289     if (FPList.size() > 0)
00290     {
00291         if (!view->msgYesNo(_("Do you want to append the new forbidden patterns into the existing ones? (Selecting no will delete them)")))
00292         {
00293             deleteFPs = true;
00294             //We have to delete existing FPs
00295             //Check if the user wants to save modified FPs
00296             if (modified)
00297             {
00298                 if (view->msgYesNo(_("Do you want to save modified forbidden patterns?")))
00299                 {
00300                     saveFile();
00301                 }
00302             }
00303         }
00304         else
00305         {
00306             deleteFPs = false;
00307         }
00308         
00309         if (!appendFPs(deleteFPs, false))
00310         {
00311             view->errorMsg(_("Error opening forbidden pattern file"));
00312             clean();
00313         }
00314     }
00315     else
00316     {
00317         if (!appendFPs(deleteFPs, true))
00318         {
00319             view->errorMsg(_("Error opening forbidden pattern file"));
00320             clean();
00321         }
00322     }
00323     controller->elementChanged();
00324 }
00325 
00326 
00328 void ForbiddenPatternManager::deleteFPs()
00329 {
00330     FPList.clear();
00331     nextId = 1;
00332     currentFP = 0; //No current FP
00333     controller->elementChanged();
00334 }
00335 
00336 
00338 
00344 bool ForbiddenPatternManager::openPFile(wxString file)
00345 {
00346     bool result;
00347     fileName = file;
00348     deleteFPs();
00349     result = diskManager->openFPs(file);
00350     updateList();
00351     view->selectFP(currentFP);
00352     FPSelected(currentFP);
00353     controller->elementChanged();
00354     return result;
00355 }
00356 
00357 
00359 
00369 bool ForbiddenPatternManager::appendFPs(bool mustDeleteFPs, bool newFile)
00370 {
00371     bool result;
00372     wxString name =fileView.openFileChoose(view, _("Select a forbidden pattern file to open"), _("*.rule"));
00373     
00374     if (name != wxEmptyString)
00375     {
00376         if (mustDeleteFPs)
00377         {
00378             deleteFPs();
00379         }
00380         //Set te new filename if necessary
00381         if (newFile)
00382         {
00383             fileName = name;
00384         }
00385         
00386         result = diskManager->openFPs(name);
00387     }
00388     
00389     updateList();
00390     view->selectFP(currentFP);
00391     FPSelected(currentFP);
00392     modified = true;
00393     
00394     controller->elementChanged();
00395     return result;    
00396 }
00397 
00398 
00400 
00403 void ForbiddenPatternManager::newFP(ForbiddenPattern *fp)
00404 {
00405     FPList[nextId] = fp;
00406     currentFP = nextId;
00407     nextId++;
00408     modified = true;
00409     controller->FPsChanged();
00410     controller->elementChanged();
00411 }
00412 
00413 
00415 
00418 wxArrayString ForbiddenPatternManager::getFPList()
00419 {
00420     wxArrayString result;
00421     
00422     for (map<unsigned int, ForbiddenPattern*>::iterator i = FPList.begin(); i != FPList.end(); i++)
00423     {
00424         result.Add(wxString::Format(_("Pattern %d"), (*i).first));
00425     }
00426     
00427     return result;  
00428 }
00429 
00430 
00432 
00435 bool ForbiddenPatternManager::isModified()
00436 {
00437     return modified;
00438 }
00439 
00440 
00442 
00445 wxString ForbiddenPatternManager::getFileName()
00446 {
00447     return fileName;
00448 }
00449 
00450 
00452 
00456 bool ForbiddenPatternManager::saveFilename(wxString file)
00457 {
00458     list<ForbiddenPattern*> FPs;
00459     for(map<unsigned int, ForbiddenPattern*>::iterator i = FPList.begin(); i != FPList.end(); i++)
00460         {
00461             FPs.push_back((*i).second);
00462         }
00463         
00464         if (diskManager->saveFPs(FPs, file))
00465         {
00466             fileName = file;
00467             return true;
00468         }
00469         else
00470         {
00471             return false;
00472         }
00473 }
00474 
00475 
00477 
00481 void ForbiddenPatternManager::clean()
00482 {
00483     fileName = wxEmptyString;
00484     deleteFPs();
00485     updateList();
00486     view->selectFP(currentFP);
00487     FPSelected(currentFP);
00488     controller->FPsChanged();
00489     controller->elementChanged();
00490     modified = false;
00491 }
00492 
00493 
00495 
00498 bool ForbiddenPatternManager::checkPatterns()
00499 {
00500     vector<bool> errors;
00501     errors.resize(FPList.size());
00502     for (unsigned int i = 0; i < errors.size(); i++)
00503     {
00504         errors[i] = false;
00505     }
00506     
00507     for (map<unsigned int, ForbiddenPattern*>::iterator i = FPList.begin(); i != FPList.end(); i++)
00508     {
00509         map<unsigned int, ForbiddenPattern*>::iterator j = i;
00510         j++;
00511         while (j != FPList.end())
00512         {
00513             if ((*i).second->equals((*j).second))
00514             {
00515                 cout << (*i).first << " is equal to " << (*j).first << endl;
00516                 view->setListMessage((*j).first, wxString::Format(_(" - Repeated (%d)"), (*i).first));
00517                 errors[(*j).first - 1] = true;
00518             }
00519             j++;
00520         }
00521     }
00522     
00523     bool result = true;
00524     for (unsigned int i = 0; i < errors.size(); i++)
00525     {
00526         if (!errors[i])
00527         {
00528             view->resetListItem(i + 1);
00529         }
00530         else
00531         {
00532             result = false;
00533         }
00534     }
00535     return result;
00536 }
00537 
00538 
00540 
00544 ForbiddenPattern* ForbiddenPatternManager::getFP(unsigned int id)
00545 {
00546     if (FPList.find(id) != FPList.end())
00547     {
00548         return FPList[id];
00549     }
00550     else
00551     {
00552         return NULL;
00553     }
00554 }

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