ctkMatrixWidget.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) 2010 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.commontk.org/LICENSE
  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 <Qt>
  18. #include <QHeaderView>
  19. #include <QVariant>
  20. #include <QTableWidgetItem>
  21. #include <QResizeEvent>
  22. #include <QDebug>
  23. //-----------------------------------------------------------------------------
  24. class ctkMatrixWidgetPrivate: public ctkPrivate<ctkMatrixWidget>
  25. {
  26. };
  27. // --------------------------------------------------------------------------
  28. ctkMatrixWidget::ctkMatrixWidget(QWidget* _parent) : Superclass(4, 4, _parent)
  29. {
  30. CTK_INIT_PRIVATE(ctkMatrixWidget);
  31. // Set Read-only
  32. this->setEditTriggers(ctkMatrixWidget::NoEditTriggers);
  33. // Hide headers
  34. this->verticalHeader()->hide();
  35. this->horizontalHeader()->hide();
  36. // Disable scrollBars
  37. this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  38. this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  39. // Define prototype item
  40. QTableWidgetItem* _item = new QTableWidgetItem();
  41. _item->setData(Qt::DisplayRole, QVariant(0.0));
  42. _item->setTextAlignment(Qt::AlignCenter);
  43. // The table takes ownership of the prototype.
  44. this->setItemPrototype(_item);
  45. // Initialize
  46. this->reset();
  47. }
  48. // --------------------------------------------------------------------------
  49. QSize ctkMatrixWidget::minimumSizeHint () const
  50. {
  51. return QSize(this->columnCount() * 25, this->rowCount() * 25);
  52. }
  53. // --------------------------------------------------------------------------
  54. QSize ctkMatrixWidget::sizeHint () const
  55. {
  56. return this->minimumSizeHint();
  57. }
  58. // --------------------------------------------------------------------------
  59. void ctkMatrixWidget::resizeEvent(QResizeEvent * _event)
  60. {
  61. this->Superclass::resizeEvent(_event);
  62. this->adjustRowsColumnsSize(_event->size().width(), _event->size().height());
  63. }
  64. // --------------------------------------------------------------------------
  65. void ctkMatrixWidget::adjustRowsColumnsSize(int _width, int _height)
  66. {
  67. int colwidth = _width / this->columnCount();
  68. int lastColwidth = colwidth + (_width - colwidth * this->columnCount());
  69. //qDebug() << "width:" << width << ",col-width:" << colwidth;
  70. for (int j=0; j < this->columnCount(); j++)
  71. {
  72. bool lastColumn = (j==(this->columnCount()-1));
  73. this->setColumnWidth(j, lastColumn ? lastColwidth : colwidth);
  74. }
  75. int rowheight = _height / this->rowCount();
  76. int lastRowheight = rowheight + (_height - rowheight * this->rowCount());
  77. //qDebug() << "height:" << height << ", row-height:" << rowheight;
  78. for (int i=0; i < this->rowCount(); i++)
  79. {
  80. bool lastRow = (i==(this->rowCount()-1));
  81. this->setRowHeight(i, lastRow ? lastRowheight : rowheight);
  82. }
  83. }
  84. // --------------------------------------------------------------------------
  85. void ctkMatrixWidget::reset()
  86. {
  87. // Initialize 4x4 matrix
  88. for (int i=0; i < this->rowCount(); i++)
  89. {
  90. for (int j=0; j < this->columnCount(); j++)
  91. {
  92. this->setItem(i, j, this->itemPrototype()->clone());
  93. if (i == j)
  94. {
  95. this->setValue(i, j, 1);
  96. }
  97. }
  98. }
  99. }
  100. // --------------------------------------------------------------------------
  101. double ctkMatrixWidget::value(int i, int j)
  102. {
  103. if (i<0 || i>=(this->rowCount()) || j<0 || j>=this->columnCount()) { return 0; }
  104. return this->item(i, j)->data(Qt::DisplayRole).toDouble();
  105. }
  106. // --------------------------------------------------------------------------
  107. void ctkMatrixWidget::setValue(int i, int j, double _value)
  108. {
  109. if (i<0 || i>=(this->rowCount()) || j<0 || j>=this->columnCount()) { return; }
  110. this->item(i, j)->setData(Qt::DisplayRole, QVariant(_value));
  111. }
  112. // --------------------------------------------------------------------------
  113. void ctkMatrixWidget::setVector(const QVector<double> & vector)
  114. {
  115. for (int i=0; i < this->rowCount(); i++)
  116. {
  117. for (int j=0; j < this->columnCount(); j++)
  118. {
  119. this->item(i,j)->setData(Qt::DisplayRole, QVariant(vector.at(i * this->columnCount() + j)));
  120. }
  121. }
  122. }