Work on file opening and reader

This commit is contained in:
Ronan Le Tiec 2020-04-19 16:01:46 +02:00
parent 83f5d70f98
commit f2ed6a12f2
3 changed files with 149 additions and 18 deletions

View File

@ -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);
}
//---------------------------------------------------------------------------------------------------------------------

View File

@ -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;

View File

@ -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;
}