ctkMatrixWidget.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. // Don't expand for no reason
  40. this->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
  41. // Disable the frame by default
  42. this->setFrameStyle(QFrame::NoFrame);
  43. // Define prototype item
  44. QTableWidgetItem* _item = new QTableWidgetItem();
  45. _item->setData(Qt::DisplayRole, QVariant(0.0));
  46. _item->setTextAlignment(Qt::AlignCenter);
  47. // The table takes ownership of the prototype.
  48. this->setItemPrototype(_item);
  49. // Initialize
  50. this->reset();
  51. }
  52. // --------------------------------------------------------------------------
  53. QSize ctkMatrixWidget::minimumSizeHint () const
  54. {
  55. int maxWidth = this->sizeHintForColumn(0);
  56. for (int j = 1; j < this->columnCount(); ++j)
  57. {
  58. maxWidth = qMax(maxWidth, this->sizeHintForColumn(j));
  59. }
  60. int maxHeight = this->sizeHintForRow(0);
  61. for (int i = 1; i < this->rowCount(); ++i)
  62. {
  63. maxHeight = qMax(maxHeight, this->sizeHintForRow(i));
  64. }
  65. return QSize(maxWidth*this->columnCount(), maxHeight*this->rowCount());
  66. }
  67. // --------------------------------------------------------------------------
  68. QSize ctkMatrixWidget::sizeHint () const
  69. {
  70. return this->minimumSizeHint();
  71. }
  72. // --------------------------------------------------------------------------
  73. void ctkMatrixWidget::updateGeometries()
  74. {
  75. QSize viewportSize = this->viewport()->size();
  76. // columns
  77. const int ccount = this->columnCount();
  78. int colWidth = viewportSize.width() / ccount;
  79. int lastColWidth = colWidth
  80. + (viewportSize.width() - colWidth * ccount);
  81. for (int j=0; j < ccount; j++)
  82. {
  83. bool lastColumn = (j==(ccount-1));
  84. int newWidth = lastColumn ? lastColWidth : colWidth;
  85. this->setColumnWidth(j, newWidth);
  86. Q_ASSERT(this->columnWidth(j) == newWidth);
  87. }
  88. // rows
  89. const int rcount = this->rowCount();
  90. int rowHeight = viewportSize.height() / rcount;
  91. int lastRowHeight = rowHeight + (viewportSize.height() - rowHeight * rcount);
  92. for (int i=0; i < rcount; i++)
  93. {
  94. bool lastRow = (i==(rcount-1));
  95. int newHeight = lastRow ? lastRowHeight : rowHeight;
  96. this->setRowHeight(i, newHeight);
  97. Q_ASSERT(this->rowHeight(i) == newHeight);
  98. }
  99. this->Superclass::updateGeometries();
  100. }
  101. // --------------------------------------------------------------------------
  102. void ctkMatrixWidget::reset()
  103. {
  104. // Initialize 4x4 matrix
  105. for (int i=0; i < this->rowCount(); i++)
  106. {
  107. for (int j=0; j < this->columnCount(); j++)
  108. {
  109. this->setItem(i, j, this->itemPrototype()->clone());
  110. if (i == j)
  111. {
  112. this->setValue(i, j, 1);
  113. }
  114. }
  115. }
  116. }
  117. // --------------------------------------------------------------------------
  118. double ctkMatrixWidget::value(int i, int j)const
  119. {
  120. Q_ASSERT( i>=0 && i<this->rowCount() &&
  121. j>=0 && j<this->columnCount());
  122. return this->item(i, j)->data(Qt::DisplayRole).toDouble();
  123. }
  124. // --------------------------------------------------------------------------
  125. void ctkMatrixWidget::setValue(int i, int j, double _value)
  126. {
  127. Q_ASSERT( i>=0 && i<this->rowCount() &&
  128. j>=0 && j<this->columnCount());
  129. this->item(i, j)->setData(Qt::DisplayRole, QVariant(_value));
  130. }
  131. // --------------------------------------------------------------------------
  132. void ctkMatrixWidget::setVector(const QVector<double> & vector)
  133. {
  134. for (int i=0; i < this->rowCount(); i++)
  135. {
  136. for (int j=0; j < this->columnCount(); j++)
  137. {
  138. this->item(i,j)->setData(Qt::DisplayRole, QVariant(vector.at(i * this->columnCount() + j)));
  139. }
  140. }
  141. }