2 * This file is part of the SmuView project.
4 * Copyright (C) 2020-2021 Frank Stettner <frank-stettner@gmx.net>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "settingsmanager.hpp"
28 #include "src/session.hpp"
29 #include "src/channels/basechannel.hpp"
30 #include "src/data/properties/baseproperty.hpp"
31 #include "src/data/basesignal.hpp"
32 #include "src/devices/basedevice.hpp"
33 #include "src/devices/configurable.hpp"
34 #include "src/devices/deviceutil.hpp"
36 using std::shared_ptr
;
38 using sv::devices::DeviceType
;
42 bool SettingsManager::restore_settings_
= true;
44 SettingsManager::SettingsManager()
48 string
SettingsManager::format_key(const string
&str
)
50 string str_formated
= str
;
51 std::replace(str_formated
.begin(), str_formated
.end(), ':', '_');
52 std::replace(str_formated
.begin(), str_formated
.end(), '/', '_');
53 std::replace(str_formated
.begin(), str_formated
.end(), '\\', '_');
57 bool SettingsManager::restore_settings()
59 return SettingsManager::restore_settings_
;
62 void SettingsManager::set_restore_settings(bool restore_settings
)
64 SettingsManager::restore_settings_
= restore_settings
;
67 bool SettingsManager::has_device_settings(
68 shared_ptr
<devices::BaseDevice
> device
)
71 return settings
.childGroups().contains(device
->settings_id());
74 void SettingsManager::save_configurable(
75 const shared_ptr
<devices::Configurable
> &configurable
,
76 QSettings
&settings
, shared_ptr
<sv::devices::BaseDevice
> origin_device
,
77 const QString
&key_prefix
)
79 if (origin_device
== nullptr ||
80 origin_device
->settings_id() != configurable
->device_settings_id()) {
81 // Only store the device value if this specific configurable belongs to
82 // an other device than the tab-/device-setting (origin_device) it is
83 // stored into, belongs to (>= 0.0.5).
84 settings
.setValue(key_prefix
+ "device",
85 QVariant(configurable
->device_settings_id()));
87 settings
.setValue(key_prefix
+ "configurable",
88 QVariant(QString::fromStdString(configurable
->name())));
91 void SettingsManager::save_channel(
92 const shared_ptr
<channels::BaseChannel
> &channel
,
93 QSettings
&settings
, shared_ptr
<sv::devices::BaseDevice
> origin_device
,
94 const QString
&key_prefix
)
96 if (origin_device
== nullptr ||
97 origin_device
->settings_id() != channel
->parent_device()->settings_id()) {
98 // Only store the device value if this specific channel belongs to
99 // an other device than the tab-/device-setting (origin_device) it is
100 // stored into, belongs to (>= 0.0.5).
101 settings
.setValue(key_prefix
+ "device",
102 QVariant(channel
->parent_device()->settings_id()));
104 settings
.setValue(key_prefix
+ "channel",
105 QVariant(QString::fromStdString(channel
->name())));
108 void SettingsManager::save_signal(
109 const shared_ptr
<data::BaseSignal
> &signal
,
110 QSettings
&settings
, shared_ptr
<sv::devices::BaseDevice
> origin_device
,
111 const QString
&key_prefix
)
113 SettingsManager::save_channel(
114 signal
->parent_channel(), settings
, origin_device
, key_prefix
);
116 settings
.setValue(key_prefix
+ "signal_sr_q",
117 data::datautil::get_sr_quantity_id(signal
->quantity()));
118 settings
.setValue(key_prefix
+ "signal_sr_qf", QVariant::fromValue
<uint64_t>(
119 data::datautil::get_sr_quantity_flags_id(signal
->quantity_flags())));
122 void SettingsManager::save_property(
123 const shared_ptr
<data::properties::BaseProperty
> &property
,
124 QSettings
&settings
, shared_ptr
<sv::devices::BaseDevice
> origin_device
,
125 const QString
&key_prefix
)
127 SettingsManager::save_configurable(
128 property
->configurable(), settings
, origin_device
, key_prefix
);
130 settings
.setValue(key_prefix
+ "property_sr_type",
131 devices::deviceutil::get_sr_config_key(property
->config_key())->
133 settings
.setValue(key_prefix
+ "property_sr_ck",
134 devices::deviceutil::get_sr_config_key_id(property
->config_key()));
137 std::shared_ptr
< sv::devices::BaseDevice
> sv::SettingsManager::restore_device(
138 Session
& session
, QSettings
& settings
,
139 std::shared_ptr
< sv::devices::BaseDevice
> origin_device
,
140 const QString
&key_prefix
)
142 QString device_key
= key_prefix
+ "device";
144 if (!settings
.contains(device_key
)) {
145 // Use the supplied origin_device here when no device (key) is stored in
146 // this setting (>= 0.0.5).
147 return origin_device
;
149 if (origin_device
!= nullptr && origin_device
->type() == DeviceType::DemoDev
) {
150 // This is a fallback for older settings (pre 0.0.5):
151 return origin_device
;
154 // If a device (key) is stored in the settings, it means, that this device
155 // differs from the device (origin_device) this tab belongs to.
156 string device_id
= settings
.value(device_key
).toString().toStdString();
157 if (session
.device_map().count(device_id
) == 0)
160 return session
.device_map()[device_id
];
163 shared_ptr
<devices::Configurable
> SettingsManager::restore_configurable(
164 Session
&session
, QSettings
&settings
,
165 shared_ptr
<sv::devices::BaseDevice
> origin_device
,
166 const QString
&key_prefix
)
168 QString configurable_key
= key_prefix
+ "configurable";
170 auto device
= SettingsManager::restore_device(
171 session
, settings
, origin_device
, key_prefix
);
175 if (!settings
.contains(configurable_key
))
178 string conf_id
= settings
.value(configurable_key
).toString().toStdString();
179 if (device
->configurable_map().count(conf_id
) == 0)
182 return device
->configurable_map()[conf_id
];
185 shared_ptr
<data::properties::BaseProperty
> SettingsManager::restore_property(
186 Session
&session
, QSettings
&settings
,
187 shared_ptr
<sv::devices::BaseDevice
> origin_device
,
188 const QString
&key_prefix
)
190 QString property_key
= key_prefix
+ "property";
192 auto configurable
= SettingsManager::restore_configurable(
193 session
, settings
, origin_device
, key_prefix
);
197 if (!settings
.contains(property_key
+"_sr_type") ||
198 !settings
.contains(property_key
+"_sr_ck"))
201 //auto sr_type = settings.value(property_key+"_sr_type").value<int>(); // TODO
202 auto sr_ck
= settings
.value(property_key
+"_sr_ck").value
<uint32_t>();
203 auto ck
= devices::deviceutil::get_config_key(sr_ck
);
204 if (configurable
->property_map().count(ck
) == 0)
207 return configurable
->property_map()[ck
];
210 shared_ptr
<channels::BaseChannel
> SettingsManager::restore_channel(
211 Session
&session
, QSettings
&settings
,
212 shared_ptr
<sv::devices::BaseDevice
> origin_device
,
213 const QString
&key_prefix
)
215 QString channel_key
= key_prefix
+ "channel";
217 auto device
= SettingsManager::restore_device(
218 session
, settings
, origin_device
, key_prefix
);
222 if (!settings
.contains(channel_key
))
225 string channel_id
= settings
.value(channel_key
).toString().toStdString();
226 if (device
->channel_map().count(channel_id
) == 0)
229 return device
->channel_map()[channel_id
];
232 shared_ptr
<data::BaseSignal
> SettingsManager::restore_signal(
233 Session
&session
, QSettings
&settings
,
234 shared_ptr
<sv::devices::BaseDevice
> origin_device
,
235 const QString
&key_prefix
)
237 QString signal_key
= key_prefix
+ "signal";
239 auto channel
= SettingsManager::restore_channel(
240 session
, settings
, origin_device
, key_prefix
);
244 if (!settings
.contains(signal_key
+"_sr_q") ||
245 !settings
.contains(signal_key
+"_sr_qf"))
248 auto sr_q
= settings
.value(signal_key
+"_sr_q").value
<uint32_t>();
249 auto sr_qf
= settings
.value(signal_key
+"_sr_qf").value
<uint64_t>();
251 data::datautil::get_quantity(sr_q
),
252 data::datautil::get_quantity_flags(sr_qf
));
253 if (channel
->signal_map().count(mq
) == 0)
256 return channel
->signal_map()[mq
][0];