-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCreateShapeCommand.cpp
59 lines (46 loc) · 1.91 KB
/
CreateShapeCommand.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <QGraphicsView>
#include <iostream>
#include <memory>
#include "CreateShapeCommand.h"
#include "DeleteShapeCommand.h"
using std::cout;
using std::endl;
using std::make_shared;
CreateShapeCommand::CreateShapeCommand(QGraphicsScene* scene, Shape::Type type, const QPointF& pos, const QPointF& size, const QColor& color) :
ICommand(scene), m_type(type), m_pos(pos), m_size(size), m_color(color)
{
}
void CreateShapeCommand::execute()
{
auto sceneRect = m_scene->views().first()->sceneRect();
QGraphicsItem* item = nullptr;
if (m_type == Shape::LINE) {
cout << "x: " << m_pos.x() << ", y: " << m_pos.y() << endl;
item = m_scene->addLine(m_pos.x(), m_pos.y(), m_pos.x()+m_size.x(), m_pos.y()-10);
m_scene->views().first()->setSceneRect(sceneRect);
item->setFlag(QGraphicsItem::ItemIsMovable);
item->setFlag(QGraphicsItem::ItemIsSelectable);
} else if (m_type == Shape::RECTANGLE) {
cout << "x: " << m_pos.x() << ", y: " << m_pos.y() << endl;
item = m_scene->addRect(m_pos.x(), m_pos.y(), m_size.x(), m_size.y());
m_scene->views().first()->setSceneRect(sceneRect);
item->setFlag(QGraphicsItem::ItemIsMovable);
item->setFlag(QGraphicsItem::ItemIsSelectable);
} else if (m_type == Shape::CIRCLE) {
cout << "x: " << m_pos.x() << ", y: " << m_pos.y() << endl;
item = m_scene->addEllipse(m_pos.x(), m_pos.y(), m_size.x(), m_size.y());
m_scene->views().first()->setSceneRect(sceneRect);
item->setFlag(QGraphicsItem::ItemIsMovable);
item->setFlag(QGraphicsItem::ItemIsSelectable);
}
if (!m_shape) {
m_shape = shared_ptr<Shape>(new Shape {m_type, m_pos, m_size, m_color, unique_ptr<QGraphicsItem>(item)});
} else {
m_shape->qItem.reset(item);
}
}
void CreateShapeCommand::undo()
{
DeleteShapeCommand deleteCommand(m_scene, m_shape);
deleteCommand.execute();
}