Exemple #1
0
/*! Opens specified file. This function does not ask the user to save an
    unsaved modified diagram. That is the MainWindow's job. This function
    will simply clear out the node vector and load the file in.
*/
void Document::openDocument(QString openName)
{
    QDomDocument* xmlDoc = openSaveFile(openName);
    QDomElement docElem = getNextDocumentElement(xmlDoc);
    QDomElement nodesVectorElement;
    QDomElement n;
    BaseNode* newNode;

    // Clear the nodes vector
    nodes.clear();
    ordering.clear();

    // Get the diagram type
    diagramType = static_cast<BaseNode::DiagramType>(docElem.attribute("diagramType").toInt());

    // First get out the first (and hopefully only) nodes_vector_element.
    nodesVectorElement = docElem.firstChildElement("nodes_vector_element");

    // Now we have to iterate over all the Node elements and restor them.
    // This style of iteration loop was taken from the Qt documentation for
    // QDomElement.
    n = nodesVectorElement.firstChildElement("Node");
    for (; !n.isNull(); n = n.nextSiblingElement("Node")) {
      // Now that we have the QDomElement, try to produce the node
      newNode = NodeFactory::getInstance()->produceFromClassName(n.attribute("class_name"));

      // Check we failed
      if (newNode == 0) {
        qDebug() << "Document::openDocument Error: Couldn't produce a " << n.attribute("class_name") << " with the node factory.";
      } else {
        // Restore the unique id and all the properties
        newNode->from_xml(n);

        // Push the node onto the node vector
        nodes.push_back(newNode);
        ordering.push_back(nodes.size()-1);
      }
    }

    // Now that we have all the id's restored, we can restore the connections
    // between the nodes. We loop over all the Node elements in
    // node_vector_element and loop over all the ConnectedObject elements in
    // each Node.
    n = nodesVectorElement.firstChildElement("Node");
    for (; !n.isNull(); n = n.nextSiblingElement("Node")) {
      BaseNode* sourcenode;
      QUuid sourceid;

      // Find the node that we created earlier from this element
      sourceid = QUuid(n.attribute("Id"));
      sourcenode = this->findNodeById(sourceid);

      if (sourcenode == 0) {
        qDebug() << "Document::openDocument Error: Couldn't find source node by id";
      } else {
        QUuid targetid;
        BaseNode* targetnode;

        // Now that we have the element and it's associated node, iterate
        // over all of the element's children ("ConnectedObjects")
        QDomElement child = n.firstChildElement("ConnectedObject");
        for (; !child.isNull(); child = child.nextSiblingElement("ConnectedObject")) {
          // Find the targeted node by id
          targetid = QUuid(child.attribute("id"));
          targetnode = this->findNodeById(targetid);

          // Check if it failed
          if (targetnode == 0) {
            qDebug() << "Document::openDocument Error: Couldn't find a target node by id";
          } else {
            // If it didn't fail, add the connection!
            sourcenode->addConnectedNode(targetnode);
          }
        }
      }
    }

    setFilename(openName);
    setModified(false);
    update();
}
Exemple #2
0
/*! Slot. Sets the second point in a new connection node.
  This function actually creates the node using the NodeFactory.
  It doesn't create the node if the first or second point is invalid.
*/
void Document::createConnectionPoint2(const QPoint &point) {
  // Create the object only if a valid first object was found
  int index;
  // creates a temp object node for self connectors
  BaseNode *tempnode;
  tempnode = NodeFactory::getInstance()->produce(newObjectID);

  if (firstConnectionIndex != -1) {
    index = getIndexAt(point);
    // And the second index was found
    if (((index != -1) &&
         (nodes.at(index)->isConnector() == false)) &&
            index != firstConnectionIndex) {
      // produce the object
      BaseNode *newNode;
      newNode = NodeFactory::getInstance()->produce(newObjectID);

      // now connect the connection to both objects, and connect the
      // objects to the connections
      newNode->addConnectedNode(nodes.at(firstConnectionIndex));
      newNode->addConnectedNode(nodes.at(index));
      nodes.at(firstConnectionIndex)->addConnectedNode(newNode);
      nodes.at(index)->addConnectedNode(newNode);

      addNodeToList(newNode);
      ordering.append(nodes.size()-1);

      nodes.at(index)->setSelectedForConnectionPoint(false);

      setModified(true);
      emit modelChanged();
    }
    //  Self connectors
    // And the second index was found
    else if ((((index != -1) &&
         (nodes.at(index)->isConnector() == false)) &&
            (index == firstConnectionIndex) &&
              (tempnode->getText() == "Collaboration Self Line")) ||
             ((index != -1) && (nodes.at(index)->isConnector() == false)
             &&(index == firstConnectionIndex) && (tempnode->getText() == "State Self Line"))) {

        // produce the object
      BaseNode *newNode;
      newNode = NodeFactory::getInstance()->produce(newObjectID);

      // now connect the connection to both objects, and connect the
      // objects to the connections
      newNode->addConnectedNode(nodes.at(firstConnectionIndex));
      newNode->addConnectedNode(nodes.at(index));
      nodes.at(firstConnectionIndex)->addConnectedNode(newNode);
      nodes.at(index)->addConnectedNode(newNode);

      addNodeToList(newNode);
      ordering.append(nodes.size()-1);

      nodes.at(index)->setSelectedForConnectionPoint(false);

      setModified(true);
      emit modelChanged();
    }
// end of  self connect
    else {
      QMessageBox::information(0, "pUML", "No second object selected");
    }
  }

  if (firstConnectionIndex != -1) {
    nodes.at(firstConnectionIndex)->setSelectedForConnectionPoint(false);
  }
}