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 */
25
26#include <QComboBox>
27
29
30using namespace armarx;
31
33{
34 QList<OptionalEdit*> editors = editorToProperty.keys();
35 QListIterator<OptionalEdit*> it(editors);
36 while (it.hasNext())
37 {
38 delete it.next();
39 }
40}
41
42void
44{
45 elementName = name;
46}
47
48void
50{
51 connect(manager,
52 SIGNAL(valueChanged(QtProperty*, const QVariant&)),
53 this,
54 SLOT(slotPropertyChanged(QtProperty*, const QVariant&)));
55 connect(manager,
56 SIGNAL(attributeChanged(QtProperty*, const QString&, const QVariant&)),
57 this,
58 SLOT(slotPropertyAttributeChanged(QtProperty*, const QString&, const QVariant&)));
60}
61
62QWidget*
64 QtProperty* property,
65 QWidget* parent)
66{
68 {
69
70 OptionalEdit* editor = new OptionalEdit(elementName, property->propertyName(), parent);
71 auto varProp = dynamic_cast<QtVariantProperty*>(property);
72 ARMARX_CHECK_EXPRESSION(varProp) << elementName.toStdString();
73 if (varProp)
74 {
75 auto values = static_cast<OptionalVariantManager*>(manager)
76 ->attributeValue(varProp, QLatin1String("PossibleValues"))
77 .toStringList();
78
79 editor->setPossibleValues(values);
80 }
81 editor->setValue(manager->value(property).toString());
82 editor->setPropertyEnabled(static_cast<OptionalVariantManager*>(manager)
83 ->attributeValue(property, QLatin1String("enabled"))
84 .toBool());
85 editor->setReadOnly(static_cast<OptionalVariantManager*>(manager)
86 ->attributeValue(property, QLatin1String("readOnly"))
87 .toBool());
88
89 createdEditors[property].append(editor);
90 editorToProperty[editor] = property;
91
92
93 connect(editor, SIGNAL(valueChanged(QString)), this, SLOT(slotSetValue(const QString&)));
94 connect(editor, SIGNAL(enabledChanged(bool)), this, SLOT(slotSetEnabled(const bool&)));
95 connect(editor, SIGNAL(destroyed(QObject*)), this, SLOT(slotEditorDestroyed(QObject*)));
96 return editor;
97 }
98 return QtVariantEditorFactory::createEditor(manager, property, parent);
99}
100
101void
103{
104 disconnect(manager,
105 SIGNAL(valueChanged(QtProperty*, const QVariant&)),
106 this,
107 SLOT(slotPropertyChanged(QtProperty*, const QVariant&)));
108 disconnect(manager,
109 SIGNAL(attributeChanged(QtProperty*, const QString&, const QVariant&)),
110 this,
111 SLOT(slotPropertyAttributeChanged(QtProperty*, const QString&, const QVariant&)));
113}
114
115void
116OptionalVariantFactory::slotPropertyChanged(QtProperty* property, const QVariant& value)
117{
118 if (!createdEditors.contains(property))
119 {
120 return;
121 }
122
123 QList<OptionalEdit*> editors = createdEditors[property];
124 QListIterator<OptionalEdit*> itEditor(editors);
125 while (itEditor.hasNext())
126 {
127 itEditor.next()->setValue(value.toString());
128 }
129}
130
131void
132OptionalVariantFactory::slotPropertyAttributeChanged(QtProperty* property,
133 const QString& attribute,
134 const QVariant& value)
135{
136 if (!createdEditors.contains(property))
137 {
138 return;
139 }
140
141 if (attribute == QLatin1String("enabled"))
142 {
143 QList<OptionalEdit*> editors = createdEditors[property];
144 QListIterator<OptionalEdit*> itEditor(editors);
145 while (itEditor.hasNext())
146 {
147 itEditor.next()->setPropertyEnabled(value.toBool());
148 }
149 }
150
151 if (attribute == QLatin1String("readOnly"))
152 {
153 QList<OptionalEdit*> editors = createdEditors[property];
154 QListIterator<OptionalEdit*> itEditor(editors);
155 while (itEditor.hasNext())
156 {
157 itEditor.next()->setReadOnly(value.toBool());
158 }
159 }
160}
161
162void
163OptionalVariantFactory::slotSetValue(const QString& value)
164{
165 QObject* object = sender();
166 QMap<OptionalEdit*, QtProperty*>::ConstIterator itEditor = editorToProperty.constBegin();
167 while (itEditor != editorToProperty.constEnd())
168 {
169 if (itEditor.key() == object)
170 {
171 QtProperty* property = itEditor.value();
172 OptionalVariantManager* manager =
173 static_cast<OptionalVariantManager*>(propertyManager(property));
174 if (!manager)
175 {
176 return;
177 }
178 manager->setValue(property, value);
179 return;
180 }
181 itEditor++;
182 }
183}
184
185void
186OptionalVariantFactory::slotSetEnabled(const bool& value)
187{
188 QObject* object = sender();
189 QMap<OptionalEdit*, QtProperty*>::ConstIterator itEditor = editorToProperty.constBegin();
190 while (itEditor != editorToProperty.constEnd())
191 {
192 if (itEditor.key() == object)
193 {
194 QtProperty* property = itEditor.value();
195 OptionalVariantManager* manager =
196 static_cast<OptionalVariantManager*>(propertyManager(property));
197 if (!manager)
198 {
199 return;
200 }
201 manager->setAttribute(property, QLatin1String("enabled"), value);
202 return;
203 }
204 itEditor++;
205 }
206}
207
208void
209OptionalVariantFactory::slotEditorDestroyed(QObject* object)
210{
211 QMap<OptionalEdit*, QtProperty*>::ConstIterator itEditor = editorToProperty.constBegin();
212 while (itEditor != editorToProperty.constEnd())
213 {
214 if (itEditor.key() == object)
215 {
216 OptionalEdit* editor = itEditor.key();
217 QtProperty* property = itEditor.value();
218 editorToProperty.remove(editor);
219 createdEditors[property].removeAll(editor);
220 if (createdEditors[property].isEmpty())
221 {
222 createdEditors.remove(property);
223 }
224 return;
225 }
226 itEditor++;
227 }
228}
void connectPropertyManager(QtVariantPropertyManager *manager) override
void disconnectPropertyManager(QtVariantPropertyManager *manager) override
QWidget * createEditor(QtVariantPropertyManager *manager, QtProperty *property, QWidget *parent) override
void setElementName(const QString &name)
void setValue(QtProperty *property, const QVariant &val) override
void setAttribute(QtProperty *property, const QString &attribute, const QVariant &value) override
QtVariantPropertyManager * propertyManager(QtProperty *property) const
The QtProperty class encapsulates an instance of a property.
QString propertyName() const
void connectPropertyManager(QtVariantPropertyManager *manager) override
void disconnectPropertyManager(QtVariantPropertyManager *manager) override
QWidget * createEditor(QtVariantPropertyManager *manager, QtProperty *property, QWidget *parent) override
The QtVariantPropertyManager class provides and manages QVariant based properties.
virtual QVariant value(const QtProperty *property) const
int propertyType(const QtProperty *property) const
The QtVariantProperty class is a convenience class handling QVariant based properties.
void setPossibleValues(const QStringList &values)
void setValue(const QString &value)
void setPropertyEnabled(const bool &enabled=true)
void setReadOnly(bool readOnly)
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::shared_ptr< Value > value()
Definition cxxopts.hpp:855