ctkVTKScalarsToColorsWidget.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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 "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. bool blockSignals(bool);
  48. vtkControlPointsItem* CurrentControlPointsItem;
  49. bool EditColors;
  50. };
  51. // ----------------------------------------------------------------------------
  52. // ctkVTKScalarsToColorsWidgetPrivate methods
  53. // ----------------------------------------------------------------------------
  54. ctkVTKScalarsToColorsWidgetPrivate::ctkVTKScalarsToColorsWidgetPrivate(
  55. ctkVTKScalarsToColorsWidget& object)
  56. : q_ptr(&object)
  57. {
  58. this->CurrentControlPointsItem = 0;
  59. this->EditColors = true;
  60. }
  61. // ----------------------------------------------------------------------------
  62. void ctkVTKScalarsToColorsWidgetPrivate::setupUi(QWidget* widget)
  63. {
  64. Q_Q(ctkVTKScalarsToColorsWidget);
  65. this->Ui_ctkVTKScalarsToColorsWidget::setupUi(widget);
  66. QObject::connect(this->View, SIGNAL(plotAdded(vtkPlot*)),
  67. q, SLOT(onPlotAdded(vtkPlot*)));
  68. QObject::connect(this->View, SIGNAL(boundsChanged()),
  69. q, SLOT(onBoundsChanged()));
  70. this->PointIdSpinBox->setSpecialValueText("None");
  71. QObject::connect(this->PointIdSpinBox, SIGNAL(valueChanged(int)),
  72. q, SLOT(onCurrentPointChanged(int)));
  73. this->PointIdSpinBox->setValue(-1);
  74. QObject::connect(this->ColorPickerButton, SIGNAL(colorChanged(QColor)),
  75. q, SLOT(onColorChanged(QColor)));
  76. QObject::connect(this->OpacitySpinBox, SIGNAL(valueChanged(double)),
  77. q, SLOT(onOpacityChanged(double)));
  78. QObject::connect(this->MidPointSpinBox, SIGNAL(valueChanged(double)),
  79. q, SLOT(onMidPointChanged(double)));
  80. QObject::connect(this->SharpnessSpinBox, SIGNAL(valueChanged(double)),
  81. q, SLOT(onSharpnessChanged(double)));
  82. this->ColorPickerButton->setVisible(false);
  83. this->OpacityLabel->setVisible(false);
  84. this->OpacitySpinBox->setVisible(false);
  85. QObject::connect(this->XRangeSlider, SIGNAL(valuesChanged(double,double)),
  86. q, SLOT(setXRange(double,double)));
  87. QObject::connect(this->YRangeSlider, SIGNAL(valuesChanged(double,double)),
  88. q, SLOT(setYRange(double,double)));
  89. QObject::connect(this->XRangeSlider, SIGNAL(valuesChanged(double,double)),
  90. q, SIGNAL(axesModified()));
  91. QObject::connect(this->YRangeSlider, SIGNAL(valuesChanged(double,double)),
  92. q, SIGNAL(axesModified()));
  93. q->qvtkConnect(this->View->chart()->GetAxis(0),vtkCommand::ModifiedEvent,
  94. q, SLOT(onAxesModified()));
  95. q->qvtkConnect(this->View->chart()->GetAxis(1),vtkCommand::ModifiedEvent,
  96. q, SLOT(onAxesModified()));
  97. }
  98. // ----------------------------------------------------------------------------
  99. bool ctkVTKScalarsToColorsWidgetPrivate::blockSignals(bool block)
  100. {
  101. this->ColorPickerButton->blockSignals(block);
  102. this->OpacitySpinBox->blockSignals(block);
  103. this->MidPointSpinBox->blockSignals(block);
  104. return this->SharpnessSpinBox->blockSignals(block);
  105. }
  106. // ----------------------------------------------------------------------------
  107. // ctkVTKScalarsToColorsWidget methods
  108. // ----------------------------------------------------------------------------
  109. ctkVTKScalarsToColorsWidget::ctkVTKScalarsToColorsWidget(QWidget* parentWidget)
  110. :QWidget(parentWidget)
  111. , d_ptr(new ctkVTKScalarsToColorsWidgetPrivate(*this))
  112. {
  113. Q_D(ctkVTKScalarsToColorsWidget);
  114. d->setupUi(this);
  115. }
  116. // ----------------------------------------------------------------------------
  117. ctkVTKScalarsToColorsWidget::~ctkVTKScalarsToColorsWidget()
  118. {
  119. }
  120. // ----------------------------------------------------------------------------
  121. ctkVTKScalarsToColorsView* ctkVTKScalarsToColorsWidget::view()const
  122. {
  123. Q_D(const ctkVTKScalarsToColorsWidget);
  124. return d->View;
  125. }
  126. // ----------------------------------------------------------------------------
  127. bool ctkVTKScalarsToColorsWidget::isHorizontalSliderVisible()const
  128. {
  129. Q_D(const ctkVTKScalarsToColorsWidget);
  130. return d->XRangeSlider->isVisibleTo(
  131. const_cast<ctkVTKScalarsToColorsWidget*>(this));
  132. }
  133. // ----------------------------------------------------------------------------
  134. void ctkVTKScalarsToColorsWidget::setHorizontalSliderVisible(bool visible)
  135. {
  136. Q_D(ctkVTKScalarsToColorsWidget);
  137. d->XRangeSlider->setVisible(visible);
  138. }
  139. // ----------------------------------------------------------------------------
  140. bool ctkVTKScalarsToColorsWidget::isVerticalSliderVisible()const
  141. {
  142. Q_D(const ctkVTKScalarsToColorsWidget);
  143. return d->YRangeSlider->isVisibleTo(
  144. const_cast<ctkVTKScalarsToColorsWidget*>(this));
  145. }
  146. // ----------------------------------------------------------------------------
  147. void ctkVTKScalarsToColorsWidget::setVerticalSliderVisible(bool visible)
  148. {
  149. Q_D(ctkVTKScalarsToColorsWidget);
  150. d->YRangeSlider->setVisible(visible);
  151. }
  152. // ----------------------------------------------------------------------------
  153. bool ctkVTKScalarsToColorsWidget::editColors()const
  154. {
  155. Q_D(const ctkVTKScalarsToColorsWidget);
  156. return d->EditColors;
  157. }
  158. // ----------------------------------------------------------------------------
  159. void ctkVTKScalarsToColorsWidget::setEditColors(bool edit)
  160. {
  161. Q_D(ctkVTKScalarsToColorsWidget);
  162. d->EditColors = edit;
  163. }
  164. // ----------------------------------------------------------------------------
  165. void ctkVTKScalarsToColorsWidget::onPlotAdded(vtkPlot* plot)
  166. {
  167. if (vtkControlPointsItem::SafeDownCast(plot))
  168. {
  169. this->qvtkConnect(plot, vtkControlPointsItem::CurrentPointChangedEvent,
  170. this, SLOT(setCurrentPoint(vtkObject*,void*)));
  171. }
  172. }
  173. // ----------------------------------------------------------------------------
  174. void ctkVTKScalarsToColorsWidget::onBoundsChanged()
  175. {
  176. this->onAxesModified();
  177. }
  178. // ----------------------------------------------------------------------------
  179. void ctkVTKScalarsToColorsWidget::setCurrentPoint(vtkObject* caller, void* callData)
  180. {
  181. vtkControlPointsItem* controlPoints = reinterpret_cast<vtkControlPointsItem*>(caller);
  182. long newPoint = reinterpret_cast<long>(callData);
  183. if (!controlPoints || newPoint < -1)
  184. {
  185. return;
  186. }
  187. this->setCurrentControlPointsItem(controlPoints);
  188. this->setCurrentPoint(newPoint);
  189. }
  190. // ----------------------------------------------------------------------------
  191. void ctkVTKScalarsToColorsWidget::setCurrentPoint(int newPoint)
  192. {
  193. Q_D(ctkVTKScalarsToColorsWidget);
  194. d->PointIdSpinBox->setValue(newPoint);
  195. Q_ASSERT( newPoint == d->PointIdSpinBox->value());
  196. }
  197. // ----------------------------------------------------------------------------
  198. void ctkVTKScalarsToColorsWidget::setCurrentControlPointsItem(vtkControlPointsItem* item)
  199. {
  200. Q_D(ctkVTKScalarsToColorsWidget);
  201. if (d->CurrentControlPointsItem == item)
  202. {
  203. return;
  204. }
  205. this->qvtkReconnect(d->CurrentControlPointsItem, item, vtkCommand::ModifiedEvent,
  206. this, SLOT(updateCurrentPoint()));
  207. this->qvtkReconnect(d->CurrentControlPointsItem ?
  208. d->CurrentControlPointsItem->GetXAxis() : d->View->chart()->GetAxis(0),
  209. item ? item->GetXAxis() : d->View->chart()->GetAxis(0),
  210. vtkCommand::ModifiedEvent,
  211. this, SLOT(onAxesModified()));
  212. this->qvtkReconnect(d->CurrentControlPointsItem ?
  213. d->CurrentControlPointsItem->GetYAxis() : d->View->chart()->GetAxis(1),
  214. item ? item->GetYAxis() : d->View->chart()->GetAxis(1),
  215. vtkCommand::ModifiedEvent,
  216. this, SLOT(onAxesModified()));
  217. d->CurrentControlPointsItem = item;
  218. if (item)
  219. {
  220. d->ColorPickerButton->setVisible( d->EditColors &&
  221. (vtkColorTransferControlPointsItem::SafeDownCast(item) != 0 ||
  222. vtkCompositeControlPointsItem::SafeDownCast(item) != 0));
  223. d->OpacityLabel->setVisible(vtkPiecewiseControlPointsItem::SafeDownCast(item) != 0 ||
  224. vtkCompositeControlPointsItem::SafeDownCast(item) != 0);
  225. d->OpacitySpinBox->setVisible(vtkPiecewiseControlPointsItem::SafeDownCast(item) != 0 ||
  226. vtkCompositeControlPointsItem::SafeDownCast(item) != 0);
  227. this->onAxesModified();
  228. }
  229. d->PointIdSpinBox->setEnabled(item != 0);
  230. d->PointIdSpinBox->setMaximum((item ? item->GetNumberOfPoints() : 0) - 1);
  231. d->PointIdSpinBox->setValue(item ? item->GetCurrentPoint() : -1);
  232. this->updateCurrentPoint();
  233. }
  234. // ----------------------------------------------------------------------------
  235. vtkControlPointsItem* ctkVTKScalarsToColorsWidget::currentControlPointsItem()const
  236. {
  237. Q_D(const ctkVTKScalarsToColorsWidget);
  238. return d->CurrentControlPointsItem;
  239. }
  240. // ----------------------------------------------------------------------------
  241. void ctkVTKScalarsToColorsWidget::onCurrentPointChanged(int currentPoint)
  242. {
  243. Q_D(ctkVTKScalarsToColorsWidget);
  244. if (d->CurrentControlPointsItem)
  245. {
  246. d->CurrentControlPointsItem->SetCurrentPoint(currentPoint);
  247. }
  248. d->ColorPickerButton->setEnabled(currentPoint != -1);
  249. d->OpacitySpinBox->setEnabled(currentPoint != -1);
  250. d->MidPointSpinBox->setEnabled(currentPoint != -1);
  251. d->SharpnessSpinBox->setEnabled(currentPoint != -1);
  252. if (d->CurrentControlPointsItem)
  253. {
  254. this->updateCurrentPoint();
  255. }
  256. }
  257. // ----------------------------------------------------------------------------
  258. void ctkVTKScalarsToColorsWidget::updateCurrentPoint()
  259. {
  260. Q_D(ctkVTKScalarsToColorsWidget);
  261. Q_ASSERT(d->CurrentControlPointsItem);
  262. Q_ASSERT(d->PointIdSpinBox->value() == d->CurrentControlPointsItem->GetCurrentPoint());
  263. d->PointIdSpinBox->setMaximum((d->CurrentControlPointsItem ?
  264. d->CurrentControlPointsItem->GetNumberOfPoints() : 0) - 1);
  265. int pointId = d->PointIdSpinBox->value();
  266. if (pointId == -1)
  267. {
  268. return;
  269. }
  270. double point[4];
  271. d->CurrentControlPointsItem->GetControlPoint(pointId, point);
  272. bool oldBlock = d->blockSignals(true);
  273. d->OpacitySpinBox->setValue(point[1]);
  274. d->MidPointSpinBox->setValue(point[2]);
  275. d->SharpnessSpinBox->setValue(point[3]);
  276. vtkColorTransferControlPointsItem* colorControlPoints =
  277. vtkColorTransferControlPointsItem::SafeDownCast(d->CurrentControlPointsItem);
  278. vtkCompositeControlPointsItem* compositeControlPoints =
  279. vtkCompositeControlPointsItem::SafeDownCast(d->CurrentControlPointsItem);
  280. if (colorControlPoints &&
  281. (!compositeControlPoints ||
  282. compositeControlPoints->GetPointsFunction() == vtkCompositeControlPointsItem::ColorPointsFunction ||
  283. compositeControlPoints->GetPointsFunction() == vtkCompositeControlPointsItem::ColorAndOpacityPointsFunction))
  284. {
  285. vtkColorTransferFunction* colorTF =
  286. colorControlPoints->GetColorTransferFunction();
  287. double xrgbms[6];
  288. colorTF->GetNodeValue(d->PointIdSpinBox->value(), xrgbms);
  289. QColor color = QColor::fromRgbF(xrgbms[1], xrgbms[2], xrgbms[3]);
  290. d->ColorPickerButton->setColor(color);
  291. }
  292. d->blockSignals(oldBlock);
  293. }
  294. // ----------------------------------------------------------------------------
  295. void ctkVTKScalarsToColorsWidget::onColorChanged(const QColor& color)
  296. {
  297. Q_D(ctkVTKScalarsToColorsWidget);
  298. if (!color.isValid())
  299. {
  300. return;
  301. }
  302. Q_ASSERT(d->CurrentControlPointsItem);
  303. Q_ASSERT(d->PointIdSpinBox->value() != -1);
  304. Q_ASSERT(d->PointIdSpinBox->value() == d->CurrentControlPointsItem->GetCurrentPoint());
  305. vtkColorTransferControlPointsItem* colorControlPoints =
  306. vtkColorTransferControlPointsItem::SafeDownCast(d->CurrentControlPointsItem);
  307. if (colorControlPoints)
  308. {
  309. vtkColorTransferFunction* colorTF =
  310. colorControlPoints->GetColorTransferFunction();
  311. double point[6];
  312. colorTF->GetNodeValue(d->PointIdSpinBox->value(), point);
  313. point[1] = color.redF();
  314. point[2] = color.greenF();
  315. point[3] = color.blueF();
  316. colorTF->SetNodeValue(d->PointIdSpinBox->value(), point);
  317. }
  318. }
  319. // ----------------------------------------------------------------------------
  320. void ctkVTKScalarsToColorsWidget::onOpacityChanged(double opacity)
  321. {
  322. Q_D(ctkVTKScalarsToColorsWidget);
  323. Q_ASSERT(d->CurrentControlPointsItem);
  324. double point[4];
  325. d->CurrentControlPointsItem->GetControlPoint(d->PointIdSpinBox->value(), point);
  326. point[1] = opacity;
  327. d->CurrentControlPointsItem->SetControlPoint(d->PointIdSpinBox->value(), point);
  328. }
  329. // ----------------------------------------------------------------------------
  330. void ctkVTKScalarsToColorsWidget::onMidPointChanged(double midPoint)
  331. {
  332. Q_D(ctkVTKScalarsToColorsWidget);
  333. Q_ASSERT(d->CurrentControlPointsItem);
  334. double point[4];
  335. d->CurrentControlPointsItem->GetControlPoint(d->PointIdSpinBox->value(), point);
  336. point[2] = midPoint;
  337. d->CurrentControlPointsItem->SetControlPoint(d->PointIdSpinBox->value(), point);
  338. }
  339. // ----------------------------------------------------------------------------
  340. void ctkVTKScalarsToColorsWidget::onSharpnessChanged(double sharpness)
  341. {
  342. Q_D(ctkVTKScalarsToColorsWidget);
  343. Q_ASSERT(d->CurrentControlPointsItem);
  344. double point[4];
  345. d->CurrentControlPointsItem->GetControlPoint(d->PointIdSpinBox->value(), point);
  346. point[3] = sharpness;
  347. d->CurrentControlPointsItem->SetControlPoint(d->PointIdSpinBox->value(), point);
  348. }
  349. // ----------------------------------------------------------------------------
  350. void ctkVTKScalarsToColorsWidget::xRange(double* range)const
  351. {
  352. Q_D(const ctkVTKScalarsToColorsWidget);
  353. vtkAxis* xAxis = d->CurrentControlPointsItem ?
  354. d->CurrentControlPointsItem->GetXAxis() : d->View->chart()->GetAxis(vtkAxis::BOTTOM);
  355. Q_ASSERT(xAxis);
  356. range[0] = xAxis->GetMinimum();
  357. range[1] = xAxis->GetMaximum();
  358. }
  359. // ----------------------------------------------------------------------------
  360. void ctkVTKScalarsToColorsWidget::setXRange(double min, double max)
  361. {
  362. Q_D(ctkVTKScalarsToColorsWidget);
  363. vtkAxis* xAxis = d->CurrentControlPointsItem ?
  364. d->CurrentControlPointsItem->GetXAxis() : d->View->chart()->GetAxis(vtkAxis::BOTTOM);
  365. Q_ASSERT(xAxis);
  366. if (xAxis->GetMinimum() != min || xAxis->GetMaximum() != max)
  367. {
  368. xAxis->SetRange(min, max);
  369. // Repaint the scene
  370. d->View->scene()->SetDirty(true);
  371. }
  372. }
  373. // ----------------------------------------------------------------------------
  374. void ctkVTKScalarsToColorsWidget::yRange(double* range)const
  375. {
  376. Q_D(const ctkVTKScalarsToColorsWidget);
  377. vtkAxis* yAxis = d->CurrentControlPointsItem ?
  378. d->CurrentControlPointsItem->GetYAxis() : d->View->chart()->GetAxis(vtkAxis::LEFT);
  379. Q_ASSERT(yAxis);
  380. range[0] = yAxis->GetMinimum();
  381. range[1] = yAxis->GetMaximum();
  382. }
  383. // ----------------------------------------------------------------------------
  384. void ctkVTKScalarsToColorsWidget::setYRange(double min, double max)
  385. {
  386. Q_D(ctkVTKScalarsToColorsWidget);
  387. vtkAxis* yAxis = d->CurrentControlPointsItem ?
  388. d->CurrentControlPointsItem->GetYAxis() : d->View->chart()->GetAxis(vtkAxis::LEFT);
  389. Q_ASSERT(yAxis);
  390. if (yAxis->GetMinimum() != min || yAxis->GetMaximum() != max)
  391. {
  392. yAxis->SetRange(min, max);
  393. // Repaint the scene
  394. d->View->scene()->SetDirty(true);
  395. }
  396. }
  397. // ----------------------------------------------------------------------------
  398. void ctkVTKScalarsToColorsWidget::onAxesModified()
  399. {
  400. Q_D(ctkVTKScalarsToColorsWidget);
  401. vtkAxis* xAxis = d->CurrentControlPointsItem ?
  402. d->CurrentControlPointsItem->GetXAxis() : d->View->chart()->GetAxis(vtkAxis::BOTTOM);
  403. Q_ASSERT(xAxis);
  404. bool wasBlocking = d->XRangeSlider->blockSignals(true);
  405. d->XRangeSlider->setRange(xAxis->GetMinimumLimit(), xAxis->GetMaximumLimit());
  406. d->XRangeSlider->setValues(xAxis->GetMinimum(), xAxis->GetMaximum());
  407. d->XRangeSlider->blockSignals(wasBlocking);
  408. vtkAxis* yAxis = d->CurrentControlPointsItem ?
  409. d->CurrentControlPointsItem->GetYAxis() : d->View->chart()->GetAxis(vtkAxis::LEFT);
  410. Q_ASSERT(yAxis);
  411. wasBlocking = d->YRangeSlider->blockSignals(true);
  412. d->YRangeSlider->setRange(yAxis->GetMinimumLimit(), yAxis->GetMaximumLimit());
  413. d->YRangeSlider->setValues(yAxis->GetMinimum(), yAxis->GetMaximum());
  414. d->YRangeSlider->blockSignals(wasBlocking);
  415. }