diff --git a/graphwidget.cpp b/graphwidget.cpp index ff6d748..7ae6729 100644 --- a/graphwidget.cpp +++ b/graphwidget.cpp @@ -137,6 +137,11 @@ void GraphWidget::newFile() addFirstNode(); this->show(); + + m_parent->enableCloseFile(true); + m_parent->enableSave(false); + m_parent->enableSaveAs(true); + m_parent->setTitle("untitled"); } void GraphWidget::closeFile() @@ -189,21 +194,31 @@ void GraphWidget::closeFile() removeAllNodes(); this->hide(); } -} - -void GraphWidget::saveFile() -{ + m_parent->enableCloseFile(false); + m_parent->enableSave(false); + m_parent->enableSaveAs(false); + m_parent->setTitle(""); } void GraphWidget::saveFileAs() { - QString fileName = QFileDialog::getSaveFileName( - this, - tr("Save File"), - QDir::homePath(), - tr("QtMindMap (*.qmm)")); + QFileDialog dialog(this, + tr("Save MindMap as"), + QDir::homePath(), + tr("QtMindMap (*.qmm)")); + dialog.setAcceptMode(QFileDialog::AcceptSave); + dialog.setDefaultSuffix("qmm"); + + if (dialog.exec()) + { + m_fileName = dialog.selectedFiles().first(); + saveFile(); + } +} +void GraphWidget::saveFile() +{ QDomDocument doc("QtMindMap"); QDomElement root = doc.createElement("qtmindmap"); @@ -233,7 +248,7 @@ void GraphWidget::saveFileAs() edges_root.appendChild(cn); } - QFile file(fileName); + QFile file(m_fileName); if (!file.open(QIODevice::WriteOnly)) { dynamic_cast(m_parent)->getStatusBar()->showMessage( @@ -249,13 +264,95 @@ void GraphWidget::saveFileAs() dynamic_cast(m_parent)->getStatusBar()->showMessage( tr("Saved."), 3000); // millisec + + m_parent->enableCloseFile(true); + m_parent->enableSave(true); + m_parent->enableSaveAs(true); + + m_parent->setTitle(m_fileName); } void GraphWidget::openFile() { + qDebug() << __PRETTY_FUNCTION__; + QFileDialog dialog(this, + tr("Open MindMap"), + QDir::homePath(), + tr("QtMindMap (*.qmm)")); + dialog.setAcceptMode(QFileDialog::AcceptOpen); + dialog.setDefaultSuffix("qmm"); + + if (dialog.exec()) + { + m_fileName = dialog.selectedFiles().first(); + openFile(m_fileName); + } } +void GraphWidget::openFile(const QString &fileName) +{ + m_fileName = fileName; + m_parent->enableCloseFile(true); + m_parent->enableSave(true); + m_parent->enableSaveAs(true); + + m_parent->setTitle(fileName); + + QDomDocument doc("QtMindMap"); + QFile file(fileName); + if (!file.open(QIODevice::ReadOnly)) + { + qDebug() << "faszom cannot read file"; + return; + } + + if (!doc.setContent(&file)) + { + qDebug() << "cannot parse file"; + file.close(); + return; + } + file.close(); + + removeAllNodes(); + + QDomElement docElem = doc.documentElement(); + + QDomNodeList nodes = docElem.childNodes().item(0).childNodes(); + for (unsigned int i = 0; i < nodes.length(); i++) + { + QDomElement e = nodes.item(i).toElement(); + if(!e.isNull()) + { + Node *node = new Node(this); + node->setHtml(e.attribute("htmlContent")); + m_scene->addItem(node); + node->setPos(e.attribute("x").toFloat(), + e.attribute("y").toFloat()); + m_nodeList.append(node); + } + } + + QDomNodeList edges = docElem.childNodes().item(1).childNodes(); + for (unsigned int i = 0; i < edges.length(); i++) + { + QDomElement e = edges.item(i).toElement(); + if(!e.isNull()) + { + m_scene->addItem(new Edge(m_nodeList[e.attribute("source").toInt()], + + m_nodeList[e.attribute("destination").toInt()])); + } + } + + m_activeNode = m_nodeList.first(); + m_activeNode->setActive(); + + this->show(); +} + + QGraphicsScene *GraphWidget::getScene() { return m_scene; diff --git a/graphwidget.h b/graphwidget.h index 7e2897d..bf4c178 100644 --- a/graphwidget.h +++ b/graphwidget.h @@ -32,6 +32,7 @@ public slots: void closeFile(); void saveFile(); void saveFileAs(); + void openFile(const QString &fileName); void openFile(); protected: @@ -66,6 +67,7 @@ private: bool m_edgeAdding; bool m_edgeDeleting; bool m_contentChanged; + QString m_fileName; }; #endif // GRAPHWIDGET_H diff --git a/main.cpp b/main.cpp index 33b9e09..4cd75a7 100644 --- a/main.cpp +++ b/main.cpp @@ -59,7 +59,7 @@ int main(int argc, char *argv[]) } if (!argParser.filePath().isEmpty()) - w.setFileName(argParser.filePath()); + w.openFile(argParser.filePath()); if (!argParser.isShowMinimized()) w.show(); return a.exec(); diff --git a/mainwindow.cpp b/mainwindow.cpp index 37efd01..8ebb228 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -68,7 +68,7 @@ void MainWindow::exportScene() { QFileDialog dialog(this, tr("Export MindMap to image"), - "/home/cs0rbagomba", + QDir::homePath(), tr("PNG image file (*.png)")); dialog.setAcceptMode(QFileDialog::AcceptSave); dialog.setDefaultSuffix("png"); @@ -117,3 +117,45 @@ QStatusBar * MainWindow::getStatusBar() { return m_ui->statusBar; } + +void MainWindow::openFile(QString fileName) +{ + m_fileName = fileName; + m_graphicsView->openFile(m_fileName); +} + +void MainWindow::enableSave(const bool &enable) +{ + m_ui->actionSave->setEnabled(enable); +} + +void MainWindow::enableSaveAs(const bool &enable) +{ + m_ui->actionSaveAs->setEnabled(enable); +} + +void MainWindow::enableCloseFile(const bool &enable) +{ + m_ui->actionClose->setEnabled(enable); +} + +void MainWindow::setTitle(const QString &title) +{ + if (title.isEmpty()) + { + setWindowTitle("QtMindMap"); + } + else + { + QString t(title); + t.append(" - QtMindMap"); + setWindowTitle(t); + } +} + +//void MainWindow::setModifiedTitle(const bool &modified) +//{ +// modified ? +// setWindowTitle(windowTitle().remove(0,2)) : +// setWindowTitle(windowTitle().prepend("* ")); +//} diff --git a/mainwindow.h b/mainwindow.h index 960cc29..0be0876 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -19,7 +19,12 @@ public: ~MainWindow(); QStatusBar * getStatusBar(); QString getFileName() { return m_fileName; } - void setFileName(QString fileName) { m_fileName = fileName; } + void openFile(QString fileName); + void enableSave(const bool &enable = true); + void enableSaveAs(const bool &enable = true); + void enableCloseFile(const bool &enable = true); + void setTitle(const QString &title); +// void setModifiedTitle(const bool &modified = true); public slots: diff --git a/mainwindow.ui b/mainwindow.ui index 442d0f6..b07afbd 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -79,7 +79,7 @@ - true + false &Save @@ -103,7 +103,7 @@ - true + false &Close @@ -124,6 +124,9 @@ + + false + Save&As