examples/declarative/cppextensions/qgraphicslayouts/qgraphicslinearlayout/linearlayout.h
changeset 33 3e2da88830cd
equal deleted inserted replaced
30:5dc02b23752f 33:3e2da88830cd
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:BSD$
       
    10 ** You may use this file under the terms of the BSD license as follows:
       
    11 **
       
    12 ** "Redistribution and use in source and binary forms, with or without
       
    13 ** modification, are permitted provided that the following conditions are
       
    14 ** met:
       
    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
       
    20 **     distribution.
       
    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
       
    24 **     permission.
       
    25 **
       
    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."
       
    37 ** $QT_END_LICENSE$
       
    38 **
       
    39 ****************************************************************************/
       
    40 
       
    41 #ifndef LINEARLAYOUT_H
       
    42 #define LINEARLAYOUT_H
       
    43 
       
    44 #include <qdeclarative.h>
       
    45 
       
    46 #include <QGraphicsLinearLayout>
       
    47 #include <QGraphicsLayoutItem>
       
    48 
       
    49 class GraphicsLinearLayoutStretchItemObject : public QObject, public QGraphicsLayoutItem
       
    50 {
       
    51     Q_OBJECT
       
    52     Q_INTERFACES(QGraphicsLayoutItem)
       
    53 public:
       
    54     GraphicsLinearLayoutStretchItemObject(QObject *parent = 0);
       
    55 
       
    56     virtual QSizeF sizeHint(Qt::SizeHint, const QSizeF &) const;
       
    57 };
       
    58 
       
    59 
       
    60 class LinearLayoutAttached;
       
    61 class GraphicsLinearLayoutObject : public QObject, public QGraphicsLinearLayout
       
    62 {
       
    63     Q_OBJECT
       
    64     Q_INTERFACES(QGraphicsLayout QGraphicsLayoutItem)
       
    65 
       
    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")
       
    71 public:
       
    72     GraphicsLinearLayoutObject(QObject * = 0);
       
    73     ~GraphicsLinearLayoutObject();
       
    74 
       
    75     QDeclarativeListProperty<QGraphicsLayoutItem> children() { return QDeclarativeListProperty<QGraphicsLayoutItem>(this, 0, children_append, children_count, children_at, children_clear); }
       
    76 
       
    77     qreal contentsMargin() const;
       
    78     void setContentsMargin(qreal);
       
    79 
       
    80     void removeAt(int index);
       
    81 
       
    82     static LinearLayoutAttached *qmlAttachedProperties(QObject *);
       
    83 
       
    84 private slots:
       
    85     void updateStretch(QGraphicsLayoutItem*,int);
       
    86     void updateSpacing(QGraphicsLayoutItem*,int);
       
    87     void updateAlignment(QGraphicsLayoutItem*,Qt::Alignment);
       
    88 
       
    89 private:
       
    90     friend class LinearLayoutAttached;
       
    91 
       
    92     void clearChildren();
       
    93     void insertLayoutItem(int, QGraphicsLayoutItem *);
       
    94 
       
    95     static void children_append(QDeclarativeListProperty<QGraphicsLayoutItem> *prop, QGraphicsLayoutItem *item) {
       
    96         static_cast<GraphicsLinearLayoutObject*>(prop->object)->insertLayoutItem(-1, item);
       
    97     }
       
    98 
       
    99     static void children_clear(QDeclarativeListProperty<QGraphicsLayoutItem> *prop) {
       
   100         static_cast<GraphicsLinearLayoutObject*>(prop->object)->clearChildren();
       
   101     }
       
   102 
       
   103     static int children_count(QDeclarativeListProperty<QGraphicsLayoutItem> *prop) {
       
   104         return static_cast<GraphicsLinearLayoutObject*>(prop->object)->count();
       
   105     }
       
   106 
       
   107     static QGraphicsLayoutItem *children_at(QDeclarativeListProperty<QGraphicsLayoutItem> *prop, int index) {
       
   108         return static_cast<GraphicsLinearLayoutObject*>(prop->object)->itemAt(index);
       
   109     }
       
   110 
       
   111     static QHash<QGraphicsLayoutItem*, LinearLayoutAttached*> attachedProperties;
       
   112 };
       
   113 
       
   114 
       
   115 class LinearLayoutAttached : public QObject
       
   116 {
       
   117     Q_OBJECT
       
   118 
       
   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)
       
   122 
       
   123 public:
       
   124     LinearLayoutAttached(QObject *parent);
       
   125 
       
   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);
       
   132 
       
   133 signals:
       
   134     void stretchChanged(QGraphicsLayoutItem*, int);
       
   135     void alignmentChanged(QGraphicsLayoutItem*, Qt::Alignment);
       
   136     void spacingChanged(QGraphicsLayoutItem*, int);
       
   137 
       
   138 private:
       
   139     int m_stretch;
       
   140     Qt::Alignment m_alignment;
       
   141     int m_spacing;
       
   142 };
       
   143 
       
   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)
       
   149 
       
   150 #endif
       
   151 
       
   152