RioEngine  0.1
My first attempt to create a 3D WYSIWYG Game Engine
vec3fproperty.cpp
Go to the documentation of this file.
1 // *************************************************************************************************
2 //
3 // This code is part of the Sample Application to demonstrate the use of the QPropertyEditor library.
4 // It is distributed as public domain and can be modified and used by you without any limitations.
5 //
6 // Your feedback is welcome!
7 //
8 // Author: Volker Wiendl
9 // Enum enhancement by Roman alias banal from qt-apps.org
10 // *************************************************************************************************
11 
12 
13 #include "Vec3fProperty.h"
14 #include "CustomTypes.h"
15 
16 #include <QRegExp>
17 
18 Vec3fProperty::Vec3fProperty(const QString& name /*= QString()*/, QObject* propertyObject /*= 0*/, QObject* parent /*= 0*/) : Property(name, propertyObject, parent)
19 {
20  m_x = new Property("x", this, this);
21  m_y = new Property("y", this, this);
22  m_z = new Property("z", this, this);
23  setEditorHints("minimumX=-2147483647;maximumX=2147483647;minimumY=-2147483647;maximumY=2147483647;minimumZ=-2147483647;maximumZ=2147483647;");
24 }
25 
26 QVariant Vec3fProperty::value(int role) const
27 {
28  QVariant data = Property::value();
29  if (data.isValid() && role != Qt::UserRole)
30  {
31  switch (role)
32  {
33  case Qt::DisplayRole:
34  return tr("[ %1, %2, %3]").arg(data.value<btVector3>().x()).arg(data.value<btVector3>().y()).arg(data.value<btVector3>().z());
35  case Qt::EditRole:
36  return tr("%1, %2, %3").arg(data.value<btVector3>().x()).arg(data.value<btVector3>().y()).arg(data.value<btVector3>().z());
37  };
38  }
39  return data;
40 }
41 
42 void Vec3fProperty::setValue(const QVariant& value)
43 {
44  if (value.type() == QVariant::String)
45  {
46  QString v = value.toString();
47  QRegExp rx("([+-]?([0-9]*[\\.,])?[0-9]+(e[+-]?[0-9]+)?)");
48  rx.setCaseSensitivity(Qt::CaseInsensitive);
49  int count = 0;
50  int pos = 0;
51  float x = 0.0f, y = 0.0f, z = 0.0f;
52  while ((pos = rx.indexIn(v, pos)) != -1)
53  {
54  if (count == 0)
55  x = rx.cap(1).toDouble();
56  else if (count == 1)
57  y = rx.cap(1).toDouble();
58  else if (count == 2)
59  z = rx.cap(1).toDouble();
60  else if (count > 2)
61  break;
62  ++count;
63  pos += rx.matchedLength();
64  }
65  m_x->setProperty("x", x);
66  m_y->setProperty("y", y);
67  m_z->setProperty("z", z);
68  Property::setValue(QVariant::fromValue(btVector3(x, y, z)));
69  }
70  else
71  Property::setValue(value);
72 }
73 
74 void Vec3fProperty::setEditorHints(const QString& hints)
75 {
76  m_x->setEditorHints(parseHints(hints, 'X'));
77  m_y->setEditorHints(parseHints(hints, 'Y'));
78  m_z->setEditorHints(parseHints(hints, 'Z'));
79 }
80 
81 float Vec3fProperty::x() const
82 {
83  return value().value<btVector3>().x();
84 }
85 
86 void Vec3fProperty::setX(float x)
87 {
88  Property::setValue(QVariant::fromValue(btVector3(x, y(), z())));
89 }
90 
91 float Vec3fProperty::y() const
92 {
93  return value().value<btVector3>().y();
94 }
95 
96 void Vec3fProperty::setY(float y)
97 {
98  Property::setValue(QVariant::fromValue(btVector3(x(), y, z())));
99 }
100 
101 float Vec3fProperty::z() const
102 {
103  return value().value<btVector3>().z();
104 }
105 
106 void Vec3fProperty::setZ(float z)
107 {
108  Property::setValue(QVariant::fromValue(btVector3(x(), y(), z)));
109 }
110 
111 QString Vec3fProperty::parseHints(const QString& hints, const QChar component )
112 {
113  QRegExp rx(QString("(.*)(")+component+QString("{1})(=\\s*)(.*)(;{1})"));
114  rx.setMinimal(true);
115  int pos = 0;
116  QString componentHints;
117  while ((pos = rx.indexIn(hints, pos)) != -1)
118  {
119  // cut off additional front settings (TODO create correct RegExp for that)
120  if (rx.cap(1).lastIndexOf(';') != -1)
121  componentHints += QString("%1=%2;").arg(rx.cap(1).remove(0, rx.cap(1).lastIndexOf(';')+1)).arg(rx.cap(4));
122  else
123  componentHints += QString("%1=%2;").arg(rx.cap(1)).arg(rx.cap(4));
124  pos += rx.matchedLength();
125  }
126  return componentHints;
127 }
void setZ(float z)
Property(const QString &name=QString(), QObject *propertyObject=0, QObject *parent=0)
Definition: property.cpp:31
virtual void setValue(const QVariant &value)
Definition: property.cpp:51
Vec3fProperty(const QString &name=QString(), QObject *propertyObject=0, QObject *parent=0)
void setEditorHints(const QString &hints)
void setY(float y)
float x() const
QVariant value(int role=Qt::UserRole) const
virtual void setValue(const QVariant &value)
void setX(float x)
float z() const
virtual QVariant value(int role=Qt::UserRole) const
Definition: property.cpp:43
float y() const
virtual void setEditorHints(const QString &hints)
Definition: property.h:96