ctkQImageView.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. #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. #include <QColor>
  27. #include <QTextEdit>
  28. #include <QDialog>
  29. #include <cmath>
  30. //--------------------------------------------------------------------------
  31. class ctkQImageViewPrivate
  32. {
  33. Q_DECLARE_PUBLIC( ctkQImageView );
  34. protected:
  35. ctkQImageView* const q_ptr;
  36. public:
  37. ctkQImageViewPrivate( ctkQImageView& object );
  38. void init();
  39. QLabel * Window;
  40. double Zoom;
  41. double PositionX;
  42. double PositionY;
  43. double CenterX;
  44. double CenterY;
  45. int SliceNumber;
  46. double IntensityLevel;
  47. double IntensityWindow;
  48. double IntensityMin;
  49. double IntensityMax;
  50. bool FlipXAxis;
  51. bool FlipYAxis;
  52. bool TransposeXY;
  53. QList< QImage > ImageList;
  54. QPixmap TmpImage;
  55. int TmpXMin;
  56. int TmpXMax;
  57. int TmpYMin;
  58. int TmpYMax;
  59. bool InvertImage;
  60. int MouseLastX;
  61. int MouseLastY;
  62. double MouseLastZoom;
  63. double MouseLastIntensityLevel;
  64. double MouseLastIntensityWindow;
  65. bool MouseLeftDragging;
  66. bool MouseMiddleDragging;
  67. bool MouseRightDragging;
  68. double clamp( double x, double xMin, double xMax );
  69. void fitImageRectangle( double x0, double y0, double x1, double y1 );
  70. };
  71. //--------------------------------------------------------------------------
  72. ctkQImageViewPrivate::ctkQImageViewPrivate(
  73. ctkQImageView& object )
  74. : q_ptr( &object )
  75. {
  76. this->Window = new QLabel();
  77. }
  78. //--------------------------------------------------------------------------
  79. void ctkQImageViewPrivate::init()
  80. {
  81. Q_Q( ctkQImageView );
  82. this->Window->setParent(q);
  83. QHBoxLayout* layout = new QHBoxLayout(q);
  84. layout->addWidget(this->Window);
  85. layout->setContentsMargins(0,0,0,0);
  86. q->setLayout(layout);
  87. q->setMouseTracking( true );
  88. // Set parameters for the view
  89. this->Zoom = 1;
  90. this->PositionX = 0;
  91. this->PositionY = 0;
  92. this->SliceNumber = 0;
  93. this->CenterX = 0;
  94. this->CenterY = 0;
  95. this->InvertImage = false;
  96. this->IntensityMin = 0;
  97. this->IntensityMax = 0;
  98. this->IntensityLevel = 0;
  99. this->IntensityWindow = 0;
  100. this->FlipXAxis = false;
  101. this->FlipYAxis = false;
  102. this->TransposeXY = false;
  103. this->ImageList.clear();
  104. this->TmpXMin = 0;
  105. this->TmpXMax = 0;
  106. this->TmpYMin = 0;
  107. this->TmpYMax = 0;
  108. this->MouseLastX = 0;
  109. this->MouseLastY = 0;
  110. this->MouseLastZoom = 0;
  111. this->MouseLeftDragging = false;
  112. this->MouseMiddleDragging = false;
  113. this->MouseRightDragging = false;
  114. // Don't expand for no reason
  115. q->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
  116. }
  117. //--------------------------------------------------------------------------
  118. double ctkQImageViewPrivate::clamp( double x, double xMin,
  119. double xMax )
  120. {
  121. if( x < xMin )
  122. {
  123. return xMin;
  124. }
  125. if( x > xMax )
  126. {
  127. return xMax;
  128. }
  129. return x;
  130. }
  131. //--------------------------------------------------------------------------
  132. void ctkQImageViewPrivate::fitImageRectangle( double x0,
  133. double x1, double y0, double y1 )
  134. {
  135. if( this->SliceNumber >= 0 && this->SliceNumber < this->ImageList.size() )
  136. {
  137. this->TmpXMin = this->clamp( x0, 0,
  138. this->ImageList[ this->SliceNumber ].width() );
  139. this->TmpXMax = this->clamp( x1, this->TmpXMin,
  140. this->ImageList[ this->SliceNumber ].width() );
  141. this->TmpYMin = this->clamp( y0, 0,
  142. this->ImageList[ this->SliceNumber ].height() );
  143. this->TmpYMax = this->clamp( y1, this->TmpYMin,
  144. this->ImageList[ this->SliceNumber ].height() );
  145. }
  146. }
  147. // -------------------------------------------------------------------------
  148. ctkQImageView::ctkQImageView( QWidget* _parent )
  149. : Superclass( _parent ),
  150. d_ptr( new ctkQImageViewPrivate( *this ) )
  151. {
  152. Q_D( ctkQImageView );
  153. d->init();
  154. d->TmpXMax = this->width();
  155. d->TmpYMax = this->height();
  156. }
  157. // -------------------------------------------------------------------------
  158. ctkQImageView::ctkQImageView(
  159. ctkQImageViewPrivate& pvt,
  160. QWidget* _parent)
  161. : Superclass(_parent), d_ptr(&pvt)
  162. {
  163. Q_D(ctkQImageView);
  164. d->init();
  165. }
  166. // -------------------------------------------------------------------------
  167. ctkQImageView::~ctkQImageView()
  168. {
  169. }
  170. // -------------------------------------------------------------------------
  171. void ctkQImageView::addImage( const QImage & image )
  172. {
  173. Q_D( ctkQImageView );
  174. d->ImageList.push_back( image );
  175. d->TmpXMin = 0;
  176. d->TmpXMax = image.width();
  177. d->TmpYMin = 0;
  178. d->TmpYMax = image.height();
  179. if( image.isGrayscale() )
  180. {
  181. d->IntensityMin = 0;
  182. d->IntensityMax = image.colorCount();
  183. this->setIntensityWindowLevel(
  184. image.colorCount(), image.colorCount()/2 );
  185. }
  186. this->update( true, false );
  187. this->setCenter( image.width()/2.0, image.height()/2.0 );
  188. }
  189. // -------------------------------------------------------------------------
  190. void ctkQImageView::clearImages( void )
  191. {
  192. Q_D( ctkQImageView );
  193. d->ImageList.clear();
  194. this->update( true, true );
  195. }
  196. // -------------------------------------------------------------------------
  197. double ctkQImageView::xSpacing( void )
  198. {
  199. Q_D( ctkQImageView );
  200. if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size() )
  201. {
  202. return( 1000.0 / d->ImageList[ d->SliceNumber ].dotsPerMeterX() );
  203. }
  204. else
  205. {
  206. return 1;
  207. }
  208. }
  209. // -------------------------------------------------------------------------
  210. double ctkQImageView::ySpacing( void )
  211. {
  212. Q_D( ctkQImageView );
  213. if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size() )
  214. {
  215. return( 1000.0 / d->ImageList[ d->SliceNumber ].dotsPerMeterY() );
  216. }
  217. else
  218. {
  219. return 1;
  220. }
  221. }
  222. // -------------------------------------------------------------------------
  223. double ctkQImageView::sliceSpacing( void )
  224. {
  225. return 1;
  226. }
  227. // -------------------------------------------------------------------------
  228. double ctkQImageView::sliceThickness( void )
  229. {
  230. return 1;
  231. }
  232. // -------------------------------------------------------------------------
  233. double ctkQImageView::xPosition( void )
  234. {
  235. Q_D( ctkQImageView );
  236. return d->PositionX;
  237. }
  238. // -------------------------------------------------------------------------
  239. double ctkQImageView::yPosition( void )
  240. {
  241. Q_D( ctkQImageView );
  242. return d->PositionY;
  243. }
  244. // -------------------------------------------------------------------------
  245. double ctkQImageView::slicePosition( void )
  246. {
  247. Q_D( ctkQImageView );
  248. return d->SliceNumber;
  249. }
  250. // -------------------------------------------------------------------------
  251. double ctkQImageView::positionValue( void )
  252. {
  253. Q_D( ctkQImageView );
  254. if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size() )
  255. {
  256. QColor vc( d->ImageList[ d->SliceNumber ].pixel( d->PositionX,
  257. d->PositionY ) );
  258. return vc.value();
  259. }
  260. return 0;
  261. }
  262. // -------------------------------------------------------------------------
  263. double ctkQImageView::xCenter( void )
  264. {
  265. Q_D( ctkQImageView );
  266. return d->CenterX;
  267. }
  268. // -------------------------------------------------------------------------
  269. double ctkQImageView::yCenter( void )
  270. {
  271. Q_D( ctkQImageView );
  272. return d->CenterY;
  273. }
  274. // -------------------------------------------------------------------------
  275. void ctkQImageView::setSliceNumber( int slicenum )
  276. {
  277. Q_D( ctkQImageView );
  278. if( slicenum >= 0 && slicenum < d->ImageList.size()
  279. && slicenum != d->SliceNumber )
  280. {
  281. d->SliceNumber = slicenum;
  282. emit this->sliceNumberChanged( slicenum );
  283. emit this->xSpacingChanged( this->xSpacing() );
  284. emit this->ySpacingChanged( this->ySpacing() );
  285. emit this->sliceThicknessChanged( this->sliceThickness() );
  286. emit this->slicePositionChanged( this->slicePosition() );
  287. this->update( false, false );
  288. }
  289. }
  290. //
  291. // -------------------------------------------------------------------------
  292. int ctkQImageView::sliceNumber( void ) const
  293. {
  294. Q_D( const ctkQImageView );
  295. if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size() )
  296. {
  297. return d->SliceNumber;
  298. }
  299. else
  300. {
  301. return -1;
  302. }
  303. }
  304. // -------------------------------------------------------------------------
  305. void ctkQImageView::setIntensityWindowLevel( double iwWindow,
  306. double iwLevel )
  307. {
  308. Q_D( ctkQImageView );
  309. if( iwLevel != d->IntensityLevel ||
  310. iwWindow != d->IntensityWindow )
  311. {
  312. d->IntensityLevel = iwLevel;
  313. d->IntensityWindow = iwWindow;
  314. emit this->intensityWindowChanged( iwWindow );
  315. emit this->intensityLevelChanged( iwLevel );
  316. this->update( false, false );
  317. }
  318. }
  319. // -------------------------------------------------------------------------
  320. double ctkQImageView::intensityLevel( void ) const
  321. {
  322. Q_D( const ctkQImageView );
  323. return d->IntensityLevel;
  324. }
  325. // -------------------------------------------------------------------------
  326. double ctkQImageView::intensityWindow( void ) const
  327. {
  328. Q_D( const ctkQImageView );
  329. return d->IntensityWindow;
  330. }
  331. // -------------------------------------------------------------------------
  332. void ctkQImageView::setInvertImage( bool invert )
  333. {
  334. Q_D( ctkQImageView );
  335. if( invert != d->InvertImage )
  336. {
  337. d->InvertImage = invert;
  338. emit this->invertImageChanged( invert );
  339. this->update( false, false );
  340. }
  341. }
  342. // -------------------------------------------------------------------------
  343. bool ctkQImageView::invertImage( void ) const
  344. {
  345. Q_D( const ctkQImageView );
  346. return d->InvertImage;
  347. }
  348. // -------------------------------------------------------------------------
  349. void ctkQImageView::setFlipXAxis( bool flip )
  350. {
  351. Q_D( ctkQImageView );
  352. if( flip != d->FlipXAxis )
  353. {
  354. d->FlipXAxis = flip;
  355. emit this->flipXAxisChanged( flip );
  356. this->update( false, false );
  357. }
  358. }
  359. // -------------------------------------------------------------------------
  360. bool ctkQImageView::flipXAxis( void ) const
  361. {
  362. Q_D( const ctkQImageView );
  363. return d->FlipXAxis;
  364. }
  365. // -------------------------------------------------------------------------
  366. void ctkQImageView::setFlipYAxis( bool flip )
  367. {
  368. Q_D( ctkQImageView );
  369. if( flip != d->FlipYAxis )
  370. {
  371. d->FlipYAxis = flip;
  372. emit this->flipYAxisChanged( flip );
  373. this->update( false, false );
  374. }
  375. }
  376. // -------------------------------------------------------------------------
  377. bool ctkQImageView::flipYAxis( void ) const
  378. {
  379. Q_D( const ctkQImageView );
  380. return d->FlipYAxis;
  381. }
  382. // -------------------------------------------------------------------------
  383. void ctkQImageView::setTransposeXY( bool transpose )
  384. {
  385. Q_D( ctkQImageView );
  386. if( transpose != d->TransposeXY )
  387. {
  388. d->TransposeXY = transpose;
  389. emit this->transposeXYChanged( transpose );
  390. this->update( false, false );
  391. }
  392. }
  393. // -------------------------------------------------------------------------
  394. bool ctkQImageView::transposeXY( void ) const
  395. {
  396. Q_D( const ctkQImageView );
  397. return d->TransposeXY;
  398. }
  399. // -------------------------------------------------------------------------
  400. void ctkQImageView::setCenter( double x, double y )
  401. {
  402. Q_D( ctkQImageView );
  403. if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size() )
  404. {
  405. int tmpXRange = d->TmpXMax - d->TmpXMin;
  406. if( tmpXRange > d->ImageList[ d->SliceNumber ].width() )
  407. {
  408. tmpXRange = d->ImageList[ d->SliceNumber ].width();
  409. }
  410. int tmpYRange = d->TmpYMax - d->TmpYMin;
  411. if( tmpYRange > d->ImageList[ d->SliceNumber ].height() )
  412. {
  413. tmpYRange = d->ImageList[ d->SliceNumber ].height();
  414. }
  415. int xMin2 = static_cast<int>(x) - tmpXRange/2.0;
  416. if( xMin2 < 0 )
  417. {
  418. xMin2 = 0;
  419. }
  420. int xMax2 = xMin2 + tmpXRange;
  421. if( xMax2 > d->ImageList[ d->SliceNumber ].width() )
  422. {
  423. xMax2 = d->ImageList[ d->SliceNumber ].width();
  424. xMin2 = xMax2 - tmpXRange;
  425. }
  426. int yMin2 = static_cast<int>(y) - tmpYRange/2.0;
  427. if( yMin2 < 0 )
  428. {
  429. yMin2 = 0;
  430. }
  431. int yMax2 = yMin2 + tmpYRange;
  432. if( yMax2 > d->ImageList[ d->SliceNumber ].height() )
  433. {
  434. yMax2 = d->ImageList[ d->SliceNumber ].height();
  435. yMin2 = yMax2 - tmpYRange;
  436. }
  437. d->fitImageRectangle( xMin2, xMax2, yMin2, yMax2 );
  438. d->CenterX = x;
  439. d->CenterY = y;
  440. emit this->xCenterChanged( x );
  441. emit this->yCenterChanged( y );
  442. this->update( false, false );
  443. }
  444. }
  445. // -------------------------------------------------------------------------
  446. void ctkQImageView::setPosition( double x, double y )
  447. {
  448. Q_D( ctkQImageView );
  449. if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size()
  450. && x >= 0 && y >= 0 && x < d->ImageList[ d->SliceNumber ].width()
  451. && y < d->ImageList[ d->SliceNumber ].height() )
  452. {
  453. d->PositionX = x;
  454. d->PositionY = y;
  455. emit this->xPositionChanged( x );
  456. emit this->yPositionChanged( y );
  457. emit this->positionValueChanged( this->positionValue() );
  458. this->update( false, false );
  459. }
  460. }
  461. // -------------------------------------------------------------------------
  462. double ctkQImageView::zoom( void )
  463. {
  464. Q_D( ctkQImageView );
  465. if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size() )
  466. {
  467. return d->Zoom;
  468. }
  469. return 1;
  470. }
  471. // -------------------------------------------------------------------------
  472. void ctkQImageView::setZoom( double factor )
  473. {
  474. Q_D( ctkQImageView );
  475. if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size() )
  476. {
  477. const QImage * img = & d->ImageList[ d->SliceNumber ];
  478. if( factor < 2.0 / img->width() )
  479. {
  480. factor = 2.0 / img->width();
  481. }
  482. if( factor > img->width()/2.0 )
  483. {
  484. factor = img->width()/2.0;
  485. }
  486. d->Zoom = factor;
  487. double cx = d->CenterX;
  488. double cy = d->CenterY;
  489. double x2 = img->width() / factor;
  490. double y2 = img->height() / factor;
  491. int xMin2 = static_cast<int>(cx) - x2 / 2.0;
  492. if( xMin2 < 0 )
  493. {
  494. xMin2 = 0;
  495. }
  496. int xMax2 = xMin2 + x2;
  497. if( xMax2 > d->ImageList[ d->SliceNumber ].width() )
  498. {
  499. xMax2 = d->ImageList[ d->SliceNumber ].width();
  500. xMin2 = xMax2 - x2;
  501. }
  502. int yMin2 = static_cast<int>(cy) - y2 / 2.0;
  503. if( yMin2 < 0 )
  504. {
  505. yMin2 = 0;
  506. }
  507. int yMax2 = yMin2 + y2;
  508. if( yMax2 > d->ImageList[ d->SliceNumber ].height() )
  509. {
  510. yMax2 = d->ImageList[ d->SliceNumber ].height();
  511. yMin2 = yMax2 - y2;
  512. }
  513. d->fitImageRectangle( xMin2, xMax2, yMin2, yMax2 );
  514. this->update( true, true );
  515. }
  516. }
  517. // -------------------------------------------------------------------------
  518. void ctkQImageView::reset( )
  519. {
  520. Q_D( ctkQImageView );
  521. if( d->ImageList.size() > 0 )
  522. {
  523. if( d->SliceNumber < 0 )
  524. {
  525. this->setSliceNumber( 0 );
  526. }
  527. }
  528. this->setZoom( 1 );
  529. if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size() )
  530. {
  531. this->setCenter( d->ImageList[ d->SliceNumber ].width()/2,
  532. d->ImageList[ d->SliceNumber ].height()/2 );
  533. }
  534. }
  535. // -------------------------------------------------------------------------
  536. void ctkQImageView::keyPressEvent( QKeyEvent * event )
  537. {
  538. Q_D( ctkQImageView );
  539. if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size() )
  540. {
  541. switch( event->key() )
  542. {
  543. case Qt::Key_H:
  544. {
  545. QTextEdit * help = new QTextEdit();
  546. help->setWindowFlags( Qt::Window );
  547. help->setMinimumSize( 500, 500 );
  548. help->setSizePolicy( QSizePolicy::Preferred,
  549. QSizePolicy::Preferred );
  550. help->setReadOnly( true );
  551. help->append("<h1>CTK Simple Image Viewer Widget</h1>");
  552. help->append("Contributed by: Kitware, Inc.<br>");
  553. help->append("<h3>Keyboard commands:</h3>");
  554. help->append(" <em>q</em> : quit");
  555. help->append(" <em>h</em> : display this help");
  556. help->append(" <em>i</em> : invert intensities");
  557. help->append(" <em>[ ]</em> : increase / decrease zoom");
  558. help->append(" <em>x y</em> : flip along the x / y axis");
  559. help->append(" <em>r</em> : reset to initial conditions");
  560. help->append(" <em>spacebar</em> : toggle continuous tracking of cursor");
  561. help->append(" <em>up-arrow down-arrow</em> : change to next / previous slice");
  562. help->append("<h3>Mouse commands:</h3>");
  563. help->append(" <em>left-button</em> : window and level");
  564. help->append(" <em>middle-button</em> : zoom");
  565. help->append(" <em>right-button</em> : center");
  566. help->show();
  567. break;
  568. }
  569. case Qt::Key_Space:
  570. {
  571. d->Window->setMouseTracking( ! d->Window->hasMouseTracking() );
  572. break;
  573. }
  574. case Qt::Key_X:
  575. {
  576. this->setFlipXAxis( ! this->flipXAxis() );
  577. break;
  578. }
  579. case Qt::Key_Y:
  580. {
  581. this->setFlipYAxis( ! this->flipYAxis() );
  582. break;
  583. }
  584. case Qt::Key_T:
  585. {
  586. this->setTransposeXY( ! this->transposeXY() );
  587. break;
  588. }
  589. case Qt::Key_BracketRight:
  590. {
  591. this->setZoom( this->zoom() * 1.1 );
  592. break;
  593. }
  594. case Qt::Key_BracketLeft:
  595. {
  596. this->setZoom( this->zoom() * 0.9 );
  597. break;
  598. }
  599. case Qt::Key_I:
  600. {
  601. this->setInvertImage( ! this->invertImage() );
  602. this->update( false, false );
  603. break;
  604. }
  605. case Qt::Key_Q:
  606. {
  607. exit( EXIT_SUCCESS );
  608. break;
  609. }
  610. case Qt::Key_R:
  611. {
  612. this->reset();
  613. break;
  614. }
  615. case Qt::Key_Up:
  616. {
  617. this->setSliceNumber( d->SliceNumber+1 );
  618. break;
  619. }
  620. case Qt::Key_Down:
  621. {
  622. this->setSliceNumber( d->SliceNumber-1 );
  623. break;
  624. }
  625. default:
  626. {
  627. event->ignore();
  628. }
  629. };
  630. }
  631. }
  632. // -------------------------------------------------------------------------
  633. void ctkQImageView::mousePressEvent( QMouseEvent * event )
  634. {
  635. Q_D( ctkQImageView );
  636. if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size() )
  637. {
  638. switch( event->button() )
  639. {
  640. case Qt::LeftButton:
  641. {
  642. d->MouseLeftDragging = true;
  643. d->MouseLastX = event->x();
  644. d->MouseLastY = event->y();
  645. d->MouseLastIntensityWindow = this->intensityWindow();
  646. d->MouseLastIntensityLevel = this->intensityLevel();
  647. break;
  648. }
  649. case Qt::MidButton:
  650. {
  651. d->MouseMiddleDragging = true;
  652. d->MouseLastX = event->x();
  653. d->MouseLastY = event->y();
  654. d->MouseLastZoom = this->zoom();
  655. break;
  656. }
  657. case Qt::RightButton:
  658. {
  659. d->MouseRightDragging = true;
  660. double relativeX = static_cast<double>( event->x() )
  661. / this->width();
  662. double relativeY = static_cast<double>( event->y() )
  663. / this->height();
  664. if( d->FlipXAxis )
  665. {
  666. relativeX = 1 - relativeX;
  667. }
  668. if( d->FlipYAxis )
  669. {
  670. relativeY = 1 - relativeY;
  671. }
  672. double x = (d->TmpXMax - d->TmpXMin) * relativeX + d->TmpXMin;
  673. double y = (d->TmpYMax - d->TmpYMin) * relativeY + d->TmpYMin;
  674. this->setCenter( x, y );
  675. break;
  676. }
  677. default:
  678. {
  679. event->ignore();
  680. }
  681. };
  682. }
  683. }
  684. // -------------------------------------------------------------------------
  685. void ctkQImageView::mouseReleaseEvent( QMouseEvent * event )
  686. {
  687. Q_D( ctkQImageView );
  688. d->MouseLeftDragging = false;
  689. d->MouseMiddleDragging = false;
  690. d->MouseRightDragging = false;
  691. event->ignore();
  692. }
  693. // -------------------------------------------------------------------------
  694. void ctkQImageView::mouseMoveEvent( QMouseEvent * event )
  695. {
  696. Q_D( ctkQImageView );
  697. if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size() )
  698. {
  699. if( d->MouseLeftDragging )
  700. {
  701. double distX = event->x() - d->MouseLastX;
  702. double distY = event->y() - d->MouseLastY;
  703. double deltaWin = ( distX / this->height() );
  704. if( deltaWin < 0 )
  705. {
  706. // Heuristic to make shrinking propotional to enlarging
  707. deltaWin *= -deltaWin;
  708. }
  709. double deltaLevel = ( distY / this->width() );
  710. if( deltaLevel < 0 )
  711. {
  712. // Heuristic to make shrinking propotional to enlarging
  713. deltaLevel *= -deltaLevel;
  714. }
  715. double iRange = d->IntensityMax - d->IntensityMin;
  716. deltaWin *= iRange;
  717. deltaLevel *= iRange;
  718. double newWin = d->MouseLastIntensityWindow + deltaWin;
  719. double newLevel = d->MouseLastIntensityLevel + deltaLevel;
  720. this->setIntensityWindowLevel( newWin, newLevel );
  721. }
  722. else if( d->MouseMiddleDragging )
  723. {
  724. double distY = d->MouseLastY - event->y();
  725. double deltaZ = 2 * (distY / this->height());
  726. if( deltaZ < 0 )
  727. {
  728. // Heuristic to make shrinking propotional to enlarging
  729. deltaZ *= -deltaZ;
  730. }
  731. double newZoom = d->MouseLastZoom + deltaZ;
  732. this->setZoom( newZoom );
  733. }
  734. else if( d->MouseRightDragging )
  735. {
  736. double relativeX = static_cast<double>( event->x() )
  737. / this->width();
  738. double relativeY = static_cast<double>( event->y() )
  739. / this->height();
  740. if( d->FlipXAxis )
  741. {
  742. relativeX = 1 - relativeX;
  743. }
  744. if( d->FlipYAxis )
  745. {
  746. relativeY = 1 - relativeY;
  747. }
  748. double x = (d->TmpXMax - d->TmpXMin) * relativeX + d->TmpXMin;
  749. double y = (d->TmpYMax - d->TmpYMin) * relativeY + d->TmpYMin;
  750. this->setCenter( x, y );
  751. }
  752. else
  753. {
  754. double relativeX = static_cast<double>( event->x() )
  755. / this->width();
  756. double relativeY = static_cast<double>( event->y() )
  757. / this->height();
  758. if( d->FlipXAxis )
  759. {
  760. relativeX = 1 - relativeX;
  761. }
  762. if( d->FlipYAxis )
  763. {
  764. relativeY = 1 - relativeY;
  765. }
  766. double x = (d->TmpXMax - d->TmpXMin) * relativeX + d->TmpXMin;
  767. double y = (d->TmpYMax - d->TmpYMin) * relativeY + d->TmpYMin;
  768. this->setPosition( x, y );
  769. }
  770. }
  771. }
  772. // -------------------------------------------------------------------------
  773. void ctkQImageView::enterEvent( QEvent * )
  774. {
  775. Q_D( ctkQImageView );
  776. QApplication::setOverrideCursor( QCursor(Qt::CrossCursor) );
  777. d->Window->grabKeyboard();
  778. }
  779. // -------------------------------------------------------------------------
  780. void ctkQImageView::leaveEvent( QEvent * )
  781. {
  782. Q_D( ctkQImageView );
  783. QApplication::restoreOverrideCursor();
  784. d->Window->releaseKeyboard();
  785. }
  786. // -------------------------------------------------------------------------
  787. void ctkQImageView::resizeEvent( QResizeEvent* event )
  788. {
  789. this->Superclass::resizeEvent( event );
  790. this->update( false, true );
  791. }
  792. // -------------------------------------------------------------------------
  793. void ctkQImageView::update( bool zoomChanged,
  794. bool sizeChanged )
  795. {
  796. Q_D( ctkQImageView );
  797. if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size() )
  798. {
  799. const QImage * img = & ( d->ImageList[ d->SliceNumber ] );
  800. if( zoomChanged || sizeChanged )
  801. {
  802. if( this->width() > 0 && this->height() > 0
  803. && d->TmpXMax > d->TmpXMin && d->TmpYMax > d->TmpYMin)
  804. {
  805. int tmpXRange = d->TmpXMax - d->TmpXMin;
  806. int tmpYRange = d->TmpYMax - d->TmpYMin;
  807. double tmpAspectRatio = static_cast<double>(tmpYRange) / tmpXRange;
  808. double screenAspectRatio = static_cast<double>(this->height())
  809. / this->width();
  810. if( screenAspectRatio > tmpAspectRatio )
  811. {
  812. int extraTmpYAbove = d->TmpYMin;
  813. int extraTmpYBelow = img->height() - d->TmpYMax;
  814. int extraTmpYNeeded = tmpXRange * screenAspectRatio
  815. - tmpYRange;
  816. int minExtra = extraTmpYAbove;
  817. if( extraTmpYBelow < minExtra )
  818. {
  819. minExtra = extraTmpYBelow;
  820. }
  821. if(2 * minExtra >= extraTmpYNeeded)
  822. {
  823. int minNeeded = extraTmpYNeeded / 2.0;
  824. int maxNeeded = extraTmpYNeeded - minNeeded;
  825. d->TmpYMin -= minNeeded;
  826. d->TmpYMax += maxNeeded;
  827. }
  828. else if(extraTmpYAbove + extraTmpYBelow >= extraTmpYNeeded)
  829. {
  830. if(extraTmpYAbove < extraTmpYBelow)
  831. {
  832. d->TmpYMin = 0;
  833. d->TmpYMax += extraTmpYNeeded - extraTmpYAbove;
  834. }
  835. else
  836. {
  837. d->TmpYMax = img->height();
  838. d->TmpYMin -= extraTmpYNeeded - extraTmpYBelow;
  839. }
  840. }
  841. else
  842. {
  843. d->TmpYMin = 0;
  844. d->TmpYMax = img->height();
  845. }
  846. d->TmpImage = QPixmap( this->width(),
  847. static_cast<unsigned int>(
  848. static_cast<double>(d->TmpYMax - d->TmpYMin)
  849. / (d->TmpXMax - d->TmpXMin)
  850. * this->width() + 0.5 ) );
  851. }
  852. else if(screenAspectRatio < tmpAspectRatio)
  853. {
  854. int extraTmpXLeft = d->TmpXMin;
  855. int extraTmpXRight = img->width() - d->TmpXMax;
  856. int extraTmpXNeeded = static_cast<double>(tmpYRange)
  857. / screenAspectRatio - tmpXRange;
  858. int minExtra = extraTmpXLeft;
  859. if( extraTmpXRight < minExtra )
  860. {
  861. minExtra = extraTmpXRight;
  862. }
  863. if(2 * minExtra >= extraTmpXNeeded)
  864. {
  865. int minNeeded = extraTmpXNeeded / 2.0;
  866. int maxNeeded = extraTmpXNeeded - minNeeded;
  867. d->TmpXMin -= minNeeded;
  868. d->TmpXMax += maxNeeded;
  869. }
  870. else if(extraTmpXLeft + extraTmpXRight >= extraTmpXNeeded)
  871. {
  872. if(extraTmpXLeft < extraTmpXRight)
  873. {
  874. d->TmpXMin = 0;
  875. d->TmpXMax += extraTmpXNeeded - extraTmpXLeft;
  876. }
  877. else
  878. {
  879. d->TmpXMax = img->width();
  880. d->TmpXMin -= extraTmpXNeeded - extraTmpXRight;
  881. }
  882. }
  883. else
  884. {
  885. d->TmpXMin = 0;
  886. d->TmpXMax = img->width();
  887. }
  888. d->TmpImage = QPixmap( static_cast<unsigned int>( this->height()
  889. / ( static_cast<double>(d->TmpYMax - d->TmpYMin)
  890. / (d->TmpXMax - d->TmpXMin) )
  891. + 0.5 ), this->height() );
  892. }
  893. else
  894. {
  895. d->TmpImage = QPixmap( this->width(), this->height() );
  896. }
  897. }
  898. }
  899. if( d->TmpImage.width() > 0 && d->TmpImage.height() > 0)
  900. {
  901. QRectF target( 0, 0, d->TmpImage.width(), d->TmpImage.height() );
  902. double sourceX = d->TmpXMin;
  903. double sourceY = d->TmpYMin;
  904. double sourceW = d->TmpXMax - d->TmpXMin;
  905. double sourceH = d->TmpYMax - d->TmpYMin;
  906. QPainter painter( &(d->TmpImage) );
  907. QImage tmpI = *img;
  908. if( d->InvertImage )
  909. {
  910. tmpI.invertPixels();
  911. }
  912. if( d->FlipXAxis || d->FlipYAxis )
  913. {
  914. tmpI = tmpI.mirrored( d->FlipXAxis, d->FlipYAxis );
  915. if( d->FlipXAxis )
  916. {
  917. sourceX = tmpI.width() - (d->TmpXMax - d->TmpXMin) - d->TmpXMin;
  918. }
  919. if( d->FlipYAxis )
  920. {
  921. sourceY = tmpI.height() - (d->TmpYMax - d->TmpYMin) - d->TmpYMin;
  922. }
  923. }
  924. QRectF source( sourceX, sourceY, sourceW, sourceH );
  925. painter.drawPixmap( target, QPixmap::fromImage( tmpI ), source );
  926. //if( ! sizeChanged )
  927. {
  928. int maxNumCharsPerLine = 50;
  929. int fontPointSize = 12;
  930. if( fontPointSize * maxNumCharsPerLine > this->width() )
  931. {
  932. fontPointSize = this->width() / maxNumCharsPerLine;
  933. }
  934. if( fontPointSize > 7 )
  935. {
  936. QString fontFamily( "Helvetica" );
  937. QFont textFont( fontFamily, fontPointSize );
  938. painter.setFont( textFont );
  939. QColor textColor;
  940. textColor.setNamedColor( "lime" );
  941. textColor.setAlpha( 128 );
  942. painter.setPen( textColor );
  943. int textFlags = Qt::AlignLeft | Qt::TextDontClip;
  944. QRectF pointRect( 0, 0, 1, 1 );
  945. QRectF spaceBound = painter.boundingRect( pointRect, textFlags,
  946. "X" );
  947. if( img->isGrayscale() )
  948. {
  949. QString intString = "Intensity Range = ";
  950. intString.append( QString::number( d->IntensityMin,
  951. 'f', 3 ) );
  952. intString.append( " - " );
  953. intString.append( QString::number( d->IntensityMax,
  954. 'f', 3 ) );
  955. QRectF intBound = painter.boundingRect( pointRect, textFlags,
  956. intString );
  957. QRectF intRect(
  958. spaceBound.width()/2,
  959. spaceBound.height()/8,
  960. intBound.width(), intBound.height() );
  961. painter.drawText( intRect, textFlags, intString,
  962. &intBound );
  963. QString wlString = "W / L = ";
  964. wlString.append( QString::number( this->intensityWindow(),
  965. 'f', 3 ) );
  966. wlString.append( " / " );
  967. wlString.append( QString::number( this->intensityLevel(),
  968. 'f', 3 ) );
  969. QRectF wlBound = painter.boundingRect( pointRect, textFlags,
  970. wlString );
  971. QRectF wlRect(
  972. spaceBound.width()/2,
  973. intRect.y() + intRect.height() + spaceBound.height()/8,
  974. wlBound.width(), wlBound.height() );
  975. painter.drawText( wlRect, textFlags, wlString,
  976. &wlBound );
  977. }
  978. QString spacingString = "Spacing = ";
  979. spacingString.append( QString::number( this->xSpacing(),
  980. 'f', 3 ) );
  981. spacingString.append( ", " );
  982. spacingString.append( QString::number( this->ySpacing(),
  983. 'f', 3 ) );
  984. spacingString.append( ", " );
  985. spacingString.append( QString::number( this->sliceThickness(),
  986. 'f', 3 ) );
  987. QRectF spacingBound = painter.boundingRect( pointRect, textFlags,
  988. spacingString );
  989. QRectF spacingRect(
  990. this->width() - spacingBound.width() - spaceBound.width()/2,
  991. this->height() - spacingBound.height() - spaceBound.height()/8,
  992. spacingBound.width(), spacingBound.height() );
  993. painter.drawText( spacingRect, textFlags, spacingString,
  994. &spacingBound );
  995. QString dimString = "Size = ";
  996. dimString.append(
  997. QString::number( d->ImageList[ d->SliceNumber ].width() ) );
  998. dimString.append( ", " );
  999. dimString.append(
  1000. QString::number( d->ImageList[ d->SliceNumber ].height() ) );
  1001. dimString.append( ", " );
  1002. dimString.append(
  1003. QString::number( d->ImageList.size() ) );
  1004. QRectF dimBound = painter.boundingRect( pointRect, textFlags,
  1005. dimString );
  1006. QRectF dimRect(
  1007. this->width() - dimBound.width() - spaceBound.width()/2,
  1008. spacingBound.y() - dimBound.height() - spaceBound.height()/8,
  1009. dimBound.width(), dimBound.height() );
  1010. painter.drawText( dimRect, textFlags, dimString, &dimBound );
  1011. QString rasString = "RAS = ";
  1012. rasString.append( QString::number(
  1013. this->xPosition() * this->xSpacing(), 'f', 3 ) );
  1014. rasString.append( ", " );
  1015. rasString.append( QString::number(
  1016. this->yPosition() * this->ySpacing(), 'f', 3 ) );
  1017. rasString.append( ", " );
  1018. rasString.append( QString::number(
  1019. this->slicePosition() * this->sliceSpacing(), 'f', 3 ) );
  1020. QRectF rasBound = painter.boundingRect( pointRect, textFlags,
  1021. rasString );
  1022. QRectF rasRect(
  1023. spaceBound.width()/2,
  1024. this->height() - rasBound.height() - spaceBound.height()/8,
  1025. rasBound.width(), rasBound.height() );
  1026. painter.drawText( rasRect, textFlags, rasString,
  1027. &rasBound );
  1028. QString ijkString = "IJK = ";
  1029. ijkString.append( QString::number( this->xPosition() ) );
  1030. ijkString.append( ", " );
  1031. ijkString.append( QString::number( this->yPosition() ) );
  1032. ijkString.append( ", " );
  1033. ijkString.append( QString::number( this->slicePosition() ) );
  1034. QRectF ijkBound = painter.boundingRect( pointRect, textFlags,
  1035. ijkString );
  1036. QRectF ijkRect(
  1037. spaceBound.width()/2,
  1038. rasBound.y() - ijkBound.height() - spaceBound.height()/8,
  1039. ijkBound.width(), ijkBound.height() );
  1040. painter.drawText( ijkRect, textFlags, ijkString, &ijkBound );
  1041. QString valString = "Value = ";
  1042. valString.append( QString::number( this->positionValue(),
  1043. 'f', 3 ) );
  1044. QRectF valBound = painter.boundingRect( pointRect, textFlags,
  1045. valString );
  1046. QRectF valRect(
  1047. spaceBound.width()/2,
  1048. ijkBound.y() - valBound.height() - spaceBound.height()/8,
  1049. valBound.width(), valBound.height() );
  1050. painter.drawText( valRect, textFlags, valString, &valBound );
  1051. }
  1052. QColor lineColor;
  1053. lineColor.setNamedColor( "red" );
  1054. lineColor.setAlpha( 128 );
  1055. painter.setPen( lineColor );
  1056. double x = ( this->xPosition() - d->TmpXMin )
  1057. / (d->TmpXMax - d->TmpXMin) * this->width();
  1058. double y = ( this->yPosition() - d->TmpYMin )
  1059. / (d->TmpYMax - d->TmpYMin) * this->height();
  1060. if( d->FlipXAxis )
  1061. {
  1062. x = this->width() - x;
  1063. }
  1064. if( d->FlipYAxis )
  1065. {
  1066. y = this->height() - y;
  1067. }
  1068. QLine lineX( x, 0, x, this->height() );
  1069. painter.drawLine( lineX );
  1070. QLine lineY( 0, y, this->width(), y );
  1071. painter.drawLine( lineY );
  1072. }
  1073. }
  1074. d->Window->setPixmap( d->TmpImage );
  1075. }
  1076. else
  1077. {
  1078. d->Window->setText( "No Image Loaded." );
  1079. }
  1080. }