Selaa lähdekoodia

Add ctkVTKDataSetComboBox

Julien Finet 14 vuotta sitten
vanhempi
commit
4d6f0be359

+ 3 - 0
Libs/Visualization/VTK/Widgets/CMakeLists.txt

@@ -24,6 +24,8 @@ SET(KIT_SRCS
   ctkVTKAbstractMatrixWidget_p.h
   ctkVTKDataSetModel.cpp
   ctkVTKDataSetModel.h
+  ctkVTKDataSetArrayComboBox.cpp
+  ctkVTKDataSetArrayComboBox.h
   ctkVTKMatrixWidget.cpp
   ctkVTKMatrixWidget.h
   ctkVTKRenderView.cpp
@@ -45,6 +47,7 @@ SET(KIT_SRCS
 # Headers that should run through moc
 SET(KIT_MOC_SRCS
   ctkVTKAbstractMatrixWidget_p.h
+  ctkVTKDataSetArrayComboBox.h
   ctkVTKDataSetModel.h
   ctkVTKMatrixWidget.h
   ctkVTKRenderView.h

+ 2 - 0
Libs/Visualization/VTK/Widgets/Testing/Cpp/CMakeLists.txt

@@ -4,6 +4,7 @@ SET(KIT ${PROJECT_NAME})
 # Tests
 #
 SET(TEST_SOURCES
+  ctkVTKDataSetArrayComboBoxTest1.cpp
   ctkVTKDataSetModelTest1.cpp
   ctkVTKMatrixWidgetTest1.cpp
   ctkTransferFunctionBarsItemTest1.cpp
@@ -62,6 +63,7 @@ ENDMACRO( SIMPLE_TEST  )
 # Add Tests
 #
 
+SIMPLE_TEST( ctkVTKDataSetArrayComboBoxTest1 )
 SIMPLE_TEST( ctkVTKDataSetModelTest1 )
 SIMPLE_TEST( ctkVTKMatrixWidgetTest1 )
 SIMPLE_TEST( ctkTransferFunctionBarsItemTest1 )

+ 64 - 0
Libs/Visualization/VTK/Widgets/Testing/Cpp/ctkVTKDataSetArrayComboBoxTest1.cpp

@@ -0,0 +1,64 @@
+/*=========================================================================
+
+  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.commontk.org/LICENSE
+
+  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 <QApplication>
+#include <QDebug>
+#include <QTimer>
+
+// CTK includes
+#include "ctkVTKDataSetArrayComboBox.h"
+
+// VTK includes
+#include <vtkCellData.h>
+#include <vtkFloatArray.h>
+#include <vtkIntArray.h>
+#include <vtkPointData.h>
+#include <vtkPoints.h>
+#include <vtkPolyData.h>
+#include <vtkSmartPointer.h>
+
+// STD includes
+#include <iostream>
+
+//-----------------------------------------------------------------------------
+int ctkVTKDataSetArrayComboBoxTest1(int argc, char * argv [] )
+{
+  QApplication app(argc, argv);
+
+  vtkSmartPointer<vtkPolyData> dataSet =
+      vtkSmartPointer<vtkPolyData>::New();
+  vtkSmartPointer<vtkIntArray> ints = vtkSmartPointer<vtkIntArray>::New();
+  ints->SetName("Ints");
+  dataSet->GetPointData()->AddArray(ints);
+  vtkSmartPointer<vtkFloatArray> floats= vtkSmartPointer<vtkFloatArray>::New();
+  floats->SetName("Floats");
+  dataSet->GetCellData()->AddArray(floats);
+  
+  ctkVTKDataSetArrayComboBox comboBox;
+  comboBox.setDataSet(dataSet);
+  comboBox.show();
+
+  if (argc < 2 || QString(argv[1]) != "-I")
+    {
+    QTimer::singleShot(1000, &app, SLOT(quit()));
+    }
+  return app.exec();
+}

+ 158 - 0
Libs/Visualization/VTK/Widgets/ctkVTKDataSetArrayComboBox.cpp

@@ -0,0 +1,158 @@
+/*=========================================================================
+
+  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.commontk.org/LICENSE
+
+  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 <QDebug>
+
+// CTK includes
+#include "ctkVTKDataSetArrayComboBox.h"
+#include "ctkVTKDataSetModel.h"
+
+// VTK includes
+#include <vtkDataArray.h>
+
+//-----------------------------------------------------------------------------
+class ctkVTKDataSetArrayComboBoxPrivate
+{
+  Q_DECLARE_PUBLIC(ctkVTKDataSetArrayComboBox);
+protected:
+  ctkVTKDataSetArrayComboBox* const q_ptr;
+public:
+  ctkVTKDataSetArrayComboBoxPrivate(ctkVTKDataSetArrayComboBox& object);
+  void init();
+  ctkVTKDataSetModel* dataSetModel()const;
+  
+  int indexFromArrayName(const QString& dataArrayName)const;
+  int indexFromArray(vtkDataArray* dataArray)const;
+  vtkDataArray* arrayFromIndex(int index)const;
+  QString arrayNameFromIndex(int index)const;
+};
+
+// --------------------------------------------------------------------------
+// ctkVTKDataSetArrayComboBoxPrivate methods
+
+// --------------------------------------------------------------------------
+ctkVTKDataSetArrayComboBoxPrivate::ctkVTKDataSetArrayComboBoxPrivate(ctkVTKDataSetArrayComboBox& object)
+  :q_ptr(&object)
+{
+}
+
+// --------------------------------------------------------------------------
+void ctkVTKDataSetArrayComboBoxPrivate::init()
+{
+  Q_Q(ctkVTKDataSetArrayComboBox);
+  q->setModel(new ctkVTKDataSetModel);
+  QObject::connect(q, SIGNAL(currentIndexChanged(int)),
+                   q, SLOT(onCurrentIndexChanged(int)));
+}
+
+// --------------------------------------------------------------------------
+ctkVTKDataSetModel* ctkVTKDataSetArrayComboBoxPrivate::dataSetModel()const
+{
+  Q_Q(const ctkVTKDataSetArrayComboBox);
+  return qobject_cast<ctkVTKDataSetModel*>(q->model());
+}
+
+// --------------------------------------------------------------------------
+int ctkVTKDataSetArrayComboBoxPrivate::indexFromArrayName(const QString& dataArrayName)const
+{
+  Q_Q(const ctkVTKDataSetArrayComboBox);
+  return q->findText(dataArrayName);
+}
+
+// --------------------------------------------------------------------------
+int ctkVTKDataSetArrayComboBoxPrivate::indexFromArray(vtkDataArray* dataArray)const
+{
+  return this->dataSetModel()->indexFromArray(dataArray,0).row();
+}
+
+// --------------------------------------------------------------------------
+vtkDataArray* ctkVTKDataSetArrayComboBoxPrivate::arrayFromIndex(int index)const
+{
+  return this->dataSetModel()->arrayFromIndex(
+    this->dataSetModel()->index(index, 0));
+}
+
+// --------------------------------------------------------------------------
+QString ctkVTKDataSetArrayComboBoxPrivate::arrayNameFromIndex(int index)const
+{
+  vtkDataArray* dataArray = this->arrayFromIndex(index);
+  return dataArray ? dataArray->GetName() : "";
+}
+
+// --------------------------------------------------------------------------
+// ctkVTKDataSetArrayComboBox methods
+
+// --------------------------------------------------------------------------
+ctkVTKDataSetArrayComboBox::ctkVTKDataSetArrayComboBox(QWidget* _parent)
+  : Superclass(_parent)
+  , d_ptr(new ctkVTKDataSetArrayComboBoxPrivate(*this))
+{
+  Q_D(ctkVTKDataSetArrayComboBox);
+  d->init();
+}
+
+// --------------------------------------------------------------------------
+ctkVTKDataSetArrayComboBox::~ctkVTKDataSetArrayComboBox()
+{
+}
+
+// --------------------------------------------------------------------------
+vtkDataArray* ctkVTKDataSetArrayComboBox::currentArray()const
+{
+  Q_D(const ctkVTKDataSetArrayComboBox);
+  return d->arrayFromIndex(this->currentIndex());
+}
+
+// --------------------------------------------------------------------------
+void ctkVTKDataSetArrayComboBox::setDataSet(vtkDataSet* dataSet)
+{
+  Q_D(ctkVTKDataSetArrayComboBox);
+  d->dataSetModel()->setDataSet(dataSet);
+}
+
+// --------------------------------------------------------------------------
+vtkDataSet* ctkVTKDataSetArrayComboBox::dataSet()const
+{
+  Q_D(const ctkVTKDataSetArrayComboBox);
+  return d->dataSetModel()->dataSet();
+}
+
+// --------------------------------------------------------------------------
+void ctkVTKDataSetArrayComboBox::setCurrentArray(vtkDataArray* dataArray)
+{
+  Q_D(ctkVTKDataSetArrayComboBox);
+  this->setCurrentIndex(d->indexFromArray(dataArray));
+}
+
+// --------------------------------------------------------------------------
+void ctkVTKDataSetArrayComboBox::setCurrentArray(const QString& dataArrayName)
+{
+  Q_D(ctkVTKDataSetArrayComboBox);
+  this->setCurrentIndex(d->indexFromArrayName(dataArrayName));
+}
+
+// --------------------------------------------------------------------------
+void ctkVTKDataSetArrayComboBox::onCurrentIndexChanged(int index)
+{
+  Q_D(ctkVTKDataSetArrayComboBox);
+  emit currentArrayChanged(d->arrayNameFromIndex(index));
+  emit currentArrayChanged(d->arrayFromIndex(index));
+}

+ 71 - 0
Libs/Visualization/VTK/Widgets/ctkVTKDataSetArrayComboBox.h

@@ -0,0 +1,71 @@
+/*=========================================================================
+
+  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.commontk.org/LICENSE
+
+  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.
+
+=========================================================================*/
+
+#ifndef __ctkVTKDataSetArrayComboBox_h
+#define __ctkVTKDataSetArrayComboBox_h
+
+// Qt includes
+#include <QComboBox>
+
+// CTK includes
+#include "ctkVisualizationVTKWidgetsExport.h"
+class ctkVTKDataSetArrayComboBoxPrivate;
+
+class vtkDataArray;
+class vtkDataSet;
+
+///
+/// QComboBox linked to vtkDataSet field arrays
+class CTK_VISUALIZATION_VTK_WIDGETS_EXPORT ctkVTKDataSetArrayComboBox : public QComboBox
+{
+  Q_OBJECT
+  
+public:
+  /// Superclass typedef
+  typedef QComboBox Superclass;
+  
+  /// Constructors
+  explicit ctkVTKDataSetArrayComboBox(QWidget* parent = 0);
+  virtual ~ctkVTKDataSetArrayComboBox();
+  
+  vtkDataArray* currentArray()const;
+  vtkDataSet* dataSet()const;
+
+public slots:
+  void setDataSet(vtkDataSet* dataSet);
+  /// The array must exist in the dataset
+  void setCurrentArray(vtkDataArray* dataArray);
+  /// the array must exist in the dataset
+  void setCurrentArray(const QString& name);
+
+signals:
+  void currentArrayChanged(vtkDataArray*);
+  void currentArrayChanged(const QString& name);
+protected slots:
+  void onCurrentIndexChanged(int);
+protected:
+  QScopedPointer<ctkVTKDataSetArrayComboBoxPrivate> d_ptr;
+
+private:
+  Q_DECLARE_PRIVATE(ctkVTKDataSetArrayComboBox);
+  Q_DISABLE_COPY(ctkVTKDataSetArrayComboBox);
+};
+
+#endif

+ 8 - 0
Libs/Visualization/VTK/Widgets/ctkVTKDataSetModel.h

@@ -61,6 +61,7 @@ public:
   /// 0 if the index doesn't contain a vtkDataArray
   inline vtkDataArray* arrayFromIndex(const QModelIndex& arrayIndex)const;
   vtkDataArray* arrayFromItem(QStandardItem* nodeItem)const;
+  inline QModelIndex indexFromArray(vtkDataArray* dataArray, int column = 0)const;
   QStandardItem* itemFromArray(vtkDataArray* dataArray, int column = 0)const;
   QModelIndexList indexes(vtkDataArray* dataArray)const;
 
@@ -94,4 +95,11 @@ vtkDataArray* ctkVTKDataSetModel::arrayFromIndex(const QModelIndex &nodeIndex)co
   return this->arrayFromItem(this->itemFromIndex(nodeIndex));
 }
 
+// -----------------------------------------------------------------------------
+QModelIndex ctkVTKDataSetModel::indexFromArray(vtkDataArray* dataArray, int column)const
+{
+  QStandardItem* item = this->itemFromArray(dataArray, column);
+  return item ? item->index() : QModelIndex();
+}
+
 #endif