ctkVTKChartView.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 <vtkGenericOpenGLRenderWindow.h>
  28. #include <vtkOpenGLContextDevice2D.h>
  29. #include <vtkPlot.h>
  30. #include <vtkRenderWindow.h>
  31. //----------------------------------------------------------------------------
  32. static ctkLogger logger("org.commontk.visualization.vtk.widgets.ctkVTKChartView");
  33. //----------------------------------------------------------------------------
  34. class ctkVTKChartViewPrivate
  35. {
  36. Q_DECLARE_PUBLIC(ctkVTKChartView);
  37. protected:
  38. ctkVTKChartView* const q_ptr;
  39. public:
  40. ctkVTKChartViewPrivate(ctkVTKChartView& object);
  41. void init();
  42. void chartBounds(double* bounds)const;
  43. #if CTK_USE_QVTKOPENGLWIDGET
  44. vtkSmartPointer<vtkGenericOpenGLRenderWindow> RenderWindow;
  45. #endif
  46. vtkSmartPointer<vtkContextView> ContextView;
  47. vtkSmartPointer<vtkChartXY> Chart;
  48. double UserBounds[8];
  49. mutable double OldBounds[8];
  50. };
  51. // ----------------------------------------------------------------------------
  52. // ctkVTKChartViewPrivate methods
  53. // ----------------------------------------------------------------------------
  54. ctkVTKChartViewPrivate::ctkVTKChartViewPrivate(ctkVTKChartView& object)
  55. :q_ptr(&object)
  56. {
  57. this->ContextView = vtkSmartPointer<vtkContextView>::New();
  58. #if CTK_USE_QVTKOPENGLWIDGET
  59. this->RenderWindow = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();
  60. #endif
  61. this->Chart = vtkSmartPointer<vtkChartXY>::New();
  62. this->ContextView->GetScene()->AddItem(this->Chart);
  63. this->UserBounds[0] = this->UserBounds[2] = this->UserBounds[4] = this->UserBounds[6] = 0.;
  64. this->UserBounds[1] = this->UserBounds[3] = this->UserBounds[5] = this->UserBounds[7] = -1.;
  65. this->OldBounds[0] = this->OldBounds[2] = this->OldBounds[4] = this->OldBounds[6] = 0.;
  66. this->OldBounds[1] = this->OldBounds[3] = this->OldBounds[5] = this->OldBounds[7] = -1.;
  67. }
  68. // ----------------------------------------------------------------------------
  69. void ctkVTKChartViewPrivate::init()
  70. {
  71. Q_Q(ctkVTKChartView);
  72. #if CTK_USE_QVTKOPENGLWIDGET
  73. q->SetRenderWindow(this->RenderWindow);
  74. this->ContextView->SetRenderWindow(this->RenderWindow);
  75. #endif
  76. this->ContextView->SetInteractor(q->GetInteractor());
  77. q->SetRenderWindow(this->ContextView->GetRenderWindow());
  78. // low def for now (faster)
  79. //q->GetRenderWindow()->SetMultiSamples(0);
  80. //vtkOpenGLContextDevice2D::SafeDownCast(this->ContextView->GetContext()->GetDevice())
  81. // ->SetStringRendererToQt();
  82. #ifndef Q_WS_X11
  83. q->GetRenderWindow()->SetLineSmoothing(true);
  84. #endif
  85. this->Chart->SetActionToButton(vtkChart::PAN, vtkContextMouseEvent::MIDDLE_BUTTON);
  86. this->Chart->SetActionToButton(vtkChart::SELECT, vtkContextMouseEvent::RIGHT_BUTTON);
  87. q->qvtkConnect(q->chart()->GetAxis(vtkAxis::BOTTOM),vtkCommand::ModifiedEvent,
  88. q, SIGNAL(extentChanged()));
  89. q->qvtkConnect(q->chart()->GetAxis(vtkAxis::LEFT),vtkCommand::ModifiedEvent,
  90. q, SIGNAL(extentChanged()));
  91. }
  92. // ----------------------------------------------------------------------------
  93. void ctkVTKChartViewPrivate::chartBounds(double* bounds)const
  94. {
  95. if (!bounds)
  96. {
  97. return;
  98. }
  99. Q_Q(const ctkVTKChartView);
  100. bounds[0] = bounds[2] = bounds[4] = bounds[6] = VTK_DOUBLE_MAX;
  101. bounds[1] = bounds[3] = bounds[5] = bounds[7] = VTK_DOUBLE_MIN;
  102. vtkChartXY* chart = q->chart();
  103. const vtkIdType plotCount = chart->GetNumberOfPlots();
  104. for (vtkIdType i = 0; i < plotCount; ++i)
  105. {
  106. vtkPlot* plot = chart->GetPlot(i);
  107. int corner = chart->GetPlotCorner(plot);
  108. double plotBounds[4];
  109. plot->GetBounds(plotBounds);
  110. switch (corner)
  111. {
  112. // bottom left
  113. case 0:
  114. // x
  115. bounds[2] = bounds[2] > plotBounds[0] ? plotBounds[0] : bounds[2];
  116. bounds[3] = bounds[3] < plotBounds[1] ? plotBounds[1] : bounds[3];
  117. // y
  118. bounds[0] = bounds[0] > plotBounds[2] ? plotBounds[2] : bounds[0];
  119. bounds[1] = bounds[1] < plotBounds[3] ? plotBounds[3] : bounds[1];
  120. break;
  121. // bottom right
  122. case 1:
  123. // x
  124. bounds[2] = bounds[2] > plotBounds[0] ? plotBounds[0] : bounds[2];
  125. bounds[3] = bounds[3] < plotBounds[1] ? plotBounds[1] : bounds[3];
  126. // y
  127. bounds[4] = bounds[4] > plotBounds[2] ? plotBounds[2] : bounds[4];
  128. bounds[5] = bounds[5] < plotBounds[3] ? plotBounds[3] : bounds[5];
  129. break;
  130. // top right
  131. case 2:
  132. // x
  133. bounds[6] = bounds[6] > plotBounds[0] ? plotBounds[0] : bounds[6];
  134. bounds[7] = bounds[7] < plotBounds[1] ? plotBounds[1] : bounds[7];
  135. // y
  136. bounds[4] = bounds[4] > plotBounds[2] ? plotBounds[2] : bounds[4];
  137. bounds[5] = bounds[5] < plotBounds[3] ? plotBounds[3] : bounds[5];
  138. break;
  139. // top left
  140. case 3:
  141. // x
  142. bounds[6] = bounds[6] > plotBounds[0] ? plotBounds[0] : bounds[6];
  143. bounds[7] = bounds[7] < plotBounds[1] ? plotBounds[1] : bounds[7];
  144. // y
  145. bounds[0] = bounds[0] > plotBounds[2] ? plotBounds[2] : bounds[1];
  146. bounds[1] = bounds[0] < plotBounds[3] ? plotBounds[3] : bounds[1];
  147. break;
  148. }
  149. }
  150. }
  151. // ----------------------------------------------------------------------------
  152. // ctkVTKChartView methods
  153. // ----------------------------------------------------------------------------
  154. ctkVTKChartView::ctkVTKChartView(QWidget* parentWidget)
  155. :Superclass(parentWidget)
  156. , d_ptr(new ctkVTKChartViewPrivate(*this))
  157. {
  158. Q_D(ctkVTKChartView);
  159. d->init();
  160. //this->setAutomaticImageCacheEnabled(true);
  161. }
  162. // ----------------------------------------------------------------------------
  163. ctkVTKChartView::~ctkVTKChartView()
  164. {
  165. }
  166. // ----------------------------------------------------------------------------
  167. void ctkVTKChartView::setTitle(const QString& newTitle)
  168. {
  169. Q_D(ctkVTKChartView);
  170. d->Chart->SetTitle(newTitle.toLatin1().data());
  171. }
  172. // ----------------------------------------------------------------------------
  173. QString ctkVTKChartView::title()const
  174. {
  175. Q_D(const ctkVTKChartView);
  176. return QString(d->Chart->GetTitle());
  177. }
  178. // ----------------------------------------------------------------------------
  179. vtkChartXY* ctkVTKChartView::chart()const
  180. {
  181. Q_D(const ctkVTKChartView);
  182. return d->Chart;
  183. }
  184. // ----------------------------------------------------------------------------
  185. vtkContextScene* ctkVTKChartView::scene()const
  186. {
  187. Q_D(const ctkVTKChartView);
  188. return d->ContextView->GetScene();
  189. }
  190. // ----------------------------------------------------------------------------
  191. void ctkVTKChartView::addPlot(vtkPlot* plot)
  192. {
  193. Q_D(ctkVTKChartView);
  194. d->Chart->AddPlot(plot);
  195. emit this->plotAdded(plot);
  196. this->onChartUpdated();
  197. }
  198. // ----------------------------------------------------------------------------
  199. void ctkVTKChartView::removePlot(vtkPlot* plot)
  200. {
  201. Q_D(ctkVTKChartView);
  202. vtkIdType index = this->plotIndex(plot);
  203. if (index == vtkIdType(-1))
  204. {
  205. return;
  206. }
  207. d->Chart->RemovePlot(index);
  208. emit this->plotRemoved(plot);
  209. this->onChartUpdated();
  210. }
  211. // ----------------------------------------------------------------------------
  212. void ctkVTKChartView::removeAllPlots()
  213. {
  214. Q_D(ctkVTKChartView);
  215. while(d->Chart->GetNumberOfPlots() > 0)
  216. {
  217. this->removePlot(d->Chart->GetPlot(0));
  218. }
  219. }
  220. // ----------------------------------------------------------------------------
  221. vtkIdType ctkVTKChartView::plotIndex(vtkPlot* plot)
  222. {
  223. Q_D(ctkVTKChartView);
  224. // GetPlotIndex is missing from vtkChart API
  225. for (vtkIdType i = 0; i < d->Chart->GetNumberOfPlots(); ++i)
  226. {
  227. if (plot == d->Chart->GetPlot(i))
  228. {
  229. return i;
  230. }
  231. }
  232. return -1;
  233. }
  234. // ----------------------------------------------------------------------------
  235. void ctkVTKChartView::onChartUpdated()
  236. {
  237. Q_D(ctkVTKChartView);
  238. double oldBounds[8];
  239. memcpy(oldBounds, d->OldBounds, 8 * sizeof(double));
  240. double newBounds[8];
  241. this->chartBounds(newBounds);
  242. if (oldBounds[0] != newBounds[0] ||
  243. oldBounds[1] != newBounds[1] ||
  244. oldBounds[2] != newBounds[2] ||
  245. oldBounds[3] != newBounds[3] ||
  246. oldBounds[4] != newBounds[4] ||
  247. oldBounds[5] != newBounds[5] ||
  248. oldBounds[6] != newBounds[6] ||
  249. oldBounds[7] != newBounds[7])
  250. {
  251. emit boundsChanged();
  252. }
  253. }
  254. // ----------------------------------------------------------------------------
  255. void ctkVTKChartView::chartExtent(double* extent)const
  256. {
  257. if (!extent)
  258. {
  259. return;
  260. }
  261. extent[0] = extent[2] = extent[4] = extent[6] = VTK_DOUBLE_MAX;
  262. extent[1] = extent[3] = extent[5] = extent[7] = VTK_DOUBLE_MIN;
  263. vtkChartXY* chart = this->chart();
  264. vtkAxis* axis = chart->GetAxis(vtkAxis::BOTTOM);
  265. extent[0] = qMin(axis->GetMinimum(), extent[0]);
  266. extent[1] = qMax(axis->GetMaximum(), extent[1]);
  267. axis = chart->GetAxis(vtkAxis::LEFT);
  268. extent[2] = qMin(axis->GetMinimum(), extent[2]);
  269. extent[3] = qMax(axis->GetMaximum(), extent[3]);
  270. axis = chart->GetAxis(vtkAxis::TOP);
  271. extent[4] = qMin(axis->GetMinimum(), extent[4]);
  272. extent[5] = qMax(axis->GetMaximum(), extent[5]);
  273. axis = chart->GetAxis(vtkAxis::RIGHT);
  274. extent[6] = qMin(axis->GetMinimum(), extent[6]);
  275. extent[7] = qMax(axis->GetMaximum(), extent[7]);
  276. }
  277. // ----------------------------------------------------------------------------
  278. void ctkVTKChartView::setChartUserExtent(double* userExtent)
  279. {
  280. if (!userExtent)
  281. {
  282. qCritical() << Q_FUNC_INFO << ": Invalid user extent";
  283. return;
  284. }
  285. vtkChartXY* chart = this->chart();
  286. vtkAxis* axis = chart->GetAxis(vtkAxis::BOTTOM);
  287. axis->SetRange(userExtent[0], userExtent[1]);
  288. axis = chart->GetAxis(vtkAxis::LEFT);
  289. axis->SetRange(userExtent[2], userExtent[3]);
  290. axis = chart->GetAxis(vtkAxis::TOP);
  291. axis->SetRange(userExtent[4], userExtent[5]);
  292. axis = chart->GetAxis(vtkAxis::RIGHT);
  293. axis->SetRange(userExtent[6], userExtent[7]);
  294. }
  295. // ----------------------------------------------------------------------------
  296. void ctkVTKChartView::chartBounds(double* bounds)const
  297. {
  298. Q_D(const ctkVTKChartView);
  299. if (d->UserBounds[1] < d->UserBounds[0])
  300. {
  301. // Invalid user bounds, return the real chart bounds
  302. d->chartBounds(bounds);
  303. }
  304. else
  305. {
  306. this->chartUserBounds(bounds);
  307. }
  308. memcpy(d->OldBounds, bounds, 8 * sizeof(double));
  309. }
  310. // ----------------------------------------------------------------------------
  311. void ctkVTKChartView::setChartUserBounds(double* userBounds)
  312. {
  313. Q_D(ctkVTKChartView);
  314. for (int i= 0; i < 8; ++i)
  315. {
  316. d->UserBounds[i] = userBounds[i];
  317. }
  318. this->onChartUpdated();
  319. }
  320. // ----------------------------------------------------------------------------
  321. void ctkVTKChartView::chartUserBounds(double* bounds)const
  322. {
  323. Q_D(const ctkVTKChartView);
  324. for (int i= 0; i < 8; ++i)
  325. {
  326. bounds[i] = d->UserBounds[i];
  327. }
  328. }
  329. // ----------------------------------------------------------------------------
  330. void ctkVTKChartView::setAxesToChartBounds()
  331. {
  332. vtkChartXY* chart = this->chart();
  333. double bounds[8];
  334. this->chartBounds(bounds);
  335. for (int i = 0; i < chart->GetNumberOfAxes(); ++i)
  336. {
  337. if (bounds[2*i] != VTK_DOUBLE_MAX)
  338. {
  339. chart->GetAxis(i)->SetRange(bounds[2*i], bounds[2*i+1]);
  340. //chart->GetAxis(i)->SetBehavior(2);
  341. }
  342. }
  343. }
  344. // ----------------------------------------------------------------------------
  345. void ctkVTKChartView::boundAxesToChartBounds()
  346. {
  347. vtkChartXY* chart = this->chart();
  348. double bounds[8];
  349. this->chartBounds(bounds);
  350. for (int i = 0; i < chart->GetNumberOfAxes(); ++i)
  351. {
  352. if (bounds[2*i] != VTK_DOUBLE_MAX)
  353. {
  354. chart->GetAxis(i)->SetMinimumLimit(bounds[2*i]);
  355. chart->GetAxis(i)->SetMaximumLimit(bounds[2*i + 1]);
  356. }
  357. }
  358. }
  359. // ----------------------------------------------------------------------------
  360. void ctkVTKChartView::chartBoundsToPlotBounds(double bounds[8], double plotBounds[4])const
  361. {
  362. plotBounds[0] = bounds[vtkAxis::BOTTOM*2];
  363. plotBounds[1] = bounds[vtkAxis::BOTTOM*2 + 1];
  364. plotBounds[2] = bounds[vtkAxis::LEFT*2];
  365. plotBounds[3] = bounds[vtkAxis::LEFT*2+1];
  366. }
  367. // ----------------------------------------------------------------------------
  368. void ctkVTKChartView::mouseDoubleClickEvent(QMouseEvent* event)
  369. {
  370. if (event->button() == Qt::MidButton)
  371. {
  372. this->setAxesToChartBounds();
  373. }
  374. this->Superclass::mouseDoubleClickEvent(event);
  375. }