Make the placement algorithm slightly more intelligent

svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=751696
This commit is contained in:
Jason Stubbs 2007-12-22 16:42:09 +00:00
parent d4e9929555
commit 03ae8a076b

View File

@ -437,34 +437,33 @@ Applet* Containment::addApplet(const QString& name, const QVariantList& args, ui
QRectF Containment::geometryForApplet(Applet *applet) const QRectF Containment::geometryForApplet(Applet *applet) const
{ {
// first try the top left of the containment // The value part of these maps isn't used. Only sorted keys are needed.
QRectF placement = QRectF(geometry().topLeft(), applet->sizeHint()); QMap<qreal, bool> xPositions;
if (regionIsEmpty(placement, applet)) { QMap<qreal, bool> yPositions;
return placement;
}
QList<Applet *> otherApplets = d->applets; // Add the top-left corner offset by the applet's border
otherApplets.removeAll(applet); QPointF offset = applet->boundingRect().topLeft();
xPositions[-offset.x()] = true;
yPositions[-offset.y()] = true;
// Then below existing applets QRectF placement(QPointF(0, 0), applet->sizeHint());
foreach (Applet *otherApplet, otherApplets) { foreach (Applet *existingApplet, d->applets) {
placement.moveTo(otherApplet->pos() + QPointF(0, otherApplet->size().height())); QPointF bottomRight = existingApplet->geometry().bottomRight();
if (!geometry().contains(placement)) { if (bottomRight.x() + placement.width() < geometry().width()) {
continue; xPositions[bottomRight.x() + 1] = true;
} }
if (regionIsEmpty(placement, applet)) { if (bottomRight.y() + placement.height() < geometry().height()) {
return placement; yPositions[bottomRight.y() + 1] = true;
} }
} }
// Then to the right // Try to fit it in an empty space
foreach (Applet *otherApplet, otherApplets) { foreach (qreal x, xPositions.keys()) {
placement.moveTo(otherApplet->pos() + QPointF(otherApplet->size().width(), 0)); foreach (qreal y, yPositions.keys()) {
if (!geometry().contains(placement)) { placement.moveTo(x, y);
continue; if (regionIsEmpty(placement, applet)) {
} return placement;
if (regionIsEmpty(placement, applet)) { }
return placement;
} }
} }