ctkQImageView.cpp 23 KB

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