-
Notifications
You must be signed in to change notification settings - Fork 0
/
buttonwidgetmediator.cpp
134 lines (110 loc) · 2.65 KB
/
buttonwidgetmediator.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "buttonwidgetmediator.h"
#include <QDebug>
#include "qexception.h"
#include "graphicsscene.h"
Mediator::Mediator(QWidget *parent) :
QWidget(parent)
{
}
ButtonWidgetMediator::ButtonWidgetMediator(QWidget *parent) :
Mediator(parent),
layout(new QVBoxLayout())
{
this->setLayout(layout);
this->show();
registerScene(new GraphicsScene(width(), height()));
}
ButtonWidgetMediator::~ButtonWidgetMediator()
{
if (scene) {
scene->deleteLater();
}
for (Command* command : commands) {
delete command;
}
}
void ButtonWidgetMediator::registerCommand(const QString &uid, Command *command)
{
commands[uid] = command;
command->initialize(this);
registerButton(uid);
}
void ButtonWidgetMediator::registerButton(const QString &uid)
{
QPushButton* button = new QPushButton(uid);
if (registry.contains(uid)) {
throw QException();
}
registry[uid] = button;
layout->addWidget(button);
connect(button, SIGNAL(clicked()), this, SLOT(buttonClicked()));
}
void ButtonWidgetMediator::registerScene(GraphicsScene *scene)
{
if (!scene) {
throw QException();
} else if (this->scene) {
this->scene->deleteLater();
}
this->scene = scene;
connect(this->scene, SIGNAL(clicked()), this, SLOT(sceneClicked()));
//connect(this->scene, SIGNAL(mousepressed()), this, SLOT(mousepressed()));
//connect(this->scene, SIGNAL(mousereleased()), this, SLOT(mousereleased()));
//connect(this->scene, SIGNAL(mousemove(QPointF)), this, SLOT(mousemoved(QPointF)));
}
GraphicsScene *ButtonWidgetMediator::getScene() const
{
return scene;
}
QList<QGraphicsItem *> ButtonWidgetMediator::getSelection() const
{
return selected;
}
void ButtonWidgetMediator::toggleSelection(QGraphicsItem *item)
{
if (selected.contains(item)) {
dynamic_cast<ColorSetter*>(item)->setColor(Qt::black);
selected.removeAll(item);
} else {
dynamic_cast<ColorSetter*>(item)->setColor(Qt::red);
selected.append(item);
}
}
void ButtonWidgetMediator::clearSelection()
{
selected = QList<QGraphicsItem *>();
}
void ButtonWidgetMediator::sceneClicked()
{
if (!scene) {
return;
}
handleItem(getParent(scene->itemAt(scene->getPos(), QTransform())));
}
void ButtonWidgetMediator::buttonClicked()
{
if (!scene) {
return;
}
QPushButton* button = qobject_cast<QPushButton*>(sender());
this->uid = registry.key(button);
emit clicked(uid);
}
void ButtonWidgetMediator::handleItem(QGraphicsItem *item)
{
if (!scene) {
return;
}
Command* command = commands[uid];
if (command) {
command->execute(item);
}
}
QGraphicsItem *ButtonWidgetMediator::getParent(QGraphicsItem *item)
{
if (!item) {
return nullptr;
}
QGraphicsItem* result = item->parentItem();
return (result ? getParent(result) : item);
}