ctkVTKScalarsToColorsWidget.cpp 23 KB

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