From 822eea5a5b2bbc5656df934ccb4f005e2cb9d2ac Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Wed, 15 Jun 2011 16:58:57 +0200 Subject: [PATCH] save file as --- graphwidget.cpp | 37 ++++++++++++++++++++++++++++++++++++- graphwidget.h | 1 + mainwindow.cpp | 32 +++++++++++++++++--------------- mainwindow.h | 2 +- mainwindow.ui | 13 +++++++++++-- node.cpp | 27 +++++++++++++++++++++++++++ node.h | 2 ++ qtmindmap.pro | 2 +- 8 files changed, 96 insertions(+), 20 deletions(-) diff --git a/graphwidget.cpp b/graphwidget.cpp index 558a6b4..0027537 100644 --- a/graphwidget.cpp +++ b/graphwidget.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "node.h" #include "edge.h" @@ -162,7 +163,7 @@ void GraphWidget::closeFile() this, tr("Save File"), QDir::homePath(), - tr("Images (*.png *.xpm *.jpg)")); + tr("QtMindMap (*.qmm)")); qDebug() << fileName; @@ -197,7 +198,36 @@ void GraphWidget::saveFile() void GraphWidget::saveFileAs() { + QString fileName = QFileDialog::getSaveFileName( + this, + tr("Save File"), + QDir::homePath(), + tr("QtMindMap (*.qmm)")); + QDomDocument doc("QtMindMap"); + + QDomElement root = doc.createElement("qtmindmap"); + doc.appendChild( root ); + + foreach(Node *node, m_nodeList) + root.appendChild(node->createXMLNode(doc)); + + QFile file(fileName); + if (!file.open(QIODevice::WriteOnly)) + { + dynamic_cast(m_parent)->getStatusBar()->showMessage( + tr("Couldn't open file to write."), + 3000); // millisec + return; + } + + QTextStream ts( &file ); + ts << doc.toString(); + file.close(); + + dynamic_cast(m_parent)->getStatusBar()->showMessage( + tr("Saved."), + 3000); // millisec } void GraphWidget::openFile() @@ -551,6 +581,11 @@ void GraphWidget::nodeSelected(Node *node) } } +int GraphWidget::nodeId(Node *node) +{ + return m_nodeList.indexOf(node); +} + void GraphWidget::addEdge(Node *source, Node *destination) { if (source->isConnected(destination)) diff --git a/graphwidget.h b/graphwidget.h index 641c367..307132f 100644 --- a/graphwidget.h +++ b/graphwidget.h @@ -23,6 +23,7 @@ public: void insertNode(); void setActiveNodeEditable(); void nodeSelected(Node *node); + int nodeId(Node *node); public slots: diff --git a/mainwindow.cpp b/mainwindow.cpp index 45b025e..37efd01 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -37,16 +37,23 @@ MainWindow::MainWindow(QWidget *parent) : m_graphicsView = new GraphWidget(this); m_ui->setupUi(this); - connect(m_ui->actionNew, SIGNAL(activated()), m_graphicsView, SLOT(newFile())); - connect(m_ui->actionOpen, SIGNAL(activated()), this, SLOT(klakk())); - connect(m_ui->actionSave, SIGNAL(activated()), this, SLOT(klakk())); - connect(m_ui->actionClose, SIGNAL(activated()), m_graphicsView, SLOT(closeFile())); - connect(m_ui->actionExport, SIGNAL(activated()), this, SLOT(exportScene())); - connect(m_ui->actionQuit, SIGNAL(activated()), QApplication::instance(), - SLOT(quit())); - connect(m_ui->actionAbout_QtMindMap, SIGNAL(activated()), this, - SLOT(about())); - + connect(m_ui->actionNew, SIGNAL(activated()), + m_graphicsView, SLOT(newFile())); + connect(m_ui->actionOpen, SIGNAL(activated()), + m_graphicsView, SLOT(openFile())); + connect(m_ui->actionSave, SIGNAL(activated()), + m_graphicsView, SLOT(saveFile())); + connect(m_ui->actionSaveAs, SIGNAL(activated()), + m_graphicsView, SLOT(saveFileAs())); + + connect(m_ui->actionClose, SIGNAL(activated()), + m_graphicsView, SLOT(closeFile())); + connect(m_ui->actionExport, SIGNAL(activated()), + this, SLOT(exportScene())); + connect(m_ui->actionQuit, SIGNAL(activated()), + QApplication::instance(), SLOT(quit())); + connect(m_ui->actionAbout_QtMindMap, SIGNAL(activated()), + this, SLOT(about())); setCentralWidget(m_graphicsView); m_graphicsView->hide(); @@ -57,11 +64,6 @@ MainWindow::~MainWindow() delete m_ui; } -void MainWindow::klakk() -{ - qDebug() << __PRETTY_FUNCTION__; -} - void MainWindow::exportScene() { QFileDialog dialog(this, diff --git a/mainwindow.h b/mainwindow.h index 38c7223..960cc29 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -22,7 +22,7 @@ public: void setFileName(QString fileName) { m_fileName = fileName; } public slots: - void klakk(); + void exportScene(); void about(); diff --git a/mainwindow.ui b/mainwindow.ui index 386e820..442d0f6 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -30,6 +30,7 @@ + @@ -67,7 +68,7 @@ - false + true &Open @@ -78,7 +79,7 @@ - false + true &Save @@ -122,6 +123,14 @@ Ctrl+X + + + Save&As + + + Ctrl+A + + diff --git a/node.cpp b/node.cpp index 6c5d581..f017d5a 100644 --- a/node.cpp +++ b/node.cpp @@ -319,3 +319,30 @@ bool Node::isConnected(const Node *node) const return false; } + +QDomElement Node::createXMLNode( QDomDocument &d ) +{ + 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()); + + 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); + } + + return cn; +} diff --git a/node.h b/node.h index 4668a69..7e06d68 100644 --- a/node.h +++ b/node.h @@ -3,6 +3,7 @@ #include #include +#include #include "edge.h" #include "graphwidget.h" @@ -29,6 +30,7 @@ public: // changing visibility from prot to pub void keyPressEvent(QKeyEvent *event); bool isConnected(const Node *node) const; + QDomElement createXMLNode( QDomDocument &d ); protected: diff --git a/qtmindmap.pro b/qtmindmap.pro index 3b29aec..7d48db6 100644 --- a/qtmindmap.pro +++ b/qtmindmap.pro @@ -4,7 +4,7 @@ # #------------------------------------------------- -QT += core gui svg +QT += core gui svg xml TARGET = qtmindmap TEMPLATE = app