diff options
author | Fabian Bumberger <fbumberger@rim.com> | 2013-01-14 15:00:41 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-01-28 20:42:48 +0100 |
commit | 233e83b20512a5e3748542f1c279a5c7ec6310ad (patch) | |
tree | e8b900290c08f708753e08f3f9d356f65d07f7e5 /src/quick/items/qquickpositioners.cpp | |
parent | 8b62bb86cd94287a29fd1474efe04822523600df (diff) |
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 <aalpert@rim.com>
Diffstat (limited to 'src/quick/items/qquickpositioners.cpp')
-rw-r--r-- | src/quick/items/qquickpositioners.cpp | 111 |
1 files changed, 109 insertions, 2 deletions
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()) |