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

📄 qboxlayout.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/******************************************************************************** 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 "qboxlayout.h"#include "qapplication.h"#include "qwidget.h"#include "qlist.h"#include "qsizepolicy.h"#include "qvector.h"#include "qlayoutengine_p.h"#include "qlayout_p.h"/*    Returns true if the \a widget can be added to the \a layout;    otherwise returns false.*/static bool checkWidget(QLayout *layout, QWidget *widget){    if (!widget) {        qWarning("QLayout: Cannot add null widget to %s/%s", layout->metaObject()->className(),                  layout->objectName().toLocal8Bit().data());        return false;    }    return true;}struct QBoxLayoutItem{    QBoxLayoutItem(QLayoutItem *it, int stretch_ = 0)        : item(it), stretch(stretch_), magic(false) { }    ~QBoxLayoutItem() { delete item; }    int hfw(int w) {        if (item->hasHeightForWidth()) {            return item->heightForWidth(w);        } else {            return item->sizeHint().height();        }    }    int mhfw(int w) {        if (item->hasHeightForWidth()) {            return item->heightForWidth(w);        } else {            return item->minimumSize().height();        }    }    int hStretch() {        if (stretch == 0 && item->widget()) {            return item->widget()->sizePolicy().horizontalStretch();        } else {            return stretch;        }    }    int vStretch() {        if (stretch == 0 && item->widget()) {            return item->widget()->sizePolicy().verticalStretch();        } else {            return stretch;        }    }    QLayoutItem *item;    int stretch;    bool magic;};class QBoxLayoutPrivate : public QLayoutPrivate{    Q_DECLARE_PUBLIC(QBoxLayout)public:    QBoxLayoutPrivate() : hfwWidth(-1), dirty(true), spacing(-1) { }    ~QBoxLayoutPrivate();    void setDirty() {        geomArray.clear();        hfwWidth = -1;        hfwHeight = -1;        dirty = true;    }    QList<QBoxLayoutItem *> list;    QVector<QLayoutStruct> geomArray;    int hfwWidth;    int hfwHeight;    int hfwMinHeight;    QSize sizeHint;    QSize minSize;    QSize maxSize;    int leftMargin, topMargin, rightMargin, bottomMargin;    Qt::Orientations expanding;    uint hasHfw : 1;    uint dirty : 1;    QBoxLayout::Direction dir;    int spacing;    inline void deleteAll() { while (!list.isEmpty()) delete list.takeFirst(); }    void setupGeom();    void calcHfw(int);    void effectiveMargins(int *left, int *top, int *right, int *bottom) const;};QBoxLayoutPrivate::~QBoxLayoutPrivate(){}static inline bool horz(QBoxLayout::Direction dir){    return dir == QBoxLayout::RightToLeft || dir == QBoxLayout::LeftToRight;}/** * The purpose of this function is to make sure that widgets are not laid out outside its layout. * E.g. the layoutItemRect margins are only meant to take of the surrounding margins/spacings. * However, if the margin is 0, it can easily cover the area of a widget above it. */void QBoxLayoutPrivate::effectiveMargins(int *left, int *top, int *right, int *bottom) const{    int l = leftMargin;    int t = topMargin;    int r = rightMargin;    int b = bottomMargin;#ifdef Q_WS_MAC    Q_Q(const QBoxLayout);    if (horz(dir)) {        QBoxLayoutItem *leftBox = 0;        QBoxLayoutItem *rightBox = 0;        if (left || right) {            leftBox = list.value(0);            rightBox = list.value(list.count() - 1);            if (dir == QBoxLayout::RightToLeft)                qSwap(leftBox, rightBox);            int leftDelta = 0;            int rightDelta = 0;            if (leftBox) {                QLayoutItem *itm = leftBox->item;                if (QWidget *w = itm->widget())                    leftDelta = itm->geometry().left() - w->geometry().left();            }            if (rightBox) {                QLayoutItem *itm = rightBox->item;                if (QWidget *w = itm->widget())                    rightDelta = w->geometry().right() - itm->geometry().right();            }            QWidget *w = q->parentWidget();            Qt::LayoutDirection layoutDirection = w ? w->layoutDirection() : QApplication::layoutDirection();            if (layoutDirection == Qt::RightToLeft)                qSwap(leftDelta, rightDelta);            l = qMax(l, leftDelta);            r = qMax(r, rightDelta);        }        int count = top || bottom ? list.count() : 0;        for (int i = 0; i < count; ++i) {            QBoxLayoutItem *box = list.at(i);            QLayoutItem *itm = box->item;            QWidget *w = itm->widget();            if (w) {                QRect lir = itm->geometry();                QRect wr = w->geometry();                if (top)                    t = qMax(t, lir.top() - wr.top());                if (bottom)                    b = qMax(b, wr.bottom() - lir.bottom());            }        }    } else {    // vertical layout        QBoxLayoutItem *topBox = 0;        QBoxLayoutItem *bottomBox = 0;        if (top || bottom) {            topBox = list.value(0);            bottomBox = list.value(list.count() - 1);            if (dir == QBoxLayout::BottomToTop) {                qSwap(topBox, bottomBox);            }            if (top && topBox) {                QLayoutItem *itm = topBox->item;                QWidget *w = itm->widget();                if (w)                    t = qMax(t, itm->geometry().top() - w->geometry().top());            }            if (bottom && bottomBox) {                QLayoutItem *itm = bottomBox->item;                QWidget *w = itm->widget();                if (w)                    b = qMax(b, w->geometry().bottom() - itm->geometry().bottom());            }        }        int count = left || right ? list.count() : 0;        for (int i = 0; i < count; ++i) {            QBoxLayoutItem *box = list.at(i);            QLayoutItem *itm = box->item;            QWidget *w = itm->widget();            if (w) {                QRect lir = itm->geometry();                QRect wr = w->geometry();                if (left)                    l = qMax(l, lir.left() - wr.left());                if (right)                    r = qMax(r, wr.right() - lir.right());            }        }            }#endif    if (left)        *left = l;    if (top)        *top = t;    if (right)        *right = r;    if (bottom)        *bottom = b;}/*    Initializes the data structure needed by qGeomCalc and    recalculates max/min and size hint.*/void QBoxLayoutPrivate::setupGeom(){    if (!dirty)        return;    Q_Q(QBoxLayout);    int maxw = horz(dir) ? 0 : QLAYOUTSIZE_MAX;    int maxh = horz(dir) ? QLAYOUTSIZE_MAX : 0;    int minw = 0;    int minh = 0;    int hintw = 0;    int hinth = 0;    bool horexp = false;    bool verexp = false;    hasHfw = false;    int n = list.count();    geomArray.clear();    QVector<QLayoutStruct> a(n);    QSizePolicy::ControlTypes controlTypes1;    QSizePolicy::ControlTypes controlTypes2;    int fixedSpacing = q->spacing();    int previousNonEmptyIndex = -1;    QStyle *style = 0;    if (fixedSpacing < 0) {        if (QWidget *parentWidget = q->parentWidget())            style = parentWidget->style();    }    for (int i = 0; i < n; i++) {        QBoxLayoutItem *box = list.at(i);        QSize max = box->item->maximumSize();        QSize min = box->item->minimumSize();        QSize hint = box->item->sizeHint();        Qt::Orientations exp = box->item->expandingDirections();        bool empty = box->item->isEmpty();        int spacing = 0;        if (!empty) {            if (fixedSpacing >= 0) {                spacing = (previousNonEmptyIndex >= 0) ? fixedSpacing : 0;#ifdef Q_WS_MAC                if (!horz(dir) && previousNonEmptyIndex >= 0) {                    QBoxLayoutItem *sibling = (dir == QBoxLayout::TopToBottom  ? box : list.at(previousNonEmptyIndex));                    if (sibling) {                        QWidget *wid = sibling->item->widget();                        if (wid)                            spacing = qMax(spacing, sibling->item->geometry().top() - wid->geometry().top());                    }                }#endif            } else {                controlTypes1 = controlTypes2;                controlTypes2 = box->item->controlTypes();                if (previousNonEmptyIndex >= 0) {                    QSizePolicy::ControlTypes actual1 = controlTypes1;                    QSizePolicy::ControlTypes actual2 = controlTypes2;                    if (dir == QBoxLayout::RightToLeft || dir == QBoxLayout::BottomToTop)                        qSwap(actual1, actual2);                    if (style) {                        spacing = style->combinedLayoutSpacing(actual1, actual2,                                             horz(dir) ? Qt::Horizontal : Qt::Vertical,                                             0, q->parentWidget());                        if (spacing < 0)                            spacing = 0;                    }                }            }            if (previousNonEmptyIndex >= 0)                a[previousNonEmptyIndex].spacing = spacing;            previousNonEmptyIndex = i;        }        bool ignore = empty && box->item->widget(); // ignore hidden widgets        bool dummy = true;        if (horz(dir)) {            bool expand = (exp & Qt::Horizontal || box->stretch > 0);            horexp = horexp || expand;            maxw += spacing + max.width();            minw += spacing + min.width();            hintw += spacing + hint.width();            if (!ignore)                qMaxExpCalc(maxh, verexp, dummy,                            max.height(), exp & Qt::Vertical, box->item->isEmpty());            minh = qMax(minh, min.height());            hinth = qMax(hinth, hint.height());            a[i].sizeHint = hint.width();            a[i].maximumSize = max.width();            a[i].minimumSize = min.width();            a[i].expansive = expand;            a[i].stretch = box->stretch ? box->stretch : box->hStretch();        } else {            bool expand = (exp & Qt::Vertical || box->stretch > 0);            verexp = verexp || expand;            maxh += spacing + max.height();            minh += spacing + min.height();            hinth += spacing + hint.height();            if (!ignore)                qMaxExpCalc(maxw, horexp, dummy,                            max.width(), exp & Qt::Horizontal, box->item->isEmpty());            minw = qMax(minw, min.width());            hintw = qMax(hintw, hint.width());            a[i].sizeHint = hint.height();            a[i].maximumSize = max.height();            a[i].minimumSize = min.height();            a[i].expansive = expand;            a[i].stretch = box->stretch ? box->stretch : box->vStretch();        }        a[i].empty = empty;        a[i].spacing = 0;   // might be be initialized with a non-zero value in a later iteration        hasHfw = hasHfw || box->item->hasHeightForWidth();    }    geomArray = a;    expanding = (Qt::Orientations)                       ((horexp ? Qt::Horizontal : 0)                         | (verexp ? Qt::Vertical : 0));    minSize = QSize(minw, minh);    maxSize = QSize(maxw, maxh).expandedTo(minSize);    sizeHint = QSize(hintw, hinth).expandedTo(minSize).boundedTo(maxSize);    q->getContentsMargins(&leftMargin, &topMargin, &rightMargin, &bottomMargin);    int left, top, right, bottom;    effectiveMargins(&left, &top, &right, &bottom);    QSize extra(left + right, top + bottom);    minSize += extra;    maxSize += extra;    sizeHint += extra;    dirty = false;}/*  Calculates and stores the preferred height given the width \a w.*/void QBoxLayoutPrivate::calcHfw(int w){    QVector<QLayoutStruct> &a = geomArray;    int n = a.count();    int h = 0;    int mh = 0;    Q_ASSERT(n == list.size());    if (horz(dir)) {        qGeomCalc(a, 0, n, 0, w);        for (int i = 0; i < n; i++) {            QBoxLayoutItem *box = list.at(i);            h = qMax(h, box->hfw(a.at(i).size));            mh = qMax(mh, box->mhfw(a.at(i).size));        }    } else {        for (int i = 0; i < n; ++i) {            QBoxLayoutItem *box = list.at(i);            int spacing = a.at(i).spacing;            h += box->hfw(w);            mh += box->mhfw(w);            h += spacing;            mh += spacing;        }    }    hfwWidth = w;    hfwHeight = h;    hfwMinHeight = mh;}/*!    \class QBoxLayout    \brief The QBoxLayout class lines up child widgets horizontally or    vertically.    \ingroup geomanagement    \ingroup appearance    QBoxLayout takes the space it gets (from its parent layout or from    the parentWidget()), divides it up into a row of boxes, and makes    each managed widget fill one box.    \image qhboxlayout-with-5-children.png Horizontal box layout with five child widgets    If the QBoxLayout's orientation is Qt::Horizontal the boxes are    placed in a row, with suitable sizes. Each widget (or other box)    will get at least its minimum size and at most its maximum size.    Any excess space is shared according to the stretch factors (more    about that below).    \image qvboxlayout-with-5-children.png Vertical box layout with five child widgets    If the QBoxLayout's orientation is Qt::Vertical, the boxes are    placed in a column, again with suitable sizes.    The easiest way to create a QBoxLayout is to use one of the    convenience classes, e.g. QHBoxLayout (for Qt::Horizontal boxes)    or QVBoxLayout (for Qt::Vertical boxes). You can also use the    QBoxLayout constructor directly, specifying its direction as    LeftToRight, RightToLeft, TopToBottom, or BottomToTop.    If the QBoxLayout is not the top-level layout (i.e. it is not    managing all of the widget's area and children), you must add it    to its parent layout before you can do anything with it. The    normal way to add a layout is by calling    parentLayout-\>addLayout().    Once you have done this, you can add boxes to the QBoxLayout using    one of four functions:    \list    \o addWidget() to add a widget to the QBoxLayout and set the    widget's stretch factor. (The stretch factor is along the row of    boxes.)    \o addSpacing() to create an empty box; this is one of the    functions you use to create nice and spacious dialogs. See below    for ways to set margins.

⌨️ 快捷键说明

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