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

master
Denes Matetelki 14 years ago
parent 822eea5a5b
commit 790bd6a964

@ -209,8 +209,29 @@ void GraphWidget::saveFileAs()
QDomElement root = doc.createElement("qtmindmap"); QDomElement root = doc.createElement("qtmindmap");
doc.appendChild( root ); doc.appendChild( root );
// nodes
QDomElement nodes_root = doc.createElement("nodes");
root.appendChild(nodes_root);
foreach(Node *node, m_nodeList) 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); QFile file(fileName);
if (!file.open(QIODevice::WriteOnly)) if (!file.open(QIODevice::WriteOnly))
@ -581,10 +602,10 @@ void GraphWidget::nodeSelected(Node *node)
} }
} }
int GraphWidget::nodeId(Node *node) //int GraphWidget::nodeId(Node *node)
{ //{
return m_nodeList.indexOf(node); // return m_nodeList.indexOf(node);
} //}
void GraphWidget::addEdge(Node *source, Node *destination) void GraphWidget::addEdge(Node *source, Node *destination)
{ {
@ -649,3 +670,13 @@ void GraphWidget::addFirstNode()
m_activeNode = m_nodeList.first(); m_activeNode = m_nodeList.first();
m_activeNode->setActive(); m_activeNode->setActive();
} }
QList<Edge *> GraphWidget::edges() const
{
QList<Edge *> list;
foreach(Node * node, m_nodeList)
list.append(node->edgesFrom());
return list;
}

@ -23,7 +23,8 @@ public:
void insertNode(); void insertNode();
void setActiveNodeEditable(); void setActiveNodeEditable();
void nodeSelected(Node *node); void nodeSelected(Node *node);
int nodeId(Node *node); // int nodeId(Node *node);
QList<Edge *> edges() const;
public slots: public slots:

@ -320,29 +320,13 @@ bool Node::isConnected(const Node *node) const
return false; return false;
} }
QDomElement Node::createXMLNode( QDomDocument &d ) QList<Edge *> Node::edgesFrom() const
{ {
QDomElement cn = d.createElement("node"); QList<Edge *> list;
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());
foreach(EdgeElement element, m_edgeList) foreach(EdgeElement element, m_edgeList)
{
QDomElement edge = d.createElement("edge");
if (element.startsFromThisNode) if (element.startsFromThisNode)
{ list.push_back(element.edge);
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);
}
return cn; return list;
} }

@ -3,7 +3,6 @@
#include <QGraphicsTextItem> #include <QGraphicsTextItem>
#include <QTextCursor> #include <QTextCursor>
#include <QDomElement>
#include "edge.h" #include "edge.h"
#include "graphwidget.h" #include "graphwidget.h"
@ -30,7 +29,8 @@ public:
// changing visibility from prot to pub // changing visibility from prot to pub
void keyPressEvent(QKeyEvent *event); void keyPressEvent(QKeyEvent *event);
bool isConnected(const Node *node) const; bool isConnected(const Node *node) const;
QDomElement createXMLNode( QDomDocument &d );
QList<Edge *> edgesFrom() const;
protected: protected:

Loading…
Cancel
Save