Bläddra i källkod

ctkMaterialPropertyWidget presets now support invalid colors

When a color is invalid, the preset icon (ctkMaterialPreviewLabel) is
generated using the current color of ctkMaterialPropertyWidget.
Julien Finet 14 år sedan
förälder
incheckning
724b93f1e4
2 ändrade filer med 51 tillägg och 13 borttagningar
  1. 19 8
      Libs/Widgets/ctkMaterialPropertyWidget.cpp
  2. 32 5
      Libs/Widgets/ctkMaterialPropertyWidget.h

+ 19 - 8
Libs/Widgets/ctkMaterialPropertyWidget.cpp

@@ -77,10 +77,10 @@ ctkMaterialPropertyWidget::ctkMaterialPropertyWidget(QWidget* _parent)
   connect(d->PresetsListWidget, SIGNAL(itemClicked(QListWidgetItem*)),
           this, SLOT(selectPreset(QListWidgetItem*)));
   // default presets
-  this->addPreset(Qt::white,1.,1.,0.,0.,1.,"Full ambient eliminating all directional shading.");
-  this->addPreset(Qt::white,1.,0.2,1.,0.,1.,"Dull material properties (no specular lighting).");
-  this->addPreset(Qt::white,1.,0.1,0.9,0.2,10.,"Smooth material properties (moderate specular lighting).");
-  this->addPreset(Qt::white,1.,0.1,0.6,0.5,40.,"Shiny material properties (high specular lighting).");
+  this->addPreset(QColor(),1.,1.,0.,0.,1.,"Full ambient eliminating all directional shading.");
+  this->addPreset(QColor(),1.,0.2,1.,0.,1.,"Dull material properties (no specular lighting).");
+  this->addPreset(QColor(),1.,0.1,0.9,0.2,10.,"Smooth material properties (moderate specular lighting).");
+  this->addPreset(QColor(),1.,0.1,0.6,0.5,40.,"Shiny material properties (high specular lighting).");
   
   d->PresetsListWidget->viewport()->setAutoFillBackground( false);
   d->PresetsListWidget->setAutoFillBackground( false );
@@ -245,15 +245,23 @@ void ctkMaterialPropertyWidget::addPreset(
   d->PresetsListWidget->addItem("");
   QListWidgetItem* item = d->PresetsListWidget->item(d->PresetsListWidget->count()-1);
   item->setToolTip(label);
-  item->setData(Qt::UserRole, color);
+  if (color.isValid())
+    {
+    item->setData(Qt::UserRole, color);
+    }
   item->setData(Qt::UserRole + 1, opacity);
-  item->setData(Qt::UserRole + 2, ambient); 
+  item->setData(Qt::UserRole + 2, ambient);
   item->setData(Qt::UserRole + 3, diffuse);
   item->setData(Qt::UserRole + 4, specular);
   item->setData(Qt::UserRole + 5, power);
   ctkMaterialPropertyPreviewLabel* preset =
     new ctkMaterialPropertyPreviewLabel(color, opacity, ambient, diffuse, specular, power);
-  preset->setColor(d->MaterialPropertyPreviewLabel->color());
+  if (!color.isValid())
+    {
+    connect(this, SIGNAL(colorChanged(const QColor&)),
+            preset, SLOT(setColor(const QColor&)));
+    preset->setColor(this->color());
+    }
   preset->setGridOpacity(d->MaterialPropertyPreviewLabel->gridOpacity());
   item->setSizeHint(preset->sizeHint());
   d->PresetsListWidget->setItemWidget(item, preset);
@@ -263,7 +271,10 @@ void ctkMaterialPropertyWidget::addPreset(
 void ctkMaterialPropertyWidget::selectPreset(QListWidgetItem* preset)
 {
   Q_D(ctkMaterialPropertyWidget);
-  d->ColorPickerButton->setColor(preset->data(Qt::UserRole).value<QColor>());
+  if (preset->data(Qt::UserRole).isValid())
+    {
+    d->ColorPickerButton->setColor(preset->data(Qt::UserRole).value<QColor>());
+    }
   d->OpacitySliderSpinBox->setValue(preset->data(Qt::UserRole + 1).toDouble());
   d->AmbientSliderSpinBox->setValue(preset->data(Qt::UserRole + 2).toDouble());
   d->DiffuseSliderSpinBox->setValue(preset->data(Qt::UserRole + 3).toDouble());

+ 32 - 5
Libs/Widgets/ctkMaterialPropertyWidget.h

@@ -30,15 +30,36 @@
 class ctkMaterialPropertyWidgetPrivate;
 class QListWidgetItem;
 
+/// ctkMaterialPropertyWidget is a panel to control material properties
+/// such as color and lighting coefficients. It contains a preview icon
+/// and a list of presets.
+/// Anytime a property is modified, the preview icon is updated with the
+/// new lighting coefficient.
 class CTK_WIDGETS_EXPORT ctkMaterialPropertyWidget : public QWidget
 {
   Q_OBJECT
+  /// This property holds the color of the material.
   Q_PROPERTY(QColor color  READ color WRITE setColor);
+  /// Opacity component of the material property.
   Q_PROPERTY(double opacity READ opacity WRITE setOpacity);
+  /// This property holds the ambient lighting coefficient,
+  /// it is a nondirectional property.
+  /// Its range is [0,1], where 0 means no ambient light, and 1 means
+  /// full ambient light
+  /// Hint: A range of [0.1,0.5] is more realistic.
   Q_PROPERTY(double ambient READ ambient WRITE setAmbient);
+  /// This property holds the diffuse lighting coefficient.
+  /// Its range is [0,1], where 0 means no diffuse light, and 1 means
+  /// full diffuse light
   Q_PROPERTY(double diffuse READ diffuse WRITE setDiffuse);
+  /// This property holds the specular lighting coefficient.
+  /// Its range is [0,1], where 0 means no specular light, and 1 means
+  /// full specular light
   Q_PROPERTY(double specular READ specular WRITE setSpecular);
+  /// This property holds the power of specular lighting coefficient.
+  /// Its range is [1,50].
   Q_PROPERTY(double specularPower READ specularPower WRITE setSpecularPower);
+  /// This property controls weither backface culling should be enabled or not
   Q_PROPERTY(bool backfaceCulling READ backfaceCulling WRITE setBackfaceCulling);
 public:
   /// Superclass typedef
@@ -46,10 +67,10 @@ public:
 
   /// Constructor
   explicit ctkMaterialPropertyWidget(QWidget* parent = 0);
-  
+
   /// Destructor
   virtual ~ctkMaterialPropertyWidget();
-  
+
   QColor color()const;
   double opacity()const;
 
@@ -57,9 +78,15 @@ public:
   double diffuse()const;
   double specular()const;
   double specularPower()const;
-  
+
   bool backfaceCulling()const;
-  
+
+  /// Add a preset to the preset list. A preview icon will be generated and be
+  /// added on the bottom right corner list. If space is needed, a scrollbar
+  /// will appear. When the user clicks on the icon representing the preset,
+  /// all the preset properties will be applied.
+  /// If color is invalid, the preset color is synchronized with the current
+  /// color property.
   void addPreset(const QColor& color, double opacity,
                  double ambient, double diffuse,
                  double specular, double power,
@@ -94,7 +121,7 @@ protected slots:
   virtual void onDiffuseChanged(double newDiffuse);
   virtual void onSpecularChanged(double newSpecular);
   virtual void onSpecularPowerChanged(double newSpecularPower);
-  
+
   virtual void onBackfaceCullingChanged(bool newBackFaceCulling);
 
   void selectPreset(QListWidgetItem*);