ctkCoordinatesWidget.cpp 22 KB

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