ctkVTKChartView.cpp 10.0 KB

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