📄 qlayoutengine.cpp
字号:
n--; break; } } } } else { // extra space int n = count; int space_left = space - sumSpacing; // first give to the fixed ones, and handle non-expansiveness for (i = start; i < start + count; i++) { QLayoutStruct *data = &chain[i]; if (!data->done && (data->maximumSize <= data->smartSizeHint() || (wannaGrow && !data->expansive && data->stretch == 0) || (!allEmptyNonstretch && data->empty && !data->expansive && data->stretch == 0))) { data->size = data->smartSizeHint(); data->done = true; space_left -= data->size; sumStretch -= data->stretch; n--; } } extraspace = space_left; /* Do a trial distribution and calculate how much it is off. If there are more deficit pixels than surplus pixels, give the minimum size items what they need, and repeat. Otherwise give to the maximum size items, and repeat. Paul Olav Tvete has a wonderful mathematical proof of the correctness of this principle, but unfortunately this comment is too small to contain it. */ int surplus, deficit; do { surplus = deficit = 0; Fixed fp_space = toFixed(space_left); Fixed fp_w = 0; for (i = start; i < start + count; i++) { QLayoutStruct *data = &chain[i]; if (data->done) continue; extraspace = 0; if (sumStretch <= 0) fp_w += fp_space / n; else fp_w += (fp_space * data->stretch) / sumStretch; int w = fRound(fp_w); data->size = w; fp_w -= toFixed(w); // give the difference to the next if (w < data->smartSizeHint()) { deficit += data->smartSizeHint() - w; } else if (w > data->maximumSize) { surplus += w - data->maximumSize; } } if (deficit > 0 && surplus <= deficit) { // give to the ones that have too little for (i = start; i < start+count; i++) { QLayoutStruct *data = &chain[i]; if (!data->done && data->size < data->smartSizeHint()) { data->size = data->smartSizeHint(); data->done = true; space_left -= data->smartSizeHint(); sumStretch -= data->stretch; n--; } } } if (surplus > 0 && surplus >= deficit) { // take from the ones that have too much for (i = start; i < start + count; i++) { QLayoutStruct *data = &chain[i]; if (!data->done && data->size > data->maximumSize) { data->size = data->maximumSize; data->done = true; space_left -= data->maximumSize; sumStretch -= data->stretch; n--; } } } } while (n > 0 && surplus != deficit); if (n == 0) extraspace = space_left; } /* As a last resort, we distribute the unwanted space equally among the spacers (counting the start and end of the chain). We could, but don't, attempt a sub-pixel allocation of the extra space. */ int extra = extraspace / (spacerCount + 2); int p = pos + extra; for (i = start; i < start+count; i++) { QLayoutStruct *data = &chain[i]; data->pos = p; p += data->size; if (!data->empty) p += data->effectiveSpacer(spacer) + extra; }#ifdef QLAYOUT_EXTRA_DEBUG qDebug() << "qGeomCalc" << "start" << start << "count" << count << "pos" << pos << "space" << space << "spacer" << spacer; for (i = start; i < start + count; ++i) { qDebug() << i << ":" << chain[i].minimumSize << chain[i].smartSizeHint() << chain[i].maximumSize << "stretch" << chain[i].stretch << "empty" << chain[i].empty << "expansive" << chain[i].expansive << "spacing" << chain[i].spacing; qDebug() << "result pos" << chain[i].pos << "size" << chain[i].size; }#endif}Q_GUI_EXPORT QSize qSmartMinSize(const QSize &sizeHint, const QSize &minSizeHint, const QSize &minSize, const QSize &maxSize, const QSizePolicy &sizePolicy){ QSize s(0, 0); if (sizePolicy.horizontalPolicy() != QSizePolicy::Ignored) { if (sizePolicy.horizontalPolicy() & QSizePolicy::ShrinkFlag) s.setWidth(minSizeHint.width()); else s.setWidth(qMax(sizeHint.width(), minSizeHint.width())); } if (sizePolicy.verticalPolicy() != QSizePolicy::Ignored) { if (sizePolicy.verticalPolicy() & QSizePolicy::ShrinkFlag) { s.setHeight(minSizeHint.height()); } else { s.setHeight(qMax(sizeHint.height(), minSizeHint.height())); } } s = s.boundedTo(maxSize); if (minSize.width() > 0) s.setWidth(minSize.width()); if (minSize.height() > 0) s.setHeight(minSize.height()); return s.expandedTo(QSize(0,0));}Q_GUI_EXPORT QSize qSmartMinSize(const QWidgetItem *i){ QWidget *w = ((QWidgetItem *)i)->widget(); return qSmartMinSize(w->sizeHint(), w->minimumSizeHint(), w->minimumSize(), w->maximumSize(), w->sizePolicy());}Q_GUI_EXPORT QSize qSmartMinSize(const QWidget *w){ return qSmartMinSize(w->sizeHint(), w->minimumSizeHint(), w->minimumSize(), w->maximumSize(), w->sizePolicy());}Q_GUI_EXPORT QSize qSmartMaxSize(const QSize &sizeHint, const QSize &minSize, const QSize &maxSize, const QSizePolicy &sizePolicy, Qt::Alignment align){ if (align & Qt::AlignHorizontal_Mask && align & Qt::AlignVertical_Mask) return QSize(QLAYOUTSIZE_MAX, QLAYOUTSIZE_MAX); QSize s = maxSize; QSize hint = sizeHint.expandedTo(minSize); if (s.width() == QWIDGETSIZE_MAX && !(align & Qt::AlignHorizontal_Mask)) if (!(sizePolicy.horizontalPolicy() & QSizePolicy::GrowFlag) ) s.setWidth(hint.width()); if (s.height() == QWIDGETSIZE_MAX && !(align & Qt::AlignVertical_Mask)) if (!(sizePolicy.verticalPolicy() & QSizePolicy::GrowFlag) ) s.setHeight(hint.height()); if (align & Qt::AlignHorizontal_Mask) s.setWidth(QLAYOUTSIZE_MAX); if (align & Qt::AlignVertical_Mask) s.setHeight(QLAYOUTSIZE_MAX); return s;}Q_GUI_EXPORT QSize qSmartMaxSize(const QWidgetItem *i, Qt::Alignment align){ QWidget *w = ((QWidgetItem*)i)->widget(); return qSmartMaxSize(w->sizeHint().expandedTo(w->minimumSizeHint()), w->minimumSize(), w->maximumSize(), w->sizePolicy(), align);}Q_GUI_EXPORT QSize qSmartMaxSize(const QWidget *w, Qt::Alignment align){ return qSmartMaxSize(w->sizeHint().expandedTo(w->minimumSizeHint()), w->minimumSize(), w->maximumSize(), w->sizePolicy(), align);}int qSmartSpacing(const QLayout *layout, QStyle::PixelMetric pm){ QObject *parent = layout->parent(); if (!parent) { return -1; } else if (parent->isWidgetType()) { QWidget *pw = static_cast<QWidget *>(parent); return pw->style()->pixelMetric(pm, 0, pw); } else { return static_cast<QLayout *>(parent)->spacing(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -