ctkCoordinatesWidget.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QDebug>
  16. #include <QDoubleSpinBox>
  17. #include <QHBoxLayout>
  18. // CTK includes
  19. #include "ctkCoordinatesWidget.h"
  20. #include "ctkDoubleSpinBox.h"
  21. #include "ctkDoubleSpinBox_p.h"
  22. #include "ctkUtils.h"
  23. #include "ctkValueProxy.h"
  24. // STD includes
  25. #include <cmath>
  26. #include <limits>
  27. //------------------------------------------------------------------------------
  28. ctkCoordinatesWidget::ctkCoordinatesWidget(QWidget* _parent) :QWidget(_parent)
  29. {
  30. this->Decimals = 3;
  31. ctkDoubleSpinBox temp;
  32. this->DecimalsOption = temp.decimalsOption();
  33. this->SingleStep = 1.;
  34. this->Minimum = -std::numeric_limits<double>::max();
  35. this->Maximum = std::numeric_limits<double>::max();
  36. this->Normalized = false;
  37. this->Dimension = 0;
  38. this->SizeHintPolicy = ctkDoubleSpinBox::SizeHintByValue;
  39. this->Coordinates = 0;
  40. this->ChangingDecimals = false;
  41. QHBoxLayout* hboxLayout = new QHBoxLayout(this);
  42. hboxLayout->setContentsMargins(0, 0, 0, 0);
  43. this->setLayout(hboxLayout);
  44. this->setDimension(3);
  45. }
  46. //------------------------------------------------------------------------------
  47. ctkCoordinatesWidget::~ctkCoordinatesWidget()
  48. {
  49. delete [] this->Coordinates;
  50. }
  51. //------------------------------------------------------------------------------
  52. void ctkCoordinatesWidget::addSpinBox()
  53. {
  54. ctkDoubleSpinBox* spinBox = new ctkDoubleSpinBox(this);
  55. spinBox->setDecimals(this->Decimals);
  56. spinBox->setDecimalsOption(this->DecimalsOption);
  57. spinBox->setSingleStep(this->SingleStep);
  58. spinBox->setMinimum(this->Minimum);
  59. spinBox->setMaximum(this->Maximum);
  60. spinBox->setSizeHintPolicy(this->SizeHintPolicy);
  61. spinBox->setValueProxy(this->Proxy.data());
  62. connect( spinBox, SIGNAL(valueChanged(double)),
  63. this, SLOT(updateCoordinate(double)));
  64. // Same number of decimals within the spinboxes.
  65. connect( spinBox, SIGNAL(decimalsChanged(int)),
  66. this, SLOT(updateOtherDecimals(int)));
  67. qobject_cast<QHBoxLayout*>(this->layout())->addWidget(spinBox, 1.);
  68. }
  69. //------------------------------------------------------------------------------
  70. void ctkCoordinatesWidget::setDimension(int dim)
  71. {
  72. if (dim < 1)
  73. {
  74. return;
  75. }
  76. double* newPos = new double[dim];
  77. if (dim > this->Dimension)
  78. {
  79. memcpy(newPos, this->Coordinates, this->Dimension * sizeof(double));
  80. for (int i = this->Dimension; i < dim; ++i)
  81. {
  82. newPos[i] = 0.;
  83. this->addSpinBox();
  84. this->LastUserEditedCoordinates.push_back(i);
  85. }
  86. }
  87. else
  88. {
  89. memcpy(newPos, this->Coordinates, dim * sizeof(double));
  90. for (int i = this->Dimension - 1 ; i >= dim; --i)
  91. {
  92. QLayoutItem* item = this->layout()->takeAt(i);
  93. QWidget* widget = item ? item->widget() : 0;
  94. delete item;
  95. delete widget;
  96. this->LastUserEditedCoordinates.pop_back();
  97. }
  98. }
  99. delete [] this->Coordinates;
  100. this->Coordinates = newPos;
  101. this->Dimension = dim;
  102. this->updateGeometry();
  103. this->updateCoordinates();
  104. }
  105. //------------------------------------------------------------------------------
  106. int ctkCoordinatesWidget::dimension() const
  107. {
  108. return this->Dimension;
  109. }
  110. //------------------------------------------------------------------------------
  111. void ctkCoordinatesWidget::setMinimum(double min)
  112. {
  113. for (int i = 0; i < this->Dimension; ++i)
  114. {
  115. this->spinBox(i)->setMinimum(min);
  116. }
  117. this->Minimum = min;
  118. }
  119. //------------------------------------------------------------------------------
  120. double ctkCoordinatesWidget::minimum() const
  121. {
  122. return this->Minimum;
  123. }
  124. //------------------------------------------------------------------------------
  125. void ctkCoordinatesWidget::setMaximum(double max)
  126. {
  127. for (int i = 0; i < this->Dimension; ++i)
  128. {
  129. this->spinBox(i)->setMaximum(max);
  130. }
  131. this->Maximum = max;
  132. }
  133. //------------------------------------------------------------------------------
  134. double ctkCoordinatesWidget::maximum() const
  135. {
  136. return this->Maximum;
  137. }
  138. //------------------------------------------------------------------------------
  139. void ctkCoordinatesWidget::setRange(double min, double max)
  140. {
  141. for (int i = 0; i < this->Dimension; ++i)
  142. {
  143. this->spinBox(i)->setRange(min, max);
  144. }
  145. this->Minimum = min;
  146. this->Maximum = max;
  147. }
  148. //------------------------------------------------------------------------------
  149. void ctkCoordinatesWidget::setNormalized(bool normalized)
  150. {
  151. this->Normalized = normalized;
  152. if (this->Normalized)
  153. {
  154. double* normalizedCoordinates = new double[this->Dimension];
  155. memcpy(normalizedCoordinates, this->Coordinates, sizeof(double)*this->Dimension);
  156. ctkCoordinatesWidget::normalize(normalizedCoordinates, this->Dimension);
  157. this->setMinimum(-1.);
  158. this->setMaximum(1.);
  159. this->setCoordinates(normalizedCoordinates);
  160. delete [] normalizedCoordinates;
  161. }
  162. }
  163. //------------------------------------------------------------------------------
  164. bool ctkCoordinatesWidget::isNormalized() const
  165. {
  166. return this->Normalized;
  167. }
  168. //------------------------------------------------------------------------------
  169. void ctkCoordinatesWidget::setDecimals(int newDecimals)
  170. {
  171. this->Decimals = newDecimals;
  172. for (int i = 0; i < this->Dimension; ++i)
  173. {
  174. this->spinBox(i)->setDecimals(newDecimals);
  175. }
  176. }
  177. //------------------------------------------------------------------------------
  178. void ctkCoordinatesWidget::updateDecimals()
  179. {
  180. if (this->ChangingDecimals)
  181. {
  182. return;
  183. }
  184. int maxDecimals = 0;
  185. for (int i = 0; i < this->Dimension; ++i)
  186. {
  187. int spinBoxDecimals = this->Decimals;
  188. if (this->decimalsOption() & ctkDoubleSpinBox::DecimalsByKey ||
  189. this->decimalsOption() & ctkDoubleSpinBox::DecimalsByShortcuts)
  190. {
  191. spinBoxDecimals = this->spinBox(i)->decimals();
  192. }
  193. if (this->decimalsOption() & ctkDoubleSpinBox::DecimalsByValue)
  194. {
  195. spinBoxDecimals = ctkCoordinatesWidget::spinBoxSignificantDecimals(
  196. this->spinBox(i));
  197. if (spinBoxDecimals == 16)
  198. {
  199. spinBoxDecimals = this->spinBox(i)->decimals();
  200. }
  201. }
  202. maxDecimals = qMax(maxDecimals, spinBoxDecimals);
  203. }
  204. this->ChangingDecimals = true;
  205. this->setTemporaryDecimals(maxDecimals);
  206. this->ChangingDecimals = false;
  207. }
  208. //------------------------------------------------------------------------------
  209. void ctkCoordinatesWidget::updateOtherDecimals(int senderDecimals)
  210. {
  211. if (this->ChangingDecimals)
  212. {
  213. return;
  214. }
  215. int senderSpinBoxDecimals = ctkCoordinatesWidget::spinBoxSignificantDecimals(
  216. qobject_cast<ctkDoubleSpinBox*>(this->sender()));
  217. int maxDecimals = senderDecimals;
  218. for (int i = 0; i < this->Dimension; ++i)
  219. {
  220. if (this->sender() == this->spinBox(i))
  221. {
  222. continue;
  223. }
  224. int spinBoxDecimals = maxDecimals;
  225. if (this->decimalsOption() & ctkDoubleSpinBox::DecimalsByKey)
  226. {
  227. spinBoxDecimals = this->spinBox(i)->decimals();
  228. }
  229. if (this->decimalsOption() & ctkDoubleSpinBox::DecimalsByValue)
  230. {
  231. spinBoxDecimals = ctkCoordinatesWidget::spinBoxSignificantDecimals(
  232. this->spinBox(i));
  233. // if the edited spinbox has an undefined number of decimals and the
  234. // the current spinbox too, then use the new number of decimals otherwise
  235. // there would be no way to increase/decrease decimals for all the
  236. // spinboxes.
  237. if (spinBoxDecimals == 16)
  238. {
  239. spinBoxDecimals = (senderSpinBoxDecimals == 16)?
  240. senderDecimals : this->spinBox(i)->decimals();
  241. }
  242. }
  243. maxDecimals = qMax(maxDecimals, spinBoxDecimals);
  244. }
  245. this->ChangingDecimals = true;
  246. this->setTemporaryDecimals(maxDecimals);
  247. this->ChangingDecimals = false;
  248. }
  249. //------------------------------------------------------------------------------
  250. void ctkCoordinatesWidget::setTemporaryDecimals(int newDecimals)
  251. {
  252. for (int i = 0; i < this->Dimension; ++i)
  253. {
  254. if (this->sender() == this->spinBox(i))
  255. {
  256. continue;
  257. }
  258. // Increasing the number of decimals might have lost precision.
  259. double currentValue = this->spinBox(i)->value();
  260. if (this->spinBox(i)->valueProxy())
  261. {
  262. currentValue = this->spinBox(i)->valueProxy()->proxyValueFromValue(currentValue);
  263. }
  264. this->spinBox(i)->d_ptr->setValue(currentValue, newDecimals);
  265. }
  266. }
  267. //------------------------------------------------------------------------------
  268. int ctkCoordinatesWidget::spinBoxSignificantDecimals(ctkDoubleSpinBox* spinBox)
  269. {
  270. if (!spinBox)
  271. {
  272. return 0;
  273. }
  274. double currentValue = spinBox->value();
  275. if (spinBox->valueProxy())
  276. {
  277. currentValue = spinBox->valueProxy()->proxyValueFromValue(currentValue);
  278. }
  279. return ctk::significantDecimals(currentValue);
  280. }
  281. //------------------------------------------------------------------------------
  282. int ctkCoordinatesWidget::decimals() const
  283. {
  284. return this->Decimals;
  285. }
  286. // --------------------------------------------------------------------------
  287. ctkDoubleSpinBox::DecimalsOptions ctkCoordinatesWidget::decimalsOption()const
  288. {
  289. return this->DecimalsOption;
  290. }
  291. // --------------------------------------------------------------------------
  292. void ctkCoordinatesWidget
  293. ::setDecimalsOption(ctkDoubleSpinBox::DecimalsOptions newDecimalsOption)
  294. {
  295. for (int i = 0; i < this->Dimension; ++i)
  296. {
  297. this->spinBox(i)->setDecimalsOption(newDecimalsOption);
  298. }
  299. this->DecimalsOption = newDecimalsOption;
  300. }
  301. //------------------------------------------------------------------------------
  302. void ctkCoordinatesWidget::setSingleStep(double step)
  303. {
  304. for (int i = 0; i < this->Dimension; ++i)
  305. {
  306. this->spinBox(i)->setSingleStep(step);
  307. }
  308. this->SingleStep = step;
  309. }
  310. //------------------------------------------------------------------------------
  311. double ctkCoordinatesWidget::singleStep() const
  312. {
  313. return this->SingleStep;
  314. }
  315. //------------------------------------------------------------------------------
  316. void ctkCoordinatesWidget::setCoordinatesAsString(QString _pos)
  317. {
  318. QStringList posList = _pos.split(',');
  319. if (posList.count() != this->Dimension)
  320. {
  321. return;
  322. }
  323. double* newPos = new double[this->Dimension];
  324. for (int i = 0; i < this->Dimension; ++i)
  325. {
  326. newPos[i] = posList[i].toDouble();
  327. }
  328. this->setCoordinates(newPos);
  329. delete [] newPos;
  330. }
  331. //------------------------------------------------------------------------------
  332. QString ctkCoordinatesWidget::coordinatesAsString()const
  333. {
  334. QString res;
  335. for (int i = 0; i < this->Dimension; ++i)
  336. {
  337. if (i != 0)
  338. {
  339. res += ",";
  340. }
  341. res += QString::number(this->Coordinates[i]);
  342. }
  343. return res;
  344. }
  345. //------------------------------------------------------------------------------
  346. void ctkCoordinatesWidget::setCoordinates(double* coordinates)
  347. {
  348. for (int i = 0; i < this->Dimension; ++i)
  349. {
  350. this->Coordinates[i] = coordinates[i];
  351. }
  352. if (this->Normalized)
  353. {
  354. this->normalize(this->Coordinates, this->Dimension);
  355. }
  356. bool valuesModified = false;
  357. bool blocked = this->blockSignals(true);
  358. for (int i = 0; i < this->Dimension; ++i)
  359. {
  360. ctkDoubleSpinBox* spinbox = this->spinBox(i);
  361. if (spinbox)
  362. {
  363. // we don't want updateCoordinate() to be called.
  364. // it could mess with the LastUserEditedCoordinates list.
  365. bool spinBoxSignalWasBlocked = spinbox->blockSignals(true);
  366. if (spinbox->value() != this->Coordinates[i])
  367. {
  368. valuesModified = true;
  369. }
  370. // Still setValue needs to be called to recompute the number of decimals
  371. // if DecimalsByValue is set.
  372. spinbox->setValue(this->Coordinates[i]);
  373. spinbox->blockSignals(spinBoxSignalWasBlocked);
  374. }
  375. }
  376. this->blockSignals(blocked);
  377. this->updateDecimals();
  378. if (valuesModified)
  379. {
  380. this->updateCoordinates();
  381. }
  382. }
  383. //------------------------------------------------------------------------------
  384. void ctkCoordinatesWidget::setCoordinates(double x, double y, double z, double w)
  385. {
  386. double* coordinates = new double[this->Dimension];
  387. if (this->Dimension >= 1)
  388. {
  389. coordinates[0] = x;
  390. }
  391. if (this->Dimension >= 2)
  392. {
  393. coordinates[1] = y;
  394. }
  395. if (this->Dimension >= 3)
  396. {
  397. coordinates[2] = z;
  398. }
  399. if (this->Dimension >= 4)
  400. {
  401. coordinates[3] = w;
  402. }
  403. for (int i = 4; i < this->Dimension; ++i)
  404. {
  405. coordinates[i] = this->Coordinates[i];
  406. }
  407. this->setCoordinates(coordinates);
  408. delete [] coordinates;
  409. }
  410. //------------------------------------------------------------------------------
  411. double const * ctkCoordinatesWidget::coordinates()const
  412. {
  413. return this->Coordinates;
  414. }
  415. //------------------------------------------------------------------------------
  416. void ctkCoordinatesWidget::updateCoordinate(double coordinate)
  417. {
  418. int element = -1;
  419. for (int i = 0; i < this->Dimension; ++i)
  420. {
  421. if ( this->spinBox(i) && this->spinBox(i) == this->sender())
  422. {
  423. this->Coordinates[i] = coordinate;
  424. element = i;
  425. }
  426. }
  427. Q_ASSERT(element != -1);
  428. // Update the last edited history by push first the element.
  429. for (int i = this->Dimension -1; i > 0; --i)
  430. {
  431. if (this->LastUserEditedCoordinates[i] == element)
  432. {
  433. this->LastUserEditedCoordinates.swap(i,i-1);
  434. }
  435. }
  436. // What is the oldest coordinate to be edited
  437. int oldestElement = this->LastUserEditedCoordinates.last();
  438. if (this->isNormalized())
  439. {
  440. // We have to ensure the coordinates are normalized.
  441. double den = 0.;
  442. double squaredNorm = this->squaredNorm();
  443. // Old Values xx + yy + zz = 1
  444. // New values: x'x' + y'y' + z'z' = 1
  445. // Say we are changing y into y':
  446. // x'x' + z'z' = 1 - y'y'
  447. if (oldestElement != -1 &&
  448. this->Coordinates[oldestElement] != 0.0 &&
  449. squaredNorm != 0.0)
  450. {
  451. // 1) Normalize only with the oldest user edited value
  452. // The oldest element is z, that means we try to have
  453. // x = x' (so that the user doesn't loose the edit he just made on the
  454. // element (x) he edited before this one (y).
  455. // Let's pose a the coef to multiply z into z' that keeps the norm to 1
  456. // xx + z'z' = 1 - y'y' (because x = x')
  457. // xx + azaz = 1 - y'y' (because z' = az)
  458. // aa*zz = 1 - y'y' - xx
  459. // a = sqrt( (1 - y'y' - xx) / zz )
  460. den = (1. - (squaredNorm -
  461. this->Coordinates[oldestElement] *
  462. this->Coordinates[oldestElement])) /
  463. (this->Coordinates[oldestElement] *
  464. this->Coordinates[oldestElement]);
  465. if (den > 0.)
  466. {
  467. den = sqrt(den);
  468. }
  469. }
  470. // Maybe 1) failed, then give 2) a chance.
  471. if (den <= 0)
  472. {
  473. oldestElement = -1;
  474. }
  475. bool mult = true;
  476. if (oldestElement == -1)
  477. {
  478. // 2) Normalize with all the coordinates
  479. // Let's pose a the coef to multiply x into x' and z into z' that keeps
  480. // the norm to 1:
  481. // axax + azaz = 1 - y'y'
  482. // aa(xx + zz) = 1 - y'y'
  483. // a = sqrt( (1 - y'y') / (xx + zz) )
  484. squaredNorm -= coordinate * coordinate;
  485. if (squaredNorm != 0.0)
  486. {
  487. den = sqrt( (1. - coordinate * coordinate) / squaredNorm);
  488. }
  489. else if (this->Dimension > 1)
  490. {
  491. mult = false;
  492. den = sqrt((1. - coordinate*coordinate) / (this->Dimension - 1));
  493. }
  494. }
  495. // Normalize coordinates
  496. double* normalizedCoordinates = new double[this->Dimension];
  497. for (int i = 0; i < this->Dimension; ++i)
  498. {
  499. if ((i != element && oldestElement == -1) ||
  500. (i == oldestElement && oldestElement != -1))
  501. {
  502. normalizedCoordinates[i] = mult ? this->Coordinates[i] * den : den;
  503. }
  504. else
  505. {
  506. normalizedCoordinates[i] = this->Coordinates[i];
  507. }
  508. }
  509. this->setCoordinates(normalizedCoordinates);
  510. delete [] normalizedCoordinates;
  511. }
  512. else
  513. {
  514. emit coordinatesChanged(this->Coordinates);
  515. }
  516. }
  517. //------------------------------------------------------------------------------
  518. void ctkCoordinatesWidget::updateCoordinates()
  519. {
  520. for (int i = 0; i < this->Dimension; ++i)
  521. {
  522. this->Coordinates[i] = this->spinBox(i)->value();
  523. }
  524. emit coordinatesChanged(this->Coordinates);
  525. }
  526. //------------------------------------------------------------------------------
  527. void ctkCoordinatesWidget::normalize()
  528. {
  529. double* normalizedCoordinates = new double[this->Dimension];
  530. memcpy(normalizedCoordinates, this->Coordinates,
  531. sizeof(double) * this->Dimension);
  532. ctkCoordinatesWidget::normalize(normalizedCoordinates, this->Dimension);
  533. this->setCoordinates(normalizedCoordinates);
  534. delete [] normalizedCoordinates;
  535. }
  536. //------------------------------------------------------------------------------
  537. double ctkCoordinatesWidget::normalize(double* coordinates, int dimension)
  538. {
  539. double den = ctkCoordinatesWidget::norm( coordinates, dimension );
  540. if ( den != 0.0 )
  541. {
  542. for (int i = 0; i < dimension; ++i)
  543. {
  544. coordinates[i] /= den;
  545. }
  546. }
  547. return den;
  548. }
  549. //------------------------------------------------------------------------------
  550. double ctkCoordinatesWidget::norm()const
  551. {
  552. return ctkCoordinatesWidget::norm(this->Coordinates, this->Dimension);
  553. }
  554. //------------------------------------------------------------------------------
  555. double ctkCoordinatesWidget::norm(double* coordinates, int dimension)
  556. {
  557. return sqrt(ctkCoordinatesWidget::squaredNorm(coordinates, dimension));
  558. }
  559. //------------------------------------------------------------------------------
  560. double ctkCoordinatesWidget::squaredNorm()const
  561. {
  562. return ctkCoordinatesWidget::squaredNorm(this->Coordinates, this->Dimension);
  563. }
  564. //------------------------------------------------------------------------------
  565. double ctkCoordinatesWidget::squaredNorm(double* coordinates, int dimension)
  566. {
  567. double sum = 0.;
  568. for (int i = 0; i < dimension; ++i)
  569. {
  570. sum += coordinates[i] * coordinates[i];
  571. }
  572. return sum;
  573. }
  574. //----------------------------------------------------------------------------
  575. void ctkCoordinatesWidget::setSizeHintPolicy(ctkDoubleSpinBox::SizeHintPolicy newSizeHintPolicy)
  576. {
  577. for (int i = 0; i < this->Dimension; ++i)
  578. {
  579. this->spinBox(i)->setSizeHintPolicy(newSizeHintPolicy);
  580. }
  581. this->SizeHintPolicy = newSizeHintPolicy;
  582. }
  583. //----------------------------------------------------------------------------
  584. ctkDoubleSpinBox::SizeHintPolicy ctkCoordinatesWidget::sizeHintPolicy()const
  585. {
  586. return this->SizeHintPolicy;
  587. }
  588. //----------------------------------------------------------------------------
  589. ctkDoubleSpinBox* ctkCoordinatesWidget::spinBox(int i)
  590. {
  591. QLayoutItem* item = this->layout()->itemAt(i);
  592. ctkDoubleSpinBox* spinBox =
  593. item ? qobject_cast<ctkDoubleSpinBox*>(item->widget()) : 0;
  594. return spinBox;
  595. }
  596. //----------------------------------------------------------------------------
  597. void ctkCoordinatesWidget::setValueProxy(ctkValueProxy* proxy)
  598. {
  599. if (this->Proxy.data() == proxy)
  600. {
  601. return;
  602. }
  603. this->onValueProxyAboutToBeModified();
  604. if (this->Proxy)
  605. {
  606. disconnect(this->Proxy.data(), SIGNAL(proxyAboutToBeModified()),
  607. this, SLOT(onValueProxyAboutToBeModified()));
  608. disconnect(this->Proxy.data(), SIGNAL(proxyModified()),
  609. this, SLOT(onValueProxyModified()));
  610. }
  611. this->Proxy = proxy;
  612. if (this->Proxy)
  613. {
  614. connect(this->Proxy.data(), SIGNAL(proxyAboutToBeModified()),
  615. this, SLOT(onValueProxyAboutToBeModified()));
  616. }
  617. for (int i = 0; i < this->Dimension; ++i)
  618. {
  619. this->spinBox(i)->setValueProxy(this->Proxy.data());
  620. }
  621. if (this->Proxy)
  622. {
  623. connect(this->Proxy.data(), SIGNAL(proxyModified()),
  624. this, SLOT(onValueProxyModified()));
  625. }
  626. this->onValueProxyModified();
  627. }
  628. //----------------------------------------------------------------------------
  629. ctkValueProxy* ctkCoordinatesWidget::valueProxy() const
  630. {
  631. return this->Proxy.data();
  632. }
  633. //----------------------------------------------------------------------------
  634. void ctkCoordinatesWidget::onValueProxyAboutToBeModified()
  635. {
  636. for (int i = 0; i < this->Dimension; ++i)
  637. {
  638. this->spinBox(i)->blockSignals(true);
  639. }
  640. }
  641. //----------------------------------------------------------------------------
  642. void ctkCoordinatesWidget::onValueProxyModified()
  643. {
  644. for (int i = 0; i < this->Dimension; ++i)
  645. {
  646. this->spinBox(i)->blockSignals(false);
  647. }
  648. // Only decimals (not range/nor value) may have change during a proxy
  649. // modification.
  650. this->updateDecimals();
  651. }