Ver código fonte

Add ctkMaterialPropertyPreviewLabel

Julien Finet 14 anos atrás
pai
commit
98b1f4c886

+ 3 - 0
Libs/Widgets/CMakeLists.txt

@@ -54,6 +54,8 @@ SET(KIT_SRCS
   ctkFittedTextBrowser.h
   ctkIconEnginePlugin.cpp
   ctkIconEnginePlugin.h
+  ctkMaterialPropertyPreviewLabel.cpp
+  ctkMaterialPropertyPreviewLabel.h
   ctkMatrixWidget.cpp
   ctkMatrixWidget.h
   ctkMenuButton.cpp
@@ -129,6 +131,7 @@ SET(KIT_MOC_SRCS
   ctkFileDialog.h
   ctkFittedTextBrowser.h
   ctkIconEnginePlugin.h
+  ctkMaterialPropertyPreviewLabel.h
   ctkMatrixWidget.h
   ctkMenuButton.h
   ctkRangeSlider.h

+ 3 - 0
Libs/Widgets/Plugins/CMakeLists.txt

@@ -39,6 +39,8 @@ SET(PLUGIN_SRCS
   #ctkExtensionFactory.h
   ctkFittedTextBrowserPlugin.cpp
   ctkFittedTextBrowserPlugin.h
+  ctkMaterialPropertyPreviewLabelPlugin.cpp
+  ctkMaterialPropertyPreviewLabelPlugin.h
   ctkMatrixWidgetPlugin.cpp
   ctkMatrixWidgetPlugin.h
   ctkMenuButtonPlugin.cpp
@@ -76,6 +78,7 @@ SET(PLUGIN_MOC_SRCS
   ctkDynamicSpacerPlugin.h
   #ctkExtensionFactory.h
   ctkFittedTextBrowserPlugin.h
+  ctkMaterialPropertyPreviewLabelPlugin.h
   ctkMatrixWidgetPlugin.h
   ctkMenuButtonPlugin.h
   ctkRangeSliderPlugin.h

+ 67 - 0
Libs/Widgets/Plugins/ctkMaterialPropertyPreviewLabelPlugin.cpp

@@ -0,0 +1,67 @@
+/*=========================================================================
+
+  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.
+
+=========================================================================*/
+
+// CTK includes
+#include "ctkMaterialPropertyPreviewLabelPlugin.h"
+#include "ctkMaterialPropertyPreviewLabel.h"
+
+//-----------------------------------------------------------------------------
+ctkMaterialPropertyPreviewLabelPlugin::ctkMaterialPropertyPreviewLabelPlugin(QObject *_parent) : QObject(_parent)
+{
+}
+
+//-----------------------------------------------------------------------------
+QWidget *ctkMaterialPropertyPreviewLabelPlugin::createWidget(QWidget *_parent)
+{
+  ctkMaterialPropertyPreviewLabel* _widget = new ctkMaterialPropertyPreviewLabel(_parent);
+  return _widget;
+}
+
+//-----------------------------------------------------------------------------
+QString ctkMaterialPropertyPreviewLabelPlugin::domXml() const
+{
+  return "<widget class=\"ctkMaterialPropertyPreviewLabel\" \
+          name=\"MaterialPropertyPreviewLabel\">\n"
+          "</widget>\n";
+}
+
+//-----------------------------------------------------------------------------
+QIcon ctkMaterialPropertyPreviewLabelPlugin::icon() const
+{
+  return QIcon(":/Icons/pushbutton.png");
+}
+
+//-----------------------------------------------------------------------------
+QString ctkMaterialPropertyPreviewLabelPlugin::includeFile() const
+{
+  return "ctkMaterialPropertyPreviewLabel.h";
+}
+
+//-----------------------------------------------------------------------------
+bool ctkMaterialPropertyPreviewLabelPlugin::isContainer() const
+{
+  return false;
+}
+
+//-----------------------------------------------------------------------------
+QString ctkMaterialPropertyPreviewLabelPlugin::name() const
+{
+  return "ctkMaterialPropertyPreviewLabel";
+}

+ 44 - 0
Libs/Widgets/Plugins/ctkMaterialPropertyPreviewLabelPlugin.h

@@ -0,0 +1,44 @@
+/*=========================================================================
+
+  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 __ctkMaterialPropertyPreviewLabelPlugin_h
+#define __ctkMaterialPropertyPreviewLabelPlugin_h
+
+// CTK includes
+#include "ctkWidgetsAbstractPlugin.h"
+
+class CTK_WIDGETS_PLUGINS_EXPORT ctkMaterialPropertyPreviewLabelPlugin :
+  public QObject,
+  public ctkWidgetsAbstractPlugin
+{
+  Q_OBJECT
+
+public:
+  ctkMaterialPropertyPreviewLabelPlugin(QObject *_parent = 0);
+  
+  QWidget *createWidget(QWidget *_parent);
+  QString  domXml() const;
+  QIcon    icon() const;
+  QString  includeFile() const;
+  bool     isContainer() const;
+  QString  name() const;
+};
+
+#endif

+ 2 - 0
Libs/Widgets/Plugins/ctkWidgetsPlugins.h

@@ -38,6 +38,7 @@
 #include "ctkDoubleSliderPlugin.h"
 #include "ctkDynamicSpacerPlugin.h"
 #include "ctkFittedTextBrowserPlugin.h"
+#include "ctkMaterialPropertyPreviewLabelPlugin.h"
 #include "ctkMatrixWidgetPlugin.h"
 #include "ctkMenuButtonPlugin.h"
 #include "ctkRangeSliderPlugin.h"
@@ -71,6 +72,7 @@ public:
             << new ctkDoubleSliderPlugin
             << new ctkDynamicSpacerPlugin
             << new ctkFittedTextBrowserPlugin
+            << new ctkMaterialPropertyPreviewLabelPlugin
             << new ctkMatrixWidgetPlugin
             << new ctkMenuButtonPlugin
             << new ctkRangeSliderPlugin

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

@@ -22,6 +22,7 @@ CREATE_TEST_SOURCELIST(Tests ${KIT}CppTests.cxx
   ctkDynamicSpacerTest2.cpp
   ctkFileDialogTest1.cpp
   ctkFittedTextBrowserTest1.cpp
+  ctkMaterialPropertyPreviewLabelTest1.cpp
   ctkMatrixWidgetTest1.cpp
   ctkMatrixWidgetTest2.cpp
   ctkMenuButtonTest1.cpp
@@ -92,6 +93,7 @@ SIMPLE_TEST( ctkDynamicSpacerTest1 )
 SIMPLE_TEST( ctkDynamicSpacerTest2 )
 SIMPLE_TEST( ctkFileDialogTest1 )
 SIMPLE_TEST( ctkFittedTextBrowserTest1 )
+SIMPLE_TEST( ctkMaterialPropertyPreviewLabelTest1 )
 SIMPLE_TEST( ctkMatrixWidgetTest1 )
 SIMPLE_TEST( ctkMatrixWidgetTest2 )
 SIMPLE_TEST( ctkMenuButtonTest1 )

+ 46 - 0
Libs/Widgets/Testing/Cpp/ctkMaterialPropertyPreviewLabelTest1.cpp

@@ -0,0 +1,46 @@
+/*=========================================================================
+
+  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 <QTimer>
+
+// CTK includes
+#include "ctkMaterialPropertyPreviewLabel.h"
+
+// STD includes
+#include <iostream>
+
+//-----------------------------------------------------------------------------
+int ctkMaterialPropertyPreviewLabelTest1(int argc, char * argv [] )
+{
+  QApplication app(argc, argv);
+
+  ctkMaterialPropertyPreviewLabel label;
+  label.show();
+
+  if (argc < 2 || QString(argv[1]) != "-I" )
+    {
+    QTimer::singleShot(200, &app, SLOT(quit()));
+    }
+
+  return app.exec();
+}
+

+ 3 - 5
Libs/Widgets/ctkAxesWidget.cpp

@@ -18,7 +18,7 @@
 
 =========================================================================*/
 
-//Qt includes
+// Qt includes
 #include <QDebug>
 #include <QBrush>
 #include <QGridLayout>
@@ -26,12 +26,10 @@
 #include <QMouseEvent>
 #include <QPainter>
 
-//CTK includes
+// CTK includes
 #include "ctkAxesWidget.h"
 
-//Test purposes
-#include <QPushButton>
-
+// STD includes
 #include <cmath>
 #include <math.h>
 

+ 288 - 0
Libs/Widgets/ctkMaterialPropertyPreviewLabel.cpp

@@ -0,0 +1,288 @@
+/*=========================================================================
+
+  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 <QColor>
+#include <QDebug>
+#include <QImage>
+#include <QPainter>
+#include <QVector3D>
+
+//CTK includes
+#include "ctkMaterialPropertyPreviewLabel.h"
+
+// STD includes
+#include <cmath>
+
+//ctkMaterialPropertyPreviewLabelPrivate
+//-----------------------------------------------------------------------------
+class ctkMaterialPropertyPreviewLabelPrivate
+{
+  Q_DECLARE_PUBLIC(ctkMaterialPropertyPreviewLabel);
+protected:
+  ctkMaterialPropertyPreviewLabel* const q_ptr;
+public:
+  ctkMaterialPropertyPreviewLabelPrivate(ctkMaterialPropertyPreviewLabel& object);
+  
+  double Ambient;
+  double Diffuse;
+  double Specular;
+  double SpecularPower;
+  
+  QColor Color;
+  double GridOpacity;
+};
+
+//-----------------------------------------------------------------------------
+ctkMaterialPropertyPreviewLabelPrivate::ctkMaterialPropertyPreviewLabelPrivate(ctkMaterialPropertyPreviewLabel& object)
+  :q_ptr(&object)
+{
+  this->Ambient = 0.5;
+  this->Diffuse = 0.5;
+  this->Specular = 0.5;
+  this->SpecularPower = 50;
+  
+  this->Color = Qt::red;
+  this->GridOpacity = 0.6;
+}
+
+
+//ctkMaterialPropertyPreviewLabel
+//-----------------------------------------------------------------------------
+ctkMaterialPropertyPreviewLabel::ctkMaterialPropertyPreviewLabel(QWidget *newParent)
+  : QFrame(newParent)
+  , d_ptr(new ctkMaterialPropertyPreviewLabelPrivate(*this))
+{
+}
+
+//-----------------------------------------------------------------------------
+ctkMaterialPropertyPreviewLabel::~ctkMaterialPropertyPreviewLabel()
+{
+}
+
+//-----------------------------------------------------------------------------
+void ctkMaterialPropertyPreviewLabel::setAmbient(double newAmbient)
+{
+  Q_D(ctkMaterialPropertyPreviewLabel);
+  d->Ambient = newAmbient;
+  this->update();
+}
+
+//-----------------------------------------------------------------------------
+double ctkMaterialPropertyPreviewLabel::ambient()const
+{
+  Q_D(const ctkMaterialPropertyPreviewLabel);
+  return d->Ambient;
+}
+
+//-----------------------------------------------------------------------------
+void ctkMaterialPropertyPreviewLabel::setDiffuse(double newDiffuse)
+{
+  Q_D(ctkMaterialPropertyPreviewLabel);
+  d->Diffuse = newDiffuse;
+  this->update();
+}
+
+//-----------------------------------------------------------------------------
+double ctkMaterialPropertyPreviewLabel::diffuse()const
+{
+  Q_D(const ctkMaterialPropertyPreviewLabel);
+  return d->Diffuse;
+}
+
+//-----------------------------------------------------------------------------
+void ctkMaterialPropertyPreviewLabel::setSpecular(double newSpecular)
+{
+  Q_D(ctkMaterialPropertyPreviewLabel);
+  d->Specular = newSpecular;
+  this->update();
+}
+
+//-----------------------------------------------------------------------------
+double ctkMaterialPropertyPreviewLabel::specular()const
+{
+  Q_D(const ctkMaterialPropertyPreviewLabel);
+  return d->Specular;
+}
+
+//-----------------------------------------------------------------------------
+void ctkMaterialPropertyPreviewLabel::setSpecularPower(double newSpecularPower)
+{
+  Q_D(ctkMaterialPropertyPreviewLabel);
+  d->SpecularPower = newSpecularPower;
+  this->update();
+}
+
+//-----------------------------------------------------------------------------
+double ctkMaterialPropertyPreviewLabel::specularPower()const
+{
+  Q_D(const ctkMaterialPropertyPreviewLabel);
+  return d->SpecularPower;
+}
+
+//-----------------------------------------------------------------------------
+void ctkMaterialPropertyPreviewLabel::setColor(const QColor& newColor)
+{
+  Q_D(ctkMaterialPropertyPreviewLabel);
+  d->Color = newColor;
+  this->update();
+}
+
+//-----------------------------------------------------------------------------
+QColor ctkMaterialPropertyPreviewLabel::color()const
+{
+  Q_D(const ctkMaterialPropertyPreviewLabel);
+  return d->Color;
+}
+
+//-----------------------------------------------------------------------------
+void ctkMaterialPropertyPreviewLabel::setGridOpacity(double newGridOpacity)
+{
+  Q_D(ctkMaterialPropertyPreviewLabel);
+  d->GridOpacity = newGridOpacity;
+  this->update();
+}
+
+//-----------------------------------------------------------------------------
+double ctkMaterialPropertyPreviewLabel::gridOpacity()const
+{
+  Q_D(const ctkMaterialPropertyPreviewLabel);
+  return d->GridOpacity;
+}
+
+//-----------------------------------------------------------------------------
+int ctkMaterialPropertyPreviewLabel::heightForWidth(int w)const
+{
+  return w;
+}
+
+//-----------------------------------------------------------------------------
+QSize ctkMaterialPropertyPreviewLabel::sizeHint()const
+{
+  return QSize(30,30);
+}
+
+//-----------------------------------------------------------------------------
+void ctkMaterialPropertyPreviewLabel::paintEvent(QPaintEvent* event)
+{
+  Q_UNUSED(event);
+  QImage image(this->size(), QImage::Format_ARGB32);
+  this->draw(image);
+  
+  QPainter widgetPainter(this);
+  widgetPainter.drawImage(0, 0, image);
+}
+
+//-----------------------------------------------------------------------------
+//From vtkKWMaterialPropertyWidget::CreateImage
+void ctkMaterialPropertyPreviewLabel::draw(QImage& image)
+{
+  Q_D(ctkMaterialPropertyPreviewLabel);
+  qreal ambient = d->Ambient;
+  qreal diffuse = d->Diffuse;
+  qreal specular = d->Specular;
+  qreal specular_power = d->SpecularPower;
+
+  int size = qMin(image.width(), image.height());
+  int size8 = qMax(size / 8, 1); 
+  qreal size2 = 0.5 * size;
+  qreal radius2 = size2*size2 - 1;
+
+  QRgb rgba;
+  for (int i = 0; i < image.width(); ++i)
+    {
+    for (int j = 0; j < image.height(); ++j)
+      {
+      qreal dist = static_cast<qreal>((i-size2)*(i-size2) + (j-size2)*(j-size2));
+      if (dist <= radius2)
+        {
+        QVector3D pt;
+        pt.setX( (i-size2) / (size2-1) );
+        pt.setY( (j-size2) / (size2-1) );
+        pt.setZ( sqrt(1. - pt.x()*pt.x() - pt.y()*pt.y()) );
+        
+        QVector3D normal = pt;
+        normal.normalize();
+
+        QVector3D light;
+        light.setX(-5 - pt.x());
+        light.setY(-5 - pt.y());
+        light.setZ( 5 - pt.z());
+        light.normalize();
+
+        QVector3D view;
+        view.setX(-pt.x());
+        view.setY(-pt.y());
+        view.setZ(5 - pt.z());
+        view.normalize();
+        
+        qreal dot = QVector3D::dotProduct(normal, light);
+        QVector3D ref;
+        ref.setX( 2.*normal.x()*dot - light.x());
+        ref.setY( 2.*normal.y()*dot - light.y());
+        ref.setZ( 2.*normal.z()*dot - light.z());
+        ref.normalize();
+
+        qreal diffuseComp = diffuse * .01 * dot;
+        if (diffuseComp < 0)
+          {
+          diffuseComp = 0.;
+          }
+        
+        qreal specularDot = QVector3D::dotProduct(ref, view);
+        if (specularDot < 0)
+          {
+          specularDot = 0.;
+          }
+        
+        qreal specularComp = specular*pow(specularDot, specular_power);
+        
+        QVector3D intensity;
+        intensity.setX( qMin((ambient + diffuseComp)*d->Color.redF() + specularComp, 1.));
+        intensity.setY( qMin((ambient + diffuseComp)*d->Color.greenF() + specularComp, 1.));
+        intensity.setZ( qMin((ambient + diffuseComp)*d->Color.blueF() + specularComp, 1.));
+        
+        rgba = qRgba(static_cast<unsigned char>(255. * intensity.x()),
+                     static_cast<unsigned char>(255. * intensity.y()),
+                     static_cast<unsigned char>(255. * intensity.z()),
+                     255);
+        }
+      else
+        {
+        int iGrid = i / size8;
+        int jGrid = j / size8;
+        
+        if (((iGrid / 2) * 2 == iGrid &&
+             (jGrid / 2) * 2 == jGrid) ||
+            ((iGrid / 2) * 2 != iGrid &&
+             (jGrid / 2) * 2 != jGrid))
+          {
+          rgba = qRgba(0, 0, 0, d->GridOpacity * 255);
+          }
+        else
+          {
+          rgba = qRgba(255, 255, 255, d->GridOpacity * 255);
+          }
+        }
+      image.setPixel(i,j,rgba);
+      }
+    }
+}
+

+ 83 - 0
Libs/Widgets/ctkMaterialPropertyPreviewLabel.h

@@ -0,0 +1,83 @@
+/*=========================================================================
+
+  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.
+
+=========================================================================*/
+// Freely inspired from KWWidgets: vtkKWMaterialPropertyWidget
+
+#ifndef __ctkMaterialPropertyPreviewLabel_h
+#define __ctkMaterialPropertyPreviewLabel_h
+
+// Qt includes
+#include <QFrame>
+
+// CTK includes
+#include "ctkWidgetsExport.h"
+
+class ctkMaterialPropertyPreviewLabelPrivate;
+
+class CTK_WIDGETS_EXPORT ctkMaterialPropertyPreviewLabel : public QFrame
+{
+  Q_OBJECT
+  Q_PROPERTY(double ambient READ ambient WRITE setAmbient)
+  Q_PROPERTY(double diffuse READ diffuse WRITE setDiffuse)
+  Q_PROPERTY(double specular READ specular WRITE setSpecular)
+  Q_PROPERTY(double specularPower READ specularPower WRITE setSpecularPower)
+
+  Q_PROPERTY(QColor color READ color WRITE setColor)
+  Q_PROPERTY(double gridOpacity READ gridOpacity WRITE setGridOpacity)
+public : 
+
+  ctkMaterialPropertyPreviewLabel(QWidget *parent = 0);
+  virtual ~ctkMaterialPropertyPreviewLabel();
+  
+  double ambient()const;
+  double diffuse()const;
+  double specular()const;
+  double specularPower()const;
+  
+  QColor color()const;
+  double gridOpacity()const;
+
+  /// Reimplemented to make it square
+  virtual int heightForWidth(int w)const;
+  
+  virtual QSize sizeHint()const;
+public slots:
+  /// Valid range: [0,1]
+  void setAmbient(double newAbient);
+  /// Valid range: [0,1]
+  void setDiffuse(double newDiffuse);
+  /// Valid range: [0,1]
+  void setSpecular(double newSpecular);
+  /// Valid range: [0,100]
+  void setSpecularPower(double newSpecularPower);
+  
+  void setColor(const QColor& newColor);
+  void setGridOpacity(double newGridOpacity);
+
+protected: 
+  void paintEvent(QPaintEvent *);
+  void draw(QImage& image);
+
+  QScopedPointer<ctkMaterialPropertyPreviewLabelPrivate> d_ptr;
+private :
+  Q_DECLARE_PRIVATE(ctkMaterialPropertyPreviewLabel);
+  Q_DISABLE_COPY(ctkMaterialPropertyPreviewLabel);
+};
+
+#endif