ctkQImageViewerWidget.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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. #include <iostream>
  15. // CTK includes
  16. #include "ctkQImageViewerWidget.h"
  17. // Qt includes
  18. #include <QLabel>
  19. #include <QHBoxLayout>
  20. #include <QDebug>
  21. #include <QResizeEvent>
  22. #include <QMouseEvent>
  23. #include <QKeyEvent>
  24. #include <QPainter>
  25. //--------------------------------------------------------------------------
  26. class ctkQImageViewerWidgetPrivate
  27. {
  28. Q_DECLARE_PUBLIC( ctkQImageViewerWidget );
  29. protected:
  30. ctkQImageViewerWidget* const q_ptr;
  31. public:
  32. ctkQImageViewerWidgetPrivate( ctkQImageViewerWidget& object );
  33. void init();
  34. QLabel * Window;
  35. double Zoom;
  36. double PositionX;
  37. double PositionY;
  38. double CenterX;
  39. double CenterY;
  40. int SliceNumber;
  41. double IntensityWindowMin;
  42. double IntensityWindowMax;
  43. bool FlipXAxis;
  44. bool FlipYAxis;
  45. bool TransposeXY;
  46. QList< const QImage * > ImageList;
  47. QPixmap TmpImage;
  48. int TmpXMin;
  49. int TmpXMax;
  50. int TmpYMin;
  51. int TmpYMax;
  52. int MouseLastX;
  53. int MouseLastY;
  54. double MouseLastZoom;
  55. double MouseLastIntensityWindowMin;
  56. double MouseLastIntensityWindowMax;
  57. bool MouseLeftDragging;
  58. bool MouseMiddleDragging;
  59. bool MouseRightDragging;
  60. double clamp( double x, double xMin, double xMax );
  61. void fitImageRectangle( double x0, double y0, double x1, double y1 );
  62. };
  63. //--------------------------------------------------------------------------
  64. ctkQImageViewerWidgetPrivate::ctkQImageViewerWidgetPrivate(
  65. ctkQImageViewerWidget& object )
  66. : q_ptr( &object )
  67. {
  68. this->Window = new QLabel();
  69. }
  70. //--------------------------------------------------------------------------
  71. void ctkQImageViewerWidgetPrivate::init()
  72. {
  73. Q_Q( ctkQImageViewerWidget );
  74. this->Window->setParent(q);
  75. QHBoxLayout* layout = new QHBoxLayout(q);
  76. layout->addWidget(this->Window);
  77. layout->setContentsMargins(0,0,0,0);
  78. q->setLayout(layout);
  79. // Set parameters for the view
  80. this->Zoom = 1;
  81. this->PositionX = 0;
  82. this->PositionY = 0;
  83. this->SliceNumber = 0;
  84. this->CenterX = 0;
  85. this->CenterY = 0;
  86. this->IntensityWindowMin = 0;
  87. this->IntensityWindowMax = 0;
  88. this->FlipXAxis = false;
  89. this->FlipYAxis = true;
  90. this->TransposeXY = false;
  91. this->ImageList.clear();
  92. this->TmpXMin = 0;
  93. this->TmpXMax = 0;
  94. this->TmpYMin = 0;
  95. this->TmpYMax = 0;
  96. this->MouseLastX = 0;
  97. this->MouseLastY = 0;
  98. this->MouseLastZoom = 0;
  99. this->MouseLeftDragging = false;
  100. this->MouseMiddleDragging = false;
  101. this->MouseRightDragging = false;
  102. // Don't expand for no reason
  103. q->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
  104. }
  105. //--------------------------------------------------------------------------
  106. double ctkQImageViewerWidgetPrivate::clamp( double x, double xMin,
  107. double xMax )
  108. {
  109. if( x < xMin )
  110. {
  111. return xMin;
  112. }
  113. if( x > xMax )
  114. {
  115. return xMax;
  116. }
  117. return x;
  118. }
  119. //--------------------------------------------------------------------------
  120. void ctkQImageViewerWidgetPrivate::fitImageRectangle( double x0,
  121. double x1, double y0, double y1 )
  122. {
  123. if( this->SliceNumber >= 0 && this->SliceNumber < this->ImageList.size() )
  124. {
  125. this->TmpXMin = this->clamp( x0, 0,
  126. this->ImageList[ this->SliceNumber ]->width() );
  127. this->TmpXMax = this->clamp( x1, this->TmpXMin,
  128. this->ImageList[ this->SliceNumber ]->width() );
  129. this->TmpYMin = this->clamp( y0, 0,
  130. this->ImageList[ this->SliceNumber ]->height() );
  131. this->TmpYMax = this->clamp( y1, this->TmpYMin,
  132. this->ImageList[ this->SliceNumber ]->height() );
  133. this->CenterX = ( this->TmpXMax + this->TmpXMin ) / 2.0;
  134. this->CenterY = ( this->TmpYMax + this->TmpYMin ) / 2.0;
  135. }
  136. }
  137. // -------------------------------------------------------------------------
  138. ctkQImageViewerWidget::ctkQImageViewerWidget( QWidget* _parent )
  139. : Superclass( _parent ),
  140. d_ptr( new ctkQImageViewerWidgetPrivate( *this ) )
  141. {
  142. Q_D( ctkQImageViewerWidget );
  143. d->init();
  144. d->TmpXMax = this->width();
  145. d->TmpYMax = this->height();
  146. }
  147. // -------------------------------------------------------------------------
  148. ctkQImageViewerWidget::ctkQImageViewerWidget(
  149. ctkQImageViewerWidgetPrivate& pvt,
  150. QWidget* _parent)
  151. : Superclass(_parent), d_ptr(&pvt)
  152. {
  153. Q_D(ctkQImageViewerWidget);
  154. d->init();
  155. }
  156. // -------------------------------------------------------------------------
  157. ctkQImageViewerWidget::~ctkQImageViewerWidget()
  158. {
  159. }
  160. // -------------------------------------------------------------------------
  161. void ctkQImageViewerWidget::addImage( const QImage * image )
  162. {
  163. Q_D( ctkQImageViewerWidget );
  164. d->ImageList.push_back( image );
  165. d->TmpXMin = 0;
  166. d->TmpXMax = image->width();
  167. d->TmpYMin = 0;
  168. d->TmpYMax = image->height();
  169. this->update( true, false );
  170. this->setCenter( image->width()/2.0, image->height()/2.0 );
  171. }
  172. // -------------------------------------------------------------------------
  173. void ctkQImageViewerWidget::clearImages( void )
  174. {
  175. Q_D( ctkQImageViewerWidget );
  176. d->ImageList.clear();
  177. this->update( true, true );
  178. }
  179. // -------------------------------------------------------------------------
  180. double ctkQImageViewerWidget::xSpacing( void )
  181. {
  182. Q_D( ctkQImageViewerWidget );
  183. if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size() )
  184. {
  185. return( 1000.0 / d->ImageList[ d->SliceNumber ]->dotsPerMeterX() );
  186. }
  187. else
  188. {
  189. return 1;
  190. }
  191. }
  192. // -------------------------------------------------------------------------
  193. double ctkQImageViewerWidget::ySpacing( void )
  194. {
  195. Q_D( ctkQImageViewerWidget );
  196. if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size() )
  197. {
  198. return( 1000.0 / d->ImageList[ d->SliceNumber ]->dotsPerMeterY() );
  199. }
  200. else
  201. {
  202. return 1;
  203. }
  204. }
  205. // -------------------------------------------------------------------------
  206. double ctkQImageViewerWidget::sliceThickness( void )
  207. {
  208. return 1;
  209. }
  210. // -------------------------------------------------------------------------
  211. double ctkQImageViewerWidget::xPosition( void )
  212. {
  213. Q_D( ctkQImageViewerWidget );
  214. return d->PositionX;
  215. }
  216. // -------------------------------------------------------------------------
  217. double ctkQImageViewerWidget::yPosition( void )
  218. {
  219. Q_D( ctkQImageViewerWidget );
  220. return d->PositionY;
  221. }
  222. // -------------------------------------------------------------------------
  223. double ctkQImageViewerWidget::slicePosition( void )
  224. {
  225. Q_D( ctkQImageViewerWidget );
  226. return d->SliceNumber;
  227. }
  228. // -------------------------------------------------------------------------
  229. double ctkQImageViewerWidget::positionValue( void )
  230. {
  231. Q_D( ctkQImageViewerWidget );
  232. if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size() )
  233. {
  234. QColor vc( d->ImageList[ d->SliceNumber ]->pixel( d->PositionX,
  235. d->PositionY ) );
  236. return vc.value();
  237. }
  238. return 0;
  239. }
  240. // -------------------------------------------------------------------------
  241. double ctkQImageViewerWidget::xCenter( void )
  242. {
  243. Q_D( ctkQImageViewerWidget );
  244. return d->CenterX;
  245. }
  246. // -------------------------------------------------------------------------
  247. double ctkQImageViewerWidget::yCenter( void )
  248. {
  249. Q_D( ctkQImageViewerWidget );
  250. return d->CenterY;
  251. }
  252. // -------------------------------------------------------------------------
  253. void ctkQImageViewerWidget::setSliceNumber( int slicenum )
  254. {
  255. Q_D( ctkQImageViewerWidget );
  256. if( slicenum < d->ImageList.size() && slicenum != d->SliceNumber )
  257. {
  258. d->SliceNumber = slicenum;
  259. emit this->sliceNumberChanged( slicenum );
  260. emit this->xSpacingChanged( this->xSpacing() );
  261. emit this->ySpacingChanged( this->ySpacing() );
  262. emit this->sliceThicknessChanged( this->sliceThickness() );
  263. emit this->slicePositionChanged( this->slicePosition() );
  264. this->update( false, false );
  265. }
  266. }
  267. //
  268. // -------------------------------------------------------------------------
  269. int ctkQImageViewerWidget::sliceNumber( void ) const
  270. {
  271. Q_D( const ctkQImageViewerWidget );
  272. if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size() )
  273. {
  274. return d->SliceNumber;
  275. }
  276. else
  277. {
  278. return -1;
  279. }
  280. }
  281. // -------------------------------------------------------------------------
  282. void ctkQImageViewerWidget::setIntensityWindow( double iwMin, double iwMax )
  283. {
  284. Q_D( ctkQImageViewerWidget );
  285. if( iwMin != d->IntensityWindowMin )
  286. {
  287. d->IntensityWindowMin = iwMin;
  288. d->IntensityWindowMax = iwMax;
  289. emit this->intensityWindowMinChanged( iwMin );
  290. emit this->intensityWindowMaxChanged( iwMax );
  291. this->update( false, false );
  292. }
  293. }
  294. // -------------------------------------------------------------------------
  295. double ctkQImageViewerWidget::intensityWindowMin( void ) const
  296. {
  297. Q_D( const ctkQImageViewerWidget );
  298. return d->IntensityWindowMin;
  299. }
  300. // -------------------------------------------------------------------------
  301. double ctkQImageViewerWidget::intensityWindowMax( void ) const
  302. {
  303. Q_D( const ctkQImageViewerWidget );
  304. return d->IntensityWindowMax;
  305. }
  306. // -------------------------------------------------------------------------
  307. void ctkQImageViewerWidget::setFlipXAxis( bool flip )
  308. {
  309. Q_D( ctkQImageViewerWidget );
  310. if( flip != d->FlipXAxis )
  311. {
  312. d->FlipXAxis = flip;
  313. emit this->flipXAxisChanged( flip );
  314. this->update( false, false );
  315. }
  316. }
  317. // -------------------------------------------------------------------------
  318. bool ctkQImageViewerWidget::flipXAxis( void ) const
  319. {
  320. Q_D( const ctkQImageViewerWidget );
  321. return d->FlipXAxis;
  322. }
  323. // -------------------------------------------------------------------------
  324. void ctkQImageViewerWidget::setFlipYAxis( bool flip )
  325. {
  326. Q_D( ctkQImageViewerWidget );
  327. if( flip != d->FlipYAxis )
  328. {
  329. d->FlipYAxis = flip;
  330. emit this->flipYAxisChanged( flip );
  331. this->update( false, false );
  332. }
  333. }
  334. // -------------------------------------------------------------------------
  335. bool ctkQImageViewerWidget::flipYAxis( void ) const
  336. {
  337. Q_D( const ctkQImageViewerWidget );
  338. return d->FlipYAxis;
  339. }
  340. // -------------------------------------------------------------------------
  341. void ctkQImageViewerWidget::setTransposeXY( bool transpose )
  342. {
  343. Q_D( ctkQImageViewerWidget );
  344. if( transpose != d->TransposeXY )
  345. {
  346. d->TransposeXY = transpose;
  347. emit this->transposeXYChanged( transpose );
  348. this->update( false, false );
  349. }
  350. }
  351. // -------------------------------------------------------------------------
  352. bool ctkQImageViewerWidget::transposeXY( void ) const
  353. {
  354. Q_D( const ctkQImageViewerWidget );
  355. return d->TransposeXY;
  356. }
  357. // -------------------------------------------------------------------------
  358. void ctkQImageViewerWidget::setCenter( double x, double y )
  359. {
  360. Q_D( ctkQImageViewerWidget );
  361. if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size() )
  362. {
  363. int tmpXRange = d->TmpXMax - d->TmpXMin;
  364. if( tmpXRange > d->ImageList[ d->SliceNumber ]->width() )
  365. {
  366. tmpXRange = d->ImageList[ d->SliceNumber ]->width();
  367. }
  368. int tmpYRange = d->TmpYMax - d->TmpYMin;
  369. if( tmpYRange > d->ImageList[ d->SliceNumber ]->height() )
  370. {
  371. tmpYRange = d->ImageList[ d->SliceNumber ]->height();
  372. }
  373. int xMin2 = static_cast<int>(x) - tmpXRange/2.0;
  374. if( xMin2 < 0 )
  375. {
  376. xMin2 = 0;
  377. }
  378. int xMax2 = xMin2 + tmpXRange;
  379. if( xMax2 > d->ImageList[ d->SliceNumber ]->width() )
  380. {
  381. xMax2 = d->ImageList[ d->SliceNumber ]->width();
  382. xMin2 = xMax2 - tmpXRange;
  383. }
  384. int yMin2 = static_cast<int>(y) - tmpYRange/2.0;
  385. if( yMin2 < 0 )
  386. {
  387. yMin2 = 0;
  388. }
  389. int yMax2 = yMin2 + tmpYRange;
  390. if( yMax2 > d->ImageList[ d->SliceNumber ]->height() )
  391. {
  392. yMax2 = d->ImageList[ d->SliceNumber ]->height();
  393. yMin2 = yMax2 - tmpYRange;
  394. }
  395. d->fitImageRectangle( xMin2, xMax2, yMin2, yMax2 );
  396. emit this->xCenterChanged( x );
  397. emit this->yCenterChanged( y );
  398. this->update( false, false );
  399. }
  400. }
  401. // -------------------------------------------------------------------------
  402. void ctkQImageViewerWidget::setPosition( double x, double y )
  403. {
  404. Q_D( ctkQImageViewerWidget );
  405. if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size() )
  406. {
  407. d->PositionX = x;
  408. d->PositionY = y;
  409. emit this->xPositionChanged( x );
  410. emit this->yPositionChanged( y );
  411. emit this->positionValueChanged( this->positionValue() );
  412. this->update( false, false );
  413. }
  414. }
  415. // -------------------------------------------------------------------------
  416. double ctkQImageViewerWidget::zoom( void )
  417. {
  418. Q_D( ctkQImageViewerWidget );
  419. if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size() )
  420. {
  421. return d->Zoom;
  422. }
  423. return 1;
  424. }
  425. // -------------------------------------------------------------------------
  426. void ctkQImageViewerWidget::setZoom( double factor )
  427. {
  428. Q_D( ctkQImageViewerWidget );
  429. if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size() )
  430. {
  431. const QImage * img = d->ImageList[ d->SliceNumber ];
  432. if( factor < 2.0 / img->width() )
  433. {
  434. factor = 2.0 / img->width();
  435. }
  436. if( factor > img->width()/2.0 )
  437. {
  438. factor = img->width()/2.0;
  439. }
  440. d->Zoom = factor;
  441. double cx = ( d->TmpXMin + d->TmpXMax ) / 2.0;
  442. double cy = ( d->TmpYMin + d->TmpYMax ) / 2.0;
  443. double x2 = img->width() / factor;
  444. double y2 = img->height() / factor;
  445. //double x2 = ( d->TmpXMax - d->TmpXMin ) / 2.0;
  446. //double y2 = ( d->TmpYMax - d->TmpYMin ) / 2.0;
  447. int xMin2 = static_cast<int>(cx) - x2 / 2.0;
  448. if( xMin2 < 0 )
  449. {
  450. xMin2 = 0;
  451. }
  452. int xMax2 = xMin2 + x2;
  453. if( xMax2 > d->ImageList[ d->SliceNumber ]->width() )
  454. {
  455. xMax2 = d->ImageList[ d->SliceNumber ]->width();
  456. xMin2 = xMax2 - x2;
  457. }
  458. int yMin2 = static_cast<int>(cy) - y2 / 2.0;
  459. if( yMin2 < 0 )
  460. {
  461. yMin2 = 0;
  462. }
  463. int yMax2 = yMin2 + y2;
  464. if( yMax2 > d->ImageList[ d->SliceNumber ]->height() )
  465. {
  466. yMax2 = d->ImageList[ d->SliceNumber ]->height();
  467. yMin2 = yMax2 - y2;
  468. }
  469. d->fitImageRectangle( xMin2, xMax2, yMin2, yMax2 );
  470. this->update( true, true );
  471. }
  472. }
  473. // -------------------------------------------------------------------------
  474. void ctkQImageViewerWidget::reset( )
  475. {
  476. Q_D( ctkQImageViewerWidget );
  477. if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size() )
  478. {
  479. d->fitImageRectangle( 0, 0, d->ImageList[ d->SliceNumber ]->width(),
  480. d->ImageList[ d->SliceNumber ]->height() );
  481. }
  482. }
  483. // -------------------------------------------------------------------------
  484. void ctkQImageViewerWidget::keyPressEvent( QKeyEvent * event )
  485. {
  486. event->ignore();
  487. }
  488. // -------------------------------------------------------------------------
  489. void ctkQImageViewerWidget::mousePressEvent( QMouseEvent * event )
  490. {
  491. Q_D( ctkQImageViewerWidget );
  492. if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size() )
  493. {
  494. switch( event->button() )
  495. {
  496. case Qt::LeftButton:
  497. {
  498. d->MouseLeftDragging = true;
  499. d->MouseLastX = event->x();
  500. d->MouseLastY = event->y();
  501. d->MouseLastIntensityWindowMin = this->intensityWindowMin();
  502. d->MouseLastIntensityWindowMax = this->intensityWindowMax();
  503. break;
  504. }
  505. case Qt::MidButton:
  506. {
  507. d->MouseMiddleDragging = true;
  508. d->MouseLastX = event->x();
  509. d->MouseLastY = event->y();
  510. d->MouseLastZoom = this->zoom();
  511. break;
  512. }
  513. case Qt::RightButton:
  514. {
  515. d->MouseRightDragging = true;
  516. double relativeX = static_cast<double>( event->x() )
  517. / this->width();
  518. double relativeY = static_cast<double>( event->y() )
  519. / this->height();
  520. double x = (d->TmpXMax - d->TmpXMin) * relativeX + d->TmpXMin;
  521. double y = (d->TmpYMax - d->TmpYMin) * relativeY + d->TmpYMin;
  522. this->setCenter( x, y );
  523. break;
  524. }
  525. default:
  526. {
  527. event->ignore();
  528. }
  529. };
  530. }
  531. }
  532. // -------------------------------------------------------------------------
  533. void ctkQImageViewerWidget::mouseReleaseEvent( QMouseEvent * event )
  534. {
  535. Q_D( ctkQImageViewerWidget );
  536. d->MouseLeftDragging = false;
  537. d->MouseMiddleDragging = false;
  538. d->MouseRightDragging = false;
  539. event->ignore();
  540. }
  541. // -------------------------------------------------------------------------
  542. void ctkQImageViewerWidget::mouseMoveEvent( QMouseEvent * event )
  543. {
  544. Q_D( ctkQImageViewerWidget );
  545. if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size() )
  546. {
  547. if( d->MouseLeftDragging )
  548. {
  549. double distX = d->MouseLastX - event->x();
  550. double distY = d->MouseLastY - event->y();
  551. double deltaMin = ( distX / this->height() );
  552. if( deltaMin < 0 )
  553. {
  554. // Heuristic to make shrinking propotional to enlarging
  555. deltaMin *= -deltaMin;
  556. }
  557. double deltaMax = ( distY / this->width() );
  558. if( deltaMax < 0 )
  559. {
  560. // Heuristic to make shrinking propotional to enlarging
  561. deltaMax *= -deltaMax;
  562. }
  563. double iRange = d->MouseLastIntensityWindowMax
  564. - d->MouseLastIntensityWindowMin;
  565. deltaMin *= iRange;
  566. deltaMax *= iRange;
  567. double newMin = d->MouseLastIntensityWindowMin + deltaMin;
  568. double newMax = d->MouseLastIntensityWindowMax + deltaMax;
  569. this->setIntensityWindow( newMin, newMax );
  570. }
  571. else if( d->MouseMiddleDragging )
  572. {
  573. double distY = d->MouseLastY - event->y();
  574. double deltaZ = (distY / this->height());
  575. if( deltaZ < 0 )
  576. {
  577. // Heuristic to make shrinking propotional to enlarging
  578. deltaZ *= -deltaZ;
  579. }
  580. double newZoom = d->MouseLastZoom + deltaZ;
  581. this->setZoom( newZoom );
  582. }
  583. else
  584. {
  585. double relativeX = static_cast<double>( event->x() )
  586. / this->width();
  587. double relativeY = static_cast<double>( event->y() )
  588. / this->height();
  589. double x = (d->TmpXMax - d->TmpXMin) * relativeX + d->TmpXMin;
  590. double y = (d->TmpYMax - d->TmpYMin) * relativeY + d->TmpYMin;
  591. this->setPosition( x, y );
  592. }
  593. }
  594. }
  595. // -------------------------------------------------------------------------
  596. void ctkQImageViewerWidget::resizeEvent( QResizeEvent* event )
  597. {
  598. this->Superclass::resizeEvent( event );
  599. this->update( false, true );
  600. }
  601. // -------------------------------------------------------------------------
  602. void ctkQImageViewerWidget::update( bool zoomChanged,
  603. bool sizeChanged )
  604. {
  605. std::cout << "Updating.." << std::endl;
  606. Q_D( ctkQImageViewerWidget );
  607. if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size() )
  608. {
  609. const QImage * img = d->ImageList[ d->SliceNumber ];
  610. if( zoomChanged || sizeChanged )
  611. {
  612. std::cout << "Update: Changed" << std::endl;
  613. if( this->width() > 0 && this->height() > 0
  614. && d->TmpXMax > d->TmpXMin && d->TmpYMax > d->TmpYMin)
  615. {
  616. int tmpXRange = d->TmpXMax - d->TmpXMin;
  617. int tmpYRange = d->TmpYMax - d->TmpYMin;
  618. double tmpAspectRatio = static_cast<double>(tmpYRange) / tmpXRange;
  619. double screenAspectRatio = static_cast<double>(this->height())
  620. / this->width();
  621. if( screenAspectRatio > tmpAspectRatio )
  622. {
  623. int extraTmpYAbove = d->TmpYMin;
  624. int extraTmpYBelow = img->height() - d->TmpYMax;
  625. int extraTmpYNeeded = tmpXRange * screenAspectRatio
  626. - tmpYRange;
  627. int minExtra = extraTmpYAbove;
  628. if( extraTmpYBelow < minExtra )
  629. {
  630. minExtra = extraTmpYBelow;
  631. }
  632. if(2 * minExtra >= extraTmpYNeeded)
  633. {
  634. int minNeeded = extraTmpYNeeded / 2.0;
  635. int maxNeeded = extraTmpYNeeded - minNeeded;
  636. d->TmpYMin -= minNeeded;
  637. d->TmpYMax += maxNeeded;
  638. }
  639. else if(extraTmpYAbove + extraTmpYBelow >= extraTmpYNeeded)
  640. {
  641. if(extraTmpYAbove < extraTmpYBelow)
  642. {
  643. d->TmpYMin = 0;
  644. d->TmpYMax += extraTmpYNeeded - extraTmpYAbove;
  645. }
  646. else
  647. {
  648. d->TmpYMax = img->height();
  649. d->TmpYMin -= extraTmpYNeeded - extraTmpYBelow;
  650. }
  651. }
  652. else
  653. {
  654. d->TmpYMin = 0;
  655. d->TmpYMax = img->height();
  656. }
  657. d->TmpImage = QPixmap( this->width(),
  658. static_cast<unsigned int>(
  659. static_cast<double>(d->TmpYMax - d->TmpYMin)
  660. / (d->TmpXMax - d->TmpXMin)
  661. * this->width() + 0.5 ) );
  662. }
  663. else if(screenAspectRatio < tmpAspectRatio)
  664. {
  665. int extraTmpXLeft = d->TmpXMin;
  666. int extraTmpXRight = img->width() - d->TmpXMax;
  667. int extraTmpXNeeded = static_cast<double>(tmpYRange)
  668. / screenAspectRatio - tmpXRange;
  669. int minExtra = extraTmpXLeft;
  670. if( extraTmpXRight < minExtra )
  671. {
  672. minExtra = extraTmpXRight;
  673. }
  674. if(2 * minExtra >= extraTmpXNeeded)
  675. {
  676. int minNeeded = extraTmpXNeeded / 2.0;
  677. int maxNeeded = extraTmpXNeeded - minNeeded;
  678. d->TmpXMin -= minNeeded;
  679. d->TmpXMax += maxNeeded;
  680. }
  681. else if(extraTmpXLeft + extraTmpXRight >= extraTmpXNeeded)
  682. {
  683. if(extraTmpXLeft < extraTmpXRight)
  684. {
  685. d->TmpXMin = 0;
  686. d->TmpXMax += extraTmpXNeeded - extraTmpXLeft;
  687. }
  688. else
  689. {
  690. d->TmpXMax = img->width();
  691. d->TmpXMin -= extraTmpXNeeded - extraTmpXRight;
  692. }
  693. }
  694. else
  695. {
  696. d->TmpXMin = 0;
  697. d->TmpXMax = img->width();
  698. }
  699. d->TmpImage = QPixmap( static_cast<unsigned int>( this->height()
  700. / ( static_cast<double>(d->TmpYMax - d->TmpYMin)
  701. / (d->TmpXMax - d->TmpXMin) )
  702. + 0.5 ), this->height() );
  703. }
  704. else
  705. {
  706. d->TmpImage = QPixmap( this->width(), this->height() );
  707. }
  708. }
  709. }
  710. if( d->TmpImage.width() > 0 && d->TmpImage.height() > 0)
  711. {
  712. QPainter painter( &(d->TmpImage) );
  713. painter.drawPixmap( 0, 0, d->TmpImage.width(), d->TmpImage.height(),
  714. QPixmap::fromImage(*img), d->TmpXMin, d->TmpYMin,
  715. d->TmpXMax - d->TmpXMin, d->TmpYMax - d->TmpYMin);
  716. }
  717. d->Window->setPixmap( d->TmpImage );
  718. }
  719. else
  720. {
  721. d->Window->setText( "No Image Loaded." );
  722. }
  723. }