Skip to content

Commit 174d427

Browse files
committed
Add ValueMonitorModel class
1 parent 57de3c0 commit 174d427

File tree

5 files changed

+390
-0
lines changed

5 files changed

+390
-0
lines changed

src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ qt_add_qml_module(scratchcpp-render
2020
spritemodel.h
2121
monitormodel.cpp
2222
monitormodel.h
23+
valuemonitormodel.cpp
24+
valuemonitormodel.h
2325
irenderedtarget.h
2426
renderedtarget.cpp
2527
renderedtarget.h

src/valuemonitormodel.cpp

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// SPDX-License-Identifier: LGPL-3.0-or-later
2+
3+
#include <scratchcpp/virtualmachine.h>
4+
#include <scratchcpp/iblocksection.h>
5+
#include <scratchcpp/monitor.h>
6+
7+
#include "valuemonitormodel.h"
8+
9+
using namespace scratchcpprender;
10+
using namespace libscratchcpp;
11+
12+
static const std::unordered_map<Monitor::Mode, ValueMonitorModel::Mode>
13+
MODE_MAP = { { Monitor::Mode::Default, ValueMonitorModel::Mode::Default }, { Monitor::Mode::Large, ValueMonitorModel::Mode::Large }, { Monitor::Mode::Slider, ValueMonitorModel::Mode::Slider } };
14+
15+
ValueMonitorModel::ValueMonitorModel(QObject *parent) :
16+
MonitorModel(parent)
17+
{
18+
}
19+
20+
ValueMonitorModel::ValueMonitorModel(IBlockSection *section, QObject *parent) :
21+
MonitorModel(parent)
22+
{
23+
if (!section)
24+
return;
25+
26+
// TODO: Get the color from the block section
27+
std::string name = section->name();
28+
if (name == "Motion")
29+
m_color = QColor::fromString("#4C97FF");
30+
else if (name == "Looks")
31+
m_color = QColor::fromString("#9966FF");
32+
else if (name == "Sound")
33+
m_color = QColor::fromString("#CF63CF");
34+
else if (name == "Sensing")
35+
m_color = QColor::fromString("#5CB1D6");
36+
else if (name == "Variables")
37+
m_color = QColor::fromString("#FF8C1A");
38+
else if (name == "Lists")
39+
m_color = QColor::fromString("#FF661A");
40+
}
41+
42+
void ValueMonitorModel::onValueChanged(const VirtualMachine *vm)
43+
{
44+
if (vm->registerCount() == 1) {
45+
m_value = QString::fromStdString(vm->getInput(0, 1)->toString());
46+
emit valueChanged();
47+
}
48+
}
49+
50+
MonitorModel::Type ValueMonitorModel::type() const
51+
{
52+
return Type::Value;
53+
}
54+
55+
const QString &ValueMonitorModel::value() const
56+
{
57+
return m_value;
58+
}
59+
60+
void ValueMonitorModel::setValue(const QString &newValue)
61+
{
62+
if (newValue == m_value)
63+
return;
64+
65+
if (monitor())
66+
monitor()->changeValue(newValue.toStdString());
67+
else {
68+
m_value = newValue;
69+
emit valueChanged();
70+
}
71+
}
72+
73+
const QColor &ValueMonitorModel::color() const
74+
{
75+
return m_color;
76+
}
77+
78+
ValueMonitorModel::Mode ValueMonitorModel::mode() const
79+
{
80+
if (monitor())
81+
return MODE_MAP.at(monitor()->mode());
82+
else
83+
return Mode::Default;
84+
}
85+
86+
double ValueMonitorModel::sliderMin() const
87+
{
88+
if (monitor())
89+
return monitor()->sliderMin();
90+
else
91+
return 0;
92+
}
93+
94+
double ValueMonitorModel::sliderMax() const
95+
{
96+
if (monitor())
97+
return monitor()->sliderMax();
98+
else
99+
return 0;
100+
}
101+
102+
bool ValueMonitorModel::discrete() const
103+
{
104+
if (monitor())
105+
return monitor()->discrete();
106+
else
107+
return true;
108+
}

src/valuemonitormodel.h

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// SPDX-License-Identifier: LGPL-3.0-or-later
2+
3+
#pragma once
4+
5+
#include <QColor>
6+
7+
#include "monitormodel.h"
8+
9+
namespace libscratchcpp
10+
{
11+
12+
class IBlockSection;
13+
14+
}
15+
16+
namespace scratchcpprender
17+
{
18+
19+
class ValueMonitorModel : public MonitorModel
20+
{
21+
Q_OBJECT
22+
QML_ELEMENT
23+
Q_PROPERTY(QString value READ value WRITE setValue NOTIFY valueChanged)
24+
Q_PROPERTY(QColor color READ color NOTIFY colorChanged)
25+
Q_PROPERTY(Mode mode READ mode NOTIFY modeChanged)
26+
Q_PROPERTY(double sliderMin READ sliderMin NOTIFY sliderMinChanged)
27+
Q_PROPERTY(double sliderMax READ sliderMax NOTIFY sliderMaxChanged)
28+
Q_PROPERTY(bool discrete READ discrete NOTIFY discreteChanged)
29+
30+
public:
31+
enum class Mode
32+
{
33+
Default,
34+
Large,
35+
Slider
36+
};
37+
38+
Q_ENUM(Mode)
39+
40+
ValueMonitorModel(QObject *parent = nullptr);
41+
ValueMonitorModel(libscratchcpp::IBlockSection *section, QObject *parent = nullptr);
42+
43+
void onValueChanged(const libscratchcpp::VirtualMachine *vm) override;
44+
45+
Type type() const override;
46+
47+
const QString &value() const;
48+
void setValue(const QString &newValue);
49+
50+
const QColor &color() const;
51+
Mode mode() const;
52+
double sliderMin() const;
53+
double sliderMax() const;
54+
bool discrete() const;
55+
56+
signals:
57+
void valueChanged();
58+
void colorChanged();
59+
void modeChanged();
60+
void sliderMinChanged();
61+
void sliderMaxChanged();
62+
void discreteChanged();
63+
64+
private:
65+
QString m_value;
66+
QColor m_color = Qt::green;
67+
};
68+
69+
} // namespace scratchcpprender

test/monitor_models/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,21 @@ target_link_libraries(
1313

1414
add_test(monitormodel_test)
1515
gtest_discover_tests(monitormodel_test)
16+
17+
add_executable(
18+
valuemonitormodel_test
19+
valuemonitormodel_test.cpp
20+
)
21+
22+
target_link_libraries(
23+
valuemonitormodel_test
24+
GTest::gtest_main
25+
GTest::gmock_main
26+
scratchcpp-render
27+
scratchcpprender_mocks
28+
${QT_LIBS}
29+
Qt6::Test
30+
)
31+
32+
add_test(valuemonitormodel_test)
33+
gtest_discover_tests(valuemonitormodel_test)

0 commit comments

Comments
 (0)