ctkVTKChartView.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 <QMouseEvent>
  16. // CTK includes
  17. #include "ctkLogger.h"
  18. #include "ctkVTKChartView.h"
  19. // VTK includes
  20. #include <vtkAxis.h>
  21. #include <vtkChartXY.h>
  22. #include <vtkContext2D.h>
  23. #include <vtkContextMouseEvent.h>
  24. #include <vtkContextScene.h>
  25. #include <vtkContextView.h>
  26. #include <vtkOpenGLContextDevice2D.h>
  27. #include <vtkPlot.h>
  28. #include <vtkRenderWindow.h>
  29. //----------------------------------------------------------------------------
  30. static ctkLogger logger("org.commontk.visualization.vtk.widgets.ctkVTKChartView");
  31. //----------------------------------------------------------------------------
  32. class ctkVTKChartViewPrivate
  33. {
  34. Q_DECLARE_PUBLIC(ctkVTKChartView);
  35. protected:
  36. ctkVTKChartView* const q_ptr;
  37. public:
  38. ctkVTKChartViewPrivate(ctkVTKChartView& object);
  39. void init();
  40. vtkSmartPointer<vtkContextView> ContextView;
  41. vtkSmartPointer<vtkChartXY> Chart;
  42. };
  43. // ----------------------------------------------------------------------------
  44. // ctkVTKChartViewPrivate methods
  45. // ----------------------------------------------------------------------------
  46. ctkVTKChartViewPrivate::ctkVTKChartViewPrivate(ctkVTKChartView& object)
  47. :q_ptr(&object)
  48. {
  49. this->ContextView = vtkSmartPointer<vtkContextView>::New();
  50. this->Chart = vtkSmartPointer<vtkChartXY>::New();
  51. this->ContextView->GetScene()->AddItem(this->Chart);
  52. }
  53. // ----------------------------------------------------------------------------
  54. void ctkVTKChartViewPrivate::init()
  55. {
  56. Q_Q(ctkVTKChartView);
  57. this->ContextView->SetInteractor(q->GetInteractor());
  58. q->SetRenderWindow(this->ContextView->GetRenderWindow());
  59. // low def for now (faster)
  60. //q->GetRenderWindow()->SetMultiSamples(0);
  61. //vtkOpenGLContextDevice2D::SafeDownCast(this->ContextView->GetContext()->GetDevice())
  62. // ->SetStringRendererToQt();
  63. this->Chart->SetActionToButton(vtkChart::PAN, vtkContextMouseEvent::MIDDLE_BUTTON);
  64. this->Chart->SetActionToButton(vtkChart::SELECT, vtkContextMouseEvent::RIGHT_BUTTON);
  65. }
  66. // ----------------------------------------------------------------------------
  67. // ctkVTKChartView methods
  68. // ----------------------------------------------------------------------------
  69. ctkVTKChartView::ctkVTKChartView(QWidget* parentWidget)
  70. :QVTKWidget(parentWidget)
  71. , d_ptr(new ctkVTKChartViewPrivate(*this))
  72. {
  73. Q_D(ctkVTKChartView);
  74. d->init();
  75. this->setAutomaticImageCacheEnabled(true);
  76. }
  77. // ----------------------------------------------------------------------------
  78. ctkVTKChartView::~ctkVTKChartView()
  79. {
  80. }
  81. // ----------------------------------------------------------------------------
  82. void ctkVTKChartView::setTitle(const QString& newTitle)
  83. {
  84. Q_D(ctkVTKChartView);
  85. d->Chart->SetTitle(newTitle.toLatin1().data());
  86. }
  87. // ----------------------------------------------------------------------------
  88. QString ctkVTKChartView::title()const
  89. {
  90. Q_D(const ctkVTKChartView);
  91. return QString(d->Chart->GetTitle());
  92. }
  93. // ----------------------------------------------------------------------------
  94. vtkChartXY* ctkVTKChartView::chart()const
  95. {
  96. Q_D(const ctkVTKChartView);
  97. return d->Chart;
  98. }
  99. // ----------------------------------------------------------------------------
  100. vtkContextScene* ctkVTKChartView::scene()const
  101. {
  102. Q_D(const ctkVTKChartView);
  103. return d->ContextView->GetScene();
  104. }
  105. // ----------------------------------------------------------------------------
  106. void ctkVTKChartView::addPlot(vtkPlot* plot)
  107. {
  108. Q_D(ctkVTKChartView);
  109. d->Chart->AddPlot(plot);
  110. emit this->plotAdded(plot);
  111. }
  112. // ----------------------------------------------------------------------------
  113. void ctkVTKChartView::fitAxesToBounds()
  114. {
  115. vtkChartXY* chart = this->chart();
  116. const vtkIdType plotCount = chart->GetNumberOfPlots();
  117. double extremaBounds[8] = {VTK_DOUBLE_MAX, VTK_DOUBLE_MIN,
  118. VTK_DOUBLE_MAX, VTK_DOUBLE_MIN,
  119. VTK_DOUBLE_MAX, VTK_DOUBLE_MIN};
  120. for (vtkIdType i = 0; i < plotCount; ++i)
  121. {
  122. vtkPlot* plot = chart->GetPlot(i);
  123. int corner = chart->GetPlotCorner(plot);
  124. double bounds[4];
  125. plot->GetBounds(bounds);
  126. switch (corner)
  127. {
  128. // bottom left
  129. case 0:
  130. // x
  131. extremaBounds[2] = extremaBounds[2] > bounds[0] ?
  132. bounds[0] : extremaBounds[2];
  133. extremaBounds[3] = extremaBounds[3] < bounds[1] ?
  134. bounds[1] : extremaBounds[3];
  135. // y
  136. extremaBounds[0] = extremaBounds[0] > bounds[2] ?
  137. bounds[2] : extremaBounds[0];
  138. extremaBounds[1] = extremaBounds[1] < bounds[3] ?
  139. bounds[3] : extremaBounds[1];
  140. break;
  141. // bottom right
  142. case 1:
  143. // x
  144. extremaBounds[2] = extremaBounds[2] > bounds[0] ?
  145. bounds[0] : extremaBounds[2];
  146. extremaBounds[3] = extremaBounds[3] < bounds[1] ?
  147. bounds[1] : extremaBounds[3];
  148. // y
  149. extremaBounds[4] = extremaBounds[4] > bounds[2] ?
  150. bounds[2] : extremaBounds[4];
  151. extremaBounds[5] = extremaBounds[5] < bounds[3] ?
  152. bounds[3] : extremaBounds[5];
  153. break;
  154. // top right
  155. case 2:
  156. // x
  157. extremaBounds[6] = extremaBounds[6] > bounds[0] ?
  158. bounds[0] : extremaBounds[6];
  159. extremaBounds[7] = extremaBounds[7] < bounds[1] ?
  160. bounds[1] : extremaBounds[7];
  161. // y
  162. extremaBounds[4] = extremaBounds[4] > bounds[2] ?
  163. bounds[2] : extremaBounds[4];
  164. extremaBounds[5] = extremaBounds[5] < bounds[3] ?
  165. bounds[3] : extremaBounds[5];
  166. break;
  167. // top left
  168. case 3:
  169. // x
  170. extremaBounds[6] = extremaBounds[6] > bounds[0] ?
  171. bounds[0] : extremaBounds[6];
  172. extremaBounds[7] = extremaBounds[7] < bounds[1] ?
  173. bounds[1] : extremaBounds[7];
  174. // y
  175. extremaBounds[0] = extremaBounds[0] > bounds[2] ?
  176. bounds[2] : extremaBounds[1];
  177. extremaBounds[1] = extremaBounds[0] < bounds[3] ?
  178. bounds[3] : extremaBounds[1];
  179. break;
  180. }
  181. }
  182. for (int i = 0; i < chart->GetNumberOfAxes(); ++i)
  183. {
  184. if (extremaBounds[2*i] != VTK_DOUBLE_MAX)
  185. {
  186. chart->GetAxis(i)->SetRange(extremaBounds[2*i], extremaBounds[2*i+1]);
  187. chart->GetAxis(i)->SetBehavior(1);
  188. }
  189. }
  190. }
  191. // ----------------------------------------------------------------------------
  192. void ctkVTKChartView::mouseDoubleClickEvent(QMouseEvent* event)
  193. {
  194. if (event->button() == Qt::MidButton)
  195. {
  196. this->fitAxesToBounds();
  197. }
  198. this->QVTKWidget::mouseDoubleClickEvent(event);
  199. }