ctkCoordinatesWidget.cpp 7.2 KB

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