Пример #1
0
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));
}
Пример #2
0
QString KPropertySizePolicyDelegate::valueToString(const QVariant& value, const QLocale &locale) const
{
    const QSizePolicy sp(value.value<QSizePolicy>());
    if (locale.language() == QLocale::C) {
        return QString::fromLatin1("%1, %2, %3, %4")
                .arg(KPropertyUtils::keyForEnumValue("SizePolicy", sp.horizontalPolicy()))
                .arg(KPropertyUtils::keyForEnumValue("SizePolicy", sp.verticalPolicy()))
                .arg(sp.horizontalStretch())
                .arg(sp.verticalStretch());
    }
    return QObject::tr("%1, %2, %3, %4", "Size Policy")
        .arg(s_sizePolicyListData->nameForPolicy(sp.horizontalPolicy()))
        .arg(s_sizePolicyListData->nameForPolicy(sp.verticalPolicy()))
        .arg(locale.toString(sp.horizontalStretch()))
        .arg(locale.toString(sp.verticalStretch()));
}
Пример #3
0
void tst_QLayout::smartMaxSize()
{
    QVector<int> expectedWidths;

    QFile f(QFINDTESTDATA("baseline/smartmaxsize"));

    QCOMPARE(f.open(QIODevice::ReadOnly | QIODevice::Text), true);

    QTextStream stream(&f);

    while(!stream.atEnd()) {
        QString line = stream.readLine(200);
        expectedWidths.append(line.section(QLatin1Char(' '), 6, -1, QString::SectionSkipEmpty).toInt());
    }
    f.close();

    int sizeCombinations[] = { 0, 10, 20, QWIDGETSIZE_MAX};
    QSizePolicy::Policy policies[] = {  QSizePolicy::Fixed,
                                        QSizePolicy::Minimum,
                                        QSizePolicy::Maximum,
                                        QSizePolicy::Preferred,
                                        QSizePolicy::Expanding,
                                        QSizePolicy::MinimumExpanding,
                                        QSizePolicy::Ignored
                                        };
    Qt::Alignment alignments[] = {  0,
                                    Qt::AlignLeft,
                                    Qt::AlignRight,
                                    Qt::AlignHCenter
                                    };

    int expectedIndex = 0;
    int regressionCount = 0;
    for (size_t p = 0; p < sizeof(policies)/sizeof(QSizePolicy::Policy); ++p) {
        QSizePolicy sizePolicy;
        sizePolicy.setHorizontalPolicy(policies[p]);
        for (size_t min = 0; min < sizeof(sizeCombinations)/sizeof(int); ++min) {
            int minSize = sizeCombinations[min];
            for (size_t max = 0; max < sizeof(sizeCombinations)/sizeof(int); ++max) {
                int maxSize = sizeCombinations[max];
                for (size_t sh = 0; sh < sizeof(sizeCombinations)/sizeof(int); ++sh) {
                    int sizeHint = sizeCombinations[sh];
                    for (size_t a = 0; a < sizeof(alignments)/sizeof(int); ++a) {
                        Qt::Alignment align = alignments[a];
                        QSize sz = qSmartMaxSize(QSize(sizeHint, 1), QSize(minSize, 1), QSize(maxSize, 1), sizePolicy, align);
                        int width = sz.width();
                        int expectedWidth = expectedWidths[expectedIndex];
                        if (width != expectedWidth) {
                            qDebug() << "error at index" << expectedIndex << ":" << sizePolicy.horizontalPolicy() << align << minSize << sizeHint << maxSize << width;
                            ++regressionCount;
                        }
                        ++expectedIndex;
                    }
                }
            }
        }
    }
    QCOMPARE(regressionCount, 0);
}
Пример #4
0
void SizePolicyComposedProperty::setValue(Property *property,
    const QVariant &value, bool rememberOldValue)
{
    const QSizePolicy sp( value.value<QSizePolicy>() );
    property->child("hor_policy")->setValue(sp.horizontalPolicy(), rememberOldValue, false);
    property->child("vert_policy")->setValue(sp.verticalPolicy(), rememberOldValue, false);
    property->child("hor_stretch")->setValue(sp.horizontalStretch(), rememberOldValue, false);
    property->child("vert_stretch")->setValue(sp.verticalStretch(), rememberOldValue, false);
}
Пример #5
0
/*!
    \reimp
*/
void QWidgetItem::setGeometry(const QRect &rect)
{
    if (isEmpty())
        return;

    QRect r = !wid->testAttribute(Qt::WA_LayoutUsesWidgetRect)
            ? fromLayoutItemRect(wid->d_func(), rect)
            : rect;
    const QSize widgetRectSurplus = r.size() - rect.size(); 

    /* 
       For historical reasons, this code is done using widget rect 
       coordinates, not layout item rect coordinates. However, 
       QWidgetItem's sizeHint(), maximumSize(), and heightForWidth() 
       all work in terms of layout item rect coordinates, so we have to 
       add or subtract widgetRectSurplus here and there. The code could 
       be much simpler if we did everything using layout item rect 
       coordinates and did the conversion right before the call to 
       QWidget::setGeometry(). 
     */ 

    QSize s = r.size().boundedTo(maximumSize() + widgetRectSurplus);  
    int x = r.x();
    int y = r.y();
    if (align & (Qt::AlignHorizontal_Mask | Qt::AlignVertical_Mask)) {
        QSize pref(sizeHint());
        QSizePolicy sp = wid->sizePolicy();
        if (sp.horizontalPolicy() == QSizePolicy::Ignored)
            pref.setWidth(wid->sizeHint().expandedTo(wid->minimumSize()).width());
        if (sp.verticalPolicy() == QSizePolicy::Ignored)
            pref.setHeight(wid->sizeHint().expandedTo(wid->minimumSize()).height());
        pref += widgetRectSurplus;
        if (align & Qt::AlignHorizontal_Mask)
            s.setWidth(qMin(s.width(), pref.width()));
        if (align & Qt::AlignVertical_Mask) {
            if (hasHeightForWidth())
                s.setHeight(qMin(s.height(), 
                                 heightForWidth(s.width() - widgetRectSurplus.width()) 
                                 + widgetRectSurplus.height()));
            else
                s.setHeight(qMin(s.height(), pref.height()));
        }
    }
    Qt::Alignment alignHoriz = QStyle::visualAlignment(wid->layoutDirection(), align);
    if (alignHoriz & Qt::AlignRight)
        x = x + (r.width() - s.width());
    else if (!(alignHoriz & Qt::AlignLeft))
        x = x + (r.width() - s.width()) / 2;

    if (align & Qt::AlignBottom)
        y = y + (r.height() - s.height());
    else if (!(align & Qt::AlignTop))
        y = y + (r.height() - s.height()) / 2;

    wid->setGeometry(x, y, s.width(), s.height());
}
Пример #6
0
QString SizePolicyDelegate::displayText( const QVariant& value ) const
{
    const QSizePolicy sp(value.value<QSizePolicy>());

    return QString::fromLatin1(SIZEPOLICY_MASK)
        .arg(s_sizePolicyListData->nameForPolicy(sp.horizontalPolicy()))
        .arg(s_sizePolicyListData->nameForPolicy(sp.verticalPolicy()))
        .arg(sp.horizontalStretch())
        .arg(sp.verticalStretch());
}
Пример #7
0
static QString sizePolicyToString(const QSizePolicy &p)
{
    QString rc; {
        QTextStream str(&rc);
        str << "Control=" << p.controlType() << " expdirs=" << p.expandingDirections()
            << " hasHeightForWidth=" << p.hasHeightForWidth()
            << " H: Policy=" << p.horizontalPolicy()
            << " stretch=" << p.horizontalStretch()
            << " V: Policy=" << p.verticalPolicy()
            << " stretch=" << p.verticalStretch();
    }
    return rc;
}
Пример #8
0
QString LayoutDumper::toString(const QSizePolicy& policy)
{
    QString result = QString("(%1, %2)")
            .arg(toString(policy.horizontalPolicy()))
            .arg(toString(policy.verticalPolicy()));
    if (policy.hasHeightForWidth()) {
        result += " [hasHeightForWidth]";
    }
    if (policy.hasWidthForHeight()) {
        result += " [hasWidthForHeight]";
    }
    return result;
}
Пример #9
0
    void verticallyStretchButtons() {
        /// do not vertically stretch if using transparent style sheet
        if (makeInnerWidgetsTransparent) return;

        /// check each widget inside MenuLineEdit
        for (int i = hLayout->count() - 1; i >= 0; --i) {
            QWidget *w = hLayout->itemAt(i)->widget();
            if (w != NULL && w != m_singleLineEditText && w != m_multiLineEditText) {
                /// for all widgets except the main editing widget: change tab focus policy
                QSizePolicy sp = w->sizePolicy();
                w->setSizePolicy(sp.horizontalPolicy(), QSizePolicy::MinimumExpanding);
            }
        }
    }
