ctkMaterialPropertyWidget.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #include <QListWidgetItem>
  17. #include <QScrollBar>
  18. // CTK includes
  19. #include "ctkMaterialPropertyWidget.h"
  20. #include "ctkMaterialPropertyPreviewLabel.h"
  21. #include "ui_ctkMaterialPropertyWidget.h"
  22. #include "ctkLogger.h"
  23. static ctkLogger logger("org.commontk.libs.widgets.ctkMaterialPropertyWidget");
  24. //-----------------------------------------------------------------------------
  25. class ctkMaterialPropertyWidgetPrivate: public Ui_ctkMaterialPropertyWidget
  26. {
  27. Q_DECLARE_PUBLIC(ctkMaterialPropertyWidget);
  28. protected:
  29. ctkMaterialPropertyWidget* const q_ptr;
  30. public:
  31. ctkMaterialPropertyWidgetPrivate(ctkMaterialPropertyWidget& object);
  32. };
  33. // --------------------------------------------------------------------------
  34. ctkMaterialPropertyWidgetPrivate::ctkMaterialPropertyWidgetPrivate(ctkMaterialPropertyWidget& object)
  35. :q_ptr(&object)
  36. {
  37. }
  38. // --------------------------------------------------------------------------
  39. ctkMaterialPropertyWidget::ctkMaterialPropertyWidget(QWidget* _parent)
  40. : Superclass(_parent)
  41. , d_ptr(new ctkMaterialPropertyWidgetPrivate(*this))
  42. {
  43. Q_D(ctkMaterialPropertyWidget);
  44. d->setupUi(this);
  45. connect(d->PresetsListWidget, SIGNAL(itemClicked(QListWidgetItem*)),
  46. this, SLOT(selectPreset(QListWidgetItem*)));
  47. this->addPreset(1.,0.,0.,1.,"Full ambient eliminating all directional shading.");
  48. this->addPreset(0.2,1.,0.,1.,"Dull material properties (no specular lighting).");
  49. this->addPreset(0.1,0.9,0.2,10.,"Smooth material properties (moderate specular lighting).");
  50. this->addPreset(0.1,0.6,0.5,40.,"Shiny material properties (high specular lighting).");
  51. d->PresetsListWidget->viewport()->setAutoFillBackground( false);
  52. d->PresetsListWidget->setAutoFillBackground( false );
  53. d->PresetsListWidget->setMaximumHeight(
  54. d->MaterialPropertyPreviewLabel->sizeHint().height()
  55. + d->PresetsListWidget->horizontalScrollBar()->sizeHint().height()
  56. + 2. * d->PresetsListWidget->frameWidth());
  57. }
  58. // --------------------------------------------------------------------------
  59. ctkMaterialPropertyWidget::~ctkMaterialPropertyWidget()
  60. {
  61. }
  62. // --------------------------------------------------------------------------
  63. void ctkMaterialPropertyWidget::setAmbient(double newAmbient)
  64. {
  65. Q_D(const ctkMaterialPropertyWidget);
  66. d->DiffuseSliderSpinBox->setValue(newAmbient);
  67. }
  68. // --------------------------------------------------------------------------
  69. double ctkMaterialPropertyWidget::ambient()const
  70. {
  71. Q_D(const ctkMaterialPropertyWidget);
  72. return d->DiffuseSliderSpinBox->value();
  73. }
  74. // --------------------------------------------------------------------------
  75. void ctkMaterialPropertyWidget::setDiffuse(double newDiffuse)
  76. {
  77. Q_D(const ctkMaterialPropertyWidget);
  78. d->DiffuseSliderSpinBox->setValue(newDiffuse);
  79. }
  80. // --------------------------------------------------------------------------
  81. double ctkMaterialPropertyWidget::diffuse()const
  82. {
  83. Q_D(const ctkMaterialPropertyWidget);
  84. return d->DiffuseSliderSpinBox->value();
  85. }
  86. // --------------------------------------------------------------------------
  87. void ctkMaterialPropertyWidget::setSpecular(double newSpecular)
  88. {
  89. Q_D(const ctkMaterialPropertyWidget);
  90. d->SpecularSliderSpinBox->setValue(newSpecular);
  91. }
  92. // --------------------------------------------------------------------------
  93. double ctkMaterialPropertyWidget::specular()const
  94. {
  95. Q_D(const ctkMaterialPropertyWidget);
  96. return d->SpecularSliderSpinBox->value();
  97. }
  98. // --------------------------------------------------------------------------
  99. void ctkMaterialPropertyWidget::setSpecularPower(double newSpecularPower)
  100. {
  101. Q_D(const ctkMaterialPropertyWidget);
  102. d->SpecularPowerSliderSpinBox->setValue(newSpecularPower);
  103. }
  104. // --------------------------------------------------------------------------
  105. double ctkMaterialPropertyWidget::specularPower()const
  106. {
  107. Q_D(const ctkMaterialPropertyWidget);
  108. return d->SpecularPowerSliderSpinBox->value();
  109. }
  110. // --------------------------------------------------------------------------
  111. void ctkMaterialPropertyWidget::addPreset(
  112. double ambient, double diffuse, double specular, double power, const QString& label)
  113. {
  114. Q_D(ctkMaterialPropertyWidget);
  115. d->PresetsListWidget->addItem("");
  116. QListWidgetItem* item = d->PresetsListWidget->item(d->PresetsListWidget->count()-1);
  117. item->setToolTip(label);
  118. item->setData(Qt::UserRole, ambient);
  119. item->setData(Qt::UserRole + 1, diffuse);
  120. item->setData(Qt::UserRole + 2, specular);
  121. item->setData(Qt::UserRole + 3, power);
  122. ctkMaterialPropertyPreviewLabel* preset =
  123. new ctkMaterialPropertyPreviewLabel(ambient, diffuse, specular, power);
  124. preset->setColor(d->MaterialPropertyPreviewLabel->color());
  125. preset->setGridOpacity(d->MaterialPropertyPreviewLabel->gridOpacity());
  126. item->setSizeHint(preset->sizeHint());
  127. d->PresetsListWidget->setItemWidget(item, preset);
  128. }
  129. // --------------------------------------------------------------------------
  130. void ctkMaterialPropertyWidget::selectPreset(QListWidgetItem* preset)
  131. {
  132. Q_D(ctkMaterialPropertyWidget);
  133. d->AmbientSliderSpinBox->setValue(preset->data(Qt::UserRole).toDouble());
  134. d->DiffuseSliderSpinBox->setValue(preset->data(Qt::UserRole + 1).toDouble());
  135. d->SpecularSliderSpinBox->setValue(preset->data(Qt::UserRole + 2).toDouble());
  136. d->SpecularPowerSliderSpinBox->setValue(preset->data(Qt::UserRole + 3).toDouble());
  137. }