From f2ed6a12f2b5305d3e199cee635ab6d1059f33e6 Mon Sep 17 00:00:00 2001 From: Ronan Le Tiec Date: Sun, 19 Apr 2020 16:01:46 +0200 Subject: [PATCH] Work on file opening and reader --- src/app/puzzle/puzzlemainwindow.cpp | 115 +++++++++++++++--- src/app/puzzle/puzzlemainwindow.h | 16 +++ .../puzzle/xml/vpuzzlelayoutfilereader.cpp | 36 +++++- 3 files changed, 149 insertions(+), 18 deletions(-) diff --git a/src/app/puzzle/puzzlemainwindow.cpp b/src/app/puzzle/puzzlemainwindow.cpp index 37061660b..ad6254d17 100644 --- a/src/app/puzzle/puzzlemainwindow.cpp +++ b/src/app/puzzle/puzzlemainwindow.cpp @@ -32,6 +32,11 @@ #include "ui_puzzlemainwindow.h" #include "dialogs/dialogaboutpuzzle.h" #include "xml/vpuzzlelayoutfilewriter.h" +#include "xml/vpuzzlelayoutfilereader.h" +#include "puzzleapplication.h" + +Q_LOGGING_CATEGORY(vPuzzleMainWindow, "v.puzzlemainwindow") + //--------------------------------------------------------------------------------------------------------------------- PuzzleMainWindow::PuzzleMainWindow(QWidget *parent) : @@ -68,7 +73,34 @@ PuzzleMainWindow::~PuzzleMainWindow() //--------------------------------------------------------------------------------------------------------------------- bool PuzzleMainWindow::LoadFile(const QString &path) { - Q_UNUSED(path) + QFile file(path); + file.open(QIODevice::ReadOnly); + + VPuzzleLayoutFileReader *fileReader = new VPuzzleLayoutFileReader(); + + if(m_layout == nullptr) + { + m_layout = new VPuzzleLayout(); + } + + fileReader->ReadFile(m_layout, &file); + + // TODO / FIXME : better return value and error handling + + return true; +} + +//--------------------------------------------------------------------------------------------------------------------- +bool PuzzleMainWindow::SaveFile(const QString &path) +{ + QFile file(path); + file.open(QIODevice::WriteOnly); + + VPuzzleLayoutFileWriter *fileWriter = new VPuzzleLayoutFileWriter(); + fileWriter->WriteFile(m_layout, &file); + + // TODO / FIXME : better return value and error handling + return true; } @@ -367,14 +399,60 @@ void PuzzleMainWindow::New() //--------------------------------------------------------------------------------------------------------------------- void PuzzleMainWindow::Open() { - // just for test purpuses, to be removed: - QMessageBox msgBox; - msgBox.setText("TODO PuzzleMainWindow::Open"); - int ret = msgBox.exec(); + qCDebug(vPuzzleMainWindow, "Openning puzzle layout file."); - Q_UNUSED(ret); + const QString filter(tr("Layout files") + QLatin1String(" (*.vlt)")); - // TODO + //Get list last open files + QStringList recentFiles = qApp->PuzzleSettings()->GetRecentFileList(); + QString dir; + if (recentFiles.isEmpty()) + { + dir = QDir::homePath(); + } + else + { + //Absolute path to last open file + dir = QFileInfo(recentFiles.first()).absolutePath(); + } + qCDebug(vPuzzleMainWindow, "Run QFileDialog::getOpenFileName: dir = %s.", qUtf8Printable(dir)); + const QString filePath = QFileDialog::getOpenFileName( + this, tr("Open file"), dir, filter, nullptr, +#ifdef Q_OS_LINUX + QFileDialog::DontUseNativeDialog +#endif + ); + + if (filePath.isEmpty()) + { + return; + } + + + // TODO : if m_layout == nullptr, open in current window + // otherwise open in new window + + // TODO : if layout file has a lock, warning message + + + if(!LoadFile(filePath)) + { + return; + } + + // Updates the list of recent files + recentFiles.removeAll(filePath); + recentFiles.prepend(filePath); + while (recentFiles.size() > MaxRecentFiles) + { + recentFiles.removeLast(); + } + qApp->PuzzleSettings()->SetRecentFileList(recentFiles); + + // updates the properties with the loaded data + SetPropertiesData(); + + // TODO : update the Carrousel and the QGraphicView } //--------------------------------------------------------------------------------------------------------------------- @@ -395,22 +473,29 @@ void PuzzleMainWindow::SaveAs() { // TODO / FIXME : See valentina how the save is done over there. we need to add the extension .vlt, check for empty file names etc. + //Get list last open files + QStringList recentFiles = qApp->PuzzleSettings()->GetRecentFileList(); + QString dir; + if (recentFiles.isEmpty()) + { + dir = QDir::homePath(); + } + else + { + //Absolute path to last open file + dir = QFileInfo(recentFiles.first()).absolutePath(); + } - QString filters(tr("Pattern files") + QLatin1String("(*.val)")); + QString filters(tr("Layout files") + QLatin1String("(*.vlt)")); QString fileName = QFileDialog::getSaveFileName(this, tr("Save as"), - /*dir +*/ QLatin1String("/") + tr("Layout") + QLatin1String(".vlt"), + dir + QLatin1String("/") + tr("Layout") + QLatin1String(".vlt"), filters, nullptr #ifdef Q_OS_LINUX , QFileDialog::DontUseNativeDialog #endif ); - - QFile file(fileName); - file.open(QIODevice::WriteOnly); - - VPuzzleLayoutFileWriter *fileWriter = new VPuzzleLayoutFileWriter(); - fileWriter->WriteFile(m_layout, &file); + SaveFile(fileName); } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/app/puzzle/puzzlemainwindow.h b/src/app/puzzle/puzzlemainwindow.h index 8c0f9edf7..8d60638eb 100644 --- a/src/app/puzzle/puzzlemainwindow.h +++ b/src/app/puzzle/puzzlemainwindow.h @@ -50,13 +50,29 @@ public: explicit PuzzleMainWindow(QWidget *parent = nullptr); virtual ~PuzzleMainWindow(); + /** + * @brief LoadFile Loads the layout file of given path in m_layout. + * This function doesn't update the gui. + * @param path + * @return + */ bool LoadFile(const QString &path); + /** + * @brief SaveFile Saves the current layout to the layout file of given path + * @param path + * @return + */ + bool SaveFile(const QString &path); + void ImportRawLayouts(const QStringList &layouts); public slots: void New(); +protected: + enum { MaxRecentFiles = 5 }; + private: Q_DISABLE_COPY(PuzzleMainWindow) Ui::PuzzleMainWindow *ui; diff --git a/src/app/puzzle/xml/vpuzzlelayoutfilereader.cpp b/src/app/puzzle/xml/vpuzzlelayoutfilereader.cpp index 76da5f53d..bc650617f 100644 --- a/src/app/puzzle/xml/vpuzzlelayoutfilereader.cpp +++ b/src/app/puzzle/xml/vpuzzlelayoutfilereader.cpp @@ -106,30 +106,49 @@ void VPuzzleLayoutFileReader::ReadProperties(VPuzzleLayout *layout) Q_ASSERT(isStartElement() && name() == QString("properties")); while (readNextStartElement()) { + qDebug(name().toString().toLatin1()); + if (name() == QString("unit")) { + qDebug("read unit"); QString unit = readElementText(); - // TODO read unit infos - + if(unit == UnitsToStr(Unit::Inch)) + { + layout->SetUnit(Unit::Inch); + } + else if(unit == UnitsToStr(Unit::Mm)) + { + layout->SetUnit(Unit::Cm); + } + else // no condition here to have a default value just in case + { + layout->SetUnit(Unit::Cm); + } } else if (name() == QString("description")) { + qDebug("read description"); QString description = readElementText(); // TODO read the description info } else if (name() == QString("size")) { + qDebug("read size"); QSizeF size = ReadSize(); layout->SetLayoutSize(size); + readElementText(); } else if (name() == QString("margin")) { + qDebug("read margin"); QMarginsF margins = ReadMargins(); layout->SetLayoutMargins(margins); + readElementText(); } else if (name() == QString("control")) { + qDebug("read control"); QXmlStreamAttributes attribs = attributes(); // attribs.value("followGrainLine"); // TODO @@ -139,14 +158,18 @@ void VPuzzleLayoutFileReader::ReadProperties(VPuzzleLayout *layout) layout->SetStickyEdges(attribs.value("stickyEdges") == "true"); layout->SetPiecesGap(attribs.value("piecesGap").toDouble()); + readElementText(); } else if (name() == QString("tiles")) { + qDebug("read tiles"); ReadTiles(layout); + readElementText(); } else { // TODO error handling, we encountered a tag that isn't defined in the specification + skipCurrentElement(); } } } @@ -168,16 +191,19 @@ void VPuzzleLayoutFileReader::ReadTiles(VPuzzleLayout *layout) QSizeF size = ReadSize(); // TODO set layout tiled size Q_UNUSED(size); + readElementText(); } else if (name() == QString("margin")) { QMarginsF margins = ReadMargins(); // TODO set layout tiled margins Q_UNUSED(margins); + readElementText(); } else { // TODO error handling, we encountered a tag that isn't defined in the specification + skipCurrentElement(); } } } @@ -200,6 +226,7 @@ void VPuzzleLayoutFileReader::ReadLayers(VPuzzleLayout *layout) else { // TODO error handling, we encountered a tag that isn't defined in the specification + skipCurrentElement(); } } } @@ -223,6 +250,7 @@ void VPuzzleLayoutFileReader::ReadLayer(VPuzzleLayer *layer) else { // TODO error handling, we encountered a tag that isn't defined in the specification + skipCurrentElement(); } } } @@ -239,10 +267,12 @@ void VPuzzleLayoutFileReader::ReadPiece(VPuzzlePiece *piece) if (name() == QString("...")) { // TODO + readElementText(); } else { // TODO error handling, we encountered a tag that isn't defined in the specification + skipCurrentElement(); } } @@ -269,7 +299,7 @@ QSizeF VPuzzleLayoutFileReader::ReadSize() QXmlStreamAttributes attribs = attributes(); size.setWidth(attribs.value("width").toDouble()); - size.setHeight(attribs.value("height").toDouble()); + size.setHeight(attribs.value("length").toDouble()); return size; }