static PyObject *meth_QSizePolicy_horizontalPolicy(PyObject *sipSelf, PyObject *sipArgs)
{
    PyObject *sipParseErr = NULL;

    {
        QSizePolicy *sipCpp;

        if (sipParseArgs(&sipParseErr, sipArgs, "B", &sipSelf, sipType_QSizePolicy, &sipCpp))
        {
            QSizePolicy::Policy sipRes;

            Py_BEGIN_ALLOW_THREADS
            sipRes = sipCpp->horizontalPolicy();
            Py_END_ALLOW_THREADS

            return sipConvertFromEnum(sipRes,sipType_QSizePolicy_Policy);
        }
    }
Пример #11
0
void AnimatedFrame::updateSizeHints()
{
	if(widget_) {
		QSize hint = widget_->minimumSizeHint();
		if(!hint.isValid())
			hint = widget_->sizeHint();

		QSizePolicy policy = widget_->sizePolicy();

		if(edge_ == kNorth || edge_ == kSouth) {
			int minimum = widget_->minimumHeight();
			int maximum = widget_->maximumHeight();

			minimumSizeHint_.setWidth(0);
			minimumSizeHint_.setHeight(qBound(minimum, hint.height(), maximum));

			setSizePolicy(policy.horizontalPolicy(), QSizePolicy::Maximum);
		}
		else {
			int minimum = widget_->minimumWidth();
			int maximum = widget_->maximumWidth();

			minimumSizeHint_.setWidth(qBound(minimum, hint.width(), maximum));
			minimumSizeHint_.setHeight(0);

			setSizePolicy(QSizePolicy::Maximum, policy.verticalPolicy());
		}

		setMinimumSize(widget_->minimumSize());
		setMaximumSize(widget_->maximumSize());
	}
	else {
		minimumSizeHint_ = QSize();
		setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
	}

	sizeHint_ = minimumSizeHint_;

	if(isUnfolded())
		updateGeometry();
}
Пример #12
0
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;
}
Пример #13
0
osg::GraphicsContext::Traits* GraphicsWindowQt::createTraits( const QGLWidget* widget )
{
    osg::GraphicsContext::Traits *traits = new osg::GraphicsContext::Traits;

    qglFormat2traits( widget->format(), traits );

    QRect r = widget->geometry();
    traits->x = r.x();
    traits->y = r.y();
    traits->width = r.width();
    traits->height = r.height();

    traits->windowName = widget->windowTitle().toLocal8Bit().data();
    Qt::WindowFlags f = widget->windowFlags();
    traits->windowDecoration = ( f & Qt::WindowTitleHint ) &&
                            ( f & Qt::WindowMinMaxButtonsHint ) &&
                            ( f & Qt::WindowSystemMenuHint );
    QSizePolicy sp = widget->sizePolicy();
    traits->supportsResize = sp.horizontalPolicy() != QSizePolicy::Fixed ||
                            sp.verticalPolicy() != QSizePolicy::Fixed;

    return traits;
}
Пример #14
0
// Apply a simple variant type to a DOM property
static bool applySimpleProperty(const QVariant &v, bool translateString, DomProperty *dom_prop)
{
    switch (v.type()) {
    case QVariant::String: {
        DomString *str = new DomString();
        str->setText(v.toString());
        if (!translateString)
            str->setAttributeNotr(QStringLiteral("true"));
        dom_prop->setElementString(str);
    }
        return true;

    case QVariant::ByteArray:
        dom_prop->setElementCstring(QString::fromUtf8(v.toByteArray()));
        return true;

    case QVariant::Int:
        dom_prop->setElementNumber(v.toInt());
        return true;

    case QVariant::UInt:
        dom_prop->setElementUInt(v.toUInt());
        return true;

    case QVariant::LongLong:
        dom_prop->setElementLongLong(v.toLongLong());
        return true;

    case QVariant::ULongLong:
        dom_prop->setElementULongLong(v.toULongLong());
        return true;

    case QVariant::Double:
        dom_prop->setElementDouble(v.toDouble());
        return true;

    case QVariant::Bool:
        dom_prop->setElementBool(v.toBool() ? QFormBuilderStrings::instance().trueValue : QFormBuilderStrings::instance().falseValue);
        return true;

    case QVariant::Char: {
        DomChar *ch = new DomChar();
        const QChar character = v.toChar();
        ch->setElementUnicode(character.unicode());
        dom_prop->setElementChar(ch);
    }
        return true;

    case QVariant::Point: {
        DomPoint *pt = new DomPoint();
        const QPoint point = v.toPoint();
        pt->setElementX(point.x());
        pt->setElementY(point.y());
        dom_prop->setElementPoint(pt);
    }
        return true;

    case QVariant::PointF: {
        DomPointF *ptf = new DomPointF();
        const QPointF pointf = v.toPointF();
        ptf->setElementX(pointf.x());
        ptf->setElementY(pointf.y());
        dom_prop->setElementPointF(ptf);
    }
        return true;

    case QVariant::Color: {
        DomColor *clr = new DomColor();
        const QColor color = qvariant_cast<QColor>(v);
        clr->setElementRed(color.red());
        clr->setElementGreen(color.green());
        clr->setElementBlue(color.blue());
        const int alphaChannel = color.alpha();
        if (alphaChannel != 255)
            clr->setAttributeAlpha(alphaChannel);
        dom_prop->setElementColor(clr);
    }
        return true;

    case QVariant::Size: {
        DomSize *sz = new DomSize();
        const QSize size = v.toSize();
        sz->setElementWidth(size.width());
        sz->setElementHeight(size.height());
        dom_prop->setElementSize(sz);
    }
        return true;

    case QVariant::SizeF: {
        DomSizeF *szf = new DomSizeF();
        const QSizeF sizef = v.toSizeF();
        szf->setElementWidth(sizef.width());
        szf->setElementHeight(sizef.height());
        dom_prop->setElementSizeF(szf);
    }
        return true;

    case QVariant::Rect: {
        DomRect *rc = new DomRect();
        const QRect rect = v.toRect();
        rc->setElementX(rect.x());
        rc->setElementY(rect.y());
        rc->setElementWidth(rect.width());
        rc->setElementHeight(rect.height());
        dom_prop->setElementRect(rc);
    }
        return true;

    case QVariant::RectF: {
        DomRectF *rcf = new DomRectF();
        const QRectF rectf = v.toRectF();
        rcf->setElementX(rectf.x());
        rcf->setElementY(rectf.y());
        rcf->setElementWidth(rectf.width());
        rcf->setElementHeight(rectf.height());
        dom_prop->setElementRectF(rcf);
    }
        return true;

    case QVariant::Font: {
        DomFont *fnt = new DomFont();
        const QFont font = qvariant_cast<QFont>(v);
        const uint mask = font.resolve();
        if (mask & QFont::WeightResolved) {
            fnt->setElementBold(font.bold());
            fnt->setElementWeight(font.weight());
        }
        if (mask & QFont::FamilyResolved)
            fnt->setElementFamily(font.family());
        if (mask & QFont::StyleResolved)
            fnt->setElementItalic(font.italic());
        if (mask & QFont::SizeResolved)
            fnt->setElementPointSize(font.pointSize());
        if (mask & QFont::StrikeOutResolved)
            fnt->setElementStrikeOut(font.strikeOut());
        if (mask & QFont::UnderlineResolved)
            fnt->setElementUnderline(font.underline());
        if (mask & QFont::KerningResolved)
            fnt->setElementKerning(font.kerning());
        if (mask & QFont::StyleStrategyResolved) {
            const QMetaEnum styleStrategy_enum = metaEnum<QAbstractFormBuilderGadget>("styleStrategy");
            fnt->setElementStyleStrategy(QLatin1String(styleStrategy_enum.valueToKey(font.styleStrategy())));
        }
        dom_prop->setElementFont(fnt);
    }
        return true;

#ifndef QT_NO_CURSOR
    case QVariant::Cursor: {
        const QMetaEnum cursorShape_enum = metaEnum<QAbstractFormBuilderGadget>("cursorShape");
        dom_prop->setElementCursorShape(QLatin1String(cursorShape_enum.valueToKey(qvariant_cast<QCursor>(v).shape())));
        }
        return true;
#endif

    case QVariant::KeySequence: {
        DomString *s = new DomString();
        s->setText(qvariant_cast<QKeySequence>(v).toString(QKeySequence::PortableText));
        dom_prop->setElementString(s);
        }
        return true;

    case QVariant::Locale: {
        DomLocale *dom = new DomLocale();
        const QLocale locale = qvariant_cast<QLocale>(v);

        const QMetaEnum language_enum = metaEnum<QAbstractFormBuilderGadget>("language");
        const QMetaEnum country_enum = metaEnum<QAbstractFormBuilderGadget>("country");

        dom->setAttributeLanguage(QLatin1String(language_enum.valueToKey(locale.language())));
        dom->setAttributeCountry(QLatin1String(country_enum.valueToKey(locale.country())));

        dom_prop->setElementLocale(dom);
        }
        return true;

    case QVariant::SizePolicy: {
        DomSizePolicy *dom = new DomSizePolicy();
        const QSizePolicy sizePolicy = qvariant_cast<QSizePolicy>(v);

        dom->setElementHorStretch(sizePolicy.horizontalStretch());
        dom->setElementVerStretch(sizePolicy.verticalStretch());

        const QMetaEnum sizeType_enum = metaEnum<QAbstractFormBuilderGadget>("sizeType");

        dom->setAttributeHSizeType(QLatin1String(sizeType_enum.valueToKey(sizePolicy.horizontalPolicy())));
        dom->setAttributeVSizeType(QLatin1String(sizeType_enum.valueToKey(sizePolicy.verticalPolicy())));

        dom_prop->setElementSizePolicy(dom);
    }
        return true;

    case QVariant::Date: {
        DomDate *dom = new DomDate();
        const QDate date = qvariant_cast<QDate>(v);

        dom->setElementYear(date.year());
        dom->setElementMonth(date.month());
        dom->setElementDay(date.day());

        dom_prop->setElementDate(dom);
        }
        return true;

    case QVariant::Time: {
        DomTime *dom = new DomTime();
        const QTime time = qvariant_cast<QTime>(v);

        dom->setElementHour(time.hour());
        dom->setElementMinute(time.minute());
        dom->setElementSecond(time.second());

        dom_prop->setElementTime(dom);
        }
        return true;

    case QVariant::DateTime: {
        DomDateTime *dom = new DomDateTime();
        const QDateTime dateTime = qvariant_cast<QDateTime>(v);

        dom->setElementHour(dateTime.time().hour());
        dom->setElementMinute(dateTime.time().minute());
        dom->setElementSecond(dateTime.time().second());
        dom->setElementYear(dateTime.date().year());
        dom->setElementMonth(dateTime.date().month());
        dom->setElementDay(dateTime.date().day());

        dom_prop->setElementDateTime(dom);
    }
        return true;

    case QVariant::Url: {
        DomUrl *dom = new DomUrl();
        const QUrl url = v.toUrl();

        DomString *str = new DomString();
        str->setText(url.toString());
        dom->setElementString(str);

        dom_prop->setElementUrl(dom);
    }
        return true;

    case QVariant::StringList: {
        DomStringList *sl = new DomStringList;
        sl->setElementString(qvariant_cast<QStringList>(v));
        dom_prop->setElementStringList(sl);
    }
        return true;

    default:
        break;
    }

    return false;
}
Пример #15
0
bool DockableWidgetLayout::insertLocation(
	QRect& rect, int& index, DockSide& side, const QSizePolicy& sizePol)
{
	// best insertion data
	// Distance is a number that represents the how far
	// the insertion rectangle is from the final location.
	unsigned bestDistance = 0xFFFFFFFF;
	int bestIndex = 0;
	DockSide bestSide;
	QRect bestRect;

	// loop over all widgets and find appropriate matching sides
	for (int i = 0; i < dockedWidgets.size(); ++i) {
		DockInfo* d = dockedWidgets[i];
		/*****************************************************
		 * Check for placement against the top of the widget *
		 *****************************************************/
		if (i == 0 || d->dockSide != BOTTOM) {
			if (!(rect.left()  > d->right() - SNAP_DISTANCE ||
			      rect.right() < d->left    + SNAP_DISTANCE) &&
			    isClose(rect.bottom(), d->top)) {
				// rectangle is close to the edge
				unsigned dist = 8 * abs(rect.bottom() - d->top);
				// now find all points on this side
				// (use set as a sorted unique list)
				QSet<int> sidePoints;
				for (int j = 0; j <= i; ++j) {
					DockInfo* d2 = dockedWidgets[j];
					if (d->top == d2->top) {
						sidePoints.insert(d2->left);
						sidePoints.insert(d2->right());
						// check if any other widget rest against this side
						for (int k = i + 1; k < dockedWidgets.size(); ++k) {
							DockInfo* d3 = dockedWidgets[k];
							if (d3->bottom() == d2->top) {
								sidePoints.insert(d3->left);
								sidePoints.insert(d3->right());
							}
						}
					}
				}
				// widget placement can occur at all points, find the closest
				QSet<int>::iterator it = sidePoints.begin();
				for (int j = 0; j < sidePoints.size() - 1; ++j) {
					// check after point
					unsigned newDist1 = dist + abs(*it - rect.left());
					if (newDist1 < bestDistance && isClose(*it, rect.left())) {
						QRect r(QPoint(*it, d->top - rect.height()), rect.size());
						if (!overlaysWithFirstNWidgets(r, i)) {
							bestDistance = newDist1;
							bestIndex = i + 1;
							bestSide = TOP;
							bestRect = r;
						}
					}
					++it;
					// check before point
					unsigned newDist2 = dist + abs(*it - rect.right());
					if (newDist2 < bestDistance && isClose(*it, rect.right())) {
						QRect r(QPoint(*it - rect.width(), d->top - rect.height()),
						        rect.size());
						if (!overlaysWithFirstNWidgets(r, i)) {
							bestDistance = newDist2;
							bestIndex = i + 1;
							bestSide = TOP;
							bestRect = r;
						}
					}
				}
				// check for resized placement options
				if (sizePol.horizontalPolicy() != QSizePolicy::Fixed) {
					int mid = rect.left() + rect.width() / 2;
					for (QSet<int>::iterator ita = sidePoints.begin() + 1;
					     ita != sidePoints.end(); ++ita) {
						for (QSet<int>::iterator itb = sidePoints.begin();
						     ita != itb; ++itb) {
							int sp_mid = (*ita + *itb) / 2;
							int sp_diff = *ita - *itb;
							if (isClose(sp_mid, mid)) {
								QRect r(*itb, d->top - rect.height(),
								        sp_diff, rect.height());
								if (!overlaysWithFirstNWidgets(r, i)) {
									bestDistance = dist + abs(sp_mid - mid);
									bestIndex = i + 1;
									bestSide = TOP;
									bestRect = r;
								}
							}
						}
					}
				}
			}
		}

		/********************************************************
		 * Check for placement against the bottom of the widget *
		 ********************************************************/
		if (i == 0 || d->dockSide != TOP) {
			if (!(rect.left()  > d->right() - SNAP_DISTANCE ||
			      rect.right() < d->left    + SNAP_DISTANCE) &&
			    isClose(rect.top(), d->bottom())) {
				// rectangle is close to the edge
				unsigned dist = 8 * abs(rect.top() - d->bottom());
				// now find all points on this side
				// (use set as a sorted unique list)
				QSet<int> sidePoints;
				for (int j = 0; j <= i; ++j) {
					DockInfo* d2 = dockedWidgets[j];
					if (d->bottom() == d2->bottom()) {
						sidePoints.insert(d2->left);
						sidePoints.insert(d2->right());
						// check if any other widget rest against this side
						for (int k = i + 1; k < dockedWidgets.size(); ++k) {
							DockInfo* d3 = dockedWidgets[k];
							if (d3->top == d2->bottom()) {
								sidePoints.insert(d3->left);
								sidePoints.insert(d3->right());
							}
						}
					}
				}
				// widget placement can occur at all points, find the closest
				QSet<int>::iterator it = sidePoints.begin();
				for (int j = 0; j < sidePoints.size() - 1; ++j) {
					// check after point
					unsigned newDist1 = dist + abs(*it - rect.left());
					if (newDist1 < bestDistance && isClose(*it, rect.left())) {
						QRect r(QPoint(*it, d->bottom()), rect.size());
						if (!overlaysWithFirstNWidgets(r, i)) {
							bestDistance = newDist1;
							bestIndex = i + 1;
							bestSide = BOTTOM;
							bestRect = r;
						}
					}
					++it;
					// check before point
					unsigned newDist2 = dist + abs(*it - rect.right());
					if (newDist2 < bestDistance && isClose(*it, rect.right())) {
						QRect r(QPoint(*it - rect.width(), d->bottom()), rect.size());
						if (!overlaysWithFirstNWidgets(r, i)) {
							bestDistance = newDist2;
							bestIndex = i + 1;
							bestSide = BOTTOM;
							bestRect = r;
						}
					}
				}
				// check for resized placement options
				if (sizePol.horizontalPolicy() != QSizePolicy::Fixed) {
					int mid = rect.left() + rect.width() / 2;
					for (QSet<int>::iterator ita = sidePoints.begin() + 1;
					     ita != sidePoints.end(); ++ita) {
						for (QSet<int>::iterator itb = sidePoints.begin();
						     ita != itb; ++itb) {
							int sp_mid = (*ita + *itb) / 2;
							int sp_diff = *ita - *itb;
							if (isClose(sp_mid, mid)) {
								QRect r(*itb, d->bottom(),
								        sp_diff, rect.height());
								if (!overlaysWithFirstNWidgets(r, i)) {
									bestDistance = dist + abs(sp_mid - mid);
									bestIndex = i + 1;
									bestSide = BOTTOM;
									bestRect = r;
								}
							}
						}
					}
				}
			}
		}

		/******************************************************
		 * Check for placement against the left of the widget *
		 ******************************************************/
		if (i == 0 || d->dockSide != RIGHT) {
			if (!(rect.top()    > d->bottom() - SNAP_DISTANCE ||
			      rect.bottom() < d->top      + SNAP_DISTANCE) &&
			    isClose(rect.right(), d->left)) {
				// rectangle is close to the edge
				unsigned dist = 8 * abs(rect.right() - d->left);
				// now find all points on this side
				// (use set as a sorted unique list)
				QSet<int> sidePoints;
				for (int j = 0; j <= i; ++j) {
					DockInfo* d2 = dockedWidgets[j];
					if (d->left == d2->left) {
						sidePoints.insert(d2->top);
						sidePoints.insert(d2->bottom());
						// check if any other widget rest against this side
						for (int k = i + 1; k < dockedWidgets.size(); ++k) {
							DockInfo* d3 = dockedWidgets[k];
							if (d3->right() == d2->left) {
								sidePoints.insert(d3->top);
								sidePoints.insert(d3->bottom());
							}
						}
					}
				}
				// widget placement can occur at all points, find the closest
				QSet<int>::iterator it = sidePoints.begin();
				for (int j = 0; j < sidePoints.size() - 1; ++j) {
					// check after point
					unsigned newDist1 = dist + abs(*it - rect.top());
					if (newDist1 < bestDistance && isClose(*it, rect.top())) {
						QRect r(QPoint(d->left - rect.width(), *it), rect.size());
						if (!overlaysWithFirstNWidgets(r, i)) {
							bestDistance = newDist1;
							bestIndex = i + 1;
							bestSide = LEFT;
							bestRect = r;
						}
					}
					++it;
					// check before point
					unsigned newDist2 = dist + abs(*it - rect.bottom());
					if (newDist2 < bestDistance && isClose(*it, rect.bottom())) {
						QRect r(QPoint(d->left - rect.width(), *it - rect.height()),
						        rect.size());
						if (!overlaysWithFirstNWidgets(r, i)) {
							bestDistance = newDist2;
							bestIndex = i + 1;
							bestSide = LEFT;
							bestRect = r;
						}
					}
				}
				// check for resized placement options
				if (sizePol.verticalPolicy() != QSizePolicy::Fixed) {
					int mid = rect.top() + rect.height() / 2;
					for (QSet<int>::iterator ita = sidePoints.begin() + 1;
					     ita != sidePoints.end(); ++ita) {
						for (QSet<int>::iterator itb = sidePoints.begin();
						     ita != itb; ++itb) {
							int sp_mid = (*ita + *itb) / 2;
							int sp_diff = *ita - *itb;
							if (isClose(sp_mid, mid)) {
								QRect r(d->left - rect.width(), *itb,
								        rect.width(), sp_diff);
								if (!overlaysWithFirstNWidgets(r, i)) {
									bestDistance = dist + abs(sp_mid - mid);
									bestIndex = i + 1;
									bestSide = LEFT;
									bestRect = r;
								}
							}
						}
					}
				}
			}
		}
		/*******************************************************
		 * Check for placement against the right of the widget *
		 *******************************************************/
		if (i == 0 || d->dockSide != LEFT) {
			if (!(rect.top()    > d->bottom() - SNAP_DISTANCE ||
			      rect.bottom() < d->top      + SNAP_DISTANCE) &&
			    isClose(rect.left(), d->right())) {
				// rectangle is close to the edge
				unsigned dist = 8 * abs(rect.left() - d->right());
				// now find all points on this side
				// (use set as a sorted unique list)
				QSet<int> sidePoints;
				for (int j = 0; j <= i; ++j) {
					DockInfo* d2 = dockedWidgets[j];
					if (d->right() == d2->right()) {
						sidePoints.insert(d2->top);
						sidePoints.insert(d2->bottom());
						// check if any other widget rest against this side
						for (int k = i + 1; k < dockedWidgets.size(); ++k) {
							DockInfo* d3 = dockedWidgets[k];
							if (d3->left == d2->right()) {
								sidePoints.insert(d3->top);
								sidePoints.insert(d3->bottom());
							}
						}
					}
				}
				// widget placement can occur at all points, find the closest
				QSet<int>::iterator it = sidePoints.begin();
				for (int j = 0; j < sidePoints.size() - 1; ++j) {
					// check after point
					unsigned newDist1 = dist + abs(*it - rect.top());
					if (newDist1 < bestDistance && isClose(*it, rect.top())) {
						QRect r(QPoint(d->left + d->width, *it), rect.size());
						if (!overlaysWithFirstNWidgets(r, i)) {
							bestDistance = newDist1;
							bestIndex = i + 1;
							bestSide = RIGHT;
							bestRect = r;
						}
					}
					++it;
					// check before point
					unsigned newDist2 = dist + abs(*it - rect.bottom());
					if (newDist2 < bestDistance && isClose(*it, rect.bottom())) {
						QRect r(QPoint(d->right(), *it - rect.height()), rect.size());
						if (!overlaysWithFirstNWidgets(r, i)) {
							bestDistance = newDist2;
							bestIndex = i + 1;
							bestSide = RIGHT;
							bestRect = r;
						}
					}
				}
				// check for resized placement options
				if (sizePol.verticalPolicy() != QSizePolicy::Fixed) {
					int mid = rect.top() + rect.height() / 2;
					for (QSet<int>::iterator ita = sidePoints.begin() + 1;
					     ita != sidePoints.end(); ++ita) {
						for (QSet<int>::iterator itb = sidePoints.begin();
						     ita != itb; ++itb) {
							int sp_mid = (*ita + *itb) / 2;
							int sp_diff = *ita - *itb;
							if (isClose(sp_mid, mid)) {
								QRect r(d->right(), *itb, rect.width(), sp_diff);
								if (!overlaysWithFirstNWidgets(r, i)) {
									bestDistance = dist + abs(sp_mid - mid);
									bestIndex = i + 1;
									bestSide = RIGHT;
									bestRect = r;
								}
							}
						}
					}
				}
			}
		}
	}

	if (bestIndex) {
		rect = bestRect;
		index = bestIndex;
		side = bestSide;
		return true;
	} else {
		return false;
	}
}
Пример #16
0
QDebug operator<<(QDebug dbg, const QSizePolicy &p)
{
    dbg.nospace() << "QSizePolicy(horizontalPolicy = " << p.horizontalPolicy()
                  << ", verticalPolicy = " << p.verticalPolicy() << ')';
    return dbg.space();
}