Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <QDebug>
- #include <QGraphicsScene>
- #include <QWheelEvent>
- #include <QGraphicsItem>
- #include <QGenericMatrix>
- #include <QApplication>
- #include <math.h>
- #include "MyGraphicsView.h"
- MyGraphicsView::MyGraphicsView(QWidget* iQWidget):
- QGraphicsView(iQWidget),
- Mode(SELECTION),
- ZoomBox(NULL),
- Panning(false),
- MaxZoomFactor(100)
- {
- Q_INIT_RESOURCE(MyGraphicsView);
- Scene = new QGraphicsScene(this);
- Scene->setItemIndexMethod(QGraphicsScene::NoIndex);
- setScene(Scene);
- setCacheMode(CacheBackground);
- setViewportUpdateMode(BoundingRectViewportUpdate);
- setRenderHint(QPainter::Antialiasing);
- setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
- setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
- setResizeAnchor(AnchorViewCenter);
- setSceneRect(0,0,500,500);
- scale(qreal(1.0), qreal(1.0));
- setWindowTitle(tr("QT Graphics View Test"));
- //Add Control Buttons
- ControlPanelBackground = scene()->addRect(QRectF(size().width() - 100,0,100,5),QPen(),QBrush(QColor(255,255,255, 0)));
- ControlPanelBackground->setZValue(1);
- QWidget *ControlFrame = new QWidget();
- ControlFrame->setMinimumWidth(60);
- ControlFrame->setMinimumHeight(28);
- ModeButtonGroup = new QButtonGroup(ControlFrame);
- ControlFrame->setLayout(new QHBoxLayout());
- ControlFrame->layout()->setSpacing(0);
- ControlFrame->layout()->setMargin(0);
- ButtonSelect = new QPushButton(ControlFrame);
- ButtonSelect->setText("Select");
- ButtonSelect->setMinimumHeight(24);
- ButtonSelect->setCheckable(true);
- ButtonSelect->setChecked(true);
- ControlFrame->layout()->addWidget(ButtonSelect);
- ModeButtonGroup->addButton(ButtonSelect);
- ButtonZoom = new QPushButton(ControlFrame);
- ButtonZoom->setText("Zoom");
- ButtonZoom->setMinimumHeight(24);
- ButtonZoom->setCheckable(true);
- ControlFrame->layout()->addWidget(ButtonZoom);
- ModeButtonGroup->addButton(ButtonZoom);
- ((QHBoxLayout *)ControlFrame->layout())->addSpacing(6);
- ButtonFitToView = new QPushButton(ControlFrame);
- ButtonFitToView->setText("Zoom to Max");
- ButtonFitToView->setMinimumHeight(24);
- ControlFrame->layout()->addWidget(ButtonFitToView);
- FrameGraphics = scene()->addWidget(ControlFrame);
- FrameGraphics->setParentItem(ControlPanelBackground);
- ControlPanelBackground->setRect(FrameGraphics->boundingRect());
- connect(ButtonSelect, SIGNAL(clicked()), this, SLOT(vModeSelected()));
- connect(ButtonZoom, SIGNAL(clicked()), this, SLOT(vModeSelected()));
- connect(ButtonFitToView, SIGNAL(clicked()), this, SLOT(vFullExtents()));
- PixmapItem = Scene->addPixmap(QPixmap(":/new/prefix1/apple.png"));
- PixmapItem->setPos(100, 100);
- scaleView(1.0);
- }
- MyGraphicsView::~MyGraphicsView()
- {
- }
- void MyGraphicsView::vModeSelected()
- {
- QAbstractButton *_pButton = ModeButtonGroup->checkedButton();
- if (_pButton == ButtonSelect)
- {
- vSetMode(SELECTION);
- }
- else if (_pButton == ButtonZoom)
- {
- vSetMode(ZOOM_BOX);
- }
- emit vOnModeChanged(getMode());
- }
- // SETTING THE CURSORS FOR EACH MODE
- // THIS IS TRIGGERED FROM THE BUTTONS INSIDE THE GRAPHICS VIEW
- void MyGraphicsView::vSetMode(ModeEnum a_Mode)
- {
- Mode = a_Mode;
- // MUST BE CALLED SO THAT THE CURSOR CAN BE CHANGED BACK TO THE ARROW ONCE WE'VE CHANGED
- // IT TO THE CUSTOM CURSOR
- this->unsetCursor();
- if (a_Mode == SELECTION)
- {
- viewport()->setCursor(Qt::ArrowCursor);
- ButtonSelect->setChecked(true);
- }
- else if (a_Mode == ZOOM_BOX)
- {
- QCursor *_pCursor = new QCursor(QPixmap(":/new/prefix1/zoom.png"));
- viewport()->setCursor(*_pCursor);
- setCursor(*_pCursor);
- ButtonZoom->setChecked(true);
- }
- // NEEDED TO ENSURE THAT THE CURSOR IS ACTUALLY CHANGED
- setDragMode(QGraphicsView::ScrollHandDrag);
- setDragMode(QGraphicsView::NoDrag);
- }
- // SETTING THE CURSOR FROM A TRIGGER OUTSIDE THE GRAPHICS VIEW
- // THIS IS CALLED FROM THE BUTTON ABOVE THE VIEW
- void MyGraphicsView::vStartHiddenMode()
- {
- TempMode = Mode;
- vSetMode(QUERY);
- QCursor *_pCursor = new QCursor(QPixmap(":/new/prefix1/pencil.png"));
- viewport()->setCursor(*_pCursor);
- setCursor(*_pCursor);
- setFocus();
- }
- void MyGraphicsView::vFullExtents()
- {
- QRectF unity = matrix().mapRect(QRectF(0, 0, 1, 1));
- scaleView(1 / unity.width());
- }
- void MyGraphicsView::wheelEvent(QWheelEvent *event)
- {
- scaleView(pow((double)2, event->delta() / 240.0));
- }
- void MyGraphicsView::resizeEvent(QResizeEvent *event)
- {
- setSceneRect(0,0, this->width(), this->height());
- this->setBackgroundBrush(QBrush(QColor(26, 51, 71)));
- int wNewSizeWidth = event->size().width();
- int wNewSizeHeight = event->size().height();
- int _width = size().width();
- QPoint wPointScreen(_width <= 250 ? 0 : _width- 250,5);
- QPointF wPointScene = mapToScene(wPointScreen);
- qreal ActualFactor = matrix().mapRect(QRectF(0, 0, 1, 1)).width();
- if(ControlPanelBackground)
- {
- ControlPanelBackground->setPos(wPointScene);
- ControlPanelBackground->setScale(1/ActualFactor);
- }
- }
- void MyGraphicsView::mousePressEvent(QMouseEvent *a_MouseEvent)
- {
- // Since the map should drag on the middle mouse, we make QGraphicsView believe that left button is pressed
- if(a_MouseEvent->button() == Qt::MidButton)
- {
- Panning = true;
- setDragMode(QGraphicsView::ScrollHandDrag);
- QMouseEvent* _Event = new QMouseEvent(a_MouseEvent->type(),a_MouseEvent->pos(),Qt::LeftButton,(a_MouseEvent->buttons() | Qt::MidButton),a_MouseEvent->modifiers());
- QGraphicsView::mousePressEvent(_Event);
- return;
- }
- Panning = false;
- if(a_MouseEvent->button() == Qt::LeftButton && (Mode == ZOOM_BOX))
- {
- if(ZoomBox == NULL)
- {
- QPen wPen;
- wPen.setColor(QColor("Red"));
- wPen.setStyle(Qt::DashLine);
- ZoomBox = Scene->addRect(0,0,0,0,wPen);
- }
- ZoomX = a_MouseEvent->x();
- ZoomY = a_MouseEvent->y();
- ZoomBox->setRect(ZoomX, ZoomY,0,0);
- }
- else if(a_MouseEvent->button() == Qt::LeftButton && Mode == QUERY)
- {
- double _Lat, _Lon;
- int _ScreenX = a_MouseEvent->x();
- int _ScreenY = a_MouseEvent->y();
- }
- QGraphicsView::mousePressEvent(a_MouseEvent);
- }
- void MyGraphicsView::mouseReleaseEvent(QMouseEvent *a_MouseEvent)
- {
- // Since the view should drag on the middle mouse, we make QGraphicsView believe that left button is released
- if(a_MouseEvent->button() == Qt::MidButton)
- {
- Panning = false;
- QMouseEvent* _Event = new QMouseEvent(a_MouseEvent->type(),a_MouseEvent->pos(),Qt::LeftButton,(a_MouseEvent->buttons() | Qt::MidButton),a_MouseEvent->modifiers());
- QGraphicsView::mouseReleaseEvent(_Event);
- setDragMode(QGraphicsView::NoDrag);
- return;
- }
- // Once we release the mouse in the query mode we turn it off and return to the previous mode.
- if (a_MouseEvent->button() == Qt::LeftButton && Mode == QUERY)
- {
- vSetMode(TempMode);
- }
- if(ZoomBox && Mode == ZOOM_BOX)
- {
- Scene->removeItem(ZoomBox);
- delete ZoomBox;
- ZoomBox = NULL;
- }
- setDragMode(QGraphicsView::NoDrag);
- QGraphicsView::mouseReleaseEvent(a_MouseEvent);
- }
- void MyGraphicsView::mouseMoveEvent(QMouseEvent * a_MouseEvent)
- {
- if(ZoomBox && (Mode == ZOOM_BOX))
- {
- int _ScreenX = a_MouseEvent->x();
- int _ScreenY = a_MouseEvent->y();
- double _x = ZoomX;
- double _y = ZoomY;
- double _x2 = _x;
- double _y2 = _y;
- double _width, _height;
- _height = _ScreenY - _y;
- _width = _ScreenX - _x;
- ZoomBox->setRect(_x2,_y2,_width, _height);
- }
- QGraphicsView::mouseMoveEvent(a_MouseEvent);
- }
- void MyGraphicsView::mouseDoubleClickEvent( QMouseEvent *event )
- {
- if(Mode != SELECTION)
- {
- mousePressEvent(event);
- }
- else
- {
- QGraphicsView::mouseDoubleClickEvent(event);
- }
- }
- void MyGraphicsView::scaleView(qreal scaleFactor)
- {
- QRectF _Rect = matrix().mapRect(QRectF(0, 0, 1, 1));
- qreal _ActualFactor = matrix().mapRect(QRectF(0, 0, 1, 1)).width();
- qreal _NewFactor = matrix().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
- // Limit zoom factor to 50...
- if(_NewFactor>MaxZoomFactor)
- {
- return;
- }
- scale(scaleFactor, scaleFactor);
- int _width = size().width();
- QPoint wPointScreen(_width <= 250 ? 0 : _width- 250,5);
- QPointF wPointScene = mapToScene(wPointScreen);
- qreal ActualFactor = matrix().mapRect(QRectF(0, 0, 1, 1)).width();
- if(ControlPanelBackground)
- {
- ControlPanelBackground->setPos(wPointScene);
- ControlPanelBackground->setScale(1/ActualFactor);
- }
- }
Add Comment
Please, Sign In to add comment