/[qspeakers]/importexport.cpp
ViewVC logotype

Contents of /importexport.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 175 - (show annotations)
Thu May 11 13:03:49 2017 UTC (6 weeks, 6 days ago) by (unknown author)
File size: 4053 byte(s)
fix default project standard path
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::DataLocation);
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 qDebug() << "exporting" << path;
38 QFile file(path);
39 /* do not test if it exists, just override it */
40 exportProject(file, speaker, sbox, pbox, bpbox, number, tab);
41 }
42
43 void ImportExport::restoreProject(Speaker &speaker, SealedBox &sbox, PortedBox &pbox, BandPassBox &bpbox, int* number, int* tab)
44 {
45 QString path = ImportExport::getSavePath();
46 qDebug() << "importing" << path;
47 QFile file(path);
48 importProject(speaker, sbox, pbox, bpbox, number, tab, file);
49 }
50
51 void ImportExport::exportProject(QFile &file, const Speaker &speaker, const SealedBox &sbox, const PortedBox &pbox, const BandPassBox &bpbox, int number, int tab)
52 {
53 QDomDocument xml("QSpeakersProject");
54 QDomElement root = xml.createElement("project");
55 xml.appendChild(root);
56
57 QDomElement spk = speaker.toDomElement(xml);
58 root.appendChild(spk);
59
60 QDomElement xsbox = sbox.toDomElement(xml);
61 root.appendChild(xsbox);
62
63 QDomElement xpbox = pbox.toDomElement(xml);
64 root.appendChild(xpbox);
65
66 QDomElement xbpbox = bpbox.toDomElement(xml);
67 root.appendChild(xbpbox);
68
69 QDomElement xlayout = xml.createElement("layout");
70 xlayout.setAttribute("sibling", number);
71 root.appendChild(xlayout);
72
73 QDomElement xstate = xml.createElement("state");
74 xstate.setAttribute("tab", tab);
75 root.appendChild(xstate);
76
77 file.open(QIODevice::WriteOnly);
78 file.write(xml.toByteArray());
79 file.close();
80 }
81
82 void ImportExport::importProject(Speaker &speaker, SealedBox &sbox, PortedBox &pbox, BandPassBox &bpbox, int *number, int *tab, QFile &file)
83 {
84 QDomDocument doc("QSpeakersProject");
85
86 if (file.exists()) {
87 file.open(QIODevice::ReadOnly);
88 doc.setContent(&file);
89 file.close();
90 }
91
92 QDomElement root = doc.firstChildElement("project");
93
94 QDomNodeList speakers = root.elementsByTagName("speaker");
95 /* for now, only one speaker is considered */
96 QDomElement spk = speakers.at(0).toElement();
97
98 if (!spk.isNull())
99 speaker.fromDomElement(spk);
100
101 /* crawl 1st level boxes */
102 QDomElement box = root.firstChildElement("box");
103 while (!box.isNull()) {
104 if (box.attribute("type") == "sealed")
105 sbox.fromDomElement(box);
106 else if (box.attribute("type") == "ported")
107 pbox.fromDomElement(box);
108 else if (box.attribute("type") == "bandpass")
109 bpbox.fromDomElement(box);
110 else
111 qWarning() << __func__ << "unrecognized box type";
112 box = box.nextSiblingElement("box");
113 }
114
115 if (number != NULL) {
116 QDomElement layout = root.firstChildElement("layout");
117 if (layout.isNull())
118 *number = 1; /* default */
119 else
120 *number = layout.attribute("sibling", "1").toInt();
121 }
122
123 if (tab != NULL) {
124 QDomElement state = root.firstChildElement("state");
125 if (state.isNull())
126 *tab = 0;
127 else
128 *tab = state.attribute("tab", "0").toInt();
129 }
130 }
131
132 void ImportExport::setSavePath(const QString &path)
133 {
134 ImportExport::savePath = path;
135 }

  ViewVC Help
Powered by ViewVC 1.1.26