ctkVTKChartView.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.commontk.org/LICENSE
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QDebug>
  16. #include <QMouseEvent>
  17. // CTK includes
  18. #include "ctkLogger.h"
  19. #include "ctkVTKChartView.h"
  20. // VTK includes
  21. #include <vtkAxis.h>
  22. #include <vtkChartXY.h>
  23. #include <vtkContext2D.h>
  24. #include <vtkContextMouseEvent.h>
  25. #include <vtkContextScene.h>
  26. #include <vtkContextView.h>
  27. #include <vtkOpenGLContextDevice2D.h>
  28. #include <vtkPlot.h>
  29. #include <vtkRenderWindow.h>
  30. //----------------------------------------------------------------------------
  31. static ctkLogger logger("org.commontk.visualization.vtk.widgets.ctkVTKChartView");
  32. //----------------------------------------------------------------------------
  33. class ctkVTKChartViewPrivate
  34. {
  35. Q_DECLARE_PUBLIC(ctkVTKChartView);
  36. protected:
  37. ctkVTKChartView* const q_ptr;
  38. public:
  39. ctkVTKChartViewPrivate(ctkVTKChartView& object);
  40. void init();
  41. vtkSmartPointer<vtkContextView> ContextView;
  42. vtkSmartPointer<vtkChartXY> Chart;
  43. };
  44. // ----------------------------------------------------------------------------
  45. // ctkVTKChartViewPrivate methods
  46. // ----------------------------------------------------------------------------
  47. ctkVTKChartViewPrivate::ctkVTKChartViewPrivate(ctkVTKChartView& object)
  48. :q_ptr(&object)
  49. {
  50. this->ContextView = vtkSmartPointer<vtkContextView>::New();
  51. this->Chart = vtkSmartPointer<vtkChartXY>::New();
  52. this->ContextView->GetScene()->AddItem(this->Chart);
  53. }
  54. // ----------------------------------------------------------------------------
  55. void ctkVTKChartViewPrivate::init()
  56. {
  57. Q_Q(ctkVTKChartView);
  58. this->ContextView->SetInteractor(q->GetInteractor());
  59. q->SetRenderWindow(this->ContextView->GetRenderWindow());
  60. // low def for now (faster)
  61. //q->GetRenderWindow()->SetMultiSamples(0);
  62. //vtkOpenGLContextDevice2D::SafeDownCast(this->ContextView->GetContext()->GetDevice())
  63. // ->SetStringRendererToQt();
  64. this->Chart->SetActionToButton(vtkChart::PAN, vtkContextMouseEvent::MIDDLE_BUTTON);
  65. this->Chart->SetActionToButton(vtkChart::SELECT, vtkContextMouseEvent::RIGHT_BUTTON);
  66. }
  67. // ----------------------------------------------------------------------------
  68. // ctkVTKChartView methods
  69. // ----------------------------------------------------------------------------
  70. ctkVTKChartView::ctkVTKChartView(QWidget* parentWidget)
  71. :QVTKWidget(parentWidget)
  72. , d_ptr(new ctkVTKChartViewPrivate(*this))
  73. {
  74. Q_D(ctkVTKChartView);
  75. d->init();
  76. this->setAutomaticImageCacheEnabled(true);
  77. }
  78. // ----------------------------------------------------------------------------
  79. ctkVTKChartView::~ctkVTKChartView()
  80. {
  81. }
  82. // ----------------------------------------------------------------------------
  83. void ctkVTKChartView::setTitle(const QString& newTitle)
  84. {
  85. Q_D(ctkVTKChartView);
  86. d->Chart->SetTitle(newTitle.toLatin1().data());
  87. }
  88. // ----------------------------------------------------------------------------
  89. QString ctkVTKChartView::title()const
  90. {
  91. Q_D(const ctkVTKChartView);
  92. return QString(d->Chart->GetTitle());
  93. }
  94. // ----------------------------------------------------------------------------
  95. vtkChartXY* ctkVTKChartView::chart()const
  96. {
  97. Q_D(const ctkVTKChartView);
  98. return d->Chart;
  99. }
  100. // ----------------------------------------------------------------------------
  101. vtkContextScene* ctkVTKChartView::scene()const
  102. {
  103. Q_D(const ctkVTKChartView);
  104. return d->ContextView->GetScene();
  105. }
  106. // ----------------------------------------------------------------------------
  107. void ctkVTKChartView::addPlot(vtkPlot* plot)
  108. {
  109. Q_D(ctkVTKChartView);
  110. d->Chart->AddPlot(plot);
  111. emit this->plotAdded(plot);
  112. this->onChartUpdated();
  113. }
  114. // ----------------------------------------------------------------------------
  115. void ctkVTKChartView::onChartUpdated()
  116. {
  117. }
  118. // ----------------------------------------------------------------------------
  119. void ctkVTKChartView::chartBounds(double* bounds)
  120. {
  121. bounds[0] = bounds[2] = bounds[4] = bounds[6] = VTK_DOUBLE_MAX;
  122. bounds[1] = bounds[3] = bounds[5] = bounds[7] = VTK_DOUBLE_MIN;
  123. vtkChartXY* chart = this->chart();
  124. const vtkIdType plotCount = chart->GetNumberOfPlots();
  125. for (vtkIdType i = 0; i < plotCount; ++i)
  126. {
  127. vtkPlot* plot = chart->GetPlot(i);
  128. int corner = chart->GetPlotCorner(plot);
  129. double plotBounds[4];
  130. plot->GetBounds(plotBounds);
  131. switch (corner)
  132. {
  133. // bottom left
  134. case 0:
  135. // x
  136. bounds[2] = bounds[2] > plotBounds[0] ?
  137. plotBounds[0] : bounds[2];
  138. bounds[3] = bounds[3] < plotBounds[1] ?
  139. plotBounds[1] : bounds[3];
  140. // y
  141. bounds[0] = bounds[0] > plotBounds[2] ?
  142. plotBounds[2] : bounds[0];
  143. bounds[1] = bounds[1] < plotBounds[3] ?
  144. plotBounds[3] : bounds[1];
  145. break;
  146. // bottom right
  147. case 1:
  148. // x
  149. bounds[2] = bounds[2] > plotBounds[0] ?
  150. plotBounds[0] : bounds[2];
  151. bounds[3] = bounds[3] < plotBounds[1] ?
  152. plotBounds[1] : bounds[3];
  153. // y
  154. bounds[4] = bounds[4] > plotBounds[2] ?
  155. plotBounds[2] : bounds[4];
  156. bounds[5] = bounds[5] < plotBounds[3] ?
  157. plotBounds[3] : bounds[5];
  158. break;
  159. // top right
  160. case 2:
  161. // x
  162. bounds[6] = bounds[6] > plotBounds[0] ?
  163. plotBounds[0] : bounds[6];
  164. bounds[7] = bounds[7] < plotBounds[1] ?
  165. plotBounds[1] : bounds[7];
  166. // y
  167. bounds[4] = bounds[4] > plotBounds[2] ?
  168. plotBounds[2] : bounds[4];
  169. bounds[5] = bounds[5] < plotBounds[3] ?
  170. plotBounds[3] : bounds[5];
  171. break;
  172. // top left
  173. case 3:
  174. // x
  175. bounds[6] = bounds[6] > plotBounds[0] ?
  176. plotBounds[0] : bounds[6];
  177. bounds[7] = bounds[7] < plotBounds[1] ?
  178. plotBounds[1] : bounds[7];
  179. // y
  180. bounds[0] = bounds[0] > plotBounds[2] ?
  181. plotBounds[2] : bounds[1];
  182. bounds[1] = bounds[0] < plotBounds[3] ?
  183. plotBounds[3] : bounds[1];
  184. break;
  185. }
  186. }
  187. }
  188. // ----------------------------------------------------------------------------
  189. void ctkVTKChartView::setAxesToChartBounds()
  190. {
  191. vtkChartXY* chart = this->chart();
  192. double bounds[8];
  193. this->chartBounds(bounds);
  194. for (int i = 0; i < chart->GetNumberOfAxes(); ++i)
  195. {
  196. if (bounds[2*i] != VTK_DOUBLE_MAX)
  197. {
  198. chart->GetAxis(i)->SetRange(bounds[2*i], bounds[2*i+1]);
  199. chart->GetAxis(i)->SetBehavior(1);
  200. }
  201. }
  202. }
  203. // ----------------------------------------------------------------------------
  204. void ctkVTKChartView::boundAxesToChartBounds()
  205. {
  206. vtkChartXY* chart = this->chart();
  207. double bounds[8];
  208. this->chartBounds(bounds);
  209. for (int i = 0; i < chart->GetNumberOfAxes(); ++i)
  210. {
  211. if (bounds[2*i] != VTK_DOUBLE_MAX)
  212. {
  213. chart->GetAxis(i)->SetMinimumLimit(bounds[2*i]);
  214. chart->GetAxis(i)->SetMaximumLimit(bounds[2*i + 1]);
  215. }
  216. }
  217. emit boundsChanged();
  218. }
  219. // ----------------------------------------------------------------------------
  220. void ctkVTKChartView::mouseDoubleClickEvent(QMouseEvent* event)
  221. {
  222. if (event->button() == Qt::MidButton)
  223. {
  224. this->setAxesToChartBounds();
  225. }
  226. this->QVTKWidget::mouseDoubleClickEvent(event);
  227. }