cctw  0.2.1
cctwdoublematrix3x3property.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 
9  QObject *parent, const char *name,
10  CctwDoubleMatrix3x3 value, QString toolTip) :
11  QcepProperty(saver, parent, name, toolTip),
12  m_Default(value),
13  m_Value(value)
14 {
15 }
16 
17 CctwDoubleMatrix3x3Property::CctwDoubleMatrix3x3Property(QcepSettingsSaverWPtr saver, QObject *parent, const char *name,
18  double r0c0, double r0c1, double r0c2,
19  double r1c0, double r1c1, double r1c2,
20  double r2c0, double r2c1, double r2c2,
21  QString toolTip) :
22  QcepProperty(saver, parent, name, toolTip),
23  m_Default(),
24  m_Value()
25 {
27 
28  val(0,0) = r0c0; val(0,1) = r0c1; val(0,2) = r0c2;
29  val(1,0) = r1c0; val(1,1) = r1c1; val(1,2) = r1c2;
30  val(2,0) = r2c0; val(2,1) = r2c1; val(2,2) = r2c2;
31 
32  m_Default = val;
33  m_Value = val;
34 }
35 
37 {
38  qRegisterMetaType< CctwDoubleMatrix3x3 >("CctwDoubleMatrix3x3");
39 
40  qRegisterMetaTypeStreamOperators< CctwDoubleMatrix3x3 >("CctwDoubleMatrix3x3");
41 
42  registerCustomSaver("CctwDoubleMatrix3x3", CctwMatrix3x3<double>::customSaver);
43 }
44 
46 {
47  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
48 
49  return m_Value;
50 }
51 
53 {
54  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
55 
56  return m_Default;
57 }
58 
59 double CctwDoubleMatrix3x3Property::subValue(int row, int col) const
60 {
61  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
62 
63  return m_Value(row, col);
64 }
65 
67 {
68  if (debug()) {
69  printMessage(tr("%1 CctwqtDoubleMatrix3x3Property::setValue(CctwDoubleMatrix3x3 %2, int %3) [%4]")
70  .arg(name()).arg(toString(val)).arg(index).arg(this->index()));
71  }
72 
73  if (index == this->index()) {
74  setValue(val);
75  }
76 }
77 
79 {
80  setValue(m_Value + step);
81 }
82 
83 void CctwDoubleMatrix3x3Property::setSubValue(int row, int col, double value, int index)
84 {
85  if (index == this->index()) {
86  setSubValue(row, col, value);
87  }
88 }
89 
90 void CctwDoubleMatrix3x3Property::setSubValue(int row, int col, double value)
91 {
92  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
93 
94  if (value != m_Value(row, col)) {
95  int newIndex = incIndex(1);
96 
97  emit subValueChanged(row, col, value, newIndex);
98 
99  m_Value(row, col) = value;
100 
101  emit valueChanged(m_Value, newIndex);
102  }
103 }
104 
106 {
107  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
108 
109  QString res = "[";
110 
111  for (int row=0; row<3; row++) {
112  res += " [ ";
113  for (int col=0; col<3; col++) {
114  res += tr("%1 ").arg(val(row, col));
115  }
116  res += "]";
117  }
118 
119  res += "]";
120 
121  return res;
122 }
123 
125 {
126  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
127 
128  if (qcepDebug(DEBUG_PROPERTIES)) {
129  printMessage(tr("%1 CctwqtDoubleMatrix3x3Property::setValue(CctwDoubleMatrix3x3 %2)")
130  .arg(name()).arg(toString(val)));
131  }
132 
133  if (val != m_Value) {
134  int newIndex = incIndex(1);
135 
136  if (debug()) {
137  printMessage(tr("%1: CctwqtDoubleMatrix3x3Property::setValue(CctwDoubleMatrix3x3 %2) [%3]")
138  .arg(name()).arg(toString(val)).arg(index()));
139  }
140 
141  for (int row=0; row<3; row++) {
142  for (int col=0; col<3; col++) {
143  if(val(row,col) != m_Value(row,col)) {
144  emit subValueChanged(row, col, val(row,col), newIndex);
145  }
146  }
147  }
148 
149  m_Value = val;
150 
151  QcepSettingsSaverPtr saver(m_Saver);
152 
153  if (saver) {
154  saver->changed(this);
155  }
156 
157  emit valueChanged(m_Value, newIndex);
158  }
159 }
160 
162 {
163  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
164 
165  m_Default = val;
166 }
167 
169 {
170  if (qcepDebug(DEBUG_PROPERTIES)) {
171  printMessage(tr("%1: CctwqtDoubleMatrix3x3Property::resetValue").arg(name()));
172  }
173 
175 }
176 
177 QScriptValue CctwDoubleMatrix3x3Property::toScriptValue(QScriptEngine *engine, const CctwDoubleMatrix3x3 &mat)
178 {
179  QScriptValue obj = engine->newArray(3);
180 
181  for (int i=0; i<3; i++) {
182  obj.setProperty(i, engine->newArray(3));
183  }
184 
185  for (int r=0; r<3; r++) {
186  for (int c=0; c<3; c++) {
187  obj.property(r).setProperty(c, mat(r,c));
188  }
189  }
190 
191 // for (int r=0; r<3; r++) {
192 // for (int c=0; c<3; c++) {
193 // obj.setProperty(tr("r%1c%2").arg(r).arg(c), mat(r,c));
194 // }
195 // }
196 
197  return obj;
198 }
199 
201 {
202  for (int r=0; r<3; r++) {
203  for (int c=0; c<3; c++) {
204  mat(r,c)=obj.property(r).property(c).toNumber();
205 // mat(r,c) = obj.property(tr("r%1c%2").arg(r).arg(c)).toNumber();
206  }
207  }
208 }
209 
210 //void CctwDoubleMatrix3x3Property::linkTo(QDoubleSpinBox *xSpinBox, QDoubleSpinBox *ySpinBox, QDoubleSpinBox *zSpinBox)
211 //{
212 // if (xSpinBox) {
213 // linkTo(0, xSpinBox);
214 // }
215 
216 // if (ySpinBox) {
217 // linkTo(1, ySpinBox);
218 // }
219 
220 // if (zSpinBox) {
221 // linkTo(2, zSpinBox);
222 // }
223 //}
224 
225 void CctwDoubleMatrix3x3Property::linkTo(int row, int col, QDoubleSpinBox *spinBox)
226 {
227  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
228 
230  = new CctwDoubleMatrix3x3PropertyDoubleSpinBoxHelper(spinBox, this, row, col);
231 
232  helper->moveToThread(spinBox->thread());
233  helper->connect();
234 
235  spinBox -> setValue(subValue(row, col));
236  spinBox -> setKeyboardTracking(false);
237 
238  setWidgetToolTip(spinBox);
239 
240  connect(this, SIGNAL(subValueChanged(int,int,double,int)), helper, SLOT(setSubValue(int,int,double,int)));
241  connect(helper, SIGNAL(subValueChanged(int,int,double,int)), this, SLOT(setSubValue(int,int,double,int)));
242 }
243 
245  (QDoubleSpinBox *spinBox, CctwDoubleMatrix3x3Property *property, int row, int col)
246  : QObject(spinBox),
247  m_DoubleSpinBox(spinBox),
248  m_Property(property),
249  m_Row(row),
250  m_Col(col)
251 {
252 }
253 
255 {
256  CONNECT_CHECK(QObject::connect(m_DoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(setValue(double)), Qt::DirectConnection));
257 }
258 
259 void CctwDoubleMatrix3x3PropertyDoubleSpinBoxHelper::setSubValue(int row, int col, double value, int index)
260 {
261  if (m_Property->index() == index) {
262  if (m_Row == row && m_Col == col) {
263  if (m_DoubleSpinBox->value() != value) {
264  bool block = m_DoubleSpinBox->blockSignals(true);
265  m_DoubleSpinBox->setValue(value);
266  m_DoubleSpinBox->blockSignals(block);
267  }
268  }
269  }
270 }
271 
273 {
274  emit subValueChanged(m_Row, m_Col, value, m_Property->incIndex(1));
275 }
276 
void incValue(CctwDoubleMatrix3x3 step)
static void fromScriptValue(const QScriptValue &obj, CctwDoubleMatrix3x3 &mat)
CctwDoubleMatrix3x3Property(QcepSettingsSaverWPtr saver, QObject *parent, const char *name, CctwDoubleMatrix3x3 value, QString toolTip)
void subValueChanged(int row, int col, double val, int index)
double subValue(int row, int col) const
void setDefaultValue(CctwDoubleMatrix3x3 val)
void setSubValue(int row, int col, double value, int index)
CctwDoubleMatrix3x3 value() const
CctwDoubleMatrix3x3 defaultValue() const
void setValue(CctwDoubleMatrix3x3 val, int index)
void setSubValue(int row, int col, double value, int index)
void valueChanged(CctwDoubleMatrix3x3 val, int index)
CctwDoubleMatrix3x3PropertyDoubleSpinBoxHelper(QDoubleSpinBox *spinBox, CctwDoubleMatrix3x3Property *property, int row, int col)
static QScriptValue toScriptValue(QScriptEngine *engine, const CctwDoubleMatrix3x3 &mat)
QString toString(const CctwDoubleMatrix3x3 &mat)
void subValueChanged(int row, int col, double value, int index)
void linkTo(int row, int col, QDoubleSpinBox *spinBox)