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

📄 qsplitter.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 4 页
字号:
/******************************************************************************** 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 "qsplitter.h"#ifndef QT_NO_SPLITTER#include "qapplication.h"#include "qcursor.h"#include "qdrawutil.h"#include "qevent.h"#include "qlayout.h"#include "qlist.h"#include "qpainter.h"#include "qrubberband.h"#include "qstyle.h"#include "qstyleoption.h"#include "qtextstream.h"#include "qvarlengtharray.h"#include "qvector.h"#include "private/qlayoutengine_p.h"#include "private/qsplitter_p.h"#include "qdebug.h"#include <ctype.h>//#define QSPLITTER_DEBUG/*!    \class QSplitterHandle    \brief The QSplitterHandle class provides handle functionality of the splitter.    \ingroup organizers    QSplitterHandle is typically what people think about when they think about    a splitter. It is the handle that is used to resize the widgets.    A typical developer using QSplitter will never have to worry about    QSplitterHandle. It is provided for developers who want splitter handles    that provide extra features, such as popup menus.    The typical way one would create splitter handles is to subclass QSplitter then    reimplement QSplitter::createHandle() to instantiate the custom splitter    handle. For example, a minimum QSplitter subclass might look like this:    \quotefromfile snippets/splitterhandle/splitter.h    \skipto class Splitter : public QSplitter    \printuntil /^\};/    The \l{QSplitter::}{createHandle()} implementation simply constructs a    custom splitter handle, called \c Splitter in this example:    \quotefromfile snippets/splitterhandle/splitter.cpp    \skipto createHandle()    \printuntil /^\}/    Information about a given handle can be obtained using functions like    orientation() and opaqueResize(), and is retrieved from its parent splitter.    Details like these can be used to give custom handles different appearances    depending on the splitter's orientation.    The complexity of a custom handle subclass depends on the tasks that it    needs to perform. A simple subclass might only provide a paintEvent()    implementation:    \quotefromfile snippets/splitterhandle/splitter.cpp    \skipto paintEvent    \printuntil /^\}/    In this example, a predefined gradient is set up differently depending on    the orientation of the handle. QSplitterHandle provides a reasonable    size hint for the handle, so the subclass does not need to provide a    reimplementation of sizeHint() unless the handle has special size    requirements.    \sa QSplitter*//*!    Creates a QSplitter handle with the given \a orientation and    QSplitter \a parent.*/QSplitterHandle::QSplitterHandle(Qt::Orientation orientation, QSplitter *parent)    : QWidget(*new QSplitterHandlePrivate, parent, 0){    Q_D(QSplitterHandle);    d->s = parent;    d->hover = false;    setOrientation(orientation);}/*!    Sets the orientation of the splitter handle to \a orientation.    This is usually propogated from the QSplitter.    \sa QSplitter::setOrientation()*/void QSplitterHandle::setOrientation(Qt::Orientation orientation){    Q_D(QSplitterHandle);    d->orient = orientation;#ifndef QT_NO_CURSOR    setCursor(orientation == Qt::Horizontal ? Qt::SplitHCursor : Qt::SplitVCursor);#endif}/*!   Returns the handle's orientation. This is usually propagated from the QSplitter.   \sa QSplitter::orientation()*/Qt::Orientation QSplitterHandle::orientation() const{    Q_D(const QSplitterHandle);    return d->orient;}/*!    Returns true if widgets are resized dynamically (opaquely), otherwise    returns false. This value is controlled by the QSplitter.    \sa QSplitter::opaqueResize()*/bool QSplitterHandle::opaqueResize() const{    Q_D(const QSplitterHandle);    return d->s->opaqueResize();}/*!    Returns the splitter associated with this splitter handle.    \sa QSplitter::handle()*/QSplitter *QSplitterHandle::splitter() const{    return d_func()->s;}/*!    Tells the splitter to move this handle to position \a pos, which is    the distance from the left or top edge of the widget.    Note that \a pos is also measured from the left (or top) for    right-to-left languages. This function will map \a pos to the    appropriate position before calling QSplitter::moveSplitter().    \sa QSplitter::moveSplitter() closestLegalPosition()*/void QSplitterHandle::moveSplitter(int pos){    Q_D(QSplitterHandle);    if (d->s->isRightToLeft() && d->orient == Qt::Horizontal)        pos = d->s->contentsRect().width() - pos;    d->s->moveSplitter(pos, d->s->indexOf(this));}/*!   Returns the closest legal position to \a pos of the splitter   handle. The positions are measured from the left or top edge of   the splitter, even for right-to-left languages.   \sa QSplitter::closestLegalPosition(), moveSplitter()*/int QSplitterHandle::closestLegalPosition(int pos){    Q_D(QSplitterHandle);    QSplitter *s = d->s;    if (s->isRightToLeft() && d->orient == Qt::Horizontal) {        int w = s->contentsRect().width();        return w - s->closestLegalPosition(w - pos, s->indexOf(this));    }    return s->closestLegalPosition(pos, s->indexOf(this));}/*!    \reimp*/QSize QSplitterHandle::sizeHint() const{    Q_D(const QSplitterHandle);    int hw = d->s->handleWidth();    QStyleOption opt(0);    opt.init(d->s);    opt.state = QStyle::State_None;    return parentWidget()->style()->sizeFromContents(QStyle::CT_Splitter, &opt, QSize(hw, hw), d->s)        .expandedTo(QApplication::globalStrut());}/*!    \reimp*/bool QSplitterHandle::event(QEvent *event){    Q_D(QSplitterHandle);    switch(event->type()) {    case QEvent::HoverEnter:        d->hover = true;        update();        break;    case QEvent::HoverLeave:        d->hover = false;        update();        break;    default:        break;    }    return QWidget::event(event);}/*!    \reimp*/void QSplitterHandle::mouseMoveEvent(QMouseEvent *e){    Q_D(QSplitterHandle);    if (!(e->buttons() & Qt::LeftButton))        return;    int pos = d->pick(parentWidget()->mapFromGlobal(e->globalPos()))                 - d->mouseOffset;    if (opaqueResize()) {        moveSplitter(pos);    } else {        d->s->setRubberBand(closestLegalPosition(pos));    }}/*!   \reimp*/void QSplitterHandle::mousePressEvent(QMouseEvent *e){    Q_D(QSplitterHandle);    if (e->button() == Qt::LeftButton)        d->mouseOffset = d->pick(e->pos());}/*!   \reimp*/void QSplitterHandle::mouseReleaseEvent(QMouseEvent *e){    Q_D(QSplitterHandle);    if (!opaqueResize() && e->button() == Qt::LeftButton) {        int pos = d->pick(parentWidget()->mapFromGlobal(e->globalPos()))                     - d->mouseOffset;        d->s->setRubberBand(-1);        moveSplitter(pos);    }}/*!   \reimp*/void QSplitterHandle::paintEvent(QPaintEvent *){    Q_D(QSplitterHandle);    QPainter p(this);    QStyleOption opt(0);    opt.rect = rect();    opt.palette = palette();    if (orientation() == Qt::Horizontal)        opt.state = QStyle::State_Horizontal;    else        opt.state = QStyle::State_None;    if (d->hover)        opt.state |= QStyle::State_MouseOver;    if (isEnabled())        opt.state |= QStyle::State_Enabled;    parentWidget()->style()->drawControl(QStyle::CE_Splitter, &opt, &p, d->s);}int QSplitterLayoutStruct::getWidgetSize(Qt::Orientation orient){    if (sizer == -1) {        QSize s = widget->sizeHint();        const int presizer = pick(s, orient);        const int realsize = pick(widget->size(), orient);        if (!s.isValid() || (widget->testAttribute(Qt::WA_Resized) && (realsize > presizer))) {            sizer = pick(widget->size(), orient);        } else {            sizer = presizer;        }        QSizePolicy p = widget->sizePolicy();        int sf = (orient == Qt::Horizontal) ? p.horizontalStretch() : p.verticalStretch();        if (sf > 1)            sizer *= sf;    }    return sizer;}int QSplitterLayoutStruct::getHandleSize(Qt::Orientation orient){    return pick(handle->sizeHint(), orient);}void QSplitterPrivate::init(){    Q_Q(QSplitter);    QSizePolicy sp(QSizePolicy::Expanding, QSizePolicy::Preferred);    if (orient == Qt::Vertical)        sp.transpose();    q->setSizePolicy(sp);    q->setAttribute(Qt::WA_WState_OwnSizePolicy, false);}void QSplitterPrivate::recalc(bool update){    Q_Q(QSplitter);    int n = list.count();    /*      Splitter handles before the first visible widget or right      before a hidden widget must be hidden.    */    bool first = true;    for (int i = 0; i < n ; ++i) {        QSplitterLayoutStruct *s = list.at(i);        s->handle->setHidden(first || s->widget->isHidden());        if (!s->widget->isHidden())            first = false;    }    int fi = 2 * q->frameWidth();    int maxl = fi;    int minl = fi;    int maxt = QWIDGETSIZE_MAX;    int mint = fi;    /*      calculate min/max sizes for the whole splitter    */    bool empty = true;    for (int j = 0; j < n; j++) {        QSplitterLayoutStruct *s = list.at(j);        if (!s->widget->isHidden()) {            empty = false;            if (!s->handle->isHidden()) {                minl += s->getHandleSize(orient);                maxl += s->getHandleSize(orient);            }            QSize minS = qSmartMinSize(s->widget);            minl += pick(minS);            maxl += pick(s->widget->maximumSize());            mint = qMax(mint, trans(minS));            int tm = trans(s->widget->maximumSize());            if (tm > 0)                maxt = qMin(maxt, tm);        }    }    if (empty) {        if (qobject_cast<QSplitter *>(q->parentWidget())) {            // nested splitters; be nice            maxl = maxt = 0;        } else {            // QSplitter with no children yet            maxl = QWIDGETSIZE_MAX;        }    } else {        maxl = qMin<int>(maxl, QWIDGETSIZE_MAX);    }    if (maxt < mint)        maxt = mint;    if (update) {        if (orient == Qt::Horizontal) {            q->setMaximumSize(maxl, maxt);            if (q->isWindow())                q->setMinimumSize(minl,mint);        } else {            q->setMaximumSize(maxt, maxl);            if (q->isWindow())                q->setMinimumSize(mint,minl);        }        doResize();        q->updateGeometry();    } else {        firstShow = true;    }}void QSplitterPrivate::doResize(){    Q_Q(QSplitter);    QRect r = q->contentsRect();    int n = list.count();    QVector<QLayoutStruct> a(n*2);    int i;    bool noStretchFactorsSet = true;    for (i = 0; i < n; ++i) {        QSizePolicy p = list.at(i)->widget->sizePolicy();        int sf = orient == Qt::Horizontal ? p.horizontalStretch() : p.verticalStretch();        if (sf != 0) {            noStretchFactorsSet = false;            break;        }    }    int j=0;    for (i = 0; i < n; ++i) {        QSplitterLayoutStruct *s = list.at(i);#ifdef QSPLITTER_DEBUG        qDebug("widget %d hidden: %d collapsed: %d handle hidden: %d", i, s->widget->isHidden(),               s->collapsed, s->handle->isHidden());#endif        a[j].init();        if (s->handle->isHidden()) {            a[j].maximumSize = 0;        } else {            a[j].sizeHint = a[j].minimumSize = a[j].maximumSize = s->getHandleSize(orient);            a[j].empty = false;        }

⌨️ 快捷键说明

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