ctkMatrixWidget.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. // CTK includes
  15. #include "ctkMatrixWidget.h"
  16. // Qt includes
  17. #include <Qt>
  18. #include <QHeaderView>
  19. #include <QVariant>
  20. #include <QTableWidgetItem>
  21. #include <QResizeEvent>
  22. #include <QDoubleSpinBox>
  23. #include <QItemEditorFactory>
  24. #include <QStyledItemDelegate>
  25. #include <QDebug>
  26. //-----------------------------------------------------------------------------
  27. // Custom item editors
  28. namespace
  29. {
  30. //-----------------------------------------------------------------------------
  31. class CustomDoubleSpinBox : public QDoubleSpinBox
  32. {
  33. public:
  34. CustomDoubleSpinBox(QWidget * newParent):QDoubleSpinBox(newParent)
  35. {
  36. // We know that the parentWidget of newParent will be a ctkMatrixWidget because this object is
  37. // created by the QItemEditorFactory
  38. ctkMatrixWidget* matrixWidget = qobject_cast<ctkMatrixWidget*>(newParent->parentWidget());
  39. Q_ASSERT(matrixWidget);
  40. this->setMinimum(matrixWidget->minimum());
  41. this->setMaximum(matrixWidget->maximum());
  42. this->setDecimals(matrixWidget->decimals());
  43. this->setSingleStep(matrixWidget->singleStep());
  44. }
  45. };
  46. }
  47. //-----------------------------------------------------------------------------
  48. class ctkMatrixWidgetPrivate: public ctkPrivate<ctkMatrixWidget>
  49. {
  50. public:
  51. void init();
  52. void validateElements();
  53. // Parameters for the spinbox used to change the value of matrix elements
  54. double Minimum;
  55. double Maximum;
  56. int Decimals;
  57. double SingleStep;
  58. };
  59. //-----------------------------------------------------------------------------
  60. void ctkMatrixWidgetPrivate::init()
  61. {
  62. CTK_P(ctkMatrixWidget);
  63. // Set Read-only
  64. p->setEditable(false);
  65. // Set parameters for the spinbox
  66. this->Minimum = -100;
  67. this->Maximum = 100;
  68. this->Decimals = 2;
  69. this->SingleStep = 0.01;
  70. // Hide headers
  71. p->verticalHeader()->hide();
  72. p->horizontalHeader()->hide();
  73. // Disable scrollBars
  74. p->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  75. p->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  76. // Don't expand for no reason
  77. p->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
  78. // Disable the frame by default
  79. p->setFrameStyle(QFrame::NoFrame);
  80. // Register custom editors
  81. QItemEditorFactory *editorFactory = new QItemEditorFactory;
  82. editorFactory->registerEditor(QVariant::Double, new QStandardItemEditorCreator<CustomDoubleSpinBox>);
  83. QStyledItemDelegate* defaultItemDelegate =
  84. qobject_cast<QStyledItemDelegate*>(p->itemDelegate());
  85. Q_ASSERT(defaultItemDelegate);
  86. defaultItemDelegate->setItemEditorFactory(editorFactory);
  87. // Define prototype item
  88. QTableWidgetItem* _item = new QTableWidgetItem();
  89. _item->setData(Qt::DisplayRole, QVariant(0.0));
  90. _item->setTextAlignment(Qt::AlignCenter);
  91. // The table takes ownership of the prototype.
  92. p->setItemPrototype(_item);
  93. // Initialize
  94. p->reset();
  95. }
  96. // --------------------------------------------------------------------------
  97. void ctkMatrixWidgetPrivate::validateElements()
  98. {
  99. CTK_P(ctkMatrixWidget);
  100. for (int i=0; i < p->rowCount(); i++)
  101. {
  102. for (int j=0; j < p->columnCount(); j++)
  103. {
  104. double value = p->item(i, j)->data(Qt::DisplayRole).toDouble();
  105. if (value < this->Minimum)
  106. {
  107. p->item(i,j)->setData(Qt::DisplayRole, QVariant(this->Minimum));
  108. }
  109. if (value > this->Maximum)
  110. {
  111. p->item(i,j)->setData(Qt::DisplayRole, QVariant(this->Maximum));
  112. }
  113. }
  114. }
  115. }
  116. // --------------------------------------------------------------------------
  117. ctkMatrixWidget::ctkMatrixWidget(QWidget* _parent) : Superclass(4, 4, _parent)
  118. {
  119. CTK_INIT_PRIVATE(ctkMatrixWidget);
  120. CTK_D(ctkMatrixWidget);
  121. d->init();
  122. }
  123. // --------------------------------------------------------------------------
  124. ctkMatrixWidget::ctkMatrixWidget(int rows, int columns, QWidget* _parent)
  125. : Superclass(rows, columns, _parent)
  126. {
  127. CTK_INIT_PRIVATE(ctkMatrixWidget);
  128. CTK_D(ctkMatrixWidget);
  129. d->init();
  130. }
  131. // --------------------------------------------------------------------------
  132. bool ctkMatrixWidget::editable()const
  133. {
  134. return this->editTriggers();
  135. }
  136. // --------------------------------------------------------------------------
  137. void ctkMatrixWidget::setEditable(bool newEditable)
  138. {
  139. if (newEditable)
  140. {
  141. this->setEditTriggers(ctkMatrixWidget::DoubleClicked);
  142. }
  143. else
  144. {
  145. this->setEditTriggers(ctkMatrixWidget::NoEditTriggers);
  146. }
  147. }
  148. // --------------------------------------------------------------------------
  149. CTK_GET_CXX(ctkMatrixWidget, double, minimum, Minimum);
  150. CTK_GET_CXX(ctkMatrixWidget, double, maximum, Maximum);
  151. CTK_GET_CXX(ctkMatrixWidget, double, singleStep, SingleStep);
  152. CTK_SET_CXX(ctkMatrixWidget, double, setSingleStep, SingleStep);
  153. CTK_GET_CXX(ctkMatrixWidget, int, decimals, Decimals);
  154. CTK_SET_CXX(ctkMatrixWidget, int, setDecimals, Decimals);
  155. // --------------------------------------------------------------------------
  156. void ctkMatrixWidget::setMinimum(double newMinimum)
  157. {
  158. CTK_D(ctkMatrixWidget);
  159. d->Minimum = newMinimum;
  160. d->validateElements();
  161. }
  162. // --------------------------------------------------------------------------
  163. void ctkMatrixWidget::setMaximum(double newMaximum)
  164. {
  165. CTK_D(ctkMatrixWidget);
  166. d->Maximum = newMaximum;
  167. d->validateElements();
  168. }
  169. // --------------------------------------------------------------------------
  170. void ctkMatrixWidget::setRange(double newMinimum, double newMaximum)
  171. {
  172. CTK_D(ctkMatrixWidget);
  173. d->Minimum = newMinimum;
  174. d->Maximum = newMaximum;
  175. d->validateElements();
  176. }
  177. // --------------------------------------------------------------------------
  178. QSize ctkMatrixWidget::minimumSizeHint() const
  179. {
  180. int maxWidth = this->sizeHintForColumn(0);
  181. for (int j = 1; j < this->columnCount(); ++j)
  182. {
  183. maxWidth = qMax(maxWidth, this->sizeHintForColumn(j));
  184. }
  185. int maxHeight = this->sizeHintForRow(0);
  186. for (int i = 1; i < this->rowCount(); ++i)
  187. {
  188. maxHeight = qMax(maxHeight, this->sizeHintForRow(i));
  189. }
  190. return QSize(maxWidth*this->columnCount(), maxHeight*this->rowCount());
  191. }
  192. // --------------------------------------------------------------------------
  193. QSize ctkMatrixWidget::sizeHint() const
  194. {
  195. return this->minimumSizeHint();
  196. }
  197. // --------------------------------------------------------------------------
  198. void ctkMatrixWidget::updateGeometries()
  199. {
  200. QSize viewportSize = this->viewport()->size();
  201. // columns
  202. const int ccount = this->columnCount();
  203. int colWidth = viewportSize.width() / ccount;
  204. int lastColWidth = colWidth
  205. + (viewportSize.width() - colWidth * ccount);
  206. for (int j=0; j < ccount; j++)
  207. {
  208. bool lastColumn = (j==(ccount-1));
  209. int newWidth = lastColumn ? lastColWidth : colWidth;
  210. this->setColumnWidth(j, newWidth);
  211. Q_ASSERT(this->columnWidth(j) == newWidth);
  212. }
  213. // rows
  214. const int rcount = this->rowCount();
  215. int rowHeight = viewportSize.height() / rcount;
  216. int lastRowHeight = rowHeight + (viewportSize.height() - rowHeight * rcount);
  217. for (int i=0; i < rcount; i++)
  218. {
  219. bool lastRow = (i==(rcount-1));
  220. int newHeight = lastRow ? lastRowHeight : rowHeight;
  221. this->setRowHeight(i, newHeight);
  222. Q_ASSERT(this->rowHeight(i) == newHeight);
  223. }
  224. this->Superclass::updateGeometries();
  225. }
  226. // --------------------------------------------------------------------------
  227. void ctkMatrixWidget::reset()
  228. {
  229. CTK_D(ctkMatrixWidget);
  230. // Initialize 4x4 matrix
  231. for (int i=0; i < this->rowCount(); i++)
  232. {
  233. for (int j=0; j < this->columnCount(); j++)
  234. {
  235. this->setItem(i, j, this->itemPrototype()->clone());
  236. if (i == j)
  237. {
  238. if (d->Maximum < 1.0)
  239. {
  240. this->setValue(i, j, d->Maximum);
  241. }
  242. else if (d->Minimum > 1.0)
  243. {
  244. this->setValue(i, j, d->Minimum);
  245. }
  246. else
  247. {
  248. this->setValue(i, j, 1.0);
  249. }
  250. }
  251. }
  252. }
  253. }
  254. // --------------------------------------------------------------------------
  255. double ctkMatrixWidget::value(int i, int j)const
  256. {
  257. Q_ASSERT( i>=0 && i<this->rowCount() &&
  258. j>=0 && j<this->columnCount());
  259. return this->item(i, j)->data(Qt::DisplayRole).toDouble();
  260. }
  261. // --------------------------------------------------------------------------
  262. void ctkMatrixWidget::setValue(int i, int j, double _value)
  263. {
  264. CTK_D(ctkMatrixWidget);
  265. Q_ASSERT( i>=0 && i<this->rowCount() &&
  266. j>=0 && j<this->columnCount());
  267. if (_value >= d->Minimum && _value <= d->Maximum)
  268. {
  269. this->item(i, j)->setData(Qt::DisplayRole, QVariant(_value));
  270. }
  271. }
  272. // --------------------------------------------------------------------------
  273. void ctkMatrixWidget::setVector(const QVector<double> & vector)
  274. {
  275. CTK_D(ctkMatrixWidget);
  276. for (int i=0; i < this->rowCount(); i++)
  277. {
  278. for (int j=0; j < this->columnCount(); j++)
  279. {
  280. double value = vector.at(i * this->columnCount() + j);
  281. if (value >= d->Minimum && value <= d->Maximum)
  282. {
  283. this->item(i,j)->setData(Qt::DisplayRole, QVariant(value));
  284. }
  285. }
  286. }
  287. }