From 790bd6a964e73ff7f3ec6442f087d5ea41c0c4d6 Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Thu, 16 Jun 2011 13:41:49 +0200 Subject: [PATCH] refactor: node and edge does not have createXml functions, graphwidget do it from them with the values of the public getvalue functions of node/edge. But probalby graphwidget shall not have the save functionality either, but mainwindow --- graphwidget.cpp | 41 ++++++++++++++++++++++++++++++++++++----- graphwidget.h | 3 ++- node.cpp | 24 ++++-------------------- node.h | 4 ++-- 4 files changed, 44 insertions(+), 28 deletions(-) diff --git a/graphwidget.cpp b/graphwidget.cpp index 0027537..ff6d748 100644 --- a/graphwidget.cpp +++ b/graphwidget.cpp @@ -209,8 +209,29 @@ void GraphWidget::saveFileAs() QDomElement root = doc.createElement("qtmindmap"); doc.appendChild( root ); + // nodes + QDomElement nodes_root = doc.createElement("nodes"); + root.appendChild(nodes_root); foreach(Node *node, m_nodeList) - root.appendChild(node->createXMLNode(doc)); + { + QDomElement cn = doc.createElement("nodes"); + cn.setAttribute( "id", QString::number(m_nodeList.indexOf(node))); + cn.setAttribute( "x", QString::number(node->pos().x())); + cn.setAttribute( "y", QString::number(node->pos().y())); + cn.setAttribute( "htmlContent", node->toHtml()); + nodes_root.appendChild(cn); + } + + //edges + QDomElement edges_root = doc.createElement("edges"); + root.appendChild(edges_root); + foreach(Edge *edge, edges()) + { + QDomElement cn = doc.createElement("edge"); + cn.setAttribute( "source", QString::number(m_nodeList.indexOf(edge->sourceNode()))); + cn.setAttribute( "destination", QString::number(m_nodeList.indexOf(edge->destNode()))); + edges_root.appendChild(cn); + } QFile file(fileName); if (!file.open(QIODevice::WriteOnly)) @@ -581,10 +602,10 @@ void GraphWidget::nodeSelected(Node *node) } } -int GraphWidget::nodeId(Node *node) -{ - return m_nodeList.indexOf(node); -} +//int GraphWidget::nodeId(Node *node) +//{ +// return m_nodeList.indexOf(node); +//} void GraphWidget::addEdge(Node *source, Node *destination) { @@ -649,3 +670,13 @@ void GraphWidget::addFirstNode() m_activeNode = m_nodeList.first(); m_activeNode->setActive(); } + +QList GraphWidget::edges() const +{ + QList list; + + foreach(Node * node, m_nodeList) + list.append(node->edgesFrom()); + + return list; +} diff --git a/graphwidget.h b/graphwidget.h index 307132f..7e2897d 100644 --- a/graphwidget.h +++ b/graphwidget.h @@ -23,7 +23,8 @@ public: void insertNode(); void setActiveNodeEditable(); void nodeSelected(Node *node); - int nodeId(Node *node); +// int nodeId(Node *node); + QList edges() const; public slots: diff --git a/node.cpp b/node.cpp index f017d5a..b08a642 100644 --- a/node.cpp +++ b/node.cpp @@ -320,29 +320,13 @@ bool Node::isConnected(const Node *node) const return false; } -QDomElement Node::createXMLNode( QDomDocument &d ) +QList Node::edgesFrom() const { - QDomElement cn = d.createElement("node"); - - cn.setAttribute( "id", QString::number(m_graph->nodeId(this))); - cn.setAttribute( "pos_x", QString::number(pos().x())); - cn.setAttribute( "pos_y", QString::number(pos().y())); - cn.setAttribute( "content", toHtml()); + QList list; foreach(EdgeElement element, m_edgeList) - { - QDomElement edge = d.createElement("edge"); if (element.startsFromThisNode) - { - edge.setAttribute("to", - QString::number(m_graph->nodeId(element.edge->destNode()))); - } else - { - edge.setAttribute("from", - QString::number(m_graph->nodeId(element.edge->sourceNode()))); - } - cn.appendChild(edge); - } + list.push_back(element.edge); - return cn; + return list; } diff --git a/node.h b/node.h index 7e06d68..4ceb22b 100644 --- a/node.h +++ b/node.h @@ -3,7 +3,6 @@ #include #include -#include #include "edge.h" #include "graphwidget.h" @@ -30,7 +29,8 @@ public: // changing visibility from prot to pub void keyPressEvent(QKeyEvent *event); bool isConnected(const Node *node) const; - QDomElement createXMLNode( QDomDocument &d ); + + QList edgesFrom() const; protected: