From 3c8db8e6841f82da2938942ce73e9d06143c32ec Mon Sep 17 00:00:00 2001 From: tsujan Date: Tue, 1 Oct 2024 21:17:05 +0330 Subject: [PATCH] Support reversing the order of items in LXQt's grid layout (#353) --- lxqtgridlayout.cpp | 36 +++++++++++++++++++++++++++++++++++- lxqtgridlayout.h | 25 +++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/lxqtgridlayout.cpp b/lxqtgridlayout.cpp index ebe1ff20..c741bb61 100644 --- a/lxqtgridlayout.cpp +++ b/lxqtgridlayout.cpp @@ -44,6 +44,7 @@ class LXQt::GridLayoutPrivate int mRowCount; int mColumnCount; GridLayout::Direction mDirection; + GridLayout::ItemsOrder mItemsOrder; bool mIsValid; QSize mCellSizeHint; @@ -104,6 +105,7 @@ GridLayoutPrivate::GridLayoutPrivate() mColumnCount = 0; mRowCount = 0; mDirection = GridLayout::LeftToRight; + mItemsOrder = GridLayout::FirstToLast; mIsValid = false; mVisibleCount = 0; mStretch = GridLayout::StretchHorizontal | GridLayout::StretchVertical; @@ -241,7 +243,14 @@ GridLayout::~GridLayout() ************************************************/ void GridLayout::addItem(QLayoutItem *item) { - d_ptr->mItems.append(item); + if (d_ptr->mItemsOrder == GridLayout::ItemsOrder::FirstToLast) + { + d_ptr->mItems.append(item); + } + else + { + d_ptr->mItems.prepend(item); + } } @@ -387,6 +396,31 @@ void GridLayout::setStretch(Stretch value) } +/************************************************ + + ************************************************/ +GridLayout::ItemsOrder GridLayout::itemsOrder() const +{ + Q_D(const GridLayout); + return d->mItemsOrder; +} + + +/************************************************ + + ************************************************/ +void GridLayout::setItemsOrder(GridLayout::ItemsOrder value) +{ + Q_D(GridLayout); + if (d->mItemsOrder != value) + { + d->mItemsOrder = value; + std::reverse(d->mItems.begin(), d->mItems.end()); + invalidate(); + } +} + + /************************************************ ************************************************/ diff --git a/lxqtgridlayout.h b/lxqtgridlayout.h index 9d822825..8540ecbd 100644 --- a/lxqtgridlayout.h +++ b/lxqtgridlayout.h @@ -67,6 +67,15 @@ class LXQT_API GridLayout: public QLayout }; Q_DECLARE_FLAGS(Stretch, StretchFlag) + /** + This enum type is used to describe the order of items. + **/ + enum ItemsOrder + { + FirstToLast, ///< The newest item comes last + LastToFirst ///< The newest item comes first + }; + /** @@ -140,6 +149,22 @@ class LXQT_API GridLayout: public QLayout **/ void setDirection(Direction value); + + /** + Returns the order of items. + + \sa GridLayout::ItemsOrder + **/ + ItemsOrder itemsOrder() const; + + /** + Sets the the order of items. + + \sa GridLayout::ItemsOrder + **/ + void setItemsOrder(ItemsOrder value); + + /** Returns the stretch flags of this grid.