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 <QtCore>
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
#include <QStandardPaths>
#endif
#include <QDesktopServices>
#include <QDir>
#include <QDebug>
#include "importexport.h"
QString ImportExport::savePath;
QString ImportExport::getSavePath(void)
{
if (!ImportExport::savePath.isNull() && !ImportExport::savePath.isEmpty()) {
return ImportExport::savePath;
}
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
QString prefix = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
#elif QT_VERSION >= QT_VERSION_CHECK(5,0,0)
QString prefix = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
#else
QString prefix = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
#endif
QDir dir(prefix);
if (!dir.exists())
dir.mkpath(prefix);
QString path = prefix + QDir::separator() + SAVE_FILENAME;
return path;
}
bool ImportExport::saveProject(const ProjectProperties& props, const Speaker &speaker, const SealedBox &sbox, const PortedBox &pbox, const BandPassBox &bpbox, int number, int tab)
{
QString path = ImportExport::getSavePath();
qDebug() << "exporting" << path;
QFile file(path);
/* do not test if it exists, just override it */
return exportProject(file, props, speaker, sbox, pbox, bpbox, number, tab);
}
bool ImportExport::restoreProject(ProjectProperties &props, Speaker &speaker, SealedBox &sbox, PortedBox &pbox, BandPassBox &bpbox, int* number, int* tab)
{
QString path = ImportExport::getSavePath();
qDebug() << "importing" << path;
QFile file(path);
return importProject(props, speaker, sbox, pbox, bpbox, number, tab, file);
}
bool ImportExport::exportProject(QFile &file, const ProjectProperties& props, const Speaker &speaker, const SealedBox &sbox, const PortedBox &pbox, const BandPassBox &bpbox, int number, int tab)
{
QDomDocument xml("QSpeakersProject");
QDomElement root = xml.createElement("project");
xml.appendChild(root);
QDomElement spk = speaker.toDomElement(xml);
root.appendChild(spk);
QDomElement xsbox = sbox.toDomElement(xml);
root.appendChild(xsbox);
QDomElement xpbox = pbox.toDomElement(xml);
root.appendChild(xpbox);
QDomElement xbpbox = bpbox.toDomElement(xml);
root.appendChild(xbpbox);
QDomElement xlayout = xml.createElement("layout");
xlayout.setAttribute("sibling", number);
root.appendChild(xlayout);
QDomElement xstate = xml.createElement("state");
xstate.setAttribute("tab", tab);
root.appendChild(xstate);
QDomElement xprops = props.toDomElement(xml);
root.appendChild(xprops);
if (!file.open(QIODevice::WriteOnly))
return false;
if (!file.write(xml.toByteArray())) {
file.close();
return false;
}
file.close();
return true;
}
bool ImportExport::importProject(ProjectProperties& props, Speaker &speaker, SealedBox &sbox, PortedBox &pbox, BandPassBox &bpbox, int *number, int *tab, QFile &file)
{
QDomDocument doc("QSpeakersProject");
if (!file.exists())
return false;
if (!file.open(QIODevice::ReadOnly))
return false;
if (!doc.setContent(&file)) {
file.close();
return false;
}
file.close();
QDomElement root = doc.firstChildElement("project");
QDomNodeList speakers = root.elementsByTagName("speaker");
if (!speakers.isEmpty()) {
/* for now, only one speaker is considered */
QDomElement spk = speakers.at(0).toElement();
if (!spk.isNull())
speaker.fromDomElement(spk);
}
/* crawl 1st level boxes */
QDomElement box = root.firstChildElement("box");
while (!box.isNull()) {
if (box.attribute("type") == "sealed")
sbox.fromDomElement(box);
else if (box.attribute("type") == "ported")
pbox.fromDomElement(box);
else if (box.attribute("type") == "bandpass")
bpbox.fromDomElement(box);
else
qWarning() << __func__ << "unrecognized box type";
box = box.nextSiblingElement("box");
}
if (number != nullptr) {
QDomElement layout = root.firstChildElement("layout");
if (layout.isNull())
*number = 1; /* default */
else
*number = layout.attribute("sibling", "1").toInt();
}
if (tab != nullptr) {
QDomElement state = root.firstChildElement("state");
if (state.isNull())
*tab = 0;
else
*tab = state.attribute("tab", "0").toInt();
}
props.clear();
QDomNodeList xprops = root.elementsByTagName("properties");
if (!xprops.isEmpty())
props.fromDomElement(xprops.at(0).toElement());
return true;
}
void ImportExport::setSavePath(const QString &path)
{
ImportExport::savePath = path;
}
|