~ubuntu-branches/debian/sid/qspeakers/sid

« back to all changes in this revision

Viewing changes to importexport.cpp

  • Committer: Package Import Robot
  • Author(s): Benoît Rouits
  • Date: 2016-10-25 21:23:27 UTC
  • Revision ID: package-import@ubuntu.com-20161025212327-oyyitrn6c9ac6706
Tags: upstream-1.0
Import upstream version 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <QtCore>
 
2
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
 
3
#include <QStandardPaths>
 
4
#endif
 
5
#include <QDesktopServices>
 
6
#include <QDir>
 
7
#include <QDebug>
 
8
 
 
9
#include "importexport.h"
 
10
 
 
11
QString ImportExport::savePath;
 
12
 
 
13
QString ImportExport::getSavePath(void)
 
14
{
 
15
    if (!ImportExport::savePath.isNull() && !ImportExport::savePath.isEmpty()) {
 
16
        return ImportExport::savePath;
 
17
    }
 
18
 
 
19
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
 
20
    QString prefix = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);
 
21
#else
 
22
    QString prefix = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
 
23
#endif
 
24
 
 
25
    QDir dir(prefix);
 
26
    if (!dir.exists())
 
27
        dir.mkpath(prefix);
 
28
 
 
29
    QString path = prefix + QDir::separator() + SAVE_FILENAME;
 
30
    return path;
 
31
}
 
32
 
 
33
void ImportExport::saveProject(const Speaker &speaker, const SealedBox &sbox, const PortedBox &pbox, const BandPassBox &bpbox, int number, int tab)
 
34
{
 
35
    QString path = ImportExport::getSavePath();
 
36
 
 
37
    QFile file(path);
 
38
    /* do not test if it exists, just override it */
 
39
    exportProject(file, speaker, sbox, pbox, bpbox, number, tab);
 
40
}
 
41
 
 
42
void ImportExport::restoreProject(Speaker &speaker, SealedBox &sbox, PortedBox &pbox, BandPassBox &bpbox, int* number, int* tab)
 
43
{
 
44
    QString path = ImportExport::getSavePath();
 
45
 
 
46
    QFile file(path);
 
47
    importProject(speaker, sbox, pbox, bpbox, number, tab, file);
 
48
}
 
49
 
 
50
void ImportExport::exportProject(QFile &file, const Speaker &speaker, const SealedBox &sbox, const PortedBox &pbox, const BandPassBox &bpbox, int number, int tab)
 
51
{
 
52
    QDomDocument xml("QSpeakersProject");
 
53
    QDomElement root = xml.createElement("project");
 
54
    xml.appendChild(root);
 
55
 
 
56
    QDomElement spk = speaker.toDomElement(xml);
 
57
    root.appendChild(spk);
 
58
 
 
59
    QDomElement xsbox = sbox.toDomElement(xml);
 
60
    root.appendChild(xsbox);
 
61
 
 
62
    QDomElement xpbox = pbox.toDomElement(xml);
 
63
    root.appendChild(xpbox);
 
64
 
 
65
    QDomElement xbpbox = bpbox.toDomElement(xml);
 
66
    root.appendChild(xbpbox);
 
67
 
 
68
    QDomElement xlayout = xml.createElement("layout");
 
69
    xlayout.setAttribute("sibling", number);
 
70
    root.appendChild(xlayout);
 
71
 
 
72
    QDomElement xstate = xml.createElement("state");
 
73
    xstate.setAttribute("tab", tab);
 
74
    root.appendChild(xstate);
 
75
 
 
76
    file.open(QIODevice::WriteOnly);
 
77
    file.write(xml.toByteArray());
 
78
    file.close();
 
79
}
 
80
 
 
81
void ImportExport::importProject(Speaker &speaker, SealedBox &sbox, PortedBox &pbox, BandPassBox &bpbox, int *number, int *tab, QFile &file)
 
82
{
 
83
    QDomDocument doc("QSpeakersProject");
 
84
 
 
85
    if (file.exists()) {
 
86
        file.open(QIODevice::ReadOnly);
 
87
        doc.setContent(&file);
 
88
        file.close();
 
89
    }
 
90
 
 
91
    QDomElement root = doc.firstChildElement("project");
 
92
 
 
93
    QDomNodeList speakers = root.elementsByTagName("speaker");
 
94
    /* for now, only one speaker is considered */
 
95
    QDomElement spk = speakers.at(0).toElement();
 
96
 
 
97
    if (!spk.isNull())
 
98
        speaker.fromDomElement(spk);
 
99
 
 
100
    /* crawl 1st level boxes */
 
101
    QDomElement box = root.firstChildElement("box");
 
102
    while (!box.isNull()) {
 
103
        if (box.attribute("type") == "sealed")
 
104
            sbox.fromDomElement(box);
 
105
        else if (box.attribute("type") == "ported")
 
106
            pbox.fromDomElement(box);
 
107
        else if (box.attribute("type") == "bandpass")
 
108
            bpbox.fromDomElement(box);
 
109
        else
 
110
            qWarning() << __func__ << "unrecognized box type";
 
111
        box = box.nextSiblingElement("box");
 
112
    }
 
113
 
 
114
    if (number != NULL) {
 
115
        QDomElement layout = root.firstChildElement("layout");
 
116
        if (layout.isNull())
 
117
            *number = 1; /* default */
 
118
        else
 
119
            *number = layout.attribute("sibling", "1").toInt();
 
120
    }
 
121
 
 
122
    if (tab != NULL) {
 
123
        QDomElement state = root.firstChildElement("state");
 
124
        if (state.isNull())
 
125
            *tab = 0;
 
126
        else
 
127
            *tab = state.attribute("tab", "0").toInt();
 
128
    }
 
129
}
 
130
 
 
131
void ImportExport::setSavePath(const QString &path)
 
132
{
 
133
    ImportExport::savePath = path;
 
134
}