aboutsummaryrefslogtreecommitdiff
path: root/src/layout/duiflowlayoutpolicy.h
blob: 04385897b23432f167e44b79d504166118b1c44d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (directui@nokia.com)
**
** This file is part of libdui.
**
** If you have questions regarding the use of this file, please contact
** Nokia at directui@nokia.com.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/

#ifndef DUIFLOWLAYOUTPOLICY_H
#define DUIFLOWLAYOUTPOLICY_H

#include "duiabstractlayoutpolicy.h"
#include "duilayout.h"

class DuiFlowLayoutPolicyPrivate;

/*!
 *  \class DuiFlowLayoutPolicy
 *
 *  \brief This policy implements a simple flow layout.
 *  \ingroup layouts
 *
 *  The items in the policy are organised into rows, starting from the top left.
 *  The size of each item is initially determined by its
 *  \link QGraphicsLayoutItem::preferredSize() preferredSize()\endlink.
 *  Items with an expanding \link QGraphicsLayoutItem::sizePolicy() sizePolicy()\endlink
 *  are then expanded horizontally and vertically to fill any left over available space.
 *
 *  The following example adds 10 labels to the flow layout policy:
 *
 *  \dontinclude duiflowlayoutpolicy/duiflowlayoutpolicy.cpp
 *  \skip Create a DuiLayout
 *  \until }
 *
 *  The result, with appropriate CSS styling, looks like:
 *  \image html duiflowlayoutpolicy.png
 *
 *  \sa \ref layout-duiflowlayoutpolicy
 *
 *  \section duibuttonsInFlowLayout Using DuiButton in a DuiFlowLayoutPolicy
 *
 *  In the default CSS theme, a DuiButton has a fixed \link QGraphicsLayoutItem::preferredWidth() preferredWidth()\endlink,
 *  which does not depend on the text inside of it.  This makes sense normally for buttons,
 *  but does not usually produce the desired behaviour in a flow layout.
 *  Every DuiButton in a DuiFlowLayoutPolicy will have the same width.
 *
 */
class DUI_EXPORT DuiFlowLayoutPolicy : public DuiAbstractLayoutPolicy
{
public:
    /*!
     * \brief Constructs a flow layout policy.
     */
    explicit DuiFlowLayoutPolicy(DuiLayout *);

    /*!
     * \brief Destroys a flow layout policy.
     *
     * \sa ~DuiAbstractLayoutPolicy()
     */
    virtual ~DuiFlowLayoutPolicy();

    /*!
     * \brief Return the number of rows that the items in the flow layout occupy in its current geometry.
     *
     * The number of rows depends on the layout's current geometry, and the layout's geometry is not usually
     * set until show() has been called.  This will not be greater than rowLimit(), if set.
     *
     * If the policy is not currently active, and has since been \link invalidate() invalidated\endlink, this will return -1.
     * If the policy is currently active and has been invalidated, this will trigger a relayout.
     *
     * @return The number of rows in the current geometry, or -1 if \link invalidate() invalidated\endlink and not active
     */
    int rowCount();

    /*! \brief Set the maximum number of rows to show.
     *
     *  No more than @p rowLimit rows will be shown.  The rest of the items will be hidden.
     *
     *  The default is -1, indicating no row limit */
    void setRowLimit(int rowLimit);

    /*! \brief Return the maximum number of rows to show.
     *
     *  No more than @p rowLimit rows will be shown.  The rest of the items will be hidden.
     *
     *  The default is -1, indicating no row limit */
    int rowLimit() const;

    /*! \reimp */
    virtual QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const;
    virtual void addItem(QGraphicsLayoutItem *item);
    virtual void insertItem(int index, QGraphicsLayoutItem *item);

    virtual void invalidate();
protected:
    virtual void relayout();
    virtual bool hasHeightForWidth() const {
        return true;
    }
    /*! \reimp_end */

private:
    Q_DISABLE_COPY(DuiFlowLayoutPolicy)
    Q_DECLARE_PRIVATE(DuiFlowLayoutPolicy)
};

#endif // Header Guard