ctkVTKSurfaceMaterialPropertyWidget.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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.commontk.org/LICENSE
  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 <QDebug>
  16. // CTK includes
  17. #include "ctkVTKSurfaceMaterialPropertyWidget.h"
  18. // VTK includes
  19. #include <vtkSmartPointer.h>
  20. #include <vtkProperty.h>
  21. //-----------------------------------------------------------------------------
  22. class ctkVTKSurfaceMaterialPropertyWidgetPrivate
  23. {
  24. Q_DECLARE_PUBLIC(ctkVTKSurfaceMaterialPropertyWidget);
  25. protected:
  26. ctkVTKSurfaceMaterialPropertyWidget* const q_ptr;
  27. public:
  28. ctkVTKSurfaceMaterialPropertyWidgetPrivate(ctkVTKSurfaceMaterialPropertyWidget& object);
  29. vtkSmartPointer<vtkProperty> Property;
  30. double SettingColor;
  31. };
  32. //-----------------------------------------------------------------------------
  33. ctkVTKSurfaceMaterialPropertyWidgetPrivate::ctkVTKSurfaceMaterialPropertyWidgetPrivate(ctkVTKSurfaceMaterialPropertyWidget& object)
  34. :q_ptr(&object)
  35. {
  36. this->SettingColor = false;
  37. }
  38. //-----------------------------------------------------------------------------
  39. ctkVTKSurfaceMaterialPropertyWidget::~ctkVTKSurfaceMaterialPropertyWidget()
  40. {
  41. }
  42. //-----------------------------------------------------------------------------
  43. ctkVTKSurfaceMaterialPropertyWidget::ctkVTKSurfaceMaterialPropertyWidget(QWidget* parentWidget)
  44. : Superclass(parentWidget)
  45. , d_ptr(new ctkVTKSurfaceMaterialPropertyWidgetPrivate(*this))
  46. {
  47. this->updateFromProperty();
  48. }
  49. //-----------------------------------------------------------------------------
  50. ctkVTKSurfaceMaterialPropertyWidget::ctkVTKSurfaceMaterialPropertyWidget(vtkProperty* property, QWidget* parentWidget)
  51. : Superclass(parentWidget)
  52. , d_ptr(new ctkVTKSurfaceMaterialPropertyWidgetPrivate(*this))
  53. {
  54. Q_D(ctkVTKSurfaceMaterialPropertyWidget);
  55. this->setProperty(property);
  56. }
  57. //-----------------------------------------------------------------------------
  58. void ctkVTKSurfaceMaterialPropertyWidget::setProperty(vtkProperty* property)
  59. {
  60. Q_D(ctkVTKSurfaceMaterialPropertyWidget);
  61. if (d->Property.GetPointer() == property)
  62. {
  63. return;
  64. }
  65. qvtkReconnect(d->Property, property, vtkCommand::ModifiedEvent,
  66. this, SLOT(updateFromProperty()));
  67. d->Property = property;
  68. this->updateFromProperty();
  69. }
  70. //-----------------------------------------------------------------------------
  71. vtkProperty* ctkVTKSurfaceMaterialPropertyWidget::property()const
  72. {
  73. Q_D(const ctkVTKSurfaceMaterialPropertyWidget);
  74. return d->Property.GetPointer();
  75. }
  76. //-----------------------------------------------------------------------------
  77. void ctkVTKSurfaceMaterialPropertyWidget::updateFromProperty()
  78. {
  79. Q_D(ctkVTKSurfaceMaterialPropertyWidget);
  80. this->setEnabled(d->Property.GetPointer() != 0);
  81. if (d->Property.GetPointer() == 0 || d->SettingColor)
  82. {
  83. return;
  84. }
  85. double* c = d->Property->GetColor();
  86. this->setColor(QColor::fromRgbF(qMin(c[0],1.), qMin(c[1], 1.), qMin(c[2],1.)));
  87. this->setOpacity(d->Property->GetOpacity());
  88. this->setAmbient(d->Property->GetAmbient());
  89. this->setDiffuse(d->Property->GetDiffuse());
  90. this->setSpecular(d->Property->GetSpecular());
  91. this->setSpecularPower(d->Property->GetSpecularPower());
  92. }
  93. // --------------------------------------------------------------------------
  94. void ctkVTKSurfaceMaterialPropertyWidget::onColorChanged(const QColor& newColor)
  95. {
  96. Q_D(ctkVTKSurfaceMaterialPropertyWidget);
  97. this->Superclass::onColorChanged(newColor);
  98. if (d->Property.GetPointer() != 0)
  99. {
  100. // the value might have changed since we fired the signal, use the current
  101. // up-to-date value then.
  102. const QColor c = this->color();
  103. // Need to work around a VTK bug of SetColor() that fires event
  104. // in an unstable state:
  105. // d->Property->SetColor(c.redF(), c.greenF(), c.blueF());
  106. d->SettingColor = true;
  107. d->Property->SetAmbientColor(c.redF(), c.greenF(), c.blueF());
  108. d->Property->SetDiffuseColor(c.redF(), c.greenF(), c.blueF());
  109. d->Property->SetSpecularColor(c.redF(), c.greenF(), c.blueF());
  110. d->SettingColor = false;
  111. // update just in case something connected to the modified event of the
  112. // vtkProperty modified any attribute
  113. this->updateFromProperty();
  114. }
  115. }
  116. // --------------------------------------------------------------------------
  117. void ctkVTKSurfaceMaterialPropertyWidget::onOpacityChanged(double newOpacity)
  118. {
  119. Q_D(ctkVTKSurfaceMaterialPropertyWidget);
  120. this->Superclass::onOpacityChanged(newOpacity);
  121. if (d->Property.GetPointer() != 0)
  122. {
  123. // the value might have changed since we fired the signal, use the current
  124. // up-to-date value then.
  125. d->Property->SetOpacity(this->opacity());
  126. }
  127. }
  128. // --------------------------------------------------------------------------
  129. void ctkVTKSurfaceMaterialPropertyWidget::onAmbientChanged(double newAmbient)
  130. {
  131. Q_D(ctkVTKSurfaceMaterialPropertyWidget);
  132. this->Superclass::onAmbientChanged(newAmbient);
  133. if (d->Property.GetPointer() != 0)
  134. {
  135. // the value might have changed since we fired the signal, use the current
  136. // up-to-date value then.
  137. d->Property->SetAmbient(this->ambient());
  138. }
  139. }
  140. // --------------------------------------------------------------------------
  141. void ctkVTKSurfaceMaterialPropertyWidget::onDiffuseChanged(double newDiffuse)
  142. {
  143. Q_D(ctkVTKSurfaceMaterialPropertyWidget);
  144. this->Superclass::onDiffuseChanged(newDiffuse);
  145. if (d->Property.GetPointer() != 0)
  146. {
  147. // the value might have changed since we fired the signal, use the current
  148. // up-to-date value then.
  149. d->Property->SetDiffuse(this->diffuse());
  150. }
  151. }
  152. // --------------------------------------------------------------------------
  153. void ctkVTKSurfaceMaterialPropertyWidget::onSpecularChanged(double newSpecular)
  154. {
  155. Q_D(ctkVTKSurfaceMaterialPropertyWidget);
  156. this->Superclass::onSpecularChanged(newSpecular);
  157. if (d->Property.GetPointer() != 0)
  158. {
  159. // the value might have changed since we fired the signal, use the current
  160. // up-to-date value then.
  161. d->Property->SetSpecular(this->specular());
  162. }
  163. }
  164. // --------------------------------------------------------------------------
  165. void ctkVTKSurfaceMaterialPropertyWidget::onSpecularPowerChanged(double newSpecularPower)
  166. {
  167. Q_D(ctkVTKSurfaceMaterialPropertyWidget);
  168. this->Superclass::onSpecularPowerChanged(newSpecularPower);
  169. if (d->Property.GetPointer() != 0)
  170. {
  171. // the value might have changed since we fired the signal, use the current
  172. // up-to-date value then.
  173. d->Property->SetSpecularPower(this->specularPower());
  174. }
  175. }
  176. // --------------------------------------------------------------------------
  177. void ctkVTKSurfaceMaterialPropertyWidget::onBackfaceCullingChanged(bool newBackfaceCulling)
  178. {
  179. Q_D(ctkVTKSurfaceMaterialPropertyWidget);
  180. this->Superclass::onBackfaceCullingChanged(newBackfaceCulling);
  181. if (d->Property.GetPointer() != 0)
  182. {
  183. // the value might have changed since we fired the signal, use the current
  184. // up-to-date value then.
  185. d->Property->SetBackfaceCulling(this->backfaceCulling());
  186. }
  187. }