ctkMatrixWidget.cpp 14 KB

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