SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
mainsettings.cpp
Go to the documentation of this file.
1 #include <QSettings>
2 #include <QStringList>
3 
5 #include "commands/command.h"
7 #include "ambe/ambeengine.h"
8 
10  m_audioDeviceManager(nullptr),
11  m_ambeEngine(nullptr)
12 {
14  qInfo("MainSettings::MainSettings: settings file: format: %d location: %s", getFileFormat(), qPrintable(getFileLocation()));
15 }
16 
18 {
19  for(int i = 0; i < m_presets.count(); ++i)
20  {
21  delete m_presets[i];
22  }
23 
24  for(int i = 0; i < m_commands.count(); ++i)
25  {
26  delete m_commands[i];
27  }
28 }
29 
31 {
32  QSettings s;
33  return s.fileName();
34 }
35 
37 {
38  QSettings s;
39  return (int) s.format();
40 }
41 
43 {
44  QSettings s;
45 
46  m_preferences.deserialize(qUncompress(QByteArray::fromBase64(s.value("preferences").toByteArray())));
47  m_workingPreset.deserialize(qUncompress(QByteArray::fromBase64(s.value("current").toByteArray())));
48 
50  m_audioDeviceManager->deserialize(qUncompress(QByteArray::fromBase64(s.value("audio").toByteArray())));
51  }
52 
53  if (m_ambeEngine) {
54  m_ambeEngine->deserialize(qUncompress(QByteArray::fromBase64(s.value("ambe").toByteArray())));
55  }
56 
57  QStringList groups = s.childGroups();
58 
59  for(int i = 0; i < groups.size(); ++i)
60  {
61  if (groups[i].startsWith("preset"))
62  {
63  s.beginGroup(groups[i]);
64  Preset* preset = new Preset;
65 
66  if(preset->deserialize(qUncompress(QByteArray::fromBase64(s.value("data").toByteArray()))))
67  {
68  m_presets.append(preset);
69  }
70  else
71  {
72  delete preset;
73  }
74 
75  s.endGroup();
76  }
77  else if (groups[i].startsWith("command"))
78  {
79  s.beginGroup(groups[i]);
80  Command* command = new Command;
81 
82  if(command->deserialize(qUncompress(QByteArray::fromBase64(s.value("data").toByteArray()))))
83  {
84  m_commands.append(command);
85  }
86  else
87  {
88  delete command;
89  }
90 
91  s.endGroup();
92  }
93  }
94 
95  m_hardwareDeviceUserArgs.deserialize(qUncompress(QByteArray::fromBase64(s.value("hwDeviceUserArgs").toByteArray())));
96 }
97 
98 void MainSettings::save() const
99 {
100  QSettings s;
101 
102  s.setValue("preferences", qCompress(m_preferences.serialize()).toBase64());
103  s.setValue("current", qCompress(m_workingPreset.serialize()).toBase64());
104 
105  if (m_audioDeviceManager) {
106  s.setValue("audio", qCompress(m_audioDeviceManager->serialize()).toBase64());
107  }
108 
109  if (m_ambeEngine) {
110  s.setValue("ambe", qCompress(m_ambeEngine->serialize()).toBase64());
111  }
112 
113  QStringList groups = s.childGroups();
114 
115  for(int i = 0; i < groups.size(); ++i)
116  {
117  if ((groups[i].startsWith("preset")) || (groups[i].startsWith("command")))
118  {
119  s.remove(groups[i]);
120  }
121  }
122 
123  for (int i = 0; i < m_presets.count(); ++i)
124  {
125  QString group = QString("preset-%1").arg(i + 1);
126  s.beginGroup(group);
127  s.setValue("data", qCompress(m_presets[i]->serialize()).toBase64());
128  s.endGroup();
129  }
130 
131  for (int i = 0; i < m_commands.count(); ++i)
132  {
133  QString group = QString("command-%1").arg(i + 1);
134  s.beginGroup(group);
135  s.setValue("data", qCompress(m_commands[i]->serialize()).toBase64());
136  s.endGroup();
137  }
138 
139  s.setValue("hwDeviceUserArgs", qCompress(m_hardwareDeviceUserArgs.serialize()).toBase64());
140 }
141 
143 {
146 }
147 
148 Preset* MainSettings::newPreset(const QString& group, const QString& description)
149 {
150  Preset* preset = new Preset();
151  preset->setGroup(group);
152  preset->setDescription(description);
153  m_presets.append(preset);
154  return preset;
155 }
156 
158 {
159  m_presets.removeAll((Preset*)preset);
160  delete (Preset*)preset;
161 }
162 
163 void MainSettings::deletePresetGroup(const QString& groupName)
164 {
165  Presets::iterator it = m_presets.begin();
166 
167  while (it != m_presets.end())
168  {
169  if ((*it)->getGroup() == groupName) {
170  it = m_presets.erase(it);
171  } else {
172  ++it;
173  }
174  }
175 }
176 
178 {
179  qSort(m_presets.begin(), m_presets.end(), Preset::presetCompare);
180 }
181 
182 void MainSettings::renamePresetGroup(const QString& oldGroupName, const QString& newGroupName)
183 {
184  int nbPresets = getPresetCount();
185 
186  for (int i = 0; i < nbPresets; i++)
187  {
188  if (getPreset(i)->getGroup() == oldGroupName)
189  {
190  Preset *preset_mod = const_cast<Preset*>(getPreset(i));
191  preset_mod->setGroup(newGroupName);
192  }
193  }
194 }
195 
196 const Preset* MainSettings::getPreset(const QString& groupName, quint64 centerFrequency, const QString& description, const QString& type) const
197 {
198  int nbPresets = getPresetCount();
199 
200  for (int i = 0; i < nbPresets; i++)
201  {
202  if ((getPreset(i)->getGroup() == groupName) &&
203  (getPreset(i)->getCenterFrequency() == centerFrequency) &&
204  (getPreset(i)->getDescription() == description))
205  {
206  if (type == "R" && getPreset(i)->isSourcePreset()) {
207  return getPreset(i);
208  } else if (type == "T" && !getPreset(i)->isSourcePreset()) {
209  return getPreset(i);
210  }
211  }
212  }
213 
214  return 0;
215 }
216 
218 {
219  m_commands.append(command);
220 }
221 
223 {
224  m_commands.removeAll((Command*)command);
225  delete (Command*)command;
226 }
227 
228 void MainSettings::deleteCommandGroup(const QString& groupName)
229 {
230  Commands::iterator it = m_commands.begin();
231 
232  while (it != m_commands.end())
233  {
234  if ((*it)->getGroup() == groupName) {
235  it = m_commands.erase(it);
236  } else {
237  ++it;
238  }
239  }
240 }
241 
243 {
244  qSort(m_commands.begin(), m_commands.end(), Command::commandCompare);
245 }
246 
247 void MainSettings::renameCommandGroup(const QString& oldGroupName, const QString& newGroupName)
248 {
249  int nbCommands = getCommandCount();
250 
251  for (int i = 0; i < nbCommands; i++)
252  {
253  if (getCommand(i)->getGroup() == oldGroupName)
254  {
255  Command *command_mod = const_cast<Command*>(getCommand(i));
256  command_mod->setGroup(newGroupName);
257  }
258  }
259 }
260 
261 const Command* MainSettings::getCommand(const QString& groupName, const QString& description) const
262 {
263  int nbCommands = getCommandCount();
264 
265  for (int i = 0; i < nbCommands; i++)
266  {
267  if ((getCommand(i)->getGroup() == groupName) &&
268  (getCommand(i)->getDescription() == description))
269  {
270  return getCommand(i);
271  }
272  }
273 
274  return 0;
275 }
Preferences m_preferences
Definition: mainsettings.h:73
QString getFileLocation() const
void resetToDefaults()
Definition: preferences.cpp:9
void resetToDefaults()
Definition: preset.cpp:30
void addCommand(Command *command)
int getFileFormat() const
see QSettings::Format for the values
bool deserialize(const QByteArray &data)
QByteArray serialize() const
void deleteCommand(const Command *command)
void sortCommands()
const Command * getCommand(int index) const
Definition: mainsettings.h:40
Presets m_presets
Definition: mainsettings.h:77
QByteArray serialize() const
static bool commandCompare(const Command *c1, Command *c2)
Definition: command.h:72
void sortPresets()
Preset * newPreset(const QString &group, const QString &description)
Commands m_commands
Definition: mainsettings.h:79
void renamePresetGroup(const QString &oldGroupName, const QString &newGroupName)
int getPresetCount() const
Definition: mainsettings.h:30
AMBEEngine * m_ambeEngine
Definition: mainsettings.h:81
Preset m_workingPreset
Definition: mainsettings.h:75
void deleteCommandGroup(const QString &groupName)
QByteArray serialize() const
Definition: preset.cpp:43
Definition: preset.h:28
void setDescription(const QString &description)
Definition: preset.h:73
int32_t i
Definition: decimators.h:244
void setGroup(const QString &group)
Definition: preset.h:71
void deletePreset(const Preset *preset)
bool deserialize(const QByteArray &data)
Definition: preferences.cpp:39
QByteArray serialize() const
Definition: ambeengine.cpp:335
const Preset * getPreset(int index) const
Definition: mainsettings.h:31
QByteArray serialize() const
Definition: preferences.cpp:23
bool deserialize(const QByteArray &data)
AudioDeviceManager * m_audioDeviceManager
Definition: mainsettings.h:74
bool deserialize(const QByteArray &data)
Definition: ambeengine.cpp:355
static bool presetCompare(const Preset *p1, Preset *p2)
Definition: preset.h:103
void deletePresetGroup(const QString &groupName)
bool deserialize(const QByteArray &data)
Definition: preset.cpp:94
void save() const
DeviceUserArgs m_hardwareDeviceUserArgs
Definition: mainsettings.h:80
void setGroup(const QString &group)
Definition: command.h:47
int getCommandCount() const
Definition: mainsettings.h:39
void resetToDefaults()
void renameCommandGroup(const QString &oldGroupName, const QString &newGroupName)
bool deserialize(const QByteArray &data)
Definition: command.cpp:110