From 233e83b20512a5e3748542f1c279a5c7ec6310ad Mon Sep 17 00:00:00 2001 From: Fabian Bumberger Date: Mon, 14 Jan 2013 15:00:41 +0100 Subject: Allow aligning items in a grid This change introduces two new properties, horizontalItemAlignment and verticalItemAlignment to a Grid element. This gives the user the possibility to align the items. Change-Id: I7322a689f1bbc1da342bd618f6c30dd8c139ee29 Reviewed-by: Alan Alpert --- src/quick/items/qquickitemsmodule.cpp | 2 + src/quick/items/qquickpositioners.cpp | 111 +++++++++++++++++++++++++++++++++- src/quick/items/qquickpositioners_p.h | 24 ++++++++ 3 files changed, 135 insertions(+), 2 deletions(-) (limited to 'src/quick/items') diff --git a/src/quick/items/qquickitemsmodule.cpp b/src/quick/items/qquickitemsmodule.cpp index 043baebd0a..71c23111d5 100644 --- a/src/quick/items/qquickitemsmodule.cpp +++ b/src/quick/items/qquickitemsmodule.cpp @@ -222,6 +222,8 @@ static void qt_quickitems_defineModule(const char *uri, int major, int minor) #ifndef QT_NO_ACCESSIBILITY qmlRegisterUncreatableType("QtQuick", 2, 0, "Accessible",QQuickAccessibleAttached::tr("Accessible is only available via attached properties")); #endif + + qmlRegisterType(uri, 2, 1, "Grid"); } void QQuickItemsModule::defineModule() diff --git a/src/quick/items/qquickpositioners.cpp b/src/quick/items/qquickpositioners.cpp index 2b03ed2dc4..df80e77bfa 100644 --- a/src/quick/items/qquickpositioners.cpp +++ b/src/quick/items/qquickpositioners.cpp @@ -1102,6 +1102,8 @@ QQuickGrid::QQuickGrid(QQuickItem *parent) , m_useRowSpacing(false) , m_useColumnSpacing(false) , m_flow(LeftToRight) + , m_hItemAlign(AlignLeft) + , m_vItemAlign(AlignTop) { } @@ -1248,6 +1250,7 @@ void QQuickGrid::setLayoutDirection(Qt::LayoutDirection layoutDirection) prePositioning(); emit layoutDirectionChanged(); emit effectiveLayoutDirectionChanged(); + emit effectiveHorizontalAlignmentChanged(effectiveHAlign()); } } @@ -1266,6 +1269,97 @@ Qt::LayoutDirection QQuickGrid::effectiveLayoutDirection() const return QQuickBasePositionerPrivate::getEffectiveLayoutDirection(this); } +/*! + \qmlproperty enumeration QtQuick2::Grid::horizontalItmeAlignment + \qmlproperty enumeration QtQuick2::Grid::verticalItemAlignment + \qmlproperty enumeration QtQuick2::Grid::effectiveHorizontalItemAlignment + + Sets the horizontal and vertical alignment of items in the Grid. By default, + the items are vertically aligned to the top. Horizontal + alignment follows the layoutDirection of the Grid, for example when having a layoutDirection + from LeftToRight, the items will be aligned on the left. + + The valid values for \c horizontalItemAlignment are, \c Grid.AlignLeft, \c Grid.AlignRight and + \c Grid.AlignHCenter. + + The valid values for \c verticalItemAlignment are \c Grid.AlignTop, \c Grid.AlignBottom + and \c Grid.AlignVCenter. + + The below images show three examples of how to align items. + + \table + \row + \li + \li \inlineimage gridLayout_aligntopleft.png + \li \inlineimage gridLayout_aligntop.png + \li \inlineimage gridLayout_aligncenter.png + \row + \li Horizontal alignment + \li AlignLeft + \li AlignHCenter + \li AlignHCenter + \row + \li Vertical alignment + \li AlignTop + \li AlignTop + \li AlignVCenter + \endtable + + + When mirroring the layout using either the attached property LayoutMirroring::enabled or + by setting the layoutDirection, the horizontal alignment of items will be mirrored as well. + However, the property \c horizontalItemAlignment will remain unchanged. + To query the effective horizontal alignment of items, use the read-only property + \c effectiveHorizontalItemAlignment. + + \sa Grid::layoutDirection, {LayoutMirroring}{LayoutMirroring} +*/ +QQuickGrid::HAlignment QQuickGrid::hItemAlign() const +{ + return m_hItemAlign; +} +void QQuickGrid::setHItemAlign(HAlignment align) +{ + if (m_hItemAlign != align) { + m_hItemAlign = align; + prePositioning(); + emit horizontalAlignmentChanged(align); + emit effectiveHorizontalAlignmentChanged(effectiveHAlign()); + } +} + +QQuickGrid::HAlignment QQuickGrid::effectiveHAlign() const +{ + HAlignment effectiveAlignment = m_hItemAlign; + if (effectiveLayoutDirection() == Qt::RightToLeft) { + switch (hItemAlign()) { + case AlignLeft: + effectiveAlignment = AlignRight; + break; + case AlignRight: + effectiveAlignment = AlignLeft; + break; + default: + break; + } + } + return effectiveAlignment; +} + + +QQuickGrid::VAlignment QQuickGrid::vItemAlign() const +{ + return m_vItemAlign; +} +void QQuickGrid::setVItemAlign(VAlignment align) +{ + if (m_vItemAlign != align) { + m_vItemAlign = align; + prePositioning(); + emit verticalAlignmentChanged(align); + } +} + void QQuickGrid::doPositioning(QSizeF *contentSize) { //Precondition: All items in the positioned list have a valid item pointer and should be positioned @@ -1362,9 +1456,22 @@ void QQuickGrid::doPositioning(QSizeF *contentSize) for (int i = 0; i < positionedItems.count(); ++i) { PositionedItem &child = positionedItems[i]; qreal childXOffset = xoffset; + + if (effectiveHAlign() == AlignRight) + childXOffset += maxColWidth[curCol] - child.item->width(); + else if (hItemAlign() == AlignHCenter) + childXOffset += (maxColWidth[curCol] - child.item->width())/2.0; + if (!d->isLeftToRight()) - childXOffset -= child.item->width(); - positionItem(childXOffset, yoffset, &child); + childXOffset -= maxColWidth[curCol]; + + qreal alignYOffset = yoffset; + if (m_vItemAlign == AlignVCenter) + alignYOffset += (maxRowHeight[curRow] - child.item->height())/2.0; + else if (m_vItemAlign == AlignBottom) + alignYOffset += maxRowHeight[curRow] - child.item->height(); + + positionItem(childXOffset, alignYOffset, &child); if (m_flow == LeftToRight) { if (d->isLeftToRight()) diff --git a/src/quick/items/qquickpositioners_p.h b/src/quick/items/qquickpositioners_p.h index aeb722dd60..1ff5a7a74c 100644 --- a/src/quick/items/qquickpositioners_p.h +++ b/src/quick/items/qquickpositioners_p.h @@ -220,6 +220,9 @@ class Q_AUTOTEST_EXPORT QQuickGrid : public QQuickBasePositioner Q_PROPERTY(Flow flow READ flow WRITE setFlow NOTIFY flowChanged) Q_PROPERTY(Qt::LayoutDirection layoutDirection READ layoutDirection WRITE setLayoutDirection NOTIFY layoutDirectionChanged) Q_PROPERTY(Qt::LayoutDirection effectiveLayoutDirection READ effectiveLayoutDirection NOTIFY effectiveLayoutDirectionChanged) + Q_PROPERTY(HAlignment horizontalItemAlignment READ hItemAlign WRITE setHItemAlign NOTIFY horizontalAlignmentChanged REVISION 1) + Q_PROPERTY(HAlignment effectiveHorizontalItemAlignment READ effectiveHAlign NOTIFY effectiveHorizontalAlignmentChanged REVISION 1) + Q_PROPERTY(VAlignment verticalItemAlignment READ vItemAlign WRITE setVItemAlign NOTIFY verticalAlignmentChanged REVISION 1) public: QQuickGrid(QQuickItem *parent=0); @@ -247,6 +250,22 @@ public: void setLayoutDirection (Qt::LayoutDirection); Qt::LayoutDirection effectiveLayoutDirection() const; + Q_ENUMS(HAlignment) + Q_ENUMS(VAlignment) + enum HAlignment { AlignLeft = Qt::AlignLeft, + AlignRight = Qt::AlignRight, + AlignHCenter = Qt::AlignHCenter}; + enum VAlignment { AlignTop = Qt::AlignTop, + AlignBottom = Qt::AlignBottom, + AlignVCenter = Qt::AlignVCenter }; + + HAlignment hItemAlign() const; + void setHItemAlign(HAlignment align); + HAlignment effectiveHAlign() const; + + VAlignment vItemAlign() const; + void setVItemAlign(VAlignment align); + Q_SIGNALS: void rowsChanged(); void columnsChanged(); @@ -255,6 +274,9 @@ Q_SIGNALS: void effectiveLayoutDirectionChanged(); void rowSpacingChanged(); void columnSpacingChanged(); + Q_REVISION(1) void horizontalAlignmentChanged(HAlignment alignment); + Q_REVISION(1) void effectiveHorizontalAlignmentChanged(HAlignment alignment); + Q_REVISION(1) void verticalAlignmentChanged(VAlignment alignment); protected: virtual void doPositioning(QSizeF *contentSize); @@ -268,6 +290,8 @@ private: bool m_useRowSpacing; bool m_useColumnSpacing; Flow m_flow; + HAlignment m_hItemAlign; + VAlignment m_vItemAlign; Q_DISABLE_COPY(QQuickGrid) }; -- cgit v1.2.3