ctkMatrixWidget.cpp 5.4 KB

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