bool GraphScene::saveTo(const QString &fileName) { QFile file(fileName); if (file.open(QFile::WriteOnly | QFile::Truncate)) { QTextStream stream(&file); // Save the nodes with the following line format: 'node $NAME $TYPE $ROW $COL $FORMULA' for (int row = 0; row < m_gridLayout->rowCount(); row++) { for (int column = 0; column < m_gridLayout->columnCount(); column++) { NodeItem *node = static_cast<NodeItem *>(m_gridLayout->itemAt(row, column)); if (node) { stream << "node " << node->name() << ' ' << node->nodeType() << ' ' << row << ' ' << column << " \"" << node->formula() << '"' << endl; } } } // Save the edges with the following line format: 'edge $NAME $START $END $FORMULA' foreach (const DirectedEdgeItem *edge, m_edges) { stream << "edge " << edge->name() << ' ' << edge->start()->name() << ' ' << edge->end()->name() << " \"" << edge->formula() << '"' << endl; } file.close(); return true; }
bool GraphScene::loadFrom(const QString &fileName) { QFile file(fileName); if (file.exists() && file.open(QFile::ReadOnly)) { QTextStream stream(&file); QHash<QString, NodeItem *> nodes; // Temporary hashmap for faster lookup init(EmptyInit); // Create an empty scene QString line; do { // Iterate over all the lines in the file line = stream.readLine(); // and split the line at ' ' characters const QStringList splittedLine = line.split(" "); if (splittedLine[0] == "node") { // Found a node, add it to the scene NodeItem *node = new NodeItem(NULL, this); node->setName(splittedLine[1]); NodeItem::NodeType nodeType = static_cast<NodeItem::NodeType>(splittedLine[2].toInt()); switch (nodeType) { case NodeItem::InputNode: setInputNode(node); // We found the input node break; case NodeItem::OutputNode: setOuputNode(node); // We found the ouput node break; case NodeItem::StandardNode: // This should already be set by default but doesn't harm here node->setNodeType(NodeItem::StandardNode); break; } // There is obviously a (Matlab) formula attached, parse it... if (splittedLine.length() > 5) { int formulaStart = line.indexOf('"'); QString formula = line; formula.remove(0, formulaStart); while (!formula.endsWith('"')) { // Formula spans multiple lines formula += '\n' + stream.readLine(); } formula.chop(1); // Remove leading '"' formula.remove(0, 1); // Remove trailing '"' node->setFormula(formula); } nodes.insert(node->name(), node); m_gridLayout->addItem(node, splittedLine[3].toInt(), splittedLine[4].toInt()); } else if (splittedLine[0] == "edge") { NodeItem *start = nodes[splittedLine[2]]; // Found an edge, add it to the scene NodeItem *end = nodes[splittedLine[3]]; DirectedEdgeItem *edge = addEdge(start, end); edge->setName(splittedLine[1]); // There is obviously a (Matlab) formula attached, parse it... if (splittedLine.length() > 4) { int formulaStart = line.indexOf('"'); QString formula = line; formula.remove(0, formulaStart); while (!formula.endsWith('"')) { // Formula spans multiple lines formula += '\n' + stream.readLine(); } formula.chop(1); // Remove leading '"' formula.remove(0, 1); // Remove trailing '"' edge->setFormula(formula); } } } while (!line.isNull()); emit graphChanged(); file.close(); return true; } return false; }