ctkMatrixWidget.cpp 10 KB

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