ctkMatrixWidget.cpp 10 KB

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