-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConfigGroup.h
More file actions
429 lines (377 loc) · 11.2 KB
/
ConfigGroup.h
File metadata and controls
429 lines (377 loc) · 11.2 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
///////////////////////////////////////////////////////////////////////////////
// FILE: ConfigGroup.h
// PROJECT: Micro-Manager
// SUBSYSTEM: MMCore
//-----------------------------------------------------------------------------
// DESCRIPTION: Groups of configurations and container for the groups
//
// AUTHOR: Nenad Amodaj, nenad@amodaj.com, 23/01/2006
//
// COPYRIGHT: University of California, San Francisco, 2006
//
// LICENSE: This file is distributed under the "Lesser GPL" (LGPL) license.
// License text is included with the source distribution.
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES.
#pragma once
#include "Configuration.h"
#include "Error.h"
#include <string>
#include <vector>
/**
* Encapsulates a collection (map) of user-defined presets.
*/
template <class T>
class ConfigGroupBase {
public:
/**
* Defines a new preset.
*/
void Define(const char* configName)
{
configs_[configName];
}
/**
* Defines a new preset.
*/
void Define(const char* configName, const char* deviceLabel, const char* propName, const char* value)
{
PropertySetting setting(deviceLabel, propName, value);
configs_[configName].addSetting(setting);
}
/**
* Finds preset by name.
*/
T* Find(const char* configName)
{
typename std::map<std::string,T>::iterator it = configs_.find(configName);
if (it == configs_.end())
return 0;
else
return &(it->second);
}
/**
* Renames a preset (addressed by old name).
*/
bool Rename(const char* oldConfigName, const char* newConfigName)
{
// tolerate empty configuration names
if (strlen(oldConfigName) == 0)
return true;
typename std::map<std::string, T>::const_iterator it = configs_.find(oldConfigName);
if (it == configs_.end())
return false;
configs_[newConfigName] = it->second;
configs_.erase(it->first);
return true;
}
/**
* Deletes a preset by name.
*/
bool Delete(const char* configName)
{
// tolerate empty configuration names
if (strlen(configName) == 0)
return true;
typename std::map<std::string, T>::const_iterator it = configs_.find(configName);
if (it == configs_.end())
return false;
configs_.erase(configName);
return true;
}
/**
* Deletes a preset property by name.
*/
bool Delete(const char* configName, const char* deviceLabel, const char* propName)
{
// tolerate empty configuration names
if (strlen(configName) == 0)
return true;
// Check if configuration with configName exists:
typename std::map<std::string, T>::const_iterator it = configs_.find(configName);
if (it == configs_.end())
return false;
// Delete the specified property
configs_[configName].deleteSetting(deviceLabel,propName);
return true;
}
/**
* Returns a list of available configurations.
*/
std::vector<std::string> GetAvailable() const
{
std::vector<std::string> configList;
typename std::map<std::string, T>::const_iterator it = configs_.begin();
while(it != configs_.end())
configList.push_back(it++->first);
return configList;
}
bool IsEmpty()
{
return configs_.size() == 0;
}
protected:
ConfigGroupBase() {}
virtual ~ConfigGroupBase() {}
std::map<std::string, T> configs_;
};
/**
* Encapsulates a collection (map) of user-defined presets.
*/
class ConfigGroup : public ConfigGroupBase<Configuration>
{
};
namespace mmcore {
namespace internal {
/**
* Encapsulates a collection of preset groups.
*/
class ConfigGroupCollection {
public:
ConfigGroupCollection() {}
~ConfigGroupCollection() {}
/**
* Define a configuration.
*/
void Define(const char* groupName, const char* configName)
{
groups_[groupName].Define(configName);
}
/**
* Define a configuration property value.
*/
void Define(const char* groupName, const char* configName, const char* deviceLabel, const char* propName, const char* value)
{
groups_[groupName].Define(configName, deviceLabel, propName, value);
}
/**
* Define a new empty group.
*/
bool Define(const char* groupName)
{
std::map<std::string, ConfigGroup>::iterator it = groups_.find(groupName);
if (it == groups_.end())
{
groups_[groupName]; // effectively inserts an empty group
return true;
}
else
return false; // group name already in use
}
/**
* Finds preset (configuration) based on the group and preset names.
*/
Configuration* Find(const char* groupName, const char* configName)
{
std::map<std::string, ConfigGroup>::iterator it = groups_.find(groupName);
if (it == groups_.end())
return 0;
else
return it->second.Find(configName);
}
/**
* Checks if group exists.
*/
bool isDefined(const char* groupName)
{
return groups_.find(groupName) != groups_.end();
}
/**
* Rename a configuration preset within a group
*/
bool RenameConfig(const char* groupName, const char* oldConfigName, const char* newConfigName)
{
if (0 != strcmp(oldConfigName, newConfigName))
{
// tolerate empty group names
if (strlen(groupName) == 0)
return true;
std::map<std::string, ConfigGroup>::iterator it = groups_.find(groupName);
if (it == groups_.end())
return false; // group not found
return it->second.Rename(oldConfigName, newConfigName);
} else {
return true;
}
}
/**
* Delete a property from a configuration in the specified group
*/
bool Delete(const char* groupName, const char* configName, const char* deviceLabel, const char* propName)
{
// tolerate empty group names
if (strlen(groupName) == 0)
return true;
std::map<std::string, ConfigGroup>::iterator it = groups_.find(groupName);
if (it == groups_.end())
return false; // group not found
return it->second.Delete(configName, deviceLabel, propName);
}
/**
* Delete a configuration from group
*/
bool Delete(const char* groupName, const char* configName)
{
// tolerate empty group names
if (strlen(groupName) == 0)
return true;
std::map<std::string, ConfigGroup>::iterator it = groups_.find(groupName);
if (it == groups_.end())
return false; // group not found
return it->second.Delete(configName);
}
/**
* Delete an entire group.
*/
bool Delete(const char* groupName)
{
// tolerate empty group names
if (strlen(groupName) == 0)
return true;
std::map<std::string, ConfigGroup>::iterator it = groups_.find(groupName);
if (it != groups_.end())
{
groups_.erase(it->first);
return true;
}
return false; //not found
}
/**
* Rename a configuration group.
*/
bool RenameGroup(const char* oldGroupName, const char* newGroupName)
{
if (0 != strcmp(oldGroupName, newGroupName))
{
// tolerate empty group names
if (strlen(oldGroupName) == 0 || strlen(newGroupName)==0)
return true;
std::map<std::string, ConfigGroup>::iterator it = groups_.find(oldGroupName);
if (it != groups_.end())
{
groups_[newGroupName] = it->second;
groups_.erase(it->first);
return true;
}
return false; //not found
}
else
{
return true;
}
}
/**
* Returns a list of groups names.
*/
std::vector<std::string> GetAvailableGroups() const
{
std::vector<std::string> groupList;
groupList.clear();
std::map<std::string, ConfigGroup>::const_iterator it = groups_.begin();
while(it != groups_.end())
groupList.push_back(it++->first);
return groupList;
}
/**
* Returns a list of preset names.
*/
std::vector<std::string> GetAvailableConfigs(const char* groupName) const
{
std::vector<std::string> confList;
confList.clear();
std::map<std::string, ConfigGroup>::const_iterator it = groups_.find(groupName);
if (it != groups_.end())
{
confList = it->second.GetAvailable();
}
return confList;
}
void Clear()
{
groups_.clear();
}
private:
std::map<std::string, ConfigGroup> groups_;
};
} // namespace internal
} // namespace mmcore
/**
* Specialized form of configuration designed to detect pixel size
* from current settings.
*/
class PixelSizeConfiguration : public Configuration
{
public:
PixelSizeConfiguration() : pixelSizeUm_(0.0),
dxdz_(0.0),
dydz_(0.0),
optimalZ_(0.0)
{
affineMatrix_.push_back(1.0);
affineMatrix_.push_back(0.0);
affineMatrix_.push_back(0.0);
affineMatrix_.push_back(0.0);
affineMatrix_.push_back(1.0);
affineMatrix_.push_back(0.0);
}
~PixelSizeConfiguration() {}
void setPixelSizeUm(double pixSize) {pixelSizeUm_ = pixSize;}
double getPixelSizeUm() const {return pixelSizeUm_;}
void setPixelConfigAffineMatrix(std::vector<double> &affineMatrix) MMCORE_LEGACY_THROW(CMMError)
{
if (affineMatrix.size() != 6)
{
throw CMMError("PixelConfig affineMatrix must have 6 elements");
}
for (unsigned int i=0; i < affineMatrix.size(); i++)
{
affineMatrix_.at(i) = affineMatrix.at(i);
}
}
std::vector<double> getPixelConfigAffineMatrix() {return affineMatrix_;}
void setdxdz(double dxdz) { dxdz_ = dxdz; }
double getdxdz() const { return dxdz_; }
void setdydz(double dydz) { dydz_ = dydz; }
double getdydz() const { return dydz_; }
void setOptimalZUm(double optimalZ) { optimalZ_ = optimalZ; }
double getOptimalZUm() const { return optimalZ_; }
private:
double pixelSizeUm_;
std::vector<double> affineMatrix_;
double dxdz_;
double dydz_;
double optimalZ_;
};
/**
* Encapsulates a collection (map) of user-defined pixel size presets.
*/
class PixelSizeConfigGroup : public ConfigGroupBase<PixelSizeConfiguration>
{
public:
/**
* Defines a new preset with pixel size.
*/
bool DefinePixelSize(const char* resolutionID, const char* deviceLabel, const char* propName, const char* value, double pixSizeUm)
{
PropertySetting setting(deviceLabel, propName, value);
configs_[resolutionID].addSetting(setting);
if (configs_[resolutionID].getPixelSizeUm() == 0.0)
{
// this is the first setting, so it is OK to set pixel size
configs_[resolutionID].setPixelSizeUm(pixSizeUm);
return true;
}
else
{
// pixel size already set and we won't allow the change
// - this signifies a conflict in the configuration
return false;
}
}
};