ctkQImageView.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  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< 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. 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->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 ctkQImageView::keyPressEvent( QKeyEvent * event )
  485. {
  486. Q_D( ctkQImageView );
  487. if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size() )
  488. {
  489. switch( event->key() )
  490. {
  491. case Qt::Key_R:
  492. {
  493. this->reset();
  494. break;
  495. }
  496. case Qt::Key_Up:
  497. {
  498. this->setSliceNumber( d->SliceNumber+1 );
  499. break;
  500. }
  501. case Qt::Key_Down:
  502. {
  503. this->setSliceNumber( d->SliceNumber-1 );
  504. break;
  505. }
  506. default:
  507. {
  508. event->ignore();
  509. }
  510. };
  511. }
  512. }
  513. // -------------------------------------------------------------------------
  514. void ctkQImageView::mousePressEvent( QMouseEvent * event )
  515. {
  516. Q_D( ctkQImageView );
  517. if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size() )
  518. {
  519. switch( event->button() )
  520. {
  521. case Qt::LeftButton:
  522. {
  523. d->MouseLeftDragging = true;
  524. d->MouseLastX = event->x();
  525. d->MouseLastY = event->y();
  526. d->MouseLastIntensityWindowMin = this->intensityWindowMin();
  527. d->MouseLastIntensityWindowMax = this->intensityWindowMax();
  528. break;
  529. }
  530. case Qt::MidButton:
  531. {
  532. d->MouseMiddleDragging = true;
  533. d->MouseLastX = event->x();
  534. d->MouseLastY = event->y();
  535. d->MouseLastZoom = this->zoom();
  536. break;
  537. }
  538. case Qt::RightButton:
  539. {
  540. d->MouseRightDragging = true;
  541. double relativeX = static_cast<double>( event->x() )
  542. / this->width();
  543. double relativeY = static_cast<double>( event->y() )
  544. / this->height();
  545. double x = (d->TmpXMax - d->TmpXMin) * relativeX + d->TmpXMin;
  546. double y = (d->TmpYMax - d->TmpYMin) * relativeY + d->TmpYMin;
  547. this->setCenter( x, y );
  548. break;
  549. }
  550. default:
  551. {
  552. event->ignore();
  553. }
  554. };
  555. }
  556. }
  557. // -------------------------------------------------------------------------
  558. void ctkQImageView::mouseReleaseEvent( QMouseEvent * event )
  559. {
  560. Q_D( ctkQImageView );
  561. d->MouseLeftDragging = false;
  562. d->MouseMiddleDragging = false;
  563. d->MouseRightDragging = false;
  564. event->ignore();
  565. }
  566. // -------------------------------------------------------------------------
  567. void ctkQImageView::mouseMoveEvent( QMouseEvent * event )
  568. {
  569. Q_D( ctkQImageView );
  570. if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size() )
  571. {
  572. if( d->MouseLeftDragging )
  573. {
  574. double distX = d->MouseLastX - event->x();
  575. double distY = d->MouseLastY - event->y();
  576. double deltaMin = ( distX / this->height() );
  577. if( deltaMin < 0 )
  578. {
  579. // Heuristic to make shrinking propotional to enlarging
  580. deltaMin *= -deltaMin;
  581. }
  582. double deltaMax = ( distY / this->width() );
  583. if( deltaMax < 0 )
  584. {
  585. // Heuristic to make shrinking propotional to enlarging
  586. deltaMax *= -deltaMax;
  587. }
  588. double iRange = d->MouseLastIntensityWindowMax
  589. - d->MouseLastIntensityWindowMin;
  590. deltaMin *= iRange;
  591. deltaMax *= iRange;
  592. double newMin = d->MouseLastIntensityWindowMin + deltaMin;
  593. double newMax = d->MouseLastIntensityWindowMax + deltaMax;
  594. this->setIntensityWindow( newMin, newMax );
  595. }
  596. else if( d->MouseMiddleDragging )
  597. {
  598. double distY = d->MouseLastY - event->y();
  599. double deltaZ = (distY / this->height());
  600. if( deltaZ < 0 )
  601. {
  602. // Heuristic to make shrinking propotional to enlarging
  603. deltaZ *= -deltaZ;
  604. }
  605. double newZoom = d->MouseLastZoom + deltaZ;
  606. this->setZoom( newZoom );
  607. }
  608. else
  609. {
  610. double relativeX = static_cast<double>( event->x() )
  611. / this->width();
  612. double relativeY = static_cast<double>( event->y() )
  613. / this->height();
  614. double x = (d->TmpXMax - d->TmpXMin) * relativeX + d->TmpXMin;
  615. double y = (d->TmpYMax - d->TmpYMin) * relativeY + d->TmpYMin;
  616. this->setPosition( x, y );
  617. }
  618. }
  619. }
  620. // -------------------------------------------------------------------------
  621. void ctkQImageView::resizeEvent( QResizeEvent* event )
  622. {
  623. this->Superclass::resizeEvent( event );
  624. this->update( false, true );
  625. }
  626. // -------------------------------------------------------------------------
  627. void ctkQImageView::update( bool zoomChanged,
  628. bool sizeChanged )
  629. {
  630. std::cout << "Updating.." << std::endl;
  631. Q_D( ctkQImageView );
  632. if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size() )
  633. {
  634. const QImage * img = d->ImageList[ d->SliceNumber ];
  635. if( zoomChanged || sizeChanged )
  636. {
  637. std::cout << "Update: Changed" << std::endl;
  638. if( this->width() > 0 && this->height() > 0
  639. && d->TmpXMax > d->TmpXMin && d->TmpYMax > d->TmpYMin)
  640. {
  641. int tmpXRange = d->TmpXMax - d->TmpXMin;
  642. int tmpYRange = d->TmpYMax - d->TmpYMin;
  643. double tmpAspectRatio = static_cast<double>(tmpYRange) / tmpXRange;
  644. double screenAspectRatio = static_cast<double>(this->height())
  645. / this->width();
  646. if( screenAspectRatio > tmpAspectRatio )
  647. {
  648. int extraTmpYAbove = d->TmpYMin;
  649. int extraTmpYBelow = img->height() - d->TmpYMax;
  650. int extraTmpYNeeded = tmpXRange * screenAspectRatio
  651. - tmpYRange;
  652. int minExtra = extraTmpYAbove;
  653. if( extraTmpYBelow < minExtra )
  654. {
  655. minExtra = extraTmpYBelow;
  656. }
  657. if(2 * minExtra >= extraTmpYNeeded)
  658. {
  659. int minNeeded = extraTmpYNeeded / 2.0;
  660. int maxNeeded = extraTmpYNeeded - minNeeded;
  661. d->TmpYMin -= minNeeded;
  662. d->TmpYMax += maxNeeded;
  663. }
  664. else if(extraTmpYAbove + extraTmpYBelow >= extraTmpYNeeded)
  665. {
  666. if(extraTmpYAbove < extraTmpYBelow)
  667. {
  668. d->TmpYMin = 0;
  669. d->TmpYMax += extraTmpYNeeded - extraTmpYAbove;
  670. }
  671. else
  672. {
  673. d->TmpYMax = img->height();
  674. d->TmpYMin -= extraTmpYNeeded - extraTmpYBelow;
  675. }
  676. }
  677. else
  678. {
  679. d->TmpYMin = 0;
  680. d->TmpYMax = img->height();
  681. }
  682. d->TmpImage = QPixmap( this->width(),
  683. static_cast<unsigned int>(
  684. static_cast<double>(d->TmpYMax - d->TmpYMin)
  685. / (d->TmpXMax - d->TmpXMin)
  686. * this->width() + 0.5 ) );
  687. }
  688. else if(screenAspectRatio < tmpAspectRatio)
  689. {
  690. int extraTmpXLeft = d->TmpXMin;
  691. int extraTmpXRight = img->width() - d->TmpXMax;
  692. int extraTmpXNeeded = static_cast<double>(tmpYRange)
  693. / screenAspectRatio - tmpXRange;
  694. int minExtra = extraTmpXLeft;
  695. if( extraTmpXRight < minExtra )
  696. {
  697. minExtra = extraTmpXRight;
  698. }
  699. if(2 * minExtra >= extraTmpXNeeded)
  700. {
  701. int minNeeded = extraTmpXNeeded / 2.0;
  702. int maxNeeded = extraTmpXNeeded - minNeeded;
  703. d->TmpXMin -= minNeeded;
  704. d->TmpXMax += maxNeeded;
  705. }
  706. else if(extraTmpXLeft + extraTmpXRight >= extraTmpXNeeded)
  707. {
  708. if(extraTmpXLeft < extraTmpXRight)
  709. {
  710. d->TmpXMin = 0;
  711. d->TmpXMax += extraTmpXNeeded - extraTmpXLeft;
  712. }
  713. else
  714. {
  715. d->TmpXMax = img->width();
  716. d->TmpXMin -= extraTmpXNeeded - extraTmpXRight;
  717. }
  718. }
  719. else
  720. {
  721. d->TmpXMin = 0;
  722. d->TmpXMax = img->width();
  723. }
  724. d->TmpImage = QPixmap( static_cast<unsigned int>( this->height()
  725. / ( static_cast<double>(d->TmpYMax - d->TmpYMin)
  726. / (d->TmpXMax - d->TmpXMin) )
  727. + 0.5 ), this->height() );
  728. }
  729. else
  730. {
  731. d->TmpImage = QPixmap( this->width(), this->height() );
  732. }
  733. }
  734. }
  735. if( d->TmpImage.width() > 0 && d->TmpImage.height() > 0)
  736. {
  737. QPainter painter( &(d->TmpImage) );
  738. painter.drawPixmap( 0, 0, d->TmpImage.width(), d->TmpImage.height(),
  739. QPixmap::fromImage(*img), d->TmpXMin, d->TmpYMin,
  740. d->TmpXMax - d->TmpXMin, d->TmpYMax - d->TmpYMin);
  741. }
  742. d->Window->setPixmap( d->TmpImage );
  743. }
  744. else
  745. {
  746. d->Window->setText( "No Image Loaded." );
  747. }
  748. }