OptionalPropertyFactory.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package
19  * @author
20  * @date
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
26 #include <QComboBox>
27 
28 using namespace armarx;
29 
31 {
32  QList<OptionalEdit*> editors = editorToProperty.keys();
33  QListIterator<OptionalEdit*> it(editors);
34  while (it.hasNext())
35  {
36  delete it.next();
37  }
38 }
39 
40 void OptionalVariantFactory::setElementName(const QString& name)
41 {
42  elementName = name;
43 }
44 
46 {
47  connect(manager, SIGNAL(valueChanged(QtProperty*, const QVariant&)),
48  this, SLOT(slotPropertyChanged(QtProperty*, const QVariant&)));
49  connect(manager, SIGNAL(attributeChanged(QtProperty*, const QString&, const QVariant&)),
50  this, SLOT(slotPropertyAttributeChanged(QtProperty*, const QString&, const QVariant&)));
52 }
53 
55  QtProperty* property, QWidget* parent)
56 {
58  {
59 
60  OptionalEdit* editor = new OptionalEdit(elementName, property->propertyName(), parent);
61  auto varProp = dynamic_cast<QtVariantProperty*>(property);
62  ARMARX_CHECK_EXPRESSION(varProp) << elementName.toStdString();
63  if (varProp)
64  {
65  auto values = static_cast<OptionalVariantManager*>(manager)->attributeValue(varProp, QLatin1String("PossibleValues")).toStringList();
66 
67  editor->setPossibleValues(values);
68  }
69  editor->setValue(manager->value(property).toString());
70  editor->setPropertyEnabled(static_cast<OptionalVariantManager*>(manager)->attributeValue(property, QLatin1String("enabled")).toBool());
71  editor->setReadOnly(static_cast<OptionalVariantManager*>(manager)->attributeValue(property, QLatin1String("readOnly")).toBool());
72 
73  createdEditors[property].append(editor);
74  editorToProperty[editor] = property;
75 
76 
77  connect(editor, SIGNAL(valueChanged(QString)),
78  this, SLOT(slotSetValue(const QString&)));
79  connect(editor, SIGNAL(enabledChanged(bool)),
80  this, SLOT(slotSetEnabled(const bool&)));
81  connect(editor, SIGNAL(destroyed(QObject*)),
82  this, SLOT(slotEditorDestroyed(QObject*)));
83  return editor;
84  }
85  return QtVariantEditorFactory::createEditor(manager, property, parent);
86 }
87 
89 {
90  disconnect(manager, SIGNAL(valueChanged(QtProperty*, const QVariant&)),
91  this, SLOT(slotPropertyChanged(QtProperty*, const QVariant&)));
92  disconnect(manager, SIGNAL(attributeChanged(QtProperty*, const QString&, const QVariant&)),
93  this, SLOT(slotPropertyAttributeChanged(QtProperty*, const QString&, const QVariant&)));
95 }
96 
97 void OptionalVariantFactory::slotPropertyChanged(QtProperty* property,
98  const QVariant& value)
99 {
100  if (!createdEditors.contains(property))
101  {
102  return;
103  }
104 
105  QList<OptionalEdit*> editors = createdEditors[property];
106  QListIterator<OptionalEdit*> itEditor(editors);
107  while (itEditor.hasNext())
108  {
109  itEditor.next()->setValue(value.toString());
110  }
111 }
112 
113 void OptionalVariantFactory::slotPropertyAttributeChanged(QtProperty* property,
114  const QString& attribute, const QVariant& value)
115 {
116  if (!createdEditors.contains(property))
117  {
118  return;
119  }
120 
121  if (attribute == QLatin1String("enabled"))
122  {
123  QList<OptionalEdit*> editors = createdEditors[property];
124  QListIterator<OptionalEdit*> itEditor(editors);
125  while (itEditor.hasNext())
126  {
127  itEditor.next()->setPropertyEnabled(value.toBool());
128  }
129  }
130 
131  if (attribute == QLatin1String("readOnly"))
132  {
133  QList<OptionalEdit*> editors = createdEditors[property];
134  QListIterator<OptionalEdit*> itEditor(editors);
135  while (itEditor.hasNext())
136  {
137  itEditor.next()->setReadOnly(value.toBool());
138  }
139  }
140 }
141 
142 void OptionalVariantFactory::slotSetValue(const QString& value)
143 {
144  QObject* object = sender();
145  QMap<OptionalEdit*, QtProperty*>::ConstIterator itEditor =
146  editorToProperty.constBegin();
147  while (itEditor != editorToProperty.constEnd())
148  {
149  if (itEditor.key() == object)
150  {
151  QtProperty* property = itEditor.value();
152  OptionalVariantManager* manager = static_cast<OptionalVariantManager*>(propertyManager(property));
153  if (!manager)
154  {
155  return;
156  }
157  manager->setValue(property, value);
158  return;
159  }
160  itEditor++;
161  }
162 }
163 
164 void OptionalVariantFactory::slotSetEnabled(const bool& value)
165 {
166  QObject* object = sender();
167  QMap<OptionalEdit*, QtProperty*>::ConstIterator itEditor =
168  editorToProperty.constBegin();
169  while (itEditor != editorToProperty.constEnd())
170  {
171  if (itEditor.key() == object)
172  {
173  QtProperty* property = itEditor.value();
174  OptionalVariantManager* manager = static_cast<OptionalVariantManager*>(propertyManager(property));
175  if (!manager)
176  {
177  return;
178  }
179  manager->setAttribute(property, QLatin1String("enabled"), value);
180  return;
181  }
182  itEditor++;
183  }
184 }
185 
186 void OptionalVariantFactory::slotEditorDestroyed(QObject* object)
187 {
188  QMap<OptionalEdit*, QtProperty*>::ConstIterator itEditor =
189  editorToProperty.constBegin();
190  while (itEditor != editorToProperty.constEnd())
191  {
192  if (itEditor.key() == object)
193  {
194  OptionalEdit* editor = itEditor.key();
195  QtProperty* property = itEditor.value();
196  editorToProperty.remove(editor);
197  createdEditors[property].removeAll(editor);
198  if (createdEditors[property].isEmpty())
199  {
200  createdEditors.remove(property);
201  }
202  return;
203  }
204  itEditor++;
205  }
206 }
OptionalVariantManager::setAttribute
void setAttribute(QtProperty *property, const QString &attribute, const QVariant &value) override
Definition: OptionalPropertyManager.cpp:185
QtVariantEditorFactory::createEditor
QWidget * createEditor(QtVariantPropertyManager *manager, QtProperty *property, QWidget *parent) override
Definition: qtvariantproperty.cpp:2717
OptionalVariantFactory::connectPropertyManager
void connectPropertyManager(QtVariantPropertyManager *manager) override
Definition: OptionalPropertyFactory.cpp:45
OptionalPropertyFactory.h
armarx::OptionalEdit
Definition: OptionalEdit.h:58
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
QtVariantPropertyManager
The QtVariantPropertyManager class provides and manages QVariant based properties.
Definition: qtvariantproperty.h:75
OptionalVariantFactory::setElementName
void setElementName(const QString &name)
Definition: OptionalPropertyFactory.cpp:40
QtProperty
The QtProperty class encapsulates an instance of a property.
Definition: qtpropertybrowser.h:71
OptionalVariantManager
Definition: OptionalPropertyManager.h:29
QtVariantProperty
The QtVariantProperty class is a convenience class handling QVariant based properties.
Definition: qtvariantproperty.h:55
QtProperty::propertyName
QString propertyName() const
Definition: qtpropertybrowser.cpp:247
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
QtVariantPropertyManager::value
virtual QVariant value(const QtProperty *property) const
Definition: qtvariantproperty.cpp:1433
armarx::OptionalEdit::setValue
void setValue(const QString &value)
Definition: OptionalEdit.cpp:119
OptionalVariantManager::attributeValue
QVariant attributeValue(const QtProperty *property, const QString &attribute) const override
Definition: OptionalPropertyManager.cpp:128
armarx::OptionalEdit::setPossibleValues
void setPossibleValues(const QStringList &values)
Definition: OptionalEdit.cpp:86
ExpressionException.h
OptionalVariantFactory::~OptionalVariantFactory
~OptionalVariantFactory() override
Definition: OptionalPropertyFactory.cpp:30
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
QtVariantPropertyManager::propertyType
int propertyType(const QtProperty *property) const
Definition: qtvariantproperty.cpp:1570
QtVariantEditorFactory::connectPropertyManager
void connectPropertyManager(QtVariantPropertyManager *manager) override
Definition: qtvariantproperty.cpp:2525
OptionalVariantFactory::disconnectPropertyManager
void disconnectPropertyManager(QtVariantPropertyManager *manager) override
Definition: OptionalPropertyFactory.cpp:88
armarx::OptionalEdit::setReadOnly
void setReadOnly(bool readOnly)
Definition: OptionalEdit.cpp:110
armarx::OptionalEdit::setPropertyEnabled
void setPropertyEnabled(const bool &enabled=true)
Definition: OptionalEdit.cpp:99
OptionalVariantManager::optionalProprtyTypeId
static int optionalProprtyTypeId()
Definition: OptionalPropertyManager.cpp:72
QtVariantEditorFactory::disconnectPropertyManager
void disconnectPropertyManager(QtVariantPropertyManager *manager) override
Definition: qtvariantproperty.cpp:2736
OptionalVariantManager::setValue
void setValue(QtProperty *property, const QVariant &val) override
Definition: OptionalPropertyManager.cpp:162
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
OptionalVariantFactory::createEditor
QWidget * createEditor(QtVariantPropertyManager *manager, QtProperty *property, QWidget *parent) override
Definition: OptionalPropertyFactory.cpp:54