summaryrefslogtreecommitdiffstats
path: root/util/qt3d/modeltweak/quickfile.cpp
blob: 8c8ad6b1b86e63a71fd79027f9fd18cfc31a783b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "quickfile.h"
#include <QFileDialog>
#include <QUrl>

/*!
    \qmlclass QuickFile QuickFile
    \brief QuickFile is a quick-and-dirty file handling object that supports file-io that QML/Javascript doesn't.
    \inherits QObject

    The QuickFile element is part of the \c{ModelTweak} namespace,
    so the following must appear at the top of any QML file that
    references it:

    \code
    import ModelTweak 1.0
    \endcode

    \sa QObject
*/

/*!
    \internal
*/
QuickFile::QuickFile(QObject *parent) :
    QObject(parent)
{
}

/*!
    Prompts for a location and filename to save the qml \c{QuickFile::data} generated by ModelTweaker.
    Returns an error message if the file was unsuccessfully saved,
    or an empty string if the save was successful.
*/
QString QuickFile::save() const
{
    QString filename = QFileDialog::getSaveFileName(0, tr("Save QML File"), "",tr("Files (*.qml)"));

    // FIXME: ensure fileName is appropriate as a qml Component
    if (!filename.endsWith(".qml"))
        filename.append(".qml");

    QString modelFilename = QUrl(_filename).toLocalFile();

    QDir outputDir = QFileInfo(filename).absoluteDir();
    QString relativeFilename = outputDir.relativeFilePath(modelFilename);

    QFile file(filename);

    qDebug("Attempting to write: %s", file.fileName().toAscii().constData());

    if (!file.open(QFile::WriteOnly))
        return file.errorString();

    QString dataToWrite = _data.arg(relativeFilename);

    file.write(dataToWrite.toUtf8());

    file.close();

    return QString();
}

/*!
    Prompts for a location to load a model file from and stores it into the the \c{QuickFile::filename}
    variable.
*/
void QuickFile::load()
{
    QString qmlFilename = QFileDialog::getOpenFileName(0, tr("Load File"), "",tr("Model Files (*.*)"));

    setFilename(qmlFilename);
}

QString QuickFile::filename() const {
    return _filename;
}

void QuickFile::setFilename(const QString filename) {
    if (_filename != filename)
        _filename = filename;
    emit filenameChanged(filename);
}

QString QuickFile::data() const {
    return _data;
}

void QuickFile::setData(const QString data) {
    if (_data != data)
        _data = data;
    emit dataChanged(data);
}