From 03ae8a076bfeffb71e4953f86fd1a8abb5d34e35 Mon Sep 17 00:00:00 2001 From: Jason Stubbs Date: Sat, 22 Dec 2007 16:42:09 +0000 Subject: [PATCH] Make the placement algorithm slightly more intelligent svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=751696 --- containment.cpp | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/containment.cpp b/containment.cpp index 50cd0c681..8ddff09a0 100644 --- a/containment.cpp +++ b/containment.cpp @@ -437,34 +437,33 @@ Applet* Containment::addApplet(const QString& name, const QVariantList& args, ui QRectF Containment::geometryForApplet(Applet *applet) const { - // first try the top left of the containment - QRectF placement = QRectF(geometry().topLeft(), applet->sizeHint()); - if (regionIsEmpty(placement, applet)) { - return placement; - } + // The value part of these maps isn't used. Only sorted keys are needed. + QMap xPositions; + QMap yPositions; - QList otherApplets = d->applets; - otherApplets.removeAll(applet); + // Add the top-left corner offset by the applet's border + QPointF offset = applet->boundingRect().topLeft(); + xPositions[-offset.x()] = true; + yPositions[-offset.y()] = true; - // Then below existing applets - foreach (Applet *otherApplet, otherApplets) { - placement.moveTo(otherApplet->pos() + QPointF(0, otherApplet->size().height())); - if (!geometry().contains(placement)) { - continue; + QRectF placement(QPointF(0, 0), applet->sizeHint()); + foreach (Applet *existingApplet, d->applets) { + QPointF bottomRight = existingApplet->geometry().bottomRight(); + if (bottomRight.x() + placement.width() < geometry().width()) { + xPositions[bottomRight.x() + 1] = true; } - if (regionIsEmpty(placement, applet)) { - return placement; + if (bottomRight.y() + placement.height() < geometry().height()) { + yPositions[bottomRight.y() + 1] = true; } } - // Then to the right - foreach (Applet *otherApplet, otherApplets) { - placement.moveTo(otherApplet->pos() + QPointF(otherApplet->size().width(), 0)); - if (!geometry().contains(placement)) { - continue; - } - if (regionIsEmpty(placement, applet)) { - return placement; + // Try to fit it in an empty space + foreach (qreal x, xPositions.keys()) { + foreach (qreal y, yPositions.keys()) { + placement.moveTo(x, y); + if (regionIsEmpty(placement, applet)) { + return placement; + } } }