Skip to content

Commit 438d349

Browse files
committed
Add Comment class
1 parent 8f50de6 commit 438d349

File tree

8 files changed

+322
-0
lines changed

8 files changed

+322
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ target_sources(scratchcpp
5252
include/scratchcpp/iimageformatfactory.h
5353
include/scratchcpp/rect.h
5454
include/scratchcpp/igraphicseffect.h
55+
include/scratchcpp/comment.h
5556
)
5657

5758
add_library(zip SHARED

include/scratchcpp/comment.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// SPDX-License-Identifier: LGPL-3.0-or-later
2+
3+
#pragma once
4+
5+
#include "entity.h"
6+
7+
namespace libscratchcpp
8+
{
9+
10+
class Block;
11+
class CommentPrivate;
12+
13+
/*! \brief The Comment class represents a comment in the code area. */
14+
class LIBSCRATCHCPP_EXPORT Comment : public Entity
15+
{
16+
public:
17+
Comment(const std::string &id, double x = 0, double y = 0);
18+
Comment(const Comment &) = delete;
19+
20+
const std::string &blockId() const;
21+
void setBlockId(const std::string id);
22+
23+
std::shared_ptr<Block> block() const;
24+
void setBlock(std::shared_ptr<Block> block);
25+
26+
double x() const;
27+
void setX(double x);
28+
29+
double y() const;
30+
void setY(double y);
31+
32+
double width() const;
33+
void setWidth(double width);
34+
35+
double height() const;
36+
void setHeight(double height);
37+
38+
bool minimized() const;
39+
void setMinimized(bool minimized);
40+
41+
const std::string &text() const;
42+
void setText(const std::string &text);
43+
44+
private:
45+
spimpl::unique_impl_ptr<CommentPrivate> impl;
46+
};
47+
48+
} // namespace libscratchcpp

src/scratch/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,7 @@ target_sources(scratchcpp
4848
keyevent.cpp
4949
keyevent_p.cpp
5050
keyevent_p.h
51+
comment.cpp
52+
comment_p.cpp
53+
comment_p.h
5154
)

src/scratch/comment.cpp

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// SPDX-License-Identifier: LGPL-3.0-or-later
2+
3+
#include <scratchcpp/comment.h>
4+
#include <scratchcpp/block.h>
5+
6+
#include "comment_p.h"
7+
8+
using namespace libscratchcpp;
9+
10+
/*! Constructs Comment at the given position in the code area. */
11+
Comment::Comment(const std::string &id, double x, double y) :
12+
Entity(id),
13+
impl(spimpl::make_unique_impl<CommentPrivate>(x, y))
14+
{
15+
}
16+
17+
/*! Returns the ID of the block the comment is attached to. */
18+
const std::string &Comment::blockId() const
19+
{
20+
return impl->blockId;
21+
}
22+
23+
/*! Sets the ID of the block the comment is attached to. */
24+
void Comment::setBlockId(const std::string id)
25+
{
26+
impl->blockId = id;
27+
impl->block = nullptr;
28+
}
29+
30+
/*! Returns the block the comment is attached to. */
31+
std::shared_ptr<Block> Comment::block() const
32+
{
33+
return impl->block;
34+
}
35+
36+
/*! Sets the block the comment is attached to. */
37+
void Comment::setBlock(std::shared_ptr<Block> block)
38+
{
39+
impl->block = block;
40+
41+
if (block)
42+
impl->blockId = block->id();
43+
else
44+
impl->blockId = "";
45+
}
46+
47+
/*! Returns the x-coordinate of the comment in the code area. */
48+
double Comment::x() const
49+
{
50+
return impl->x;
51+
}
52+
53+
/*! Sets the x-coordinate of the comment in the code area. */
54+
void Comment::setX(double x)
55+
{
56+
impl->x = x;
57+
}
58+
59+
/*! Returns the y-coordinate of the comment in the code area. */
60+
double Comment::y() const
61+
{
62+
return impl->y;
63+
}
64+
65+
/*! Sets the x-coordinate of the comment in the code area. */
66+
void Comment::setY(double y)
67+
{
68+
impl->y = y;
69+
}
70+
71+
/*! Returns the width. */
72+
double Comment::width() const
73+
{
74+
return impl->width;
75+
}
76+
77+
/*! Sets the width. */
78+
void Comment::setWidth(double width)
79+
{
80+
impl->width = width;
81+
}
82+
83+
/*! Returns the height. */
84+
double Comment::height() const
85+
{
86+
return impl->height;
87+
}
88+
89+
/*! Sets the height. */
90+
void Comment::setHeight(double height)
91+
{
92+
impl->height = height;
93+
}
94+
95+
/*! Returns true if the comment is collapsed and false otherwise. */
96+
bool Comment::minimized() const
97+
{
98+
return impl->minimized;
99+
}
100+
101+
/*! Sets whether the comment is collapsed. */
102+
void Comment::setMinimized(bool minimized)
103+
{
104+
impl->minimized = minimized;
105+
}
106+
107+
/*! Returns the text. */
108+
const std::string &Comment::text() const
109+
{
110+
return impl->text;
111+
}
112+
113+
/*! Sets the text. */
114+
void Comment::setText(const std::string &text)
115+
{
116+
impl->text = text;
117+
}

src/scratch/comment_p.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// SPDX-License-Identifier: LGPL-3.0-or-later
2+
3+
#include "comment_p.h"
4+
5+
using namespace libscratchcpp;
6+
7+
CommentPrivate::CommentPrivate(double x, double y) :
8+
x(x),
9+
y(y)
10+
{
11+
}

src/scratch/comment_p.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// SPDX-License-Identifier: LGPL-3.0-or-later
2+
3+
#pragma once
4+
5+
#include <string>
6+
#include <memory>
7+
8+
namespace libscratchcpp
9+
{
10+
11+
class Block;
12+
13+
struct CommentPrivate
14+
{
15+
CommentPrivate(double x, double y);
16+
CommentPrivate(const CommentPrivate &) = delete;
17+
18+
std::string blockId;
19+
std::shared_ptr<Block> block;
20+
double x = 0;
21+
double y = 0;
22+
double width = 200;
23+
double height = 200;
24+
bool minimized = false;
25+
std::string text;
26+
};
27+
28+
} // namespace libscratchcpp

test/scratch_classes/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,17 @@ target_link_libraries(
203203
)
204204

205205
gtest_discover_tests(keyevent_test)
206+
207+
# comment_test
208+
add_executable(
209+
comment_test
210+
comment_test.cpp
211+
)
212+
213+
target_link_libraries(
214+
comment_test
215+
GTest::gtest_main
216+
scratchcpp
217+
)
218+
219+
gtest_discover_tests(comment_test)
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include <scratchcpp/comment.h>
2+
#include <scratchcpp/block.h>
3+
4+
#include "../common.h"
5+
6+
using namespace libscratchcpp;
7+
8+
TEST(CommentTest, Constructors)
9+
{
10+
{
11+
Comment comment("abc");
12+
ASSERT_EQ(comment.id(), "abc");
13+
ASSERT_EQ(comment.x(), 0);
14+
ASSERT_EQ(comment.y(), 0);
15+
}
16+
17+
{
18+
Comment comment("def", 46.09, -64.12);
19+
ASSERT_EQ(comment.id(), "def");
20+
ASSERT_EQ(comment.x(), 46.09);
21+
ASSERT_EQ(comment.y(), -64.12);
22+
}
23+
}
24+
25+
TEST(CommentTest, Block)
26+
{
27+
Comment comment("a");
28+
ASSERT_EQ(comment.block(), nullptr);
29+
ASSERT_TRUE(comment.blockId().empty());
30+
31+
auto block = std::make_shared<Block>("abc", "");
32+
comment.setBlock(block);
33+
ASSERT_EQ(comment.block(), block);
34+
ASSERT_EQ(comment.blockId(), "abc");
35+
36+
comment.setBlock(nullptr);
37+
ASSERT_EQ(comment.block(), nullptr);
38+
ASSERT_TRUE(comment.blockId().empty());
39+
40+
comment.setBlockId("hello");
41+
ASSERT_EQ(comment.blockId(), "hello");
42+
ASSERT_EQ(comment.block(), nullptr);
43+
}
44+
45+
TEST(CommentTest, X)
46+
{
47+
Comment comment("a");
48+
ASSERT_EQ(comment.x(), 0);
49+
50+
comment.setX(-76.29);
51+
ASSERT_EQ(comment.x(), -76.29);
52+
}
53+
54+
TEST(CommentTest, Y)
55+
{
56+
Comment comment("a");
57+
ASSERT_EQ(comment.y(), 0);
58+
59+
comment.setY(38.16);
60+
ASSERT_EQ(comment.y(), 38.16);
61+
}
62+
63+
TEST(CommentTest, Width)
64+
{
65+
Comment comment("a");
66+
ASSERT_EQ(comment.width(), 200);
67+
68+
comment.setWidth(64.49);
69+
ASSERT_EQ(comment.width(), 64.49);
70+
}
71+
72+
TEST(CommentTest, Height)
73+
{
74+
Comment comment("a");
75+
ASSERT_EQ(comment.height(), 200);
76+
77+
comment.setHeight(-89.65);
78+
ASSERT_EQ(comment.height(), -89.65);
79+
}
80+
81+
TEST(CommentTest, Minimized)
82+
{
83+
Comment comment("a");
84+
ASSERT_FALSE(comment.minimized());
85+
86+
comment.setMinimized(true);
87+
ASSERT_TRUE(comment.minimized());
88+
89+
comment.setMinimized(false);
90+
ASSERT_FALSE(comment.minimized());
91+
}
92+
93+
TEST(CommentTest, Text)
94+
{
95+
Comment comment("a");
96+
ASSERT_TRUE(comment.text().empty());
97+
98+
comment.setText("Hello, world!");
99+
ASSERT_EQ(comment.text(), "Hello, world!");
100+
}

0 commit comments

Comments
 (0)