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

📄 qscrollbar.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************** Copyright (C) 1992-2007 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://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** 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 "qapplication.h"#include "qcursor.h"#include "qevent.h"#include "qpainter.h"#include "qscrollbar.h"#include "qstyle.h"#include "qstyleoption.h"#include "qmenu.h"#ifndef QT_NO_SCROLLBAR#ifndef QT_NO_ACCESSIBILITY#include "qaccessible.h"#endif#include <limits.h>#include "qabstractslider_p.h"/*!    \class QScrollBar    \brief The QScrollBar widget provides a vertical or horizontal scroll bar.    \ingroup basicwidgets    A scroll bar is a control that enables the user to access parts of a    document that is larger than the widget used to display it. It provides    a visual indication of the user's current position within the document    and the amount of the document that is visible. Scroll bars are usually    equipped with other controls that enable more accurate navigation.    Qt displays scroll bars in a way that is appropriate for each platform.    If you need to provide a scrolling view onto another widget, it may be    more convenient to use the QScrollArea class because this provides a    viewport widget and scroll bars. QScrollBar is useful if you need to    implement similar functionality for specialized widgets using QAbstractScrollArea;    for example, if you decide to subclass QAbstractItemView.    For most other situations where a slider control is used to obtain a value    within a given range, the QSlider class may be more appropriate for your    needs.    \table    \row \i \image qscrollbar-picture.png    \i Scroll bars typically include four separate controls: a slider,    scroll arrows, and a page control.    \list    \i a. The slider provides a way to quickly go to any part of the    document, but does not support accurate navigation within large    documents.    \i b. The scroll arrows are push buttons which can be used to accurately    navigate to a particular place in a document. For a vertical scroll bar    connected to a text editor, these typically move the current position one    "line" up or down, and adjust the position of the slider by a small    amount. In editors and list boxes a "line" might mean one line of text;    in an image viewer it might mean 20 pixels.    \i c. The page control is the area over which the slider is dragged (the    scroll bar's background). Clicking here moves the scroll bar towards    the click by one "page". This value is usually the same as the length of    the slider.    \endlist    \endtable    Each scroll bar has a value that indicates how far the slider is from    the start of the scroll bar; this is obtained with value() and set    with setValue(). This value always lies within the range of values    defined for the scroll bar, from \l{QAbstractSlider::minimum()}{minimum()}    to \l{QAbstractSlider::minimum()}{maximum()} inclusive. The range of    acceptable values can be set with setMinimum() and setMaximum().    At the minimum value, the top edge of the slider (for a vertical scroll    bar) or left edge (for a horizontal scroll bar) will be at the top (or    left) end of the scroll bar. At the maximum value, the bottom (or right)    edge of the slider will be at the bottom (or right) end of the scroll bar.    The length of the slider is usually related to the value of the page step,    and typically represents the proportion of the document area shown in a    scrolling view. The page step is the amount that the value changes by    when the user presses the \key{Page Up} and \key{Page Down} keys, and is    set with setPageStep(). Smaller changes to the value defined by the    line step are made using the cursor keys, and this quantity is set with    setLineStep().    Note that the range of values used is independent of the actual size    of the scroll bar widget. You do not need to take this into account when    you choose values for the range and the page step.    The range of values specified for the scroll bar are often determined    differently to those for a QSlider because the length of the slider    needs to be taken into account. If we have a document with 100 lines,    and we can only show 20 lines in a widget, we may wish to construct a    scroll bar with a page step of 20, a minimum value of 0, and a maximum    value of 80. This would give us a scroll bar with five "pages".    \table    \row \i \inlineimage qscrollbar-values.png    \i The relationship between a document length, the range of values used    in a scroll bar, and the page step is simple in many common situations.    The scroll bar's range of values is determined by subtracting a    chosen page step from some value representing the length of the document.    In such cases, the following equation is useful:    \e{document length} = maximum() - minimum() + pageStep().    \endtable    QScrollBar only provides integer ranges. Note that although    QScrollBar handles very large numbers, scroll bars on current    screens cannot usefully represent ranges above about 100,000 pixels.    Beyond that, it becomes difficult for the user to control the    slider using either the keyboard or the mouse, and the scroll    arrows will have limited use.    ScrollBar inherits a comprehensive set of signals from QAbstractSlider:    \list    \i \l{QAbstractSlider::valueChanged()}{valueChanged()} is emitted when the       scroll bar's value has changed. The tracking() determines whether this       signal is emitted during user interaction.    \i \l{QAbstractSlider::rangeChanged()}{rangeChanged()} is emitted when the       scroll bar's range of values has changed.    \i \l{QAbstractSlider::sliderPressed()}{sliderPressed()} is emitted when       the user starts to drag the slider.    \i \l{QAbstractSlider::sliderMoved()}{sliderMoved()} is emitted when the user       drags the slider.    \i \l{QAbstractSlider::sliderReleased()}{sliderReleased()} is emitted when       the user releases the slider.    \i \l{QAbstractSlider::actionTriggered()}{actionTriggered()} is emitted       when the scroll bar is changed by user interaction or via the       \l{QAbstractSlider::triggerAction()}{triggerAction()} function.    \endlist    A scroll bar can be controlled by the keyboard, but it has a    default focusPolicy() of Qt::NoFocus. Use setFocusPolicy() to    enable keyboard interaction with the scroll bar:    \list         \i Left/Right move a horizontal scroll bar by one single step.         \i Up/Down move a vertical scroll bar by one single step.         \i PageUp moves up one page.         \i PageDown moves down one page.         \i Home moves to the start (mininum).         \i End moves to the end (maximum).     \endlist    The slider itself can be controlled by using the    \l{QAbstractSlider::triggerAction()}{triggerAction()} function to simulate    user interaction with the scroll bar controls. This is useful if you have    many different widgets that use a common range of values.    Most GUI styles use the pageStep() value to calculate the size of the    slider.    \table 100%    \row \o \inlineimage macintosh-horizontalscrollbar.png Screenshot of a Macintosh style scroll bar         \o A scroll bar shown in the \l{Macintosh Style Widget Gallery}{Macintosh widget style}.    \row \o \inlineimage windowsxp-horizontalscrollbar.png Screenshot of a Windows XP style scroll bar         \o A scroll bar shown in the \l{Windows XP Style Widget Gallery}{Windows XP widget style}.    \row \o \inlineimage plastique-horizontalscrollbar.png Screenshot of a Plastique style scroll bar         \o A scroll bar shown in the \l{Plastique Style Widget Gallery}{Plastique widget style}.    \endtable    \sa QScrollArea, QSlider, QDial, QSpinBox, {fowler}{GUI Design Handbook: Scroll Bar}, {Sliders Example}*/class QScrollBarPrivate : public QAbstractSliderPrivate{    Q_DECLARE_PUBLIC(QScrollBar)public:    QStyle::SubControl pressedControl;    bool pointerOutsidePressedControl;    int clickOffset, snapBackPosition;    void activateControl(uint control, int threshold = 500);    int pixelPosToRangeValue(int pos) const;    void init();    bool updateHoverControl(const QPoint &pos);    QStyle::SubControl newHoverControl(const QPoint &pos);    QStyle::SubControl hoverControl;    QRect hoverRect;};bool QScrollBarPrivate::updateHoverControl(const QPoint &pos){    Q_Q(QScrollBar);    QRect lastHoverRect = hoverRect;    QStyle::SubControl lastHoverControl = hoverControl;    bool doesHover = q->testAttribute(Qt::WA_Hover);    if (lastHoverControl != newHoverControl(pos) && doesHover) {        q->update(lastHoverRect);        q->update(hoverRect);        return true;    }    return !doesHover;}QStyle::SubControl QScrollBarPrivate::newHoverControl(const QPoint &pos){    Q_Q(QScrollBar);    QStyleOptionSlider opt;    q->initStyleOption(&opt);    opt.subControls = QStyle::SC_All;    hoverControl = q->style()->hitTestComplexControl(QStyle::CC_ScrollBar, &opt, pos, q);    if (hoverControl == QStyle::SC_None)        hoverRect = QRect();    else        hoverRect = q->style()->subControlRect(QStyle::CC_ScrollBar, &opt, hoverControl, q);    return hoverControl;}void QScrollBarPrivate::activateControl(uint control, int threshold){    QAbstractSlider::SliderAction action = QAbstractSlider::SliderNoAction;    switch (control) {    case QStyle::SC_ScrollBarAddPage:        action = QAbstractSlider::SliderPageStepAdd;        break;    case QStyle::SC_ScrollBarSubPage:        action = QAbstractSlider::SliderPageStepSub;        break;    case QStyle::SC_ScrollBarAddLine:        action = QAbstractSlider::SliderSingleStepAdd;        break;    case QStyle::SC_ScrollBarSubLine:        action = QAbstractSlider::SliderSingleStepSub;        break;    case QStyle::SC_ScrollBarFirst:        action = QAbstractSlider::SliderToMinimum;        break;    case QStyle::SC_ScrollBarLast:        action = QAbstractSlider::SliderToMaximum;        break;    default:        break;    }    if (action) {        q_func()->setRepeatAction(action, threshold);        q_func()->triggerAction(action);    }}/*!    Initialize \a option with the values from this QScrollBar. This method    is useful for subclasses when they need a QStyleOptionSlider, but don't want    to fill in all the information themselves.    \sa QStyleOption::initFrom()*/void QScrollBar::initStyleOption(QStyleOptionSlider *option) const{    if (!option)        return;    Q_D(const QScrollBar);    option->initFrom(this);    option->subControls = QStyle::SC_None;    option->activeSubControls = QStyle::SC_None;    option->orientation = d->orientation;    option->minimum = d->minimum;    option->maximum = d->maximum;    option->sliderPosition = d->position;    option->sliderValue = d->value;    option->singleStep = d->singleStep;    option->pageStep = d->pageStep;    option->upsideDown = d->invertedAppearance;    if (d->orientation == Qt::Horizontal)        option->state |= QStyle::State_Horizontal;}#define HORIZONTAL (d_func()->orientation == Qt::Horizontal)#define VERTICAL !HORIZONTAL/*!    Constructs a vertical scroll bar.    The \a parent arguments is sent to the QWidget constructor.    The \l minimum defaults to 0, the \l maximum to 99, with a \l    singleStep size of 1 and a \l pageStep size of 10, and an initial    \l value of 0.*/QScrollBar::QScrollBar(QWidget *parent)    : QAbstractSlider(*new QScrollBarPrivate, parent){    d_func()->orientation = Qt::Vertical;    d_func()->init();}/*!    Constructs a scroll bar with the given \a orientation.    The \a parent argument is passed to the QWidget constructor.    The \l minimum defaults to 0, the \l maximum to 99, with a \l    singleStep size of 1 and a \l pageStep size of 10, and an initial    \l value of 0.*/QScrollBar::QScrollBar(Qt::Orientation orientation, QWidget *parent)    : QAbstractSlider(*new QScrollBarPrivate, parent){    d_func()->orientation = orientation;    d_func()->init();}#ifdef QT3_SUPPORT/*!    Use one of the constructors that doesn't take the \a name    argument and then use setObjectName() instead.*/QScrollBar::QScrollBar(QWidget *parent, const char *name)    : QAbstractSlider(*new QScrollBarPrivate, parent){    setObjectName(QString::fromAscii(name));    d_func()->orientation = Qt::Vertical;    d_func()->init();}/*!    Use one of the constructors that doesn't take the \a name    argument and then use setObjectName() instead.*/

⌨️ 快捷键说明

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