save file as

master
Denes Matetelki 14 years ago
parent 89afb2dd3a
commit 822eea5a5b

@ -4,6 +4,7 @@
#include <QStatusBar> #include <QStatusBar>
#include <QMessageBox> #include <QMessageBox>
#include <QFileDialog> #include <QFileDialog>
#include <QtXml>
#include "node.h" #include "node.h"
#include "edge.h" #include "edge.h"
@ -162,7 +163,7 @@ void GraphWidget::closeFile()
this, this,
tr("Save File"), tr("Save File"),
QDir::homePath(), QDir::homePath(),
tr("Images (*.png *.xpm *.jpg)")); tr("QtMindMap (*.qmm)"));
qDebug() << fileName; qDebug() << fileName;
@ -197,7 +198,36 @@ void GraphWidget::saveFile()
void GraphWidget::saveFileAs() 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<MainWindow *>(m_parent)->getStatusBar()->showMessage(
tr("Couldn't open file to write."),
3000); // millisec
return;
}
QTextStream ts( &file );
ts << doc.toString();
file.close();
dynamic_cast<MainWindow *>(m_parent)->getStatusBar()->showMessage(
tr("Saved."),
3000); // millisec
} }
void GraphWidget::openFile() 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) void GraphWidget::addEdge(Node *source, Node *destination)
{ {
if (source->isConnected(destination)) if (source->isConnected(destination))

@ -23,6 +23,7 @@ public:
void insertNode(); void insertNode();
void setActiveNodeEditable(); void setActiveNodeEditable();
void nodeSelected(Node *node); void nodeSelected(Node *node);
int nodeId(Node *node);
public slots: public slots:

@ -37,16 +37,23 @@ MainWindow::MainWindow(QWidget *parent) :
m_graphicsView = new GraphWidget(this); m_graphicsView = new GraphWidget(this);
m_ui->setupUi(this); m_ui->setupUi(this);
connect(m_ui->actionNew, SIGNAL(activated()), m_graphicsView, SLOT(newFile())); connect(m_ui->actionNew, SIGNAL(activated()),
connect(m_ui->actionOpen, SIGNAL(activated()), this, SLOT(klakk())); m_graphicsView, SLOT(newFile()));
connect(m_ui->actionSave, SIGNAL(activated()), this, SLOT(klakk())); connect(m_ui->actionOpen, SIGNAL(activated()),
connect(m_ui->actionClose, SIGNAL(activated()), m_graphicsView, SLOT(closeFile())); m_graphicsView, SLOT(openFile()));
connect(m_ui->actionExport, SIGNAL(activated()), this, SLOT(exportScene())); connect(m_ui->actionSave, SIGNAL(activated()),
connect(m_ui->actionQuit, SIGNAL(activated()), QApplication::instance(), m_graphicsView, SLOT(saveFile()));
SLOT(quit())); connect(m_ui->actionSaveAs, SIGNAL(activated()),
connect(m_ui->actionAbout_QtMindMap, SIGNAL(activated()), this, m_graphicsView, SLOT(saveFileAs()));
SLOT(about()));
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); setCentralWidget(m_graphicsView);
m_graphicsView->hide(); m_graphicsView->hide();
@ -57,11 +64,6 @@ MainWindow::~MainWindow()
delete m_ui; delete m_ui;
} }
void MainWindow::klakk()
{
qDebug() << __PRETTY_FUNCTION__;
}
void MainWindow::exportScene() void MainWindow::exportScene()
{ {
QFileDialog dialog(this, QFileDialog dialog(this,

@ -22,7 +22,7 @@ public:
void setFileName(QString fileName) { m_fileName = fileName; } void setFileName(QString fileName) { m_fileName = fileName; }
public slots: public slots:
void klakk();
void exportScene(); void exportScene();
void about(); void about();

@ -30,6 +30,7 @@
<addaction name="actionNew"/> <addaction name="actionNew"/>
<addaction name="actionOpen"/> <addaction name="actionOpen"/>
<addaction name="actionSave"/> <addaction name="actionSave"/>
<addaction name="actionSaveAs"/>
<addaction name="actionClose"/> <addaction name="actionClose"/>
<addaction name="separator"/> <addaction name="separator"/>
<addaction name="actionExport"/> <addaction name="actionExport"/>
@ -67,7 +68,7 @@
</action> </action>
<action name="actionOpen"> <action name="actionOpen">
<property name="enabled"> <property name="enabled">
<bool>false</bool> <bool>true</bool>
</property> </property>
<property name="text"> <property name="text">
<string>&amp;Open</string> <string>&amp;Open</string>
@ -78,7 +79,7 @@
</action> </action>
<action name="actionSave"> <action name="actionSave">
<property name="enabled"> <property name="enabled">
<bool>false</bool> <bool>true</bool>
</property> </property>
<property name="text"> <property name="text">
<string>&amp;Save</string> <string>&amp;Save</string>
@ -122,6 +123,14 @@
<string notr="true">Ctrl+X</string> <string notr="true">Ctrl+X</string>
</property> </property>
</action> </action>
<action name="actionSaveAs">
<property name="text">
<string>Save&amp;As</string>
</property>
<property name="shortcut">
<string>Ctrl+A</string>
</property>
</action>
</widget> </widget>
<layoutdefault spacing="6" margin="11"/> <layoutdefault spacing="6" margin="11"/>
<resources/> <resources/>

@ -319,3 +319,30 @@ bool Node::isConnected(const Node *node) const
return false; 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;
}

@ -3,6 +3,7 @@
#include <QGraphicsTextItem> #include <QGraphicsTextItem>
#include <QTextCursor> #include <QTextCursor>
#include <QDomElement>
#include "edge.h" #include "edge.h"
#include "graphwidget.h" #include "graphwidget.h"
@ -29,6 +30,7 @@ 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 );
protected: protected:

@ -4,7 +4,7 @@
# #
#------------------------------------------------- #-------------------------------------------------
QT += core gui svg QT += core gui svg xml
TARGET = qtmindmap TARGET = qtmindmap
TEMPLATE = app TEMPLATE = app

Loading…
Cancel
Save