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 };
35 Q_DECLARE_METATYPE(OptionalPropertyType)
36 
37 static QIcon
38 drawCheckBox(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 
75 int
77 {
78  return qMetaTypeId<OptionalPropertyType>();
79 }
80 
81 bool
83 {
85  {
86  return true;
87  }
89 }
90 
91 int
92 OptionalVariantManager::valueType(int propertyType) const
93 {
95  {
96  return QVariant::String;
97  }
99 }
100 
101 QVariant
103 {
104  if (dataMap.contains(property))
105  {
106  return dataMap[property]["value"].variant;
107  }
108  return QtVariantPropertyManager::value(property);
109 }
110 
111 QStringList
112 OptionalVariantManager::attributes(int propertyType) const
113 {
115  {
116  QStringList attr;
117  attr << QLatin1String("enabled");
118  return attr;
119  }
121 }
122 
123 int
124 OptionalVariantManager::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 
137 QVariant
138 OptionalVariantManager::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 
147 QString
149 {
150  if (dataMap.contains(property))
151  {
152  return dataMap[property]["value"].variant.toString();
153  }
154  return QtVariantPropertyManager::valueText(property);
155 }
156 
157 QIcon
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  }
173  return QtVariantPropertyManager::valueIcon(property);
174 }
175 
176 void
177 OptionalVariantManager::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  }
197  QtVariantPropertyManager::setValue(property, val);
198 }
199 
200 void
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 
237 void
239 {
240  if (propertyType(property) == optionalProprtyTypeId())
241  {
242  dataMap[property];
243  }
245 }
246 
247 void
249 {
250  dataMap.remove(property);
252 }
QtVariantPropertyManager::uninitializeProperty
void uninitializeProperty(QtProperty *property) override
Definition: qtvariantproperty.cpp:2606
OptionalVariantManager::setAttribute
void setAttribute(QtProperty *property, const QString &attribute, const QVariant &value) override
Definition: OptionalPropertyManager.cpp:201
QtVariantPropertyManager::initializeProperty
void initializeProperty(QtProperty *property) override
Definition: qtvariantproperty.cpp:2561
str
std::string str(const T &t)
Definition: UserAssistedSegmenterGuiWidgetController.cpp:43
QtVariantPropertyManager::setAttribute
virtual void setAttribute(QtProperty *property, const QString &attribute, const QVariant &value)
Definition: qtvariantproperty.cpp:2303
OptionalVariantManager::OptionalVariantManager
OptionalVariantManager(QObject *parent=0)
Definition: OptionalPropertyManager.cpp:68
QtVariantPropertyManager::valueChanged
void valueChanged(QtProperty *property, const QVariant &val)
OptionalVariantManager::isPropertyTypeSupported
bool isPropertyTypeSupported(int propertyType) const override
Definition: OptionalPropertyManager.cpp:82
OptionalVariantManager::value
QVariant value(const QtProperty *property) const override
Definition: OptionalPropertyManager.cpp:102
OptionalVariantManager::valueIcon
QIcon valueIcon(const QtProperty *property) const override
Definition: OptionalPropertyManager.cpp:158
armarx::VariantType::Bool
const VariantTypeId Bool
Definition: Variant.h:916
QtVariantPropertyManager
The QtVariantPropertyManager class provides and manages QVariant based properties.
Definition: qtvariantproperty.h:78
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:124
QtVariantPropertyManager::setValue
virtual void setValue(QtProperty *property, const QVariant &val)
Definition: qtvariantproperty.cpp:2143
QtVariantPropertyManager::attributes
virtual QStringList attributes(int propertyType) const
Definition: qtvariantproperty.cpp:2085
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
OptionalVariantManager::valueText
QString valueText(const QtProperty *property) const override
Definition: OptionalPropertyManager.cpp:148
QtVariantPropertyManager::attributeChanged
void attributeChanged(QtProperty *property, const QString &attribute, const QVariant &val)
QtVariantPropertyManager::value
virtual QVariant value(const QtProperty *property) const
Definition: qtvariantproperty.cpp:1682
QtVariantPropertyManager::isPropertyTypeSupported
virtual bool isPropertyTypeSupported(int propertyType) const
Definition: qtvariantproperty.cpp:1626
enabled
std::atomic< bool > * enabled
Definition: RemoteGuiWidgetController.cpp:75
QtVariantPropertyManager::valueText
QString valueText(const QtProperty *property) const override
Definition: qtvariantproperty.cpp:2539
OptionalPropertyType
Definition: OptionalPropertyManager.cpp:32
OptionalVariantManager::initializeProperty
void initializeProperty(QtProperty *property) override
Definition: OptionalPropertyManager.cpp:238
OptionalVariantManager::valueType
int valueType(int propertyType) const override
Definition: OptionalPropertyManager.cpp:92
OptionalVariantManager::attributeValue
QVariant attributeValue(const QtProperty *property, const QString &attribute) const override
Definition: OptionalPropertyManager.cpp:138
QtVariantPropertyManager::attributeValue
virtual QVariant attributeValue(const QtProperty *property, const QString &attribute) const
Definition: qtvariantproperty.cpp:1853
QtVariantPropertyManager::propertyType
int propertyType(const QtProperty *property) const
Definition: qtvariantproperty.cpp:1829
QtVariantPropertyManager::attributeType
virtual int attributeType(int propertyType, const QString &attribute) const
Definition: qtvariantproperty.cpp:2109
QtAbstractPropertyManager::propertyChanged
void propertyChanged(QtProperty *property)
OptionalVariantManager::attributes
QStringList attributes(int propertyType) const override
Definition: OptionalPropertyManager.cpp:112
QtVariantPropertyManager::valueType
int valueType(const QtProperty *property) const
Definition: qtvariantproperty.cpp:1801
cxxopts::String
std::string String
Definition: cxxopts.hpp:192
armarx::transparent
QColor transparent()
Definition: StyleSheets.h:90
OptionalVariantManager::optionalProprtyTypeId
static int optionalProprtyTypeId()
Definition: OptionalPropertyManager.cpp:76
QtVariantPropertyManager::valueIcon
QIcon valueIcon(const QtProperty *property) const override
Definition: qtvariantproperty.cpp:2551
OptionalVariantManager::setValue
void setValue(QtProperty *property, const QVariant &val) override
Definition: OptionalPropertyManager.cpp:177
OptionalPropertyManager.h
OptionalVariantManager::uninitializeProperty
void uninitializeProperty(QtProperty *property) override
Definition: OptionalPropertyManager.cpp:248
qtpropertymanager.h