cctw  0.2.1
cctwintvector3dproperty.cpp
Go to the documentation of this file.
2 #include "qcepmutexlocker.h"
3 #include "qcepdebug.h"
4 #include "qcepsettingssaver.h"
5 #include <QScriptEngine>
6 #include <stdio.h>
7 
8 CctwIntVector3DProperty::CctwIntVector3DProperty(QcepSettingsSaverWPtr saver, QObject *parent, const char *name, CctwIntVector3D value, QString toolTip) :
9  QcepProperty(saver, parent, name, toolTip),
10  m_Default(value),
11  m_Value(value)
12 {
13 }
14 
15 CctwIntVector3DProperty::CctwIntVector3DProperty(QcepSettingsSaverWPtr saver, QObject *parent, const char *name, int x, int y, int z, QString toolTip) :
16  QcepProperty(saver, parent, name, toolTip),
17  m_Default(CctwIntVector3D(x,y,z)),
18  m_Value(CctwIntVector3D(x,y,z))
19 {
20 }
21 
23 {
24  qRegisterMetaType< CctwIntVector3D >("CctwIntVector3D");
25 
26  qRegisterMetaTypeStreamOperators< CctwIntVector3D >("CctwIntVector3D");
27 
28  registerCustomSaver("CctwIntVector3D", CctwIntVector3D::customSaver);
29 }
30 
32 {
33  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
34 
35  return m_Value;
36 }
37 
39 {
40  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
41 
42  return m_Default;
43 }
44 
46 {
47  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
48 
49  return m_Value(axis);
50 }
51 
53 {
54  if (debug()) {
55  printMessage(tr("%1 CctwqtIntVector3DProperty::setValue(CctwIntVector3D %2, int %3) [%4]")
56  .arg(name()).arg(toString(val)).arg(index).arg(this->index()));
57  }
58 
59  if (index == this->index()) {
60  setValue(val);
61  }
62 }
63 
65 {
66  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
67 
68  if (qcepDebug(DEBUG_PROPERTIES) || debug()) {
69  printMessage(tr("%1: CctwqtIntVector3DProperty::incValue(CctwIntVector3D %2...)")
70  .arg(name()).arg(toString(step)));
71  }
72 
73  m_Value += step;
74 
75  QcepSettingsSaverPtr saver(m_Saver);
76 
77  if (saver) {
78  saver->changed(this);
79  }
80 
81  emit valueChanged(m_Value, incIndex(1));
82 }
83 
84 void CctwIntVector3DProperty::setSubValue(int axis, int value, int index)
85 {
86  if (index == this->index()) {
87  setSubValue(axis, value);
88  }
89 }
90 
91 void CctwIntVector3DProperty::setSubValue(int axis, int value)
92 {
93  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
94 
95  if (value != m_Value(axis)) {
96  int newIndex = incIndex(1);
97 
98  emit subValueChanged(axis, value, newIndex);
99 
100  m_Value(axis) = value;
101 
102  emit valueChanged(m_Value, newIndex);
103  }
104 }
105 
107 {
108  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
109 
110  QString res = tr("[ %1 %2 %3 ]").arg(val.x()).arg(val.y()).arg(val.z());
111 
112  return res;
113 }
114 
116 {
117  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
118 
119  if (qcepDebug(DEBUG_PROPERTIES)) {
120  printMessage(tr("%1 CctwqtIntVector3DProperty::setValue(CctwIntVector3D %2)")
121  .arg(name()).arg(toString(val)));
122  }
123 
124  if (val != m_Value) {
125  int newIndex = incIndex(1);
126 
127  if (debug()) {
128  printMessage(tr("%1: CctwqtIntVector3DProperty::setValue(CctwIntVector3D %2) [%3]")
129  .arg(name()).arg(toString(val)).arg(index()));
130  }
131 
132  for (int axis=0; axis<3; axis++) {
133  if (val(axis) != m_Value(axis)) {
134  emit subValueChanged(axis, val(axis), newIndex);
135  }
136  }
137 
138  m_Value = val;
139 
140  QcepSettingsSaverPtr saver(m_Saver);
141 
142  if (saver) {
143  saver->changed(this);
144  }
145 
146  emit valueChanged(m_Value, incIndex(1));
147  }
148 }
149 
151 {
152  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
153 
154  m_Default = val;
155 }
156 
158 {
159  if (qcepDebug(DEBUG_PROPERTIES)) {
160  printMessage(tr("%1: CctwqtIntVector3DProperty::resetValue").arg(name()));
161  }
162 
164 }
165 
166 QScriptValue CctwIntVector3DProperty::toScriptValue(QScriptEngine *engine, const CctwIntVector3D &vec)
167 {
168  QScriptValue obj = engine->newArray(3);
169 
170  obj.setProperty(0, vec.x());
171  obj.setProperty(1, vec.y());
172  obj.setProperty(2, vec.z());
173 
174  return obj;
175 }
176 
178 {
179  vec.x() = obj.property(0).toInteger();
180  vec.y() = obj.property(1).toInteger();
181  vec.z() = obj.property(2).toInteger();
182 }
183 
184 void CctwIntVector3DProperty::linkTo(QSpinBox *xSpinBox, QSpinBox *ySpinBox, QSpinBox *zSpinBox)
185 {
186  if (xSpinBox) {
187  linkTo(0, xSpinBox);
188  }
189 
190  if (ySpinBox) {
191  linkTo(1, ySpinBox);
192  }
193 
194  if (zSpinBox) {
195  linkTo(2, zSpinBox);
196  }
197 }
198 
199 void CctwIntVector3DProperty::linkTo(int axis, QSpinBox *spinBox)
200 {
201  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
202 
204  = new CctwIntVector3DPropertySpinBoxHelper(spinBox, this, axis);
205 
206  helper->moveToThread(spinBox->thread());
207  helper->connect();
208 
209  spinBox -> setValue(subValue(axis));
210  spinBox -> setKeyboardTracking(false);
211 
212  setWidgetToolTip(spinBox);
213 
214  connect(this, SIGNAL(subValueChanged(int,int,int)), helper, SLOT(setSubValue(int,int,int)));
215  connect(helper, SIGNAL(subValueChanged(int,int,int)), this, SLOT(setSubValue(int,int,int)));
216 }
217 
219  (QSpinBox *spinBox, CctwIntVector3DProperty *property, int axis)
220  : QObject(spinBox),
221  m_SpinBox(spinBox),
222  m_Property(property),
223  m_Axis(axis)
224 {
225 }
226 
228 {
229  CONNECT_CHECK(QObject::connect(m_SpinBox, SIGNAL(valueChanged(int)), this, SLOT(setValue(int)), Qt::DirectConnection));
230 }
231 
232 void CctwIntVector3DPropertySpinBoxHelper::setSubValue(int axis, int value, int index)
233 {
234  if (m_Property->index() == index) {
235  if (m_Axis == axis) {
236  if (m_SpinBox->value() != value) {
237  bool block = m_SpinBox->blockSignals(true);
238  m_SpinBox->setValue(value);
239  m_SpinBox->blockSignals(block);
240  }
241  }
242  }
243 }
244 
246 {
247  emit subValueChanged(m_Axis, value, m_Property->incIndex(1));
248 }
249 
250 #ifndef QT_NO_DATASTREAM
251 
252 QDataStream &operator<<(QDataStream &stream, const CctwIntVector3D &vec)
253 {
254  for (int i = 0; i < 3; ++i) {
255  stream << vec(i);
256  }
257 
258  return stream;
259 }
260 
261 QDataStream &operator>>(QDataStream &stream, CctwIntVector3D &vec)
262 {
263  int x;
264  for (int i = 0; i < 3; ++i) {
265  stream >> x;
266  vec(i) = x;
267  }
268 
269  return stream;
270 }
271 
272 #endif
CctwIntVector3DPropertySpinBoxHelper(QSpinBox *spinBox, CctwIntVector3DProperty *property, int axis)
void setSubValue(int axis, int value, int index)
void valueChanged(CctwIntVector3D val, int index)
QDataStream & operator<<(QDataStream &stream, const CctwIntVector3D &vec)
T x() const
Definition: cctwvector3d.h:17
void incValue(CctwIntVector3D step)
void subValueChanged(int axis, int val, int index)
static void fromScriptValue(const QScriptValue &obj, CctwIntVector3D &vec)
void setSubValue(int axis, int value, int index)
CctwIntVector3D value() const
T z() const
Definition: cctwvector3d.h:19
void setValue(CctwIntVector3D val, int index)
void setDefaultValue(CctwIntVector3D val)
void subValueChanged(int axis, int value, int index)
static QScriptValue toScriptValue(QScriptEngine *engine, const CctwIntVector3D &vec)
CctwIntVector3DProperty(QcepSettingsSaverWPtr saver, QObject *parent, const char *name, CctwIntVector3D value, QString toolTip)
static void customSaver(const QVariant &val, QSettings *settings, QString name)
QDataStream & operator>>(QDataStream &stream, CctwIntVector3D &vec)
QString toString(const CctwIntVector3D &mat)
T y() const
Definition: cctwvector3d.h:18
CctwIntVector3D defaultValue() const
void linkTo(QSpinBox *xSpinBox, QSpinBox *ySpinBox, QSpinBox *zSpinBox)