ctkVTKSurfaceMaterialPropertyWidget.cpp 7.5 KB

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