ctkCoordinatesWidget.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 <QHBoxLayout>
  16. #include <QDoubleSpinBox>
  17. // CTK includes
  18. #include "ctkCoordinatesWidget.h"
  19. //------------------------------------------------------------------------------
  20. ctkCoordinatesWidget::ctkCoordinatesWidget(QWidget* _parent) :QWidget(_parent)
  21. {
  22. this->Decimals = 3;
  23. this->SingleStep = 1.;
  24. this->Minimum = -100000.;
  25. this->Maximum = 100000.;
  26. this->Dimension = 3;
  27. this->Coordinates = new double [this->Dimension];
  28. QHBoxLayout* hboxLayout = new QHBoxLayout(this);
  29. this->setLayout(hboxLayout);
  30. for (int i = 0; i < this->Dimension; ++i)
  31. {
  32. this->Coordinates[i] = 0.;
  33. this->addSpinBox();
  34. }
  35. hboxLayout->setContentsMargins(0, 0, 0, 0);
  36. }
  37. //------------------------------------------------------------------------------
  38. ctkCoordinatesWidget::~ctkCoordinatesWidget()
  39. {
  40. delete [] this->Coordinates;
  41. }
  42. //------------------------------------------------------------------------------
  43. void ctkCoordinatesWidget::addSpinBox()
  44. {
  45. QDoubleSpinBox* spinBox = new QDoubleSpinBox(this);
  46. spinBox->setDecimals(this->Decimals);
  47. spinBox->setSingleStep(this->SingleStep);
  48. spinBox->setMinimum(this->Minimum);
  49. spinBox->setMaximum(this->Maximum);
  50. connect( spinBox, SIGNAL(valueChanged(double)),
  51. this, SLOT(updateCoordinate(double)));
  52. this->layout()->addWidget(spinBox);
  53. }
  54. //------------------------------------------------------------------------------
  55. void ctkCoordinatesWidget::setDimension(int dim)
  56. {
  57. if (dim < 1)
  58. {
  59. return;
  60. }
  61. double* newPos = new double[dim];
  62. if (dim > this->Dimension)
  63. {
  64. memcpy(newPos, this->Coordinates, this->Dimension * sizeof(double));
  65. for (int i = this->Dimension; i < dim; ++i)
  66. {
  67. newPos[i] = 0.;
  68. this->addSpinBox();
  69. }
  70. }
  71. else
  72. {
  73. memcpy(newPos, this->Coordinates, dim * sizeof(double));
  74. for (int i = this->Dimension - 1 ; i >= dim; --i)
  75. {
  76. QLayoutItem* item = this->layout()->takeAt(i);
  77. QWidget* widget = item ? item->widget() : 0;
  78. delete item;
  79. delete widget;
  80. }
  81. }
  82. delete [] this->Coordinates;
  83. this->Coordinates = newPos;
  84. this->Dimension = dim;
  85. this->updateGeometry();
  86. this->updateCoordinates();
  87. }
  88. //------------------------------------------------------------------------------
  89. int ctkCoordinatesWidget::dimension() const
  90. {
  91. return this->Dimension;
  92. }
  93. //------------------------------------------------------------------------------
  94. void ctkCoordinatesWidget::setMinimum(double min)
  95. {
  96. for (int i = 0; this->layout()->itemAt(i); ++i)
  97. {
  98. QLayoutItem* item = this->layout()->itemAt(i);
  99. QDoubleSpinBox* spinBox = item ? dynamic_cast<QDoubleSpinBox*>(
  100. item->widget()) : 0;
  101. if (spinBox)
  102. {
  103. spinBox->setMinimum(min);
  104. }
  105. }
  106. this->Minimum = min;
  107. }
  108. //------------------------------------------------------------------------------
  109. double ctkCoordinatesWidget::minimum() const
  110. {
  111. return this->Minimum;
  112. }
  113. //------------------------------------------------------------------------------
  114. void ctkCoordinatesWidget::setMaximum(double max)
  115. {
  116. for (int i = 0; this->layout()->itemAt(i); ++i)
  117. {
  118. QLayoutItem* item = this->layout()->itemAt(i);
  119. QDoubleSpinBox* spinBox = item ? dynamic_cast<QDoubleSpinBox*>(
  120. item->widget()) : 0;
  121. if (spinBox)
  122. {
  123. spinBox->setMaximum(max);
  124. }
  125. }
  126. this->Maximum = max;
  127. }
  128. //------------------------------------------------------------------------------
  129. double ctkCoordinatesWidget::maximum() const
  130. {
  131. return this->Maximum;
  132. }
  133. //------------------------------------------------------------------------------
  134. void ctkCoordinatesWidget::setDecimals(int newDecimals)
  135. {
  136. this->Decimals = newDecimals;
  137. for (int i = 0; this->layout()->itemAt(i); ++i)
  138. {
  139. QLayoutItem* item = this->layout()->itemAt(i);
  140. QDoubleSpinBox* spinBox = item ? dynamic_cast<QDoubleSpinBox*>(
  141. item->widget()) : 0;
  142. if (spinBox)
  143. {
  144. spinBox->setDecimals(newDecimals);
  145. }
  146. }
  147. }
  148. //------------------------------------------------------------------------------
  149. int ctkCoordinatesWidget::decimals() const
  150. {
  151. return this->Decimals;
  152. }
  153. //------------------------------------------------------------------------------
  154. void ctkCoordinatesWidget::setSingleStep(double step)
  155. {
  156. for (int i = 0; this->layout()->itemAt(i); ++i)
  157. {
  158. QLayoutItem* item = this->layout()->itemAt(i);
  159. QDoubleSpinBox* spinBox = item ? dynamic_cast<QDoubleSpinBox*>(
  160. item->widget()) : 0;
  161. if (spinBox)
  162. {
  163. spinBox->setSingleStep(step);
  164. }
  165. }
  166. this->SingleStep = step;
  167. }
  168. //------------------------------------------------------------------------------
  169. double ctkCoordinatesWidget::singleStep() const
  170. {
  171. return this->SingleStep;
  172. }
  173. //------------------------------------------------------------------------------
  174. void ctkCoordinatesWidget::setCoordinatesAsString(QString _pos)
  175. {
  176. QStringList posList = _pos.split(',');
  177. if (posList.count() != this->Dimension)
  178. {
  179. return;
  180. }
  181. double* newPos = new double[this->Dimension];
  182. for (int i = 0; i < this->Dimension; ++i)
  183. {
  184. newPos[i] = posList[i].toDouble();
  185. }
  186. this->setCoordinates(newPos);
  187. delete [] newPos;
  188. }
  189. //------------------------------------------------------------------------------
  190. QString ctkCoordinatesWidget::coordinatesAsString()const
  191. {
  192. QString res;
  193. for (int i = 0; i < this->Dimension; ++i)
  194. {
  195. if (i != 0)
  196. {
  197. res += ",";
  198. }
  199. res += QString::number(this->Coordinates[i]);
  200. }
  201. return res;
  202. }
  203. //------------------------------------------------------------------------------
  204. void ctkCoordinatesWidget::setCoordinates(double* _pos)
  205. {
  206. for (int i = 0; i < this->Dimension; ++i)
  207. {
  208. this->Coordinates[i] = _pos[i];
  209. }
  210. bool blocked = this->blockSignals(true);
  211. for (int i = 0; i < this->Dimension; ++i)
  212. {
  213. QLayoutItem* item = this->layout()->itemAt(i);
  214. QDoubleSpinBox* spinBox =
  215. item ? dynamic_cast<QDoubleSpinBox*>(item->widget()) : 0;
  216. if (spinBox)
  217. {
  218. spinBox->setValue(this->Coordinates[i]);
  219. }
  220. }
  221. this->blockSignals(blocked);
  222. this->updateCoordinates();
  223. }
  224. //------------------------------------------------------------------------------
  225. double const * ctkCoordinatesWidget::coordinates()const
  226. {
  227. return this->Coordinates;
  228. }
  229. //------------------------------------------------------------------------------
  230. void ctkCoordinatesWidget::updateCoordinate(double coordinate)
  231. {
  232. for (int i = 0; i < this->Dimension; ++i)
  233. {
  234. QLayoutItem* item = this->layout()->itemAt(i);
  235. QDoubleSpinBox* spinBox =
  236. item ? qobject_cast<QDoubleSpinBox*>(item->widget()) : 0;
  237. if ( spinBox && spinBox == this->sender())
  238. {
  239. this->Coordinates[i] = coordinate;
  240. break;
  241. }
  242. }
  243. emit coordinatesChanged(this->Coordinates);
  244. }
  245. //------------------------------------------------------------------------------
  246. void ctkCoordinatesWidget::updateCoordinates()
  247. {
  248. for (int i = 0; i < this->Dimension; ++i)
  249. {
  250. QLayoutItem* item = this->layout()->itemAt(i);
  251. QDoubleSpinBox* spinBox =
  252. item ? qobject_cast<QDoubleSpinBox*>(item->widget()) : 0;
  253. if ( spinBox)
  254. {
  255. this->Coordinates[i] = spinBox->value();
  256. }
  257. }
  258. emit coordinatesChanged(this->Coordinates);
  259. }