ctkVTKChartView.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. void chartBounds(double* bounds)const;
  42. vtkSmartPointer<vtkContextView> ContextView;
  43. vtkSmartPointer<vtkChartXY> Chart;
  44. double UserBounds[8];
  45. };
  46. // ----------------------------------------------------------------------------
  47. // ctkVTKChartViewPrivate methods
  48. // ----------------------------------------------------------------------------
  49. ctkVTKChartViewPrivate::ctkVTKChartViewPrivate(ctkVTKChartView& object)
  50. :q_ptr(&object)
  51. {
  52. this->ContextView = vtkSmartPointer<vtkContextView>::New();
  53. this->Chart = vtkSmartPointer<vtkChartXY>::New();
  54. this->ContextView->GetScene()->AddItem(this->Chart);
  55. this->UserBounds[0] = this->UserBounds[2] = this->UserBounds[4] = this->UserBounds[6] = 0.;
  56. this->UserBounds[1] = this->UserBounds[3] = this->UserBounds[5] = this->UserBounds[7] = -1.;
  57. }
  58. // ----------------------------------------------------------------------------
  59. void ctkVTKChartViewPrivate::init()
  60. {
  61. Q_Q(ctkVTKChartView);
  62. this->ContextView->SetInteractor(q->GetInteractor());
  63. q->SetRenderWindow(this->ContextView->GetRenderWindow());
  64. // low def for now (faster)
  65. //q->GetRenderWindow()->SetMultiSamples(0);
  66. //vtkOpenGLContextDevice2D::SafeDownCast(this->ContextView->GetContext()->GetDevice())
  67. // ->SetStringRendererToQt();
  68. this->Chart->SetActionToButton(vtkChart::PAN, vtkContextMouseEvent::MIDDLE_BUTTON);
  69. this->Chart->SetActionToButton(vtkChart::SELECT, vtkContextMouseEvent::RIGHT_BUTTON);
  70. }
  71. // ----------------------------------------------------------------------------
  72. void ctkVTKChartViewPrivate::chartBounds(double* bounds)const
  73. {
  74. Q_Q(const ctkVTKChartView);
  75. bounds[0] = bounds[2] = bounds[4] = bounds[6] = VTK_DOUBLE_MAX;
  76. bounds[1] = bounds[3] = bounds[5] = bounds[7] = VTK_DOUBLE_MIN;
  77. vtkChartXY* chart = q->chart();
  78. const vtkIdType plotCount = chart->GetNumberOfPlots();
  79. for (vtkIdType i = 0; i < plotCount; ++i)
  80. {
  81. vtkPlot* plot = chart->GetPlot(i);
  82. int corner = chart->GetPlotCorner(plot);
  83. double plotBounds[4];
  84. plot->GetBounds(plotBounds);
  85. switch (corner)
  86. {
  87. // bottom left
  88. case 0:
  89. // x
  90. bounds[2] = bounds[2] > plotBounds[0] ?
  91. plotBounds[0] : bounds[2];
  92. bounds[3] = bounds[3] < plotBounds[1] ?
  93. plotBounds[1] : bounds[3];
  94. // y
  95. bounds[0] = bounds[0] > plotBounds[2] ?
  96. plotBounds[2] : bounds[0];
  97. bounds[1] = bounds[1] < plotBounds[3] ?
  98. plotBounds[3] : bounds[1];
  99. break;
  100. // bottom right
  101. case 1:
  102. // x
  103. bounds[2] = bounds[2] > plotBounds[0] ?
  104. plotBounds[0] : bounds[2];
  105. bounds[3] = bounds[3] < plotBounds[1] ?
  106. plotBounds[1] : bounds[3];
  107. // y
  108. bounds[4] = bounds[4] > plotBounds[2] ?
  109. plotBounds[2] : bounds[4];
  110. bounds[5] = bounds[5] < plotBounds[3] ?
  111. plotBounds[3] : bounds[5];
  112. break;
  113. // top right
  114. case 2:
  115. // x
  116. bounds[6] = bounds[6] > plotBounds[0] ?
  117. plotBounds[0] : bounds[6];
  118. bounds[7] = bounds[7] < plotBounds[1] ?
  119. plotBounds[1] : bounds[7];
  120. // y
  121. bounds[4] = bounds[4] > plotBounds[2] ?
  122. plotBounds[2] : bounds[4];
  123. bounds[5] = bounds[5] < plotBounds[3] ?
  124. plotBounds[3] : bounds[5];
  125. break;
  126. // top left
  127. case 3:
  128. // x
  129. bounds[6] = bounds[6] > plotBounds[0] ?
  130. plotBounds[0] : bounds[6];
  131. bounds[7] = bounds[7] < plotBounds[1] ?
  132. plotBounds[1] : bounds[7];
  133. // y
  134. bounds[0] = bounds[0] > plotBounds[2] ?
  135. plotBounds[2] : bounds[1];
  136. bounds[1] = bounds[0] < plotBounds[3] ?
  137. plotBounds[3] : bounds[1];
  138. break;
  139. }
  140. }
  141. }
  142. // ----------------------------------------------------------------------------
  143. // ctkVTKChartView methods
  144. // ----------------------------------------------------------------------------
  145. ctkVTKChartView::ctkVTKChartView(QWidget* parentWidget)
  146. :QVTKWidget(parentWidget)
  147. , d_ptr(new ctkVTKChartViewPrivate(*this))
  148. {
  149. Q_D(ctkVTKChartView);
  150. d->init();
  151. this->setAutomaticImageCacheEnabled(true);
  152. }
  153. // ----------------------------------------------------------------------------
  154. ctkVTKChartView::~ctkVTKChartView()
  155. {
  156. }
  157. // ----------------------------------------------------------------------------
  158. void ctkVTKChartView::setTitle(const QString& newTitle)
  159. {
  160. Q_D(ctkVTKChartView);
  161. d->Chart->SetTitle(newTitle.toLatin1().data());
  162. }
  163. // ----------------------------------------------------------------------------
  164. QString ctkVTKChartView::title()const
  165. {
  166. Q_D(const ctkVTKChartView);
  167. return QString(d->Chart->GetTitle());
  168. }
  169. // ----------------------------------------------------------------------------
  170. vtkChartXY* ctkVTKChartView::chart()const
  171. {
  172. Q_D(const ctkVTKChartView);
  173. return d->Chart;
  174. }
  175. // ----------------------------------------------------------------------------
  176. vtkContextScene* ctkVTKChartView::scene()const
  177. {
  178. Q_D(const ctkVTKChartView);
  179. return d->ContextView->GetScene();
  180. }
  181. // ----------------------------------------------------------------------------
  182. void ctkVTKChartView::addPlot(vtkPlot* plot)
  183. {
  184. Q_D(ctkVTKChartView);
  185. d->Chart->AddPlot(plot);
  186. emit this->plotAdded(plot);
  187. this->onChartUpdated();
  188. }
  189. // ----------------------------------------------------------------------------
  190. void ctkVTKChartView::onChartUpdated()
  191. {
  192. }
  193. // ----------------------------------------------------------------------------
  194. void ctkVTKChartView::chartBounds(double* bounds)const
  195. {
  196. Q_D(const ctkVTKChartView);
  197. if (d->UserBounds[1] < d->UserBounds[0])
  198. {
  199. d->chartBounds(bounds);
  200. return;
  201. }
  202. this->chartUserBounds(bounds);
  203. }
  204. // ----------------------------------------------------------------------------
  205. void ctkVTKChartView::setChartUserBounds(double* userBounds)
  206. {
  207. Q_D(ctkVTKChartView);
  208. for (int i= 0; i < 8; ++i)
  209. {
  210. d->UserBounds[i] = userBounds[i];
  211. }
  212. emit boundsChanged();
  213. }
  214. // ----------------------------------------------------------------------------
  215. void ctkVTKChartView::chartUserBounds(double* bounds)const
  216. {
  217. Q_D(const ctkVTKChartView);
  218. for (int i= 0; i < 8; ++i)
  219. {
  220. bounds[i] = d->UserBounds[i];
  221. }
  222. }
  223. // ----------------------------------------------------------------------------
  224. void ctkVTKChartView::setAxesToChartBounds()
  225. {
  226. vtkChartXY* chart = this->chart();
  227. double bounds[8];
  228. this->chartBounds(bounds);
  229. for (int i = 0; i < chart->GetNumberOfAxes(); ++i)
  230. {
  231. if (bounds[2*i] != VTK_DOUBLE_MAX)
  232. {
  233. chart->GetAxis(i)->SetRange(bounds[2*i], bounds[2*i+1]);
  234. chart->GetAxis(i)->SetBehavior(1);
  235. }
  236. }
  237. }
  238. // ----------------------------------------------------------------------------
  239. void ctkVTKChartView::boundAxesToChartBounds()
  240. {
  241. vtkChartXY* chart = this->chart();
  242. double bounds[8];
  243. this->chartBounds(bounds);
  244. for (int i = 0; i < chart->GetNumberOfAxes(); ++i)
  245. {
  246. if (bounds[2*i] != VTK_DOUBLE_MAX)
  247. {
  248. chart->GetAxis(i)->SetMinimumLimit(bounds[2*i]);
  249. chart->GetAxis(i)->SetMaximumLimit(bounds[2*i + 1]);
  250. }
  251. }
  252. emit boundsChanged();
  253. }
  254. // ----------------------------------------------------------------------------
  255. void ctkVTKChartView::mouseDoubleClickEvent(QMouseEvent* event)
  256. {
  257. if (event->button() == Qt::MidButton)
  258. {
  259. this->setAxesToChartBounds();
  260. }
  261. this->QVTKWidget::mouseDoubleClickEvent(event);
  262. }