ctkVTKScalarsToColorsWidget.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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 <QTimer>
  17. // CTK includes
  18. #include "ctkLogger.h"
  19. #include "ctkUtils.h"
  20. #include "ctkVTKScalarsToColorsView.h"
  21. #include "ctkVTKScalarsToColorsWidget.h"
  22. #include "ui_ctkVTKScalarsToColorsWidget.h"
  23. // VTK includes
  24. #include <vtkAxis.h>
  25. #include <vtkChartXY.h>
  26. #include <vtkColorTransferControlPointsItem.h>
  27. #include <vtkColorTransferFunction.h>
  28. #include <vtkColorTransferFunctionItem.h>
  29. #include <vtkCompositeControlPointsItem.h>
  30. #include <vtkCompositeTransferFunctionItem.h>
  31. #include <vtkContextScene.h>
  32. #include <vtkLookupTable.h>
  33. #include <vtkLookupTableItem.h>
  34. #include <vtkPiecewiseControlPointsItem.h>
  35. #include <vtkPiecewiseFunction.h>
  36. #include <vtkPiecewiseFunctionItem.h>
  37. //----------------------------------------------------------------------------
  38. static ctkLogger logger("org.commontk.visualization.vtk.widgets.ctkVTKScalarsToColorsWidget");
  39. //----------------------------------------------------------------------------
  40. class ctkVTKScalarsToColorsWidgetPrivate:
  41. public Ui_ctkVTKScalarsToColorsWidget
  42. {
  43. Q_DECLARE_PUBLIC(ctkVTKScalarsToColorsWidget);
  44. protected:
  45. ctkVTKScalarsToColorsWidget* const q_ptr;
  46. public:
  47. ctkVTKScalarsToColorsWidgetPrivate(ctkVTKScalarsToColorsWidget& object);
  48. void setupUi(QWidget* widget);
  49. bool blockSignals(bool);
  50. bool checkXRange(double x, int pointId);
  51. vtkControlPointsItem* CurrentControlPointsItem;
  52. bool EditColors;
  53. bool TopWidgetsVisible;
  54. };
  55. // ----------------------------------------------------------------------------
  56. // ctkVTKScalarsToColorsWidgetPrivate methods
  57. // ----------------------------------------------------------------------------
  58. ctkVTKScalarsToColorsWidgetPrivate::ctkVTKScalarsToColorsWidgetPrivate(
  59. ctkVTKScalarsToColorsWidget& object)
  60. : q_ptr(&object)
  61. {
  62. this->CurrentControlPointsItem = 0;
  63. this->EditColors = true;
  64. this->TopWidgetsVisible = true;
  65. }
  66. // ----------------------------------------------------------------------------
  67. void ctkVTKScalarsToColorsWidgetPrivate::setupUi(QWidget* widget)
  68. {
  69. Q_Q(ctkVTKScalarsToColorsWidget);
  70. this->Ui_ctkVTKScalarsToColorsWidget::setupUi(widget);
  71. QObject::connect(this->View, SIGNAL(plotAdded(vtkPlot*)),
  72. q, SLOT(onPlotAdded(vtkPlot*)));
  73. QObject::connect(this->View, SIGNAL(boundsChanged()),
  74. q, SLOT(onBoundsChanged()));
  75. QObject::connect(this->View, SIGNAL(functionChanged()),
  76. q, SLOT(resetRange()));
  77. this->PointIdSpinBox->setSpecialValueText("None");
  78. QObject::connect(this->PointIdSpinBox, SIGNAL(valueChanged(int)),
  79. q, SLOT(onCurrentPointChanged(int)));
  80. this->PointIdSpinBox->setValue(-1);
  81. QObject::connect(this->ColorPickerButton, SIGNAL(colorChanged(QColor)),
  82. q, SLOT(onColorChanged(QColor)));
  83. QObject::connect(this->XSpinBox, SIGNAL(valueChanged(double)),
  84. q, SLOT(onXChanged(double)));
  85. QObject::connect(this->OpacitySpinBox, SIGNAL(valueChanged(double)),
  86. q, SLOT(onOpacityChanged(double)));
  87. QObject::connect(this->MidPointSpinBox, SIGNAL(valueChanged(double)),
  88. q, SLOT(onMidPointChanged(double)));
  89. QObject::connect(this->SharpnessSpinBox, SIGNAL(valueChanged(double)),
  90. q, SLOT(onSharpnessChanged(double)));
  91. this->ColorPickerButton->setVisible(false);
  92. this->XLabel->setVisible(false);
  93. this->XSpinBox->setVisible(false);
  94. this->OpacityLabel->setVisible(false);
  95. this->OpacitySpinBox->setVisible(false);
  96. this->SharpnessLabel->setVisible(false);
  97. this->SharpnessSpinBox->setVisible(false);
  98. this->MidPointLabel->setVisible(false);
  99. this->MidPointSpinBox->setVisible(false);
  100. QObject::connect(this->XRangeSlider, SIGNAL(valuesChanged(double,double)),
  101. q, SLOT(setXRange(double,double)));
  102. QObject::connect(this->YRangeSlider, SIGNAL(valuesChanged(double,double)),
  103. q, SLOT(setYRange(double,double)));
  104. QObject::connect(this->XRangeSlider, SIGNAL(valuesChanged(double,double)),
  105. q, SIGNAL(axesModified()));
  106. QObject::connect(this->YRangeSlider, SIGNAL(valuesChanged(double,double)),
  107. q, SIGNAL(axesModified()));
  108. QObject::connect(this->View, SIGNAL(extentChanged()),
  109. q, SLOT(onAxesModified()));
  110. this->ExpandButton->setMirrorOnExpand(true);
  111. QObject::connect(this->ExpandButton, SIGNAL(clicked(bool)),
  112. q, SLOT(onExpandButton(bool)));
  113. }
  114. // ----------------------------------------------------------------------------
  115. bool ctkVTKScalarsToColorsWidgetPrivate::blockSignals(bool block)
  116. {
  117. this->ColorPickerButton->blockSignals(block);
  118. this->XSpinBox->blockSignals(block);
  119. this->OpacitySpinBox->blockSignals(block);
  120. this->MidPointSpinBox->blockSignals(block);
  121. return this->SharpnessSpinBox->blockSignals(block);
  122. }
  123. // ----------------------------------------------------------------------------
  124. bool ctkVTKScalarsToColorsWidgetPrivate::checkXRange(double x, int pointId)
  125. {
  126. Q_Q(ctkVTKScalarsToColorsWidget);
  127. QPalette wrongPalette = q->palette();
  128. wrongPalette.setColor(QPalette::Highlight, Qt::red);
  129. if (pointId < 0 ||
  130. pointId >= this->PointIdSpinBox->maximum())
  131. {
  132. QTimer::singleShot(2000, q, SLOT(restorePalette()));
  133. return false;
  134. }
  135. if (pointId > 0)
  136. {
  137. double previous[4];
  138. this->CurrentControlPointsItem->GetControlPoint(pointId - 1, previous);
  139. if (x < previous[0])
  140. {
  141. this->XSpinBox->setPalette(wrongPalette);
  142. this->XSpinBox->selectAll();
  143. QTimer::singleShot(2000, q, SLOT(restorePalette()));
  144. this->XSpinBox->setValue(previous[0]);
  145. return false;
  146. }
  147. }
  148. if (pointId < this->PointIdSpinBox->maximum() - 1)
  149. {
  150. double next[4];
  151. this->CurrentControlPointsItem->GetControlPoint(pointId + 1, next);
  152. if (x > next[0])
  153. {
  154. this->XSpinBox->setPalette(wrongPalette);
  155. this->XSpinBox->selectAll();
  156. QTimer::singleShot(2000, q, SLOT(restorePalette()));
  157. this->XSpinBox->setValue(next[0]);
  158. return false;
  159. }
  160. }
  161. return true;
  162. }
  163. // ----------------------------------------------------------------------------
  164. // ctkVTKScalarsToColorsWidget methods
  165. // ----------------------------------------------------------------------------
  166. ctkVTKScalarsToColorsWidget::ctkVTKScalarsToColorsWidget(QWidget* parentWidget)
  167. :QWidget(parentWidget)
  168. , d_ptr(new ctkVTKScalarsToColorsWidgetPrivate(*this))
  169. {
  170. Q_D(ctkVTKScalarsToColorsWidget);
  171. d->setupUi(this);
  172. }
  173. // ----------------------------------------------------------------------------
  174. ctkVTKScalarsToColorsWidget::~ctkVTKScalarsToColorsWidget()
  175. {
  176. }
  177. // ----------------------------------------------------------------------------
  178. ctkVTKScalarsToColorsView* ctkVTKScalarsToColorsWidget::view()const
  179. {
  180. Q_D(const ctkVTKScalarsToColorsWidget);
  181. return d->View;
  182. }
  183. // ----------------------------------------------------------------------------
  184. bool ctkVTKScalarsToColorsWidget::isHorizontalSliderVisible()const
  185. {
  186. Q_D(const ctkVTKScalarsToColorsWidget);
  187. return d->XRangeSlider->isVisibleTo(
  188. const_cast<ctkVTKScalarsToColorsWidget*>(this));
  189. }
  190. // ----------------------------------------------------------------------------
  191. void ctkVTKScalarsToColorsWidget::setHorizontalSliderVisible(bool visible)
  192. {
  193. Q_D(ctkVTKScalarsToColorsWidget);
  194. d->XRangeSlider->setVisible(visible);
  195. }
  196. // ----------------------------------------------------------------------------
  197. bool ctkVTKScalarsToColorsWidget::isVerticalSliderVisible()const
  198. {
  199. Q_D(const ctkVTKScalarsToColorsWidget);
  200. return d->YRangeSlider->isVisibleTo(
  201. const_cast<ctkVTKScalarsToColorsWidget*>(this));
  202. }
  203. // ----------------------------------------------------------------------------
  204. void ctkVTKScalarsToColorsWidget::setVerticalSliderVisible(bool visible)
  205. {
  206. Q_D(ctkVTKScalarsToColorsWidget);
  207. d->YRangeSlider->setVisible(visible);
  208. }
  209. // ----------------------------------------------------------------------------
  210. bool ctkVTKScalarsToColorsWidget::editColors()const
  211. {
  212. Q_D(const ctkVTKScalarsToColorsWidget);
  213. return d->EditColors;
  214. }
  215. // ----------------------------------------------------------------------------
  216. void ctkVTKScalarsToColorsWidget::setEditColors(bool edit)
  217. {
  218. Q_D(ctkVTKScalarsToColorsWidget);
  219. d->EditColors = edit;
  220. }
  221. // ----------------------------------------------------------------------------
  222. bool ctkVTKScalarsToColorsWidget::areTopWidgetsVisible()const
  223. {
  224. Q_D(const ctkVTKScalarsToColorsWidget);
  225. return d->TopWidgetsVisible;
  226. }
  227. // ----------------------------------------------------------------------------
  228. void ctkVTKScalarsToColorsWidget::setTopWidgetsVisible(bool visible)
  229. {
  230. Q_D(ctkVTKScalarsToColorsWidget);
  231. d->ExpandButton->setVisible(visible);
  232. d->PointIdLabel->setVisible(visible);
  233. d->PointIdSpinBox->setVisible(visible);
  234. foreach(QWidget* widget, this->extraWidgets())
  235. {
  236. widget->setVisible(visible);
  237. }
  238. d->TopSpacer->changeSize(visible ? 40 : 0, visible ? 20 : 0);
  239. d->TopSpacer->invalidate();
  240. d->TopWidgetsVisible = visible;
  241. }
  242. // ----------------------------------------------------------------------------
  243. void ctkVTKScalarsToColorsWidget::onPlotAdded(vtkPlot* plot)
  244. {
  245. vtkControlPointsItem* controlPoints = vtkControlPointsItem::SafeDownCast(plot);
  246. if (controlPoints)
  247. {
  248. this->setCurrentControlPointsItem(controlPoints);
  249. this->qvtkConnect(plot, vtkControlPointsItem::CurrentPointChangedEvent,
  250. this, SLOT(setCurrentPoint(vtkObject*,void*)));
  251. }
  252. }
  253. // ----------------------------------------------------------------------------
  254. void ctkVTKScalarsToColorsWidget::onBoundsChanged()
  255. {
  256. this->onAxesModified();
  257. }
  258. // ----------------------------------------------------------------------------
  259. void ctkVTKScalarsToColorsWidget::setCurrentPoint(vtkObject* caller, void* callData)
  260. {
  261. Q_D(ctkVTKScalarsToColorsWidget);
  262. vtkControlPointsItem* controlPoints = reinterpret_cast<vtkControlPointsItem*>(caller);
  263. long newPoint = reinterpret_cast<long>(callData);
  264. if (!controlPoints || newPoint < -1)
  265. {
  266. return;
  267. }
  268. if (d->CurrentControlPointsItem != controlPoints)
  269. {
  270. this->setCurrentControlPointsItem(controlPoints);
  271. }
  272. else
  273. {
  274. // When a new point is added, the modified event is fired later.
  275. // however we need to update the max of the current spin box before
  276. // setting the new value.
  277. this->updateNumberOfPoints();
  278. }
  279. this->setCurrentPoint(newPoint);
  280. }
  281. // ----------------------------------------------------------------------------
  282. void ctkVTKScalarsToColorsWidget::setCurrentPoint(int newPoint)
  283. {
  284. Q_D(ctkVTKScalarsToColorsWidget);
  285. d->PointIdSpinBox->setValue(newPoint);
  286. Q_ASSERT( newPoint == d->PointIdSpinBox->value());
  287. }
  288. // ----------------------------------------------------------------------------
  289. void ctkVTKScalarsToColorsWidget::setCurrentControlPointsItem(vtkControlPointsItem* item)
  290. {
  291. Q_D(ctkVTKScalarsToColorsWidget);
  292. if (d->CurrentControlPointsItem == item)
  293. {
  294. return;
  295. }
  296. this->qvtkReconnect(d->CurrentControlPointsItem, item, vtkCommand::ModifiedEvent,
  297. this, SLOT(updateCurrentPoint()));
  298. this->qvtkReconnect(d->CurrentControlPointsItem ?
  299. d->CurrentControlPointsItem->GetXAxis() : d->View->chart()->GetAxis(0),
  300. item ? item->GetXAxis() : d->View->chart()->GetAxis(0),
  301. vtkCommand::ModifiedEvent,
  302. this, SLOT(onAxesModified()));
  303. this->qvtkReconnect(d->CurrentControlPointsItem ?
  304. d->CurrentControlPointsItem->GetYAxis() : d->View->chart()->GetAxis(1),
  305. item ? item->GetYAxis() : d->View->chart()->GetAxis(1),
  306. vtkCommand::ModifiedEvent,
  307. this, SLOT(onAxesModified()));
  308. d->CurrentControlPointsItem = item;
  309. if (item)
  310. {
  311. d->ColorPickerButton->setVisible( d->EditColors && d->TopWidgetsVisible &&
  312. (vtkColorTransferControlPointsItem::SafeDownCast(item) != 0 ||
  313. vtkCompositeControlPointsItem::SafeDownCast(item) != 0));
  314. d->XLabel->setVisible(d->TopWidgetsVisible);
  315. d->XSpinBox->setVisible(d->TopWidgetsVisible);
  316. d->OpacityLabel->setVisible(d->TopWidgetsVisible &&
  317. (vtkPiecewiseControlPointsItem::SafeDownCast(item) != 0 ||
  318. vtkCompositeControlPointsItem::SafeDownCast(item) != 0));
  319. d->OpacitySpinBox->setVisible(d->TopWidgetsVisible &&
  320. (vtkPiecewiseControlPointsItem::SafeDownCast(item) != 0 ||
  321. vtkCompositeControlPointsItem::SafeDownCast(item) != 0));
  322. this->onAxesModified();
  323. }
  324. d->PointIdSpinBox->setEnabled(item != 0);
  325. d->PointIdSpinBox->setMaximum((item ? item->GetNumberOfPoints() : 0) - 1);
  326. d->PointIdSpinBox->setValue(item ? item->GetCurrentPoint() : -1);
  327. this->updateCurrentPoint();
  328. }
  329. // ----------------------------------------------------------------------------
  330. vtkControlPointsItem* ctkVTKScalarsToColorsWidget::currentControlPointsItem()const
  331. {
  332. Q_D(const ctkVTKScalarsToColorsWidget);
  333. return d->CurrentControlPointsItem;
  334. }
  335. // ----------------------------------------------------------------------------
  336. void ctkVTKScalarsToColorsWidget::onCurrentPointChanged(int currentPoint)
  337. {
  338. Q_D(ctkVTKScalarsToColorsWidget);
  339. if (d->CurrentControlPointsItem)
  340. {
  341. d->CurrentControlPointsItem->SetCurrentPoint(currentPoint);
  342. }
  343. d->ColorPickerButton->setEnabled(currentPoint != -1);
  344. d->XSpinBox->setEnabled(currentPoint != -1);
  345. d->OpacitySpinBox->setEnabled(currentPoint != -1);
  346. d->MidPointSpinBox->setEnabled(currentPoint != -1);
  347. d->SharpnessSpinBox->setEnabled(currentPoint != -1);
  348. if (d->CurrentControlPointsItem)
  349. {
  350. this->updateCurrentPoint();
  351. }
  352. }
  353. // ----------------------------------------------------------------------------
  354. void ctkVTKScalarsToColorsWidget::updateNumberOfPoints()
  355. {
  356. Q_D(ctkVTKScalarsToColorsWidget);
  357. const int numberOfPoints = d->CurrentControlPointsItem ?
  358. d->CurrentControlPointsItem->GetNumberOfPoints() : 0;
  359. d->PointIdSpinBox->setMaximum( numberOfPoints - 1 );
  360. }
  361. // ----------------------------------------------------------------------------
  362. void ctkVTKScalarsToColorsWidget::updateCurrentPoint()
  363. {
  364. Q_D(ctkVTKScalarsToColorsWidget);
  365. Q_ASSERT(d->CurrentControlPointsItem);
  366. Q_ASSERT(d->PointIdSpinBox->value() == d->CurrentControlPointsItem->GetCurrentPoint());
  367. this->updateNumberOfPoints();
  368. int pointId = d->PointIdSpinBox->value();
  369. if (pointId == -1)
  370. {
  371. return;
  372. }
  373. double point[4] = {0.0};
  374. d->CurrentControlPointsItem->GetControlPoint(pointId, point);
  375. vtkAxis* xAxis = d->CurrentControlPointsItem ?
  376. d->CurrentControlPointsItem->GetXAxis() : d->View->chart()->GetAxis(vtkAxis::BOTTOM);
  377. Q_ASSERT(xAxis);
  378. if (xAxis && (xAxis->GetMinimumLimit() > point[0] || xAxis->GetMaximumLimit() < point[0]))
  379. {
  380. xAxis->SetMinimumLimit(qMin(xAxis->GetMinimumLimit(), point[0]));
  381. xAxis->SetMaximumLimit(qMax(xAxis->GetMaximumLimit(), point[0]));
  382. d->View->boundAxesToChartBounds();
  383. this->onAxesModified();
  384. }
  385. bool oldBlock = d->blockSignals(true);
  386. d->XSpinBox->setValue(point[0]);
  387. d->OpacitySpinBox->setValue(point[1]);
  388. d->MidPointSpinBox->setValue(point[2]);
  389. d->SharpnessSpinBox->setValue(point[3]);
  390. vtkColorTransferControlPointsItem* colorControlPoints =
  391. vtkColorTransferControlPointsItem::SafeDownCast(d->CurrentControlPointsItem);
  392. vtkCompositeControlPointsItem* compositeControlPoints =
  393. vtkCompositeControlPointsItem::SafeDownCast(d->CurrentControlPointsItem);
  394. if (colorControlPoints &&
  395. (!compositeControlPoints ||
  396. compositeControlPoints->GetPointsFunction() == vtkCompositeControlPointsItem::ColorPointsFunction ||
  397. compositeControlPoints->GetPointsFunction() == vtkCompositeControlPointsItem::ColorAndOpacityPointsFunction))
  398. {
  399. vtkColorTransferFunction* colorTF =
  400. colorControlPoints->GetColorTransferFunction();
  401. double xrgbms[6];
  402. colorTF->GetNodeValue(d->PointIdSpinBox->value(), xrgbms);
  403. QColor color = QColor::fromRgbF(xrgbms[1], xrgbms[2], xrgbms[3]);
  404. d->ColorPickerButton->setColor(color);
  405. }
  406. d->blockSignals(oldBlock);
  407. }
  408. // ----------------------------------------------------------------------------
  409. void ctkVTKScalarsToColorsWidget::onColorChanged(const QColor& color)
  410. {
  411. Q_D(ctkVTKScalarsToColorsWidget);
  412. if (!color.isValid())
  413. {
  414. return;
  415. }
  416. Q_ASSERT(d->CurrentControlPointsItem);
  417. Q_ASSERT(d->PointIdSpinBox->value() != -1);
  418. Q_ASSERT(d->PointIdSpinBox->value() == d->CurrentControlPointsItem->GetCurrentPoint());
  419. vtkColorTransferControlPointsItem* colorControlPoints =
  420. vtkColorTransferControlPointsItem::SafeDownCast(d->CurrentControlPointsItem);
  421. if (colorControlPoints)
  422. {
  423. vtkColorTransferFunction* colorTF =
  424. colorControlPoints->GetColorTransferFunction();
  425. double point[6];
  426. colorTF->GetNodeValue(d->PointIdSpinBox->value(), point);
  427. point[1] = color.redF();
  428. point[2] = color.greenF();
  429. point[3] = color.blueF();
  430. colorTF->SetNodeValue(d->PointIdSpinBox->value(), point);
  431. }
  432. }
  433. // ----------------------------------------------------------------------------
  434. void ctkVTKScalarsToColorsWidget::onXChanged(double x)
  435. {
  436. Q_D(ctkVTKScalarsToColorsWidget);
  437. if (!d->CurrentControlPointsItem)
  438. {
  439. return;
  440. }
  441. bool validRange = d->checkXRange(x, d->PointIdSpinBox->value());
  442. if (!validRange)
  443. {
  444. return;
  445. }
  446. double point[4];
  447. d->CurrentControlPointsItem->GetControlPoint(d->PointIdSpinBox->value(), point);
  448. point[0] = x;
  449. d->CurrentControlPointsItem->SetControlPoint(d->PointIdSpinBox->value(), point);
  450. }
  451. // ----------------------------------------------------------------------------
  452. void ctkVTKScalarsToColorsWidget::onOpacityChanged(double opacity)
  453. {
  454. Q_D(ctkVTKScalarsToColorsWidget);
  455. Q_ASSERT(d->CurrentControlPointsItem);
  456. double point[4];
  457. d->CurrentControlPointsItem->GetControlPoint(d->PointIdSpinBox->value(), point);
  458. point[1] = opacity;
  459. d->CurrentControlPointsItem->SetControlPoint(d->PointIdSpinBox->value(), point);
  460. }
  461. // ----------------------------------------------------------------------------
  462. void ctkVTKScalarsToColorsWidget::onMidPointChanged(double midPoint)
  463. {
  464. Q_D(ctkVTKScalarsToColorsWidget);
  465. Q_ASSERT(d->CurrentControlPointsItem);
  466. double point[4];
  467. d->CurrentControlPointsItem->GetControlPoint(d->PointIdSpinBox->value(), point);
  468. point[2] = midPoint;
  469. d->CurrentControlPointsItem->SetControlPoint(d->PointIdSpinBox->value(), point);
  470. }
  471. // ----------------------------------------------------------------------------
  472. void ctkVTKScalarsToColorsWidget::onSharpnessChanged(double sharpness)
  473. {
  474. Q_D(ctkVTKScalarsToColorsWidget);
  475. Q_ASSERT(d->CurrentControlPointsItem);
  476. double point[4];
  477. d->CurrentControlPointsItem->GetControlPoint(d->PointIdSpinBox->value(), point);
  478. point[3] = sharpness;
  479. d->CurrentControlPointsItem->SetControlPoint(d->PointIdSpinBox->value(), point);
  480. }
  481. // ----------------------------------------------------------------------------
  482. void ctkVTKScalarsToColorsWidget::xRange(double* range)const
  483. {
  484. Q_D(const ctkVTKScalarsToColorsWidget);
  485. vtkAxis* xAxis = d->CurrentControlPointsItem ?
  486. d->CurrentControlPointsItem->GetXAxis() : d->View->chart()->GetAxis(vtkAxis::BOTTOM);
  487. Q_ASSERT(xAxis);
  488. range[0] = xAxis->GetMinimum();
  489. range[1] = xAxis->GetMaximum();
  490. }
  491. // ----------------------------------------------------------------------------
  492. void ctkVTKScalarsToColorsWidget::setXRange(double min, double max)
  493. {
  494. Q_D(ctkVTKScalarsToColorsWidget);
  495. vtkAxis* xAxis = d->CurrentControlPointsItem ?
  496. d->CurrentControlPointsItem->GetXAxis() : d->View->chart()->GetAxis(vtkAxis::BOTTOM);
  497. Q_ASSERT(xAxis);
  498. if (xAxis->GetMinimum() != min || xAxis->GetMaximum() != max)
  499. {
  500. xAxis->SetRange(min, max);
  501. // Repaint the scene
  502. d->View->scene()->SetDirty(true);
  503. }
  504. }
  505. // ----------------------------------------------------------------------------
  506. void ctkVTKScalarsToColorsWidget::yRange(double* range)const
  507. {
  508. Q_D(const ctkVTKScalarsToColorsWidget);
  509. vtkAxis* yAxis = d->CurrentControlPointsItem ?
  510. d->CurrentControlPointsItem->GetYAxis() : d->View->chart()->GetAxis(vtkAxis::LEFT);
  511. Q_ASSERT(yAxis);
  512. range[0] = yAxis->GetMinimum();
  513. range[1] = yAxis->GetMaximum();
  514. }
  515. // ----------------------------------------------------------------------------
  516. void ctkVTKScalarsToColorsWidget::setYRange(double min, double max)
  517. {
  518. Q_D(ctkVTKScalarsToColorsWidget);
  519. vtkAxis* yAxis = d->CurrentControlPointsItem ?
  520. d->CurrentControlPointsItem->GetYAxis() : d->View->chart()->GetAxis(vtkAxis::LEFT);
  521. Q_ASSERT(yAxis);
  522. if (yAxis->GetMinimum() != min || yAxis->GetMaximum() != max)
  523. {
  524. yAxis->SetRange(min, max);
  525. // Repaint the scene
  526. d->View->scene()->SetDirty(true);
  527. }
  528. }
  529. // ----------------------------------------------------------------------------
  530. void ctkVTKScalarsToColorsWidget::resetRange()
  531. {
  532. Q_D(ctkVTKScalarsToColorsWidget);
  533. vtkAxis* xAxis = d->CurrentControlPointsItem ?
  534. d->CurrentControlPointsItem->GetXAxis() : d->View->chart()->GetAxis(vtkAxis::BOTTOM);
  535. if (xAxis)
  536. {
  537. this->setXRange(xAxis->GetMinimumLimit(), xAxis->GetMaximumLimit());
  538. }
  539. vtkAxis* yAxis = d->CurrentControlPointsItem ?
  540. d->CurrentControlPointsItem->GetYAxis() : d->View->chart()->GetAxis(vtkAxis::LEFT);
  541. if (yAxis)
  542. {
  543. this->setYRange(yAxis->GetMinimumLimit(), yAxis->GetMaximumLimit());
  544. }
  545. }
  546. // ----------------------------------------------------------------------------
  547. void ctkVTKScalarsToColorsWidget::onAxesModified()
  548. {
  549. Q_D(ctkVTKScalarsToColorsWidget);
  550. vtkAxis* xAxis = d->CurrentControlPointsItem ?
  551. d->CurrentControlPointsItem->GetXAxis() : d->View->chart()->GetAxis(vtkAxis::BOTTOM);
  552. Q_ASSERT(xAxis);
  553. bool wasBlocking = d->XRangeSlider->blockSignals(true);
  554. d->XRangeSlider->setRange(xAxis->GetMinimumLimit(), xAxis->GetMaximumLimit());
  555. d->XRangeSlider->setValues(xAxis->GetMinimum(), xAxis->GetMaximum());
  556. d->XSpinBox->setRange(xAxis->GetMinimumLimit(), xAxis->GetMaximumLimit());
  557. d->XSpinBox->setSingleStep(
  558. ctk::closestPowerOfTen(xAxis->GetMaximumLimit() - xAxis->GetMinimumLimit()) / 100);
  559. d->XRangeSlider->blockSignals(wasBlocking);
  560. vtkAxis* yAxis = d->CurrentControlPointsItem ?
  561. d->CurrentControlPointsItem->GetYAxis() : d->View->chart()->GetAxis(vtkAxis::LEFT);
  562. Q_ASSERT(yAxis);
  563. wasBlocking = d->YRangeSlider->blockSignals(true);
  564. d->YRangeSlider->setRange(yAxis->GetMinimumLimit(), yAxis->GetMaximumLimit());
  565. d->YRangeSlider->setValues(yAxis->GetMinimum(), yAxis->GetMaximum());
  566. d->YRangeSlider->blockSignals(wasBlocking);
  567. }
  568. // ----------------------------------------------------------------------------
  569. void ctkVTKScalarsToColorsWidget::restorePalette()
  570. {
  571. Q_D(ctkVTKScalarsToColorsWidget);
  572. d->XSpinBox->setPalette(this->palette());
  573. }
  574. // ----------------------------------------------------------------------------
  575. void ctkVTKScalarsToColorsWidget::onExpandButton(bool state)
  576. {
  577. Q_D(ctkVTKScalarsToColorsWidget);
  578. d->MidPointLabel->setVisible(state);;
  579. d->MidPointSpinBox->setVisible(state);
  580. d->SharpnessLabel->setVisible(state);
  581. d->SharpnessSpinBox->setVisible(state);
  582. }
  583. // ----------------------------------------------------------------------------
  584. QWidgetList ctkVTKScalarsToColorsWidget::extraWidgets()const
  585. {
  586. Q_D(const ctkVTKScalarsToColorsWidget);
  587. QWidgetList widgets;
  588. for (int i = 0; i < d->TopLayout->count(); ++i)
  589. {
  590. QLayoutItem* topLeftItem = d->TopLayout->itemAt(i);
  591. if (topLeftItem->spacerItem())
  592. {
  593. break;
  594. }
  595. widgets << topLeftItem->widget();
  596. }
  597. return widgets;
  598. }
  599. // ----------------------------------------------------------------------------
  600. void ctkVTKScalarsToColorsWidget::addExtraWidget(QWidget* extraWidget)
  601. {
  602. Q_D(const ctkVTKScalarsToColorsWidget);
  603. d->TopLayout->insertWidget(this->extraWidgets().count(), extraWidget);
  604. if (!d->TopWidgetsVisible)
  605. {
  606. extraWidget->setVisible(d->TopWidgetsVisible);
  607. }
  608. }