ctkVTKScalarsToColorsWidget.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 "ctkLogger.h"
  18. #include "ctkVTKScalarsToColorsView.h"
  19. #include "ctkVTKScalarsToColorsWidget.h"
  20. #include "ui_ctkVTKScalarsToColorsWidget.h"
  21. // VTK includes
  22. #include <vtkAxis.h>
  23. #include <vtkChartXY.h>
  24. #include <vtkColorTransferControlPointsItem.h>
  25. #include <vtkColorTransferFunction.h>
  26. #include <vtkColorTransferFunctionItem.h>
  27. #include <vtkCompositeControlPointsItem.h>
  28. #include <vtkCompositeTransferFunctionItem.h>
  29. #include <vtkContextScene.h>
  30. #include <vtkLookupTable.h>
  31. #include <vtkLookupTableItem.h>
  32. #include <vtkPiecewiseControlPointsItem.h>
  33. #include <vtkPiecewiseFunction.h>
  34. #include <vtkPiecewiseFunctionItem.h>
  35. //----------------------------------------------------------------------------
  36. static ctkLogger logger("org.commontk.visualization.vtk.widgets.ctkVTKScalarsToColorsWidget");
  37. //----------------------------------------------------------------------------
  38. class ctkVTKScalarsToColorsWidgetPrivate:
  39. public Ui_ctkVTKScalarsToColorsWidget
  40. {
  41. Q_DECLARE_PUBLIC(ctkVTKScalarsToColorsWidget);
  42. protected:
  43. ctkVTKScalarsToColorsWidget* const q_ptr;
  44. public:
  45. ctkVTKScalarsToColorsWidgetPrivate(ctkVTKScalarsToColorsWidget& object);
  46. void setupUi(QWidget* widget);
  47. vtkControlPointsItem* CurrentControlPointsItem;
  48. };
  49. // ----------------------------------------------------------------------------
  50. // ctkVTKScalarsToColorsWidgetPrivate methods
  51. // ----------------------------------------------------------------------------
  52. ctkVTKScalarsToColorsWidgetPrivate::ctkVTKScalarsToColorsWidgetPrivate(
  53. ctkVTKScalarsToColorsWidget& object)
  54. : q_ptr(&object)
  55. {
  56. this->CurrentControlPointsItem = 0;
  57. }
  58. // ----------------------------------------------------------------------------
  59. void ctkVTKScalarsToColorsWidgetPrivate::setupUi(QWidget* widget)
  60. {
  61. Q_Q(ctkVTKScalarsToColorsWidget);
  62. this->Ui_ctkVTKScalarsToColorsWidget::setupUi(widget);
  63. QObject::connect(this->View, SIGNAL(plotAdded(vtkPlot*)),
  64. q, SLOT(onPlotAdded(vtkPlot*)));
  65. this->PointIdSpinBox->setSpecialValueText("None");
  66. QObject::connect(this->PointIdSpinBox, SIGNAL(valueChanged(int)),
  67. q, SLOT(onCurrentPointChanged(int)));
  68. this->PointIdSpinBox->setValue(-1);
  69. QObject::connect(this->ColorPickerButton, SIGNAL(colorChanged(const QColor&)),
  70. q, SLOT(onColorChanged(const QColor&)));
  71. QObject::connect(this->OpacitySpinBox, SIGNAL(valueChanged(double)),
  72. q, SLOT(onOpacityChanged(double)));
  73. QObject::connect(this->MidPointSpinBox, SIGNAL(valueChanged(double)),
  74. q, SLOT(onMidPointChanged(double)));
  75. QObject::connect(this->SharpnessSpinBox, SIGNAL(valueChanged(double)),
  76. q, SLOT(onSharpnessChanged(double)));
  77. this->ColorPickerButton->setVisible(false);
  78. this->OpacityLabel->setVisible(false);
  79. this->OpacitySpinBox->setVisible(false);
  80. QObject::connect(this->XRangeSlider, SIGNAL(valuesChanged(double, double)),
  81. q, SLOT(onXRangeChanged(double, double)));
  82. QObject::connect(this->YRangeSlider, SIGNAL(valuesChanged(double, double)),
  83. q, SLOT(onYRangeChanged(double, double)));
  84. q->qvtkConnect(this->View->chart()->GetAxis(0),vtkCommand::ModifiedEvent,
  85. q, SLOT(onAxesModified()));
  86. q->qvtkConnect(this->View->chart()->GetAxis(1),vtkCommand::ModifiedEvent,
  87. q, SLOT(onAxesModified()));
  88. }
  89. // ----------------------------------------------------------------------------
  90. // ctkVTKScalarsToColorsWidget methods
  91. // ----------------------------------------------------------------------------
  92. ctkVTKScalarsToColorsWidget::ctkVTKScalarsToColorsWidget(QWidget* parentWidget)
  93. :QWidget(parentWidget)
  94. , d_ptr(new ctkVTKScalarsToColorsWidgetPrivate(*this))
  95. {
  96. Q_D(ctkVTKScalarsToColorsWidget);
  97. d->setupUi(this);
  98. }
  99. // ----------------------------------------------------------------------------
  100. ctkVTKScalarsToColorsWidget::~ctkVTKScalarsToColorsWidget()
  101. {
  102. }
  103. // ----------------------------------------------------------------------------
  104. ctkVTKScalarsToColorsView* ctkVTKScalarsToColorsWidget::view()const
  105. {
  106. Q_D(const ctkVTKScalarsToColorsWidget);
  107. return d->View;
  108. }
  109. // ----------------------------------------------------------------------------
  110. void ctkVTKScalarsToColorsWidget::onPlotAdded(vtkPlot* plot)
  111. {
  112. if (vtkControlPointsItem::SafeDownCast(plot))
  113. {
  114. this->qvtkConnect(plot, vtkControlPointsItem::CurrentPointChangedEvent,
  115. this, SLOT(setCurrentPoint(vtkObject*, void*)));
  116. }
  117. }
  118. // ----------------------------------------------------------------------------
  119. void ctkVTKScalarsToColorsWidget::setCurrentPoint(vtkObject* caller, void* callData)
  120. {
  121. vtkControlPointsItem* controlPoints = reinterpret_cast<vtkControlPointsItem*>(caller);
  122. long newPoint = reinterpret_cast<long>(callData);
  123. if (!controlPoints || newPoint < -1)
  124. {
  125. return;
  126. }
  127. this->setCurrentControlPointsItem(controlPoints);
  128. this->setCurrentPoint(newPoint);
  129. }
  130. // ----------------------------------------------------------------------------
  131. void ctkVTKScalarsToColorsWidget::setCurrentPoint(int newPoint)
  132. {
  133. Q_D(ctkVTKScalarsToColorsWidget);
  134. d->PointIdSpinBox->setValue(newPoint);
  135. }
  136. // ----------------------------------------------------------------------------
  137. void ctkVTKScalarsToColorsWidget::setCurrentControlPointsItem(vtkControlPointsItem* item)
  138. {
  139. Q_D(ctkVTKScalarsToColorsWidget);
  140. if (d->CurrentControlPointsItem == item)
  141. {
  142. return;
  143. }
  144. this->qvtkReconnect(d->CurrentControlPointsItem, item, vtkCommand::ModifiedEvent,
  145. this, SLOT(updateCurrentPoint()));
  146. this->qvtkReconnect(d->CurrentControlPointsItem ?
  147. d->CurrentControlPointsItem->GetXAxis() : d->View->chart()->GetAxis(0),
  148. item ? item->GetXAxis() : d->View->chart()->GetAxis(0),
  149. vtkCommand::ModifiedEvent,
  150. this, SLOT(onAxesModified()));
  151. this->qvtkReconnect(d->CurrentControlPointsItem ?
  152. d->CurrentControlPointsItem->GetYAxis() : d->View->chart()->GetAxis(1),
  153. item ? item->GetYAxis() : d->View->chart()->GetAxis(1),
  154. vtkCommand::ModifiedEvent,
  155. this, SLOT(onAxesModified()));
  156. d->CurrentControlPointsItem = item;
  157. if (item)
  158. {
  159. d->ColorPickerButton->setVisible(
  160. vtkColorTransferControlPointsItem::SafeDownCast(item) != 0 ||
  161. vtkCompositeControlPointsItem::SafeDownCast(item) != 0);
  162. d->OpacityLabel->setVisible(vtkPiecewiseControlPointsItem::SafeDownCast(item) != 0 ||
  163. vtkCompositeControlPointsItem::SafeDownCast(item) != 0);
  164. d->OpacitySpinBox->setVisible(vtkPiecewiseControlPointsItem::SafeDownCast(item) != 0 ||
  165. vtkCompositeControlPointsItem::SafeDownCast(item) != 0);
  166. this->onAxesModified();
  167. }
  168. d->PointIdSpinBox->setEnabled(item != 0);
  169. d->PointIdSpinBox->setMaximum((item ? item->GetNumberOfPoints() : 0) - 1);
  170. d->PointIdSpinBox->setValue(item ? item->GetCurrentPoint() : -1);
  171. this->updateCurrentPoint();
  172. }
  173. // ----------------------------------------------------------------------------
  174. vtkControlPointsItem* ctkVTKScalarsToColorsWidget::currentControlPointsItem()const
  175. {
  176. Q_D(const ctkVTKScalarsToColorsWidget);
  177. return d->CurrentControlPointsItem;
  178. }
  179. // ----------------------------------------------------------------------------
  180. void ctkVTKScalarsToColorsWidget::onCurrentPointChanged(int currentPoint)
  181. {
  182. Q_D(ctkVTKScalarsToColorsWidget);
  183. if (d->CurrentControlPointsItem)
  184. {
  185. d->CurrentControlPointsItem->SetCurrentPoint(currentPoint);
  186. }
  187. d->ColorPickerButton->setEnabled(currentPoint != -1);
  188. d->OpacitySpinBox->setEnabled(currentPoint != -1);
  189. d->MidPointSpinBox->setEnabled(currentPoint != -1);
  190. d->SharpnessSpinBox->setEnabled(currentPoint != -1);
  191. if (d->CurrentControlPointsItem)
  192. {
  193. this->updateCurrentPoint();
  194. }
  195. }
  196. // ----------------------------------------------------------------------------
  197. void ctkVTKScalarsToColorsWidget::updateCurrentPoint()
  198. {
  199. Q_D(ctkVTKScalarsToColorsWidget);
  200. Q_ASSERT(d->CurrentControlPointsItem);
  201. Q_ASSERT(d->PointIdSpinBox->value() == d->CurrentControlPointsItem->GetCurrentPoint());
  202. d->PointIdSpinBox->setMaximum((d->CurrentControlPointsItem ?
  203. d->CurrentControlPointsItem->GetNumberOfPoints() : 0) - 1);
  204. int pointId = d->PointIdSpinBox->value();
  205. if (pointId == -1)
  206. {
  207. return;
  208. }
  209. double point[4];
  210. d->CurrentControlPointsItem->GetControlPoint(pointId, point);
  211. d->OpacitySpinBox->setValue(point[1]);
  212. d->MidPointSpinBox->setValue(point[2]);
  213. d->SharpnessSpinBox->setValue(point[3]);
  214. vtkColorTransferControlPointsItem* colorControlPoints =
  215. vtkColorTransferControlPointsItem::SafeDownCast(d->CurrentControlPointsItem);
  216. if (colorControlPoints)
  217. {
  218. vtkColorTransferFunction* colorTF =
  219. colorControlPoints->GetColorTransferFunction();
  220. double xrgbms[6];
  221. colorTF->GetNodeValue(d->PointIdSpinBox->value(), xrgbms);
  222. QColor color = QColor::fromRgbF(xrgbms[1], xrgbms[2], xrgbms[3]);
  223. d->ColorPickerButton->setColor(color);
  224. }
  225. }
  226. // ----------------------------------------------------------------------------
  227. void ctkVTKScalarsToColorsWidget::onColorChanged(const QColor& color)
  228. {
  229. Q_D(ctkVTKScalarsToColorsWidget);
  230. if (!color.isValid())
  231. {
  232. return;
  233. }
  234. Q_ASSERT(d->CurrentControlPointsItem);
  235. Q_ASSERT(d->PointIdSpinBox->value() != -1);
  236. Q_ASSERT(d->PointIdSpinBox->value() == d->CurrentControlPointsItem->GetCurrentPoint());
  237. vtkColorTransferControlPointsItem* colorControlPoints =
  238. vtkColorTransferControlPointsItem::SafeDownCast(d->CurrentControlPointsItem);
  239. if (colorControlPoints)
  240. {
  241. vtkColorTransferFunction* colorTF =
  242. colorControlPoints->GetColorTransferFunction();
  243. double point[6];
  244. colorTF->GetNodeValue(d->PointIdSpinBox->value(), point);
  245. point[1] = color.redF();
  246. point[2] = color.greenF();
  247. point[3] = color.blueF();
  248. colorTF->SetNodeValue(d->PointIdSpinBox->value(), point);
  249. }
  250. }
  251. // ----------------------------------------------------------------------------
  252. void ctkVTKScalarsToColorsWidget::onOpacityChanged(double opacity)
  253. {
  254. Q_D(ctkVTKScalarsToColorsWidget);
  255. Q_ASSERT(d->CurrentControlPointsItem);
  256. double point[4];
  257. d->CurrentControlPointsItem->GetControlPoint(d->PointIdSpinBox->value(), point);
  258. point[1] = opacity;
  259. d->CurrentControlPointsItem->SetControlPoint(d->PointIdSpinBox->value(), point);
  260. }
  261. // ----------------------------------------------------------------------------
  262. void ctkVTKScalarsToColorsWidget::onMidPointChanged(double midPoint)
  263. {
  264. Q_D(ctkVTKScalarsToColorsWidget);
  265. Q_ASSERT(d->CurrentControlPointsItem);
  266. double point[4];
  267. d->CurrentControlPointsItem->GetControlPoint(d->PointIdSpinBox->value(), point);
  268. point[2] = midPoint;
  269. d->CurrentControlPointsItem->SetControlPoint(d->PointIdSpinBox->value(), point);
  270. }
  271. // ----------------------------------------------------------------------------
  272. void ctkVTKScalarsToColorsWidget::onSharpnessChanged(double sharpness)
  273. {
  274. Q_D(ctkVTKScalarsToColorsWidget);
  275. Q_ASSERT(d->CurrentControlPointsItem);
  276. double point[4];
  277. d->CurrentControlPointsItem->GetControlPoint(d->PointIdSpinBox->value(), point);
  278. point[3] = sharpness;
  279. d->CurrentControlPointsItem->SetControlPoint(d->PointIdSpinBox->value(), point);
  280. }
  281. // ----------------------------------------------------------------------------
  282. void ctkVTKScalarsToColorsWidget::onXRangeChanged(double min, double max)
  283. {
  284. Q_D(ctkVTKScalarsToColorsWidget);
  285. vtkAxis* xAxis = d->CurrentControlPointsItem ?
  286. d->CurrentControlPointsItem->GetXAxis() : d->View->chart()->GetAxis(vtkAxis::BOTTOM);
  287. Q_ASSERT(xAxis);
  288. xAxis->SetRange(min, max);
  289. d->View->scene()->SetDirty(true);
  290. }
  291. // ----------------------------------------------------------------------------
  292. void ctkVTKScalarsToColorsWidget::onYRangeChanged(double min, double max)
  293. {
  294. Q_D(ctkVTKScalarsToColorsWidget);
  295. vtkAxis* yAxis = d->CurrentControlPointsItem ?
  296. d->CurrentControlPointsItem->GetYAxis() : d->View->chart()->GetAxis(vtkAxis::LEFT);
  297. Q_ASSERT(yAxis);
  298. yAxis->SetRange(min, max);
  299. d->View->scene()->SetDirty(true);
  300. }
  301. // ----------------------------------------------------------------------------
  302. void ctkVTKScalarsToColorsWidget::onAxesModified()
  303. {
  304. Q_D(ctkVTKScalarsToColorsWidget);
  305. bool modified = false;
  306. vtkAxis* xAxis = d->CurrentControlPointsItem ?
  307. d->CurrentControlPointsItem->GetXAxis() : d->View->chart()->GetAxis(vtkAxis::BOTTOM);
  308. Q_ASSERT(xAxis);
  309. d->XRangeSlider->setValues(xAxis->GetMinimum(), xAxis->GetMaximum());
  310. vtkAxis* yAxis = d->CurrentControlPointsItem ?
  311. d->CurrentControlPointsItem->GetYAxis() : d->View->chart()->GetAxis(vtkAxis::LEFT);
  312. Q_ASSERT(yAxis);
  313. d->YRangeSlider->setValues(yAxis->GetMinimum(), yAxis->GetMaximum());
  314. }