📄 qgraphicsview.cpp
字号:
rubberBanding(false), rubberBandSelectionMode(Qt::IntersectsItemShape),#endif handScrolling(false), handScrollMotions(0), cacheMode(0), mustResizeBackgroundPixmap(true),#ifndef QT_NO_CURSOR hasStoredOriginalCursor(false),#endif lastDragDropEvent(0){}/*! \internal*/void QGraphicsViewPrivate::recalculateContentSize(){ Q_Q(QGraphicsView); QSize maxSize = q->maximumViewportSize(); int width = maxSize.width(); int height = maxSize.height(); QRectF viewRect = matrix.mapRect(q->sceneRect()); bool frameOnlyAround = (q->style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents, 0, q)); if (frameOnlyAround) { if (hbarpolicy == Qt::ScrollBarAlwaysOn) height -= frameWidth * 2; if (vbarpolicy == Qt::ScrollBarAlwaysOn) width -= frameWidth * 2; } // Adjust the maximum width and height of the viewport based on the width // of visible scroll bars. int scrollBarExtent = q->style()->pixelMetric(QStyle::PM_ScrollBarExtent, 0, q); if (frameOnlyAround) scrollBarExtent += frameWidth * 2; bool useHorizontalScrollBar = (viewRect.width() > width) && hbarpolicy != Qt::ScrollBarAlwaysOff; bool useVerticalScrollBar = (viewRect.height() > height) && vbarpolicy != Qt::ScrollBarAlwaysOff; if (useHorizontalScrollBar && !useVerticalScrollBar) { if (viewRect.height() > height - scrollBarExtent) useVerticalScrollBar = true; } if (useVerticalScrollBar && !useHorizontalScrollBar) { if (viewRect.width() > width - scrollBarExtent) useHorizontalScrollBar = true; } if (useHorizontalScrollBar && hbarpolicy != Qt::ScrollBarAlwaysOn) height -= scrollBarExtent; if (useVerticalScrollBar && vbarpolicy != Qt::ScrollBarAlwaysOn) width -= scrollBarExtent; // Setting the ranges of these scroll bars can/will cause the values to // change, and scrollContentsBy() will be called correspondingly. This // will reset the last center point. QPointF savedLastCenterPoint = lastCenterPoint; // Remember the former indent settings qreal oldLeftIndent = leftIndent; qreal oldTopIndent = topIndent; // If the whole scene fits horizontally, we center the scene horizontally, // and ignore the horizontal scroll bars. int left = int(qMax<qreal>(viewRect.left(), INT_MIN)); int right = int(qMin<qreal>(viewRect.right() - width, INT_MAX)); if (left >= right) { q->horizontalScrollBar()->setRange(0, 0); switch (alignment & Qt::AlignHorizontal_Mask) { case Qt::AlignLeft: leftIndent = -viewRect.left(); break; case Qt::AlignRight: leftIndent = width - viewRect.width() - viewRect.left() - 1; break; case Qt::AlignHCenter: default: leftIndent = width / 2 - (viewRect.left() + viewRect.right()) / 2; break; } } else { q->horizontalScrollBar()->setRange(left, right); q->horizontalScrollBar()->setPageStep(width); q->horizontalScrollBar()->setSingleStep(width / 20); leftIndent = 0; } // If the whole scene fits vertically, we center the scene vertically, and // ignore the vertical scroll bars. int top = int(qMax<qreal>(viewRect.top(), INT_MIN)); int bottom = int(qMin<qreal>(viewRect.bottom() - height, INT_MAX)); if (top >= bottom) { q->verticalScrollBar()->setRange(0, 0); switch (alignment & Qt::AlignVertical_Mask) { case Qt::AlignTop: topIndent = -viewRect.top(); break; case Qt::AlignBottom: topIndent = height - viewRect.height() - viewRect.top() - 1; break; case Qt::AlignVCenter: default: topIndent = height / 2 - (viewRect.top() + viewRect.bottom()) / 2; break; } } else { q->verticalScrollBar()->setRange(top, bottom); q->verticalScrollBar()->setPageStep(height); q->verticalScrollBar()->setSingleStep(height / 20); topIndent = 0; } // Restorethe center point from before the ranges changed. lastCenterPoint = savedLastCenterPoint; // Issue a full update if the indents change if (oldLeftIndent != leftIndent || oldTopIndent != topIndent) q->viewport()->update(); if (cacheMode & QGraphicsView::CacheBackground) { // Invalidate the background pixmap mustResizeBackgroundPixmap = true; }}/*! \internal*/void QGraphicsViewPrivate::centerView(QGraphicsView::ViewportAnchor anchor){ Q_Q(QGraphicsView); switch (anchor) { case QGraphicsView::AnchorUnderMouse: { if (q->underMouse()) { // Last scene pos: lastMouseMoveScenePoint // Current mouse pos: QPointF transformationDiff = q->mapToScene(q->viewport()->rect().center()) - q->mapToScene(q->mapFromGlobal(QCursor::pos())); q->centerOn(lastMouseMoveScenePoint + transformationDiff);; } else { q->centerOn(lastCenterPoint); } break; } case QGraphicsView::AnchorViewCenter: q->centerOn(lastCenterPoint); break; case QGraphicsView::NoAnchor: break; }}/*! \internal*/void QGraphicsViewPrivate::updateLastCenterPoint(){ Q_Q(QGraphicsView); lastCenterPoint = q->mapToScene(q->viewport()->rect().center());}/*! \internal Returns the horizontal scroll value (the X value of the left edge of the viewport).*/qint64 QGraphicsViewPrivate::horizontalScroll() const{ Q_Q(const QGraphicsView); qint64 horizontal = qint64(-leftIndent); if (q->isRightToLeft()) { if (!leftIndent) { horizontal += q->horizontalScrollBar()->minimum(); horizontal += q->horizontalScrollBar()->maximum(); horizontal -= q->horizontalScrollBar()->value(); } } else { horizontal += q->horizontalScrollBar()->value(); } return horizontal;}/*! \internal Returns the vertical scroll value (the X value of the top edge of the viewport).*/qint64 QGraphicsViewPrivate::verticalScroll() const{ Q_Q(const QGraphicsView); return qint64(q->verticalScrollBar()->value() - topIndent);}/*! \internal*/void QGraphicsViewPrivate::replayLastMouseEvent(){ Q_Q(QGraphicsView); if (!useLastMouseEvent || !scene) return; // Check if we need to replay the event QPointF newScenePos = q->mapToScene(lastMouseEvent.pos()); QGraphicsItem *item = scene->itemAt(newScenePos); QPointF newItemPos = item ? item->mapFromScene(newScenePos) : QPointF(); if (item == lastItemUnderCursor && newItemPos == lastItemUnderCursorPos) return; // Store updated item data lastItemUnderCursor = item; lastItemUnderCursorPos = newItemPos; QMouseEvent *mouseEvent = new QMouseEvent(QEvent::MouseMove, lastMouseEvent.pos(), lastMouseEvent.globalPos(), lastMouseEvent.button(), lastMouseEvent.buttons(), lastMouseEvent.modifiers()); QApplication::postEvent(q->viewport(), mouseEvent);}/*! \internal*/void QGraphicsViewPrivate::storeMouseEvent(QMouseEvent *event){ useLastMouseEvent = true; lastMouseEvent = QMouseEvent(QEvent::MouseMove, event->pos(), event->globalPos(), event->button(), event->buttons(), event->modifiers());}/*! \internal*/#ifndef QT_NO_RUBBERBANDQRegion QGraphicsViewPrivate::rubberBandRegion(const QWidget *widget, const QRect &rect) const{ QStyleHintReturnMask mask; QStyleOptionRubberBand option; option.initFrom(widget); option.rect = rect; option.opaque = false; option.shape = QRubberBand::Rectangle; QRegion tmp; tmp += rect; if (widget->style()->styleHint(QStyle::SH_RubberBand_Mask, &option, widget, &mask)) tmp &= mask.region; return tmp;}#endif/*! \internal*/#ifndef QT_NO_CURSORvoid QGraphicsViewPrivate::_q_setViewportCursor(const QCursor &cursor){ Q_Q(QGraphicsView); QWidget *viewport = q->viewport(); if (!hasStoredOriginalCursor) { hasStoredOriginalCursor = true; originalCursor = viewport->cursor(); } viewport->setCursor(cursor);}#endif/*! \internal*/#ifndef QT_NO_CURSORvoid QGraphicsViewPrivate::_q_unsetViewportCursor(){ Q_Q(QGraphicsView); foreach (QGraphicsItem *item, q->items(lastMouseEvent.pos())) { if (item->hasCursor()) { _q_setViewportCursor(item->cursor()); return; } } // Restore the original viewport cursor. hasStoredOriginalCursor = false; if (dragMode == QGraphicsView::ScrollHandDrag) q->viewport()->setCursor(Qt::OpenHandCursor); else q->viewport()->setCursor(originalCursor);}#endif/*! \internal*/void QGraphicsViewPrivate::storeDragDropEvent(const QGraphicsSceneDragDropEvent *event){ delete lastDragDropEvent; lastDragDropEvent = new QGraphicsSceneDragDropEvent(event->type()); lastDragDropEvent->setScenePos(event->scenePos()); lastDragDropEvent->setScreenPos(event->screenPos()); lastDragDropEvent->setButtons(event->buttons()); lastDragDropEvent->setModifiers(event->modifiers()); lastDragDropEvent->setPossibleActions(event->possibleActions()); lastDragDropEvent->setProposedAction(event->proposedAction()); lastDragDropEvent->setDropAction(event->dropAction()); lastDragDropEvent->setMimeData(event->mimeData()); lastDragDropEvent->setWidget(event->widget()); lastDragDropEvent->setSource(event->source());}/*! \internal*/void QGraphicsViewPrivate::populateSceneDragDropEvent(QGraphicsSceneDragDropEvent *dest, QDropEvent *source){#ifndef QT_NO_DRAGANDDROP Q_Q(QGraphicsView); dest->setScenePos(q->mapToScene(source->pos())); dest->setScreenPos(q->mapToGlobal(source->pos())); dest->setButtons(source->mouseButtons()); dest->setModifiers(source->keyboardModifiers()); dest->setPossibleActions(source->possibleActions()); dest->setProposedAction(source->proposedAction()); dest->setDropAction(source->dropAction()); dest->setMimeData(source->mimeData()); dest->setWidget(q->viewport()); dest->setSource(source->source());#else Q_UNUSED(dest) Q_UNUSED(source)#endif}/*! Constructs a QGraphicsView. \a parent is passed to QWidget's constructor.*/QGraphicsView::QGraphicsView(QWidget *parent) : QAbstractScrollArea(*new QGraphicsViewPrivate, parent){ setViewport(0); setAcceptDrops(true); setBackgroundRole(QPalette::Base); setAttribute(Qt::WA_InputMethodEnabled);}/*! Constructs a QGraphicsView and sets the visualized scene to \a scene. \a parent is passed to QWidget's constructor.*/QGraphicsView::QGraphicsView(QGraphicsScene *scene, QWidget *parent) : QAbstractScrollArea(*new QGraphicsViewPrivate, parent){ setScene(scene); setViewport(0); setAcceptDrops(true); setBackgroundRole(QPalette::Base); setAttribute(Qt::WA_InputMethodEnabled);}/*! Destructs the QGraphicsView object.*/QGraphicsView::~QGraphicsView(){ Q_D(QGraphicsView); if (d->scene) d->scene->d_func()->views.removeAll(this); delete d->lastDragDropEvent;}/*! \reimp*/QSize QGraphicsView::sizeHint() const{ Q_D(const QGraphicsView); if (d->scene) { QSizeF baseSize = d->matrix.mapRect(sceneRect()).size(); baseSize += QSizeF(d->frameWidth * 2, d->frameWidth * 2); return baseSize.boundedTo((3 * QApplication::desktop()->size()) / 4).toSize();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -