TinkerCell Core 1.0
TinkerCell's Core library providing all basic functionalities
NetworkHandle.cpp
Go to the documentation of this file.
00001 /***************************************************************************************************
00002 
00003 Copyright (c) 2008 Deepak Chandran
00004 Contact: Deepak Chandran (dchandran1@gmail.com)
00005 See COPYRIGHT.TXT
00006 
00007 A class that is used to store a network. The network is a collection of Item Handles. 
00008 The history stack is also a key component of a network.
00009 The network can either be represented as text using TextEditor or visualized with graphical items in the
00010 GraphicsScene. Each node and connection are contained in a handle, and each handle can either be represented as text or as graphics.
00011 The two main components of NetworkWindow are the SymbolsTable and HistoryStack
00012 This class provides functions for inserting items, removing items, and changing information inside the model.
00013 
00014 ***************************************************************************************************/
00015 
00016 #include <QHBoxLayout>
00017 #include "MainWindow.h"
00018 #include "NetworkWindow.h"
00019 #include "ConsoleWindow.h"
00020 #include "ItemHandle.h"
00021 #include "Tool.h"
00022 #include "GraphicsScene.h"
00023 #include "TextGraphicsItem.h"
00024 #include "TextEditor.h"
00025 #include "UndoCommands.h"
00026 #include "NetworkHandle.h"
00027 #include "ExternalCode/muparser/muParserDef.h"
00028 #include "ExternalCode/muparser/muParser.h"
00029 #include "ExternalCode/muparser/muParserInt.h"
00030 #include <QRegExp>
00031 
00032 namespace Tinkercell
00033 {
00034         NetworkHandle::~NetworkHandle()
00035         {
00036                 close();
00037                 /*
00038                 QList<NetworkWindow*> list = networkWindows;
00039                 networkWindows.clear();
00040 
00041                 for (int i=0; i < list.size(); ++i)
00042                         if (list[i])
00043                         {
00044                                 delete list[i];
00045                         }*/
00046                 history.clear();
00047         }
00048         
00049         QList<ItemHandle*> NetworkHandle::findItem(const QString& s) const
00050         {
00051                 QList<ItemHandle*> items;
00052                 if (symbolsTable.uniqueHandlesWithDot.contains(s))
00053                         items << symbolsTable.uniqueHandlesWithDot[s];
00054                 else
00055                 if (symbolsTable.uniqueHandlesWithUnderscore.contains(s))
00056                         items << symbolsTable.uniqueHandlesWithUnderscore[s];
00057                 else
00058                 if (symbolsTable.nonuniqueHandles.contains(s))
00059                 {
00060                         items = symbolsTable.nonuniqueHandles.values(s);
00061                 }
00062                 return items;
00063         }
00064 
00065         QList<ItemHandle*> NetworkHandle::findItem(const QRegExp& re) const
00066         {
00067                 QList<ItemHandle*> items = this->handles(true), items2;
00068 
00069                 for (int i=0; i < items.size(); ++i)
00070                         if (items[i] && (items[i]->fullName().contains(re) ||  items[i]->fullName("_").contains(re)))
00071                                 items2 += items[i];
00072                         
00073                 return items2;
00074         }
00075         
00076         QList<ItemHandle*> NetworkHandle::findItem(const QStringList& list) const
00077         {
00078                 QList<ItemHandle*> items;
00079                 
00080                 for (int i=0; i < list.size(); ++i)
00081                 {
00082                         QString s = list[i];
00083                         if (symbolsTable.uniqueHandlesWithDot.contains(s))
00084                                 items += symbolsTable.uniqueHandlesWithDot[s];
00085                         else
00086                         if (symbolsTable.uniqueHandlesWithUnderscore.contains(s))
00087                                 items += symbolsTable.uniqueHandlesWithUnderscore[s];
00088                         else
00089                         if (symbolsTable.nonuniqueHandles.contains(s))
00090                         {
00091                                 QList<ItemHandle*> items2 = symbolsTable.nonuniqueHandles.values(s);
00092                                 for (int j=0; j < items2.size(); ++j)
00093                                 {
00094                                         if (!items.contains(items2[j]))
00095                                                 items += items2[j];
00096                                 }
00097                         }
00098                 }
00099                 return items;
00100         }
00101         
00102         QList< QPair<ItemHandle*,QString> > NetworkHandle::findData(const QString& s) const
00103         {
00104                 QList< QPair<ItemHandle*,QString> > list;
00105 
00106                 if (symbolsTable.uniqueDataWithDot.contains(s))
00107                         list = symbolsTable.uniqueDataWithDot.values(s);
00108                 else
00109                 if (symbolsTable.uniqueDataWithUnderscore.contains(s))
00110                         list = symbolsTable.uniqueDataWithUnderscore.values(s);
00111                 else
00112                         list = symbolsTable.nonuniqueData.values(s);
00113 
00114                 return list;
00115         }
00116 
00117         QList< QPair<ItemHandle*,QString> > NetworkHandle::findData(const QRegExp& re) const
00118         {
00119                 QList< QPair<ItemHandle*,QString> > list;
00120 
00121                 QStringList keys = symbolsTable.uniqueDataWithDot.keys();
00122 
00123                 for (int i=0; i < keys.size(); ++i)
00124                         if (keys[i].contains(re))
00125                                 list += symbolsTable.uniqueDataWithDot.values(keys[i]);
00126 
00127                 if (list.isEmpty())
00128                 {
00129                         keys = symbolsTable.uniqueDataWithUnderscore.keys();
00130 
00131                         for (int i=0; i < keys.size(); ++i)
00132                                 if (keys[i].contains(re))
00133                                         list += symbolsTable.uniqueDataWithUnderscore.values(keys[i]);
00134                 }
00135 
00136                 return list;
00137         }
00138         
00139         QList< QPair<ItemHandle*,QString> > NetworkHandle::findData(const QStringList& list) const
00140         {
00141                 QList<ItemHandle*> items;
00142                 QList< QPair<ItemHandle*,QString> > data;
00143                 
00144                 for (int i=0; i < list.size(); ++i)
00145                 {
00146                         QString s = list[i];
00147                         if (symbolsTable.uniqueDataWithDot.contains(s))
00148                                 data += symbolsTable.uniqueDataWithDot[s];                      
00149                         else
00150                         if (symbolsTable.uniqueDataWithUnderscore.contains(s))
00151                                 data += symbolsTable.uniqueDataWithUnderscore[s];                       
00152                         else
00153                         if (symbolsTable.nonuniqueData.contains(s))
00154                         {
00155                                 QList< QPair<ItemHandle*,QString> > items2 = symbolsTable.nonuniqueData.values(s);
00156                                 for (int j=0; j < items2.size(); ++j)
00157                                 {
00158                                         if (!items.contains(items2[j].first))
00159                                         {
00160                                                 items += items2[j].first;
00161                                                 data += items2[j];
00162                                         }
00163                                 }
00164                         }
00165                 }
00166                 return data;
00167         }
00168 
00169         void NetworkHandle::close()
00170         {
00171                 disconnect(&history);
00172                 
00173                 QList<NetworkWindow*> & list = networkWindows;
00174                 networkWindows.clear();
00175 
00176                 for (int i=0; i < list.size(); ++i)
00177                         if (list[i])
00178                         {
00179                                 list[i]->disconnect();
00180                                 list[i]->network = 0;
00181                                 list[i]->close();
00182                         }
00183 
00184                 if (mainWindow)
00185                         mainWindow->allNetworks.removeAll(this);
00186         }
00187         
00188         void NetworkHandle::setWindowTitle(const QString& title)
00189         {
00190                 for (int i=0; i < networkWindows.size(); ++i)
00191                         if (networkWindows[i])
00192                                 networkWindows[i]->setWindowTitle(title);
00193         }
00194 
00195         QList<GraphicsScene*> NetworkHandle::scenes() const
00196         {
00197                 QList<GraphicsScene*> list;
00198                 for (int i=0; i < networkWindows.size(); ++i)
00199                         if (networkWindows[i] && networkWindows[i]->scene)
00200                                 list << networkWindows[i]->scene;
00201                 return list;
00202         }
00203         
00204         QList<TextEditor*> NetworkHandle::editors() const
00205         {
00206                 QList<TextEditor*> list;
00207                 for (int i=0; i < networkWindows.size(); ++i)
00208                         if (networkWindows[i] && networkWindows[i]->editor)
00209                                 list << networkWindows[i]->editor;
00210                 return list;
00211         }
00212         
00213         void NetworkHandle::showScene(GraphicsScene * scene)
00214         {
00215                 if (!scene) return;
00216 
00217                 for (int i=0; i < networkWindows.size(); ++i)
00218                         if (networkWindows[i] && networkWindows[i]->scene == scene && !networkWindows[i]->isVisible())
00219                         {
00220                                 networkWindows[i]->popOut();
00221                                 return;
00222                         }
00223                 if (scene->networkWindow)
00224                 {
00225                         NetworkWindow * window = scene->networkWindow;
00226                         QList<QGraphicsView*> views = scene->views();
00227                         if (views.isEmpty() || !views[0]) return;
00228                         window->scene = scene;
00229                         window->editor = 0;                     
00230                         window->setCentralWidget(views[0]);
00231                         if (!window->isVisible())
00232                                 window->popOut();
00233                 }
00234         }
00235 
00236         void NetworkHandle::showTextEditor(TextEditor * editor)
00237         {
00238                 if (!editor) return;
00239                 for (int i=0; i < networkWindows.size(); ++i)
00240                         if (networkWindows[i] && networkWindows[i]->editor == editor && !networkWindows[i]->isVisible())
00241                         {
00242                                 networkWindows[i]->popOut();
00243                                 return;
00244                         }
00245                 if (editor->networkWindow)
00246                 {
00247                         NetworkWindow * window = editor->networkWindow;
00248                         window->scene = 0;
00249                         window->editor = editor;
00250                         window->setCentralWidget(editor);
00251                         if (!window->isVisible())
00252                                 window->popOut();
00253                 }
00254         }
00255         
00256         TextEditor * NetworkHandle::createTextEditor(const QString& text)
00257         {
00258                 if (!mainWindow) return 0;
00259                 
00260                 TextEditor * tedit = new TextEditor(this);
00261                 tedit->setText(text);
00262                 
00263                 networkWindows << (new NetworkWindow(this,tedit));
00264                 
00265                 return tedit;
00266         }
00267 
00268         GraphicsScene * NetworkHandle::createScene(const QList<QGraphicsItem*>& insertItems)
00269         {
00270                 if (!mainWindow) return 0;
00271                 
00272                 GraphicsScene * scene = new GraphicsScene(this);
00273                 
00274                 for (int i=0; i < insertItems.size(); ++i)
00275                 {
00276                         scene->addItem(insertItems[i]);
00277                 }
00278                 
00279                 networkWindows << (new NetworkWindow(this,scene));
00280                 
00281                 return scene;
00282         }
00283         
00284         GraphicsScene * NetworkHandle::createScene(ItemHandle * item, const QRectF& boundingRect)
00285         {
00286                 if (!item) return 0;
00287                 
00288                 QList<QGraphicsItem*> graphicsItems = item->allGraphicsItems();
00289                 QList<QGraphicsItem*> graphicsItems2;
00290                 
00291                 if (boundingRect.width() > 0 && boundingRect.height() > 0)
00292                 {
00293                         for (int i=0; i < graphicsItems.size(); ++i)
00294                                 if (graphicsItems[i] && boundingRect.contains(graphicsItems[i]->sceneBoundingRect()))
00295                                         graphicsItems2 << graphicsItems[i];
00296                 }
00297                 else
00298                         graphicsItems2 = graphicsItems;
00299                 
00300                 
00301                 GraphicsScene * scene = createScene(graphicsItems2);
00302                 
00303                 if (scene->networkWindow)
00304                         scene->networkWindow->handle = item;
00305                 
00306                 return scene;
00307         }
00308 
00309         NetworkHandle::NetworkHandle(MainWindow * main) : QObject(main), mainWindow(main), symbolsTable(this)
00310         {               
00311                 if (main && !main->allNetworks.contains(this))
00312                         main->allNetworks << this;
00313 
00314                 connect(&history, SIGNAL(indexChanged(int)), this, SLOT(updateSymbolsTable(int)));
00315                 connect(this, SIGNAL(historyChanged(int)), mainWindow, SIGNAL(historyChanged(int)));
00316                 
00317                 connect(this,SIGNAL(parentHandleChanged(NetworkHandle *, const QList<ItemHandle*>&, const QList<ItemHandle*>&)),
00318                                 main ,SIGNAL(parentHandleChanged(NetworkHandle *, const QList<ItemHandle*>&, const QList<ItemHandle*>&)));
00319                         
00320                 connect(this,SIGNAL(handleFamilyChanged(NetworkHandle *, const QList<ItemHandle*>&, const QList<ItemFamily*>&)),
00321                                 main ,SIGNAL(handleFamilyChanged(NetworkHandle *, const QList<ItemHandle*>&, const QList<ItemFamily*>&)));
00322 
00323                 connect(this,SIGNAL(dataChanged(const QList<ItemHandle*>&)),
00324                         main ,SIGNAL(dataChanged(const QList<ItemHandle*>&)));
00325 
00326                 connect(this,SIGNAL(itemsRenamed(NetworkHandle*, const QList<ItemHandle*>&, const QList<QString>&, const QList<QString>&)),
00327                         main ,SIGNAL(itemsRenamed(NetworkHandle*, const QList<ItemHandle*>&, const QList<QString>&, const QList<QString>&)));
00328         }
00329 
00330         ItemHandle* NetworkHandle::globalHandle()
00331         {
00332                 return &(symbolsTable.globalHandle);
00333         }
00334 
00335         QList<ItemHandle*> NetworkHandle::handles(bool sort) const
00336         {
00337                 QList<ItemHandle*> handles;
00338 
00339                 if (sort)
00340                 {
00341                         QStringList names = symbolsTable.uniqueHandlesWithDot.keys();
00342                         names.sort();                   
00343                         for (int i=0; i < names.size(); ++i)
00344                                 handles += symbolsTable.uniqueHandlesWithDot.value( names[i] );
00345                 }
00346                 else
00347                 {
00348                         handles += symbolsTable.uniqueHandlesWithDot.values();
00349                 }
00350                 return handles;
00351         }
00352 
00353         QList<ItemHandle*> NetworkHandle::handlesSortedByFamily() const
00354         {
00355                 return symbolsTable.allHandlesSortedByFamily();
00356         }
00357         
00358         GraphicsScene * NetworkHandle::currentScene() const
00359         {
00360                 if (!mainWindow || !mainWindow->currentNetworkWindow || !networkWindows.contains(mainWindow->currentNetworkWindow))
00361                         return 0;
00362                 
00363                 return mainWindow->currentNetworkWindow->scene;
00364         }
00365         
00366         NetworkWindow * NetworkHandle::currentWindow() const
00367         {
00368                 if (!mainWindow || !mainWindow->currentNetworkWindow || !networkWindows.contains(mainWindow->currentNetworkWindow))
00369                         return 0;
00370                 
00371                 return mainWindow->currentNetworkWindow;
00372         }
00373 
00374         TextEditor * NetworkHandle::currentTextEditor() const
00375         {
00376                 if (!mainWindow || !mainWindow->currentNetworkWindow || !networkWindows.contains(mainWindow->currentNetworkWindow))
00377                         return 0;
00378                 
00379                 return mainWindow->currentNetworkWindow->editor;
00380         }
00381 
00382         QString NetworkHandle::rename(const QString& oldname, const QString& s)
00383         {
00384                 if (oldname == s) return s;
00385 
00386                 if (symbolsTable.uniqueHandlesWithDot.contains(oldname))
00387                 {
00388                         return rename(symbolsTable.uniqueHandlesWithDot[oldname],s);
00389                 }
00390 
00391                 if (symbolsTable.uniqueHandlesWithUnderscore.contains(oldname))
00392                 {
00393                         return rename(symbolsTable.uniqueHandlesWithDot[oldname],s);
00394                 }
00395 
00396                 if (symbolsTable.nonuniqueHandles.contains(oldname) && 
00397                         !symbolsTable.uniqueDataWithDot.contains(oldname) &&
00398                         !symbolsTable.uniqueDataWithUnderscore.contains(oldname))
00399                 {
00400                         return rename(symbolsTable.nonuniqueHandles[oldname],s);
00401                 }
00402 
00403                 QList<QString> oldNames, newNames;
00404                 oldNames += oldname;
00405 
00406                 QString newname = Tinkercell::RemoveDisallowedCharactersFromName(s);
00407 
00408                 newname = makeUnique(newname);
00409                 newNames += newname;
00410 
00411                 QUndoCommand * command = new RenameCommand(oldname + tr(" renamed to ") + newname,this,oldname,newname);
00412 
00413                 history.push(command);
00414         
00415                 QList<ItemHandle*> items = handles();
00416                 emit itemsRenamed(this, items, oldNames, newNames);
00417                 emit dataChanged(items);
00418 
00419                 return newname;
00420         }
00421 
00422         QString NetworkHandle::rename(ItemHandle* handle, const QString& s)
00423         {
00424                 if (!handle) return QString();
00425                 if (handle->fullName() == s) return s;
00426 
00427                 QList<ItemHandle*> items;
00428                 items += handle;
00429                 QList<QString> oldNames, newNames;
00430 
00431                 QString newname = s;
00432                 oldNames += handle->fullName();
00433 
00434                 if (handle->parent && !newname.contains(handle->parent->fullName()))
00435                         newname = handle->parent->fullName() + tr(".") + Tinkercell::RemoveDisallowedCharactersFromName(newname);
00436                 else
00437                         newname = Tinkercell::RemoveDisallowedCharactersFromName(newname);
00438 
00439                 newname = makeUnique(newname);
00440                 newNames += newname;
00441 
00442                 QUndoCommand * command = new RenameCommand(handle->fullName() + tr(" renamed to ") + newname,this,items,newNames);
00443 
00444                 history.push(command);
00445 
00446                 emit itemsRenamed(this, items, oldNames, newNames);
00447                 emit dataChanged(items);
00448 
00449                 return newname;
00450         }
00451 
00452         QStringList NetworkHandle::rename(const QList<ItemHandle*>& items, const QList<QString>& new_names)
00453         {
00454                 if (items.isEmpty() || items.size() > new_names.size()) return QStringList();
00455 
00456                 QList<QString> oldNames, newNames;
00457                 ItemHandle * handle;
00458                 QString newname;
00459 
00460                 for (int i=0; i < items.size(); ++i)
00461                         if ((handle = items[i]))
00462                         {
00463                                 newname = new_names[i];
00464                                 oldNames += handle->fullName();
00465 
00466                                 if (handle->parent && !newname.contains(handle->parent->fullName()))
00467                                         newname = handle->parent->fullName() + tr(".") + Tinkercell::RemoveDisallowedCharactersFromName(newname);
00468                                 else
00469                                         newname = Tinkercell::RemoveDisallowedCharactersFromName(newname);
00470 
00471                                 newname = makeUnique(newname);
00472                                 newNames += newname;
00473                         }
00474 
00475                 QUndoCommand * command = new RenameCommand(tr("items renamed"),this,items,newNames);
00476 
00477                 history.push(command);
00478 
00479                 emit itemsRenamed(this, items, oldNames, newNames);
00480                 emit dataChanged(items);
00481 
00482                 return newNames;
00483         }
00484 
00485         void NetworkHandle::setParentHandle(const QList<ItemHandle*>& handles, const QList<ItemHandle*>& parentHandles)
00486         {
00487                 if (handles.size() != parentHandles.size()) return;
00488 
00489                 SetParentHandleCommand * command = new SetParentHandleCommand(tr("parent(s) changed"), this, handles, parentHandles);
00490                 history.push(command);
00491 
00492                 emit parentHandleChanged(this, command->children, command->oldParents);
00493                 emit dataChanged(command->children);
00494         }
00495 
00496         void NetworkHandle::setParentHandle(ItemHandle * child, ItemHandle * parent)
00497         {
00498                 QList<ItemHandle*> children, parents;
00499                 children << child;
00500                 parents << parent;
00501                 setParentHandle(children,parents);
00502         }
00503 
00504         void NetworkHandle::setParentHandle(const QList<ItemHandle*> children, ItemHandle * parent)
00505         {
00506                 QList<ItemHandle*> parents;
00507                 for (int i=0; i < children.size(); ++i)
00508                         parents << parent;
00509                 setParentHandle(children,parents);
00510         }
00511         
00512         void NetworkHandle::setHandleFamily(const QList<ItemHandle*>& handles, const QList<ItemFamily*>& newfamilies)
00513         {
00514                 if (handles.size() != newfamilies.size()) return;
00515 
00516                 SetHandleFamilyCommand * command = new SetHandleFamilyCommand(tr("family changed"), handles, newfamilies);
00517                 history.push(command);
00518 
00519                 emit handleFamilyChanged(this, command->handles, command->oldFamily);
00520         }
00521 
00522         void NetworkHandle::setHandleFamily(ItemHandle * handle, ItemFamily * newfamily)
00523         {
00524                 setHandleFamily(QList<ItemHandle*>() << handle, QList<ItemFamily*>() << newfamily);
00525         }
00526 
00527         void NetworkHandle::setHandleFamily(const QList<ItemHandle*> handles, ItemFamily * newfamily)
00528         {
00529                 QList<ItemFamily*> list;
00530                 for (int i=0; i < handles.size(); ++i)
00531                         list << newfamily;
00532                 setHandleFamily(handles,list);
00533         }
00534 
00536         void NetworkHandle::changeData(const QString& name, ItemHandle* handle, const QString& hashstring, const NumericalDataTable* newdata)
00537         {
00538                 if (handle && handle->hasNumericalData(hashstring))
00539                 {
00540                         QUndoCommand * command = new ChangeDataCommand<qreal>(name,&(handle->numericalDataTable(hashstring)),newdata);
00541 
00542                         history.push(command);
00543 
00544                         QList<ItemHandle*> handles;
00545                         handles += handle;
00546                         emit dataChanged(handles);
00547                 }
00548         }
00550         void NetworkHandle::changeData(const QString& name, const QList<ItemHandle*>& handles, const QList<QString>& hashstrings, const QList<NumericalDataTable*>& newdata)
00551         {
00552                 QList<NumericalDataTable*> oldTables, newTables;
00553 
00554                 for (int i=0; i < handles.size() && i < hashstrings.size() && i < newdata.size(); ++i)
00555                 {
00556                         if (newdata[i] && handles[i] && handles[i]->hasNumericalData(hashstrings[i]))
00557                         {
00558                                 oldTables += &(handles[i]->numericalDataTable(hashstrings[i]));
00559                                 newTables += newdata[i];
00560                         }
00561                 }
00562 
00563                 if (oldTables.isEmpty() || newTables.isEmpty()) return;
00564 
00565 
00566                 QUndoCommand * command = new ChangeDataCommand<qreal>(name,oldTables,newTables);
00567 
00568                 history.push(command);
00569 
00570                 emit dataChanged(handles);
00571         }
00573         void NetworkHandle::changeData(const QString& name, const QList<ItemHandle*>& handles, const QString& hashstring, const QList<NumericalDataTable*>& newdata)
00574         {
00575                 QList<NumericalDataTable*> oldTables, newTables;
00576 
00577                 for (int i=0; i < handles.size() && i < newdata.size(); ++i)
00578                 {
00579                         if (newdata[i] && handles[i] && handles[i]->hasNumericalData(hashstring))
00580                         {
00581                                 oldTables += &(handles[i]->numericalDataTable( hashstring ));
00582                                 newTables += newdata[i];
00583                         }
00584                 }
00585 
00586                 if (oldTables.isEmpty() || newTables.isEmpty()) return;
00587 
00588 
00589                 QUndoCommand * command = new ChangeDataCommand<qreal>(name,oldTables,newTables);
00590 
00591                 history.push(command);
00592 
00593                 emit dataChanged(handles);
00594         }
00596         void NetworkHandle::changeData(const QString& name, ItemHandle* handle, const QString& hashstring, const TextDataTable* newdata)
00597         {
00598                 if (handle && handle->hasTextData(hashstring))
00599                 {
00600                         QUndoCommand * command = new ChangeDataCommand<QString>(name,&(handle->textDataTable(hashstring)),newdata);
00601                         history.push(command);
00602 
00603                         QList<ItemHandle*> handles;
00604                         handles += handle;
00605                         emit dataChanged(handles);
00606                 }
00607         }
00609         void NetworkHandle::changeData(const QString& name, const QList<ItemHandle*>& handles, const QList<QString>& hashstrings, const QList<TextDataTable*>& newdata)
00610         {
00611                 QList<TextDataTable*> oldTables, newTables;
00612 
00613                 for (int i=0; i < handles.size() && i < hashstrings.size() && i < newdata.size(); ++i)
00614                 {
00615                         if (newdata[i] && handles[i] && handles[i]->hasTextData(hashstrings[i]))
00616                         {
00617                                 oldTables += &(handles[i]->textDataTable( hashstrings[i] ));
00618                                 newTables += newdata[i];
00619                         }
00620                 }
00621 
00622                 if (oldTables.isEmpty() || newTables.isEmpty()) return;
00623 
00624 
00625                 QUndoCommand * command = new ChangeDataCommand<QString>(name,oldTables,newTables);
00626 
00627                 history.push(command);
00628 
00629                 emit dataChanged(handles);
00630         }
00632         void NetworkHandle::changeData(const QString& name, const QList<ItemHandle*>& handles, const QString& hashstring, const QList<TextDataTable*>& newdata)
00633         {
00634                 QList<TextDataTable*> oldTables, newTables;
00635 
00636                 for (int i=0; i < handles.size() && i < newdata.size(); ++i)
00637                 {
00638                         if (newdata[i] && handles[i] && handles[i]->hasTextData(hashstring))
00639                         {
00640                                 oldTables += &(handles[i]->textDataTable( hashstring ));
00641                                 newTables += newdata[i];
00642                         }
00643                 }
00644 
00645                 if (oldTables.isEmpty() || newTables.isEmpty()) return;
00646 
00647 
00648                 QUndoCommand * command = new ChangeDataCommand<QString>(name,oldTables,newTables);
00649 
00650                 history.push(command);
00651 
00652                 emit dataChanged(handles);
00653         }
00655         void NetworkHandle::changeData(const QString& name, ItemHandle* handle, const QString& hashstring, const NumericalDataTable* newdata1, const TextDataTable* newdata2)
00656         {
00657                 if (handle && handle->hasNumericalData(hashstring) && handle->hasTextData(hashstring))
00658                 {
00659                         QUndoCommand * command = new Change2DataCommand<qreal,QString>(name,&(handle->numericalDataTable(hashstring)), newdata1, &(handle->textDataTable(hashstring)),newdata2);
00660 
00661                         history.push(command);
00662 
00663                         QList<ItemHandle*> handles;
00664                         handles += handle;
00665                         emit dataChanged(handles);
00666                 }
00667         }
00669         void NetworkHandle::changeData(const QString& name, const QList<ItemHandle*>& handles, const QList<QString>& hashstrings, const QList<NumericalDataTable*>& newdata1, const QList<TextDataTable*>& newdata2)
00670         {
00671                 QList<TextDataTable*> oldTablesS, newTablesS;
00672                 QList<NumericalDataTable*> oldTablesN, newTablesN;
00673 
00674                 int j = 0;
00675                 for (int i=0; j < handles.size() && j < hashstrings.size() && i < newdata1.size(); ++i, ++j)
00676                 {
00677                         if (newdata1[i] && handles[j] && handles[j]->hasNumericalData(hashstrings[j]))
00678                         {
00679                                 oldTablesN += &(handles[j]->numericalDataTable( hashstrings[j] ));
00680                                 newTablesN += newdata1[i];
00681                         }
00682                 }
00683 
00684                 for (int i=0; j < handles.size() && j < hashstrings.size() && i < newdata2.size(); ++i, ++j)
00685                 {
00686                         if (newdata2[i] && handles[j] && handles[j]->hasTextData(hashstrings[j]))
00687                         {
00688                                 oldTablesS += &(handles[j]->textDataTable( hashstrings[j] ));
00689                                 newTablesS += newdata2[i];
00690                         }
00691                 }
00692 
00693                 if ((oldTablesS.isEmpty() || newTablesS.isEmpty()) &&
00694                         (oldTablesN.isEmpty() || newTablesN.isEmpty())) return;
00695 
00696                 QUndoCommand * command = new Change2DataCommand<qreal,QString>(name,oldTablesN,newTablesN,oldTablesS,newTablesS);
00697 
00698                 history.push(command);
00699 
00700                 emit dataChanged(handles);
00701         }
00702 
00704         void NetworkHandle::changeData(const QString& name, const QList<ItemHandle*>& handles, const QString& hashstring, const QList<NumericalDataTable*>& newdata1, const QList<TextDataTable*>& newdata2)
00705         {
00706                 QList<TextDataTable*> oldTablesS, newTablesS;
00707 
00708                 for (int i=0; i < handles.size() && i < newdata2.size(); ++i)
00709                 {
00710                         if (newdata2[i] && handles[i] && handles[i]->hasTextData(hashstring))
00711                         {
00712                                 oldTablesS += &(handles[i]->textDataTable( hashstring ));
00713                                 newTablesS += newdata2[i];
00714                         }
00715                 }
00716 
00717                 QList<NumericalDataTable*> oldTablesN, newTablesN;
00718 
00719                 for (int i=0; i < handles.size()  && i < newdata1.size(); ++i)
00720                 {
00721                         if (newdata1[i] && handles[i] && handles[i]->hasNumericalData(hashstring))
00722                         {
00723                                 oldTablesN += &(handles[i]->numericalDataTable( hashstring ));
00724                                 newTablesN += newdata1[i];
00725                         }
00726                 }
00727 
00728                 if ((oldTablesS.isEmpty() || newTablesS.isEmpty()) &&
00729                         (oldTablesN.isEmpty() || newTablesN.isEmpty())) return;
00730 
00731 
00732                 QUndoCommand * command = new Change2DataCommand<qreal,QString>(name,oldTablesN,newTablesN,oldTablesS,newTablesS);
00733 
00734                 history.push(command);
00735 
00736                 emit dataChanged(handles);
00737         }
00738 
00740         void NetworkHandle::changeData(const QString& name, const QList<ItemHandle*>& handles, const QList<NumericalDataTable*>& olddata1, const QList<NumericalDataTable*>& newdata1, const QList<TextDataTable*>& olddata2, const QList<TextDataTable*>& newdata2)
00741         {
00742                 if ((olddata1.isEmpty() || newdata1.isEmpty()) &&
00743                         (olddata2.isEmpty() || newdata2.isEmpty())) return;
00744 
00745                 QUndoCommand * command = new Change2DataCommand<qreal,QString>(name,olddata1,newdata1,olddata2,newdata2);
00746 
00747                 history.push(command);
00748 
00749                 emit dataChanged(handles);
00750         }
00751 
00753         void NetworkHandle::changeData(const QString& name, const QList<ItemHandle*>& handles, const QList<NumericalDataTable*>& olddata1, const QList<NumericalDataTable*>& newdata1)
00754         {
00755                 if (olddata1.isEmpty() || newdata1.isEmpty()) return;
00756 
00757                 QUndoCommand * command = new ChangeDataCommand<qreal>(name,olddata1,newdata1);
00758 
00759                 history.push(command);
00760 
00761                 emit dataChanged(handles);
00762         }
00763         
00765         void NetworkHandle::changeData(const QString& name, const QList<ItemHandle*>& handles, const QList<TextDataTable*>& olddata1, const QList<TextDataTable*>& newdata1)
00766         {
00767                 if (olddata1.isEmpty() || newdata1.isEmpty()) return;
00768 
00769                 QUndoCommand * command = new ChangeDataCommand<QString>(name,olddata1,newdata1);
00770 
00771                 history.push(command);
00772 
00773                 emit dataChanged(handles);
00774         }
00775 
00777         void NetworkHandle::changeData(const QString& name, const QList<ItemHandle*>& handles, NumericalDataTable* olddata1, const NumericalDataTable* newdata1, TextDataTable* olddata2, const TextDataTable* newdata2)
00778         {
00779                 if ((!olddata1 || !newdata1) &&
00780                         (!olddata2 || !newdata2)) return;
00781 
00782                 QUndoCommand * command = new Change2DataCommand<qreal,QString>(name,olddata1,newdata1,olddata2,newdata2);
00783 
00784                 history.push(command);
00785 
00786                 emit dataChanged(handles);
00787         }
00788 
00790         void NetworkHandle::changeData(const QString& name, const QList<ItemHandle*>& handles, NumericalDataTable* olddata1, const NumericalDataTable* newdata1)
00791         {
00792                 if (!olddata1 || !newdata1) return;
00793 
00794                 QUndoCommand * command = new ChangeDataCommand<qreal>(name,olddata1,newdata1);
00795 
00796                 history.push(command);
00797 
00798                 emit dataChanged(handles);
00799         }
00800 
00802         void NetworkHandle::changeData(const QString& name, const QList<ItemHandle*>& handles, TextDataTable* olddata1, const TextDataTable* newdata1)
00803         {
00804                 if (!olddata1 || !newdata1) return;
00805 
00806                 QUndoCommand * command = new ChangeDataCommand<QString>(name,olddata1,newdata1);
00807 
00808                 history.push(command);
00809 
00810                 emit dataChanged(handles);
00811         }
00812 
00814         void NetworkHandle::updateSymbolsTable()
00815         {
00816                 symbolsTable.update(0);         
00817         }
00818 
00820         void NetworkHandle::updateSymbolsTable(int i)
00821         {
00822                 symbolsTable.update(i);
00823         }
00824 
00825         static double d = 1.0;
00826         static double* AddVariable(const char*, void*)
00827         {
00828                 return &d;
00829         }
00830 
00831         bool NetworkHandle::parseMath(QString& s, QStringList& newvars, QStringList& existingVars,QList<ItemHandle*>& usedHandles)
00832         {
00833                 static QStringList reservedWords;
00834                 if (reservedWords.isEmpty())
00835                         reservedWords << "time" << "TIME" << "Time";
00836 
00837                 bool ok;
00838                 double d = s.toDouble(&ok);
00839                 if (ok) return true;
00840 
00841                 mu::Parser parser;
00842 
00843                 s.replace(QRegExp(tr("\\.(?!\\d)")),tr("__Q_X_Z_W__"));
00844                 parser.SetExpr(s.toAscii().data());
00845                 s.replace(tr("__Q_X_Z_W__"),tr("."));
00846                 parser.SetVarFactory(AddVariable, 0);
00847                 QString str;
00848 
00849                 try
00850                 {
00851                         parser.Eval();
00852 
00853                         // Get the map with the variables
00854                         mu::varmap_type variables = parser.GetVar();
00855 
00856                         // Get the number of variables
00857                         mu::varmap_type::const_iterator item = variables.begin();
00858 
00859                         // Query the variables
00860                         for (; item!=variables.end(); ++item)
00861                         {
00862                                 str = tr(item->first.data());
00863                                 str.replace(QRegExp(tr("[^A-Za-z0-9_]")),tr(""));
00864                                 str.replace(tr("__Q_X_Z_W__"),tr("."));
00865                                 QString str2 = str;
00866                                 str2.replace(tr("."),tr("_"));
00867                                 if (!reservedWords.contains(str))
00868                                 {
00869                                         if (symbolsTable.uniqueHandlesWithDot.contains(str))
00870                                         {
00871                                                 existingVars << str;
00872                                                 usedHandles << symbolsTable.uniqueHandlesWithDot[str];
00873                                         }
00874                                         else
00875                                         if (symbolsTable.uniqueHandlesWithUnderscore.contains(str2))
00876                                         {
00877                                                 existingVars << str2;
00878                                                 usedHandles << symbolsTable.uniqueHandlesWithUnderscore[str2];
00879                                         }
00880                                         else
00881                                         {
00882                                                 if (symbolsTable.uniqueDataWithDot.contains(str) && symbolsTable.uniqueDataWithDot[str].first)
00883                                                 {
00884                                                         usedHandles << symbolsTable.uniqueDataWithDot[str].first;
00885                                                         existingVars << str;
00886                                                 }
00887                                                 else
00888                                                 if (symbolsTable.uniqueDataWithUnderscore.contains(str2) && symbolsTable.uniqueDataWithUnderscore[str2].first)
00889                                                 {
00890                                                         usedHandles << symbolsTable.uniqueDataWithUnderscore[str2].first;
00891                                                         existingVars << str;
00892                                                 }
00893                                                 else
00894                                                 if (symbolsTable.uniqueDataWithDot.contains(str) && symbolsTable.uniqueDataWithDot[str].first)
00895                                                 {
00896                                                         existingVars << str;
00897                                                         usedHandles << symbolsTable.uniqueDataWithDot[str].first;
00898                                                 }
00899                                                 else
00900                                                 if (symbolsTable.nonuniqueData.contains(str) && symbolsTable.nonuniqueData[str].first)
00901                                                 {
00902                                                         ItemHandle * handle = symbolsTable.nonuniqueData[str].first;
00903                                                         if (handle != globalHandle() && !str.contains(QRegExp(tr("^")+handle->fullName())))
00904                                                         {
00905                                                                 s.replace(QRegExp(tr("^")+str+tr("([^a-zA-Z0-9_])")),handle->fullName() + tr(".") + str + tr("\\1"));
00906                                                                 s.replace(QRegExp(tr("([^a-zA-Z0-9_\\.])")+str+tr("([^a-zA-Z0-9_])")), tr("\\1") + handle->fullName() + tr(".") + str + tr("\\2"));
00907                                                                 s.replace(QRegExp(tr("([^a-zA-Z0-9_\\.])")+str+tr("$")),tr("\\1") + handle->fullName() + tr(".")  + str);
00908                                                                 usedHandles << handle;
00909                                                         }
00910                                                 }
00911                                                 else
00912                                                 {
00913                                                         if (symbolsTable.nonuniqueHandles.contains(str))
00914                                                         {
00915                                                                 ItemHandle * handle = symbolsTable.nonuniqueHandles[str];
00916                                                                 if (handle && handle != globalHandle())
00917                                                                 {
00918                                                                         s.replace(QRegExp(tr("^")+str+tr("([^a-zA-Z0-9_])")),handle->fullName() + tr("\\1"));
00919                                                                         s.replace(QRegExp(tr("([^a-zA-Z0-9_])")+str+tr("([^a-zA-Z0-9_])")), tr("\\1") + handle->fullName() + tr("\\2"));
00920                                                                         s.replace(QRegExp(tr("([^a-zA-Z0-9_])")+str+tr("$")),tr("\\1") + handle->fullName());
00921                                                                         usedHandles << handle;
00922                                                                 }
00923                                                         }
00924                                                         else
00925                                                         if (symbolsTable.nonuniqueHandles.contains(str2) && symbolsTable.nonuniqueHandles[str2])
00926                                                         {
00927                                                                 ItemHandle * handle = symbolsTable.nonuniqueHandles[str2];
00928                                                                 if (handle && handle != globalHandle())
00929                                                                 {
00930                                                                         s.replace(QRegExp(tr("^")+str+tr("([^a-zA-Z0-9_])")), handle->fullName() + tr("\\1"));
00931                                                                         s.replace(QRegExp(tr("([^a-zA-Z0-9_])")+str+tr("([^a-zA-Z0-9_])")), tr("\\1") + handle->fullName() + tr("\\2"));
00932                                                                         s.replace(QRegExp(tr("([^a-zA-Z0-9_])")+str+tr("$")),tr("\\1") + handle->fullName());
00933                                                                         usedHandles << handle;
00934                                                                 }
00935                                                         }
00936                                                         else
00937                                                         {
00938                                                                 newvars << str;
00939                                                         }
00940                                                 }
00941                                         }
00942                                 }
00943                         }
00944                 }
00945                 catch(mu::Parser::exception_type &)
00946                 {
00947                         return false;
00948                 }
00949                 return true;
00950         }
00951         
00952         void NetworkHandle::assignHandles(const QList<QGraphicsItem*>& items, ItemHandle* newHandle)
00953         {
00954                 if (!newHandle) return;
00955                 QList<ItemHandle*> handles;
00956                 for (int i=0; i < items.size(); ++i)
00957                         handles += getHandle(items[i]);
00958 
00959                 QUndoCommand * command = new AssignHandleCommand(tr("item defined"),items,newHandle);
00960 
00961                 history.push(command);
00962 
00963                 emit handlesChanged(this, items, handles);
00964         }
00965         
00966         void NetworkHandle::mergeHandles(const QList<ItemHandle*>& handles)
00967         {
00968                 if (handles.isEmpty()) return;
00969 
00970                 MergeHandlesCommand * command = new MergeHandlesCommand(tr("items merged"),this, handles);
00971 
00972                 if (!command->newHandle)
00973                 {
00974                         delete command;
00975                         return;
00976                 }
00977 
00978                 history.push(command);
00979 
00980                 QList<QGraphicsItem*> items;
00981                 for (int i=0; i < handles.size(); ++i)
00982                         if (handles[i])
00983                                 items << handles[i]->allGraphicsItems();
00984         
00985                 emit handlesChanged(this, items, handles);
00986         }
00987         
00988         void NetworkHandle::undo()
00989         {
00990                 history.undo();
00991         }
00992         
00993         void NetworkHandle::redo()
00994         {
00995                 history.redo();
00996         }
00997         
00998         void NetworkHandle::push(QUndoCommand * cmd)
00999         {
01000                 if (cmd)
01001                         history.push(cmd);
01002         }
01003         
01004         QString NetworkHandle::windowTitle() const
01005         {
01006                 QString s;
01007                 for (int i=0; i < networkWindows.size(); ++i)
01008                         if (networkWindows[i])
01009                         {
01010                                 s = networkWindows[i]->windowTitle();
01011                                 if (mainWindow && networkWindows[i] == mainWindow->currentNetworkWindow)
01012                                         break;
01013                         }
01014                 return s;
01015         }
01016         
01017         void NetworkHandle::remove(const QString& name, const QList<QGraphicsItem*>& items)
01018         {
01019                 if (items.isEmpty()) return;
01020                 
01021                 QHash< GraphicsScene*, QList<QGraphicsItem*> > hash;
01022                 GraphicsScene * scene;
01023                 
01024                 for (int i=0; i < items.size(); ++i)
01025                         if (items[i] && items[i]->scene())
01026                         {
01027                                 scene = static_cast<GraphicsScene*>(items[i]->scene());
01028                                 if (!hash.contains(scene))
01029                                         hash[scene] = QList<QGraphicsItem*>();
01030                                 hash[scene] += items[i];                                
01031                         }
01032                 
01033                 QList<GraphicsScene*> scenes = hash.keys();
01034                 for (int i=0; i < scenes.size(); ++i)
01035                 {
01036                         scenes[i]->remove(name,hash[ scenes[i] ]);
01037                 }
01038         }
01039         
01040         void NetworkHandle::remove(const QString& name, const QList<ItemHandle*>& items)
01041         {
01042                 if (items.isEmpty()) return;
01043 
01044                 QList<QGraphicsItem*> gitems;
01045                 for (int i=0; i < items.size(); ++i)
01046                         if (items[i])
01047                                 gitems += items[i]->graphicsItems;
01048                 
01049                 if (!gitems.isEmpty())
01050                 {
01051                         remove(name,gitems);
01052                         return;
01053                 }
01054                 
01055                 QHash< TextEditor*, QList<ItemHandle*> > hash;
01056                 QList<TextEditor*> editors = this->editors();
01057                 
01058                 for (int i=0; i < items.size(); ++i)
01059                         for (int j=0; j < editors.size(); ++j)
01060                                 if (items[i] && editors[j] && editors[j]->allItems.contains(items[i]))
01061                                 {
01062                                         if (!hash.contains(editors[j]))
01063                                                 hash[ editors[j] ] = QList<ItemHandle*>();
01064                                         hash[ editors[j] ] += items[i];                         
01065                                 }
01066                 
01067                 editors = hash.keys();
01068                 for (int i=0; i < editors.size(); ++i)
01069                 {
01070                         editors[i]->remove(hash[ editors[i] ]);
01071                 }
01072         }
01073         
01074         ConsoleWindow * NetworkHandle::console() const
01075         {
01076                 if (mainWindow)
01077                         return mainWindow->console();
01078                 return 0;
01079         }
01080         
01081         QString NetworkHandle::makeUnique(const QString& str, const QStringList& doNotUse) const
01082         {
01083                 QString name = str;
01084 
01085                 int k = name.length();
01086                 while (k > 0 && name[k-1].isNumber())
01087                         --k;
01088                 if (k < name.length())
01089                         name = name.left(k);
01090 
01091                 int c = 1;
01092                 QString str2 = name;
01093                 
01094                 bool taken = symbolsTable.uniqueHandlesWithDot.contains(str) || 
01095                                          symbolsTable.uniqueHandlesWithUnderscore.contains(str) || 
01096                                          symbolsTable.uniqueDataWithDot.contains(str) ||
01097                                          symbolsTable.uniqueDataWithUnderscore.contains(str) ||  
01098                                          doNotUse.contains(str);
01099                 if (!taken) return str;
01100                 
01101                 while (taken)
01102                 {
01103                         str2 = name + QString::number(c);
01104                         taken = symbolsTable.uniqueHandlesWithDot.contains(str2) || 
01105                                          symbolsTable.uniqueHandlesWithUnderscore.contains(str2) || 
01106                                          symbolsTable.uniqueDataWithDot.contains(str2) ||
01107                                          symbolsTable.uniqueDataWithUnderscore.contains(str2) ||  
01108                                          doNotUse.contains(str2);
01109                         ++c;
01110                 }
01111                 return str2;
01112         }
01113         
01114         QStringList NetworkHandle::makeUnique(const QStringList& oldnames, const QStringList& doNotUse) const
01115         {
01116                 QStringList newnames;
01117                 bool taken = true;
01118                 int c,k;
01119                 QString str2;
01120                 
01121                 for (int i=0; i < oldnames.size(); ++i)
01122                 {
01123                         QString name = oldnames[i];
01124                         taken = symbolsTable.uniqueHandlesWithDot.contains(name) || 
01125                                          symbolsTable.uniqueHandlesWithUnderscore.contains(name) || 
01126                                          symbolsTable.uniqueDataWithDot.contains(name) ||
01127                                          symbolsTable.uniqueDataWithUnderscore.contains(name) ||  
01128                                          doNotUse.contains(name);
01129                         if (taken)
01130                         {       
01131                                 k = name.length();
01132                                 while (k > 0 && name[k-1].isNumber())
01133                                         --k;
01134                                 if (k < name.length())
01135                                         name = name.left(k);
01136                                 c = 1;
01137                                 str2 = name;
01138                         
01139                                 while (taken)
01140                                 {
01141                                         str2 = name + QString::number(c);
01142                                         taken = symbolsTable.uniqueHandlesWithDot.contains(str2) || 
01143                                                          symbolsTable.uniqueHandlesWithUnderscore.contains(str2) || 
01144                                                          symbolsTable.uniqueDataWithDot.contains(str2) ||
01145                                                          symbolsTable.uniqueDataWithUnderscore.contains(str2) ||  
01146                                                          doNotUse.contains(str2);
01147                                         ++c;
01148                                 }
01149                                 newnames += str2;
01150                         }
01151                         else
01152                          newnames += name;
01153                 }
01154                 
01155                 return newnames;
01156         }
01157         
01158         QString NetworkHandle::makeUnique(ItemHandle * handle, const QStringList& doNotUse) const
01159         {
01160                 if (!handle) return QString();
01161 
01162                 QString saveName = handle->name;
01163                 QString name = handle->name;
01164 
01165                 int k = name.length();
01166                 while (k > 0 && name[k-1].isNumber())
01167                         --k;
01168                 if (k < name.length())
01169                         name = name.left(k);
01170 
01171                 int c = 1;
01172                 QString str = handle->fullName();
01173                 
01174                 bool taken = (symbolsTable.uniqueHandlesWithDot.contains(str) && symbolsTable.uniqueHandlesWithDot[str] != handle) || 
01175                                          (symbolsTable.uniqueHandlesWithUnderscore.contains(str) && symbolsTable.uniqueHandlesWithUnderscore[str] != handle) || 
01176                                          (symbolsTable.uniqueDataWithDot.contains(str) && symbolsTable.uniqueDataWithDot[str].first != handle) ||
01177                                          (symbolsTable.uniqueDataWithUnderscore.contains(str) && symbolsTable.uniqueDataWithUnderscore[str].first != handle) ||  
01178                                          doNotUse.contains(str);
01179                 if (!taken) return str;
01180                 
01181                 while (taken)
01182                 {                       
01183                         handle->name = name + QString::number(c);
01184                         str = handle->fullName();
01185                         taken = (symbolsTable.uniqueHandlesWithDot.contains(str) && symbolsTable.uniqueHandlesWithDot[str] != handle) || 
01186                                          (symbolsTable.uniqueHandlesWithUnderscore.contains(str) && symbolsTable.uniqueHandlesWithUnderscore[str] != handle) || 
01187                                          (symbolsTable.uniqueDataWithDot.contains(str) && symbolsTable.uniqueDataWithDot[str].first != handle) ||
01188                                          (symbolsTable.uniqueDataWithUnderscore.contains(str) && symbolsTable.uniqueDataWithUnderscore[str].first != handle) ||  
01189                                          doNotUse.contains(str);
01190                         ++c;
01191                 }
01192                 str = handle->fullName();
01193                 handle->name = saveName;
01194                 return str;
01195         }
01196         
01197         void NetworkHandle::emitHistoryChanged(int i)
01198         {
01199                 emit historyChanged(i);
01200         }
01201         
01202         QString NetworkHandle::annotations() const
01203         {
01204                 QString s;
01205                 TextGraphicsItem * textItem = 0;
01206                 QList<GraphicsScene*> scenes = this->scenes();
01207                 for (int i=0; i < scenes.size(); ++i)
01208                 {
01209                         QList<QGraphicsItem*> items = scenes[i]->items();
01210                         for (int j=0; j < items.size(); ++j)
01211                                 if ((textItem = TextGraphicsItem::cast(items[j])) && !textItem->handle())
01212                                         s += textItem->text() + tr("\n");
01213                 }
01214                 return s;
01215         }
01216         
01217         void NetworkHandle::setModelValues(const QStringList& names, const QList<double>& values, int column, const QString& defaultDataTable)
01218         {
01219                 NumericalDataTable dat;
01220                 dat.resize(names.size(), 1);
01221                 for (int i=0; i < names.size() && i < values.size() && i < dat.rows(); ++i)
01222                 {
01223                         dat.setRowName(i, names[i]);
01224                         dat(i,0) = values[i];
01225                 }
01226                 setModelValues(dat,defaultDataTable);
01227         }
01228 
01229         void NetworkHandle::setModelValues(const QStringList& names, const QStringList& values, int column, const QString& defaultDataTable)
01230         {
01231                 TextDataTable dat;
01232                 dat.resize(names.size(), 1);
01233                 for (int i=0; i < names.size() && i < values.size() && i < dat.rows(); ++i)
01234                 {
01235                         dat.setRowName(i, names[i]);
01236                         dat(i,0) = values[i];
01237                 }
01238                 setModelValues(dat,defaultDataTable);
01239         }
01240 
01241         void NetworkHandle::setModelValues(const NumericalDataTable& newvalues, const QString& defaultDataTable)
01242         {
01243                 SymbolsTable & symbols = symbolsTable;
01244                 QString s;
01245                 QList<NumericalDataTable*> newTables, oldTables;
01246                 NumericalDataTable * newTable, * oldTable;
01247                 QPair<ItemHandle*,QString> pair;
01248                 int k;
01249                 for (int i=0; i < newvalues.rows(); ++i)
01250                         {
01251                                 s = newvalues.rowName(i);
01252 
01253                                 if (symbols.uniqueDataWithDot.contains(s))
01254                                 {
01255                                         pair = symbols.uniqueDataWithDot.value(s);
01256                                         if (pair.first && pair.first->hasNumericalData(pair.second))
01257                                         {
01258                                                 oldTable = &(pair.first->numericalDataTable(pair.second));
01259                                                 s.remove(pair.first->fullName() + tr("."));
01260                                                 if (oldTable->hasRow(s))
01261                                                 {
01262                                                         k = oldTables.indexOf(oldTable);
01263                                                         if (k > -1)
01264                                                                 newTable = newTables[k];
01265                                                         else
01266                                                         {
01267                                                                 newTable = new NumericalDataTable(*oldTable);
01268                                                                 oldTables << oldTable;
01269                                                                 newTables << newTable;
01270                                                         }
01271                                                         for (int j=0; j < newTable->columns() && j < newvalues.columns(); ++j)
01272                                                                 newTable->value(s,j) = newvalues(i,j);
01273                                                 }
01274                                         }
01275                                 }
01276                                 else
01277                                 {
01278                                         s.replace(tr("."),tr("_"));
01279                                         if (symbols.uniqueDataWithUnderscore.contains(s))
01280                                         {
01281                                                 pair = symbols.uniqueDataWithUnderscore.value(s);
01282                                                 if (pair.first && pair.first->hasNumericalData(pair.second))
01283                                                 {
01284                                                         oldTable = &(pair.first->numericalDataTable(pair.second));
01285                                                         s.remove(pair.first->fullName(tr("_")) + tr("_"));
01286                                                         if (oldTable->hasRow(s))
01287                                                         {
01288                                                                 k = oldTables.indexOf(oldTable);
01289                                                                 if (k > -1)                                                     
01290                                                                         newTable = newTables[k];
01291                                                                 else
01292                                                                 {
01293                                                                         newTable = new NumericalDataTable(*oldTable);
01294                                                                         oldTables << oldTable;
01295                                                                         newTables << newTable;
01296                                                                 }
01297                                                                 for (int j=0; j < newTable->columns() && j < newvalues.columns(); ++j)
01298                                                                         newTable->value(s,j) = newvalues(i,j);
01299                                                         }
01300                                                 }
01301                                         }
01302                                         else
01303                                         if (!defaultDataTable.isNull() &&
01304                                                         !defaultDataTable.isEmpty() &&  
01305                                                         symbols.uniqueHandlesWithDot.contains(s))
01306                                         {
01307                                                 ItemHandle * h = symbols.uniqueHandlesWithDot[s];
01308                                                 if (h->hasNumericalData(defaultDataTable))
01309                                                 {
01310                                                         oldTable = &(h->numericalDataTable(defaultDataTable));
01311                                                         {
01312                                                                 k = oldTables.indexOf(oldTable);
01313                                                                 if (k > -1)                                                     
01314                                                                         newTable = newTables[k];
01315                                                                 else
01316                                                                 {
01317                                                                         newTable = new NumericalDataTable(*oldTable);
01318                                                                         oldTables << oldTable;
01319                                                                         newTables << newTable;
01320                                                                 }
01321                                                                 for (int j=0; j < newTable->columns() && j < newvalues.columns(); ++j)
01322                                                                         newTable->value(0,j) = newvalues(i,j);
01323                                                         }
01324                                                 }
01325                                         }
01326                                 }
01327                 }
01328                 
01329                 if (!newTables.isEmpty())
01330                 {
01331                         push(new ChangeNumericalDataCommand(tr("Updated model values"), oldTables, newTables));
01332                         for (int i=0; i < newTables.size(); ++i)
01333                                 delete newTables[i];
01334                 }
01335         }
01336         
01337         void NetworkHandle::setModelValues(const TextDataTable& newvalues, const QString& defaultDataTable)
01338         {
01339                 SymbolsTable & symbols = symbolsTable;
01340                 QString s;
01341                 QList<TextDataTable*> newTables, oldTables;
01342                 TextDataTable * newTable, * oldTable;
01343                 QPair<ItemHandle*,QString> pair;
01344                 int k;
01345                 for (int i=0; i < newvalues.rows(); ++i)
01346                         {
01347                                 s = newvalues.rowName(i);
01348 
01349                                 if (symbols.uniqueDataWithDot.contains(s))
01350                                 {
01351                                         pair = symbols.uniqueDataWithDot.value(s);
01352                                         if (pair.first && pair.first->hasTextData(pair.second))
01353                                         {
01354                                                 oldTable = &(pair.first->textDataTable(pair.second));
01355                                                 s.remove(pair.first->fullName() + tr("."));
01356                                                 if (oldTable->hasRow(s))
01357                                                 {
01358                                                         k = oldTables.indexOf(oldTable);
01359                                                         if (k > -1)
01360                                                                 newTable = newTables[k];
01361                                                         else
01362                                                         {
01363                                                                 newTable = new TextDataTable(*oldTable);
01364                                                                 oldTables << oldTable;
01365                                                                 newTables << newTable;
01366                                                         }
01367                                                         for (int j=0; j < newTable->columns() && j < newvalues.columns(); ++j)
01368                                                                 newTable->value(s,j) = newvalues(i,j);
01369                                                 }
01370                                         }
01371                                 }
01372                                 else
01373                                 {
01374                                         s.replace(tr("."),tr("_"));
01375                                         if (symbols.uniqueDataWithUnderscore.contains(s))
01376                                         {
01377                                                 pair = symbols.uniqueDataWithUnderscore.value(s);
01378                                                 if (pair.first && pair.first->hasTextData(pair.second))
01379                                                 {
01380                                                         oldTable = &(pair.first->textDataTable(pair.second));
01381                                                         s.remove(pair.first->fullName(tr("_")) + tr("_"));
01382                                                         k = oldTables.indexOf(oldTable);
01383                                                         if (k > -1)                                                     
01384                                                                 newTable = newTables[k];
01385                                                         else
01386                                                         {
01387                                                                 newTable = new TextDataTable(*oldTable);
01388                                                                 oldTables << oldTable;
01389                                                                 newTables << newTable;
01390                                                         }
01391                                                         for (int j=0; j < newTable->columns() && j < newvalues.columns(); ++j)
01392                                                                 newTable->value(s,j) = newvalues(i,j);                                          
01393                                                 }
01394                                         }
01395                                         else
01396                                         if (!defaultDataTable.isNull() &&
01397                                                         !defaultDataTable.isEmpty() &&  
01398                                                         symbols.uniqueHandlesWithDot.contains(s))
01399                                         {
01400                                                 ItemHandle * h = symbols.uniqueHandlesWithDot[s];
01401                                                 if (h->hasTextData(defaultDataTable))
01402                                                 {
01403                                                         oldTable = &(h->textDataTable(defaultDataTable));
01404                                                         k = oldTables.indexOf(oldTable);
01405                                                         if (k > -1)                                                     
01406                                                                 newTable = newTables[k];
01407                                                         else
01408                                                         {
01409                                                                 newTable = new TextDataTable(*oldTable);
01410                                                                 oldTables << oldTable;
01411                                                                 newTables << newTable;
01412                                                         }
01413                                                         for (int j=0; j < newTable->columns() && j < newvalues.columns(); ++j)
01414                                                                 newTable->value(0,j) = newvalues(i,j);
01415                                                 }
01416                                         }
01417                                 }
01418                 }
01419                 
01420                 if (!newTables.isEmpty())
01421                 {
01422                         push(new ChangeTextDataCommand(tr("Updated model values"), oldTables, newTables));
01423                         for (int i=0; i < newTables.size(); ++i)
01424                                 delete newTables[i];
01425                 }
01426         }
01427 }
01428 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines