ctkMatrixWidget.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. All rights reserved.
  5. Distributed under a BSD License. See LICENSE.txt file.
  6. This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even
  7. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the above copyright notice for more information.
  9. =========================================================================*/
  10. // CTK includes
  11. #include "ctkMatrixWidget.h"
  12. // Qt includes
  13. #include <Qt>
  14. #include <QHeaderView>
  15. #include <QVariant>
  16. #include <QTableWidgetItem>
  17. #include <QResizeEvent>
  18. #include <QDebug>
  19. //-----------------------------------------------------------------------------
  20. class ctkMatrixWidgetPrivate: public ctkPrivate<ctkMatrixWidget>
  21. {
  22. };
  23. // --------------------------------------------------------------------------
  24. ctkMatrixWidget::ctkMatrixWidget(QWidget* _parent) : Superclass(4, 4, _parent)
  25. {
  26. CTK_INIT_PRIVATE(ctkMatrixWidget);
  27. // Set Read-only
  28. this->setEditTriggers(ctkMatrixWidget::NoEditTriggers);
  29. // Hide headers
  30. this->verticalHeader()->hide();
  31. this->horizontalHeader()->hide();
  32. // Disable scrollBars
  33. this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  34. this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  35. // Define prototype item
  36. QTableWidgetItem* _item = new QTableWidgetItem();
  37. _item->setData(Qt::DisplayRole, QVariant(0.0));
  38. _item->setTextAlignment(Qt::AlignCenter);
  39. // The table takes ownership of the prototype.
  40. this->setItemPrototype(_item);
  41. // Initialize
  42. this->reset();
  43. }
  44. // --------------------------------------------------------------------------
  45. QSize ctkMatrixWidget::minimumSizeHint () const
  46. {
  47. return QSize(this->columnCount() * 25, this->rowCount() * 25);
  48. }
  49. // --------------------------------------------------------------------------
  50. QSize ctkMatrixWidget::sizeHint () const
  51. {
  52. return this->minimumSizeHint();
  53. }
  54. // --------------------------------------------------------------------------
  55. void ctkMatrixWidget::resizeEvent(QResizeEvent * _event)
  56. {
  57. this->Superclass::resizeEvent(_event);
  58. this->adjustRowsColumnsSize(_event->size().width(), _event->size().height());
  59. }
  60. // --------------------------------------------------------------------------
  61. void ctkMatrixWidget::adjustRowsColumnsSize(int _width, int _height)
  62. {
  63. int colwidth = _width / this->columnCount();
  64. int lastColwidth = colwidth + (_width - colwidth * this->columnCount());
  65. //qDebug() << "width:" << width << ",col-width:" << colwidth;
  66. for (int j=0; j < this->columnCount(); j++)
  67. {
  68. bool lastColumn = (j==(this->columnCount()-1));
  69. this->setColumnWidth(j, lastColumn ? lastColwidth : colwidth);
  70. }
  71. int rowheight = _height / this->rowCount();
  72. int lastRowheight = rowheight + (_height - rowheight * this->rowCount());
  73. //qDebug() << "height:" << height << ", row-height:" << rowheight;
  74. for (int i=0; i < this->rowCount(); i++)
  75. {
  76. bool lastRow = (i==(this->rowCount()-1));
  77. this->setRowHeight(i, lastRow ? lastRowheight : rowheight);
  78. }
  79. }
  80. // --------------------------------------------------------------------------
  81. void ctkMatrixWidget::reset()
  82. {
  83. // Initialize 4x4 matrix
  84. for (int i=0; i < this->rowCount(); i++)
  85. {
  86. for (int j=0; j < this->columnCount(); j++)
  87. {
  88. this->setItem(i, j, this->itemPrototype()->clone());
  89. if (i == j)
  90. {
  91. this->setValue(i, j, 1);
  92. }
  93. }
  94. }
  95. }
  96. // --------------------------------------------------------------------------
  97. double ctkMatrixWidget::value(int i, int j)
  98. {
  99. if (i<0 || i>=(this->rowCount()) || j<0 || j>=this->columnCount()) { return 0; }
  100. return this->item(i, j)->data(Qt::DisplayRole).toDouble();
  101. }
  102. // --------------------------------------------------------------------------
  103. void ctkMatrixWidget::setValue(int i, int j, double _value)
  104. {
  105. if (i<0 || i>=(this->rowCount()) || j<0 || j>=this->columnCount()) { return; }
  106. this->item(i, j)->setData(Qt::DisplayRole, QVariant(_value));
  107. }
  108. // --------------------------------------------------------------------------
  109. void ctkMatrixWidget::setVector(const QVector<double> & vector)
  110. {
  111. for (int i=0; i < this->rowCount(); i++)
  112. {
  113. for (int j=0; j < this->columnCount(); j++)
  114. {
  115. this->item(i,j)->setData(Qt::DisplayRole, QVariant(vector.at(i * this->columnCount() + j)));
  116. }
  117. }
  118. }