ctkVTKDataSetArrayComboBox.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. // CTK includes
  17. #include "ctkVTKDataSetArrayComboBox.h"
  18. #include "ctkVTKDataSetModel.h"
  19. // VTK includes
  20. #include <vtkDataArray.h>
  21. //-----------------------------------------------------------------------------
  22. class ctkVTKDataSetArrayComboBoxPrivate
  23. {
  24. Q_DECLARE_PUBLIC(ctkVTKDataSetArrayComboBox);
  25. protected:
  26. ctkVTKDataSetArrayComboBox* const q_ptr;
  27. public:
  28. ctkVTKDataSetArrayComboBoxPrivate(ctkVTKDataSetArrayComboBox& object);
  29. void init();
  30. ctkVTKDataSetModel* dataSetModel()const;
  31. int indexFromArrayName(const QString& dataArrayName)const;
  32. int indexFromArray(vtkDataArray* dataArray)const;
  33. vtkDataArray* arrayFromIndex(int index)const;
  34. QString arrayNameFromIndex(int index)const;
  35. };
  36. // --------------------------------------------------------------------------
  37. // ctkVTKDataSetArrayComboBoxPrivate methods
  38. // --------------------------------------------------------------------------
  39. ctkVTKDataSetArrayComboBoxPrivate::ctkVTKDataSetArrayComboBoxPrivate(ctkVTKDataSetArrayComboBox& object)
  40. :q_ptr(&object)
  41. {
  42. }
  43. // --------------------------------------------------------------------------
  44. void ctkVTKDataSetArrayComboBoxPrivate::init()
  45. {
  46. Q_Q(ctkVTKDataSetArrayComboBox);
  47. q->setModel(new ctkVTKDataSetModel(q));
  48. QObject::connect(q, SIGNAL(currentIndexChanged(int)),
  49. q, SLOT(onCurrentIndexChanged(int)));
  50. }
  51. // --------------------------------------------------------------------------
  52. ctkVTKDataSetModel* ctkVTKDataSetArrayComboBoxPrivate::dataSetModel()const
  53. {
  54. Q_Q(const ctkVTKDataSetArrayComboBox);
  55. return qobject_cast<ctkVTKDataSetModel*>(q->model());
  56. }
  57. // --------------------------------------------------------------------------
  58. int ctkVTKDataSetArrayComboBoxPrivate::indexFromArrayName(const QString& dataArrayName)const
  59. {
  60. Q_Q(const ctkVTKDataSetArrayComboBox);
  61. return q->findText(dataArrayName);
  62. }
  63. // --------------------------------------------------------------------------
  64. int ctkVTKDataSetArrayComboBoxPrivate::indexFromArray(vtkDataArray* dataArray)const
  65. {
  66. return this->dataSetModel()->indexFromArray(dataArray,0).row();
  67. }
  68. // --------------------------------------------------------------------------
  69. vtkDataArray* ctkVTKDataSetArrayComboBoxPrivate::arrayFromIndex(int index)const
  70. {
  71. return this->dataSetModel()->arrayFromIndex(
  72. this->dataSetModel()->index(index, 0));
  73. }
  74. // --------------------------------------------------------------------------
  75. QString ctkVTKDataSetArrayComboBoxPrivate::arrayNameFromIndex(int index)const
  76. {
  77. vtkDataArray* dataArray = this->arrayFromIndex(index);
  78. return dataArray ? dataArray->GetName() : "";
  79. }
  80. // --------------------------------------------------------------------------
  81. // ctkVTKDataSetArrayComboBox methods
  82. // --------------------------------------------------------------------------
  83. ctkVTKDataSetArrayComboBox::ctkVTKDataSetArrayComboBox(QWidget* _parent)
  84. : Superclass(_parent)
  85. , d_ptr(new ctkVTKDataSetArrayComboBoxPrivate(*this))
  86. {
  87. Q_D(ctkVTKDataSetArrayComboBox);
  88. d->init();
  89. }
  90. // --------------------------------------------------------------------------
  91. ctkVTKDataSetArrayComboBox::~ctkVTKDataSetArrayComboBox()
  92. {
  93. }
  94. // --------------------------------------------------------------------------
  95. vtkDataArray* ctkVTKDataSetArrayComboBox::currentArray()const
  96. {
  97. Q_D(const ctkVTKDataSetArrayComboBox);
  98. return d->arrayFromIndex(this->currentIndex());
  99. }
  100. // --------------------------------------------------------------------------
  101. QString ctkVTKDataSetArrayComboBox::currentArrayName()const
  102. {
  103. vtkDataArray* dataArray = this->currentArray();
  104. return dataArray ? dataArray->GetName() : "";
  105. }
  106. // --------------------------------------------------------------------------
  107. void ctkVTKDataSetArrayComboBox::setDataSet(vtkDataSet* dataSet)
  108. {
  109. Q_D(ctkVTKDataSetArrayComboBox);
  110. d->dataSetModel()->setDataSet(dataSet);
  111. }
  112. // --------------------------------------------------------------------------
  113. vtkDataSet* ctkVTKDataSetArrayComboBox::dataSet()const
  114. {
  115. Q_D(const ctkVTKDataSetArrayComboBox);
  116. return d->dataSetModel()->dataSet();
  117. }
  118. // --------------------------------------------------------------------------
  119. void ctkVTKDataSetArrayComboBox::setCurrentArray(vtkDataArray* dataArray)
  120. {
  121. Q_D(ctkVTKDataSetArrayComboBox);
  122. this->setCurrentIndex(d->indexFromArray(dataArray));
  123. }
  124. // --------------------------------------------------------------------------
  125. void ctkVTKDataSetArrayComboBox::setCurrentArray(const QString& dataArrayName)
  126. {
  127. Q_D(ctkVTKDataSetArrayComboBox);
  128. this->setCurrentIndex(d->indexFromArrayName(dataArrayName));
  129. }
  130. // --------------------------------------------------------------------------
  131. void ctkVTKDataSetArrayComboBox::onCurrentIndexChanged(int index)
  132. {
  133. Q_D(ctkVTKDataSetArrayComboBox);
  134. emit currentArrayChanged(d->arrayNameFromIndex(index));
  135. emit currentArrayChanged(d->arrayFromIndex(index));
  136. }