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

📄 kwqscrollbar.cpp

📁 手机浏览器源码程序,功能强大
💻 CPP
字号:
/*
 * Copyright (C) 2003 Apple Computer, Inc.  All rights reserved.
 * Portions Copyright (c) 2005 Nokia Corporation, Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "KWQScrollBar.h"

#include "KWQExceptions.h"

#include "WebCoreFormControlFactory.h"

QScrollBar::QScrollBar(Qt::Orientation orientation, QWidget* parent)
:m_valueChanged(this, SIGNAL(valueChanged(int)))
{

    m_orientation = orientation;
    m_visibleSize = 0;
    m_totalSize = 0;
    m_currentPos = 0;
    m_lineStep = 0;
    m_pageStep = 0;
    m_scroller = 0;

    m_scroller = TWebCoreFormControlFactory::Factory()->ConstructScrollBar();
    m_scroller->SetOrientation(orientation==Qt::Vertical?EWebCoreScrollBarVertical:EWebCoreScrollBarHorizontal);

    setView(m_scroller);

    if (parent)
        m_scroller->SetParent(static_cast<MWebCoreScrollView*>(parent->getView()));

    setFocusPolicy(NoFocus);

}

QScrollBar::~QScrollBar()
{
    m_scroller->Close();
}

bool QScrollBar::setValue(int v)
{

    int maxPos = m_totalSize - m_visibleSize;
    if (v < 0) v = 0;
    if (v > maxPos)
        v = maxPos;
    if (m_currentPos == v)
        return false; // Our value stayed the same.
    m_currentPos = v;

    m_scroller->SetValue((float)m_currentPos/maxPos);

    valueChanged(); // Emit the signal that indicates our value has changed.

    return false;
}

void QScrollBar::setSteps(int lineStep, int pageStep)
{
    m_lineStep = lineStep;
    m_pageStep = pageStep;
}

void QScrollBar::setKnobProportion(int visibleArea, int totalArea)
{
    m_visibleSize = visibleArea;
    m_totalSize = totalArea;
    float val = (float)m_visibleSize/m_totalSize;

    if (!(val == m_scroller->KnobProportion() || val < 0.0))
        m_scroller->SetKnobProportion(val);
}

void QScrollBar::scrollbarHit(TWebCoreScrollBarPart hitPart)
{
    int maxPos = m_totalSize - m_visibleSize;
    if (maxPos <= 0)
        return; // Impossible to scroll anywhere.

    volatile int newPos = m_currentPos;
    switch (hitPart) {
        case EWebCoreScrollBarDecrementLine:
            newPos -= m_lineStep;
            break;
        case EWebCoreScrollBarIncrementLine:
            newPos += m_lineStep;
            break;
        case EWebCoreScrollBarDecrementPage:
            newPos -= m_pageStep;
            break;
        case EWebCoreScrollBarIncrementPage:
            newPos += m_pageStep;
            break;

            // If the thumb is hit, then the scrollbar changed its value for us.
        case EWebCoreScrollBarKnob:
            newPos = (int)(m_scroller->Value()*maxPos);
            break;
        default: ;
    }

    setValue(newPos);
}

void QScrollBar::valueChanged()
{
    m_valueChanged.call(m_currentPos);
}

bool QScrollBar::scroll(KWQScrollDirection direction, KWQScrollGranularity granularity, float multiplier)
{
    float delta = 0.0;
    if ((direction == KWQScrollUp && m_orientation == Vertical) || (direction == KWQScrollLeft && m_orientation == Horizontal)) {
        if (granularity == KWQScrollLine) {
            delta = -(m_lineStep * 4);
        } else if (granularity == KWQScrollPage) {
            delta = -m_pageStep;
        } else if (granularity == KWQScrollDocument) {
            delta = -m_currentPos;
        } else if (granularity == KWQScrollWheel) {
            delta = -m_lineStep;
        }
    } else if ((direction == KWQScrollDown && m_orientation == Vertical) || (direction == KWQScrollRight && m_orientation == Horizontal)) {
        if (granularity == KWQScrollLine) {
            delta = (m_lineStep * 4);
        } else if (granularity == KWQScrollPage) {
            delta = m_pageStep;
        } else if (granularity == KWQScrollDocument) {
           delta = m_totalSize - m_visibleSize - m_currentPos;
        } else if (granularity == KWQScrollWheel) {
            delta = m_lineStep;
        }
    }
    int newPos = (int)(m_currentPos + (delta * multiplier));
    return setValue(newPos);
}

bool QScrollBar::clickAtPoint(const QPoint& point)
{
    TWebCoreScrollBarPart barpart = m_scroller->ClickAtPoint(point.Point());
    if (barpart!=EWebCoreScrollBarNone)
        scrollbarHit(barpart);
    return barpart!=EWebCoreScrollBarNone;
}

⌨️ 快捷键说明

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