ctkVTKAbstractMatrixWidget.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. // Qt includes
  15. #include <QDebug>
  16. #include <QVector>
  17. // CTK includes
  18. #include "ctkVTKAbstractMatrixWidget_p.h"
  19. #include "ctkVTKAbstractMatrixWidget.h"
  20. // VTK includes
  21. #include <vtkMatrix4x4.h>
  22. // --------------------------------------------------------------------------
  23. ctkVTKAbstractMatrixWidgetPrivate
  24. ::ctkVTKAbstractMatrixWidgetPrivate(ctkVTKAbstractMatrixWidget& object)
  25. : QObject(0) // will be reparented in init()
  26. , q_ptr(&object)
  27. {
  28. }
  29. // --------------------------------------------------------------------------
  30. ctkVTKAbstractMatrixWidget::~ctkVTKAbstractMatrixWidget()
  31. {
  32. }
  33. // --------------------------------------------------------------------------
  34. void ctkVTKAbstractMatrixWidgetPrivate::init()
  35. {
  36. Q_Q(ctkVTKAbstractMatrixWidget);
  37. this->setParent(q);
  38. connect(q, SIGNAL(matrixChanged()), this, SLOT(updateVTKMatrix()));
  39. this->updateMatrix();
  40. }
  41. // --------------------------------------------------------------------------
  42. void ctkVTKAbstractMatrixWidgetPrivate::setMatrix(vtkMatrix4x4* matrixVariable)
  43. {
  44. qvtkReconnect(this->Matrix.GetPointer(), matrixVariable,
  45. vtkCommand::ModifiedEvent, this, SLOT(updateMatrix()));
  46. this->Matrix = matrixVariable;
  47. this->updateMatrix();
  48. }
  49. // --------------------------------------------------------------------------
  50. vtkMatrix4x4* ctkVTKAbstractMatrixWidgetPrivate::matrix() const
  51. {
  52. return this->Matrix;
  53. }
  54. // --------------------------------------------------------------------------
  55. void ctkVTKAbstractMatrixWidgetPrivate::updateMatrix()
  56. {
  57. Q_Q(ctkVTKAbstractMatrixWidget);
  58. // if there is no transform to show/edit, disable the widget
  59. q->setEnabled(this->Matrix.GetPointer() != 0);
  60. if (this->Matrix.GetPointer() == 0)
  61. {
  62. q->identity();
  63. return;
  64. }
  65. QVector<double> vector;
  66. for (int i=0; i < 4; i++)
  67. {
  68. for (int j=0; j < 4; j++)
  69. {
  70. vector.append(this->Matrix->GetElement(i,j));
  71. }
  72. }
  73. q->setValues( vector );
  74. }
  75. // --------------------------------------------------------------------------
  76. void ctkVTKAbstractMatrixWidgetPrivate::updateVTKMatrix()
  77. {
  78. Q_Q(ctkVTKAbstractMatrixWidget);
  79. if (this->Matrix.GetPointer() == 0)
  80. {
  81. return;
  82. }
  83. double elements[16];
  84. int n = 0;
  85. for (int i=0; i < 4; i++)
  86. {
  87. for (int j=0; j < 4; j++)
  88. {
  89. elements[n++] = q->value(i,j);
  90. }
  91. }
  92. bool blocked = this->qvtkBlockAll(true);
  93. this->Matrix->DeepCopy(elements);
  94. this->qvtkBlockAll(blocked);
  95. }
  96. // --------------------------------------------------------------------------
  97. ctkVTKAbstractMatrixWidget::ctkVTKAbstractMatrixWidget(QWidget* parentVariable)
  98. : Superclass(4, 4, parentVariable)
  99. , d_ptr(new ctkVTKAbstractMatrixWidgetPrivate(*this))
  100. {
  101. Q_D(ctkVTKAbstractMatrixWidget);
  102. d->init();
  103. }
  104. // --------------------------------------------------------------------------
  105. vtkMatrix4x4* ctkVTKAbstractMatrixWidget::matrix()const
  106. {
  107. Q_D(const ctkVTKAbstractMatrixWidget);
  108. return d->matrix();
  109. }
  110. // --------------------------------------------------------------------------
  111. void ctkVTKAbstractMatrixWidget::setMatrixInternal(vtkMatrix4x4* matrixVariable)
  112. {
  113. Q_D(ctkVTKAbstractMatrixWidget);
  114. d->setMatrix(matrixVariable);
  115. }
  116. // --------------------------------------------------------------------------
  117. void ctkVTKAbstractMatrixWidget::setColumnCount(int newColumnCount)
  118. {
  119. Q_UNUSED(newColumnCount);
  120. this->Superclass::setColumnCount(4);
  121. }
  122. // --------------------------------------------------------------------------
  123. void ctkVTKAbstractMatrixWidget::setRowCount(int newRowCount)
  124. {
  125. Q_UNUSED(newRowCount);
  126. this->Superclass::setRowCount(4);
  127. }