OptionalPropertyManager.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 <QApplication>
27#include <QPainter>
28#include <QStyleOptionButton>
29
31
33{
34};
35Q_DECLARE_METATYPE(OptionalPropertyType)
36
37static QIcon
38drawCheckBox(bool value)
39{
40 QStyleOptionButton opt;
41 opt.state |= value ? QStyle::State_On : QStyle::State_Off;
42 opt.state |= QStyle::State_Enabled;
43 const QStyle* style = QApplication::style();
44 // Figure out size of an indicator and make sure it is not scaled down in a list view item
45 // by making the pixmap as big as a list view icon and centering the indicator in it.
46 // (if it is smaller, it can't be helped)
47 const int indicatorWidth = style->pixelMetric(QStyle::PM_IndicatorWidth, &opt);
48 const int indicatorHeight = style->pixelMetric(QStyle::PM_IndicatorHeight, &opt);
49 const int listViewIconSize = indicatorWidth;
50 const int pixmapWidth = indicatorWidth;
51 const int pixmapHeight = qMax(indicatorHeight, listViewIconSize);
52
53 opt.rect = QRect(0, 0, indicatorWidth, indicatorHeight);
54 QPixmap pixmap = QPixmap(pixmapWidth, pixmapHeight);
55 pixmap.fill(Qt::transparent);
56 {
57 // Center?
58 const int xoff = (pixmapWidth > indicatorWidth) ? (pixmapWidth - indicatorWidth) / 2 : 0;
59 const int yoff =
60 (pixmapHeight > indicatorHeight) ? (pixmapHeight - indicatorHeight) / 2 : 0;
61 QPainter painter(&pixmap);
62 painter.translate(xoff, yoff);
63 style->drawPrimitive(QStyle::PE_IndicatorCheckBox, &opt, &painter);
64 }
65 return QIcon(pixmap);
66}
67
70 checkedIcon(drawCheckBox(true)),
71 uncheckedIcon(drawCheckBox(false))
72{
73}
74
75int
77{
78 return qMetaTypeId<OptionalPropertyType>();
79}
80
81bool
90
91int
93{
95 {
96 return QVariant::String;
97 }
99}
100
101QVariant
103{
104 if (dataMap.contains(property))
105 {
106 return dataMap[property]["value"].variant;
107 }
108 return QtVariantPropertyManager::value(property);
109}
110
111QStringList
113{
115 {
116 QStringList attr;
117 attr << QLatin1String("enabled");
118 return attr;
119 }
121}
122
123int
124OptionalVariantManager::attributeType(int propertyType, const QString& attribute) const
125{
127 {
128 if (attribute == QLatin1String("enabled"))
129 {
130 return QVariant::Bool;
131 }
132 return 0;
133 }
135}
136
137QVariant
138OptionalVariantManager::attributeValue(const QtProperty* property, const QString& attribute) const
139{
140 if (dataMap.contains(property))
141 {
142 return dataMap[property][attribute].variant;
143 }
144 return QtVariantPropertyManager::attributeValue(property, attribute);
145}
146
147QString
149{
150 if (dataMap.contains(property))
151 {
152 return dataMap[property]["value"].variant.toString();
153 }
155}
156
157QIcon
159{
160 if (dataMap.contains(property))
161 {
162 auto it = dataMap.constFind(property);
163
164 if (it == dataMap.constEnd())
165 {
166 return QIcon();
167 }
168
169 return (it.value()["enabled"].variant.isValid() && it.value()["enabled"].variant.toBool())
170 ? checkedIcon
171 : uncheckedIcon;
172 }
174}
175
176void
177OptionalVariantManager::setValue(QtProperty* property, const QVariant& val)
178{
179 if (dataMap.contains(property))
180 {
181 if (val.type() != QVariant::String && !val.canConvert(QVariant::String))
182 {
183 return;
184 }
185 QString str = val.value<QString>();
186 Data d = dataMap[property]["value"];
187 if (d.variant.isValid() && d.variant.toString() == str)
188 {
189 return;
190 }
191 d.variant = str;
192 dataMap[property]["value"] = d;
193 emit propertyChanged(property);
194 emit valueChanged(property, str);
195 return;
196 }
198}
199
200void
202 const QString& attribute,
203 const QVariant& val)
204{
205 if (dataMap.contains(property))
206 {
207 // special legacy case
208 if (attribute == QLatin1String("enabled"))
209 {
210 if (val.type() != QVariant::Bool && !val.canConvert(QVariant::Bool))
211 {
212 return;
213 }
214 bool enabled = val.value<bool>();
215 Data d = dataMap[property]["enabled"];
216 if (d.variant.isValid() && d.variant.toBool() == enabled)
217 {
218 return;
219 }
220 dataMap[property]["enabled"].variant = val;
221 emit propertyChanged(property);
222 emit attributeChanged(property, attribute, enabled);
223 }
224 else
225 {
226 dataMap[property][attribute].variant = val;
227 emit propertyChanged(property);
228 emit attributeChanged(property, attribute, val);
229 }
230 return;
231 }
232
233
234 QtVariantPropertyManager::setAttribute(property, attribute, val);
235}
236
237void
239{
240 if (propertyType(property) == optionalProprtyTypeId())
241 {
242 dataMap[property];
243 }
245}
246
247void
std::string str(const T &t)
void initializeProperty(QtProperty *property) override
QVariant attributeValue(const QtProperty *property, const QString &attribute) const override
void setValue(QtProperty *property, const QVariant &val) override
int attributeType(int propertyType, const QString &attribute) const override
QVariant value(const QtProperty *property) const override
OptionalVariantManager(QObject *parent=0)
int valueType(int propertyType) const override
QIcon valueIcon(const QtProperty *property) const override
QStringList attributes(int propertyType) const override
void uninitializeProperty(QtProperty *property) override
void setAttribute(QtProperty *property, const QString &attribute, const QVariant &value) override
QString valueText(const QtProperty *property) const override
bool isPropertyTypeSupported(int propertyType) const override
void propertyChanged(QtProperty *property)
void initializeProperty(QtProperty *property) override
virtual QVariant value(const QtProperty *property) const
QtVariantPropertyManager(QObject *parent=0)
virtual int attributeType(int propertyType, const QString &attribute) const
virtual QVariant attributeValue(const QtProperty *property, const QString &attribute) const
virtual void setValue(QtProperty *property, const QVariant &val)
QIcon valueIcon(const QtProperty *property) const override
void valueChanged(QtProperty *property, const QVariant &val)
void uninitializeProperty(QtProperty *property) override
virtual void setAttribute(QtProperty *property, const QString &attribute, const QVariant &value)
virtual bool isPropertyTypeSupported(int propertyType) const
virtual QStringList attributes(int propertyType) const
int propertyType(const QtProperty *property) const
QString valueText(const QtProperty *property) const override
int valueType(const QtProperty *property) const
void attributeChanged(QtProperty *property, const QString &attribute, const QVariant &val)