123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- /*=========================================================================
- Library: CTK
- Copyright (c) Kitware Inc.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0.txt
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- =========================================================================*/
- // Qt includes
- #include <QPainter>
- // CTK includes
- #include "ctkCompilerDetections_p.h" // For CTK_NULLPTR
- #include "ctkVTKScalarsToColorsComboBox.h"
- #include "ctkVTKWidgetsUtils.h"
- // VTK includes
- #include <vtkScalarsToColors.h>
- //-----------------------------------------------------------------------------
- class ctkVTKScalarsToColorsComboBoxPrivate
- {
- Q_DECLARE_PUBLIC(ctkVTKScalarsToColorsComboBox);
- protected:
- ctkVTKScalarsToColorsComboBox* const q_ptr;
- public:
- ctkVTKScalarsToColorsComboBoxPrivate(ctkVTKScalarsToColorsComboBox& object);
- void init();
- };
- // --------------------------------------------------------------------------
- // ctkVTKScalarsToColorsComboBoxPrivate methods
- // --------------------------------------------------------------------------
- ctkVTKScalarsToColorsComboBoxPrivate::ctkVTKScalarsToColorsComboBoxPrivate(
- ctkVTKScalarsToColorsComboBox& object)
- : q_ptr(&object)
- {
- }
- // --------------------------------------------------------------------------
- void ctkVTKScalarsToColorsComboBoxPrivate::init()
- {
- Q_Q(ctkVTKScalarsToColorsComboBox);
- QObject::connect(q->model(),
- SIGNAL(rowsAboutToBeRemoved(const QModelIndex&, int, int)),
- q, SLOT(onRowsAboutToBeRemoved(const QModelIndex&, int, int)));
- q->setIconSize(QSize(100, 20));
- // Add default raw
- q->setDefaultText(q->tr("Select a color transfer function..."));
- q->forceDefault(true);
- // Connect signals and slots
- QObject::connect(q, SIGNAL(currentIndexChanged(int)),
- q, SLOT(onCurrentIndexChanged(int)));
- }
- // --------------------------------------------------------------------------
- // ctkVTKScalarsToColorsComboBox methods
- // --------------------------------------------------------------------------
- ctkVTKScalarsToColorsComboBox::ctkVTKScalarsToColorsComboBox(QWidget* _parent)
- : Superclass(_parent)
- , d_ptr(new ctkVTKScalarsToColorsComboBoxPrivate(*this))
- {
- Q_D(ctkVTKScalarsToColorsComboBox);
- d->init();
- }
- // --------------------------------------------------------------------------
- ctkVTKScalarsToColorsComboBox::~ctkVTKScalarsToColorsComboBox()
- {
- }
- // --------------------------------------------------------------------------
- int ctkVTKScalarsToColorsComboBox::addScalarsToColors(
- vtkScalarsToColors* scFunction, const QString& name)
- {
- QImage img;
- if (scFunction != CTK_NULLPTR)
- {
- scFunction->Register(CTK_NULLPTR);
- img = ctk::scalarsToColorsImage(scFunction, this->iconSize());
- }
- else
- {
- img = QImage(this->iconSize(), QImage::Format::Format_ARGB32);
- img.fill(Qt::transparent);
- }
- this->addItem(QPixmap::fromImage(img), name,
- QVariant::fromValue<void*>(scFunction));
- return count() - 1;
- }
- // --------------------------------------------------------------------------
- vtkScalarsToColors* ctkVTKScalarsToColorsComboBox::getScalarsToColors(
- int index) const
- {
- QVariant data = itemData(index);
- if (!data.isValid())
- {
- return CTK_NULLPTR;
- }
- vtkScalarsToColors* ctf =
- reinterpret_cast<vtkScalarsToColors*>(data.value<void*>());
- return ctf;
- }
- // --------------------------------------------------------------------------
- int ctkVTKScalarsToColorsComboBox::findScalarsToColors(
- vtkScalarsToColors* scFunction) const
- {
- return findData(QVariant::fromValue<void*>(scFunction));
- }
- // --------------------------------------------------------------------------
- void ctkVTKScalarsToColorsComboBox::removeScalarsToColors(
- vtkScalarsToColors* scFunction)
- {
- QComboBox::removeItem(findScalarsToColors(scFunction));
- }
- // --------------------------------------------------------------------------
- vtkScalarsToColors*
- ctkVTKScalarsToColorsComboBox::currentScalarsToColors() const
- {
- return getScalarsToColors(currentIndex());
- }
- // --------------------------------------------------------------------------
- void ctkVTKScalarsToColorsComboBox::setCurrentScalarsToColors(
- vtkScalarsToColors* scFunction)
- {
- setCurrentIndex(findScalarsToColors(scFunction));
- }
- // --------------------------------------------------------------------------
- void ctkVTKScalarsToColorsComboBox::onCurrentIndexChanged(int index)
- {
- Q_D(ctkVTKScalarsToColorsComboBox);
- emit currentScalarsToColorsChanged(getScalarsToColors(index));
- }
- // --------------------------------------------------------------------------
- void ctkVTKScalarsToColorsComboBox::onRowsAboutToBeRemoved(
- const QModelIndex& parent, int first, int last)
- {
- for (int i = first; i <= last; ++i)
- {
- vtkScalarsToColors* scFunction = reinterpret_cast<vtkScalarsToColors*>(
- model()->data(model()->index(i, 0, parent)).value<void*>());
- if (scFunction != CTK_NULLPTR)
- {
- scFunction->Delete();
- }
- }
- }
|