aboutsummaryrefslogtreecommitdiff
path: root/src/corelib/layout/mlinearlayoutpolicy.h
blob: 0076ddfd53763be0c7749d8366ba76e99488b6b7 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (directui@nokia.com)
**
** This file is part of libmeegotouch.
**
** 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 MLINEARLAYOUTPOLICY_H
#define MLINEARLAYOUTPOLICY_H

#include "mabstractlayoutpolicy.h"

class MLinearLayoutPolicyPrivate;
class QGraphicsLayoutItem;

/*!
 * \class MLinearLayoutPolicy
 * \brief Policy that uses the Qt grid layout engine to provide a horizontal or vertical linear layout.
 * \ingroup layouts
 *
 * This class provides a policy which is similar to QGraphicsLinearLayout, with the advantage
 * of allowing multiple policies and animation.  You can use QGraphicsLinearLayout instead
 * to slightly reduce memory overhead if these advantages are not required.
 *
 * The following example adds items to the linear layout policy:
 *
 * \dontinclude mlinearlayoutpolicy/mlinearlayoutpolicy.cpp
 * \skip Create a MLayout
 * \until }
 *
 * The result, with appropriate CSS styling, looks like:
 * \image html mlinearlayoutpolicy.jpg
 *
 * \sa \ref layout-mlinearlayoutpolicy
 *
 * \section using_qt_instead Using QGraphicsLinearLayout instead
 *
 * If you do not need animations or multiple policies, you can use QGraphicsLinearLayout for same effect in less code.
 * For example:
 *
 * \dontinclude qgraphicslinearlayout/qgraphicslinearlayout.cpp
 * \skip Create a linear layout
 * \until }
 * \sa \ref layout-qgraphicslinearlayout, \ref layout-qgraphicslayout
 */
class M_EXPORT MLinearLayoutPolicy : public MAbstractLayoutPolicy
{
public:
    /*!
     * \brief Constructs a linear layout policy.
     *
     * The initial @p orientation is required by the constructor, but can be changed at any time
     * with setOrientation().
     *
     * @param layout The layout to associate with.
     * @param orientation The orientation to use for this layout.
     */
    explicit MLinearLayoutPolicy(MLayout *layout, Qt::Orientation orientation);

    /*!
     * \brief Destroys the linear layout policy.
     */
    virtual ~MLinearLayoutPolicy();

    /*!
     * \brief  Get the spacing.  The spacing is the distance between the items
     *
     * Note that in the CSS file, the attributes are horizontal-spacing and vertical-spacing.
     * The appropriate spacing is used depending on whether the linear layout is horizontal
     * or vertical.
     * The horizontal and vertical spacing can also be set individually with
     * setHorizontalSpacing() and setVerticalSpacing()
     *
     * @return The spacing to use.
     * \sa QGraphicsLinearLayout::spacing()
     */
    qreal spacing() const;

    /*!
     * \brief Set the spacing.  The spacing is the distance between the items
     *
     * \sa QGraphicsLinearLayout::setSpacing()
     */
    virtual void setSpacing(qreal spacing);

    /*!
     * \brief  Set the spacing after the item at @p index.
     *
     * This overrides any spacing set by setSpacing().
     *
     * @param index The position of the item to set the spacing after.
     * @param spacing The spacing to use.
     *
     * \sa itemSpacing(), setSpacing(), spacing(), QGraphicsLinearLayout::setItemSpacing()
     */
    void setItemSpacing(int index, qreal spacing);

    /*!
     * \brief  Get the spacing after the item at the given @p index.
     *
     * This overrides any spacing set by setSpacing().
     *
     * @param index The position of the item to get the spacing after.
     * @return The spacing to use.
     * \sa QGraphicsLinearLayout::itemSpacing()
     */
    qreal itemSpacing(int index) const;

    /*!
     * \brief Returns the layout orientation.
     * \sa setOrientation()
     */
    Qt::Orientation orientation() const;

    /*!
     * \brief Change the layout policy orientation to \a orientation.
     *
     * Changing the policy orientation will automatically invalidate the policy
     * and invalidate the layout if this policy is the current layout policy.
     *
     * \sa orientation()
     */
    void setOrientation(Qt::Orientation orientation);

