{
m_background_brush.setStyle(Qt::SolidPattern);
m_background_brush.
setColor(QColor(3,
24,
39));
Scene->setBackgroundBrush(m_background_brush);
//Set-up the view
setSceneRect(-500, -500, 1000, 1000);
}
void MyGraphicsView::SetCenter(const QPointF& centerPoint) {
//Get the rectangle of the visible area in scene coords
QRectF visibleArea
= mapToScene
(rect
()).
boundingRect();
//Get the scene area
QRectF sceneBounds
= sceneRect
();
double boundX, boundY, boundWidth, boundHeight;
if(centerPoint.x() >= 0)
{
boundX = visibleArea.width() / 2.0;
boundY = visibleArea.height() / 2.0;
boundWidth = (sceneBounds.width() - 2.0 * boundX);
boundHeight = (sceneBounds.height() - 2.0 * boundY);
} else
{
boundX = visibleArea.width() / 2.0;
boundY = visibleArea.height() / 2.0;
boundWidth = (sceneBounds.width() + 2.0 * boundX);
boundHeight = (sceneBounds.height() + 2.0 * boundY);
}
//The max boundary that the centerPoint can be to
QRectF bounds
(boundX, boundY, boundWidth, boundHeight
);
if(bounds.contains(centerPoint)) {
CurrentCenterPoint = centerPoint;
} else {
if(visibleArea.contains(sceneBounds)) {
CurrentCenterPoint = sceneBounds.center();
} else {
CurrentCenterPoint = centerPoint;
if(centerPoint.x() > bounds.x() + bounds.width()) {
qDebug()<<"The Clamping on the min X"<<bounds <<" and the clamp:"<< centerPoint;
CurrentCenterPoint.setX(bounds.x() + bounds.width());
} else if(centerPoint.x() < bounds.x()) {
CurrentCenterPoint.setX(bounds.x() );
}
if(centerPoint.y() > bounds.y() + bounds.height()) {
qDebug()<<"The Clamping on the min Y";
CurrentCenterPoint.setY(bounds.y() + bounds.height());
} else if(centerPoint.y() < bounds.y()) {
CurrentCenterPoint.setY(bounds.y() );
}
}
}
qDebug() <<"Update the scrollbars:" << CurrentCenterPoint;
centerOn(CurrentCenterPoint);
}
*/
void MyGraphicsView
::mousePressEvent(QMouseEvent* event
) { //For panning the view
LastPanPoint = event->pos();
setCursor(Qt::ClosedHandCursor);
}
/**
* Handles when the mouse button is released
*/
void MyGraphicsView
::mouseReleaseEvent(QMouseEvent* event
) { setCursor(Qt::OpenHandCursor);
}
/**
*Handles the mouse move event
*/
void MyGraphicsView
::mouseMoveEvent(QMouseEvent* event
) { if(!LastPanPoint.isNull()) {
//Get how much we panned
QPointF delta
= mapToScene
(LastPanPoint
) - mapToScene
(event
->pos
());
qDebug()<<"The Wheel Delta value:"<< delta;
LastPanPoint = event->pos();
//Update the center ie. do the pan
if(delta.x() < 0 )
{
SetCenter(GetCenter() - delta);
} else
SetCenter(GetCenter() + delta);
}
}
/**
* Zoom the view in and out.
*/
//Get the position of the mouse before scaling, in scene coords
QPointF pointBeforeScale
(mapToScene
(event
->pos
()));
//Get the original screen centerpoint
QPointF screenCenter
= GetCenter
();
//CurrentCenterPoint; //(visRect.center());
//Scale the view ie. do the zoom
double scaleFactor = 1.15; //How fast we zoom
if(event->delta() > 0) {
//Zoom in
scale(scaleFactor, scaleFactor);
} else {
//Zooming out
scale(1.0 / scaleFactor, 1.0 / scaleFactor);
}
//Get the position after scaling, in scene coords
QPointF pointAfterScale
(mapToScene
(event
->pos
()));
//Get the offset of how the screen moved
QPointF offset
= pointBeforeScale
- pointAfterScale;
//Adjust to the new center for correct zooming
QPointF newCenter
= screenCenter
+ offset;
SetCenter(newCenter);
}
QRectF visibleArea
= mapToScene
(rect
()).
boundingRect();
SetCenter(visibleArea.center());
//Call the subclass resize so the scrollbars are updated correctly
}