Commit 3f9d162c authored by Julian Helfferich's avatar Julian Helfferich 💬
Browse files

Clear cache when refreshing positions.

Since refreshPosition() is called when the layout changes, the cache
for each SVG item should be cleared to ensure that the new theme is
displayed correctly. This fixes issue A) of bug 380474.
parent 3bcd2927
......@@ -23,6 +23,7 @@ class testKBlocksItemGroup : public QObject
Q_OBJECT
private slots:
void refreshPositionShouldSetCorrectPositions();
void refreshPositionShouldClearCache();
void updateGameShouldProcessGameActionsOnGameOver();
void updateGameShouldRefreshItemsOnGameOver();
void stopGameShouldProcessRemainingGameActions();
......@@ -87,6 +88,50 @@ void testKBlocksItemGroup::refreshPositionShouldSetCorrectPositions()
}
}
void testKBlocksItemGroup::refreshPositionShouldClearCache()
{
/**
* The positions are refreshed when the layout changes. Then, also
* the cache should be cleared to ensure that the graphics for each
* item is updated.
*
* See bug 380474
*/
MockSingleGame singleGame;
MockGraphics graphics;
MockSound sound;
TestingKBlocksItemGroup itemGroup(0, &singleGame, &graphics, &sound);
itemGroup.replacePrepareCell(0, new MockSvgItem());
itemGroup.replaceFreezeCells(new MockSvgItem());
auto *prepareCellBefore = dynamic_cast<MockSvgItem*>(
itemGroup.getPrepareCell(0)
);
QVERIFY(prepareCellBefore != nullptr);
QCOMPARE(prepareCellBefore->clearCacheCalled, false);
auto *freezeCellBefore = dynamic_cast<MockSvgItem*>(
itemGroup.getFreezeCell(0)
);
QVERIFY(freezeCellBefore != nullptr);
QCOMPARE(freezeCellBefore->clearCacheCalled, false);
itemGroup.refreshPosition();
auto *prepareCellAfter = dynamic_cast<MockSvgItem*>(
itemGroup.getPrepareCell(0)
);
QVERIFY(prepareCellAfter != nullptr);
QCOMPARE(prepareCellAfter->clearCacheCalled, true);
auto *freezeCellAfter = dynamic_cast<MockSvgItem*>(
itemGroup.getFreezeCell(0)
);
QVERIFY(freezeCellAfter != nullptr);
QCOMPARE(freezeCellAfter->clearCacheCalled, true);
}
void testKBlocksItemGroup::updateGameShouldProcessGameActionsOnGameOver()
{
......
......@@ -121,11 +121,13 @@ void KBlocksItemGroup::refreshPosition()
for (int i = 0; i < mMaxPrepareCellNum; i++) {
maPrepareCells[i]->setPos(mPrepareLeft + mItemSize * (i % PREPARE_AREA_WIDTH),
mPrepareTop + mItemSize * (i / PREPARE_AREA_WIDTH));
maPrepareCells[i]->clearCache();
}
for (int i = 0; i < mMaxFreezeCellNum; i++) {
maFreezeCells[i]->setPos(mFieldLeft + mItemSize * (i % mFieldWidth),
mFieldTop + mItemSize * (i / mFieldWidth));
maFreezeCells[i]->clearCache();
}
}
......
......@@ -59,6 +59,13 @@ bool KBlocksSvgItem::updateSelf()
return true;
}
void KBlocksSvgItem::clearCache()
{
setCacheMode(NoCache);
setCacheMode(DeviceCoordinateCache);
}
void KBlocksSvgItem::startOpAnim()
{
setElementId(QStringLiteral("BLOCK_OUT_%1").arg(mColor));
......
......@@ -35,6 +35,7 @@ public:
void setLayoutPos(int posX, int posY);
bool updateSelf() override;
void clearCache() override;
void startOpAnim() override;
void stopOpAnim() override;
......
......@@ -21,6 +21,7 @@ public:
public:
virtual bool updateSelf() = 0;
virtual void clearCache() = 0;
virtual void startOpAnim() = 0;
virtual void stopOpAnim() = 0;
......
......@@ -21,6 +21,7 @@ public:
public:
bool updateSelf() override { updateSelfCalled = true; return true; }
void clearCache() override { clearCacheCalled = true; }
void startOpAnim() override {}
void stopOpAnim() override {}
void startPosAnim(QPointF) override {}
......@@ -28,6 +29,7 @@ public:
void stopPosAnim() override {}
bool updateSelfCalled;
bool clearCacheCalled = false;
};
#endif //MOCKSVGITEM_H
......@@ -35,6 +35,11 @@ public:
return nullptr;
}
int getMaxPrepareCellNum() const
{
return mMaxPrepareCellNum;
}
SvgItemInterface *getPrepareCell(int cellNum)
{
if (cellNum < mMaxPrepareCellNum) {
......@@ -43,6 +48,18 @@ public:
return nullptr;
}
void replacePrepareCell(int cellNum, SvgItemInterface* newItem)
{
if (cellNum >= mMaxPrepareCellNum) {
return;
}
removeFromGroup(maPrepareCells[cellNum]);
delete maPrepareCells[cellNum];
maPrepareCells[cellNum] = newItem;
addToGroup(maPrepareCells[cellNum]);
}
void replaceFreezeCells(SvgItemInterface* newItem)
{
for (int i = 0; i < mMaxFreezeCellNum; i++) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment