Logo Search packages:      
Sourcecode: ktechlab version File versions  Download package

void ItemDocumentData::mergeWithDocument ( ItemDocument itemDocument,
bool  selectNew 
)

Merges the stuff stored here with the given document. If this is being used for e.g. pasting, you should call generateUniqueIDs()

Parameters:
selectNew if true then the newly created items & connectors will be selected

Definition at line 954 of file itemdocumentdata.cpp.

References Node::addInputConnector(), Node::addOutputConnector(), CNItem::childNode(), ICNDocument::cnItemWithID(), ICNDocument::connectorWithID(), ItemLibrary::createItem(), Item::finishedCreation(), ItemDocument::flushDeleteList(), ItemDocument::isValidItem(), ItemDocument::itemWithID(), ICNDocument::nodeWithID(), Item::removeItem(), Connector::restoreFromConnectorData(), Item::restoreFromItemData(), ItemDocument::select(), and Document::type().

Referenced by ItemDocument::paste(), and restoreDocument().

{
      if ( !itemDocument )
            return;
      
      ICNDocument *icnd = dynamic_cast<ICNDocument*>(itemDocument);
      
      //BEGIN Restore Nodes
      if (icnd)
      {
            const NodeDataMap::iterator nodeEnd = m_nodeDataMap.end();
            for ( NodeDataMap::iterator it = m_nodeDataMap.begin(); it != nodeEnd; ++it )
            {
                  if ( !icnd->nodeWithID( it.key() ) )
                  {
                        QString id = it.key();
                        if ( itemDocument->type() == Document::dt_circuit )
                              new ECNode( icnd, Node::ec_junction, Node::dir_up, QPoint( int(it.data().x), int(it.data().y) ), &id );
                  
                        else if ( itemDocument->type() == Document::dt_flowcode )
                              new FPNode( icnd, Node::fp_junction, Node::dir_up, QPoint( int(it.data().x), int(it.data().y) ), &id );
                  }
            }
            for ( NodeDataMap::iterator it = m_nodeDataMap.begin(); it != nodeEnd; ++it )
            {
                  Node *node = icnd->nodeWithID( it.key() );
                  if (node)
                        node->move( it.data().x, it.data().y );
            }
      }
      //END Restore Nodes
      
      
      //BEGIN Restore items
      const ItemDataMap::iterator itemEnd = m_itemDataMap.end();
      for ( ItemDataMap::iterator it = m_itemDataMap.begin(); it != itemEnd; ++it )
      {
            if ( !it.data().type.isEmpty() && !itemDocument->itemWithID( it.key() ) )
            {
                  Item *item = itemLibrary()->createItem( it.data().type, itemDocument, false, it.key(), false );
                  if ( item && !itemDocument->isValidItem(item) )
                  {
                        kdWarning() << "Attempted to create invalid item with id: " << it.key() << endl;
                        item->removeItem();
                        itemDocument->flushDeleteList();
                        item = 0l;
                  }
                  if (item)
                  {
                        //HACK We move the item now before restoreFromItemData is called later, in case it is to be parented
                        //(as we don't want to move children)...
                        item->move( it.data().x, it.data().y );
                  }
            }
      }
      for ( ItemDataMap::iterator it = m_itemDataMap.begin(); it != itemEnd; ++it )
      {
            Item *item = itemDocument->itemWithID(it.key());
            if (!item)
                  continue;
            
            item->restoreFromItemData( it.data() );
            item->finishedCreation();
            if (selectNew)
                  itemDocument->select(item);
            item->show();
      }
      //END Restore Items
      
      //BEGIN Restore Connectors
      if (icnd)
      {
            const ConnectorDataMap::iterator connectorEnd = m_connectorDataMap.end();
            for ( ConnectorDataMap::iterator it = m_connectorDataMap.begin(); it != connectorEnd; ++it )
            {
                  if ( icnd->connectorWithID( it.key() ) )
                        continue;
                  
                  QString id = it.key();
                  Node *startNode = 0l;
                  Node *endNode = 0l;
                  
                  if ( it.data().startNodeIsChild )
                  {
                        CNItem *item = icnd->cnItemWithID( it.data().startNodeParent );
                        if (!item)
                              kdError() << k_funcinfo << "Unable to find node parent with id: "<<it.data().startNodeParent<<endl;
                        else
                              startNode = item->childNode( it.data().startNodeCId );
                  }
                  else
                        startNode = icnd->nodeWithID( it.data().startNodeId );
                  
                  if ( it.data().endNodeIsChild )
                  {
                        CNItem *item = icnd->cnItemWithID( it.data().endNodeParent );
                        if (!item)
                              kdError() << k_funcinfo << "Unable to find node parent with id: "<<it.data().endNodeParent<<endl;
                        else
                              endNode = item->childNode( it.data().endNodeCId );
                  }
                  else
                        endNode = icnd->nodeWithID( it.data().endNodeId );
                  
                  if ( !startNode || !endNode )
                  {
                        kdError() << k_funcinfo << "End and start nodes for the connector do not both exist" << endl;
                  }
                  else
                  {
                        Connector *connector = new Connector( startNode, endNode, icnd, &id );
                              
                        startNode->addOutputConnector(connector);
                        endNode->addInputConnector(connector);
                  }
            }
            for ( ConnectorDataMap::iterator it = m_connectorDataMap.begin(); it != connectorEnd; ++it )
            {
                  Connector *connector = icnd->connectorWithID( it.key() );
                  if (connector)
                  {
                        connector->restoreFromConnectorData( it.data() );
                        if (selectNew)
                              icnd->select(connector);
                  }
            }
      }
      //END Restore Connectors
      
      // This is kind of hackish, but never mind
      if ( FlowCodeDocument *fcd = dynamic_cast<FlowCodeDocument*>(itemDocument) )
      {
            const ItemList fcdItems = fcd->itemList();
            const ItemList::const_iterator fcdItemsEnd = fcdItems.constEnd();
            for ( ItemList::const_iterator it = fcdItems.constBegin(); it != fcdItemsEnd; ++it )
            {
                  if ( FlowContainer * fc = dynamic_cast<FlowContainer*>((Item*)*it) )
                        fc->updateContainedVisibility();
            }
      }
}


Generated by  Doxygen 1.6.0   Back to index