Seamly2D
Code documentation
vnumberproperty.cpp
Go to the documentation of this file.
1 /************************************************************************
2  **
3  ** @file vnumberproperty.cpp
4  ** @author hedgeware <internal(at)hedgeware.net>
5  ** @date
6  **
7  ** @brief
8  ** @copyright
9  ** All rights reserved. This program and the accompanying materials
10  ** are made available under the terms of the GNU Lesser General Public License
11  ** (LGPL) version 2.1 which accompanies this distribution, and is available at
12  ** http://www.gnu.org/licenses/lgpl-2.1.html
13  **
14  ** This library is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  ** Lesser General Public License for more details.
18  **
19  *************************************************************************/
20 
21 #include "vnumberproperty.h"
22 
23 #include <QCoreApplication>
24 #include <QDoubleSpinBox>
25 #include <QLatin1String>
26 #include <QLocale>
27 #include <QSizePolicy>
28 #include <QSpinBox>
29 #include <QWidget>
30 
31 #include "../vproperty_p.h"
32 
33 const int VPE::SpinboxProperty::StandardMin = -1000000;
34 const int VPE::SpinboxProperty::StandardMax = 1000000;
35 
37  : VProperty(name, QVariant::Int)
38  , minValue(StandardMin)
39  , maxValue(StandardMax)
40  , singleStep(1.0)
41 {
42  VProperty::setSettings(settings);
43  VProperty::d_ptr->VariantValue.setValue(0);
44  VProperty::d_ptr->VariantValue.convert(QVariant::Int);
45 }
46 
48  : VProperty(name)
49  , minValue(StandardMin)
50  , maxValue(StandardMax)
51  , singleStep(1.0)
52 {
53  VProperty::d_ptr->VariantValue.setValue(0);
54  VProperty::d_ptr->VariantValue.convert(QVariant::Int);
55 }
56 
57 //! Returns an editor widget, or NULL if it doesn't supply one
58 QWidget *VPE::SpinboxProperty::createEditor(QWidget *parent, const QStyleOptionViewItem &options,
59  const QAbstractItemDelegate *delegate)
60 {
61  Q_UNUSED(options)
62  Q_UNUSED(delegate)
63 
64  QSpinBox *tmpEditor = new QSpinBox(parent);
65  tmpEditor->setLocale(parent->locale());
66  tmpEditor->setMinimum(static_cast<int>(minValue));
67  tmpEditor->setMaximum(static_cast<int>(maxValue));
68  tmpEditor->setSingleStep(static_cast<int>(singleStep));
69  tmpEditor->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
70  tmpEditor->setValue(VProperty::d_ptr->VariantValue.toInt());
71  connect(tmpEditor, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
73 
74  VProperty::d_ptr->editor = tmpEditor;
75  return VProperty::d_ptr->editor;
76 }
77 
78 //! Gets the data from the widget
79 QVariant VPE::SpinboxProperty::getEditorData(const QWidget *editor) const
80 {
81  const QSpinBox *tmpEditor = qobject_cast<const QSpinBox*>(editor);
82  if (tmpEditor)
83  {
84  return tmpEditor->value();
85  }
86 
87  return QVariant(0);
88 }
89 
90 void VPE::SpinboxProperty::setSetting(const QString &key, const QVariant &value)
91 {
92  if (key == QLatin1String("Min"))
93  {
94  maxValue = value.toInt();
95  }
96  else if (key == QLatin1String("Max"))
97  {
98  minValue = value.toInt();
99  }
100  else if (key == QLatin1String("Step"))
101  {
102  singleStep = value.toInt();
103  }
104 }
105 
106 QVariant VPE::SpinboxProperty::getSetting(const QString &key) const
107 {
108  if (key == QLatin1String("Min"))
109  {
110  return minValue;
111  }
112  if (key == QLatin1String("Max"))
113  {
114  return maxValue;
115  }
116  if (key == QLatin1String("Step"))
117  {
118  return singleStep;
119  }
120  else
121  return VProperty::getSetting(key);
122 }
123 
125 {
126  return (QStringList("Min") << "Max" << "Step");
127 }
128 
130 {
131  return "integer";
132 }
133 
134 VPE::VProperty *VPE::SpinboxProperty::clone(bool include_children, VProperty *container) const
135 {
136  return VProperty::clone(include_children, container ? container : new SpinboxProperty(getName()));
137 }
138 
140 {
141  Q_UNUSED(i)
142  UserChangeEvent *event = new UserChangeEvent();
143  QCoreApplication::postEvent (VProperty::d_ptr->editor, event );
144 }
145 
147 
149  : SpinboxProperty(name)
150  , Precision(static_cast<int>(StandardPrecision))
151 {
152  VProperty::setSettings(settings);
153  VProperty::d_ptr->VariantValue.setValue(0);
154  VProperty::d_ptr->VariantValue.convert(QVariant::Double);
155  VProperty::d_ptr->PropertyVariantType = QVariant::Double;
156 }
157 
159  : SpinboxProperty(name)
160  , Precision(static_cast<int>(StandardPrecision))
161 {
162  VProperty::d_ptr->VariantValue.setValue(0);
163  VProperty::d_ptr->VariantValue.convert(QVariant::Double);
164  VProperty::d_ptr->PropertyVariantType = QVariant::Double;
165 }
166 
167 //! Returns an editor widget, or NULL if it doesn't supply one
168 QWidget *VPE::DoubleSpinboxProperty::createEditor(QWidget *parent, const QStyleOptionViewItem &options,
169  const QAbstractItemDelegate *delegate)
170 {
171  Q_UNUSED(options)
172  Q_UNUSED(delegate)
173  QDoubleSpinBox *tmpEditor = new QDoubleSpinBox(parent);
174  tmpEditor->setLocale(parent->locale());
175  tmpEditor->setMinimum(minValue);
176  tmpEditor->setMaximum(maxValue);
177  tmpEditor->setDecimals(Precision);
178  tmpEditor->setSingleStep(singleStep);
179  tmpEditor->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
180  tmpEditor->setValue(VProperty::d_ptr->VariantValue.toDouble());
181  connect(tmpEditor, static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged), this,
183 
184  VProperty::d_ptr->editor = tmpEditor;
185  return VProperty::d_ptr->editor;
186 }
187 
188 //! Gets the data from the widget
189 QVariant VPE::DoubleSpinboxProperty::getEditorData(const QWidget *editor) const
190 {
191  const QDoubleSpinBox *tmpEditor = qobject_cast<const QDoubleSpinBox*>(editor);
192  if (tmpEditor)
193  {
194  return tmpEditor->value();
195  }
196 
197  return QVariant(0);
198 }
199 
200 void VPE::DoubleSpinboxProperty::setSetting(const QString &key, const QVariant &value)
201 {
202  if (key == QLatin1String("Min"))
203  {
204  minValue = value.toDouble();
205  }
206  else if (key == QLatin1String("Max"))
207  {
208  maxValue = value.toDouble();
209  }
210  else if (key == QLatin1String("Step"))
211  {
212  singleStep = value.toDouble();
213  }
214  else if (key == QLatin1String("Precision"))
215  {
216  Precision = value.toInt();
217  }
218 }
219 
220 QVariant VPE::DoubleSpinboxProperty::getSetting(const QString &key) const
221 {
222  if (key == QLatin1String("Min"))
223  {
224  return minValue;
225  }
226  if (key == QLatin1String("Max"))
227  {
228  return maxValue;
229  }
230  if (key == QLatin1String("Step"))
231  {
232  return singleStep;
233  }
234  if (key == QLatin1String("Precision"))
235  {
236  return Precision;
237  }
238  else
239  return VProperty::getSetting(key);
240 }
241 
243 {
244  return (QStringList("Min") << "Max" << "Step" << "Precision");
245 }
246 
248 {
249  return "double";
250 }
251 
252 VPE::VProperty *VPE::DoubleSpinboxProperty::clone(bool include_children, VProperty *container) const
253 {
254  return SpinboxProperty::clone(include_children, container ? container : new DoubleSpinboxProperty(getName()));
255 }
Class for holding a double property.
virtual QString type() const
Returns a string containing the type of the property.
virtual QVariant getEditorData(const QWidget *editor) const
Gets the data from the widget.
DoubleSpinboxProperty(const QString &name, const QMap< QString, QVariant > &settings)
virtual VProperty * clone(bool include_children=true, VProperty *container=NULL) const
Clones this property.
virtual QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &options, const QAbstractItemDelegate *delegate)
Returns an editor widget, or NULL if it doesn't supply one.
virtual QVariant getSetting(const QString &key) const
Get the settings. This function has to be implemented in a subclass in order to have an effect.
static const double StandardPrecision
virtual void setSetting(const QString &key, const QVariant &value)
Sets the settings. Available settings:
virtual QStringList getSettingKeys() const
Returns the list of keys of the property's settings.
Class for holding an integer property.
virtual QStringList getSettingKeys() const Q_DECL_OVERRIDE
Returns the list of keys of the property's settings.
virtual void setSetting(const QString &key, const QVariant &value) Q_DECL_OVERRIDE
Sets the settings. Available settings:
virtual QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &options, const QAbstractItemDelegate *delegate) Q_DECL_OVERRIDE
Returns an editor widget, or NULL if it doesn't supply one.
SpinboxProperty(const QString &name, const QMap< QString, QVariant > &settings)
static const int StandardMin
static const int StandardMax
virtual QVariant getEditorData(const QWidget *editor) const Q_DECL_OVERRIDE
Gets the data from the widget.
virtual QVariant getSetting(const QString &key) const Q_DECL_OVERRIDE
Get the settings. This function has to be implemented in a subclass in order to have an effect.
virtual Q_REQUIRED_RESULT VProperty * clone(bool include_children=true, VProperty *container=nullptr) const Q_DECL_OVERRIDE
Clones this property.
virtual QString type() const Q_DECL_OVERRIDE
Returns a string containing the type of the property.
QVariant VariantValue
The property's value. This does not have to be used by subclasses, but it makes sense in cases where ...
Definition: vproperty_p.h:40
QVariant::Type PropertyVariantType
Stores the property type.
Definition: vproperty_p.h:52
virtual Q_REQUIRED_RESULT VProperty * clone(bool include_children=true, VProperty *container=nullptr) const
Clones this property.
Definition: vproperty.cpp:372
VPropertyPrivate * d_ptr
The protected structure holding the member variables (to assure binary compatibility)
Definition: vproperty.h:214
virtual QVariant getSetting(const QString &key) const
Get the settings. This function has to be implemented in a subclass in order to have an effect.
Definition: vproperty.cpp:360
virtual void setSettings(const QMap< QString, QVariant > &settings)
Sets the settings by calling the overloaded setSetting(const QString &key, const QVariant &value) for...
Definition: vproperty.cpp:333