/home/enzo/treballs/fib/pfc/nanocomp/src/truthTableManager.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 "truthTableManager.hpp"
00022 #include <wx/filename.h>
00023 #include <list>
00024 
00026 
00029 TruthTableManager::TruthTableManager(MainController *controller)
00030 {
00031     currentTable = wxEmptyString; //No tables
00032     modified = false;
00033     needNewFile = false;
00034     fileName = wxEmptyString;
00035     diskManager = new TruthTableDiskManager(this);
00036     this->controller = controller;
00037 }
00038 
00039 
00041 TruthTableManager::~TruthTableManager()
00042 {
00043     delete diskManager;
00044 }
00045 
00046 
00048 
00051 void TruthTableManager::setView(TruthTableView *view)
00052 {
00053     this->view = view;
00054 }
00055 
00056 
00058 void TruthTableManager::updateTable()
00059 {
00060     if (currentTable != wxEmptyString)
00061     {
00062         view->updateTable(*tableList[currentTable]);
00063     }
00064     else
00065     {
00066         view->updateTable(TruthTable());
00067     }
00068 }
00069 
00070 
00072 
00077 void TruthTableManager::newTable(wxString name, int inputs, int outputs)
00078 {
00079     if (tableList.find(name) != tableList.end())
00080     {
00081         view->errorMsg(_("This name is already taken, please select another one"));
00082         return;
00083     }
00084     wxStopWatch s1;
00085     TruthTable *newTable = new TruthTable(name, inputs, outputs);
00086     tableList[name] = newTable;
00087     currentTable = name;
00088     wxStopWatch s2;
00089     view->updateTable(*newTable);
00090     
00091     updateList();
00092     view->selectTable(currentTable);
00093     tableSelected(currentTable);
00094     if (fileName == wxEmptyString)
00095     {
00096         needNewFile = true;
00097     }
00098     modified = true;
00099     controller->tablesChanged();
00100     controller->elementChanged();
00101 }
00102 
00103 
00105 void TruthTableManager::updateList()
00106 {
00107     wxArrayString stringList;
00108     for (map<wxString, TruthTable*>::iterator i = tableList.begin(); i != tableList.end(); i++)
00109     {
00110         stringList.Add((*i).second->getName());
00111     }
00112     view->updateList(stringList);
00113 }
00114 
00115 
00117 
00120 void TruthTableManager::tableSelected(wxString tableId)
00121 {
00122     currentTable = tableId;
00123     updateTable();    
00124 }
00125 
00126 
00128 void TruthTableManager::removeTable()
00129 {
00130     delete tableList[currentTable];
00131     map<wxString, TruthTable*>::iterator i = tableList.find(currentTable);
00132     bool first = (i == tableList.begin());
00133     map<wxString, TruthTable*>::iterator j = i;
00134     map<wxString, TruthTable*>::iterator k = i;
00135     j++;
00136     k--;
00137     tableList.erase(i);
00138     if (j != tableList.end())
00139     {
00140         currentTable = (*j).first;
00141     }
00142     else
00143     {
00144         if (first)
00145         {
00146             currentTable = wxEmptyString;//No Tables
00147         }
00148         else
00149         {
00150             currentTable = (*k).first;
00151         }
00152     } 
00153     updateList();
00154     view->selectTable(currentTable);
00155     tableSelected(currentTable);
00156     modified = true;
00157     controller->tablesChanged();
00158     controller->elementChanged();
00159 }
00160 
00161 
00163 
00167 void TruthTableManager::tableChanged(vector<bool> input, int output)
00168 {
00169     vector<bool> newOutput = tableList[currentTable]->getOutput(input);
00170     newOutput[output] = !newOutput[output];
00171     tableList[currentTable]->setOutput(input, newOutput);
00172     modified = true;
00173     //Optimization: since we already update the table on the
00174     //event we don't need to update it.
00175     //updateTable();
00176     controller->elementChanged();
00177 }
00178 
00179 
00181 
00186 bool TruthTableManager::saveFile()
00187 {
00188     if (modified)
00189     {
00190         if (needNewFile)
00191         {
00192             wxString name = fileView.saveFileChoose(view, _("Select a truth table file to save"), _("*.ttb"));
00193     
00194             if (name != wxEmptyString)
00195             {
00196                 fileName = name;
00197             }
00198             else
00199             {
00200                 return false;
00201             }
00202         }
00203     
00204         list<TruthTable*> tables;
00205     
00206         for(map<wxString, TruthTable*>::iterator i = tableList.begin(); i != tableList.end(); i++)
00207         {
00208             tables.push_back((*i).second);
00209         }
00210     
00211         //Set the extension for the filename
00212         wxFileName extension(fileName);
00213         extension.SetExt(_("ttb"));
00214         fileName = extension.GetFullPath();
00215         if (diskManager->saveTables(tables, fileName))
00216         {
00217             modified = false;
00218             needNewFile = false;
00219         }
00220         else
00221         {
00222             view->errorMsg(_("Error truth table pattern file!"));
00223             return false;
00224         }
00225     }
00226     return true;
00227 }
00228 
00229 
00231 
00234 bool TruthTableManager::saveFileAs()
00235 {
00236     wxString name = fileView.saveFileChoose(view, _("Select a truth table file to save"), _("*.ttb"));
00237     
00238     if (name != wxEmptyString)
00239     {
00240         fileName = name;
00241     }
00242     else
00243     {
00244         return false;
00245     }
00246     
00247     list<TruthTable*> tables;
00248   
00249     for(map<wxString, TruthTable*>::iterator i = tableList.begin(); i != tableList.end(); i++)
00250     {
00251         tables.push_back((*i).second);
00252     }
00253     
00254     //Set the extension for the filename
00255     wxFileName extension(fileName);
00256     extension.SetExt(_("ttb"));
00257     fileName = extension.GetFullPath();
00258     if (diskManager->saveTables(tables, fileName))
00259     {
00260         modified = false;
00261         needNewFile = false;
00262     }
00263     else
00264     {
00265         view->errorMsg(_("Error saving truth table file!"));
00266         return false;
00267     }
00268     return true;
00269 }
00270 
00271 
00273 void TruthTableManager::openFile()
00274 {
00275     bool deleteTables = false;;
00276     //We should ask if the user wants to replace
00277     //the tables or if he wants to append them to
00278     //existing
00279     if (tableList.size() > 0)
00280     {
00281         if (!view->msgYesNo(_("Do you want to append the new truth tables into the existing ones? (Selecting no will delete them)")))
00282         {
00283             deleteTables = true;
00284             //We have to delete existing tables
00285             //Check if the user wants to save modified tables
00286             if (modified)
00287             {
00288                 if (view->msgYesNo(_("Do you want to save modified truth tables?")))
00289                 {
00290                     saveFile();
00291                 }
00292             }
00293         }
00294         else
00295         {
00296             deleteTables = false;
00297         }
00298         
00299         if (!appendTables(deleteTables, false))
00300         {
00301             view->errorMsg(_("Error opening truth table file"));
00302             clean();
00303         }
00304     }
00305     else
00306     {
00307         if (!appendTables(deleteTables, true))
00308         {
00309             view->errorMsg(_("Error opening truth table file"));
00310             clean();
00311         }
00312     }
00313     controller->elementChanged();
00314 }
00315 
00316 
00318 
00322 bool TruthTableManager::openPFile(wxString file)
00323 {
00324     bool result;
00325     deleteTables();
00326     fileName = file;
00327     result = diskManager->openTables(file);
00328     updateList();
00329     view->selectTable(currentTable);
00330     tableSelected(currentTable);
00331     controller->elementChanged();
00332     return result;
00333 }
00334 
00335 
00337 void TruthTableManager::deleteTables()
00338 {
00339     tableList.clear();
00340     currentTable = wxEmptyString; //No current table
00341     controller->elementChanged();
00342 }
00343 
00344 
00346 
00352 bool TruthTableManager::appendTables(bool mustDeleteTables, bool newFile)
00353 {
00354     wxString name =fileView.openFileChoose(view, _("Select a truth table file to open"), _("*.ttb"));
00355     
00356     if (name != wxEmptyString)
00357     {
00358         if (mustDeleteTables)
00359         {
00360             deleteTables();
00361         }
00362         //Set te new filename if necessary
00363         if (newFile)
00364         {
00365             fileName = name;
00366         }
00367         
00368         diskManager->openTables(name);
00369     }
00370     
00371     updateList();
00372     view->selectTable(currentTable);
00373     tableSelected(currentTable);
00374     modified = true;
00375     controller->elementChanged();
00376     return true;    
00377 }
00378 
00379 
00380 //TODO: check if the table name is available
00382 
00385 void TruthTableManager::newTable(TruthTable *table)
00386 {
00387     if (tableList.find(table->getName()) != tableList.end())
00388     {
00389         view->errorMsg(_("A table with name ") + table->getName() + _(" is already taken, it won't be added"));
00390         return;
00391     }
00392     tableList[table->getName()] = table;
00393     currentTable = table->getName();
00394     controller->tablesChanged();
00395     modified = true;
00396     controller->elementChanged();
00397 }
00398 
00399 
00401 
00404 wxArrayString TruthTableManager::getTableList()
00405 {
00406     wxArrayString result;
00407     
00408     for (map<wxString, TruthTable*>::iterator i = tableList.begin(); i != tableList.end(); i++)
00409     {
00410         result.Add((*i).first);
00411     }
00412     
00413     return result;  
00414 }
00415 
00416 
00418 
00422 int TruthTableManager::getInputs(wxString tableId)
00423 {
00424     if (tableList.find(tableId) == tableList.end())
00425     {
00426         return 0;
00427     }
00428     else
00429     {
00430         return tableList[tableId]->getInputs();
00431     }
00432 }
00433 
00434 
00436 
00440 int TruthTableManager::getOutputs(wxString tableId)
00441 {
00442     if (tableList.find(tableId) == tableList.end())
00443     {
00444         return 0;
00445     }
00446     else
00447     {
00448         return tableList[tableId]->getOutputs();
00449     }
00450 }
00451 
00452 
00454 
00458 bool TruthTableManager::isModified()
00459 {
00460     return modified;
00461 }
00462 
00463 
00465 
00468 wxString TruthTableManager::getFileName()
00469 {
00470     return fileName;
00471 }
00472 
00473 
00475 
00479 bool TruthTableManager::saveFilename(wxString file)
00480 {
00481     list<TruthTable*> tables;
00482     
00483     for(map<wxString, TruthTable*>::iterator i = tableList.begin(); i != tableList.end(); i++)
00484     {
00485         tables.push_back((*i).second);
00486     }
00487     
00488     if (diskManager->saveTables(tables, file))
00489     {
00490         fileName = file;
00491         return true;
00492     }
00493     else
00494     {
00495         return false;
00496     }
00497 }
00498 
00499 
00501 void TruthTableManager::clean()
00502 {
00503     fileName = wxEmptyString;
00504     deleteTables();
00505     updateList();
00506     view->selectTable(currentTable);
00507     tableSelected(currentTable);
00508     controller->tablesChanged();
00509     modified = false;
00510 }
00511 
00512 
00514 
00518 TruthTable * TruthTableManager::getTable(wxString id)
00519 {
00520     if (tableList.find(id) != tableList.end())
00521     {
00522         return tableList[id];
00523     }
00524     else
00525     {
00526         return NULL;
00527     }
00528 }

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