-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathEditActionsHandlerRegistry.cpp
More file actions
163 lines (124 loc) · 4.8 KB
/
EditActionsHandlerRegistry.cpp
File metadata and controls
163 lines (124 loc) · 4.8 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "EditActionsHandlerRegistry.h"
#include "EditActionsHandlerRegistry_p.h"
namespace Core {
EditActionsHandlerRegistry::EditActionsHandlerRegistry(QObject *parent)
: EditActionsHandler(parent), d_ptr(new EditActionsHandlerRegistryPrivate) {
Q_D(EditActionsHandlerRegistry);
d->q_ptr = this;
}
EditActionsHandlerRegistry::~EditActionsHandlerRegistry() = default;
bool EditActionsHandlerRegistry::pushHandler(EditActionsHandler *handler) {
Q_D(EditActionsHandlerRegistry);
if (!handler) {
return false;
}
// Remove if already exists to avoid duplicates
for (auto it = d->handlers.begin(); it != d->handlers.end(); ++it) {
if (*it == handler) {
d->handlers.erase(it);
break;
}
}
// Add to end of list
d->handlers.append(handler);
d->connectHandler(handler);
d->updateCurrentHandler();
return true;
}
bool EditActionsHandlerRegistry::popHandler(EditActionsHandler *handler) {
Q_D(EditActionsHandlerRegistry);
if (!handler) {
return false;
}
bool found = false;
for (auto it = d->handlers.begin(); it != d->handlers.end(); ++it) {
if (*it == handler) {
d->disconnectHandler(handler);
d->handlers.erase(it);
found = true;
break;
}
}
if (found) {
d->updateCurrentHandler();
}
return found;
}
EditActionsHandler::EditAction EditActionsHandlerRegistry::enabledActions() const {
Q_D(const EditActionsHandlerRegistry);
if (d->currentHandler) {
return d->currentHandler->enabledActions();
}
return EditAction();
}
bool EditActionsHandlerRegistry::triggerEditAction(EditAction action) {
Q_D(EditActionsHandlerRegistry);
if (d->currentHandler) {
return d->currentHandler->triggerEditAction(action);
}
return false;
}
EditActionsHandler::MoveDirection EditActionsHandlerRegistry::enabledMoveDirections() const {
Q_D(const EditActionsHandlerRegistry);
if (d->currentHandler) {
return d->currentHandler->enabledMoveDirections();
}
return MoveDirection();
}
bool EditActionsHandlerRegistry::move(MoveDirection direction, int offset) {
Q_D(EditActionsHandlerRegistry);
if (d->currentHandler) {
return d->currentHandler->move(direction, offset);
}
return false;
}
void EditActionsHandlerRegistryPrivate::updateCurrentHandler() {
Q_Q(EditActionsHandlerRegistry);
EditActionsHandler *newCurrentHandler = nullptr;
// Find the last handler in the list
if (!handlers.isEmpty()) {
newCurrentHandler = handlers.last();
}
if (currentHandler != newCurrentHandler) {
currentHandler = newCurrentHandler;
// Emit signals for property changes
emit q->enabledActionsChanged(q->enabledActions());
emit q->enabledMoveDirectionsChanged(q->enabledMoveDirections());
}
}
void EditActionsHandlerRegistryPrivate::connectHandler(EditActionsHandler *handler) {
Q_Q(EditActionsHandlerRegistry);
if (!handler) {
return;
}
// Connect signals to update when the handler's properties change
QObject::connect(handler, &EditActionsHandler::enabledActionsChanged, q, [this, q]() {
if (currentHandler == qobject_cast<EditActionsHandler *>(q->sender())) {
emit q->enabledActionsChanged(q->enabledActions());
}
});
QObject::connect(handler, &EditActionsHandler::enabledMoveDirectionsChanged, q, [this, q]() {
if (currentHandler == qobject_cast<EditActionsHandler *>(q->sender())) {
emit q->enabledMoveDirectionsChanged(q->enabledMoveDirections());
}
});
// Handle handler destruction
QObject::connect(handler, &QObject::destroyed, q, [this](QObject *obj) {
onHandlerDestroyed(obj);
});
}
void EditActionsHandlerRegistryPrivate::disconnectHandler(EditActionsHandler *handler) {
if (handler) {
QObject::disconnect(handler, nullptr, q_ptr, nullptr);
}
}
void EditActionsHandlerRegistryPrivate::onHandlerDestroyed(QObject *handler) {
// Find and remove the destroyed handler from the list
auto destroyedHandler = static_cast<EditActionsHandler *>(handler);
handlers.removeAll(destroyedHandler);
// Update current handler if needed
if (currentHandler == destroyedHandler) {
updateCurrentHandler();
}
}
}