Commit 140e1188 authored by Julian Helfferich's avatar Julian Helfferich 💬
Browse files

Remove mpGameScene member from KBlocksView

KBlocksView actually stored two pointers to the same Scene object. The
parent QGraphicsView stores a QGraphicsScene pointer, accessible via
scene() and setScene(). And KBlocksView also stored a SceneInterface
pointer as a private member function.

This could lead to confusing behavior. For example, calling setScene()
on the KBlocksView object only replaces one of the two pointers. I have
removed the private mpGameScene member. The scene is now always accessed
via the scene() interface.
parent 3a0a84f9
......@@ -29,3 +29,10 @@ ecm_add_test(
LINK_LIBRARIES Qt5::Test KBlocksCore KBlocksTesting
NAME_PREFIX "UnitTest-"
)
ecm_add_test(
test_View-Scene.cpp
TEST_NAME View-Scene
LINK_LIBRARIES Qt5::Test KBlocksCore
NAME_PREFIX "IntegrationTest-"
)
/******************************************************************************
* KBlocks, a falling blocks game by KDE *
* Copyright (C) 2009-2021 Julian Helfferich <julian.helfferich@mailbox.org> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
******************************************************************************/
#include <QTest>
#include "KBlocksScene.h"
#include "KBlocksView.h"
#include "Testing/MockGameLogic.h"
#include "Testing/MockScene.h"
class testViewScene : public QObject
{
Q_OBJECT
private slots:
void test_settingsShouldBeUpdatedInViewAndScene();
};
void testViewScene::test_settingsShouldBeUpdatedInViewAndScene()
{
/**
* When the settings are updated for the View, the new settings
* should also be read in the scene to make sure the scene fits
* the view exactly.
*/
MockGameLogic gameLogic;
MockScene scene(&gameLogic);
KBlocksView view(&scene);
view.settingsChanged();
QVERIFY(scene.readSettingsCalled);
}
QTEST_MAIN(testViewScene)
#include "test_View-Scene.moc"
......@@ -16,8 +16,6 @@
KBlocksView::KBlocksView(SceneInterface *scene, QWidget *parent): QGraphicsView(scene, parent)
{
mpGameScene = scene;
//setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
......@@ -37,8 +35,11 @@ KBlocksView::~KBlocksView()
void KBlocksView::settingsChanged()
{
mpGameScene->readSettings(size());
fitInView(mpGameScene->sceneRect(), Qt::KeepAspectRatio);
SceneInterface* s = dynamic_cast<SceneInterface*>(scene());
if (s) {
s->readSettings(size());
}
fitInView(scene()->sceneRect(), Qt::KeepAspectRatio);
}
void KBlocksView::focusInEvent(QFocusEvent *)
......@@ -53,7 +54,7 @@ void KBlocksView::focusOutEvent(QFocusEvent *)
void KBlocksView::resizeEvent(QResizeEvent *event)
{
fitInView(mpGameScene->sceneRect(), Qt::KeepAspectRatio);
fitInView(scene()->sceneRect(), Qt::KeepAspectRatio);
event->accept();
}
......@@ -34,9 +34,6 @@ protected:
void focusInEvent(QFocusEvent *event) override;
void focusOutEvent(QFocusEvent *event) override;
void resizeEvent(QResizeEvent *event) override;
protected:
SceneInterface *mpGameScene = nullptr;
};
#endif
......
......@@ -18,6 +18,7 @@ class MockScene : public SceneInterface
public:
MockScene(MockGameLogic *gameLogic)
: singleGameDeletedWhenStopGameCalled(false),
readSettingsCalled(false),
mpGameLogic(gameLogic)
{
}
......@@ -31,7 +32,7 @@ public:
void setWaitForAllUpdate(bool) override {}
void setUpdateInterval(int) override {}
void setSoundsEnabled(bool) override {}
void readSettings(const QSize&) override {}
void readSettings(const QSize&) override { readSettingsCalled = true; }
void startGame() override {}
void stopGame() override {
if (mpGameLogic->singleGamesDeleted) {
......@@ -47,6 +48,7 @@ public slots:
public:
bool singleGameDeletedWhenStopGameCalled;
bool readSettingsCalled;
private:
MockGameLogic *mpGameLogic;
......
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