ctkVTKScalarsToColorsComboBox.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 <QPainter>
  16. // CTK includes
  17. #include "ctkCompilerDetections_p.h" // For CTK_NULLPTR
  18. #include "ctkVTKScalarsToColorsComboBox.h"
  19. #include "ctkVTKWidgetsUtils.h"
  20. // VTK includes
  21. #include <vtkScalarsToColors.h>
  22. //-----------------------------------------------------------------------------
  23. class ctkVTKScalarsToColorsComboBoxPrivate
  24. {
  25. Q_DECLARE_PUBLIC(ctkVTKScalarsToColorsComboBox);
  26. protected:
  27. ctkVTKScalarsToColorsComboBox* const q_ptr;
  28. public:
  29. ctkVTKScalarsToColorsComboBoxPrivate(ctkVTKScalarsToColorsComboBox& object);
  30. void init();
  31. };
  32. // --------------------------------------------------------------------------
  33. // ctkVTKScalarsToColorsComboBoxPrivate methods
  34. // --------------------------------------------------------------------------
  35. ctkVTKScalarsToColorsComboBoxPrivate::ctkVTKScalarsToColorsComboBoxPrivate(
  36. ctkVTKScalarsToColorsComboBox& object)
  37. : q_ptr(&object)
  38. {
  39. }
  40. // --------------------------------------------------------------------------
  41. void ctkVTKScalarsToColorsComboBoxPrivate::init()
  42. {
  43. Q_Q(ctkVTKScalarsToColorsComboBox);
  44. QObject::connect(q->model(),
  45. SIGNAL(rowsAboutToBeRemoved(const QModelIndex&, int, int)),
  46. q, SLOT(onRowsAboutToBeRemoved(const QModelIndex&, int, int)));
  47. q->setIconSize(QSize(100, 20));
  48. // Add default raw
  49. q->setDefaultText(q->tr("Select a color transfer function..."));
  50. q->forceDefault(true);
  51. // Connect signals and slots
  52. QObject::connect(q, SIGNAL(currentIndexChanged(int)),
  53. q, SLOT(onCurrentIndexChanged(int)));
  54. }
  55. // --------------------------------------------------------------------------
  56. // ctkVTKScalarsToColorsComboBox methods
  57. // --------------------------------------------------------------------------
  58. ctkVTKScalarsToColorsComboBox::ctkVTKScalarsToColorsComboBox(QWidget* _parent)
  59. : Superclass(_parent)
  60. , d_ptr(new ctkVTKScalarsToColorsComboBoxPrivate(*this))
  61. {
  62. Q_D(ctkVTKScalarsToColorsComboBox);
  63. d->init();
  64. }
  65. // --------------------------------------------------------------------------
  66. ctkVTKScalarsToColorsComboBox::~ctkVTKScalarsToColorsComboBox()
  67. {
  68. }
  69. // --------------------------------------------------------------------------
  70. int ctkVTKScalarsToColorsComboBox::addScalarsToColors(
  71. vtkScalarsToColors* scFunction, const QString& name)
  72. {
  73. QImage img;
  74. if (scFunction != CTK_NULLPTR)
  75. {
  76. scFunction->Register(CTK_NULLPTR);
  77. img = ctk::scalarsToColorsImage(scFunction, this->iconSize());
  78. }
  79. else
  80. {
  81. img = QImage(this->iconSize(), QImage::Format::Format_ARGB32);
  82. img.fill(Qt::transparent);
  83. }
  84. this->addItem(QPixmap::fromImage(img), name,
  85. QVariant::fromValue<void*>(scFunction));
  86. return count() - 1;
  87. }
  88. // --------------------------------------------------------------------------
  89. vtkScalarsToColors* ctkVTKScalarsToColorsComboBox::getScalarsToColors(
  90. int index) const
  91. {
  92. QVariant data = itemData(index);
  93. if (!data.isValid())
  94. {
  95. return CTK_NULLPTR;
  96. }
  97. vtkScalarsToColors* ctf =
  98. reinterpret_cast<vtkScalarsToColors*>(data.value<void*>());
  99. return ctf;
  100. }
  101. // --------------------------------------------------------------------------
  102. int ctkVTKScalarsToColorsComboBox::findScalarsToColors(
  103. vtkScalarsToColors* scFunction) const
  104. {
  105. return findData(QVariant::fromValue<void*>(scFunction));
  106. }
  107. // --------------------------------------------------------------------------
  108. void ctkVTKScalarsToColorsComboBox::removeScalarsToColors(
  109. vtkScalarsToColors* scFunction)
  110. {
  111. QComboBox::removeItem(findScalarsToColors(scFunction));
  112. }
  113. // --------------------------------------------------------------------------
  114. vtkScalarsToColors*
  115. ctkVTKScalarsToColorsComboBox::currentScalarsToColors() const
  116. {
  117. return getScalarsToColors(currentIndex());
  118. }
  119. // --------------------------------------------------------------------------
  120. void ctkVTKScalarsToColorsComboBox::setCurrentScalarsToColors(
  121. vtkScalarsToColors* scFunction)
  122. {
  123. setCurrentIndex(findScalarsToColors(scFunction));
  124. }
  125. // --------------------------------------------------------------------------
  126. void ctkVTKScalarsToColorsComboBox::onCurrentIndexChanged(int index)
  127. {
  128. Q_D(ctkVTKScalarsToColorsComboBox);
  129. emit currentScalarsToColorsChanged(getScalarsToColors(index));
  130. }
  131. // --------------------------------------------------------------------------
  132. void ctkVTKScalarsToColorsComboBox::onRowsAboutToBeRemoved(
  133. const QModelIndex& parent, int first, int last)
  134. {
  135. for (int i = first; i <= last; ++i)
  136. {
  137. vtkScalarsToColors* scFunction = reinterpret_cast<vtkScalarsToColors*>(
  138. model()->data(model()->index(i, 0, parent)).value<void*>());
  139. if (scFunction != CTK_NULLPTR)
  140. {
  141. scFunction->Delete();
  142. }
  143. }
  144. }