ctkMaterialPropertyWidget.cpp 13 KB

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