June 23, 2011

jesbb jesbb
Lab Rat
8 posts

Need Help on Copying an Image from one GraphicsView/GraphicsScene to Another

 

I’m working on a multitouch image viewer that has two graphics views: the main one for viewing the image, and another that stores thumbnails. I plan on either allowing users to drag and drop the thumbnails into the main graphics view (using one QGraphicsPixmapItem class) or allowing users to simply touch a thumbnail to copy it into the main graphics view (using two QGraphicsPixmapItem classes: one for the images and one for the thumbnails).

However, I don’t know how to take either approach. I don’t know if I can copy over data from the thumbnail class into the image class; I also don’t know how to approach the drag and drop operations with them.

Here’s the image class:

  1. #include "slice.h"
  2. #include <QtGui>
  3. #include <QTouchEvent>
  4.  
  5. slice::slice() : totalScaleFactor(1)
  6. {
  7.     setAcceptTouchEvents(true);
  8. }
  9.  
  10. bool slice::sceneEvent(QEvent *event)
  11. {
  12.     switch(event->type())
  13.     {
  14.     case QEvent::TouchBegin:
  15.     case QEvent::TouchUpdate:
  16.     case QEvent::TouchEnd:
  17.     {
  18.         QTouchEvent *touchEvent = static_cast<QTouchEvent *>(event);
  19.         QList<QTouchEvent::TouchPoint> touchPoints = touchEvent->touchPoints();
  20.         if(touchPoints.count() == 1)
  21.         {
  22.             const QTouchEvent::TouchPoint &touchPoint = touchPoints.first();
  23.             QLineF line(touchPoint.lastScenePos(), touchPoint.scenePos());
  24.             translate(line.dx(),line.dy());
  25.         }
  26.         else if(touchPoints.count() == 2)
  27.         {
  28.             const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.first();
  29.             const QTouchEvent::TouchPoint &touchPoint2 = touchPoints.last();
  30.  
  31.             qreal currentScaleFactor = QLineF(touchPoint1.pos(), touchPoint2.pos()).length()
  32.                     / QLineF(touchPoint1.startPos(), touchPoint2.startPos()).length();
  33.             if(touchEvent->touchPointStates() & Qt::TouchPointReleased)
  34.             {
  35.                 totalScaleFactor *= currentScaleFactor;
  36.                 currentScaleFactor = 1;
  37.             }
  38.             if((totalScaleFactor*currentScaleFactor) <= 0.8)
  39.             {
  40.                 totalScaleFactor = 0.8;
  41.             }
  42.             else if((totalScaleFactor*currentScaleFactor) >= 5.0)
  43.             {
  44.                 totalScaleFactor = 5.0;
  45.             }
  46.             else
  47.             {
  48.                 setScale(totalScaleFactor*currentScaleFactor);
  49.             }
  50.         }
  51.         else if(touchPoints.count() == 3)
  52.         {
  53.             const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.first();
  54.             const QTouchEvent::TouchPoint &touchPoint2 = touchPoints.last();
  55.  
  56.             QLineF line1(touchPoint1.lastScenePos(), touchPoint2.lastScenePos());
  57.             QLineF line2(touchPoint1.scenePos(), touchPoint2.scenePos());
  58.             rotate(line2.angleTo(line1));
  59.         }
  60.         return true;
  61.     }
  62.     default:
  63.         break;
  64.     }
  65.     return QGraphicsItem::sceneEvent(event);
  66. }

And here’s the thumbnail class:

  1. #include "thumbnail.h"
  2. #include "mainwindow.h"
  3. #include "slice.h"
  4. #include <QtGui>
  5. #include <QTouchEvent>
  6.  
  7. thumbnail::thumbnail()
  8. {
  9.     setAcceptTouchEvents(true);
  10.     scale(0.5, 0.5);
  11. }
  12.  
  13. bool thumbnail::sceneEvent(QEvent *event)
  14. {
  15.     switch(event->type())
  16.     {
  17.     case QEvent::TouchBegin:
  18.     case QEvent::TouchUpdate:
  19.     case QEvent::TouchEnd:
  20.     {
  21.         QTouchEvent *touchEvent = static_cast<QTouchEvent *>(event);
  22.         QList<QTouchEvent::TouchPoint> touchPoints = touchEvent->touchPoints();
  23.         if(touchPoints.count() == 1)
  24.         {
  25.             /*I don't know what to put here*/
  26.         }
  27.         return true;
  28.     }
  29.     default:
  30.         break;
  31.     }
  32.     return QGraphicsItem::sceneEvent(event);
  33. }

Finally, here are the relevant portions of the MainWindow class:

  1. MainWindow::MainWindow(QWidget *parent) :
  2.     QMainWindow(parent),
  3.     ui(new Ui::MainWindow)
  4. {
  5. .
  6. .
  7. .
  8.  
  9.     QGraphicsScene *scene;
  10.     QGraphicsScene *sliceScene;
  11.     scene = new QGraphicsScene;
  12.     sliceScene = new QGraphicsScene;
  13.     QPixmap item(QPixmap::fromImage(extractSlice(0,60)));
  14.     QPixmap item2(QPixmap::fromImage(extractSlice(0,50)));
  15.     slice *newSlice = new slice;
  16.     slice *newSlice2 = new slice;
  17.     newSlice->setPixmap(item);
  18.     newSlice2->setPixmap(item2);
  19.     scene->addItem(newSlice);
  20.     sliceScene->addItem(newSlice2);
  21.     ui->sliceView->setScene(sliceScene);
  22.     ui->graphicsView->setScene(scene);
  23.     ui->graphicsView->show();
  24. }
  25.  
  26. .
  27. .
  28. .
  29.  
  30. QImage MainWindow::extractSlice(int dimension, int sliceNumber)
  31. {
  32. .
  33. .
  34. .
  35.     IteratorType outputIt(newImage, newImage->GetBufferedRegion());
  36.     const int width = newImage->GetLargestPossibleRegion().GetSize()[0];
  37.     const int height = newImage->GetLargestPossibleRegion().GetSize()[1];
  38.     QImage *myQtImage = new QImage(width, height, QImage::Format_RGB32);
  39.     outputIt.Begin();
  40.     for(int i=0; i < height; ++i)
  41.     {
  42.         for(int j=0; j < width; ++j)
  43.         {
  44.             QRgb color;
  45.             color = qRgb(outputIt.Get(), outputIt.Get(), outputIt.Get());
  46.             myQtImage->setPixel(j,i,color);
  47.             ++outputIt;
  48.         }
  49.     }
  50.     return *myQtImage;
  51. }

Please let me know if there’s any other information I should provide.

0 replies

 
  ‹‹ Viewable but non-editable tables      Regarding Locking The File ››

You must log in to post a reply. Not a member yet? Register here!