Medical Imaging Interaction Toolkit  2018.4.99-a28b433c
Medical Imaging Interaction Toolkit
berryRadioState.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "berryRadioState.h"
14 
15 #include "berryObjectString.h"
16 #include "berryObjectStringMap.h"
17 
18 #include "berryIPreferences.h"
19 
20 namespace berry {
21 
22 const QString RadioState::STATE_ID = "org.blueberry.ui.commands.radioState";
23 const QString RadioState::PARAMETER_ID = "org.blueberry.ui.commands.radioStateParameter";
24 
26 {
27  SetShouldPersist(true);
28 }
29 
31  const QString& /*propertyName*/, const Object::Pointer& data)
32 {
33  bool shouldPersist = true; // persist by default
34  if (data.Cast<ObjectString>())
35  {
36  SetValue(data);
37  }
38  else if (ObjectStringMap::Pointer parameters = data.Cast<ObjectStringMap>())
39  {
40  const QString defaultString = parameters->value("default");
41  if (!defaultString.isNull())
42  {
43  Object::Pointer value(new ObjectString(defaultString));
44  SetValue(value);
45  }
46 
47  const QString persistedString = parameters->value("persisted");
48  if (!persistedString.isNull()
49  && persistedString.compare("false", Qt::CaseInsensitive) == 0)
50  shouldPersist = false;
51  }
52  SetShouldPersist(shouldPersist);
53 }
54 
55 void RadioState::Load(const SmartPointer<IPreferences>& store, const QString& preferenceKey)
56 {
57  if (!ShouldPersist())
58  return;
59 
60  const QString prefValue = store->Get(preferenceKey, QString());
61  if (!prefValue.isEmpty())
62  {
63  Object::Pointer value(new ObjectString(prefValue));
64  SetValue(value);
65  }
66 }
67 
68 void RadioState::Save(const SmartPointer<IPreferences>& store, const QString& preferenceKey)
69 {
70  if (!ShouldPersist())
71  return;
72 
73  const Object::Pointer value = GetValue();
74  if (ObjectString::Pointer objStr = value.Cast<ObjectString>())
75  {
76  store->Put(preferenceKey, *objStr);
77  }
78 }
79 
81 {
82  if (!(value.Cast<ObjectString>()))
83  return; // we set only String values
84  PersistentState::SetValue(value);
85 }
86 
87 }
void SetInitializationData(const SmartPointer< IConfigurationElement > &config, const QString &propertyName, const Object::Pointer &data) override
SmartPointer< Other > Cast() const
Implements transparent reference counting.
static const QString STATE_ID
static const QString PARAMETER_ID
void SetValue(const Object::Pointer &value) override
void Load(const SmartPointer< IPreferences > &store, const QString &preferenceKey) override
void Save(const SmartPointer< IPreferences > &store, const QString &preferenceKey) override