Home · All Classes · Main Classes · Deprecated
Public Member Functions | Protected Member Functions

MFreestyleLayoutPolicy Class Reference

Freestyle layout policy. More...

Inheritance diagram for MFreestyleLayoutPolicy:
Inheritance graph
[legend]
Collaboration diagram for MFreestyleLayoutPolicy:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 MFreestyleLayoutPolicy (MLayout *layout)
virtual ~MFreestyleLayoutPolicy ()
virtual void addItem (QGraphicsLayoutItem *item)
void addItemAtGeometry (QGraphicsLayoutItem *item, const QRectF &geom)
void moveItemTo (QGraphicsLayoutItem *item, const QRectF &geom)

Protected Member Functions

virtual void relayout ()

Detailed Description

Freestyle layout policy.

This policy arranges the items as much as possible according to the user's wishes but avoids any overlap. The height is computed as needed.

The following example adds items to the freestyle layout policy in a circle shape:

    /* Create a MLayout that we set the policy for */
    MLayout *layout = new MLayout;
    MFreestyleLayoutPolicy *policy = new MFreestyleLayoutPolicy(layout);
    policy->setSpacing(10);

    MApplicationWindow window;
    MApplicationPage page;
    /* Attach the layout to the page */
    page.centralWidget()->setLayout(layout);
    page.appear(&window);
    window.show();

    int n = 20;
    for (int i = 0; i < n; ++i) {
        MLabel *label = new MLabel(QString("Item %1").arg(i + 1));
        label->setObjectName("item");
        //Place the items in an ellipse with the width and height of the page
        int center_x = label->preferredWidth() / 2 + (page.geometry().width() - label->preferredWidth()) / 2 * (1 - cos(2 * 3.13 * i / n));
        int center_y = 25 + (page.geometry().height() - 50) / 3 * (1 - sin(2 * 3.13 * i / n));
        policy->addItemAtGeometry(label, QRect(center_x - label->preferredWidth() / 2, center_y - label->preferredHeight() / 2, label->preferredWidth(), label->preferredHeight()));
        label->setAlignment(Qt::AlignCenter);
    }

The result, with appropriate CSS styling, looks like:

mfreestylelayoutpolicy.jpg
See also:
MFreestyleLayoutPolicy Example

Definition at line 46 of file corelib/layout/mfreestylelayoutpolicy.h.


Constructor & Destructor Documentation

MFreestyleLayoutPolicy::MFreestyleLayoutPolicy ( MLayout layout  )  [explicit]

Constructs a freestyle layout policy.

Definition at line 23 of file mfreestylelayoutpolicy.cpp.

                                                              :
    MAbstractLayoutPolicy(*(new MFreestyleLayoutPolicyPrivate(layout)))
{ }

MFreestyleLayoutPolicy::~MFreestyleLayoutPolicy (  )  [virtual]

Destroys the freestyle layout policy.

Definition at line 27 of file mfreestylelayoutpolicy.cpp.

{ }


Member Function Documentation

void MFreestyleLayoutPolicy::addItem ( QGraphicsLayoutItem item  )  [virtual]

Add an item to the top left hand corner in the freestyle layout.

If the new item overlaps any existing items, the existing items will be moved out of the way.

The item will be given its preferredSize.

Parameters:
item The item to add.

Reimplemented from MAbstractLayoutPolicy.

Definition at line 30 of file mfreestylelayoutpolicy.cpp.

{
    addItemAtGeometry(item, QRectF(contentsArea().topLeft(), item->effectiveSizeHint(Qt::PreferredSize)));
}

Here is the call graph for this function:

void MFreestyleLayoutPolicy::addItemAtGeometry ( QGraphicsLayoutItem item,
const QRectF geom 
)

Add an item at a specific geometry in the freestyle layout.

If the new item overlaps any existing items, the existing items will be moved out of the way.

Parameters:
item The item to add.
geom The geometry where to add the item.

Definition at line 35 of file mfreestylelayoutpolicy.cpp.

{
    Q_D(MFreestyleLayoutPolicy);

    MAbstractLayoutPolicy::addItem(item);

    QPointF topLeft = contentsArea().topLeft();

    //set the item's geometry to geom, bounded by the item's minimum and maximum
    //size.
    QSizeF maximum = item->effectiveSizeHint(Qt::MaximumSize);
    QSizeF minimum = item->effectiveSizeHint(Qt::MinimumSize);
    QRectF new_geometry = QRectF(geom.topLeft(), geom.size().boundedTo(maximum).expandedTo(minimum));

    QList<QRectF> new_geometries;
    const int size = count();
    for (int i = 0; i < size - 1; ++i)
        new_geometries << itemGeometry(i);

    //Add the new item to the list of geometries and place it
    new_geometries << new_geometry;
    d->placeItem(size - 1, new_geometries, topLeft, contentsArea().width());

    for (int i = 0; i < size; ++i)
        setItemGeometry(i, new_geometries.at(i));

    updateGeometry();
}

Here is the call graph for this function:

void MFreestyleLayoutPolicy::moveItemTo ( QGraphicsLayoutItem item,
const QRectF geom 
)

Move an item to the given geometry.

The given item will be animated to the given geometry, if possible, moving any overlapping items out of the way, if possible.

Definition at line 64 of file mfreestylelayoutpolicy.cpp.

{
    Q_D(MFreestyleLayoutPolicy);

    int index = indexOf(item);
    if (index < 0)
        return; //Item not found

    QPointF topLeft = contentsArea().topLeft();
    QList<QRectF> new_geometries;

    const int size = count();
    for (int i = 0; i < size; ++i)
        new_geometries << itemGeometry(i);

    new_geometries[index] = geom;

    d->placeItem(index, new_geometries, topLeft, contentsArea().width());

    for (int i = 0; i < size; ++i)
        setItemGeometry(i, new_geometries.at(i));
    updateGeometry();
}

Here is the call graph for this function:

void MFreestyleLayoutPolicy::relayout (  )  [protected, virtual]

Callback triggered when a relayout is required.

This method is triggered whenever complete relayout is required (e.g. when the layout itself is resized).

All affected items are computed and the layout is triggered to update the information.

You must override this method in your custom policies.

Implements MAbstractLayoutPolicy.

Definition at line 88 of file mfreestylelayoutpolicy.cpp.

{
    Q_D(MFreestyleLayoutPolicy);

    QPointF topLeft = contentsArea().topLeft();

    QList<QRectF> new_geometries;
    const int size = count();
    for (int i = 0; i < size; ++i) {
        new_geometries << itemGeometry(i);
        d->placeItem(i, new_geometries, topLeft, contentsArea().width());
    }

    QRectF area = contentsArea();
    for (int i = 0; i < size; ++i) {
        area = area.united(new_geometries.at(i));
        setItemGeometry(i, new_geometries.at(i));
    }

    //if we have moved items outside of the bounding box, we need to invalidate the layout
    //causing another relayout, but we have to be careful that we don't end up in an
    //infinite loop because of this.
    if (area.isValid() && !contentsArea().adjusted(-0.0000001, -0.0000001, 0.0000001, 0.0000001).contains(area)) //adjust for rounding errors
        updateGeometry();
}

Here is the call graph for this function:


Copyright © 2010 Nokia Corporation Generated on Thu Nov 4 2010 18:14:25 (PDT)
Doxygen 1.7.1
MeeGo Touch