aboutsummaryrefslogtreecommitdiff
path: root/src/corelib/layout/mgridlayoutpolicy.h
blob: 65855f04feb5fdf19b3dcf29a0a5f92549ea665c (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
286
/***************************************************************************
**
** 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 MGRIDLAYOUTPOLICY_H
#define MGRIDLAYOUTPOLICY_H

#include "mabstractlayoutpolicy.h"
class MGridLayoutPolicyPrivate;

class QWidget;

/*!
 * \class MGridLayoutPolicy
 * \brief Policy that uses the Qt grid layout engine to provide a grid layout.
 * \ingroup layouts
 *
 * This class provides a policy which is similar to QGraphicsGridLayout, with the advantage
 * of allowing multiple policies and animation.  You can use QGraphicsGridLayout instead
 * to slightly reduce memory overhead if these advantages are not required.

 * The following example adds items to the grid layout policy:
 *
 * \dontinclude mgridlayoutpolicy/mgridlayoutpolicy.cpp
 * \skip Create a MLayout
 * \until }
 *
 * The result, with appropriate CSS styling, looks like:
 * \image html mgridlayoutpolicy.png
 *
 * \sa \ref layout-mgridlayoutpolicy
 *
 * \section gridLimitations Limitations - equally-sized columns
 *
 * It is difficult to create a grid layout with equally-sized columns, with both
 * MGridLayoutPolicy and QGraphicsGridLayout.  The current workaround is to
 * manually set the preferred width of all the items inside the layout to the same width.
 *
 * As an example, to layout items in two columns with equal widths:
 *
 * \dontinclude two_columns/twocolumns.cpp
 * \skip Create a MLayout that we set the policy for
 * \until labelLayout->setPreferredWidth(1)
 *
 * We are working with Qt to try to find a better solution.
 *
 * \section qgraphicsgridlayout_instead Using QGraphicsGridLayout instead
 *
 * If you do not need animations or multiple policies, you can use QGraphicsGridLayout for same effect in less code.
 * For example:
 *
 * \dontinclude qgraphicsgridlayout/qgraphicsgridlayout.cpp
 * \skip Create a grid layout
 * \until }
 *
 * \sa \ref layout-qgraphicsgridlayout
 */
class M_EXPORT MGridLayoutPolicy : public MAbstractLayoutPolicy
{
public:
    /*!
     * \brief Constructs a grid layout policy.
     *
     * @param layout The layout to associate with.
     */
    explicit MGridLayoutPolicy(MLayout *layout);

    /*!
     * \brief Destructor to clean up engine.
     */
    virtual ~MGridLayoutPolicy();

    /*!
     * \brief Get the number of rows.
     *
     * @return The number of rows.
     */
    int rowCount() const;

    /*!
     * \brief Get the number of columns.
     *
     * @return The number of columns.
     */
    int columnCount() const;

    /*!
     * \brief Set spacing for a single row.
     *
     * @param row The row to set the spacing for.
     * @param spacing The spacing to use.
     */
    void setRowSpacing(int row, qreal spacing);

    /*!
     * \brief Get spacing for a single row.
     *
     * @param row The row to get the spacing for.
     *
     * @return The spacing.
     */
    qreal rowSpacing(int row) const;

    /*!
     * \brief Set spacing for a single column.
     *
     * @param column The column to set the spacing for.
     * @param spacing The spacing to use.
     */
    void setColumnSpacing(int column, qreal spacing);

    /*!
     * \brief Get spacing for a single column.
     *
     * @param column The column to get the spacing for.
     *
     * @return The spacing.
     */
    qreal columnSpacing(int column) const;

    /*!
     * \brief Set the default alignment for a whole row.
     *  This value can be overridden on an item for item basis.
     *
     * @param   row       the row to set the default alignment for
     * @param   alignment the default alignment to use
     */
    void setRowAlignment(int row, Qt::Alignment alignment);

    /*!
     * \brief Get the default alignment for a whole row.
     *
     * @param   row       the row to get the alignment for
     * @return  the default alignment in use
     */
    Qt::Alignment rowAlignment(int row) const;

    /*!
     * \brief Set the default alignment for a whole column.
     *  This value can be overridden on an item for item basis.
     *
     * @param   column     the column to set the default alignment for
     * @param   alignment  the default alignment to use
     */
    void setColumnAlignment(int column, Qt::Alignment alignment);

    /*!
     * \brief Get the default alignment for a whole column.
     *
     * @param   column       the row to get the alignment for
     * @return  the default alignment in use
     */
    Qt::Alignment columnAlignment(int column) const;

    /*!
     * \brief Get an item.
     *
     * Get the item found at the given position in the grid.
     *
     * @param row The row to query.
     * @param col The column to query.
     *
     * @return The item at the position or 0 if invalid/unset.
     */
    QGraphicsLayoutItem *itemAt(int row, int col) const;

    /*!
     * \brief Add an item at a position in the grid.
     *
     * @param item The item to add.
     * @param row The row to add the item to.
     * @param column The column to add the item to.
     * @param rowSpan The number of cells the item spans in the row
     * @param columnSpan The number of cells the item spans in the column
     * @param alignment The alignment to use.
     */
    void addItem(QGraphicsLayoutItem *item, int row, int column,
                 int rowSpan, int columnSpan, Qt::Alignment alignment = 0);

    /*!
     * \brief Add an item at a position in the grid.
     *
     * @param item The item to add.
     * @param row The row to add the item to.
     * @param column The column to add the item to.
     * @param alignment The alignment to use.
     */
    inline void addItem(QGraphicsLayoutItem *item, int row, int column, Qt::Alignment alignment = 0) {
        addItem(item, row, column, 1, 1, alignment);
    }

    /*! \reimp */
    virtual void removeAt(int index);
    /*! \reimp_end */

    /*! Sets the stretch factor for \a row. */
    void setRowStretchFactor(int row, int stretch);
    /*! Returns the stretch factor for \a row. */
    int rowStretchFactor(int row) const;
    /*! Sets the stretch factor for \a column. */
    void setColumnStretchFactor(int column, int stretch);
    /*! Returns the stretch factor for \a column. */
    int columnStretchFactor(int column) const;
    /*! Sets the minimum height for \a row to \a height. */
    void setRowMinimumHeight(int row, qreal height);
    /*! Returns the minimum height for \a row. */
    qreal rowMinimumHeight(int row) const;
    /*! Sets the preferred height for \a row. */
    void setRowPreferredHeight(int row, qreal height);
    /*! Returns the preferred height for \a row. */
    qreal rowPreferredHeight(int row) const;
    /*! Sets the maximum height for \a row to \a height. */
    void setRowMaximumHeight(int row, qreal height);
    /*! Returns the maximum height for \a row. */
    qreal rowMaximumHeight(int row) const;
    /*! Sets the fixed height for \a row to \a height. */
    void setRowFixedHeight(int row, qreal height);
    /*! Sets the minimum width for \a column to \a width. */
    void setColumnMinimumWidth(int column, qreal width);
    /*! Returns the minimum width for \a column. */
    qreal columnMinimumWidth(int column) const;
    /*! Sets the preferred width for \a column to \a width. */
    void setColumnPreferredWidth(int column, qreal width);
    /*! Returns the preferred width for \a column. */
    qreal columnPreferredWidth(int column) const;
    /*! Sets the maximum width of \a column to \a width. */
    void setColumnMaximumWidth(int column, qreal width);
    /*! Returns the maximum width for \a column. */
    qreal columnMaximumWidth(int column) const;
    /*! Sets the fixed width of \a column to \a width. */
    void setColumnFixedWidth(int column, qreal width);

    /*!
      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);
    /*! \reimp_end */


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

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

#endif // Header Guard