ctkCoordinatesWidget.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. All rights reserved.
  5. Distributed under a BSD License. See LICENSE.txt file.
  6. This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even
  7. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the above copyright notice for more information.
  9. =========================================================================*/
  10. // Qt includes
  11. #include <QHBoxLayout>
  12. #include <QDoubleSpinBox>
  13. // CTK includes
  14. #include "ctkCoordinatesWidget.h"
  15. //------------------------------------------------------------------------------
  16. ctkCoordinatesWidget::ctkCoordinatesWidget(QWidget* _parent) :QWidget(_parent)
  17. {
  18. this->Minimum = -100000.;
  19. this->Maximum = 100000.;
  20. this->SingleStep = 1.;
  21. this->Dimension = 3;
  22. this->Coordinates = new double [this->Dimension];
  23. QHBoxLayout* hboxLayout = new QHBoxLayout(this);
  24. this->setLayout(hboxLayout);
  25. for (int i = 0; i < this->Dimension; ++i)
  26. {
  27. this->Coordinates[i] = 0.;
  28. this->AddSpinBox();
  29. }
  30. hboxLayout->setContentsMargins(0, 0, 0, 0);
  31. }
  32. //------------------------------------------------------------------------------
  33. ctkCoordinatesWidget::~ctkCoordinatesWidget()
  34. {
  35. delete [] this->Coordinates;
  36. }
  37. //------------------------------------------------------------------------------
  38. void ctkCoordinatesWidget::AddSpinBox()
  39. {
  40. QDoubleSpinBox* spinBox = new QDoubleSpinBox(this);
  41. spinBox->setMinimum(this->Minimum);
  42. spinBox->setMaximum(this->Maximum);
  43. spinBox->setSingleStep(this->SingleStep);
  44. connect( spinBox, SIGNAL(valueChanged(double)),
  45. this, SLOT(coordinateChanged(double)));
  46. this->layout()->addWidget(spinBox);
  47. }
  48. //------------------------------------------------------------------------------
  49. void ctkCoordinatesWidget::setDimension(int dim)
  50. {
  51. if (dim < 1)
  52. {
  53. return;
  54. }
  55. double* newPos = new double[dim];
  56. if (dim > this->Dimension)
  57. {
  58. memcpy(newPos, this->Coordinates, this->Dimension * sizeof(double));
  59. for (int i = this->Dimension; i < dim; ++i)
  60. {
  61. newPos[i] = 0.;
  62. this->AddSpinBox();
  63. }
  64. }
  65. else
  66. {
  67. memcpy(newPos, this->Coordinates, dim * sizeof(double));
  68. for (int i = this->Dimension - 1 ; i >= dim; --i)
  69. {
  70. QLayoutItem* item = this->layout()->takeAt(i);
  71. QWidget* widget = item ? item->widget() : 0;
  72. delete item;
  73. delete widget;
  74. }
  75. }
  76. delete [] this->Coordinates;
  77. this->Coordinates = newPos;
  78. this->Dimension = dim;
  79. this->updateGeometry();
  80. this->coordinatesChanged();
  81. }
  82. //------------------------------------------------------------------------------
  83. int ctkCoordinatesWidget::dimension() const
  84. {
  85. return this->Dimension;
  86. }
  87. //------------------------------------------------------------------------------
  88. void ctkCoordinatesWidget::setMinimum(double min)
  89. {
  90. for (int i = 0; this->layout()->itemAt(i); ++i)
  91. {
  92. QLayoutItem* item = this->layout()->itemAt(i);
  93. QDoubleSpinBox* spinBox = item ? dynamic_cast<QDoubleSpinBox*>(
  94. item->widget()) : 0;
  95. if (spinBox)
  96. {
  97. spinBox->setMinimum(min);
  98. }
  99. }
  100. this->Minimum = min;
  101. }
  102. //------------------------------------------------------------------------------
  103. double ctkCoordinatesWidget::minimum() const
  104. {
  105. return this->Minimum;
  106. }
  107. //------------------------------------------------------------------------------
  108. void ctkCoordinatesWidget::setMaximum(double max)
  109. {
  110. for (int i = 0; this->layout()->itemAt(i); ++i)
  111. {
  112. QLayoutItem* item = this->layout()->itemAt(i);
  113. QDoubleSpinBox* spinBox = item ? dynamic_cast<QDoubleSpinBox*>(
  114. item->widget()) : 0;
  115. if (spinBox)
  116. {
  117. spinBox->setMaximum(max);
  118. }
  119. }
  120. this->Maximum = max;
  121. }
  122. //------------------------------------------------------------------------------
  123. double ctkCoordinatesWidget::maximum() const
  124. {
  125. return this->Maximum;
  126. }
  127. //------------------------------------------------------------------------------
  128. void ctkCoordinatesWidget::setSingleStep(double step)
  129. {
  130. for (int i = 0; this->layout()->itemAt(i); ++i)
  131. {
  132. QLayoutItem* item = this->layout()->itemAt(i);
  133. QDoubleSpinBox* spinBox = item ? dynamic_cast<QDoubleSpinBox*>(
  134. item->widget()) : 0;
  135. if (spinBox)
  136. {
  137. spinBox->setSingleStep(step);
  138. }
  139. }
  140. this->SingleStep = step;
  141. }
  142. //------------------------------------------------------------------------------
  143. double ctkCoordinatesWidget::singleStep() const
  144. {
  145. return this->SingleStep;
  146. }
  147. //------------------------------------------------------------------------------
  148. void ctkCoordinatesWidget::setCoordinatesAsString(QString _pos)
  149. {
  150. QStringList posList = _pos.split(',');
  151. if (posList.count() != this->Dimension)
  152. {
  153. return;
  154. }
  155. double* newPos = new double[this->Dimension];
  156. for (int i = 0; i < this->Dimension; ++i)
  157. {
  158. newPos[i] = posList[i].toDouble();
  159. }
  160. this->setCoordinates(newPos);
  161. delete [] newPos;
  162. }
  163. //------------------------------------------------------------------------------
  164. QString ctkCoordinatesWidget::coordinatesAsString()const
  165. {
  166. QString res;
  167. for (int i = 0; i < this->Dimension; ++i)
  168. {
  169. if (i != 0)
  170. {
  171. res += ",";
  172. }
  173. res += QString::number(this->Coordinates[i]);
  174. }
  175. return res;
  176. }
  177. //------------------------------------------------------------------------------
  178. void ctkCoordinatesWidget::setCoordinates(double* _pos)
  179. {
  180. for (int i = 0; i < this->Dimension; ++i)
  181. {
  182. this->Coordinates[i] = _pos[i];
  183. }
  184. bool blocked = this->blockSignals(true);
  185. for (int i = 0; i < this->Dimension; ++i)
  186. {
  187. QLayoutItem* item = this->layout()->itemAt(i);
  188. QDoubleSpinBox* spinBox =
  189. item ? dynamic_cast<QDoubleSpinBox*>(item->widget()) : 0;
  190. if (spinBox)
  191. {
  192. spinBox->setValue(this->Coordinates[i]);
  193. }
  194. }
  195. this->blockSignals(blocked);
  196. emit valueChanged(this->Coordinates);
  197. }
  198. //------------------------------------------------------------------------------
  199. double* ctkCoordinatesWidget::coordinates()const
  200. {
  201. return this->Coordinates;
  202. }
  203. //------------------------------------------------------------------------------
  204. void ctkCoordinatesWidget::coordinateChanged(double coordinate)
  205. {
  206. for (int i = 0; i < this->Dimension; ++i)
  207. {
  208. QLayoutItem* item = this->layout()->itemAt(i);
  209. QObject* spinBox =
  210. item ? dynamic_cast<QObject*>(item->widget()) : 0;
  211. if ( spinBox && spinBox == this->sender())
  212. {
  213. this->Coordinates[i] = coordinate;
  214. }
  215. }
  216. emit valueChanged(this->Coordinates);
  217. }
  218. //------------------------------------------------------------------------------
  219. void ctkCoordinatesWidget::coordinatesChanged()
  220. {
  221. for (int i = 0; i < this->Dimension; ++i)
  222. {
  223. QLayoutItem* item = this->layout()->itemAt(i);
  224. QSpinBox* spinBox =
  225. item ? dynamic_cast<QSpinBox*>(item->widget()) : 0;
  226. if ( spinBox)
  227. {
  228. this->Coordinates[i] = spinBox->value();
  229. }
  230. }
  231. emit valueChanged(this->Coordinates);
  232. }