ctkMatrixWidget.cpp 15 KB

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