ctkMatrixWidget.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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.apache.org/licenses/LICENSE-2.0.txt
  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. #include "ctkDoubleSpinBox.h"
  17. // Qt includes
  18. #include <QDebug>
  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 ctkDoubleSpinBox
  34. {
  35. public:
  36. ctkMatrixDoubleSpinBox(QWidget * parentWidget)
  37. : ctkDoubleSpinBox(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. this->connect(this, SIGNAL(decimalsChanged(int)),
  50. matrixWidget, SLOT(setDecimals(int)));
  51. }
  52. };
  53. }
  54. //-----------------------------------------------------------------------------
  55. class ctkMatrixWidgetPrivate
  56. {
  57. Q_DECLARE_PUBLIC(ctkMatrixWidget);
  58. protected:
  59. ctkMatrixWidget* const q_ptr;
  60. public:
  61. ctkMatrixWidgetPrivate(ctkMatrixWidget& object, int rows = 4, int columns = 4);
  62. void init();
  63. void validateItems();
  64. void updateGeometries();
  65. void setIdentityItem(int i, int j);
  66. QTableWidget* Table;
  67. // Parameters for the spinbox used to change the value of matrix elements
  68. double Minimum;
  69. double Maximum;
  70. int Decimals;
  71. double SingleStep;
  72. };
  73. //-----------------------------------------------------------------------------
  74. ctkMatrixWidgetPrivate::ctkMatrixWidgetPrivate(ctkMatrixWidget& object, int rows, int columns)
  75. :q_ptr(&object)
  76. {
  77. this->Table = new QTableWidget(rows, columns);
  78. }
  79. //-----------------------------------------------------------------------------
  80. void ctkMatrixWidgetPrivate::init()
  81. {
  82. Q_Q(ctkMatrixWidget);
  83. this->Table->setParent(q);
  84. QHBoxLayout* layout = new QHBoxLayout(q);
  85. layout->addWidget(this->Table);
  86. layout->setContentsMargins(0,0,0,0);
  87. q->setLayout(layout);
  88. // Set parameters for the spinbox
  89. // TODO: not sure [-100. 100.] is the right default range
  90. this->Minimum = -100;
  91. this->Maximum = 100;
  92. this->Decimals = 2;
  93. this->SingleStep = 0.01;
  94. // Don't select the items
  95. this->Table->setSelectionMode(QAbstractItemView::NoSelection);
  96. // Hide headers
  97. this->Table->verticalHeader()->hide();
  98. this->Table->horizontalHeader()->hide();
  99. // Disable scrollBars
  100. this->Table->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  101. this->Table->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  102. // Don't expand for no reason
  103. q->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
  104. // Disable the frame by default
  105. this->Table->setFrameStyle(QFrame::NoFrame);
  106. // Register custom editors
  107. QItemEditorFactory *editorFactory = new QItemEditorFactory;
  108. editorFactory->registerEditor(QVariant::Double, new QStandardItemEditorCreator<ctkMatrixDoubleSpinBox>);
  109. QStyledItemDelegate* defaultItemDelegate =
  110. qobject_cast<QStyledItemDelegate*>(this->Table->itemDelegate());
  111. Q_ASSERT(defaultItemDelegate);
  112. defaultItemDelegate->setItemEditorFactory(editorFactory);
  113. // Define prototype item
  114. QTableWidgetItem* _item = new QTableWidgetItem;
  115. _item->setData(Qt::DisplayRole, QVariant(0.0));
  116. _item->setTextAlignment(Qt::AlignCenter);
  117. // The table takes ownership of the prototype.
  118. this->Table->setItemPrototype(_item);
  119. QObject::connect(this->Table, SIGNAL(cellChanged(int,int)),
  120. q, SIGNAL(matrixChanged()));
  121. /// \todo Wrap model signals to emit signals when the matrix is changed.
  122. /// Right now you can connect to the signal:
  123. /// matrixWidget->model()->dataChanged(...)
  124. // Set Read-only
  125. q->setEditable(true);
  126. // Initialize
  127. this->validateItems();
  128. this->updateGeometries();
  129. }
  130. // --------------------------------------------------------------------------
  131. void ctkMatrixWidgetPrivate::validateItems()
  132. {
  133. Q_Q(ctkMatrixWidget);
  134. for (int i=0; i < q->rowCount(); ++i)
  135. {
  136. for (int j=0; j < q->columnCount(); ++j)
  137. {
  138. QTableWidgetItem* item = this->Table->item(i, j);
  139. if (!item)
  140. {
  141. this->Table->setItem(i, j , this->Table->itemPrototype()->clone());
  142. this->setIdentityItem(i, j);
  143. }
  144. else
  145. {
  146. double value = item->data(Qt::DisplayRole).toDouble();
  147. item->setData(Qt::DisplayRole,
  148. qBound(this->Minimum, value, this->Maximum));
  149. }
  150. }
  151. }
  152. }
  153. // --------------------------------------------------------------------------
  154. void ctkMatrixWidgetPrivate::setIdentityItem(int i, int j)
  155. {
  156. Q_Q(ctkMatrixWidget);
  157. // the item must exist first
  158. Q_ASSERT(this->Table->item(i, j));
  159. // identity matrix has 1 on the diagonal, 0 everywhere else
  160. double value = (i == j ? 1. : 0.);
  161. // set the value to the table
  162. q->setValue(i, j, value);
  163. }
  164. // --------------------------------------------------------------------------
  165. void ctkMatrixWidgetPrivate::updateGeometries()
  166. {
  167. Q_Q(ctkMatrixWidget);
  168. QSize viewportSize = q->size();
  169. // columns
  170. const int ccount = q->columnCount();
  171. int colWidth = viewportSize.width() / ccount;
  172. int lastColWidth = colWidth
  173. + (viewportSize.width() - colWidth * ccount);
  174. for (int j=0; j < ccount; j++)
  175. {
  176. bool lastColumn = (j==(ccount-1));
  177. int newWidth = lastColumn ? lastColWidth : colWidth;
  178. this->Table->setColumnWidth(j, newWidth);
  179. Q_ASSERT(this->Table->columnWidth(j) == newWidth);
  180. }
  181. // rows
  182. const int rcount = q->rowCount();
  183. int rowHeight = viewportSize.height() / rcount;
  184. int lastRowHeight = rowHeight + (viewportSize.height() - rowHeight * rcount);
  185. for (int i=0; i < rcount; i++)
  186. {
  187. bool lastRow = (i==(rcount-1));
  188. int newHeight = lastRow ? lastRowHeight : rowHeight;
  189. this->Table->setRowHeight(i, newHeight);
  190. Q_ASSERT(this->Table->rowHeight(i) == newHeight);
  191. }
  192. this->Table->updateGeometry();
  193. }
  194. // --------------------------------------------------------------------------
  195. ctkMatrixWidget::ctkMatrixWidget(QWidget* _parent)
  196. :Superclass(_parent)
  197. ,d_ptr(new ctkMatrixWidgetPrivate(*this))
  198. {
  199. Q_D(ctkMatrixWidget);
  200. d->init();
  201. }
  202. // --------------------------------------------------------------------------
  203. ctkMatrixWidget::ctkMatrixWidget(int rows, int columns, QWidget* _parent)
  204. : QWidget(_parent)
  205. , d_ptr(new ctkMatrixWidgetPrivate(*this, rows, columns))
  206. {
  207. Q_D(ctkMatrixWidget);
  208. d->init();
  209. }
  210. // --------------------------------------------------------------------------
  211. ctkMatrixWidget::ctkMatrixWidget(ctkMatrixWidgetPrivate& pvt,
  212. QWidget* _parent)
  213. : Superclass(_parent)
  214. ,d_ptr(&pvt)
  215. {
  216. Q_D(ctkMatrixWidget);
  217. d->init();
  218. }
  219. // --------------------------------------------------------------------------
  220. ctkMatrixWidget::~ctkMatrixWidget()
  221. {
  222. }
  223. // --------------------------------------------------------------------------
  224. int ctkMatrixWidget::columnCount()const
  225. {
  226. Q_D(const ctkMatrixWidget);
  227. return d->Table->columnCount();
  228. }
  229. // --------------------------------------------------------------------------
  230. void ctkMatrixWidget::setColumnCount(int rc)
  231. {
  232. Q_D(ctkMatrixWidget);
  233. d->Table->setColumnCount(rc);
  234. d->validateItems();
  235. d->updateGeometries();
  236. }
  237. // --------------------------------------------------------------------------
  238. int ctkMatrixWidget::rowCount()const
  239. {
  240. Q_D(const ctkMatrixWidget);
  241. return d->Table->rowCount();
  242. }
  243. // --------------------------------------------------------------------------
  244. void ctkMatrixWidget::setRowCount(int rc)
  245. {
  246. Q_D(ctkMatrixWidget);
  247. d->Table->setRowCount(rc);
  248. d->validateItems();
  249. d->updateGeometries();
  250. }
  251. // --------------------------------------------------------------------------
  252. bool ctkMatrixWidget::isEditable()const
  253. {
  254. Q_D(const ctkMatrixWidget);
  255. return d->Table->editTriggers();
  256. }
  257. // --------------------------------------------------------------------------
  258. void ctkMatrixWidget::setEditable(bool newEditable)
  259. {
  260. Q_D(ctkMatrixWidget);
  261. d->Table->setEditTriggers(
  262. newEditable ? QTableWidget::DoubleClicked : QTableWidget::NoEditTriggers);
  263. }
  264. // --------------------------------------------------------------------------
  265. CTK_GET_CPP(ctkMatrixWidget, double, minimum, Minimum);
  266. CTK_GET_CPP(ctkMatrixWidget, double, maximum, Maximum);
  267. CTK_GET_CPP(ctkMatrixWidget, double, singleStep, SingleStep);
  268. CTK_SET_CPP(ctkMatrixWidget, double, setSingleStep, SingleStep);
  269. CTK_GET_CPP(ctkMatrixWidget, int, decimals, Decimals);
  270. // --------------------------------------------------------------------------
  271. void ctkMatrixWidget::setMinimum(double newMinimum)
  272. {
  273. Q_D(ctkMatrixWidget);
  274. d->Minimum = newMinimum;
  275. d->Maximum = qMax(newMinimum, d->Maximum);
  276. d->validateItems();
  277. }
  278. // --------------------------------------------------------------------------
  279. void ctkMatrixWidget::setMaximum(double newMaximum)
  280. {
  281. Q_D(ctkMatrixWidget);
  282. d->Minimum = qMin(d->Minimum, newMaximum);
  283. d->Maximum = newMaximum;
  284. d->validateItems();
  285. }
  286. // --------------------------------------------------------------------------
  287. void ctkMatrixWidget::setRange(double newMinimum, double newMaximum)
  288. {
  289. Q_D(ctkMatrixWidget);
  290. d->Minimum = qMin(newMinimum, newMaximum);
  291. d->Maximum = qMax(newMinimum, newMaximum);
  292. d->validateItems();
  293. }
  294. // --------------------------------------------------------------------------
  295. void ctkMatrixWidget::setDecimals(int decimals)
  296. {
  297. Q_D(ctkMatrixWidget);
  298. d->Decimals = qMax(0, decimals);
  299. }
  300. // --------------------------------------------------------------------------
  301. QSize ctkMatrixWidget::minimumSizeHint() const
  302. {
  303. Q_D(const ctkMatrixWidget);
  304. int maxWidth = d->Table->horizontalHeader()->sectionSizeHint(0);
  305. for (int j = 1; j < this->columnCount(); ++j)
  306. {
  307. maxWidth = qMax(maxWidth, d->Table->horizontalHeader()->sectionSizeHint(j));
  308. }
  309. int maxHeight = d->Table->verticalHeader()->sectionSizeHint(0);
  310. for (int i = 1; i < this->rowCount(); ++i)
  311. {
  312. maxHeight = qMax(maxHeight, d->Table->verticalHeader()->sectionSizeHint(i));
  313. }
  314. return QSize(maxWidth*this->columnCount(), maxHeight*this->rowCount());
  315. }
  316. // --------------------------------------------------------------------------
  317. QSize ctkMatrixWidget::sizeHint() const
  318. {
  319. return this->minimumSizeHint();
  320. }
  321. // --------------------------------------------------------------------------
  322. void ctkMatrixWidget::resizeEvent(QResizeEvent* event)
  323. {
  324. Q_D(ctkMatrixWidget);
  325. this->Superclass::resizeEvent(event);
  326. d->updateGeometries();
  327. }
  328. // --------------------------------------------------------------------------
  329. void ctkMatrixWidget::identity()
  330. {
  331. Q_D(ctkMatrixWidget);
  332. // Initialize 4x4 matrix
  333. for (int i=0; i < this->rowCount(); i++)
  334. {
  335. for (int j=0; j < this->columnCount(); j++)
  336. {
  337. d->setIdentityItem(i,j);
  338. }
  339. }
  340. }
  341. // --------------------------------------------------------------------------
  342. double ctkMatrixWidget::value(int i, int j)const
  343. {
  344. Q_D(const ctkMatrixWidget);
  345. Q_ASSERT( i>=0 && i<this->rowCount() &&
  346. j>=0 && j<this->columnCount());
  347. return d->Table->item(i, j)->data(Qt::DisplayRole).toDouble();
  348. }
  349. // --------------------------------------------------------------------------
  350. void ctkMatrixWidget::setValue(int i, int j, double newValue)
  351. {
  352. Q_D(ctkMatrixWidget);
  353. Q_ASSERT( i>=0 && i<this->rowCount() &&
  354. j>=0 && j<this->columnCount());
  355. newValue = qBound(d->Minimum, newValue, d->Maximum);
  356. d->Table->item(i, j)->setData(Qt::DisplayRole, QVariant(newValue));
  357. }
  358. // --------------------------------------------------------------------------
  359. QVector<double> ctkMatrixWidget::values()const
  360. {
  361. QVector<double> values;
  362. for (int i=0; i < this->rowCount(); i++)
  363. {
  364. for (int j=0; j < this->columnCount(); j++)
  365. {
  366. values.push_back(this->value(i,j));
  367. }
  368. }
  369. return values;
  370. }
  371. // --------------------------------------------------------------------------
  372. void ctkMatrixWidget::setValues(const QVector<double> & vector)
  373. {
  374. Q_D(ctkMatrixWidget);
  375. Q_ASSERT(vector.size() == this->rowCount() * this->columnCount());
  376. // As we are potentially making a lot of changes, just fire a unique
  377. // signal at the end if at least one matrix value has been changed.
  378. bool blocked = this->blockSignals(true);
  379. bool modified = false;
  380. for (int row=0; row < this->rowCount(); ++row)
  381. {
  382. for (int column=0; column < this->columnCount(); ++column)
  383. {
  384. double newValue = vector.at(row * this->columnCount() + column);
  385. newValue = qBound(d->Minimum, newValue, d->Maximum);
  386. if (newValue != this->value(row, column))
  387. {
  388. this->setValue(row, column, newValue);
  389. modified = true;
  390. }
  391. }
  392. }
  393. this->blockSignals(blocked);
  394. if (modified)
  395. {
  396. this->emit matrixChanged();
  397. }
  398. }