Skip to content

Commit 98e99ed

Browse files
committed
Add ListMonitorModel class
1 parent bed46ce commit 98e99ed

File tree

5 files changed

+264
-0
lines changed

5 files changed

+264
-0
lines changed

src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ qt_add_qml_module(scratchcpp-render
2525
monitormodel.h
2626
valuemonitormodel.cpp
2727
valuemonitormodel.h
28+
listmonitormodel.cpp
29+
listmonitormodel.h
2830
listmonitorlistmodel.cpp
2931
listmonitorlistmodel.h
3032
irenderedtarget.h

src/listmonitormodel.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// SPDX-License-Identifier: LGPL-3.0-or-later
2+
3+
#include <scratchcpp/iblocksection.h>
4+
#include <scratchcpp/virtualmachine.h>
5+
6+
#include "listmonitormodel.h"
7+
#include "listmonitorlistmodel.h"
8+
9+
using namespace scratchcpprender;
10+
11+
ListMonitorModel::ListMonitorModel(QObject *parent) :
12+
MonitorModel(parent)
13+
{
14+
m_listModel = new ListMonitorListModel(this);
15+
}
16+
17+
ListMonitorModel::ListMonitorModel(libscratchcpp::IBlockSection *section, QObject *parent) :
18+
ListMonitorModel(parent)
19+
{
20+
if (!section)
21+
return;
22+
23+
// TODO: Get the color from the block section
24+
std::string name = section->name();
25+
if (name == "Motion")
26+
m_color = QColor::fromString("#4C97FF");
27+
else if (name == "Looks")
28+
m_color = QColor::fromString("#9966FF");
29+
else if (name == "Sound")
30+
m_color = QColor::fromString("#CF63CF");
31+
else if (name == "Sensing")
32+
m_color = QColor::fromString("#5CB1D6");
33+
else if (name == "Variables")
34+
m_color = QColor::fromString("#FF8C1A");
35+
else if (name == "Lists")
36+
m_color = QColor::fromString("#FF661A");
37+
}
38+
39+
void ListMonitorModel::onValueChanged(const libscratchcpp::VirtualMachine *vm)
40+
{
41+
if (vm->registerCount() == 1) {
42+
long index = vm->getInput(0, 1)->toLong();
43+
libscratchcpp::List *list = vm->lists()[index];
44+
m_listModel->setList(list);
45+
}
46+
}
47+
48+
MonitorModel::Type ListMonitorModel::type() const
49+
{
50+
return Type::List;
51+
}
52+
53+
const QColor &ListMonitorModel::color() const
54+
{
55+
return m_color;
56+
}
57+
58+
ListMonitorListModel *ListMonitorModel::listModel() const
59+
{
60+
return m_listModel;
61+
}

src/listmonitormodel.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// SPDX-License-Identifier: LGPL-3.0-or-later
2+
3+
#pragma once
4+
5+
#include <QAbstractListModel>
6+
#include <QColor>
7+
8+
#include "monitormodel.h"
9+
10+
Q_MOC_INCLUDE("listmonitorlistmodel.h")
11+
12+
namespace libscratchcpp
13+
{
14+
15+
class IBlockSection;
16+
17+
}
18+
19+
namespace scratchcpprender
20+
{
21+
22+
class ListMonitorListModel;
23+
24+
class ListMonitorModel : public MonitorModel
25+
{
26+
Q_OBJECT
27+
QML_ELEMENT
28+
Q_PROPERTY(QColor color READ color NOTIFY colorChanged)
29+
Q_PROPERTY(ListMonitorListModel *listModel READ listModel NOTIFY listModelChanged)
30+
31+
public:
32+
ListMonitorModel(QObject *parent = nullptr);
33+
ListMonitorModel(libscratchcpp::IBlockSection *section, QObject *parent = nullptr);
34+
35+
void onValueChanged(const libscratchcpp::VirtualMachine *vm) override;
36+
37+
Type type() const override;
38+
39+
const QColor &color() const;
40+
41+
ListMonitorListModel *listModel() const;
42+
43+
signals:
44+
void colorChanged();
45+
void listModelChanged();
46+
47+
private:
48+
QColor m_color = Qt::green;
49+
ListMonitorListModel *m_listModel = nullptr;
50+
};
51+
52+
} // namespace scratchcpprender

test/monitor_models/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,22 @@ target_link_libraries(
5050

5151
add_test(listmonitorlistmodel_test)
5252
gtest_discover_tests(listmonitorlistmodel_test)
53+
54+
# listmonitormodel
55+
add_executable(
56+
listmonitormodel_test
57+
listmonitormodel_test.cpp
58+
)
59+
60+
target_link_libraries(
61+
listmonitormodel_test
62+
GTest::gtest_main
63+
GTest::gmock_main
64+
scratchcpp-render
65+
scratchcpprender_mocks
66+
${QT_LIBS}
67+
Qt6::Test
68+
)
69+
70+
add_test(listmonitormodel_test)
71+
gtest_discover_tests(listmonitormodel_test)
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#include <QtTest/QSignalSpy>
2+
#include <scratchcpp/virtualmachine.h>
3+
#include <scratchcpp/list.h>
4+
#include <listmonitormodel.h>
5+
#include <listmonitorlistmodel.h>
6+
#include <blocksectionmock.h>
7+
8+
#include "../common.h"
9+
10+
using namespace scratchcpprender;
11+
using namespace libscratchcpp;
12+
13+
using ::testing::Return;
14+
15+
TEST(ListMonitorModelTest, Constructors)
16+
{
17+
{
18+
ListMonitorModel model1;
19+
ListMonitorModel model2(&model1);
20+
ASSERT_EQ(model2.parent(), &model1);
21+
}
22+
23+
{
24+
ListMonitorModel model1;
25+
ListMonitorModel model2(nullptr, &model1);
26+
ASSERT_EQ(model2.parent(), &model1);
27+
}
28+
}
29+
30+
TEST(ListMonitorModelTest, OnValueChanged)
31+
{
32+
ListMonitorModel model;
33+
ListMonitorListModel *listModel = model.listModel();
34+
VirtualMachine vm;
35+
36+
List list1("", "");
37+
list1.push_back(1);
38+
list1.push_back(2);
39+
40+
List list2("", "");
41+
list2.push_back(1);
42+
list2.push_back(2);
43+
list2.push_back(3);
44+
list2.push_back(4);
45+
46+
List list3("", "");
47+
list3.push_back(1);
48+
list3.push_back(2);
49+
list3.push_back(3);
50+
51+
List *lists[] = { &list1, &list2, &list3 };
52+
vm.setLists(lists);
53+
54+
vm.addReturnValue(1);
55+
model.onValueChanged(&vm);
56+
ASSERT_EQ(listModel->rowCount(QModelIndex()), 4);
57+
58+
vm.reset();
59+
vm.addReturnValue(2);
60+
model.onValueChanged(&vm);
61+
ASSERT_EQ(listModel->rowCount(QModelIndex()), 3);
62+
63+
vm.reset();
64+
vm.addReturnValue(0);
65+
model.onValueChanged(&vm);
66+
ASSERT_EQ(listModel->rowCount(QModelIndex()), 2);
67+
}
68+
69+
TEST(ListMonitorModelTest, Type)
70+
{
71+
ListMonitorModel model;
72+
ASSERT_EQ(model.type(), MonitorModel::Type::List);
73+
}
74+
75+
TEST(ListMonitorModelTest, Color)
76+
{
77+
{
78+
ListMonitorModel model;
79+
ASSERT_EQ(model.color(), Qt::green);
80+
}
81+
82+
{
83+
ListMonitorModel model(nullptr, nullptr);
84+
ASSERT_EQ(model.color(), Qt::green);
85+
}
86+
87+
BlockSectionMock section;
88+
89+
{
90+
// Invalid
91+
EXPECT_CALL(section, name()).WillOnce(Return(""));
92+
ListMonitorModel model(&section);
93+
ASSERT_EQ(model.color(), Qt::green);
94+
}
95+
96+
{
97+
// Motion
98+
EXPECT_CALL(section, name()).WillOnce(Return("Motion"));
99+
ListMonitorModel model(&section);
100+
ASSERT_EQ(model.color(), QColor::fromString("#4C97FF"));
101+
}
102+
103+
{
104+
// Looks
105+
EXPECT_CALL(section, name()).WillOnce(Return("Looks"));
106+
ListMonitorModel model(&section);
107+
ASSERT_EQ(model.color(), QColor::fromString("#9966FF"));
108+
}
109+
110+
{
111+
// Sound
112+
EXPECT_CALL(section, name()).WillOnce(Return("Sound"));
113+
ListMonitorModel model(&section);
114+
ASSERT_EQ(model.color(), QColor::fromString("#CF63CF"));
115+
}
116+
117+
{
118+
// Variables
119+
EXPECT_CALL(section, name()).WillOnce(Return("Variables"));
120+
ListMonitorModel model(&section);
121+
ASSERT_EQ(model.color(), QColor::fromString("#FF8C1A"));
122+
}
123+
124+
{
125+
// Lists
126+
EXPECT_CALL(section, name()).WillOnce(Return("Lists"));
127+
ListMonitorModel model(&section);
128+
ASSERT_EQ(model.color(), QColor::fromString("#FF661A"));
129+
}
130+
}

0 commit comments

Comments
 (0)