ctkVTKChartView.cpp 10 KB

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