ctkMatrixWidget.cpp 12 KB

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