summaryrefslogtreecommitdiffstats
path: root/util/qt3d/modeltweak/quickfile.cpp
blob: 3fead170474ea67942f19e1a9b128f455fc3fd5e (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "quickfile.h"
#include <QFileDialog>
#include <QUrl>
 #include <QMessageBox>

/*!
    \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/cancelled.
*/
QString QuickFile::save() const
{
    QString filename = QFileDialog::getSaveFileName(0, tr("Save QML File"), "",tr("Files (*.qml)"));

    //If cancel button pressed (ie. filename empty), we can just drop out immediately
    if (filename.isEmpty()) {
        return QString();
    }

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

    QDir outputDir = QFileInfo(filename).absoluteDir();

    QString modelFilename = QUrl(_filename).toLocalFile();
    QString modelFN = QFileInfo(modelFilename).fileName();

    QString dstModelLocation = outputDir.path() + QDir::separator() + modelFN;
    if (dstModelLocation.compare(modelFilename,Qt::CaseInsensitive) != 0) {
        // src and dst model paths are different.
        // Copy model file over.
        qDebug("Attempting to copy:");
        qDebug("  from: %s",modelFilename.toAscii().constData());
        qDebug("  to  : %s",dstModelLocation.toAscii().constData());
        QFile dstModelFile(dstModelLocation);
        if (dstModelFile.exists()) {
            if (!dstModelFile.remove()) {
                qDebug("  failed to remove dst file!");
            }
        }
        QFile srcModelFile(modelFilename);
        if (srcModelFile.copy(dstModelLocation)) {
            qDebug("  Model was copied successfully.");
        } else {
            qDebug("  failed to copy model file !");
        }
    }

    QFile file(filename);

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

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

    QString dataToWrite = _data.arg(modelFN);

    file.write(dataToWrite.toUtf8());

    file.close();

    return QString();
}

/*!
    Save/Close dialog for unsaved changes to QML files.  This is shown if the user
    closes the app without saving the modifications they have made.

    Returns an error message if the file was unsuccessfully saved,
    or an empty string if the save was successful/cancelled.
*/
QString QuickFile::promptSave() const
{
    QMessageBox msgBox;
    msgBox.setText("The document has been modified.");
    msgBox.setInformativeText("Do you want to save your changes?");
    msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard);
    msgBox.setDefaultButton(QMessageBox::Save);
    int ret = msgBox.exec();

    if (ret==QMessageBox::Save) {
        return save();
    } else {
        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, _title, "", _filter);
    setFilename(qmlFilename);
}


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

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

QString QuickFile::title() const {
    return _title;
}

void QuickFile::setTitle(const QString title) {
    if (_title != title)
        _title = title;
    emit titleChanged(title);
}

QString QuickFile::filter() const {
    return _filter;
}

void QuickFile::setFilter(const QString filter) {
    if (_filter != filter)
        _filter = filter;
    emit filterChanged(filter);
}

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

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