1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the examples of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:BSD$
10 ** You may use this file under the terms of the BSD license as follows:
12 ** "Redistribution and use in source and binary forms, with or without
13 ** modification, are permitted provided that the following conditions are
15 ** * Redistributions of source code must retain the above copyright
16 ** notice, this list of conditions and the following disclaimer.
17 ** * Redistributions in binary form must reproduce the above copyright
18 ** notice, this list of conditions and the following disclaimer in
19 ** the documentation and/or other materials provided with the
21 ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22 ** the names of its contributors may be used to endorse or promote
23 ** products derived from this software without specific prior written
26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39 ****************************************************************************/
41 #ifndef LINEARLAYOUT_H
42 #define LINEARLAYOUT_H
44 #include <qdeclarative.h>
46 #include <QGraphicsLinearLayout>
47 #include <QGraphicsLayoutItem>
49 class GraphicsLinearLayoutStretchItemObject : public QObject, public QGraphicsLayoutItem
52 Q_INTERFACES(QGraphicsLayoutItem)
54 GraphicsLinearLayoutStretchItemObject(QObject *parent = 0);
56 virtual QSizeF sizeHint(Qt::SizeHint, const QSizeF &) const;
60 class LinearLayoutAttached;
61 class GraphicsLinearLayoutObject : public QObject, public QGraphicsLinearLayout
64 Q_INTERFACES(QGraphicsLayout QGraphicsLayoutItem)
66 Q_PROPERTY(QDeclarativeListProperty<QGraphicsLayoutItem> children READ children)
67 Q_PROPERTY(Qt::Orientation orientation READ orientation WRITE setOrientation)
68 Q_PROPERTY(qreal spacing READ spacing WRITE setSpacing)
69 Q_PROPERTY(qreal contentsMargin READ contentsMargin WRITE setContentsMargin)
70 Q_CLASSINFO("DefaultProperty", "children")
72 GraphicsLinearLayoutObject(QObject * = 0);
73 ~GraphicsLinearLayoutObject();
75 QDeclarativeListProperty<QGraphicsLayoutItem> children() { return QDeclarativeListProperty<QGraphicsLayoutItem>(this, 0, children_append, children_count, children_at, children_clear); }
77 qreal contentsMargin() const;
78 void setContentsMargin(qreal);
80 void removeAt(int index);
82 static LinearLayoutAttached *qmlAttachedProperties(QObject *);
85 void updateStretch(QGraphicsLayoutItem*,int);
86 void updateSpacing(QGraphicsLayoutItem*,int);
87 void updateAlignment(QGraphicsLayoutItem*,Qt::Alignment);
90 friend class LinearLayoutAttached;
93 void insertLayoutItem(int, QGraphicsLayoutItem *);
95 static void children_append(QDeclarativeListProperty<QGraphicsLayoutItem> *prop, QGraphicsLayoutItem *item) {
96 static_cast<GraphicsLinearLayoutObject*>(prop->object)->insertLayoutItem(-1, item);
99 static void children_clear(QDeclarativeListProperty<QGraphicsLayoutItem> *prop) {
100 static_cast<GraphicsLinearLayoutObject*>(prop->object)->clearChildren();
103 static int children_count(QDeclarativeListProperty<QGraphicsLayoutItem> *prop) {
104 return static_cast<GraphicsLinearLayoutObject*>(prop->object)->count();
107 static QGraphicsLayoutItem *children_at(QDeclarativeListProperty<QGraphicsLayoutItem> *prop, int index) {
108 return static_cast<GraphicsLinearLayoutObject*>(prop->object)->itemAt(index);
111 static QHash<QGraphicsLayoutItem*, LinearLayoutAttached*> attachedProperties;
115 class LinearLayoutAttached : public QObject
119 Q_PROPERTY(int stretchFactor READ stretchFactor WRITE setStretchFactor NOTIFY stretchChanged)
120 Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment NOTIFY alignmentChanged)
121 Q_PROPERTY(int spacing READ spacing WRITE setSpacing NOTIFY spacingChanged)
124 LinearLayoutAttached(QObject *parent);
126 int stretchFactor() const { return m_stretch; }
127 void setStretchFactor(int f);
128 Qt::Alignment alignment() const { return m_alignment; }
129 void setAlignment(Qt::Alignment a);
130 int spacing() const { return m_spacing; }
131 void setSpacing(int s);
134 void stretchChanged(QGraphicsLayoutItem*, int);
135 void alignmentChanged(QGraphicsLayoutItem*, Qt::Alignment);
136 void spacingChanged(QGraphicsLayoutItem*, int);
140 Qt::Alignment m_alignment;
144 QML_DECLARE_INTERFACE(QGraphicsLayoutItem)
145 QML_DECLARE_INTERFACE(QGraphicsLayout)
146 QML_DECLARE_TYPE(GraphicsLinearLayoutStretchItemObject)
147 QML_DECLARE_TYPE(GraphicsLinearLayoutObject)
148 QML_DECLARE_TYPEINFO(GraphicsLinearLayoutObject, QML_HAS_ATTACHED_PROPERTIES)