    /*! \brief Add an item to the end of the linear layout policy.
     *
     * This adds the given item to the end of the linear layout policy, using the default
     * alignment of 0.
     *
     * Equivalent to:
     * \code
     *   insertItem(item, -1);
     * \endcode
     *
     * Note that the order of the items in the policy is independent of the order of the items
     * in the MLayout.
     *
     * @param item The item to add.
     */
    virtual void addItem(QGraphicsLayoutItem *item);

    /*! \brief Add an item to the end of the linear layout policy with the given alignment
     *
     * This adds the given item to the end of the linear layout policy, using the given @p alignment
     *
     * Equivalent to:
     * \code
     *   insertItem(item, -1, alignment);
     * \endcode
     *
     * Note that the order of the items in the policy is independent of the order of the items
     * in the MLayout.
     *
     */
    void addItem(QGraphicsLayoutItem *item, Qt::Alignment alignment);

    /*!
     * \brief Insert an item in the policy at the given index.
     *
     * Inserts @p item into the @p layout at index, or before any item that is currently at @p index.
     *
     * Note that the order of the items in the policy is independent of the order of the items
     * in the MLayout.
     *
     * @param item The item to insert.
     * @param index The index to place the item
     */
    virtual void insertItem(int index, QGraphicsLayoutItem *item);

    /*! \brief Insert an item in the policy at the given index with the given alignment.
     *
     * Inserts @p item into the @p layout at index, or before any item that is currently at @p index.
     *
     * Note that the order of the items in the policy is independent of the order of the items
     * in the MLayout.
     */
    void insertItem(int index, QGraphicsLayoutItem *item, Qt::Alignment alignment);

    /*! \brief Add a stretch with the given \a stretch factor
      *
      * This convenience function is equivalent to calling
      * \code
      *   insertStretch(-1, stretch).
      * \endcode
      *
      * Adding a stretch does not increase the count() of the policy, cannot be refderenced by itemAt(),
      * and cannot be removed.
      *
      */
    inline void addStretch(int stretch = 1) {
        insertStretch(-1, stretch);
    }

    /** \brief Inserts a stretch of \a stretch at \a index, or before any item that is currently at \a index
      *
      * Adding a stretch does not increase the count() of the policy, cannot be refderenced by itemAt(),
      * and cannot be removed.
      *
      * \sa addStretch(), setStretchFactor(), setItemSpacing(), insertItem()
      */
    void insertStretch(int index, int stretch = 1);

    /*! \brief Returns the stretch factor for \a item.
     *
     *  The default stretch factor is 0, meaning that the item has no assigned stretch factor.
     *  \sa setStretchFactor()
     */
    int stretchFactor(QGraphicsLayoutItem *item) const;

    /*! \brief Sets the stretch factor for \a item to \a stretch.
     *
     * If an item's stretch factor changes, this function will invalidate the layout.
     * Setting \a stretch to 0 removes the stretch factor from the item, and is
     * effectively equivalent to setting \a stretch to 1.
     *
     * \sa stretchFactor()
     */
    void setStretchFactor(QGraphicsLayoutItem *item, int stretch);

    /*!
      Returns the alignment for \a item. The default alignment is
      Qt::AlignCenter.

      The alignment decides how the item is positioned within its assigned space
      in the case where there's more space available in the layout than the
      widgets can occupy.

      \sa setAlignment()
     */
    Qt::Alignment alignment(QGraphicsLayoutItem *item) const;
    /*!
      Sets the alignment of \a item to \a alignment. If \a item's alignment
      changes, the layout is automatically invalidated.

      \sa alignment(), invalidate()
     */
    void setAlignment(QGraphicsLayoutItem *item, Qt::Alignment alignment);


    /*! \reimp */
    virtual QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const;
    virtual void setHorizontalSpacing(qreal spacing);
    virtual void setVerticalSpacing(qreal spacing);
    virtual void removeAt(int index);
    /*! \reimp_end */

    /*!
      Enable/disable automatic setting of the layout position for the items inside the layout.

      \sa layoutPositioningEnabled(), MWidgetController::setLayoutPosition()
     */
    void setNotifyWidgetsOfLayoutPositionEnabled(bool enabled);

    /*!
      Returns true if positioning is enabled, false if positioning is disabled.

      \sa enableLayoutPositioning()
     */    
    bool isNotifyWidgetsOfLayoutPositionEnabled() const;

protected:
    /*! \reimp */
    virtual void relayout();
    virtual void invalidate();
    virtual void activated();
    /*! \reimp_end */

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

#endif // Header Guard