better positioning logic

When asking for a specific layout direction, try very hard at all the
cells that are available in that given direction instead of always
start to compute at a new row and wrong column
wilder-5.17
Marco Martin 7 years ago
parent dac4acd36a
commit fb7797dfd2
  1. 3
      components/containmentlayoutmanager/appletslayout.cpp
  2. 2
      components/containmentlayoutmanager/appletslayout.h
  3. 48
      components/containmentlayoutmanager/gridlayoutmanager.cpp
  4. 3
      components/containmentlayoutmanager/gridlayoutmanager.h

@ -370,12 +370,13 @@ void AppletsLayout::showPlaceHolderAt(const QRectF &geom)
m_placeHolder->setProperty("opacity", 1); m_placeHolder->setProperty("opacity", 1);
} }
void AppletsLayout::showPlaceHolderForItem(QQuickItem *item) void AppletsLayout::showPlaceHolderForItem(ItemContainer *item)
{ {
if (!m_placeHolder) { if (!m_placeHolder) {
return; return;
} }
m_placeHolder->setPreferredLayoutDirection(item->preferredLayoutDirection());
m_placeHolder->setPosition(item->position()); m_placeHolder->setPosition(item->position());
m_placeHolder->setSize(item->size()); m_placeHolder->setSize(item->size());

@ -130,7 +130,7 @@ public:
Q_INVOKABLE void save(); Q_INVOKABLE void save();
Q_INVOKABLE void showPlaceHolderAt(const QRectF &geom); Q_INVOKABLE void showPlaceHolderAt(const QRectF &geom);
Q_INVOKABLE void showPlaceHolderForItem(QQuickItem *item); Q_INVOKABLE void showPlaceHolderForItem(ItemContainer *item);
Q_INVOKABLE void hidePlaceHolder(); Q_INVOKABLE void hidePlaceHolder();
Q_INVOKABLE bool isRectAvailable(qreal x, qreal y, qreal width, qreal height); Q_INVOKABLE bool isRectAvailable(qreal x, qreal y, qreal width, qreal height);

@ -412,6 +412,42 @@ QPair<int, int> GridLayoutManager::nextAvailableCell(const QPair<int, int> &cell
return QPair<int, int>(-1, -1); return QPair<int, int>(-1, -1);
} }
QPair<int, int> GridLayoutManager::nextTakenCell(const QPair<int, int> &cell, AppletsLayout::PreferredLayoutDirection direction) const
{
QPair<int, int> nCell = cell;
while (!isOutOfBounds(nCell)) {
nCell = nextCell(nCell, direction);
if (isOutOfBounds(nCell)) {
switch (direction) {
case AppletsLayout::AppletsLayout::BottomToTop:
nCell.first = rows() - 1;
--nCell.second;
break;
case AppletsLayout::AppletsLayout::TopToBottom:
nCell.first = 0;
++nCell.second;
break;
case AppletsLayout::AppletsLayout::RightToLeft:
--nCell.first;
nCell.second = columns() - 1;
break;
case AppletsLayout::AppletsLayout::LeftToRight:
default:
++nCell.first;
nCell.second = 0;
break;
}
}
if (!isCellAvailable(nCell)) {
return nCell;
}
}
return QPair<int, int>(-1, -1);
}
int GridLayoutManager::freeSpaceInDirection(const QPair<int, int> &cell, AppletsLayout::PreferredLayoutDirection direction) const int GridLayoutManager::freeSpaceInDirection(const QPair<int, int> &cell, AppletsLayout::PreferredLayoutDirection direction) const
{ {
QPair<int, int> nCell = cell; QPair<int, int> nCell = cell;
@ -447,11 +483,11 @@ QRectF GridLayoutManager::nextAvailableSpace(ItemContainer *item, const QSizeF &
cell.first += itemCellGeom.height(); cell.first += itemCellGeom.height();
} }
while (!isOutOfBounds(cell)) { if (!isCellAvailable(cell)) {
cell = nextAvailableCell(cell, direction);
}
if (!isCellAvailable(cell)) { while (!isOutOfBounds(cell)) {
cell = nextAvailableCell(cell, direction);
}
if (direction == AppletsLayout::LeftToRight || direction == AppletsLayout::RightToLeft) { if (direction == AppletsLayout::LeftToRight || direction == AppletsLayout::RightToLeft) {
partialSize = QSize(INT_MAX, 0); partialSize = QSize(INT_MAX, 0);
@ -494,7 +530,7 @@ QRectF GridLayoutManager::nextAvailableSpace(ItemContainer *item, const QSizeF &
width, height); width, height);
} }
} else { } else {
cell.first = currentRow + 1; cell = nextAvailableCell(nextTakenCell(cell, direction), direction);
} }
} else if (direction == AppletsLayout::TopToBottom || direction == AppletsLayout::BottomToTop) { } else if (direction == AppletsLayout::TopToBottom || direction == AppletsLayout::BottomToTop) {
@ -538,7 +574,7 @@ QRectF GridLayoutManager::nextAvailableSpace(ItemContainer *item, const QSizeF &
width, height); width, height);
} }
} else { } else {
cell.second = currentColumn + 1; cell = nextAvailableCell(nextTakenCell(cell, direction), direction);
} }
} }
} }

@ -94,6 +94,9 @@ private:
// The next cell that is available given the direction // The next cell that is available given the direction
QPair<int, int> nextAvailableCell(const QPair<int, int> &cell, AppletsLayout::PreferredLayoutDirection direction) const; QPair<int, int> nextAvailableCell(const QPair<int, int> &cell, AppletsLayout::PreferredLayoutDirection direction) const;
// The next cell that is has something in it given the direction
QPair<int, int> nextTakenCell(const QPair<int, int> &cell, AppletsLayout::PreferredLayoutDirection direction) const;
// How many cells are available in the row starting from the given cell and direction // How many cells are available in the row starting from the given cell and direction
int freeSpaceInDirection(const QPair<int, int> &cell, AppletsLayout::PreferredLayoutDirection direction) const; int freeSpaceInDirection(const QPair<int, int> &cell, AppletsLayout::PreferredLayoutDirection direction) const;

Loading…
Cancel
Save