9
9
#include < gui/dialogs/dialog_box.h>
10
10
11
11
namespace sourcemenu {
12
- int offsetMode = 0 ;
13
12
int sourceId = 0 ;
14
- double customOffset = 0.0 ;
15
- double effectiveOffset = 0.0 ;
16
- int decimId = 0 ;
17
- bool iqCorrection = false ;
18
- bool invertIQ = false ;
19
-
20
13
EventHandler<std::string> sourcesChangedHandler;
21
14
EventHandler<std::string> sourceUnregisterHandler;
22
-
23
15
OptionList<std::string, std::string> sources;
24
16
std::string selectedSource;
25
- OptionList<std::string, int > offsets;
26
- std::vector< double > customOffsets ;
17
+
18
+ int decimId = 0 ;
27
19
OptionList<int , int > decimations;
28
20
21
+ bool iqCorrection = false ;
22
+ bool invertIQ = false ;
23
+
24
+ int offsetId = 0 ;
25
+ double manualOffset = 0.0 ;
26
+ std::string selectedOffset;
27
+ double effectiveOffset = 0.0 ;
28
+ OptionList<std::string, double > offsets;
29
+ std::map<std::string, double > namedOffsets;
30
+
29
31
bool showAddOffsetDialog = false ;
30
32
char newOffsetName[1024 ];
31
33
double newOffset = 0.0 ;
32
34
33
35
bool showDelOffsetDialog = false ;
34
36
std::string delOffsetName = " " ;
35
37
38
+ // Offset IDs
36
39
enum {
37
- OFFSET_MODE_NONE ,
38
- OFFSET_MODE_MANUAL ,
39
- OFFSET_MODE_CUSTOM_BASE
40
+ OFFSET_ID_NONE ,
41
+ OFFSET_ID_MANUAL ,
42
+ OFFSET_ID_CUSTOM_BASE
40
43
};
41
44
42
45
void updateOffset () {
43
46
// Compute the effective offset
44
- switch (offsetMode ) {
45
- case OFFSET_MODE_NONE :
47
+ switch (offsetId ) {
48
+ case OFFSET_ID_NONE :
46
49
effectiveOffset = 0 ;
47
50
break ;
48
- case OFFSET_MODE_MANUAL :
49
- effectiveOffset = customOffset ;
51
+ case OFFSET_ID_MANUAL :
52
+ effectiveOffset = manualOffset ;
50
53
break ;
51
54
default :
52
- effectiveOffset = customOffsets[offsetMode - OFFSET_MODE_CUSTOM_BASE ];
55
+ effectiveOffset = namedOffsets[offsets. name (offsetId) ];
53
56
break ;
54
57
}
55
58
56
59
// Apply it
57
60
sigpath::sourceManager.setTuningOffset (effectiveOffset);
58
61
}
59
62
63
+ void selectOffsetById (int id) {
64
+ // Update the offset mode
65
+ offsetId = id;
66
+ selectedOffset = offsets.name (id);
67
+
68
+ // Update the offset
69
+ updateOffset ();
70
+ }
71
+
72
+ void selectOffsetByName (const std::string& name) {
73
+ // If the name doesn't exist, select 'None'
74
+ if (!offsets.nameExists (name)) {
75
+ selectOffsetById (OFFSET_ID_NONE);
76
+ return ;
77
+ }
78
+
79
+ // Select using the ID associated with the name
80
+ selectOffsetById (offsets.nameId (name));
81
+ }
82
+
60
83
void refreshSources () {
61
84
// Get sources
62
85
auto sourceNames = sigpath::sourceManager.getSourceNames ();
@@ -83,7 +106,7 @@ namespace sourcemenu {
83
106
}
84
107
85
108
// Update the GUI variables
86
- sourceId = sources.valueExists (name);
109
+ sourceId = sources.valueId (name);
87
110
selectedSource = name;
88
111
89
112
// Select the source module
@@ -104,23 +127,22 @@ namespace sourcemenu {
104
127
// TODO: Stop everything
105
128
}
106
129
107
- void loadOffsets () {
130
+ void reloadOffsets () {
108
131
// Clear list
109
132
offsets.clear ();
110
- customOffsets .clear ();
133
+ namedOffsets .clear ();
111
134
112
135
// Define special offset modes
113
- offsets.define (" None" , 0 );
114
- offsets.define (" Manual" , 1 );
136
+ offsets.define (" None" , OFFSET_ID_NONE );
137
+ offsets.define (" Manual" , OFFSET_ID_MANUAL );
115
138
116
139
// Acquire the config file
117
140
core::configManager.acquire ();
118
141
119
142
// Load custom offsets
120
- std::vector<json> offsetList = core::configManager.conf [" offsets" ];
121
- for (auto & o : offsetList) {
122
- customOffsets.push_back (o[" offset" ]);
123
- offsets.define (o[" name" ], offsets.size ());
143
+ namedOffsets = (std::map<std::string, double >)core::configManager.conf [" offsets" ];
144
+ for (auto & [name, offset] : namedOffsets) {
145
+ offsets.define (name, offsets.size ());
124
146
}
125
147
126
148
// Release the config file
@@ -129,7 +151,7 @@ namespace sourcemenu {
129
151
130
152
void init () {
131
153
// Load offset modes
132
- loadOffsets ();
154
+ reloadOffsets ();
133
155
134
156
// Define decimation values
135
157
decimations.define (1 , " None" , 1 );
@@ -144,9 +166,9 @@ namespace sourcemenu {
144
166
core::configManager.acquire ();
145
167
146
168
// Load other settings
147
- std::string selected = core::configManager.conf [" source" ];
148
- customOffset = core::configManager.conf [" offset " ];
149
- offsetMode = core::configManager.conf [" offsetMode " ];
169
+ std::string selectedSource = core::configManager.conf [" source" ];
170
+ manualOffset = core::configManager.conf [" manualOffset " ];
171
+ std::string selectedOffset = core::configManager.conf [" selectedOffset " ];
150
172
iqCorrection = core::configManager.conf [" iqCorrection" ];
151
173
invertIQ = core::configManager.conf [" invertIQ" ];
152
174
int decimation = core::configManager.conf [" decimation" ];
@@ -159,13 +181,13 @@ namespace sourcemenu {
159
181
160
182
// Select the source module
161
183
refreshSources ();
162
- selectSource (selected );
184
+ selectSource (selectedSource );
163
185
164
186
// Update frontend settings
165
187
sigpath::iqFrontEnd.setDCBlocking (iqCorrection);
166
188
sigpath::iqFrontEnd.setInvertIQ (invertIQ);
167
- updateOffset ();
168
189
sigpath::iqFrontEnd.setDecimation (decimations.value (decimId));
190
+ selectOffsetByName (selectedOffset);
169
191
170
192
// Register handlers
171
193
sourcesChangedHandler.handler = onSourcesChanged;
@@ -180,23 +202,33 @@ namespace sourcemenu {
180
202
core::configManager.acquire ();
181
203
182
204
// Define a new offset
183
- auto newOffsetObj = json::object ();
184
- newOffsetObj[" name" ] = newOffsetName;
185
- newOffsetObj[" offset" ] = newOffset;
186
- core::configManager.conf [" offsets" ].push_back (newOffsetObj);
205
+ core::configManager.conf [" offsets" ][name] = offset;
187
206
188
207
// Acquire the config file
189
208
core::configManager.release (true );
190
209
191
210
// Reload the offsets
192
- loadOffsets ();
211
+ reloadOffsets ();
193
212
194
- // Re -select the same one
195
- // TODO: Switch from an array to a map, because two can't have the same name anyway and it'll just make things easier...+
213
+ // Attempt to re -select the same one
214
+ selectOffsetByName (selectedOffset);
196
215
}
197
216
198
217
void delOffset (const std::string& name) {
199
-
218
+ // Acquire the config file
219
+ core::configManager.acquire ();
220
+
221
+ // Define a new offset
222
+ core::configManager.conf [" offsets" ].erase (name);
223
+
224
+ // Acquire the config file
225
+ core::configManager.release (true );
226
+
227
+ // Reload the offsets
228
+ reloadOffsets ();
229
+
230
+ // Attempt to re-select the same one
231
+ selectOffsetByName (selectedOffset);
200
232
}
201
233
202
234
bool addOffsetDialog () {
@@ -217,7 +249,16 @@ namespace sourcemenu {
217
249
ImGui::SetNextItemWidth (menuWidth - ImGui::GetCursorPosX ());
218
250
ImGui::InputDouble (" ##sdrpp_add_offset_offset" , &newOffset);
219
251
220
- bool denyApply = !newOffsetName[0 ] || offsets.nameExists (newOffsetName);
252
+ bool nameExists = offsets.nameExists (newOffsetName);
253
+ bool reservedName = !strcmp (newOffsetName, " None" ) || !strcmp (newOffsetName, " Manual" );
254
+ bool denyApply = !newOffsetName[0 ] || nameExists || reservedName;
255
+
256
+ if (nameExists) {
257
+ ImGui::TextColored (ImVec4 (1 .0f , 0 .0f , 0 .0f , 1 .0f ), " An offset with the given name already exists." );
258
+ }
259
+ else if (reservedName) {
260
+ ImGui::TextColored (ImVec4 (1 .0f , 0 .0f , 0 .0f , 1 .0f ), " The given name is reserved." );
261
+ }
221
262
222
263
if (denyApply) { style::beginDisabled (); }
223
264
if (ImGui::Button (" Apply" )) {
@@ -271,20 +312,20 @@ namespace sourcemenu {
271
312
272
313
ImGui::LeftLabel (" Offset mode" );
273
314
ImGui::SetNextItemWidth (itemWidth - ImGui::GetCursorPosX () - 2 .0f *(lineHeight + 1 .5f *spacing));
274
- if (ImGui::Combo (" ##_sdrpp_offset_mode " , &offsetMode , offsets.txt )) {
275
- updateOffset ( );
315
+ if (ImGui::Combo (" ##_sdrpp_offset " , &offsetId , offsets.txt )) {
316
+ selectOffsetById (offsetId );
276
317
core::configManager.acquire ();
277
- core::configManager.conf [" offsetMode " ] = offsetMode ;
318
+ core::configManager.conf [" selectedOffset " ] = offsets. key (offsetId) ;
278
319
core::configManager.release (true );
279
320
}
280
321
ImGui::SameLine ();
281
322
ImGui::SetCursorPosX (ImGui::GetCursorPosX () - spacing);
282
- if (offsetMode < OFFSET_MODE_CUSTOM_BASE ) { ImGui::BeginDisabled (); }
323
+ if (offsetId < OFFSET_ID_CUSTOM_BASE ) { ImGui::BeginDisabled (); }
283
324
if (ImGui::Button (" -##_sdrpp_offset_del_" , ImVec2 (lineHeight + 0 .5f *spacing, 0 ))) {
284
- delOffsetName = " TEST " ;
325
+ delOffsetName = selectedOffset ;
285
326
showDelOffsetDialog = true ;
286
327
}
287
- if (offsetMode < OFFSET_MODE_CUSTOM_BASE ) { ImGui::EndDisabled (); }
328
+ if (offsetId < OFFSET_ID_CUSTOM_BASE ) { ImGui::EndDisabled (); }
288
329
ImGui::SameLine ();
289
330
ImGui::SetCursorPosX (ImGui::GetCursorPosX () - spacing);
290
331
if (ImGui::Button (" +##_sdrpp_offset_add_" , ImVec2 (lineHeight + 0 .5f *spacing, 0 ))) {
@@ -294,7 +335,7 @@ namespace sourcemenu {
294
335
295
336
// Offset delete confirmation
296
337
if (ImGui::GenericDialog (" sdrpp_del_offset_confirm" , showDelOffsetDialog, GENERIC_DIALOG_BUTTONS_YES_NO, []() {
297
- ImGui::Text (" Deleting offset named \" %s\" . Are you sure?" , delOffsetName);
338
+ ImGui::Text (" Deleting offset named \" %s\" . Are you sure?" , delOffsetName. c_str () );
298
339
}) == GENERIC_DIALOG_BUTTON_YES) {
299
340
delOffset (delOffsetName);
300
341
}
@@ -304,11 +345,11 @@ namespace sourcemenu {
304
345
305
346
ImGui::LeftLabel (" Offset" );
306
347
ImGui::FillWidth ();
307
- if (offsetMode == OFFSET_MODE_MANUAL ) {
308
- if (ImGui::InputDouble (" ##freq_offset" , &customOffset , 1.0 , 100.0 )) {
348
+ if (offsetId == OFFSET_ID_MANUAL ) {
349
+ if (ImGui::InputDouble (" ##freq_offset" , &manualOffset , 1.0 , 100.0 )) {
309
350
updateOffset ();
310
351
core::configManager.acquire ();
311
- core::configManager.conf [" offset" ] = customOffset ;
352
+ core::configManager.conf [" offset" ] = manualOffset ;
312
353
core::configManager.release (true );
313
354
}
314
355
}
0 commit comments