ctkMaterialPropertyWidget.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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->ColorPickerButton, SIGNAL(colorChanged(QColor)),
  46. this, SLOT(onColorChanged(QColor)));
  47. connect(d->OpacitySliderSpinBox, SIGNAL(valueChanged(double)),
  48. this, SLOT(onOpacityChanged(double)));
  49. connect(d->AmbientSliderSpinBox, SIGNAL(valueChanged(double)),
  50. this, SLOT(onAmbientChanged(double)));
  51. connect(d->DiffuseSliderSpinBox, SIGNAL(valueChanged(double)),
  52. this, SLOT(onDiffuseChanged(double)));
  53. connect(d->SpecularSliderSpinBox, SIGNAL(valueChanged(double)),
  54. this, SLOT(onSpecularChanged(double)));
  55. connect(d->SpecularPowerSliderSpinBox, SIGNAL(valueChanged(double)),
  56. this, SLOT(onSpecularPowerChanged(double)));
  57. connect(d->BackfaceCullingCheckBox, SIGNAL(toggled(bool)),
  58. this, SLOT(onBackfaceCullingChanged(bool)));
  59. connect(d->PresetsListWidget, SIGNAL(itemClicked(QListWidgetItem*)),
  60. this, SLOT(selectPreset(QListWidgetItem*)));
  61. // default presets
  62. this->addPreset(Qt::white,1.,1.,0.,0.,1.,"Full ambient eliminating all directional shading.");
  63. this->addPreset(Qt::white,1.,0.2,1.,0.,1.,"Dull material properties (no specular lighting).");
  64. this->addPreset(Qt::white,1.,0.1,0.9,0.2,10.,"Smooth material properties (moderate specular lighting).");
  65. this->addPreset(Qt::white,1.,0.1,0.6,0.5,40.,"Shiny material properties (high specular lighting).");
  66. d->PresetsListWidget->viewport()->setAutoFillBackground( false);
  67. d->PresetsListWidget->setAutoFillBackground( false );
  68. d->PresetsListWidget->setMinimumWidth(
  69. d->PresetsListWidget->frameWidth() // left frame width
  70. + d->PresetsListWidget->count() * d->MaterialPropertyPreviewLabel->sizeHint().width()
  71. + d->PresetsListWidget->frameWidth() ); // right frame width
  72. }
  73. // --------------------------------------------------------------------------
  74. ctkMaterialPropertyWidget::~ctkMaterialPropertyWidget()
  75. {
  76. }
  77. // --------------------------------------------------------------------------
  78. void ctkMaterialPropertyWidget::setColor(const QColor& newColor)
  79. {
  80. Q_D(const ctkMaterialPropertyWidget);
  81. d->ColorPickerButton->setColor(newColor);
  82. }
  83. // --------------------------------------------------------------------------
  84. QColor ctkMaterialPropertyWidget::color()const
  85. {
  86. Q_D(const ctkMaterialPropertyWidget);
  87. return d->ColorPickerButton->color();
  88. }
  89. // --------------------------------------------------------------------------
  90. void ctkMaterialPropertyWidget::setOpacity(double newOpacity)
  91. {
  92. Q_D(const ctkMaterialPropertyWidget);
  93. d->OpacitySliderSpinBox->setValue(newOpacity);
  94. }
  95. // --------------------------------------------------------------------------
  96. double ctkMaterialPropertyWidget::opacity()const
  97. {
  98. Q_D(const ctkMaterialPropertyWidget);
  99. return d->OpacitySliderSpinBox->value();
  100. }
  101. // --------------------------------------------------------------------------
  102. void ctkMaterialPropertyWidget::setAmbient(double newAmbient)
  103. {
  104. Q_D(const ctkMaterialPropertyWidget);
  105. d->AmbientSliderSpinBox->setValue(newAmbient);
  106. }
  107. // --------------------------------------------------------------------------
  108. double ctkMaterialPropertyWidget::ambient()const
  109. {
  110. Q_D(const ctkMaterialPropertyWidget);
  111. return d->AmbientSliderSpinBox->value();
  112. }
  113. // --------------------------------------------------------------------------
  114. void ctkMaterialPropertyWidget::setDiffuse(double newDiffuse)
  115. {
  116. Q_D(const ctkMaterialPropertyWidget);
  117. d->DiffuseSliderSpinBox->setValue(newDiffuse);
  118. }
  119. // --------------------------------------------------------------------------
  120. double ctkMaterialPropertyWidget::diffuse()const
  121. {
  122. Q_D(const ctkMaterialPropertyWidget);
  123. return d->DiffuseSliderSpinBox->value();
  124. }
  125. // --------------------------------------------------------------------------
  126. void ctkMaterialPropertyWidget::setSpecular(double newSpecular)
  127. {
  128. Q_D(const ctkMaterialPropertyWidget);
  129. d->SpecularSliderSpinBox->setValue(newSpecular);
  130. }
  131. // --------------------------------------------------------------------------
  132. double ctkMaterialPropertyWidget::specular()const
  133. {
  134. Q_D(const ctkMaterialPropertyWidget);
  135. return d->SpecularSliderSpinBox->value();
  136. }
  137. // --------------------------------------------------------------------------
  138. void ctkMaterialPropertyWidget::setSpecularPower(double newSpecularPower)
  139. {
  140. Q_D(const ctkMaterialPropertyWidget);
  141. d->SpecularPowerSliderSpinBox->setValue(newSpecularPower);
  142. }
  143. // --------------------------------------------------------------------------
  144. double ctkMaterialPropertyWidget::specularPower()const
  145. {
  146. Q_D(const ctkMaterialPropertyWidget);
  147. return d->SpecularPowerSliderSpinBox->value();
  148. }
  149. // --------------------------------------------------------------------------
  150. void ctkMaterialPropertyWidget::setBackfaceCulling(bool newBackfaceCulling)
  151. {
  152. Q_D(const ctkMaterialPropertyWidget);
  153. d->BackfaceCullingCheckBox->setChecked(newBackfaceCulling);
  154. }
  155. // --------------------------------------------------------------------------
  156. bool ctkMaterialPropertyWidget::backfaceCulling()const
  157. {
  158. Q_D(const ctkMaterialPropertyWidget);
  159. return d->BackfaceCullingCheckBox->isChecked();
  160. }
  161. // --------------------------------------------------------------------------
  162. void ctkMaterialPropertyWidget::onColorChanged(const QColor& newColor)
  163. {
  164. emit colorChanged(newColor);
  165. }
  166. // --------------------------------------------------------------------------
  167. void ctkMaterialPropertyWidget::onOpacityChanged(double newOpacity)
  168. {
  169. emit opacityChanged(newOpacity);
  170. }
  171. // --------------------------------------------------------------------------
  172. void ctkMaterialPropertyWidget::onAmbientChanged(double newAmbient)
  173. {
  174. emit ambientChanged(newAmbient);
  175. }
  176. // --------------------------------------------------------------------------
  177. void ctkMaterialPropertyWidget::onDiffuseChanged(double newDiffuse)
  178. {
  179. emit diffuseChanged(newDiffuse);
  180. }
  181. // --------------------------------------------------------------------------
  182. void ctkMaterialPropertyWidget::onSpecularChanged(double newSpecular)
  183. {
  184. emit specularChanged(newSpecular);
  185. }
  186. // --------------------------------------------------------------------------
  187. void ctkMaterialPropertyWidget::onSpecularPowerChanged(double newSpecularPower)
  188. {
  189. emit specularPowerChanged(newSpecularPower);
  190. }
  191. // --------------------------------------------------------------------------
  192. void ctkMaterialPropertyWidget::onBackfaceCullingChanged(bool newBackfaceCulling)
  193. {
  194. emit backfaceCullingChanged(newBackfaceCulling);
  195. }
  196. // --------------------------------------------------------------------------
  197. void ctkMaterialPropertyWidget::addPreset(
  198. const QColor& color, double opacity,
  199. double ambient, double diffuse, double specular, double power,
  200. const QString& label)
  201. {
  202. Q_D(ctkMaterialPropertyWidget);
  203. d->PresetsListWidget->addItem("");
  204. QListWidgetItem* item = d->PresetsListWidget->item(d->PresetsListWidget->count()-1);
  205. item->setToolTip(label);
  206. item->setData(Qt::UserRole, color);
  207. item->setData(Qt::UserRole + 1, opacity);
  208. item->setData(Qt::UserRole + 2, ambient);
  209. item->setData(Qt::UserRole + 3, diffuse);
  210. item->setData(Qt::UserRole + 4, specular);
  211. item->setData(Qt::UserRole + 5, power);
  212. ctkMaterialPropertyPreviewLabel* preset =
  213. new ctkMaterialPropertyPreviewLabel(color, opacity, ambient, diffuse, specular, power);
  214. preset->setColor(d->MaterialPropertyPreviewLabel->color());
  215. preset->setGridOpacity(d->MaterialPropertyPreviewLabel->gridOpacity());
  216. item->setSizeHint(preset->sizeHint());
  217. d->PresetsListWidget->setItemWidget(item, preset);
  218. }
  219. // --------------------------------------------------------------------------
  220. void ctkMaterialPropertyWidget::selectPreset(QListWidgetItem* preset)
  221. {
  222. Q_D(ctkMaterialPropertyWidget);
  223. d->ColorPickerButton->setColor(preset->data(Qt::UserRole).value<QColor>());
  224. d->OpacitySliderSpinBox->setValue(preset->data(Qt::UserRole + 1).toDouble());
  225. d->AmbientSliderSpinBox->setValue(preset->data(Qt::UserRole + 2).toDouble());
  226. d->DiffuseSliderSpinBox->setValue(preset->data(Qt::UserRole + 3).toDouble());
  227. d->SpecularSliderSpinBox->setValue(preset->data(Qt::UserRole + 4).toDouble());
  228. d->SpecularPowerSliderSpinBox->setValue(preset->data(Qt::UserRole + 5).toDouble());
  229. }
  230. // --------------------------------------------------------------------------
  231. void ctkMaterialPropertyWidget::resizeEvent(QResizeEvent* resize)
  232. {
  233. Q_D(ctkMaterialPropertyWidget);
  234. this->QWidget::resizeEvent(resize);
  235. d->PresetsListWidget->setMaximumWidth(
  236. d->PresetsListWidget->frameWidth() // left frame width
  237. + d->PresetsListWidget->count() * d->MaterialPropertyPreviewLabel->sizeHint().width()
  238. + d->PresetsListWidget->frameWidth() ); // right frame width
  239. d->PresetsListWidget->setMaximumHeight(
  240. d->PresetsListWidget->frameWidth() // top frame height
  241. + d->MaterialPropertyPreviewLabel->sizeHint().height()
  242. + (d->PresetsListWidget->horizontalScrollBar()->isVisibleTo(d->PresetsListWidget) ?
  243. d->PresetsListWidget->horizontalScrollBar()->sizeHint().height() : 0)
  244. + d->PresetsListWidget->frameWidth() ); // bottom frame height
  245. }