ctkQImageView.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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 <QLabel>
  19. #include <QHBoxLayout>
  20. #include <QDebug>
  21. #include <QResizeEvent>
  22. #include <QMouseEvent>
  23. #include <QKeyEvent>
  24. #include <QPainter>
  25. //--------------------------------------------------------------------------
  26. class ctkQImageViewPrivate
  27. {
  28. Q_DECLARE_PUBLIC( ctkQImageView );
  29. protected:
  30. ctkQImageView* const q_ptr;
  31. public:
  32. ctkQImageViewPrivate( ctkQImageView& 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< 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. ctkQImageViewPrivate::ctkQImageViewPrivate(
  65. ctkQImageView& object )
  66. : q_ptr( &object )
  67. {
  68. this->Window = new QLabel();
  69. }
  70. //--------------------------------------------------------------------------
  71. void ctkQImageViewPrivate::init()
  72. {
  73. Q_Q( ctkQImageView );
  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 ctkQImageViewPrivate::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 ctkQImageViewPrivate::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. ctkQImageView::ctkQImageView( QWidget* _parent )
  139. : Superclass( _parent ),
  140. d_ptr( new ctkQImageViewPrivate( *this ) )
  141. {
  142. Q_D( ctkQImageView );
  143. d->init();
  144. d->TmpXMax = this->width();
  145. d->TmpYMax = this->height();
  146. }
  147. // -------------------------------------------------------------------------
  148. ctkQImageView::ctkQImageView(
  149. ctkQImageViewPrivate& pvt,
  150. QWidget* _parent)
  151. : Superclass(_parent), d_ptr(&pvt)
  152. {
  153. Q_D(ctkQImageView);
  154. d->init();
  155. }
  156. // -------------------------------------------------------------------------
  157. ctkQImageView::~ctkQImageView()
  158. {
  159. }
  160. // -------------------------------------------------------------------------
  161. void ctkQImageView::addImage( const QImage & image )
  162. {
  163. Q_D( ctkQImageView );
  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 ctkQImageView::clearImages( void )
  174. {
  175. Q_D( ctkQImageView );
  176. d->ImageList.clear();
  177. this->update( true, true );
  178. }
  179. // -------------------------------------------------------------------------
  180. double ctkQImageView::xSpacing( void )
  181. {
  182. Q_D( ctkQImageView );
  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 ctkQImageView::ySpacing( void )
  194. {
  195. Q_D( ctkQImageView );
  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 ctkQImageView::sliceThickness( void )
  207. {
  208. return 1;
  209. }
  210. // -------------------------------------------------------------------------
  211. double ctkQImageView::xPosition( void )
  212. {
  213. Q_D( ctkQImageView );
  214. return d->PositionX;
  215. }
  216. // -------------------------------------------------------------------------
  217. double ctkQImageView::yPosition( void )
  218. {
  219. Q_D( ctkQImageView );
  220. return d->PositionY;
  221. }
  222. // -------------------------------------------------------------------------
  223. double ctkQImageView::slicePosition( void )
  224. {
  225. Q_D( ctkQImageView );
  226. return d->SliceNumber;
  227. }
  228. // -------------------------------------------------------------------------
  229. double ctkQImageView::positionValue( void )
  230. {
  231. Q_D( ctkQImageView );
  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 ctkQImageView::xCenter( void )
  242. {
  243. Q_D( ctkQImageView );
  244. return d->CenterX;
  245. }
  246. // -------------------------------------------------------------------------
  247. double ctkQImageView::yCenter( void )
  248. {
  249. Q_D( ctkQImageView );
  250. return d->CenterY;
  251. }
  252. // -------------------------------------------------------------------------
  253. void ctkQImageView::setSliceNumber( int slicenum )
  254. {
  255. Q_D( ctkQImageView );
  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 ctkQImageView::sliceNumber( void ) const
  270. {
  271. Q_D( const ctkQImageView );
  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 ctkQImageView::setIntensityWindow( double iwMin, double iwMax )
  283. {
  284. Q_D( ctkQImageView );
  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 ctkQImageView::intensityWindowMin( void ) const
  296. {
  297. Q_D( const ctkQImageView );
  298. return d->IntensityWindowMin;
  299. }
  300. // -------------------------------------------------------------------------
  301. double ctkQImageView::intensityWindowMax( void ) const
  302. {
  303. Q_D( const ctkQImageView );
  304. return d->IntensityWindowMax;
  305. }
  306. // -------------------------------------------------------------------------
  307. void ctkQImageView::setFlipXAxis( bool flip )
  308. {
  309. Q_D( ctkQImageView );
  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 ctkQImageView::flipXAxis( void ) const
  319. {
  320. Q_D( const ctkQImageView );
  321. return d->FlipXAxis;
  322. }
  323. // -------------------------------------------------------------------------
  324. void ctkQImageView::setFlipYAxis( bool flip )
  325. {
  326. Q_D( ctkQImageView );
  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 ctkQImageView::flipYAxis( void ) const
  336. {
  337. Q_D( const ctkQImageView );
  338. return d->FlipYAxis;
  339. }
  340. // -------------------------------------------------------------------------
  341. void ctkQImageView::setTransposeXY( bool transpose )
  342. {
  343. Q_D( ctkQImageView );
  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 ctkQImageView::transposeXY( void ) const
  353. {
  354. Q_D( const ctkQImageView );
  355. return d->TransposeXY;
  356. }
  357. // -------------------------------------------------------------------------
  358. void ctkQImageView::setCenter( double x, double y )
  359. {
  360. Q_D( ctkQImageView );
  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 ctkQImageView::setPosition( double x, double y )
  403. {
  404. Q_D( ctkQImageView );
  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 ctkQImageView::zoom( void )
  417. {
  418. Q_D( ctkQImageView );
  419. if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size() )
  420. {
  421. return d->Zoom;
  422. }
  423. return 1;
  424. }
  425. // -------------------------------------------------------------------------
  426. void ctkQImageView::setZoom( double factor )
  427. {
  428. Q_D( ctkQImageView );
  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 ctkQImageView::reset( )
  475. {
  476. Q_D( ctkQImageView );
  477. if( d->ImageList.size() > 0 )
  478. {
  479. if( d->SliceNumber < 0 )
  480. {
  481. this->setSliceNumber( 0 );
  482. }
  483. }
  484. if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size() )
  485. {
  486. d->fitImageRectangle( 0, 0, d->ImageList[ d->SliceNumber ].width(),
  487. d->ImageList[ d->SliceNumber ].height() );
  488. this->update( true, true );
  489. }
  490. }
  491. // -------------------------------------------------------------------------
  492. void ctkQImageView::keyPressEvent( QKeyEvent * event )
  493. {
  494. Q_D( ctkQImageView );
  495. if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size() )
  496. {
  497. switch( event->key() )
  498. {
  499. case Qt::Key_R:
  500. {
  501. this->reset();
  502. break;
  503. }
  504. case Qt::Key_Up:
  505. {
  506. this->setSliceNumber( d->SliceNumber+1 );
  507. break;
  508. }
  509. case Qt::Key_Down:
  510. {
  511. this->setSliceNumber( d->SliceNumber-1 );
  512. break;
  513. }
  514. default:
  515. {
  516. event->ignore();
  517. }
  518. };
  519. }
  520. }
  521. // -------------------------------------------------------------------------
  522. void ctkQImageView::mousePressEvent( QMouseEvent * event )
  523. {
  524. Q_D( ctkQImageView );
  525. if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size() )
  526. {
  527. switch( event->button() )
  528. {
  529. case Qt::LeftButton:
  530. {
  531. d->MouseLeftDragging = true;
  532. d->MouseLastX = event->x();
  533. d->MouseLastY = event->y();
  534. d->MouseLastIntensityWindowMin = this->intensityWindowMin();
  535. d->MouseLastIntensityWindowMax = this->intensityWindowMax();
  536. break;
  537. }
  538. case Qt::MidButton:
  539. {
  540. d->MouseMiddleDragging = true;
  541. d->MouseLastX = event->x();
  542. d->MouseLastY = event->y();
  543. d->MouseLastZoom = this->zoom();
  544. break;
  545. }
  546. case Qt::RightButton:
  547. {
  548. d->MouseRightDragging = true;
  549. double relativeX = static_cast<double>( event->x() )
  550. / this->width();
  551. double relativeY = static_cast<double>( event->y() )
  552. / this->height();
  553. double x = (d->TmpXMax - d->TmpXMin) * relativeX + d->TmpXMin;
  554. double y = (d->TmpYMax - d->TmpYMin) * relativeY + d->TmpYMin;
  555. this->setCenter( x, y );
  556. break;
  557. }
  558. default:
  559. {
  560. event->ignore();
  561. }
  562. };
  563. }
  564. }
  565. // -------------------------------------------------------------------------
  566. void ctkQImageView::mouseReleaseEvent( QMouseEvent * event )
  567. {
  568. Q_D( ctkQImageView );
  569. d->MouseLeftDragging = false;
  570. d->MouseMiddleDragging = false;
  571. d->MouseRightDragging = false;
  572. event->ignore();
  573. }
  574. // -------------------------------------------------------------------------
  575. void ctkQImageView::mouseMoveEvent( QMouseEvent * event )
  576. {
  577. Q_D( ctkQImageView );
  578. if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size() )
  579. {
  580. if( d->MouseLeftDragging )
  581. {
  582. double distX = d->MouseLastX - event->x();
  583. double distY = d->MouseLastY - event->y();
  584. double deltaMin = ( distX / this->height() );
  585. if( deltaMin < 0 )
  586. {
  587. // Heuristic to make shrinking propotional to enlarging
  588. deltaMin *= -deltaMin;
  589. }
  590. double deltaMax = ( distY / this->width() );
  591. if( deltaMax < 0 )
  592. {
  593. // Heuristic to make shrinking propotional to enlarging
  594. deltaMax *= -deltaMax;
  595. }
  596. double iRange = d->MouseLastIntensityWindowMax
  597. - d->MouseLastIntensityWindowMin;
  598. deltaMin *= iRange;
  599. deltaMax *= iRange;
  600. double newMin = d->MouseLastIntensityWindowMin + deltaMin;
  601. double newMax = d->MouseLastIntensityWindowMax + deltaMax;
  602. this->setIntensityWindow( newMin, newMax );
  603. }
  604. else if( d->MouseMiddleDragging )
  605. {
  606. double distY = d->MouseLastY - event->y();
  607. double deltaZ = (distY / this->height());
  608. if( deltaZ < 0 )
  609. {
  610. // Heuristic to make shrinking propotional to enlarging
  611. deltaZ *= -deltaZ;
  612. }
  613. double newZoom = d->MouseLastZoom + deltaZ;
  614. this->setZoom( newZoom );
  615. }
  616. else
  617. {
  618. double relativeX = static_cast<double>( event->x() )
  619. / this->width();
  620. double relativeY = static_cast<double>( event->y() )
  621. / this->height();
  622. double x = (d->TmpXMax - d->TmpXMin) * relativeX + d->TmpXMin;
  623. double y = (d->TmpYMax - d->TmpYMin) * relativeY + d->TmpYMin;
  624. this->setPosition( x, y );
  625. }
  626. }
  627. }
  628. // -------------------------------------------------------------------------
  629. void ctkQImageView::resizeEvent( QResizeEvent* event )
  630. {
  631. this->Superclass::resizeEvent( event );
  632. this->update( false, true );
  633. }
  634. // -------------------------------------------------------------------------
  635. void ctkQImageView::update( bool zoomChanged,
  636. bool sizeChanged )
  637. {
  638. Q_D( ctkQImageView );
  639. if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size() )
  640. {
  641. const QImage * img = & ( d->ImageList[ d->SliceNumber ] );
  642. if( zoomChanged || sizeChanged )
  643. {
  644. if( this->width() > 0 && this->height() > 0
  645. && d->TmpXMax > d->TmpXMin && d->TmpYMax > d->TmpYMin)
  646. {
  647. int tmpXRange = d->TmpXMax - d->TmpXMin;
  648. int tmpYRange = d->TmpYMax - d->TmpYMin;
  649. double tmpAspectRatio = static_cast<double>(tmpYRange) / tmpXRange;
  650. double screenAspectRatio = static_cast<double>(this->height())
  651. / this->width();
  652. if( screenAspectRatio > tmpAspectRatio )
  653. {
  654. int extraTmpYAbove = d->TmpYMin;
  655. int extraTmpYBelow = img->height() - d->TmpYMax;
  656. int extraTmpYNeeded = tmpXRange * screenAspectRatio
  657. - tmpYRange;
  658. int minExtra = extraTmpYAbove;
  659. if( extraTmpYBelow < minExtra )
  660. {
  661. minExtra = extraTmpYBelow;
  662. }
  663. if(2 * minExtra >= extraTmpYNeeded)
  664. {
  665. int minNeeded = extraTmpYNeeded / 2.0;
  666. int maxNeeded = extraTmpYNeeded - minNeeded;
  667. d->TmpYMin -= minNeeded;
  668. d->TmpYMax += maxNeeded;
  669. }
  670. else if(extraTmpYAbove + extraTmpYBelow >= extraTmpYNeeded)
  671. {
  672. if(extraTmpYAbove < extraTmpYBelow)
  673. {
  674. d->TmpYMin = 0;
  675. d->TmpYMax += extraTmpYNeeded - extraTmpYAbove;
  676. }
  677. else
  678. {
  679. d->TmpYMax = img->height();
  680. d->TmpYMin -= extraTmpYNeeded - extraTmpYBelow;
  681. }
  682. }
  683. else
  684. {
  685. d->TmpYMin = 0;
  686. d->TmpYMax = img->height();
  687. }
  688. d->TmpImage = QPixmap( this->width(),
  689. static_cast<unsigned int>(
  690. static_cast<double>(d->TmpYMax - d->TmpYMin)
  691. / (d->TmpXMax - d->TmpXMin)
  692. * this->width() + 0.5 ) );
  693. }
  694. else if(screenAspectRatio < tmpAspectRatio)
  695. {
  696. int extraTmpXLeft = d->TmpXMin;
  697. int extraTmpXRight = img->width() - d->TmpXMax;
  698. int extraTmpXNeeded = static_cast<double>(tmpYRange)
  699. / screenAspectRatio - tmpXRange;
  700. int minExtra = extraTmpXLeft;
  701. if( extraTmpXRight < minExtra )
  702. {
  703. minExtra = extraTmpXRight;
  704. }
  705. if(2 * minExtra >= extraTmpXNeeded)
  706. {
  707. int minNeeded = extraTmpXNeeded / 2.0;
  708. int maxNeeded = extraTmpXNeeded - minNeeded;
  709. d->TmpXMin -= minNeeded;
  710. d->TmpXMax += maxNeeded;
  711. }
  712. else if(extraTmpXLeft + extraTmpXRight >= extraTmpXNeeded)
  713. {
  714. if(extraTmpXLeft < extraTmpXRight)
  715. {
  716. d->TmpXMin = 0;
  717. d->TmpXMax += extraTmpXNeeded - extraTmpXLeft;
  718. }
  719. else
  720. {
  721. d->TmpXMax = img->width();
  722. d->TmpXMin -= extraTmpXNeeded - extraTmpXRight;
  723. }
  724. }
  725. else
  726. {
  727. d->TmpXMin = 0;
  728. d->TmpXMax = img->width();
  729. }
  730. d->TmpImage = QPixmap( static_cast<unsigned int>( this->height()
  731. / ( static_cast<double>(d->TmpYMax - d->TmpYMin)
  732. / (d->TmpXMax - d->TmpXMin) )
  733. + 0.5 ), this->height() );
  734. }
  735. else
  736. {
  737. d->TmpImage = QPixmap( this->width(), this->height() );
  738. }
  739. }
  740. }
  741. if( d->TmpImage.width() > 0 && d->TmpImage.height() > 0)
  742. {
  743. QPainter painter( &(d->TmpImage) );
  744. painter.drawPixmap( 0, 0, d->TmpImage.width(), d->TmpImage.height(),
  745. QPixmap::fromImage(*img), d->TmpXMin, d->TmpYMin,
  746. d->TmpXMax - d->TmpXMin, d->TmpYMax - d->TmpYMin);
  747. }
  748. d->Window->setPixmap( d->TmpImage );
  749. }
  750. else
  751. {
  752. d->Window->setText( "No Image Loaded." );
  753. }
  754. }