ctkMatrixWidget.cpp 14 KB

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