00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "PreCompiled.h"
00019 #ifndef _PreComp_
00020 # include <QAction>
00021 # include <QApplication>
00022 # include <QMenu>
00023 # include <QMouseEvent>
00024 # include <QSlider>
00025 # include <QStatusBar>
00026 # include <QToolBar>
00027 # include <cmath>
00028 #endif
00029
00030 #include "ImageView.h"
00031 #include "GLImageBox.h"
00032 #include "../App/ImageBase.h"
00033 #include "XpmImages.h"
00034
00035 using namespace ImageGui;
00036
00037
00038
00039
00040 ImageView::ImageView(QWidget* parent)
00041 : MDIView(0, parent)
00042 {
00043
00044 setMouseTracking(true);
00045
00046
00047 _mouseEventsEnabled = true;
00048
00049
00050 EnableStatusBar(true);
00051
00052
00053 _pGLImageBox = new GLImageBox(this);
00054 setCentralWidget(_pGLImageBox);
00055
00056 _currMode = nothing;
00057 _currX = 0;
00058 _currY = 0;
00059
00060
00061 createActions();
00062
00063
00064 connect(_pGLImageBox, SIGNAL(drawGraphics()), this, SLOT(drawGraphics()));
00065 }
00066
00067 ImageView::~ImageView()
00068 {
00069
00070 }
00071
00072
00073 void ImageView::createActions()
00074 {
00075
00076 _pFitAct = new QAction(this);
00077 _pFitAct->setText(tr("&Fit image"));
00078 _pFitAct->setIcon(QPixmap(image_stretch));
00079 _pFitAct->setStatusTip(tr("Stretch the image to fit the view"));
00080 connect(_pFitAct, SIGNAL(triggered()), this, SLOT(fitImage()));
00081
00082 _pOneToOneAct = new QAction(this);
00083 _pOneToOneAct->setText(tr("&1:1 scale"));
00084 _pOneToOneAct->setIcon(QPixmap(image_oneToOne));
00085 _pOneToOneAct->setStatusTip(tr("Display the image at a 1:1 scale"));
00086 connect(_pOneToOneAct, SIGNAL(triggered()), this, SLOT(oneToOneImage()));
00087
00088
00089 _pContextMenu = new QMenu(this);
00090 _pContextMenu->addAction(_pFitAct);
00091 _pContextMenu->addAction(_pOneToOneAct);
00092
00093
00094 _pStdToolBar = this->addToolBar(tr("Standard"));
00095 _pStdToolBar->addAction(_pFitAct);
00096 _pStdToolBar->addAction(_pOneToOneAct);
00097 }
00098
00099
00100 void ImageView::EnableStatusBar(bool Enable)
00101 {
00102 if (Enable == true)
00103 {
00104
00105 _statusBarEnabled = true;
00106 statusBar()->setSizeGripEnabled( false );
00107 statusBar()->showMessage(tr("Ready..."));
00108 }
00109 else
00110 {
00111
00112 _statusBarEnabled = false;
00113 QStatusBar *pStatusBar = statusBar();
00114 delete pStatusBar;
00115 }
00116 }
00117
00118
00119 void ImageView::EnableToolBar(bool Enable)
00120 {
00121 _pStdToolBar->setShown(Enable);
00122 }
00123
00124
00125 void ImageView::EnableMouseEvents(bool Enable)
00126 {
00127 _mouseEventsEnabled = Enable;
00128 }
00129
00130
00131
00132 void ImageView::EnableOneToOneAction(bool Enable)
00133 {
00134 _pOneToOneAct->setVisible(Enable);
00135 }
00136
00137
00138
00139 void ImageView::EnableFitImageAction(bool Enable)
00140 {
00141 _pFitAct->setVisible(Enable);
00142 }
00143
00144
00145 void ImageView::fitImage()
00146 {
00147 _pGLImageBox->stretchToFit();
00148 }
00149
00150
00151
00152 void ImageView::oneToOneImage()
00153 {
00154 _pGLImageBox->setNormal();
00155 _pGLImageBox->redraw();
00156 updateStatusBar();
00157 }
00158
00159
00160
00161
00162 void ImageView::showOriginalColors()
00163 {
00164 _pGLImageBox->clearColorMap();
00165 _pGLImageBox->redraw();
00166 }
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176 int ImageView::createColorMap(int numEntriesReq, bool Initialise)
00177 {
00178 return (_pGLImageBox->createColorMap(numEntriesReq, Initialise));
00179 }
00180
00181
00182 int ImageView::getNumColorMapEntries() const
00183 {
00184 return (_pGLImageBox->getNumColorMapEntries());
00185 }
00186
00187
00188 void ImageView::clearColorMap()
00189 {
00190 _pGLImageBox->clearColorMap();
00191 }
00192
00193
00194
00195
00196
00197
00198
00199
00200 int ImageView::setColorMapRGBAValue(int index, float red, float green, float blue, float alpha)
00201 {
00202 return (_pGLImageBox->setColorMapRGBAValue(index, red, green, blue, alpha));
00203 }
00204
00205
00206
00207
00208
00209 int ImageView::setColorMapRedValue(int index, float value)
00210 {
00211 return (_pGLImageBox->setColorMapRedValue(index, value));
00212 }
00213
00214
00215
00216
00217
00218 int ImageView::setColorMapGreenValue(int index, float value)
00219 {
00220 return (_pGLImageBox->setColorMapGreenValue(index, value));
00221 }
00222
00223
00224
00225
00226
00227 int ImageView::setColorMapBlueValue(int index, float value)
00228 {
00229 return (_pGLImageBox->setColorMapBlueValue(index, value));
00230 }
00231
00232
00233
00234
00235
00236 int ImageView::setColorMapAlphaValue(int index, float value)
00237 {
00238 return (_pGLImageBox->setColorMapAlphaValue(index, value));
00239 }
00240
00241
00242 void ImageView::clearImage()
00243 {
00244 _pGLImageBox->clearImage();
00245 _pGLImageBox->redraw();
00246 updateStatusBar();
00247 }
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261 int ImageView::createImageCopy(void* pSrcPixelData, unsigned long width, unsigned long height, int format, unsigned short numSigBitsPerSample, int displayMode)
00262 {
00263 int ret = _pGLImageBox->createImageCopy(pSrcPixelData, width, height, format, numSigBitsPerSample, displayMode);
00264 showOriginalColors();
00265 updateStatusBar();
00266 return ret;
00267 }
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285 int ImageView::pointImageTo(void* pSrcPixelData, unsigned long width, unsigned long height, int format, unsigned short numSigBitsPerSample, bool takeOwnership, int displayMode)
00286 {
00287 int ret = _pGLImageBox->pointImageTo(pSrcPixelData, width, height, format, numSigBitsPerSample, takeOwnership, displayMode);
00288 showOriginalColors();
00289 updateStatusBar();
00290 return ret;
00291 }
00292
00293
00294 void ImageView::mousePressEvent(QMouseEvent* cEvent)
00295 {
00296 if (_mouseEventsEnabled == true)
00297 {
00298
00299
00300 QPoint offset = _pGLImageBox->pos();
00301 int box_x = cEvent->x() - offset.x();
00302 int box_y = cEvent->y() - offset.y();
00303 _currX = box_x;
00304 _currY = box_y;
00305 switch(cEvent->buttons())
00306 {
00307 case Qt::MidButton:
00308 _currMode = panning;
00309 this->setCursor(QCursor(Qt::ClosedHandCursor));
00310 startDrag();
00311 break;
00312
00313
00314
00315 case Qt::LeftButton:
00316 if (cEvent->modifiers() & Qt::ShiftModifier)
00317 _currMode = addselection;
00318 else
00319 _currMode = selection;
00320 break;
00321 case Qt::RightButton:
00322 _pContextMenu->exec(cEvent->globalPos());
00323 break;
00324 default:
00325 _currMode = nothing;
00326 }
00327 }
00328 }
00329
00330 void ImageView::mouseDoubleClickEvent(QMouseEvent* cEvent)
00331 {
00332 if (_mouseEventsEnabled == true)
00333 {
00334
00335
00336 QPoint offset = _pGLImageBox->pos();
00337 int box_x = cEvent->x() - offset.x();
00338 int box_y = cEvent->y() - offset.y();
00339 _currX = box_x;
00340 _currY = box_y;
00341 if(cEvent->button() == Qt::MidButton)
00342 {
00343 double icX = _pGLImageBox->WCToIC_X(_currX);
00344 double icY = _pGLImageBox->WCToIC_Y(_currY);
00345
00346
00347 _pGLImageBox->setZoomFactor(_pGLImageBox->getZoomFactor(), true, (int)floor(icX + 0.5), (int)floor(icY + 0.5));
00348 _pGLImageBox->redraw();
00349 updateStatusBar();
00350 }
00351 }
00352 }
00353
00354
00355 void ImageView::mouseMoveEvent(QMouseEvent* cEvent)
00356 {
00357 QApplication::flush();
00358
00359
00360
00361 QPoint offset = _pGLImageBox->pos();
00362 int box_x = cEvent->x() - offset.x();
00363 int box_y = cEvent->y() - offset.y();
00364 if (_mouseEventsEnabled == true)
00365 {
00366 switch(_currMode)
00367 {
00368 case nothing:
00369 break;
00370 case panning:
00371 _pGLImageBox->relMoveWC(box_x - dragStartWCx, box_y - dragStartWCy);
00372 break;
00373 case zooming:
00374 zoom(_currX, _currY, box_x, box_y);
00375 break;
00376 default:
00377 break;
00378 }
00379 }
00380 _currX = box_x;
00381 _currY = box_y;
00382
00383
00384 updateStatusBar();
00385 }
00386
00387
00388 void ImageView::mouseReleaseEvent(QMouseEvent* cEvent)
00389 {
00390 if (_mouseEventsEnabled == true)
00391 {
00392
00393
00394 QPoint offset = _pGLImageBox->pos();
00395 int box_x = cEvent->x() - offset.x();
00396 int box_y = cEvent->y() - offset.y();
00397 switch(_currMode)
00398 {
00399 case selection:
00400 select(box_x, box_y);
00401 break;
00402 case addselection:
00403 addSelect(box_x, box_y);
00404 break;
00405 case panning:
00406 this->unsetCursor();
00407 break;
00408 default:
00409 break;
00410 }
00411 _currMode = nothing;
00412 }
00413 }
00414
00415
00416 void ImageView::wheelEvent(QWheelEvent * cEvent)
00417 {
00418 if (_mouseEventsEnabled == true)
00419 {
00420
00421
00422 QPoint offset = _pGLImageBox->pos();
00423 int box_x = cEvent->x() - offset.x();
00424 int box_y = cEvent->y() - offset.y();
00425
00426
00427 int numTicks = cEvent->delta() / 120;
00428 int ICx, ICy;
00429 _pGLImageBox->getCentrePoint(ICx, ICy);
00430 _pGLImageBox->setZoomFactor(_pGLImageBox->getZoomFactor() / pow(2.0, (double)numTicks), true, ICx, ICy);
00431 _pGLImageBox->redraw();
00432 _currX = box_x;
00433 _currY = box_y;
00434
00435
00436 updateStatusBar();
00437 }
00438 }
00439
00440 void ImageView::showEvent (QShowEvent * e)
00441 {
00442 _pGLImageBox->setFocus();
00443 }
00444
00445
00446 void ImageView::updateStatusBar()
00447 {
00448 if (_statusBarEnabled == true)
00449 {
00450
00451 QString txt = createStatusBarText();
00452
00453
00454 statusBar()->showMessage(txt);
00455 }
00456 }
00457
00458
00459
00460
00461 QString ImageView::createStatusBarText()
00462 {
00463
00464
00465 double zoomFactor = _pGLImageBox->getZoomFactor();
00466 double icX = _pGLImageBox->WCToIC_X(_currX);
00467 double icY = _pGLImageBox->WCToIC_Y(_currY);
00468 int pixX = (int)floor(icX + 0.5);
00469 int pixY = (int)floor(icY + 0.5);
00470 int colorFormat = _pGLImageBox->getImageFormat();
00471
00472
00473 QString txt;
00474 if ((colorFormat == IB_CF_GREY8) ||
00475 (colorFormat == IB_CF_GREY16) ||
00476 (colorFormat == IB_CF_GREY32))
00477 {
00478 double grey_value;
00479 if (_pGLImageBox->getImageSample(pixX, pixY, 0, grey_value) == 0)
00480 txt = QString::fromAscii("x,y = %1,%2 | %3 = %4 | %5 = %6")
00481 .arg(icX,0,'f',2).arg(icY,0,'f',2)
00482 .arg(tr("grey")).arg((int)grey_value)
00483 .arg(tr("zoom")).arg(zoomFactor,0,'f',1);
00484 else
00485 txt = QString::fromAscii("x,y = %1 | %2 = %3")
00486 .arg(tr("outside image")).arg(tr("zoom")).arg(zoomFactor,0,'f',1);
00487 }
00488 else if ((colorFormat == IB_CF_RGB24) ||
00489 (colorFormat == IB_CF_RGB48))
00490 {
00491 double red, green, blue;
00492 if ((_pGLImageBox->getImageSample(pixX, pixY, 0, red) != 0) ||
00493 (_pGLImageBox->getImageSample(pixX, pixY, 1, green) != 0) ||
00494 (_pGLImageBox->getImageSample(pixX, pixY, 2, blue) != 0))
00495 txt = QString::fromAscii("x,y = %1 | %2 = %3")
00496 .arg(tr("outside image")).arg(tr("zoom")).arg(zoomFactor,0,'f',1);
00497 else
00498 txt = QString::fromAscii("x,y = %1,%2 | rgb = %3,%4,%5 | %6 = %7")
00499 .arg(icX,0,'f',2).arg(icY,0,'f',2)
00500 .arg((int)red).arg((int)green).arg((int)blue)
00501 .arg(tr("zoom")).arg(zoomFactor,0,'f',1);
00502 }
00503 else if ((colorFormat == IB_CF_BGR24) ||
00504 (colorFormat == IB_CF_BGR48))
00505 {
00506 double red, green, blue;
00507 if ((_pGLImageBox->getImageSample(pixX, pixY, 0, blue) != 0) ||
00508 (_pGLImageBox->getImageSample(pixX, pixY, 1, green) != 0) ||
00509 (_pGLImageBox->getImageSample(pixX, pixY, 2, red) != 0))
00510 txt = QString::fromAscii("x,y = %1 | %2 = %3")
00511 .arg(tr("outside image")).arg(tr("zoom")).arg(zoomFactor,0,'f',1);
00512 else
00513 txt = QString::fromAscii("x,y = %1,%2 | rgb = %3,%4,%5 | %6 = %7")
00514 .arg(icX,0,'f',2).arg(icY,0,'f',2)
00515 .arg((int)red).arg((int)green).arg((int)blue)
00516 .arg(tr("zoom")).arg(zoomFactor,0,'f',1);
00517 }
00518 else if ((colorFormat == IB_CF_RGBA32) ||
00519 (colorFormat == IB_CF_RGBA64))
00520 {
00521 double red, green, blue, alpha;
00522 if ((_pGLImageBox->getImageSample(pixX, pixY, 0, red) != 0) ||
00523 (_pGLImageBox->getImageSample(pixX, pixY, 1, green) != 0) ||
00524 (_pGLImageBox->getImageSample(pixX, pixY, 2, blue) != 0) ||
00525 (_pGLImageBox->getImageSample(pixX, pixY, 3, alpha) != 0))
00526 txt = QString::fromAscii("x,y = %1 | %2 = %3")
00527 .arg(tr("outside image")).arg(tr("zoom")).arg(zoomFactor,0,'f',1);
00528 else
00529 txt = QString::fromAscii("x,y = %1,%2 | rgba = %3,%4,%5,%6 | %7 = %8")
00530 .arg(icX,0,'f',2).arg(icY,0,'f',2)
00531 .arg((int)red).arg((int)green).arg((int)blue).arg((int)alpha)
00532 .arg(tr("zoom")).arg(zoomFactor,0,'f',1);
00533 }
00534 else if ((colorFormat == IB_CF_BGRA32) ||
00535 (colorFormat == IB_CF_BGRA64))
00536 {
00537 double red, green, blue, alpha;
00538 if ((_pGLImageBox->getImageSample(pixX, pixY, 0, blue) != 0) ||
00539 (_pGLImageBox->getImageSample(pixX, pixY, 1, green) != 0) ||
00540 (_pGLImageBox->getImageSample(pixX, pixY, 2, red) != 0) ||
00541 (_pGLImageBox->getImageSample(pixX, pixY, 3, alpha) != 0))
00542 txt = QString::fromAscii("x,y = %1 | %2 = %3")
00543 .arg(tr("outside image")).arg(tr("zoom")).arg(zoomFactor,0,'f',1);
00544 else
00545 txt = QString::fromAscii("x,y = %1,%2 | rgba = %3,%4,%5,%6 | %7 = %8")
00546 .arg(icX,0,'f',2).arg(icY,0,'f',2)
00547 .arg((int)red).arg((int)green).arg((int)blue).arg((int)alpha)
00548 .arg(tr("zoom")).arg(zoomFactor,0,'f',1);
00549 }
00550
00551 return txt;
00552 }
00553
00554
00555 void ImageView::startDrag()
00556 {
00557 _pGLImageBox->fixBasePosCurr();
00558 dragStartWCx = _currX;
00559 dragStartWCy = _currY;
00560 }
00561
00562
00563 void ImageView::zoom(int prevX, int prevY, int currX, int currY)
00564 {
00565
00566 int dx = currX - prevX;
00567 int dy = currY - prevY;
00568 if (abs(dy) > abs(dx))
00569 {
00570
00571 int ICx, ICy;
00572 _pGLImageBox->getCentrePoint(ICx, ICy);
00573
00574
00575 double zoomFactorMultiplier = 1.05;
00576 if (currY > prevY)
00577 zoomFactorMultiplier = 0.95;
00578
00579
00580 _pGLImageBox->setZoomFactor(_pGLImageBox->getZoomFactor() * zoomFactorMultiplier, true, ICx, ICy);
00581 _pGLImageBox->redraw();
00582 }
00583 }
00584
00585
00586 void ImageView::select(int currX, int currY)
00587 {
00588
00589
00590 }
00591
00592
00593 void ImageView::addSelect(int currX, int currY)
00594 {
00595
00596
00597 }
00598
00599
00600
00601
00602 void ImageView::drawGraphics()
00603 {
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615 }
00616
00617 #include "moc_ImageView.cpp"
00618
00619