Skip to content

Commit 57de3c0

Browse files
committed
Add MonitorModel class
1 parent 2e636b2 commit 57de3c0

File tree

6 files changed

+323
-0
lines changed

6 files changed

+323
-0
lines changed

src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ qt_add_qml_module(scratchcpp-render
1818
stagemodel.h
1919
spritemodel.cpp
2020
spritemodel.h
21+
monitormodel.cpp
22+
monitormodel.h
2123
irenderedtarget.h
2224
renderedtarget.cpp
2325
renderedtarget.h

src/monitormodel.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// SPDX-License-Identifier: LGPL-3.0-or-later
2+
3+
#include <scratchcpp/monitor.h>
4+
#include <scratchcpp/sprite.h>
5+
6+
#include "monitormodel.h"
7+
8+
using namespace scratchcpprender;
9+
10+
MonitorModel::MonitorModel(QObject *parent) :
11+
QObject(parent)
12+
{
13+
}
14+
15+
QString MonitorModel::name() const
16+
{
17+
if (m_monitor) {
18+
if (libscratchcpp::Sprite *sprite = m_monitor->sprite())
19+
return QString::fromStdString(sprite->name() + ": " + m_monitor->name());
20+
else
21+
return QString::fromStdString(m_monitor->name());
22+
} else
23+
return "";
24+
}
25+
26+
bool MonitorModel::visible() const
27+
{
28+
if (m_monitor)
29+
return m_monitor->visible();
30+
else
31+
return false;
32+
}
33+
34+
void MonitorModel::init(libscratchcpp::Monitor *monitor)
35+
{
36+
m_monitor = monitor;
37+
}
38+
39+
void MonitorModel::onVisibleChanged(bool visible)
40+
{
41+
emit visibleChanged();
42+
}
43+
44+
libscratchcpp::Monitor *MonitorModel::monitor() const
45+
{
46+
return m_monitor;
47+
}
48+
49+
int MonitorModel::x() const
50+
{
51+
if (m_monitor)
52+
return m_monitor->x();
53+
else
54+
return 0;
55+
}
56+
57+
int MonitorModel::y() const
58+
{
59+
if (m_monitor)
60+
return m_monitor->y();
61+
else
62+
return 0;
63+
}
64+
65+
unsigned int MonitorModel::width() const
66+
{
67+
if (m_monitor)
68+
return m_monitor->width();
69+
else
70+
return 0;
71+
}
72+
73+
void MonitorModel::setWidth(unsigned int newWidth)
74+
{
75+
if (m_monitor) {
76+
m_monitor->setWidth(newWidth);
77+
emit widthChanged();
78+
}
79+
}
80+
81+
unsigned int MonitorModel::height() const
82+
{
83+
if (m_monitor)
84+
return m_monitor->height();
85+
else
86+
return 0;
87+
}
88+
89+
void MonitorModel::setHeight(unsigned int newHeight)
90+
{
91+
if (m_monitor) {
92+
m_monitor->setHeight(newHeight);
93+
emit heightChanged();
94+
}
95+
}

src/monitormodel.h

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// SPDX-License-Identifier: LGPL-3.0-or-later
2+
3+
#pragma once
4+
5+
#include <QObject>
6+
#include <QQmlEngine>
7+
#include <scratchcpp/imonitorhandler.h>
8+
9+
namespace scratchcpprender
10+
{
11+
12+
class MonitorModel
13+
: public QObject
14+
, public libscratchcpp::IMonitorHandler
15+
{
16+
Q_OBJECT
17+
QML_ELEMENT
18+
Q_PROPERTY(QString name READ name NOTIFY nameChanged)
19+
Q_PROPERTY(bool visible READ visible NOTIFY visibleChanged)
20+
Q_PROPERTY(Type type READ type NOTIFY typeChanged)
21+
Q_PROPERTY(int x READ x NOTIFY xChanged)
22+
Q_PROPERTY(int y READ y NOTIFY yChanged)
23+
Q_PROPERTY(unsigned int width READ width WRITE setWidth NOTIFY widthChanged)
24+
Q_PROPERTY(unsigned int height READ height WRITE setHeight NOTIFY heightChanged)
25+
26+
public:
27+
enum class Type
28+
{
29+
Invalid,
30+
Value,
31+
List
32+
};
33+
34+
Q_ENUM(Type)
35+
36+
MonitorModel(QObject *parent = nullptr);
37+
38+
void init(libscratchcpp::Monitor *monitor) override final;
39+
40+
virtual void onValueChanged(const libscratchcpp::VirtualMachine *vm) override { }
41+
void onVisibleChanged(bool visible) override final;
42+
43+
libscratchcpp::Monitor *monitor() const;
44+
45+
QString name() const;
46+
47+
bool visible() const;
48+
49+
virtual Type type() const { return Type::Invalid; }
50+
51+
int x() const;
52+
53+
int y() const;
54+
55+
unsigned int width() const;
56+
void setWidth(unsigned int newWidth);
57+
58+
unsigned int height() const;
59+
void setHeight(unsigned int newHeight);
60+
61+
signals:
62+
void visibleChanged();
63+
void typeChanged();
64+
void xChanged();
65+
void yChanged();
66+
void widthChanged();
67+
void heightChanged();
68+
void nameChanged();
69+
70+
private:
71+
libscratchcpp::Monitor *m_monitor = nullptr;
72+
};
73+
74+
} // namespace scratchcpprender

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ add_subdirectory(projectscene)
2828
add_subdirectory(keyeventhandler)
2929
add_subdirectory(mouseeventhandler)
3030
add_subdirectory(scenemousearea)
31+
add_subdirectory(monitor_models)

test/monitor_models/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
add_executable(
2+
monitormodel_test
3+
monitormodel_test.cpp
4+
)
5+
6+
target_link_libraries(
7+
monitormodel_test
8+
GTest::gtest_main
9+
scratchcpp-render
10+
${QT_LIBS}
11+
Qt6::Test
12+
)
13+
14+
add_test(monitormodel_test)
15+
gtest_discover_tests(monitormodel_test)
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
#include <QtTest/QSignalSpy>
2+
#include <scratchcpp/monitor.h>
3+
#include <scratchcpp/sprite.h>
4+
#include <monitormodel.h>
5+
6+
#include "../common.h"
7+
8+
using namespace scratchcpprender;
9+
using namespace libscratchcpp;
10+
11+
TEST(MonitorModelTest, Constructors)
12+
{
13+
MonitorModel model1;
14+
MonitorModel model2(&model1);
15+
ASSERT_EQ(model2.parent(), &model1);
16+
}
17+
18+
TEST(MonitorModelTest, Init)
19+
{
20+
MonitorModel model;
21+
ASSERT_EQ(model.monitor(), nullptr);
22+
23+
Monitor monitor("", "");
24+
model.init(&monitor);
25+
ASSERT_EQ(model.monitor(), &monitor);
26+
}
27+
28+
TEST(MonitorModelTest, OnVisibleChanged)
29+
{
30+
MonitorModel model;
31+
QSignalSpy spy(&model, &MonitorModel::visibleChanged);
32+
33+
model.onVisibleChanged(false);
34+
ASSERT_EQ(spy.count(), 1);
35+
36+
model.onVisibleChanged(true);
37+
ASSERT_EQ(spy.count(), 2);
38+
}
39+
40+
TEST(MonitorModelTest, Name)
41+
{
42+
MonitorModel model;
43+
Monitor monitor("", "");
44+
// TODO: Use monitor.setName()
45+
const_cast<std::string *>(&monitor.name())->assign("days since 2000");
46+
model.init(&monitor);
47+
ASSERT_EQ(model.name().toStdString(), monitor.name());
48+
49+
Sprite sprite;
50+
sprite.setName("Sprite2");
51+
monitor.setSprite(&sprite);
52+
// TODO: Use monitor.setName()
53+
const_cast<std::string *>(&monitor.name())->assign("x position");
54+
ASSERT_EQ(model.name().toStdString(), sprite.name() + ": " + monitor.name());
55+
}
56+
57+
TEST(MonitorModelTest, Visible)
58+
{
59+
MonitorModel model;
60+
Monitor monitor("", "");
61+
monitor.setVisible(true);
62+
model.init(&monitor);
63+
ASSERT_TRUE(model.visible());
64+
65+
monitor.setVisible(false);
66+
ASSERT_FALSE(model.visible());
67+
68+
monitor.setVisible(false);
69+
ASSERT_FALSE(model.visible());
70+
}
71+
72+
TEST(MonitorModelTest, Type)
73+
{
74+
MonitorModel model;
75+
ASSERT_EQ(model.type(), MonitorModel::Type::Invalid);
76+
}
77+
78+
TEST(MonitorModelTest, X)
79+
{
80+
MonitorModel model;
81+
Monitor monitor("", "");
82+
monitor.setX(65);
83+
model.init(&monitor);
84+
ASSERT_EQ(model.x(), 65);
85+
86+
monitor.setX(-2);
87+
ASSERT_EQ(model.x(), -2);
88+
}
89+
90+
TEST(MonitorModelTest, Y)
91+
{
92+
MonitorModel model;
93+
Monitor monitor("", "");
94+
monitor.setY(15);
95+
model.init(&monitor);
96+
ASSERT_EQ(model.y(), 15);
97+
98+
monitor.setY(-8);
99+
ASSERT_EQ(model.y(), -8);
100+
}
101+
102+
TEST(MonitorModelTest, Width)
103+
{
104+
MonitorModel model;
105+
Monitor monitor("", "");
106+
QSignalSpy spy(&model, &MonitorModel::widthChanged);
107+
monitor.setWidth(20);
108+
model.init(&monitor);
109+
ASSERT_EQ(model.width(), 20);
110+
111+
monitor.setWidth(150);
112+
ASSERT_EQ(model.width(), 150);
113+
114+
model.setWidth(87);
115+
ASSERT_EQ(model.width(), 87);
116+
ASSERT_EQ(monitor.width(), 87);
117+
ASSERT_EQ(spy.count(), 1);
118+
}
119+
120+
TEST(MonitorModelTest, Height)
121+
{
122+
MonitorModel model;
123+
Monitor monitor("", "");
124+
QSignalSpy spy(&model, &MonitorModel::heightChanged);
125+
monitor.setHeight(20);
126+
model.init(&monitor);
127+
ASSERT_EQ(model.height(), 20);
128+
129+
monitor.setHeight(150);
130+
ASSERT_EQ(model.height(), 150);
131+
132+
model.setHeight(87);
133+
ASSERT_EQ(model.height(), 87);
134+
ASSERT_EQ(monitor.height(), 87);
135+
ASSERT_EQ(spy.count(), 1);
136+
}

0 commit comments

Comments
 (0)