ctkVTKScalarsToColorsWidget.cpp 22 KB

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