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 
27 #include <QStyleOptionButton>
28 #include <QApplication>
29 #include <QPainter>
30 
32 {
33 };
34 Q_DECLARE_METATYPE(OptionalPropertyType)
35 
36 static QIcon drawCheckBox(bool value)
37 {
38  QStyleOptionButton opt;
39  opt.state |= value ? QStyle::State_On : QStyle::State_Off;
40  opt.state |= QStyle::State_Enabled;
41  const QStyle* style = QApplication::style();
42  // Figure out size of an indicator and make sure it is not scaled down in a list view item
43  // by making the pixmap as big as a list view icon and centering the indicator in it.
44  // (if it is smaller, it can't be helped)
45  const int indicatorWidth = style->pixelMetric(QStyle::PM_IndicatorWidth, &opt);
46  const int indicatorHeight = style->pixelMetric(QStyle::PM_IndicatorHeight, &opt);
47  const int listViewIconSize = indicatorWidth;
48  const int pixmapWidth = indicatorWidth;
49  const int pixmapHeight = qMax(indicatorHeight, listViewIconSize);
50 
51  opt.rect = QRect(0, 0, indicatorWidth, indicatorHeight);
52  QPixmap pixmap = QPixmap(pixmapWidth, pixmapHeight);
53  pixmap.fill(Qt::transparent);
54  {
55  // Center?
56  const int xoff = (pixmapWidth > indicatorWidth) ? (pixmapWidth - indicatorWidth) / 2 : 0;
57  const int yoff = (pixmapHeight > indicatorHeight) ? (pixmapHeight - indicatorHeight) / 2 : 0;
58  QPainter painter(&pixmap);
59  painter.translate(xoff, yoff);
60  style->drawPrimitive(QStyle::PE_IndicatorCheckBox, &opt, &painter);
61  }
62  return QIcon(pixmap);
63 }
64 
65 
67  : QtVariantPropertyManager(parent), checkedIcon(drawCheckBox(true)), uncheckedIcon(drawCheckBox(false))
68 {
69 
70 }
71 
73 {
74  return qMetaTypeId<OptionalPropertyType>();
75 }
76 
78 {
80  {
81  return true;
82  }
84 }
85 
86 int OptionalVariantManager::valueType(int propertyType) const
87 {
89  {
90  return QVariant::String;
91  }
93 }
94 
95 QVariant OptionalVariantManager::value(const QtProperty* property) const
96 {
97  if (dataMap.contains(property))
98  {
99  return dataMap[property]["value"].variant;
100  }
101  return QtVariantPropertyManager::value(property);
102 }
103 
104 QStringList OptionalVariantManager::attributes(int propertyType) const
105 {
107  {
108  QStringList attr;
109  attr << QLatin1String("enabled");
110  return attr;
111  }
113 }
114 
115 int OptionalVariantManager::attributeType(int propertyType, const QString& attribute) const
116 {
118  {
119  if (attribute == QLatin1String("enabled"))
120  {
121  return QVariant::Bool;
122  }
123  return 0;
124  }
126 }
127 
128 QVariant OptionalVariantManager::attributeValue(const QtProperty* property, const QString& attribute) const
129 {
130  if (dataMap.contains(property))
131  {
132  return dataMap[property][attribute].variant;
133  }
134  return QtVariantPropertyManager::attributeValue(property, attribute);
135 }
136 
137 QString OptionalVariantManager::valueText(const QtProperty* property) const
138 {
139  if (dataMap.contains(property))
140  {
141  return dataMap[property]["value"].variant.toString();
142  }
143  return QtVariantPropertyManager::valueText(property);
144 }
145 
146 QIcon OptionalVariantManager::valueIcon(const QtProperty* property) const
147 {
148  if (dataMap.contains(property))
149  {
150  auto it = dataMap.constFind(property);
151 
152  if (it == dataMap.constEnd())
153  {
154  return QIcon();
155  }
156 
157  return (it.value()["enabled"].variant.isValid() && it.value()["enabled"].variant.toBool()) ? checkedIcon : uncheckedIcon;
158  }
159  return QtVariantPropertyManager::valueIcon(property);
160 }
161 
162 void OptionalVariantManager::setValue(QtProperty* property, const QVariant& val)
163 {
164  if (dataMap.contains(property))
165  {
166  if (val.type() != QVariant::String && !val.canConvert(QVariant::String))
167  {
168  return;
169  }
170  QString str = val.value<QString>();
171  Data d = dataMap[property]["value"];
172  if (d.variant.isValid() && d.variant.toString() == str)
173  {
174  return;
175  }
176  d.variant = str;
177  dataMap[property]["value"] = d;
178  emit propertyChanged(property);
179  emit valueChanged(property, str);
180  return;
181  }
182  QtVariantPropertyManager::setValue(property, val);
183 }
184 
186  const QString& attribute, const QVariant& val)
187 {
188  if (dataMap.contains(property))
189  {
190  // special legacy case
191  if (attribute == QLatin1String("enabled"))
192  {
193  if (val.type() != QVariant::Bool && !val.canConvert(QVariant::Bool))
194  {
195  return;
196  }
197  bool enabled = val.value<bool>();
198  Data d = dataMap[property]["enabled"];
199  if (d.variant.isValid() && d.variant.toBool() == enabled)
200  {
201  return;
202  }
203  dataMap[property]["enabled"].variant = val;
204  emit propertyChanged(property);
205  emit attributeChanged(property, attribute, enabled);
206  }
207  else
208  {
209  dataMap[property][attribute].variant = val;
210  emit propertyChanged(property);
211  emit attributeChanged(property, attribute, val);
212  }
213  return;
214  }
215 
216 
217  QtVariantPropertyManager::setAttribute(property, attribute, val);
218 }
219 
221 {
222  if (propertyType(property) == optionalProprtyTypeId())
223  {
224  dataMap[property];
225  }
227 }
228 
230 {
231  dataMap.remove(property);
233 }
QtVariantPropertyManager::uninitializeProperty
void uninitializeProperty(QtProperty *property) override
Definition: qtvariantproperty.cpp:2319
OptionalVariantManager::setAttribute
void setAttribute(QtProperty *property, const QString &attribute, const QVariant &value) override
Definition: OptionalPropertyManager.cpp:185
QtVariantPropertyManager::initializeProperty
void initializeProperty(QtProperty *property) override
Definition: qtvariantproperty.cpp:2276
str
std::string str(const T &t)
Definition: UserAssistedSegmenterGuiWidgetController.cpp:42
QtVariantPropertyManager::setAttribute
virtual void setAttribute(QtProperty *property, const QString &attribute, const QVariant &value)
Definition: qtvariantproperty.cpp:2028
OptionalVariantManager::OptionalVariantManager
OptionalVariantManager(QObject *parent=0)
Definition: OptionalPropertyManager.cpp:66
QtVariantPropertyManager::valueChanged
void valueChanged(QtProperty *property, const QVariant &val)
OptionalVariantManager::isPropertyTypeSupported
bool isPropertyTypeSupported(int propertyType) const override
Definition: OptionalPropertyManager.cpp:77
OptionalVariantManager::value
QVariant value(const QtProperty *property) const override
Definition: OptionalPropertyManager.cpp:95
OptionalVariantManager::valueIcon
QIcon valueIcon(const QtProperty *property) const override
Definition: OptionalPropertyManager.cpp:146
armarx::VariantType::Bool
const VariantTypeId Bool
Definition: Variant.h:915
QtVariantPropertyManager
The QtVariantPropertyManager class provides and manages QVariant based properties.
Definition: qtvariantproperty.h:75
QtProperty
The QtProperty class encapsulates an instance of a property.
Definition: qtpropertybrowser.h:71
OptionalVariantManager::attributeType
int attributeType(int propertyType, const QString &attribute) const override
Definition: OptionalPropertyManager.cpp:115
QtVariantPropertyManager::setValue
virtual void setValue(QtProperty *property, const QVariant &val)
Definition: qtvariantproperty.cpp:1876
QtVariantPropertyManager::attributes
virtual QStringList attributes(int propertyType) const
Definition: qtvariantproperty.cpp:1820
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
OptionalVariantManager::valueText
QString valueText(const QtProperty *property) const override
Definition: OptionalPropertyManager.cpp:137
QtVariantPropertyManager::attributeChanged
void attributeChanged(QtProperty *property, const QString &attribute, const QVariant &val)
QtVariantPropertyManager::value
virtual QVariant value(const QtProperty *property) const
Definition: qtvariantproperty.cpp:1433
QtVariantPropertyManager::isPropertyTypeSupported
virtual bool isPropertyTypeSupported(int propertyType) const
Definition: qtvariantproperty.cpp:1379
enabled
std::atomic< bool > * enabled
Definition: RemoteGuiWidgetController.cpp:75
QtVariantPropertyManager::valueText
QString valueText(const QtProperty *property) const override
Definition: qtvariantproperty.cpp:2258
OptionalPropertyType
Definition: OptionalPropertyManager.cpp:31
OptionalVariantManager::initializeProperty
void initializeProperty(QtProperty *property) override
Definition: OptionalPropertyManager.cpp:220
OptionalVariantManager::valueType
int valueType(int propertyType) const override
Definition: OptionalPropertyManager.cpp:86
OptionalVariantManager::attributeValue
QVariant attributeValue(const QtProperty *property, const QString &attribute) const override
Definition: OptionalPropertyManager.cpp:128
QtVariantPropertyManager::attributeValue
virtual QVariant attributeValue(const QtProperty *property, const QString &attribute) const
Definition: qtvariantproperty.cpp:1592
QtVariantPropertyManager::propertyType
int propertyType(const QtProperty *property) const
Definition: qtvariantproperty.cpp:1570
QtVariantPropertyManager::attributeType
virtual int attributeType(int propertyType, const QString &attribute) const
Definition: qtvariantproperty.cpp:1843
QtAbstractPropertyManager::propertyChanged
void propertyChanged(QtProperty *property)
OptionalVariantManager::attributes
QStringList attributes(int propertyType) const override
Definition: OptionalPropertyManager.cpp:104
QtVariantPropertyManager::valueType
int valueType(const QtProperty *property) const
Definition: qtvariantproperty.cpp:1544
cxxopts::String
std::string String
Definition: cxxopts.hpp:209
armarx::transparent
QColor transparent()
Definition: StyleSheets.h:84
OptionalVariantManager::optionalProprtyTypeId
static int optionalProprtyTypeId()
Definition: OptionalPropertyManager.cpp:72
QtVariantPropertyManager::valueIcon
QIcon valueIcon(const QtProperty *property) const override
Definition: qtvariantproperty.cpp:2267
OptionalVariantManager::setValue
void setValue(QtProperty *property, const QVariant &val) override
Definition: OptionalPropertyManager.cpp:162
OptionalPropertyManager.h
OptionalVariantManager::uninitializeProperty
void uninitializeProperty(QtProperty *property) override
Definition: OptionalPropertyManager.cpp:229
qtpropertymanager.h