ctkVTKChartView.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. #ifndef Q_WS_X11
  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. q->qvtkConnect(q->chart()->GetAxis(vtkAxis::BOTTOM),vtkCommand::ModifiedEvent,
  77. q, SIGNAL(extentChanged()));
  78. q->qvtkConnect(q->chart()->GetAxis(vtkAxis::LEFT),vtkCommand::ModifiedEvent,
  79. q, SIGNAL(extentChanged()));
  80. }
  81. // ----------------------------------------------------------------------------
  82. void ctkVTKChartViewPrivate::chartBounds(double* bounds)const
  83. {
  84. Q_Q(const ctkVTKChartView);
  85. bounds[0] = bounds[2] = bounds[4] = bounds[6] = VTK_DOUBLE_MAX;
  86. bounds[1] = bounds[3] = bounds[5] = bounds[7] = VTK_DOUBLE_MIN;
  87. vtkChartXY* chart = q->chart();
  88. const vtkIdType plotCount = chart->GetNumberOfPlots();
  89. for (vtkIdType i = 0; i < plotCount; ++i)
  90. {
  91. vtkPlot* plot = chart->GetPlot(i);
  92. int corner = chart->GetPlotCorner(plot);
  93. double plotBounds[4];
  94. plot->GetBounds(plotBounds);
  95. switch (corner)
  96. {
  97. // bottom left
  98. case 0:
  99. // x
  100. bounds[2] = bounds[2] > plotBounds[0] ?
  101. plotBounds[0] : bounds[2];
  102. bounds[3] = bounds[3] < plotBounds[1] ?
  103. plotBounds[1] : bounds[3];
  104. // y
  105. bounds[0] = bounds[0] > plotBounds[2] ?
  106. plotBounds[2] : bounds[0];
  107. bounds[1] = bounds[1] < plotBounds[3] ?
  108. plotBounds[3] : bounds[1];
  109. break;
  110. // bottom right
  111. case 1:
  112. // x
  113. bounds[2] = bounds[2] > plotBounds[0] ?
  114. plotBounds[0] : bounds[2];
  115. bounds[3] = bounds[3] < plotBounds[1] ?
  116. plotBounds[1] : bounds[3];
  117. // y
  118. bounds[4] = bounds[4] > plotBounds[2] ?
  119. plotBounds[2] : bounds[4];
  120. bounds[5] = bounds[5] < plotBounds[3] ?
  121. plotBounds[3] : bounds[5];
  122. break;
  123. // top right
  124. case 2:
  125. // x
  126. bounds[6] = bounds[6] > plotBounds[0] ?
  127. plotBounds[0] : bounds[6];
  128. bounds[7] = bounds[7] < plotBounds[1] ?
  129. plotBounds[1] : bounds[7];
  130. // y
  131. bounds[4] = bounds[4] > plotBounds[2] ?
  132. plotBounds[2] : bounds[4];
  133. bounds[5] = bounds[5] < plotBounds[3] ?
  134. plotBounds[3] : bounds[5];
  135. break;
  136. // top left
  137. case 3:
  138. // x
  139. bounds[6] = bounds[6] > plotBounds[0] ?
  140. plotBounds[0] : bounds[6];
  141. bounds[7] = bounds[7] < plotBounds[1] ?
  142. plotBounds[1] : bounds[7];
  143. // y
  144. bounds[0] = bounds[0] > plotBounds[2] ?
  145. plotBounds[2] : bounds[1];
  146. bounds[1] = bounds[0] < plotBounds[3] ?
  147. plotBounds[3] : bounds[1];
  148. break;
  149. }
  150. }
  151. }
  152. // ----------------------------------------------------------------------------
  153. // ctkVTKChartView methods
  154. // ----------------------------------------------------------------------------
  155. ctkVTKChartView::ctkVTKChartView(QWidget* parentWidget)
  156. :Superclass(parentWidget)
  157. , d_ptr(new ctkVTKChartViewPrivate(*this))
  158. {
  159. Q_D(ctkVTKChartView);
  160. d->init();
  161. //this->setAutomaticImageCacheEnabled(true);
  162. }
  163. // ----------------------------------------------------------------------------
  164. ctkVTKChartView::~ctkVTKChartView()
  165. {
  166. }
  167. // ----------------------------------------------------------------------------
  168. void ctkVTKChartView::setTitle(const QString& newTitle)
  169. {
  170. Q_D(ctkVTKChartView);
  171. d->Chart->SetTitle(newTitle.toLatin1().data());
  172. }
  173. // ----------------------------------------------------------------------------
  174. QString ctkVTKChartView::title()const
  175. {
  176. Q_D(const ctkVTKChartView);
  177. return QString(d->Chart->GetTitle());
  178. }
  179. // ----------------------------------------------------------------------------
  180. vtkChartXY* ctkVTKChartView::chart()const
  181. {
  182. Q_D(const ctkVTKChartView);
  183. return d->Chart;
  184. }
  185. // ----------------------------------------------------------------------------
  186. vtkContextScene* ctkVTKChartView::scene()const
  187. {
  188. Q_D(const ctkVTKChartView);
  189. return d->ContextView->GetScene();
  190. }
  191. // ----------------------------------------------------------------------------
  192. void ctkVTKChartView::addPlot(vtkPlot* plot)
  193. {
  194. Q_D(ctkVTKChartView);
  195. d->Chart->AddPlot(plot);
  196. emit this->plotAdded(plot);
  197. this->onChartUpdated();
  198. }
  199. // ----------------------------------------------------------------------------
  200. void ctkVTKChartView::removePlot(vtkPlot* plot)
  201. {
  202. Q_D(ctkVTKChartView);
  203. vtkIdType index = this->plotIndex(plot);
  204. if (index == vtkIdType(-1))
  205. {
  206. return;
  207. }
  208. d->Chart->RemovePlot(index);
  209. emit this->plotRemoved(plot);
  210. this->onChartUpdated();
  211. }
  212. // ----------------------------------------------------------------------------
  213. void ctkVTKChartView::removeAllPlots()
  214. {
  215. Q_D(ctkVTKChartView);
  216. while(d->Chart->GetNumberOfPlots() > 0)
  217. {
  218. this->removePlot(d->Chart->GetPlot(0));
  219. }
  220. }
  221. // ----------------------------------------------------------------------------
  222. vtkIdType ctkVTKChartView::plotIndex(vtkPlot* plot)
  223. {
  224. Q_D(ctkVTKChartView);
  225. // GetPlotIndex is missing from vtkChart API
  226. for (vtkIdType i = 0; i < d->Chart->GetNumberOfPlots(); ++i)
  227. {
  228. if (plot == d->Chart->GetPlot(i))
  229. {
  230. return i;
  231. }
  232. }
  233. return -1;
  234. }
  235. // ----------------------------------------------------------------------------
  236. void ctkVTKChartView::onChartUpdated()
  237. {
  238. Q_D(ctkVTKChartView);
  239. double oldBounds[8];
  240. memcpy(oldBounds, d->OldBounds, 8 * sizeof(double));
  241. double newBounds[8];
  242. this->chartBounds(newBounds);
  243. if (oldBounds[0] != newBounds[0] ||
  244. oldBounds[1] != newBounds[1] ||
  245. oldBounds[2] != newBounds[2] ||
  246. oldBounds[3] != newBounds[3] ||
  247. oldBounds[4] != newBounds[4] ||
  248. oldBounds[5] != newBounds[5] ||
  249. oldBounds[6] != newBounds[6] ||
  250. oldBounds[7] != newBounds[7])
  251. {
  252. emit boundsChanged();
  253. }
  254. }
  255. // ----------------------------------------------------------------------------
  256. void ctkVTKChartView::chartExtent(double* extent)const
  257. {
  258. extent[0] = extent[2] = extent[4] = extent[6] = VTK_DOUBLE_MAX;
  259. extent[1] = extent[3] = extent[5] = extent[7] = VTK_DOUBLE_MIN;
  260. vtkChartXY* chart = this->chart();
  261. vtkAxis* axis = chart->GetAxis(vtkAxis::BOTTOM);
  262. extent[0] = qMin(axis->GetMinimum(), extent[0]);
  263. extent[1] = qMax(axis->GetMaximum(), extent[1]);
  264. axis = chart->GetAxis(vtkAxis::LEFT);
  265. extent[2] = qMin(axis->GetMinimum(), extent[2]);
  266. extent[3] = qMax(axis->GetMaximum(), extent[3]);
  267. axis = chart->GetAxis(vtkAxis::TOP);
  268. extent[4] = qMin(axis->GetMinimum(), extent[4]);
  269. extent[5] = qMax(axis->GetMaximum(), extent[5]);
  270. axis = chart->GetAxis(vtkAxis::RIGHT);
  271. extent[6] = qMin(axis->GetMinimum(), extent[6]);
  272. extent[7] = qMax(axis->GetMaximum(), extent[7]);
  273. }
  274. // ----------------------------------------------------------------------------
  275. void ctkVTKChartView::chartBounds(double* bounds)const
  276. {
  277. Q_D(const ctkVTKChartView);
  278. if (d->UserBounds[1] < d->UserBounds[0])
  279. {
  280. // Invalid user bounds, return the real chart bounds
  281. d->chartBounds(bounds);
  282. }
  283. else
  284. {
  285. this->chartUserBounds(bounds);
  286. }
  287. memcpy(d->OldBounds, bounds, 8 * sizeof(double));
  288. }
  289. // ----------------------------------------------------------------------------
  290. void ctkVTKChartView::setChartUserBounds(double* userBounds)
  291. {
  292. Q_D(ctkVTKChartView);
  293. for (int i= 0; i < 8; ++i)
  294. {
  295. d->UserBounds[i] = userBounds[i];
  296. }
  297. this->onChartUpdated();
  298. }
  299. // ----------------------------------------------------------------------------
  300. void ctkVTKChartView::chartUserBounds(double* bounds)const
  301. {
  302. Q_D(const ctkVTKChartView);
  303. for (int i= 0; i < 8; ++i)
  304. {
  305. bounds[i] = d->UserBounds[i];
  306. }
  307. }
  308. // ----------------------------------------------------------------------------
  309. void ctkVTKChartView::setAxesToChartBounds()
  310. {
  311. vtkChartXY* chart = this->chart();
  312. double bounds[8];
  313. this->chartBounds(bounds);
  314. for (int i = 0; i < chart->GetNumberOfAxes(); ++i)
  315. {
  316. if (bounds[2*i] != VTK_DOUBLE_MAX)
  317. {
  318. chart->GetAxis(i)->SetRange(bounds[2*i], bounds[2*i+1]);
  319. //chart->GetAxis(i)->SetBehavior(2);
  320. }
  321. }
  322. }
  323. // ----------------------------------------------------------------------------
  324. void ctkVTKChartView::boundAxesToChartBounds()
  325. {
  326. vtkChartXY* chart = this->chart();
  327. double bounds[8];
  328. this->chartBounds(bounds);
  329. for (int i = 0; i < chart->GetNumberOfAxes(); ++i)
  330. {
  331. if (bounds[2*i] != VTK_DOUBLE_MAX)
  332. {
  333. chart->GetAxis(i)->SetMinimumLimit(bounds[2*i]);
  334. chart->GetAxis(i)->SetMaximumLimit(bounds[2*i + 1]);
  335. }
  336. }
  337. }
  338. // ----------------------------------------------------------------------------
  339. void ctkVTKChartView::chartBoundsToPlotBounds(double bounds[8], double plotBounds[4])const
  340. {
  341. plotBounds[0] = bounds[vtkAxis::BOTTOM*2];
  342. plotBounds[1] = bounds[vtkAxis::BOTTOM*2 + 1];
  343. plotBounds[2] = bounds[vtkAxis::LEFT*2];
  344. plotBounds[3] = bounds[vtkAxis::LEFT*2+1];
  345. }
  346. // ----------------------------------------------------------------------------
  347. void ctkVTKChartView::mouseDoubleClickEvent(QMouseEvent* event)
  348. {
  349. if (event->button() == Qt::MidButton)
  350. {
  351. this->setAxesToChartBounds();
  352. }
  353. this->Superclass::mouseDoubleClickEvent(event);
  354. }