EditorView
::EditorView(QWidget* parent
) :{
setScene(scene);
setSceneRect(INT_MIN / 2, INT_MIN / 2, INT_MAX, INT_MAX);
item1->setRect(250.0, 250.0, 50.0, 50.0);
item2->setRect(150.0, 150.0, 50.0, 50.0);
setCursor(Qt::OpenHandCursor);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}
{
m_lastDragPos = event->pos();
}
{
QPointF delta
= mapToScene
(event
->pos
()) - mapToScene
(m_lastDragPos
);
PanView(delta);
m_lastDragPos = event->pos();
}
{
int delta = event->delta() / 120;
ScaleView(delta);
}
void EditorView
::PanView(QPointF delta
) {
QPoint viewCenter
(viewport
()->width
() / 2 - delta.
x(), viewport
()->height
() / 2 - delta.
y());
QPointF newCenter
= mapToScene
(viewCenter
);
centerOn(newCenter);
}
void EditorView::ScaleView(int delta)
{
bool zoomIn = (delta > 0);
delta = abs(delta);
ViewportAnchor oldAnchor = transformationAnchor();
for(int i = 0; i < delta; ++i)
{
if(zoomIn)
{
scale(1.10, 1.10);
}
else
{
scale(0.90, 0.90);
}
}
setTransformationAnchor(oldAnchor);
}
EditorView::EditorView(QWidget* parent) :
QGraphicsView(parent)
{
setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
QGraphicsScene* scene = new QGraphicsScene(this);
setScene(scene);
setSceneRect(INT_MIN / 2, INT_MIN / 2, INT_MAX, INT_MAX);
QGraphicsEllipseItem* item1 = new QGraphicsEllipseItem(0, scene);
item1->setRect(250.0, 250.0, 50.0, 50.0);
item1->setFlag(QGraphicsItem::ItemIsMovable);
QGraphicsEllipseItem* item2 = new QGraphicsEllipseItem(0, scene);
item2->setRect(150.0, 150.0, 50.0, 50.0);
item2->setFlag(QGraphicsItem::ItemIsMovable);
setCursor(Qt::OpenHandCursor);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}
void EditorView::mousePressEvent(QMouseEvent* event)
{
QGraphicsView::mousePressEvent(event);
m_lastDragPos = event->pos();
}
void EditorView::mouseMoveEvent(QMouseEvent* event)
{
QPointF delta = mapToScene(event->pos()) - mapToScene(m_lastDragPos);
PanView(delta);
m_lastDragPos = event->pos();
}
void EditorView::wheelEvent(QWheelEvent* event)
{
int delta = event->delta() / 120;
ScaleView(delta);
}
void EditorView::PanView(QPointF delta)
{
QPoint viewCenter(viewport()->width() / 2 - delta.x(), viewport()->height() / 2 - delta.y());
QPointF newCenter = mapToScene(viewCenter);
centerOn(newCenter);
}
void EditorView::ScaleView(int delta)
{
bool zoomIn = (delta > 0);
delta = abs(delta);
ViewportAnchor oldAnchor = transformationAnchor();
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
for(int i = 0; i < delta; ++i)
{
if(zoomIn)
{
scale(1.10, 1.10);
}
else
{
scale(0.90, 0.90);
}
}
setTransformationAnchor(oldAnchor);
}
To copy to clipboard, switch view to plain text mode
Bookmarks