⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qabstractscrollarea.cpp

📁 qt-x11-opensource-src-4.1.4.tar.gz源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file.  Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qabstractscrollarea.h"#ifndef QT_NO_SCROLLAREA#include "qscrollbar.h"#include "qapplication.h"#include "qstyle.h"#include "qstyleoption.h"#include "qevent.h"#include "qabstractscrollarea_p.h"#include <qwidget.h>#include <qdebug.h>#ifdef Q_WS_MAC#include <qmacstyle_mac.h>#endif/*!    \class QAbstractScrollArea qabstractscrollarea.h    \brief The QAbstractScrollArea widget provides a scrolling area with    on-demand scroll bars.    \ingroup abstractwidgets    QAbstractScrollArea is a low-level abstraction of a scrolling area. It gives    you full control of the scroll bars, at the cost of simplicity. In    most cases, using a QScrollArea is preferable.    QAbstractScrollArea's central child widget is the scrolling area itself,    called viewport(). The viewport widget uses all available    space. Next to the viewport is a vertical scroll bar (accessible    with verticalScrollBar()), and below a horizontal scroll bar    (accessible with horizontalScrollBar()). Each scroll bar can be    either visible or hidden, depending on the scroll bar's policy    (see \l verticalScrollBarPolicy and \l horizontalScrollBarPolicy).    When a scroll bar is hidden, the viewport expands in order to    cover all available space. When a scroll bar becomes visible    again, the viewport shrinks in order to make room for the scroll    bar.    With a scroll bar policy of Qt::ScrollBarAsNeeded (the default),    QAbstractScrollArea shows scroll bars when those provide a non-zero    scrolling range, and hides them otherwise. You control the range    of each scroll bar with QAbstractSlider::setRange().    In order to track scroll bar movements, reimplement the virtual    function scrollContentsBy(). In order to fine-tune scrolling    behavior, connect to a scroll bar's    QAbstractSlider::actionTriggered() signal and adjust the \l    QAbstractSlider::sliderPosition as you wish.    It is possible to reserve a margin area around the viewport, see    setViewportMargins(). The feature is mostly used to place a    QHeaderView widget above or beside the scrolling area.    For convenience, QAbstractScrollArea makes all viewport events available in    the virtual viewportEvent() handler.  QWidget's specialised    handlers are remapped to viewport events in the cases where this    makes sense. The remapped specialised handlers are: paintEvent(),    mousePressEvent(), mouseReleaseEvent(), mouseDoubleClickEvent(),    mouseMoveEvent(), wheelEvent(), dragEnterEvent(), dragMoveEvent(),    dragLeaveEvent(), dropEvent(), contextMenuEvent().  and    resizeEvent().*/inline  bool QAbstractScrollAreaPrivate::viewportEvent(QEvent *e){ Q_Q(QAbstractScrollArea); return q->viewportEvent(e); }class QAbstractScrollAreaViewport : public QWidget{    Q_OBJECTpublic:    QAbstractScrollAreaViewport(QWidget *parent):QWidget(parent){ setObjectName(QLatin1String("qt_scrollarea_viewport")); }    bool event(QEvent *e);    friend class QAbstractScrollArea;};bool QAbstractScrollAreaViewport::event(QEvent *e) {    if (QAbstractScrollArea* viewport = qobject_cast<QAbstractScrollArea*>(parentWidget()))        return ((QAbstractScrollAreaPrivate*)((QAbstractScrollAreaViewport*)viewport)->d_ptr)->viewportEvent(e);    return QWidget::event(e);}QAbstractScrollAreaPrivate::QAbstractScrollAreaPrivate()    :hbar(0), vbar(0), vbarpolicy(Qt::ScrollBarAsNeeded), hbarpolicy(Qt::ScrollBarAsNeeded),     viewport(0), left(0), top(0), right(0), bottom(0),     xoffset(0), yoffset(0){}void QAbstractScrollAreaPrivate::init(){    Q_Q(QAbstractScrollArea);    hbar = new QScrollBar(Qt::Horizontal, q);    hbar->setRange(0,0);    hbar->setVisible(false);    QObject::connect(hbar, SIGNAL(valueChanged(int)), q, SLOT(_q_hslide(int)));    QObject::connect(hbar, SIGNAL(rangeChanged(int,int)), q, SLOT(_q_showOrHideScrollBars()), Qt::QueuedConnection);    vbar = new QScrollBar(Qt::Vertical, q);    vbar->setRange(0,0);    vbar->setVisible(false);    QObject::connect(vbar, SIGNAL(valueChanged(int)), q, SLOT(_q_vslide(int)));    QObject::connect(vbar, SIGNAL(rangeChanged(int,int)), q, SLOT(_q_showOrHideScrollBars()), Qt::QueuedConnection);    viewport = new QAbstractScrollAreaViewport(q);    viewport->setBackgroundRole(QPalette::Base);    viewport->setAutoFillBackground(true);    viewport->setFocusProxy(q);    q->setFocusPolicy(Qt::WheelFocus);    q->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);    q->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);    layoutChildren();}void QAbstractScrollAreaPrivate::layoutChildren(){    Q_Q(QAbstractScrollArea);    bool needh = (hbarpolicy == Qt::ScrollBarAlwaysOn                  || (hbarpolicy == Qt::ScrollBarAsNeeded && hbar->minimum() < hbar->maximum()));    bool needv = (vbarpolicy == Qt::ScrollBarAlwaysOn                  || (vbarpolicy == Qt::ScrollBarAsNeeded && vbar->minimum() < vbar->maximum()));    int hsbExt = hbar->sizeHint().height();    int vsbExt = vbar->sizeHint().width();    QRect vr = q->rect();    QStyleOption opt(0);    opt.init(q);    int extra = 0;    if (q->style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents, &opt, q))        extra = q->style()->pixelMetric(QStyle::PM_DefaultFrameWidth) * 2;// If the scrollbars are at the very right and bottom of the window we// move their positions to be alligned with the size grip.#ifdef Q_WS_MAC    // Use small scrollbars for tool windows.    const QMacStyle::WidgetSizePolicy hpolicy = QMacStyle::widgetSizePolicy(hbar);    const QMacStyle::WidgetSizePolicy vpolicy = QMacStyle::widgetSizePolicy(vbar);    if (q->window()->windowType() == Qt::Tool) {        if (hpolicy != QMacStyle::SizeSmall)            QMacStyle::setWidgetSizePolicy(hbar, QMacStyle::SizeSmall);        if (vpolicy != QMacStyle::SizeSmall)            QMacStyle::setWidgetSizePolicy(vbar, QMacStyle::SizeSmall);    } else {        if (hpolicy != QMacStyle::SizeDefault)            QMacStyle::setWidgetSizePolicy(hbar, QMacStyle::SizeDefault);        if (vpolicy != QMacStyle::SizeDefault)            QMacStyle::setWidgetSizePolicy(vbar, QMacStyle::SizeDefault);    }    // Get the size of the size-grip from the style.    const int magicPixelOffset = -2;    const int sizeGripSize = q->style()->pixelMetric(QStyle::PM_SizeGripSize, &opt, q) + magicPixelOffset;    // Get coordiantes for the bottom-right corner of the scroll area and its window.    const QPoint scrollAreaBottomRight = q->mapTo(q->window(), vr.bottomRight());    const QPoint windowBottomRight = q->window()->rect().bottomRight();    const QPoint offset = windowBottomRight - scrollAreaBottomRight;    QRect frameRect = vr;    if (needh)        frameRect.setBottom(frameRect.bottom() - hsbExt - extra);    if (needv)        frameRect.setRight(frameRect.right() - vsbExt - extra);    // Set horizontal scrollbar geometry.    if (needh) {        QRect hsbRect(0, frameRect.bottom() + 1 + extra, frameRect.width(), hsbExt);        QRect visualHsbRect = QStyle::visualRect(opt.direction, opt.rect, hsbRect);        if (offset.manhattanLength() < sizeGripSize)            if (opt.direction == Qt::RightToLeft)                visualHsbRect.setWidth(visualHsbRect.width() - sizeGripSize);            else                visualHsbRect.setWidth(vr.width() - sizeGripSize);        hbar->setGeometry(visualHsbRect);    }    // Set vertical scrollbar geometry.    if (needv) {        QRect vsbRect(frameRect.right() + 1 + extra, 0, vsbExt, frameRect.height());        if (offset.manhattanLength() < sizeGripSize && opt.direction == Qt::LeftToRight)            vsbRect.setHeight(vr.height() - sizeGripSize);        vbar->setGeometry(QStyle::visualRect(opt.direction, opt.rect, vsbRect));   }    q->setFrameRect(QStyle::visualRect(opt.direction, opt.rect, frameRect));    vr = q->contentsRect();#else    if (q->style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents, &opt, q)) {        QRect fr = vr;        if (needh) {            fr.setBottom(fr.bottom() - hsbExt - extra);            hbar->setGeometry(QStyle::visualRect(opt.direction, opt.rect, QRect(0, fr.bottom() + 1 + extra, fr.width() - (needv?(vsbExt+extra):0), hsbExt)));        }        if (needv) {            fr.setRight(fr.right() - vsbExt - extra);            vbar->setGeometry(QStyle::visualRect(opt.direction, opt.rect, QRect(fr.right() + 1 + extra, 0, vsbExt, fr.height())));        }        q->setFrameRect(QStyle::visualRect(opt.direction, opt.rect, fr));        vr = q->contentsRect();    } else {        q->setFrameRect(vr);        vr = q->contentsRect();        if (needh) {            vr.setBottom(vr.bottom() - hsbExt);            hbar->setGeometry(QStyle::visualRect(opt.direction, opt.rect, QRect(vr.left(), vr.bottom() + 1, vr.width() - (needv?vsbExt:0), hsbExt)));        }        if (needv) {            vr.setRight(vr.right() - vsbExt);            vbar->setGeometry(QStyle::visualRect(opt.direction, opt.rect, QRect(vr.right() + 1, vr.top(), vsbExt, vr.height())));        }        vr = QStyle::visualRect(opt.direction, opt.rect, vr);    }#endif    hbar->setVisible(needh);    vbar->setVisible(needv);    vr.adjust(left, top, -right, -bottom);    viewport->setGeometry(vr); // resize the viewport last}/*!    \internal    Creates a new QAbstractScrollAreaPrivate, \a dd with the given \a parent.*/QAbstractScrollArea::QAbstractScrollArea(QAbstractScrollAreaPrivate &dd, QWidget *parent)    :QFrame(dd, parent){    Q_D(QAbstractScrollArea);    d->init();}/*!    Constructs a viewport.    The \a parent arguments is sent to the QWidget constructor.*/QAbstractScrollArea::QAbstractScrollArea(QWidget *parent)    :QFrame(*new QAbstractScrollAreaPrivate, parent){    Q_D(QAbstractScrollArea);    d->init();}/*!  Destroys the viewport. */QAbstractScrollArea::~QAbstractScrollArea(){}/*!    Returns the viewport widget.    Use the QScrollBar::widget() function to retrieve the contents of    the viewport widget.    \sa QScrollArea::widget()*/QWidget *QAbstractScrollArea::viewport() const{    Q_D(const QAbstractScrollArea);    return d->viewport;}/*!Returns the size of the viewport as if the scroll bars had no validscrolling range.*/// ### still thinking about the nameQSize QAbstractScrollArea::maximumViewportSize() const{    Q_D(const QAbstractScrollArea);    int hsbExt = d->hbar->sizeHint().height();    int vsbExt = d->vbar->sizeHint().width();    int f = 2 * d->frameWidth;    QSize max = size() - QSize(f + d->left + d->right, f + d->top + d->bottom);    if (d->vbarpolicy == Qt::ScrollBarAlwaysOn)        max.rwidth() -= vsbExt;    if (d->hbarpolicy == Qt::ScrollBarAlwaysOn)        max.rheight() -= hsbExt;    return max;}/*!    \property QAbstractScrollArea::verticalScrollBarPolicy    \brief the policy for the vertical scroll bar    The default policy is Qt::ScrollBarAsNeeded.    \sa horizontalScrollBarPolicy*/Qt::ScrollBarPolicy QAbstractScrollArea::verticalScrollBarPolicy() const{    Q_D(const QAbstractScrollArea);    return d->vbarpolicy;}void QAbstractScrollArea::setVerticalScrollBarPolicy(Qt::ScrollBarPolicy policy){    Q_D(QAbstractScrollArea);    d->vbarpolicy = policy;    if (isVisible())        d->layoutChildren();}/*!  Returns the vertical scroll bar.  \sa verticalScrollBarPolicy, horizontalScrollBar() */QScrollBar *QAbstractScrollArea::verticalScrollBar() const{    Q_D(const QAbstractScrollArea);    return d->vbar;}/*!    \property QAbstractScrollArea::horizontalScrollBarPolicy    \brief the policy for the horizontal scroll bar    The default policy is Qt::ScrollBarAsNeeded.    \sa verticalScrollBarPolicy*/Qt::ScrollBarPolicy QAbstractScrollArea::horizontalScrollBarPolicy() const{    Q_D(const QAbstractScrollArea);    return d->hbarpolicy;}void QAbstractScrollArea::setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy policy){    Q_D(QAbstractScrollArea);    d->hbarpolicy = policy;    if (isVisible())        d->layoutChildren();}/*!  Returns the horizontal scroll bar.  \sa horizontalScrollBarPolicy, verticalScrollBar() */QScrollBar *QAbstractScrollArea::horizontalScrollBar() const{    Q_D(const QAbstractScrollArea);    return d->hbar;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -